diff --git a/src/main/java/org/codetracker/blame/benchmark/LineNumberToCommitIDRecordManager.java b/src/main/java/org/codetracker/blame/benchmark/LineNumberToCommitIDRecordManager.java index 5cf193ba723..8ae69ba3a4e 100644 --- a/src/main/java/org/codetracker/blame/benchmark/LineNumberToCommitIDRecordManager.java +++ b/src/main/java/org/codetracker/blame/benchmark/LineNumberToCommitIDRecordManager.java @@ -15,7 +15,7 @@ public void diff(EnumMap> blameResults) { for (Map.Entry> 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()); } } diff --git a/src/main/java/org/codetracker/blame/impl/CliGitBlame.java b/src/main/java/org/codetracker/blame/impl/CliGitBlame.java index 9a85b9a3c1d..410db9e271a 100644 --- a/src/main/java/org/codetracker/blame/impl/CliGitBlame.java +++ b/src/main/java/org/codetracker/blame/impl/CliGitBlame.java @@ -23,7 +23,7 @@ public List 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()); @@ -34,15 +34,24 @@ public List blameFile(Repository repository, String commitId, S String line; int lineNumber = 1; while ((line = reader.readLine()) != null) { - // Example output line (simplified for parsing): - // CommitID ( ) - // e.g., abc12345 (John Doe 2022-07-01 12:34:56 +0000) 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; @@ -68,7 +77,7 @@ public List 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); @@ -81,7 +90,7 @@ public List blameFile(Repository repository, String commitId, S } } finally { if (process != null) { - process.destroy(); + process.destroy(); } } @@ -94,11 +103,4 @@ public List 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; - } - } diff --git a/src/main/java/org/codetracker/blame/model/LineBlameResult.java b/src/main/java/org/codetracker/blame/model/LineBlameResult.java index cf84ee85592..822f8b84621 100644 --- a/src/main/java/org/codetracker/blame/model/LineBlameResult.java +++ b/src/main/java/org/codetracker/blame/model/LineBlameResult.java @@ -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() { @@ -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() { @@ -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 latestChange, int lineNumber){ + + public static LineBlameResult of(History.HistoryInfo 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); } } diff --git a/src/test/java/org/codetracker/blame/JGitBlameVerificationTest.java b/src/test/java/org/codetracker/blame/JGitBlameVerificationTest.java index 282fec93f1d..f66bffc4242 100644 --- a/src/test/java/org/codetracker/blame/JGitBlameVerificationTest.java +++ b/src/test/java/org/codetracker/blame/JGitBlameVerificationTest.java @@ -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()); }