我如何扩展以下 logCommand 才能使命令--follow
选项git log
正常工作?
Git git = new Git(myRepository);
Iterable<RevCommit> log = git.log().addPath("com/mycompany/myclass.java").call();
这个选项是在jGit中实现的,但是我不知道怎么用。logCommand 的方法似乎没有用。谢谢!
在一些午夜工作中,我得到了以下信息:
LogCommand 的最后一次提交将针对所有较旧的提交检查重命名,直到找到重命名操作。这个循环将一直持续到找不到重命名为止。
但是,该搜索可能需要一些时间,特别是如果它遍历所有提交直到结束并且不再找到任何重命名操作。所以,我愿意接受任何改进。我猜 git 通常使用索引在更短的时间内执行后续选项。
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.RenameDetector;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.treewalk.TreeWalk;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Create a Log command that enables the follow option: git log --follow -- < path >
* User: OneWorld
* Example for usage: ArrayList<RevCommit> commits = new LogFollowCommand(repo,"src/com/mycompany/myfile.java").call();
*/
public class LogFollowCommand {
private final Repository repository;
private String path;
private Git git;
/**
* Create a Log command that enables the follow option: git log --follow -- < path >
* @param repository
* @param path
*/
public LogFollowCommand(Repository repository, String path){
this.repository = repository;
this.path = path;
}
/**
* Returns the result of a git log --follow -- < path >
* @return
* @throws IOException
* @throws MissingObjectException
* @throws GitAPIException
*/
public ArrayList<RevCommit> call() throws IOException, MissingObjectException, GitAPIException {
ArrayList<RevCommit> commits = new ArrayList<RevCommit>();
git = new Git(repository);
RevCommit start = null;
do {
Iterable<RevCommit> log = git.log().addPath(path).call();
for (RevCommit commit : log) {
if (commits.contains(commit)) {
start = null;
} else {
start = commit;
commits.add(commit);
}
}
if (start == null) return commits;
}
while ((path = getRenamedPath( start)) != null);
return commits;
}
/**
* Checks for renames in history of a certain file. Returns null, if no rename was found.
* Can take some seconds, especially if nothing is found... Here might be some tweaking necessary or the LogFollowCommand must be run in a thread.
* @param start
* @return String or null
* @throws IOException
* @throws MissingObjectException
* @throws GitAPIException
*/
private String getRenamedPath( RevCommit start) throws IOException, MissingObjectException, GitAPIException {
Iterable<RevCommit> allCommitsLater = git.log().add(start).call();
for (RevCommit commit : allCommitsLater) {
TreeWalk tw = new TreeWalk(repository);
tw.addTree(commit.getTree());
tw.addTree(start.getTree());
tw.setRecursive(true);
RenameDetector rd = new RenameDetector(repository);
rd.addAll(DiffEntry.scan(tw));
List<DiffEntry> files = rd.compute();
for (DiffEntry diffEntry : files) {
if ((diffEntry.getChangeType() == DiffEntry.ChangeType.RENAME || diffEntry.getChangeType() == DiffEntry.ChangeType.COPY) && diffEntry.getNewPath().contains(path)) {
System.out.println("Found: " + diffEntry.toString() + " return " + diffEntry.getOldPath());
return diffEntry.getOldPath();
}
}
}
return null;
}
}
我记得以前尝试过 OneWorld 的解决方案,虽然它有效,但速度很慢。我想我会到处谷歌看看是否还有其他可能性。
是的,在这个Eclipse 线程中,有人建议使用org.eclipse.jgit.revwalk.FollowFilter并在RevWalkFollowFilterTest.java中寻找一个使用示例。
所以我想我会试一试,结果是这样的代码:
private static class DiffCollector extends RenameCallback {
List<DiffEntry> diffs = new ArrayList<DiffEntry>();
@Override
public void renamed(DiffEntry diff) {
diffs.add(diff);
}
}
private DiffCollector diffCollector;
private void showFileHistory(String filepath)
{
try
{
Config config = repo.getConfig();
config.setBoolean("diff", null, "renames", true);
RevWalk rw = new RevWalk(repo);
diffCollector = new DiffCollector();
org.eclipse.jgit.diff.DiffConfig dc = config.get(org.eclipse.jgit.diff.DiffConfig.KEY);
FollowFilter followFilter =
FollowFilter.create(filepath, dc);
followFilter.setRenameCallback(diffCollector);
rw.setTreeFilter(followFilter);
rw.markStart(rw.parseCommit(repo.resolve(Constants.HEAD)));
for (RevCommit c : rw)
{
System.out.println(c.toString());
}
}
catch(...
结果是,嗯,好吧,我猜... RevWalk 确实设法完成了 git-repo 历史中文件的简单重命名(由“git mv {filename}”操作执行)。
但是,它无法处理更混乱的情况,例如当一位同事在 repo 的历史记录中执行了这组操作时:
在这种情况下,JGit 的跟随功能只会让我从头部到第二次提交,然后停在那里。
然而,真正的“ git log --follow ”命令似乎有足够的智慧来弄清楚:
所以 JGit 的跟随能力似乎比真正的 Git 弱一些。呃,好吧。
但无论如何,我可以确认使用 JGit 的FollowFilter技术确实比之前建议的技术快得多。