Skip to content

Commit

Permalink
SLCORE-1142 Improve Git blaming for the analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-knize-sonarsource committed Feb 14, 2025
1 parent 1844de1 commit c0d21fe
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class GitUtils {
private static final String MINIMUM_REQUIRED_GIT_VERSION = "2.24.0";
private static final Pattern whitespaceRegex = Pattern.compile("\\s+");
private static final Pattern semanticVersionDelimiter = Pattern.compile("\\.");
private static final String BLAME_HISTORY_WINDOW = "--since='6 months ago'";

// So we only have to make the expensive call once (or at most twice) to get the native Git executable!
private static boolean checkedForNativeGitExecutable = false;
Expand Down Expand Up @@ -103,7 +104,7 @@ public static SonarLintBlameResult getBlameResult(Path projectBaseDir, Set<Path>
return getBlameResult(projectBaseDir, projectBaseRelativeFilePaths, fileContentProvider, GitUtils::checkIfEnabled);
}

public static SonarLintBlameResult getBlameResult(Path projectBaseDir, Set<Path> projectBaseRelativeFilePaths, @Nullable UnaryOperator<String> fileContentProvider,
static SonarLintBlameResult getBlameResult(Path projectBaseDir, Set<Path> projectBaseRelativeFilePaths, @Nullable UnaryOperator<String> fileContentProvider,
Predicate<Path> isEnabled) {
if (isEnabled.test(projectBaseDir)) {
LOG.debug("Using native git blame");
Expand Down Expand Up @@ -201,7 +202,7 @@ public static SonarLintBlameResult blameFromNativeCommand(Path projectBaseDir, S
for (var relativeFilePath : projectBaseRelativeFilePaths) {
try {
return parseBlameOutput(executeGitCommand(projectBaseDir,
nativeExecutable, "blame", projectBaseDir.resolve(relativeFilePath).toString(), "--line-porcelain", "--encoding=UTF-8"),
nativeExecutable, "blame", BLAME_HISTORY_WINDOW, projectBaseDir.resolve(relativeFilePath).toString(), "--line-porcelain", "--encoding=UTF-8"),
projectBaseDir.resolve(relativeFilePath).toString().replace("\\", "/"), projectBaseDir);
} catch (IOException e) {
throw new IllegalStateException("Failed to blame repository files", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TimeZone;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -55,9 +58,11 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.sonarsource.sonarlint.core.commons.testutils.GitUtils.addFileToGitIgnoreAndCommit;
import static org.sonarsource.sonarlint.core.commons.testutils.GitUtils.commit;
import static org.sonarsource.sonarlint.core.commons.testutils.GitUtils.commitAtDate;
import static org.sonarsource.sonarlint.core.commons.testutils.GitUtils.createFile;
import static org.sonarsource.sonarlint.core.commons.testutils.GitUtils.createRepository;
import static org.sonarsource.sonarlint.core.commons.testutils.GitUtils.modifyFile;
import static org.sonarsource.sonarlint.core.commons.util.git.GitUtils.blameFromNativeCommand;
import static org.sonarsource.sonarlint.core.commons.util.git.GitUtils.blameWithFilesGitCommand;
import static org.sonarsource.sonarlint.core.commons.util.git.GitUtils.getBlameResult;
import static org.sonarsource.sonarlint.core.commons.util.git.GitUtils.getVSCChangedFiles;
Expand Down Expand Up @@ -350,6 +355,46 @@ void git_blame_works_for_bare_repos_too() {
assertThat(sonarLintBlameResult.getLatestChangeDateForLinesInFile(Path.of("fileB"), List.of(3))).isEmpty();
}

@Test
void it_should_blame_file_since_provided_period() throws IOException, GitAPIException {
var calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.add(Calendar.YEAR, -1);
var fileAStr = "fileA";
createFile(projectDirPath, fileAStr, "line1");
var yearAgo = calendar.toInstant();
// initial commit 1 year ago
commitAtDate(git, yearAgo, fileAStr);
var lines = new String[3];

// second commit 4 months after initial commit
calendar.add(Calendar.MONTH, 4);
lines[0] = "line1";
lines[1] = "line2";
var eightMonthsAgo = calendar.toInstant();
modifyFile(projectDirPath.resolve(fileAStr), lines);
commitAtDate(git, eightMonthsAgo, fileAStr);

// third commit 4 months after second commit
calendar.add(Calendar.MONTH, 4);
lines[2] = "line3";
var fourMonthsAgo = calendar.toInstant();
modifyFile(projectDirPath.resolve(fileAStr), lines);
commitAtDate(git, fourMonthsAgo, fileAStr);
var fileA = Path.of(fileAStr);

var blameResult = blameFromNativeCommand(projectDirPath, Set.of(fileA));

var line1Date = blameResult.getLatestChangeDateForLinesInFile(fileA, List.of(1)).get();
var line2Date = blameResult.getLatestChangeDateForLinesInFile(fileA, List.of(2)).get();
var line3Date = blameResult.getLatestChangeDateForLinesInFile(fileA, List.of(3)).get();
// line 1 was committed 1 year ago but should have commit date of the first commit earlier than blame time window - 6 months ago
assertThat(ChronoUnit.DAYS.between(line2Date.toInstant(), line1Date.toInstant())).isZero();
// line 2 was committed 8 months ago, it's outside the blame time window, but it's a first commit outside the range, so it has real commit date
assertThat(ChronoUnit.MINUTES.between(line2Date.toInstant(), eightMonthsAgo)).isZero();
// line 3 was committed 4 months ago, it's inside the blame time window, so it has real commit date
assertThat(ChronoUnit.MINUTES.between(line3Date.toInstant(), fourMonthsAgo)).isZero();
}

private static void setUpBareRepo(Map<String, String> filePathContentMap) throws IOException, GitAPIException {
bareRepoPath = Files.createTempDirectory("bare-repo");
workingRepoPath = Files.createTempDirectory("working-repo");
Expand Down

0 comments on commit c0d21fe

Please sign in to comment.