JGit DiffEntry显示不存在提交的文件

时间:2017-02-19 13:21:45

标签: java git jgit

我正在尝试遍历所有存在的提交来存储关于它的信息(作者,提交者,时间戳,文件......)。它工作得很好,但有时当前提交显示不存在的条目。我将它与GitHub上的原始提交和本地git repo的图形用户界面进行了比较。我的代码有问题或者以错误的方式思考它吗?

private void getInformation() {
    Repository repository = new FileRepository(path);
    Git git = new Git(repository);
    RevCommit oldCommit = null;
    DiffFormatter diffFormatter = null;
    RevWalk walk = null;
    boolean first = true;

    walk = new RevWalk(repository);
    walk.markStart(walk.parseCommit(repository.resolve("HEAD")));
    walk.sort(RevSort.REVERSE);

    for (RevCommit commit : walk) {

        if (first == true) {
            oldCommit = commit;
            first = false;
        }

        /* read the old and new commit ID to determine changes (method by Rüdiger Herrmann) */
        ObjectReader reader = git.getRepository().newObjectReader();
        CanonicalTreeParser oldTree = parseRawGitdata(git, commit, reader);
        CanonicalTreeParser newTree = parseRawGitdata(git, oldCommit, reader);
        diffFormatter = new DiffFormatter(DisabledOutputStream.INSTANCE);
        diffFormatter.setRepository(git.getRepository());
        diffFormatter.setDetectRenames(true);
        List<DiffEntry> entries = diffFormatter.scan(newTree, oldTree);

        if (!entries.isEmpty()) {
            for (DiffEntry entry : entries) {
                /* only .java class files are relevant */
                if (entry.getOldPath().endsWith(".java") || entry.getNewPath().endsWith(".java")) {
                    /* handle the current ChangeType... */
                    determineChangeType(entry, commit);
                }
            }
        }
        oldCommit = commit;
    }

    diffFormatter.close();
    walk.close();    
}

private CanonicalTreeParser parseRawGitdata(Git git, RevCommit commit, ObjectReader reader) {

    CanonicalTreeParser treeIter = new CanonicalTreeParser();
    ObjectId tree = git.getRepository().resolve(commit.getTree().getName());
    treeIter.reset(reader, tree);

    return treeIter;
}  

我只想在当前的HEAD开始(或在更改订单后结束)。例如,从开源项目Apache Struts查看提交“ af50018f11d4fdfa8ac04a55f942a4973cb15c87 ”的DiffEntry,它包含大约1170个项目:

DiffEntry[MODIFY .gitignore],   
DiffEntry[MODIFY apps/pom.xml],   
DiffEntry[DELETE apps/portlet/README.txt],   
DiffEntry[DELETE apps/portlet/pom.xml],   
DiffEntry[DELETE apps/portlet/src/main/etc/exo/web.xml],   
DiffEntry[DELETE apps/portlet/src/main/etc/gridsphere/README-gridsphere.txt],   
DiffEntry[DELETE apps/portlet/src/main/etc/gridsphere/gridsphere-portlet.xml],     
DiffEntry[DELETE apps/portlet/src/main/etc/gridsphere/group.xml],  
DiffEntry[DELETE apps/portlet/src/main/etc/gridsphere/layout.xml],   
DiffEntry[DELETE apps/portlet/src/main/etc/gridsphere/struts-portlet],    
DiffEntry[DELETE apps/portlet/src/main/etc/gridsphere/web.xml],   
DiffEntry[DELETE apps/portlet/src/main/etc/jbossportal2.0/jboss-app.xml],  
DiffEntry[DELETE apps/portlet/src/main/etc/jbossportal2.0/jboss-portlet.xml],   
DiffEntry[DELETE apps/portlet/src/main/etc/jbossportal2.0/jboss-web.xml],   
DiffEntry[DELETE apps/portlet/src/main/etc/jbossportal2.0/portlet-instances.xml],   
DiffEntry[DELETE apps/portlet/src/main/etc/jbossportal2.0/struts-portlet-pages.xml],   
DiffEntry[DELETE apps/portlet/src/main/etc/jbossportal2.2/jboss-app.xml],   
DiffEntry[DELETE apps/portlet/src/main/etc/jbossportal2.2/jboss-portlet.xml],   
DiffEntry[DELETE apps/portlet/src/main/etc/jbossportal2.2/jboss-web.xml],   
DiffEntry[DELETE apps/portlet/src/main/etc/jbossportal2.2/struts-portlet-object.xml],   
DiffEntry[DELETE apps/portlet/src/main/etc/jetspeed2/README-jetspeed2.txt],   
DiffEntry[DELETE apps/portlet/src/main/etc/jetspeed2/struts-portlet.psml],   
DiffEntry[DELETE apps/portlet/src/main/etc/liferay3.6.1/web.xml],   
DiffEntry[DELETE apps/portlet/src/main/java/org/apache/struts2/portlet/example/ExampleAction.java],   
DiffEntry[DELETE apps/portlet/src/main/java/org/apache/struts2/portlet/example/FormExample.java],   
...  

据GitHub称,auhor于2016年8月1日承诺(af50018f11d4fdfa8ac04a55f942a4973cb15c87) - 增加了76次,删除了17次。

+27 −16 core/src/main/java/org/apache/struts2/views/util/DefaultUrlHelper.java
+49 −1 core/src/test/java/org/apache/struts2/views/util/DefaultUrlHelperTest.java

为什么我的DiffEntry集包含的数据不是官方提交的一部分?

JGit是否也提供了泛滥先前数据结构的未提交数据?

0 个答案:

没有答案