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.

XooScmProvider.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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 java.io.IOException;
  22. import java.nio.charset.StandardCharsets;
  23. import java.nio.file.FileVisitResult;
  24. import java.nio.file.Files;
  25. import java.nio.file.Path;
  26. import java.nio.file.SimpleFileVisitor;
  27. import java.nio.file.attribute.BasicFileAttributes;
  28. import java.util.Collections;
  29. import java.util.HashMap;
  30. import java.util.HashSet;
  31. import java.util.List;
  32. import java.util.Map;
  33. import java.util.Set;
  34. import javax.annotation.CheckForNull;
  35. import org.apache.commons.lang.StringUtils;
  36. import org.sonar.api.batch.scm.BlameCommand;
  37. import org.sonar.api.batch.scm.IgnoreCommand;
  38. import org.sonar.api.batch.scm.ScmProvider;
  39. import java.io.File;
  40. import org.sonar.api.utils.log.Logger;
  41. import org.sonar.api.utils.log.Loggers;
  42. public class XooScmProvider extends ScmProvider {
  43. private static final Logger LOG = Loggers.get(XooScmProvider.class);
  44. private static final String SCM_EXTENSION = ".scm";
  45. private final XooBlameCommand blame;
  46. private XooIgnoreCommand ignore;
  47. public XooScmProvider(XooBlameCommand blame, XooIgnoreCommand ignore) {
  48. this.blame = blame;
  49. this.ignore = ignore;
  50. }
  51. @Override
  52. public boolean supports(File baseDir) {
  53. return new File(baseDir, ".xoo").exists();
  54. }
  55. @Override
  56. public String key() {
  57. return "xoo";
  58. }
  59. @Override
  60. public BlameCommand blameCommand() {
  61. return blame;
  62. }
  63. @Override
  64. public IgnoreCommand ignoreCommand() {
  65. return ignore;
  66. }
  67. @Override
  68. public String revisionId(Path path) {
  69. return "fakeSha1FromXoo";
  70. }
  71. @CheckForNull
  72. public Set<Path> branchChangedFiles(String targetBranchName, Path rootBaseDir) {
  73. Set<Path> changedFiles = new HashSet<>();
  74. Set<Path> scmFiles = new HashSet<>();
  75. try {
  76. Files.walkFileTree(rootBaseDir, new SimpleFileVisitor<Path>() {
  77. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
  78. if (file.getFileName().toString().endsWith(".scm")) {
  79. scmFiles.add(file);
  80. }
  81. return FileVisitResult.CONTINUE;
  82. }
  83. });
  84. if (scmFiles.isEmpty()) {
  85. return null;
  86. }
  87. for (Path scmFilePath : scmFiles) {
  88. String fileName = scmFilePath.getFileName().toString();
  89. Path filePath = scmFilePath.resolveSibling(fileName.substring(0, fileName.length() - 4));
  90. if (!Files.exists(filePath)) {
  91. continue;
  92. }
  93. Set<Integer> newLines = loadNewLines(scmFilePath);
  94. if (newLines == null) {
  95. return null;
  96. }
  97. if (!newLines.isEmpty()) {
  98. changedFiles.add(filePath);
  99. }
  100. }
  101. } catch (IOException e) {
  102. throw new IllegalStateException("Failed to find scm files", e);
  103. }
  104. return changedFiles;
  105. }
  106. @CheckForNull
  107. public Map<Path, Set<Integer>> branchChangedLines(String targetBranchName, Path rootBaseDir, Set<Path> files) {
  108. Map<Path, Set<Integer>> map = new HashMap<>();
  109. for (Path filePath : files) {
  110. Path scmFilePath = filePath.resolveSibling(filePath.getFileName() + SCM_EXTENSION);
  111. try {
  112. Set<Integer> newLines = loadNewLines(scmFilePath);
  113. if (newLines != null && !newLines.isEmpty()) {
  114. map.put(filePath, newLines);
  115. }
  116. } catch (IOException e) {
  117. throw new IllegalStateException("Failed to parse scm file", e);
  118. }
  119. }
  120. return map.isEmpty() ? null : map;
  121. }
  122. // return null when any of the scm files lack the is-new flag (column 4)
  123. @CheckForNull
  124. private Set<Integer> loadNewLines(Path filePath) throws IOException {
  125. if (!Files.isRegularFile(filePath)) {
  126. return Collections.emptySet();
  127. }
  128. LOG.debug("Processing " + filePath.toAbsolutePath());
  129. Set<Integer> newLines = new HashSet<>();
  130. List<String> lines = Files.readAllLines(filePath, StandardCharsets.UTF_8);
  131. int lineNum = 0;
  132. boolean foundNewLineFlag = false;
  133. for (String line : lines) {
  134. lineNum++;
  135. String[] fields = StringUtils.splitPreserveAllTokens(line, ',');
  136. if (fields.length < 4) {
  137. continue;
  138. }
  139. foundNewLineFlag = true;
  140. if (Boolean.parseBoolean(fields[3])) {
  141. newLines.add(lineNum);
  142. }
  143. }
  144. return foundNewLineFlag ? newLines : null;
  145. }
  146. }