Skip to content

Commit

Permalink
Add the original lineNumber to blameResult in order to facilitate the…
Browse files Browse the repository at this point in the history
… benchmarking
  • Loading branch information
pouryafard75 committed Aug 30, 2024
1 parent 34fa615 commit 3b526ba
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void diff(EnumMap<BlamerFactory, List<LineBlameResult>> blameResults) {
for (Map.Entry<BlamerFactory, List<LineBlameResult>> blamerFactoryListEntry : blameResults.entrySet()) {
for (LineBlameResult result : blamerFactoryListEntry.getValue()) {
if (result != null) {
int lineNumber = result.getLineNumber();
int lineNumber = result.getOriginalLineNumber();
this.register(blamerFactoryListEntry.getKey(), lineNumber, result.getShortCommitId());
}
}
Expand Down
28 changes: 15 additions & 13 deletions src/main/java/org/codetracker/blame/impl/CliGitBlame.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public List<LineBlameResult> blameFile(Repository repository, String commitId, S

try {
// Construct the git blame command with the commit and file path
String[] command = {"git", "blame", "-w", "--follow", commitId, "--", filePath};
String[] command = {"git", "blame", "-n", "-w", "--follow", commitId, "--", filePath};
ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.directory(repository.getDirectory());

Expand All @@ -34,15 +34,24 @@ public List<LineBlameResult> blameFile(Repository repository, String commitId, S
String line;
int lineNumber = 1;
while ((line = reader.readLine()) != null) {
// Example output line (simplified for parsing):
// CommitID (<Author> <Date>) <FilePath> <Code>
// e.g., abc12345 (John Doe 2022-07-01 12:34:56 +0000) <old/file/path> line of code

// Extract commitId, committer, commitDate, filePath, and beforeFilePath from the blame line
String[] parts = line.split("\\s+", 3);
String blameCommitId = parts[0].trim(); // Extract the commit ID
String prevFilePath = parts[1].trim();

// Find the index of the first space to separate the number
int firstSpaceIndex = parts[2].indexOf(" ");
int resultLineNumber = -1;
try {
resultLineNumber = Integer.parseInt(parts[2].substring(0, firstSpaceIndex));
}
catch (NumberFormatException e) {

}

parts[2] = parts[2].substring(firstSpaceIndex + 1);

String[] meta = parts[2].split("\\s{2,}"); // Split on at least 2 spaces
String commiter = "";
long commitTime = 0;
Expand All @@ -68,7 +77,7 @@ public List<LineBlameResult> blameFile(Repository repository, String commitId, S
Instant instant = offsetDateTime.toInstant();
commitTime = instant.toEpochMilli() / 1000;
}
LineBlameResult result = new LineBlameResult(blameCommitId, filePath, prevFilePath, commiter, commitTime, lineNumber);
LineBlameResult result = new LineBlameResult(blameCommitId, filePath, prevFilePath, commiter, commitTime, resultLineNumber, lineNumber);
blameResults.add(result);


Expand All @@ -81,7 +90,7 @@ public List<LineBlameResult> blameFile(Repository repository, String commitId, S
}
} finally {
if (process != null) {
process.destroy();
process.destroy();
}
}

Expand All @@ -94,11 +103,4 @@ public List<LineBlameResult> blameFile(Repository repository, String commitId, S
// This method is optional and can be implemented based on the requirements
throw new UnsupportedOperationException("Not implemented");
}

private String extractOriginalFilePath(String metadata) {
// Implement logic to parse and extract the original file path if the file was renamed
// This logic will vary based on how the metadata is structured in your git blame output
return metadata.contains("<") && metadata.contains(">") ? metadata.substring(metadata.indexOf("<") + 1, metadata.indexOf(">")) : null;
}

}
27 changes: 19 additions & 8 deletions src/main/java/org/codetracker/blame/model/LineBlameResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public class LineBlameResult {
private final String beforeFilePath;
private final String committer;
private final long commitDate;
private final int lineNumber;
private final int resultLineNumber;

private final int originalLineNumber; //This is the original line number (the one that you pass as the input)

@Override
public String toString() {
Expand All @@ -23,18 +25,19 @@ public String toString() {
", beforeFilePath='" + beforeFilePath + '\'' +
", committer='" + committer + '\'' +
", commitDate='" + commitDate + '\'' +
", lineNumber=" + lineNumber +
", lineNumber=" + resultLineNumber +
'}';
}

public LineBlameResult(String commitId, String filePath, String beforeFilePath, String committer, long commitDate, int lineNumber) {
public LineBlameResult(String commitId, String filePath, String beforeFilePath, String committer, long commitDate, int resultLineNumber, int originalLineNumber) {
this.commitId = commitId;
this.filePath = filePath;
this.shortCommitId = commitId.substring(0, 9);
this.beforeFilePath = beforeFilePath;
this.committer = committer;
this.commitDate = commitDate;
this.lineNumber = lineNumber;
this.resultLineNumber = resultLineNumber;
this.originalLineNumber = originalLineNumber;
}

public String getCommitId() {
Expand All @@ -61,24 +64,32 @@ public long getCommitDate() {
return commitDate;
}

public int getLineNumber() {
return lineNumber;
public int getResultLineNumber() {
return resultLineNumber;
}

public int getOriginalLineNumber() {
return originalLineNumber;
}
public static LineBlameResult of(History.HistoryInfo<? extends CodeElement> latestChange, int lineNumber){

public static LineBlameResult of(History.HistoryInfo<? extends CodeElement> latestChange, int lineNumber) {
if (latestChange == null) return null;
int resultLineNumber = latestChange.getElementAfter().getLocation().getStartLine();
return new LineBlameResult(latestChange.getCommitId(),
latestChange.getElementAfter().getFilePath(),
latestChange.getElementBefore().getFilePath(),
latestChange.getCommitterName(),
latestChange.getCommitTime(),
resultLineNumber,
lineNumber);
}
public static LineBlameResult of(BlameResult blameResult, int i) {
if (blameResult == null || i < 0 || i >= blameResult.getResultContents().size()) return null;
String commitId = blameResult.getSourceCommit(i).getId().name();
String committerName = blameResult.getSourceCommit(i).getAuthorIdent().getName();
int resultLineNumber = blameResult.getSourceLine(i);
long commitTime = blameResult.getSourceCommit(i).getCommitTime();
String filePath = blameResult.getSourcePath(i);
return new LineBlameResult(commitId, blameResult.getResultPath(), filePath, committerName, commitTime, i + 1);
return new LineBlameResult(commitId, blameResult.getResultPath(), filePath, committerName, commitTime, resultLineNumber, i + 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public void testGitBlame() throws Exception {
String path = "javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/contexts/MethodCallExprContext.java";
String jgit = getBlameOutput(url, path, new JGitBlame(), REPOS_PATH, gitService);
String cgit = getBlameOutput(url, path, new CliGitBlame(), REPOS_PATH, gitService);

Files.write(Path.of("jgit.txt"), jgit.getBytes());
// Files.write(Path.of("cgit.txt"), cgit.getBytes());
Files.write(Path.of("cgit.txt"), cgit.getBytes());

}

Expand Down

0 comments on commit 3b526ba

Please sign in to comment.