如何在JGit中“git log --follow <path>”? (要检索包括重命名的完整历史记录)</path>

时间:2012-07-13 13:50:09

标签: git git-log jgit

如何扩展以下logCommand以使--follow命令的git log选项正常工作?

Git git = new Git(myRepository);
Iterable<RevCommit> log = git.log().addPath("com/mycompany/myclass.java").call();

此选项在jGit中实现,但我不知道如何使用它。 logCommand的方法似乎没有用处。谢谢!

2 个答案:

答案 0 :(得分:10)

在午夜工作期间,我得到了以下内容:

将检查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;
    }
}

答案 1 :(得分:0)

我记得以前曾尝试过OneWorld的解决方案,尽管它行之有效,但速度非常慢。我以为我会四处搜寻,看看是否还有其他可能性。

是的,在此Eclipse thread中,建议使用 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}”操作执行)。

但是,它无法处理更混乱的情况,例如当某位同事在回购记录中执行了以下一系列操作:

  • 第一次提交:使用“ git mv”重命名文件
  • 第二次提交:在新的子文件夹位置添加了该文件的副本
  • 第3次提交:删除了旧位置的副本

在这种情况下,JGit的跟踪功能只会使我从头到第二次提交,然后就此停止。

但是,真正的“ git log --follow ”命令似乎具有足够的技巧来找出:

  • 第二次提交中添加的文件与第一次提交中的文件相同(即使它们位于不同的位置)
  • 它将为您提供整个历史记录:
    • 从HEAD到第二次提交(在新位置添加了新命名文件的副本)
    • 跳过对第三次提交的任何提及(在旧路径中删除旧文件)
    • 接着是第一次提交及其历史记录(旧位置和文件名)

因此,与真正的Git相比,JGit的跟踪功能似乎有些弱。嗯。

但是无论如何,我可以确认使用JGit的 FollowFilter 技术确实比以前建议的技术快得多。