Skip to content

Commit

Permalink
Refactor blame code
Browse files Browse the repository at this point in the history
  • Loading branch information
pouryafard75 committed Jul 3, 2024
1 parent e3db71e commit 6abf4cb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
23 changes: 23 additions & 0 deletions src/main/java/org/codetracker/blame/util/Utils.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.codetracker.blame.util;

import org.codetracker.blame.IBlame;
import org.codetracker.blame.model.LineBlameResult;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.refactoringminer.api.GitService;
import org.refactoringminer.astDiff.utils.URLHelper;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -58,4 +62,23 @@ public static List<String> getFileContentByCommit(Repository repository, String
}
return lines;
}
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));
List<String> lines = getFileContentByCommit(repository, commitId, filePath);
BlameFormatter formatter = new BlameFormatter(lines);
List<LineBlameResult> blameResult = apply(commitId, filePath, blamer, repository);
return TabularPrint.make(formatter.make(blameResult));
}


private static List<LineBlameResult> apply(String commitId, String filePath, IBlame blamer, Repository repository) throws Exception {
return blamer.blameFile(repository, commitId, filePath);
}
public static String getOwner(String gitURL){
return gitURL.split("/")[3];
}
public static String getProject(String gitURL){
return gitURL.split("/")[4];
}
}
29 changes: 4 additions & 25 deletions src/test/java/org/codetracker/blame/CodeTrackerBlameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.util.Objects;
import java.util.stream.Stream;

import static org.codetracker.blame.util.Utils.getFileContentByCommit;
import static org.codetracker.blame.util.Utils.*;

/* Created by pourya on 2024-06-26*/
@Disabled
Expand All @@ -35,12 +35,7 @@ public class CodeTrackerBlameTest {
@ParameterizedTest
@MethodSource("testBlamerInputProvider")
public void testBlameWithFormatting(String url, String filePath, IBlame blamer, String expectedPath) throws Exception {
String commitId = URLHelper.getCommit(url);
Repository repository = gitService.cloneIfNotExists(REPOS_PATH + "/" + 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);
String actual = TabularPrint.make(formatter.make(blameResult));
String actual = getBlameOutput(url, filePath, blamer, REPOS_PATH, gitService);
assertEqualWithFile(expectedPath, actual);
}

Expand All @@ -59,12 +54,8 @@ public void testBlameWithLocalRepo() throws Exception {
String url = "https://github.com/pouryafard75/DiffBenchmark/commit/5b33dc6f8cfcf8c0e31966c035b0406eca97ec76";
String filePath = "src/main/java/dat/MakeIntels.java";
String expectedPath = "/blame/blameTestWithLocalRepo.txt";
String commitId = URLHelper.getCommit(url);
Repository repository = gitService.cloneIfNotExists(REPOS_PATH + "/" + getProject(url), URLHelper.getRepo(url));
List<LineBlameResult> blameResult = new CodeTrackerBlame().blameFile(repository, commitId, filePath);
List<String> lines = getFileContentByCommit(repository, commitId, filePath);
BlameFormatter formatter = new BlameFormatter(lines);
String actual = TabularPrint.make(formatter.make(blameResult));

String actual = getBlameOutput(url, filePath, new CodeTrackerBlame(), REPOS_PATH, gitService);
assertEqualWithFile(expectedPath, actual);
}

Expand All @@ -91,16 +82,4 @@ public void blameTestSingleLine() throws Exception {
Assertions.assertEquals(expected, lineBlameResult.toString());
}

private List<LineBlameResult> apply(String commitId, String filePath, IBlame blamer, Repository repository) throws Exception {
return blamer.blameFile(repository, commitId, filePath);
}


private static String getOwner(String gitURL){
return gitURL.split("/")[3];
}
private static String getProject(String gitURL){
return gitURL.split("/")[4];
}

}

0 comments on commit 6abf4cb

Please sign in to comment.