Skip to content

Commit

Permalink
Comment blame support
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Jul 8, 2024
1 parent 74c8a79 commit 25cfc46
Show file tree
Hide file tree
Showing 9 changed files with 798 additions and 13 deletions.
337 changes: 337 additions & 0 deletions src/main/java/org/codetracker/CommentTrackerChangeHistory.java

Large diffs are not rendered by default.

337 changes: 337 additions & 0 deletions src/main/java/org/codetracker/CommentTrackerImpl.java

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/main/java/org/codetracker/api/CodeTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ static BlockTracker.Builder blockTracker() {
return new BlockTracker.Builder();
}

static CommentTracker.Builder commentTracker() {
return new CommentTracker.Builder();
}

static ClassTracker.Builder classTracker() {
return new ClassTracker.Builder();
}
Expand Down
80 changes: 80 additions & 0 deletions src/main/java/org/codetracker/api/CommentTracker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.codetracker.api;

import org.codetracker.CommentTrackerImpl;
import org.codetracker.element.Comment;
import org.eclipse.jgit.lib.Repository;

import gr.uom.java.xmi.LocationInfo.CodeElementType;

public interface CommentTracker extends CodeTracker {
default History.HistoryInfo<Comment> blame() throws Exception{
throw new UnsupportedOperationException();
}

class Builder {
private Repository repository;
private String gitURL;
private String startCommitId;
private String filePath;
private String methodName;
private int methodDeclarationLineNumber;
private CodeElementType codeElementType;
private int commentStartLineNumber;
private int commentEndLineNumber;

public Builder repository(Repository repository) {
this.repository = repository;
return this;
}

public Builder gitURL(String gitURL) {
this.gitURL = gitURL;
return this;
}

public Builder startCommitId(String startCommitId) {
this.startCommitId = startCommitId;
return this;
}

public Builder filePath(String filePath) {
this.filePath = filePath;
return this;
}

public Builder methodName(String methodName) {
this.methodName = methodName;
return this;
}

public Builder methodDeclarationLineNumber(int methodDeclarationLineNumber) {
this.methodDeclarationLineNumber = methodDeclarationLineNumber;
return this;
}

public Builder codeElementType(CodeElementType codeElementType) {
this.codeElementType = codeElementType;
return this;
}

public Builder commentStartLineNumber(int commentStartLineNumber) {
this.commentStartLineNumber = commentStartLineNumber;
return this;
}

public Builder commentEndLineNumber(int commentEndLineNumber) {
this.commentEndLineNumber = commentEndLineNumber;
return this;
}

private void checkInput() {

}

public CommentTracker build() {
checkInput();
return new CommentTrackerImpl(repository, startCommitId, filePath, methodName, methodDeclarationLineNumber,
codeElementType, commentStartLineNumber, commentEndLineNumber);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.codetracker.element.Attribute;
import org.codetracker.element.Block;
import org.codetracker.element.Class;
import org.codetracker.element.Comment;
import org.codetracker.element.Method;
import org.eclipse.jgit.lib.Repository;

Expand Down Expand Up @@ -73,6 +74,23 @@ public History.HistoryInfo<? extends CodeElement> blame(
.build();
blame = blockTracker.blame();
break;
case "Comment":
Comment comment = (Comment) codeElement;
CommentTracker commentTracker = CodeTracker
.commentTracker()
.repository(repository)
.filePath(filePath)
.startCommitId(commitId)
.methodName(comment.getOperation().get().getName())
.methodDeclarationLineNumber(
comment.getOperation().get().getLocationInfo().getStartLine()
)
.codeElementType(codeElement.getLocation().getCodeElementType())
.commentStartLineNumber(codeElement.getLocation().getStartLine())
.commentEndLineNumber(codeElement.getLocation().getEndLine())
.build();
blame = commentTracker.blame();
break;
default:
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/codetracker/blame/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static List<String> getFileContentByCommit(Repository repository, String
}
public static String getBlameOutput(String url, String filePath, IBlame blamer, String reposPath, GitService gitService) throws Exception {
String commitId = URLHelper.getCommit(url);
Repository repository = gitService.cloneIfNotExists(reposPath + "/" + getProject(url), URLHelper.getRepo(url));
Repository repository = gitService.cloneIfNotExists(reposPath + "/" + getOwner(url) + "/" + getProject(url), URLHelper.getRepo(url));
List<String> lines = getFileContentByCommit(repository, commitId, filePath);
BlameFormatter formatter = new BlameFormatter(lines);
List<LineBlameResult> blameResult = apply(commitId, filePath, blamer, repository);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/codetracker/element/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ public Optional<UMLAbstractClass> getClazz() {
return clazz != null ? Optional.of(clazz) : Optional.empty();
}

public static Comment of(UMLComment comment, VariableDeclarationContainer operation, Version version) {
Method method = Method.of(operation, version);
return of(comment, method);
}

public static Comment of(UMLComment comment, UMLAbstractClass clazz, Version version) {
Class method = Class.of(clazz, version);
return of(comment, method);
}

public static Comment of(UMLComment comment, Class clazz) {
LocationInfo commentLocationInfo = comment.getLocationInfo();
String statementType = commentLocationInfo.getCodeElementType().name();
Expand Down
19 changes: 9 additions & 10 deletions src/test/java/org/codetracker/blame/CodeTrackerBlameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.stream.Stream;

import static org.codetracker.blame.util.Utils.getBlameOutput;
import static org.codetracker.blame.util.Utils.getOwner;
import static org.codetracker.blame.util.Utils.getProject;

/* Created by pourya on 2024-06-26*/
Expand Down Expand Up @@ -72,7 +73,7 @@ public void blameTestSingleLine() throws Exception {
String expected = "LineBlameResult{commitId='ae3a4f8a50b1d25b2d3db50495a50feaa0b2b872', filePath='src/main/java/dat/MakeIntels.java', shortCommitId='ae3a4f8a5', beforeFilePath='src/main/java/dat/Make.java', committer='Pouryafard75', commitDate='1719280029', lineNumber=18}";

String commitId = URLHelper.getCommit(url);
Repository repository = gitService.cloneIfNotExists(REPOS_PATH + "/" + getProject(url), URLHelper.getRepo(url));
Repository repository = gitService.cloneIfNotExists(REPOS_PATH + "/" + getOwner(url) + "/" + getProject(url), URLHelper.getRepo(url));
History.HistoryInfo<? extends CodeElement> lineBlame =
new CodeTrackerBlame().getLineBlame(repository, commitId, filePath, lineNumber);
LineBlameResult lineBlameResult = LineBlameResult.of(lineBlame);
Expand All @@ -84,30 +85,28 @@ public void blameTestSingleLine2() throws Exception {
String url = "https://github.com/pouryafard75/DiffBenchmark/commit/5b33dc6f8cfcf8c0e31966c035b0406eca97ec76";
String filePath = "src/main/java/dat/MakeIntels.java";
int lineNumber = 24;
String expected = "LineBlameResult{commitId='ae3a4f8a50b1d25b2d3db50495a50feaa0b2b872', filePath='src/main/java/dat/MakeIntels.java', shortCommitId='ae3a4f8a5', beforeFilePath='src/main/java/dat/Make.java', committer='Pouryafard75', commitDate='1719280029', lineNumber=18}";
String expected = "LineBlameResult{commitId='e5e209d526b49544b2fb899bde8856290cf209a0', filePath='src/main/java/dat/Make.java', shortCommitId='e5e209d52', beforeFilePath='src/main/java/dat/Make.java', committer='Pouryafard75', commitDate='1709775227', lineNumber=20}";

String commitId = URLHelper.getCommit(url);
Repository repository = gitService.cloneIfNotExists(REPOS_PATH + "/" + getProject(url), URLHelper.getRepo(url));
Repository repository = gitService.cloneIfNotExists(REPOS_PATH + "/" + getOwner(url) + "/" + getProject(url), URLHelper.getRepo(url));
History.HistoryInfo<? extends CodeElement> lineBlame =
new CodeTrackerBlame().getLineBlame(repository, commitId, filePath, lineNumber);
LineBlameResult lineBlameResult = LineBlameResult.of(lineBlame);
// Assertions.assertEquals(expected, lineBlameResult.toString()); //TODO: Update this one
Assertions.assertEquals(expected, lineBlameResult.toString());
}

@Test
public void blameTestSingleLine_3() throws Exception {
public void blameTestSingleLine3() throws Exception {
String url = "https://github.com/Alluxio/alluxio/commit/9aeefcd8120bb3b89cdb437d8c32d2ed84b8a825";
String filePath = "servers/src/main/java/tachyon/worker/block/allocator/MaxFreeAllocator.java";
int lineNumber = 78;
String expected = "???";
String expected = "LineBlameResult{commitId='68514f3fe653a87899b0e0e7c9d2e67c85afefe0', filePath='servers/src/main/java/tachyon/worker/block/allocator/MaxFreeAllocator.java', shortCommitId='68514f3fe', beforeFilePath='servers/src/main/java/tachyon/worker/block/allocator/MaxFreeAllocator.java', committer='Carson Wang', commitDate='1435366348', lineNumber=49}";

String commitId = URLHelper.getCommit(url);
Repository repository = gitService.cloneIfNotExists(REPOS_PATH + "/" + getProject(url), URLHelper.getRepo(url));
Repository repository = gitService.cloneIfNotExists(REPOS_PATH + "/" + getOwner(url) + "/" + getProject(url), URLHelper.getRepo(url));
History.HistoryInfo<? extends CodeElement> lineBlame =
new CodeTrackerBlame().getLineBlame(repository, commitId, filePath, lineNumber);
LineBlameResult lineBlameResult = LineBlameResult.of(lineBlame);
// Assertions.assertEquals(expected, lineBlameResult.toString()); //TODO: Update this one
Assertions.assertEquals(expected, lineBlameResult.toString());
}


}
4 changes: 2 additions & 2 deletions src/test/resources/blame/blameTestWithLocalRepo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ e5e209d52 src/main/java/dat/Make.java (Pouryafard75 2024-03-06 20:33:47 -0
e5e209d52 src/main/java/dat/Make.java (Pouryafard75 2024-03-06 20:33:47 -0500 31) case_count++;
e5e209d52 src/main/java/dat/Make.java (Pouryafard75 2024-03-06 20:33:47 -0500 32) ProjectASTDiff projectASTDiff = runWhatever(info.getRepo(), info.getCommit());
e5e209d52 src/main/java/dat/Make.java (Pouryafard75 2024-03-06 20:33:47 -0500 33) for (ASTDiff rm_astDiff : projectASTDiff.getDiffSet()) {
e5e209d52 src/main/java/dat/Make.java (Pouryafard75 2024-03-06 20:33:47 -0500 34) // logger.info("Working on " + info.makeURL() + " " + rm_astDiff.getSrcPath());
ba790bfa4 src/main/java/dat/Make.java (Pouryafard75 2024-03-06 20:33:48 -0500 34) // logger.info("Working on " + info.makeURL() + " " + rm_astDiff.getSrcPath());
e5e209d52 src/main/java/dat/Make.java (Pouryafard75 2024-03-06 20:33:47 -0500 35) logger.info("Case " + case_count + "/" + configuration.getAllCases().size());
e5e209d52 src/main/java/dat/Make.java (Pouryafard75 2024-03-06 20:33:47 -0500 36) GridSearch dat = new GridSearch(info, projectASTDiff, rm_astDiff, configuration);
ba790bfa4 src/main/java/dat/Make.java (Pouryafard75 2024-03-06 20:33:48 -0500 37) intels.addAll(dat.run(numThreads));
e5e209d52 src/main/java/dat/Make.java (Pouryafard75 2024-03-06 20:33:47 -0500 38) }
ae3a4f8a5 src/main/java/dat/MakeIntels.java (Pouryafard75 2024-06-24 21:47:09 -0400 39) intelDao.insertIntels(intels);
e5e209d52 src/main/java/dat/Make.java (Pouryafard75 2024-03-06 20:33:47 -0500 40) // if (case_count == 2) break;
5b33dc6f8 src/main/java/dat/MakeIntels.java (Pouryafard75 2024-06-25 01:09:18 -0400 40) // if (case_count == 2) break;
e5e209d52 src/main/java/dat/Make.java (Pouryafard75 2024-03-06 20:33:47 -0500 41)
e5e209d52 src/main/java/dat/Make.java (Pouryafard75 2024-03-06 20:33:47 -0500 42) }
ba790bfa4 src/main/java/dat/Make.java (Pouryafard75 2024-03-06 20:33:48 -0500 43) }
Expand Down

0 comments on commit 25cfc46

Please sign in to comment.