Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-knize-sonarsource committed Feb 14, 2025
1 parent 5e69d12 commit bd862b3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ static SonarLintBlameResult getBlameResult(Path projectBaseDir, Set<Path> projec
Predicate<Path> isEnabled) {
if (isEnabled.test(projectBaseDir)) {
LOG.debug("Using native git blame");
return blameFromNativeCommand(projectBaseDir, projectBaseRelativeFilePaths, BLAME_HISTORY_WINDOW);
return blameFromNativeCommand(projectBaseDir, projectBaseRelativeFilePaths);
} else {
LOG.debug("Falling back to JGit git blame");
return blameWithFilesGitCommand(projectBaseDir, projectBaseRelativeFilePaths, fileContentProvider);
Expand Down Expand Up @@ -196,13 +196,13 @@ private static String executeGitCommand(Path workingDir, String... command) thro
return outputStr;
}

public static SonarLintBlameResult blameFromNativeCommand(Path projectBaseDir, Set<Path> projectBaseRelativeFilePaths, String blameHistoryWindow) {
public static SonarLintBlameResult blameFromNativeCommand(Path projectBaseDir, Set<Path> projectBaseRelativeFilePaths) {
var nativeExecutable = getNativeGitExecutable();
if (nativeExecutable != null) {
for (var relativeFilePath : projectBaseRelativeFilePaths) {
try {
return parseBlameOutput(executeGitCommand(projectBaseDir,
nativeExecutable, "blame", projectBaseDir.resolve(relativeFilePath).toString(), blameHistoryWindow, "--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,6 +23,7 @@
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;
Expand Down Expand Up @@ -356,29 +357,42 @@ void git_blame_works_for_bare_repos_too() {

@Test
void it_should_blame_file_since_provided_period() throws IOException, GitAPIException {
// Create backdated commit metadata
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
//calendar.add(Calendar.YEAR, -1); // Subtract 1 year
createFile(projectDirPath, "fileA", "line1", "line2", "line3");
var c1 = commitAtDate(git, calendar.toInstant(), "fileA");
addCommitsHistory();

var sonarLintBlameResult = blameFromNativeCommand(projectDirPath, Set.of(Path.of("fileA")), "--since='6 months ago'");
assertThat(IntStream.of(1, 2, 3)
.mapToObj(lineNumber -> sonarLintBlameResult.getLatestChangeDateForLinesInFile(Path.of("fileA"), List.of(lineNumber))))
.map(Optional::get)
.allMatch(date -> date.equals(c1));
}

void addCommitsHistory() throws IOException, GitAPIException {
var lines = new String[365];
for (int i = 0; i < 365; i++) {
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.add(Calendar.DAY_OF_YEAR, -i);
lines[i] = "line" + i;
modifyFile(projectDirPath.resolve("fileA"), lines);
commitAtDate(git, calendar.toInstant(), "fileA");
}
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 {
Expand Down

0 comments on commit bd862b3

Please sign in to comment.