You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XooBlameCommand.java 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.xoo.scm;
  21. import com.google.common.annotations.VisibleForTesting;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.nio.charset.StandardCharsets;
  25. import java.util.ArrayList;
  26. import java.util.Date;
  27. import java.util.List;
  28. import org.apache.commons.io.FileUtils;
  29. import org.apache.commons.lang.StringUtils;
  30. import org.sonar.api.batch.fs.InputFile;
  31. import org.sonar.api.batch.scm.BlameCommand;
  32. import org.sonar.api.batch.scm.BlameLine;
  33. import org.sonar.api.utils.DateUtils;
  34. public class XooBlameCommand extends BlameCommand {
  35. private static final String SCM_EXTENSION = ".scm";
  36. @Override
  37. public void blame(BlameInput input, BlameOutput result) {
  38. for (InputFile inputFile : input.filesToBlame()) {
  39. processFile(inputFile, result);
  40. }
  41. }
  42. @VisibleForTesting
  43. protected void processFile(InputFile inputFile, BlameOutput result) {
  44. File ioFile = inputFile.file();
  45. File scmDataFile = new java.io.File(ioFile.getParentFile(), ioFile.getName() + SCM_EXTENSION);
  46. if (!scmDataFile.exists()) {
  47. return;
  48. }
  49. try {
  50. List<String> lines = FileUtils.readLines(scmDataFile, StandardCharsets.UTF_8);
  51. List<BlameLine> blame = new ArrayList<>(lines.size());
  52. int lineNumber = 0;
  53. for (String line : lines) {
  54. lineNumber++;
  55. if (StringUtils.isNotBlank(line)) {
  56. // revision,author,dateTime
  57. String[] fields = StringUtils.splitPreserveAllTokens(line, ',');
  58. if (fields.length < 3) {
  59. throw new IllegalStateException("Not enough fields on line " + lineNumber);
  60. }
  61. String revision = StringUtils.trimToNull(fields[0]);
  62. String author = StringUtils.trimToNull(fields[1]);
  63. BlameLine blameLine = new BlameLine().revision(revision).author(author);
  64. String dateStr = StringUtils.trimToNull(fields[2]);
  65. if (dateStr != null) {
  66. Date dateTime = DateUtils.parseDateTimeQuietly(dateStr);
  67. if (dateTime != null) {
  68. blameLine.date(dateTime);
  69. } else {
  70. // Will throw an exception, when date is not in format "yyyy-MM-dd"
  71. blameLine.date(DateUtils.parseDate(dateStr));
  72. }
  73. }
  74. blame.add(blameLine);
  75. }
  76. }
  77. result.blameResult(inputFile, blame);
  78. } catch (IOException e) {
  79. throw new IllegalStateException(e);
  80. }
  81. }
  82. }