Skip to content

Commit

Permalink
Adopt with blame
Browse files Browse the repository at this point in the history
  • Loading branch information
pouryafard75 committed Jul 2, 2024
1 parent f689518 commit 6a81d32
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 67 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/codetracker/api/AttributeTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
public interface AttributeTracker extends CodeTracker {

History<Attribute> track() throws Exception;
default History.HistoryInfo<Attribute> blame() throws Exception{
throw new UnsupportedOperationException();
}

class Builder {
private Repository repository;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/codetracker/api/BlockTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
public interface BlockTracker extends CodeTracker {

History<Block> track() throws Exception;
default History.HistoryInfo<Block> blame() throws Exception{
throw new UnsupportedOperationException();
}

class Builder {
private Repository repository;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/codetracker/api/ClassTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
public interface ClassTracker extends CodeTracker {

History<Class> track() throws Exception;
default History.HistoryInfo<Class> blame() throws Exception{
throw new UnsupportedOperationException();
}

class Builder {
private Repository repository;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/codetracker/api/MethodTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
public interface MethodTracker extends CodeTracker {

History<Method> track() throws Exception;
default History.HistoryInfo<Method> blame() throws Exception{
throw new UnsupportedOperationException();
}

class Builder {
private Repository repository;
Expand Down
88 changes: 47 additions & 41 deletions src/main/java/org/codetracker/blame/CodeTrackerBlame.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.codetracker.blame;

import org.codetracker.api.CodeElement;
import org.codetracker.rest.changeHistory.RESTChange;
import org.codetracker.api.History;
import org.codetracker.util.CodeElementLocator;
import org.eclipse.jgit.lib.Repository;

Expand All @@ -11,7 +11,6 @@
import java.util.List;

import static org.codetracker.blame.Utils.getFileContentByCommit;
import static org.codetracker.rest.RESTHandler.trackCodeHistory;

/* Created by pourya on 2024-06-26*/
public class CodeTrackerBlame implements IBlame{
Expand All @@ -21,19 +20,57 @@ public List<String[]> blameFile(Repository repository, String commitId, String f
List<String> lines = getFileContentByCommit(repository, commitId, filePath);
int maxLine = lines.size();
List<String[]> result = new ArrayList<>();
for (int lineNumber = 0; lineNumber < maxLine; lineNumber++) {
for (int lineNumber = 1; lineNumber <= maxLine; lineNumber++) {
try {
result.add(lineBlameFormat(repository, commitId, filePath, null, lineNumber, lines));
} catch (Exception e) {
// System.out.println(e.getMessage());
}
catch (Exception e) {
// System.out.println("Error in line " + lineNumber + ": " + e.getMessage());
}

}
return result;
}

private static String[] lineBlameFormat(Repository repository, String commitId, String filePath, String name, int lineNumber, List<String> lines) {
public static String[] lineBlameFormat(Repository repository, String commitId, String filePath, String name, int lineNumber, List<String> lines) {
History.HistoryInfo<? extends CodeElement> latestChange = getLineBlame(repository, commitId, filePath, name, lineNumber);
String shortCommitId = NOT_FOUND_PLACEHOLDER;
String committer = NOT_FOUND_PLACEHOLDER;
String commitDate = NOT_FOUND_PLACEHOLDER;
String beforeFilePath = NOT_FOUND_PLACEHOLDER;
long commitTime = 0L;
if (latestChange != null) {
shortCommitId = latestChange.getCommitId().substring(0, 9); // take the first 7 characters
beforeFilePath = latestChange.getElementBefore().getFilePath();
committer = latestChange.getCommitterName();
commitTime = latestChange.getCommitTime();
commitDate = (commitTime == 0) ? NOT_FOUND_PLACEHOLDER : new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").format(new Date(commitTime * 1000L));
}
return new String[]
{
shortCommitId,
beforeFilePath,
"(" + committer,
commitDate,
lineNumber + ")",
lines.get(lineNumber-1)
};
}
public static History.HistoryInfo<? extends CodeElement> getLineBlame(Repository repository, String commitId, String filePath, String name, int lineNumber) {
CodeElement codeElement = locate(repository, commitId, filePath, name, lineNumber);
History.HistoryInfo<? extends CodeElement> history = null;
if (codeElement != null) {
try {
history = new LineTrackerImpl().blame(repository, filePath, commitId, name, lineNumber, codeElement);
} catch (Exception e) {
System.out.println("Error in tracking line blame for " + filePath + " at line " + lineNumber + " in commit " + commitId);
}
}
else {
System.out.println("Code element not found for " + filePath + " at line " + lineNumber + " in commit " + commitId);
}
return history;
}

private static CodeElement locate(Repository repository, String commitId, String filePath, String name, int lineNumber) {
CodeElementLocator locator = new CodeElementLocator(
repository,
commitId,
Expand All @@ -44,42 +81,11 @@ private static String[] lineBlameFormat(Repository repository, String commitId,
CodeElement codeElement = null;
try {
codeElement = locator.locate();
} catch (Exception e) {
throw new RuntimeException(e);
}
String shortCommitId = NOT_FOUND_PLACEHOLDER;
String committer = NOT_FOUND_PLACEHOLDER;
Long commitTime = 0L;
String beforeFilePath = NOT_FOUND_PLACEHOLDER;
if (codeElement != null) {
ArrayList<RESTChange> restChanges = trackCodeHistory(
repository,
filePath,
commitId,
name,
lineNumber,
codeElement
);
catch (Exception e) {

if (restChanges.isEmpty()) {
throw new RuntimeException("No history found for the given code element");
}
RESTChange h0 = restChanges.get(0);
shortCommitId = h0.commitId.substring(0, 7); // take the first 7 characters
beforeFilePath = h0.elementFileBefore;
committer = h0.committer;
commitTime = h0.commitTime;
}
String commitDate = (commitTime == 0) ? NOT_FOUND_PLACEHOLDER : new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").format(new Date(commitTime * 1000L));
return new String[]
{
shortCommitId,
beforeFilePath,
"(" + committer,
commitDate,
lineNumber + ")",
lines.get(lineNumber)
};
return codeElement;
}

}
2 changes: 1 addition & 1 deletion src/main/java/org/codetracker/blame/LineTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/* Created by pourya on 2024-06-26*/
public interface LineTracker {
History<? extends CodeElement> track(
History.HistoryInfo<? extends CodeElement> blame(
Repository repository,
String filePath,
String commitId,
Expand Down
48 changes: 24 additions & 24 deletions src/main/java/org/codetracker/blame/LineTrackerImpl.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package org.codetracker.blame;

import org.codetracker.api.*;
import org.codetracker.element.Attribute;
import org.codetracker.element.Block;
import org.codetracker.element.Variable;
import org.codetracker.element.Class;
import org.codetracker.element.Method;
import org.eclipse.jgit.lib.Repository;

/* Created by pourya on 2024-06-26*/
public class LineTrackerImpl implements LineTracker {
public History<? extends CodeElement> track(
public History.HistoryInfo<? extends CodeElement> blame(
Repository repository,
String filePath,
String commitId,
Expand All @@ -16,45 +18,43 @@ public History<? extends CodeElement> track(
CodeElement codeElement
) {
try {
History<? extends CodeElement> history = null;
History.HistoryInfo<? extends CodeElement> blame = null;
switch (codeElement.getClass().getSimpleName()) {
case "Method":
MethodTracker methodTracker = CodeTracker
.methodTracker()
case "Class" :
String className = ((Class) codeElement).getUmlClass().getNonQualifiedName();
ClassTracker classTracker = CodeTracker
.classTracker()
.repository(repository)
.filePath(filePath)
.startCommitId(commitId)
.methodName(name)
.methodDeclarationLineNumber(lineNumber)
.className(className)
.classDeclarationLineNumber(lineNumber)
.build();
history = methodTracker.track();
blame = classTracker.blame();
break;
case "Variable":
Variable variable = (Variable) codeElement;
VariableTracker variableTracker = CodeTracker
.variableTracker()
case "Method":
String methodName = ((Method) codeElement).getUmlOperation().getName();
MethodTracker methodTracker = CodeTracker
.methodTracker()
.repository(repository)
.filePath(filePath)
.startCommitId(commitId)
.methodName(variable.getOperation().getName())
.methodDeclarationLineNumber(
variable.getOperation().getLocationInfo().getStartLine()
)
.variableName(name)
.variableDeclarationLineNumber(lineNumber)
.methodName(methodName)
.methodDeclarationLineNumber(lineNumber)
.build();
history = variableTracker.track();
blame = methodTracker.blame();
break;
case "Attribute":
String attrName = ((Attribute) codeElement).getUmlAttribute().getName();
AttributeTracker attributeTracker = CodeTracker
.attributeTracker()
.repository(repository)
.filePath(filePath)
.startCommitId(commitId)
.attributeName(name)
.attributeName(attrName)
.attributeDeclarationLineNumber(lineNumber)
.build();
history = attributeTracker.track();
blame = attributeTracker.blame();
break;
case "Block":
Block block = (Block) codeElement;
Expand All @@ -71,12 +71,12 @@ public History<? extends CodeElement> track(
.blockStartLineNumber(codeElement.getLocation().getStartLine())
.blockEndLineNumber(codeElement.getLocation().getEndLine())
.build();
history = blockTracker.track();
blame = blockTracker.blame();
break;
default:
break;
}
return history;
return blame;
}
catch (Exception e) {
throw new RuntimeException(e);
Expand Down
59 changes: 58 additions & 1 deletion src/test/java/org/codetracker/blame/CodeTrackerBlameTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package org.codetracker.blame;

import org.eclipse.jgit.lib.Repository;
import org.junit.jupiter.api.Test;
import org.refactoringminer.api.GitService;
import org.refactoringminer.util.GitServiceImpl;

import java.util.ArrayList;
import java.util.List;

import static org.codetracker.blame.CodeTrackerBlame.lineBlameFormat;
import static org.codetracker.blame.Utils.getFileContentByCommit;

/* Created by pourya on 2024-06-26*/
public class CodeTrackerBlameTest {
private static final GitService gitService = new GitServiceImpl();
public void testDriver() throws Exception {
// @Test
public void blameTest() throws Exception {
String commitId = "9aeefcd8120bb3b89cdb437d8c32d2ed84b8a825";
String filePath = "servers/src/main/java/tachyon/worker/block/allocator/MaxFreeAllocator.java";
String owner = "Alluxio";
Expand All @@ -22,8 +28,59 @@ public void testDriver() throws Exception {
gitHubToken
);
List<String[]> blameResult = new CodeTrackerBlame().blameFile(repository, commitId, filePath);
System.out.println();
System.out.println("----------------------------------------------------------------------------------------");
System.out.println();
TabularPrint.printTabularData(blameResult);
//TODO: Add assertions
//TODO: Add test for Tabular Print formatting
}

@Test
public void blameTestWithLocalRepo() throws Exception {
String commitId = "5b33dc6f8cfcf8c0e31966c035b0406eca97ec76";
String filePath = "src/main/java/dat/MakeIntels.java";
String owner = "pouryafard75";
String repoName = "DiffBenchmark";
String gitHubToken = System.getProperty("OAuthToken");
Repository repository = gitService.cloneIfNotExists(
"/Users/pourya/IdeaProjects/DiffBenchmark",
"https://github.com/" + owner + "/" + repoName + ".git",
owner,
gitHubToken
);
List<String[]> blameResult = new CodeTrackerBlame().blameFile(repository, commitId, filePath);
System.out.println();
System.out.println("----------------------------------------------------------------------------------------");
System.out.println();
TabularPrint.printTabularData(blameResult);
//TODO: Add assertions
//TODO: Add test for Tabular Print formatting
}
@Test
public void blameTestSingleLine() throws Exception {
String commitId = "5b33dc6f8cfcf8c0e31966c035b0406eca97ec76";
String filePath = "src/main/java/dat/MakeIntels.java";
String owner = "pouryafard75";
String repoName = "DiffBenchmark";
int lineNumber = 18;
String name = null;
String gitHubToken = System.getProperty("OAuthToken");
Repository repository = gitService.cloneIfNotExists(
"/Users/pourya/IdeaProjects/DiffBenchmark",
"https://github.com/" + owner + "/" + repoName + ".git",
owner,
gitHubToken
);
List<String[]> blameResult = new ArrayList<>();
List<String> lines = getFileContentByCommit(repository, commitId, filePath);
blameResult.add(lineBlameFormat(repository, commitId, filePath, name, lineNumber, lines));
System.out.println();
System.out.println("----------------------------------------------------------------------------------------");
System.out.println();
TabularPrint.printTabularData(blameResult);
//TODO: Add assertions
//TODO: Add test for Tabular Print formatting
}

}

0 comments on commit 6a81d32

Please sign in to comment.