Skip to content

Commit 3a7b84e

Browse files
committed
SLCORE-1544 Split porcelain output of git blame more robustly
1 parent d8a0492 commit 3a7b84e

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

backend/commons/src/main/java/org/sonarsource/sonarlint/core/commons/util/git/BlameParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
public class BlameParser {
3333
private static final SonarLintLogger LOG = SonarLintLogger.get();
34-
private static final String FILENAME = "filename ";
34+
private static final String FILENAME_PORCELAIN_REGEXP = "\nfilename ";
3535
private static final String AUTHOR_MAIL = "author-mail ";
3636
private static final String COMMITTER_TIME = "committer-time ";
3737
// if this text change between different git versions it will break the implementation
@@ -48,7 +48,7 @@ public static void parseBlameOutput(String blameOutput, String currentFilePath,
4848
}
4949
var currentFileBlame = new BlameResult.FileBlame(currentFilePath, numberOfLines);
5050
blameResult.getFileBlameByPath().put(currentFilePath, currentFileBlame);
51-
var lineBlameSnippets = blameOutput.split(FILENAME);
51+
var lineBlameSnippets = blameOutput.split(FILENAME_PORCELAIN_REGEXP);
5252
var currentLineNumber = 0;
5353

5454
// because of the way we split the blame output, the last snippet should be skipped
@@ -77,7 +77,7 @@ public static void parseBlameOutput(String blameOutput, String currentFilePath,
7777
}
7878

7979
public static int numberOfLinesInBlameOutput(String blameOutput) {
80-
var pattern = Pattern.compile("^" + FILENAME, Pattern.MULTILINE);
80+
var pattern = Pattern.compile(FILENAME_PORCELAIN_REGEXP, Pattern.MULTILINE);
8181
var matcher = pattern.matcher(blameOutput);
8282
var count = 0;
8383
while (matcher.find()) {

backend/commons/src/test/java/org/sonarsource/sonarlint/core/commons/util/git/BlameParserTests.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,39 @@ void shouldNotPopulateGitBlameResultForEmptyBlameOutput() {
3333

3434
assertThat(blameResult.getFileBlameByPath()).isEmpty();
3535
}
36+
37+
@Test
38+
void shouldSplitBlameOutputCorrectlyWhenLinesContainSplitPattern() {
39+
var blameResult = new BlameResult();
40+
BlameParser.parseBlameOutput("""
41+
5746f09bf53067450843eaddff52ea7b0f16cde3 1 1 2
42+
author Some One
43+
author-mail <[email protected]>
44+
author-time 1553598120
45+
author-tz +0100
46+
committer Some One
47+
committer-mail <[email protected]>
48+
committer-time 1554191055
49+
committer-tz +0200
50+
summary Initial revision
51+
previous 35c9ca0b1f41231508e706707d76ca0485b8a3ad file.txt
52+
filename file.txt
53+
First line with filename in it
54+
5746f09bf53067450843eaddff52ea7b0f16cde3 2 2
55+
author Some One
56+
author-mail <[email protected]>
57+
author-time 1553598120
58+
author-tz +0100
59+
committer Some One
60+
committer-mail <[email protected]>
61+
committer-time 1554191055
62+
committer-tz +0200
63+
summary Initial revision
64+
previous 35c9ca0b1f41231508e706707d76ca0485b8a3ad file.txt
65+
filename file.txt
66+
Second line also with filename in it
67+
""", "", blameResult);
68+
69+
assertThat(blameResult.getFileBlameByPath()).hasSize(1);
70+
}
3671
}

0 commit comments

Comments
 (0)