Looping over commits for a file with jGit

前端 未结 4 1302
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-28 10:12

I\'ve managed to get to grips with the basics of jGit file in terms of connecting to a repos and adding, commiting, and even looping of the commit messages for the files.

4条回答
  •  时光说笑
    2020-12-28 10:46

    So I tried to get charlieboy's solution to work, and it mostly did, but it failed for me in the following case (maybe something changed in jgit since that post?)

    add fileA, commit as "commit 1" add fileB, commit as "commit 2"

    getFileVersionDateList("fileA")
    

    Both commit 1 and commit 2 were found, where I expected only commit 1.

    My solution was as follows:

    List commits = new ArrayList();
    
    RevWalk revWalk = new RevWalk(repository);
    revWalk.setTreeFilter(
            AndTreeFilter.create(
                    PathFilterGroup.createFromStrings(),
                    TreeFilter.ANY_DIFF)
    );
    
    RevCommit rootCommit = revWalk.parseCommit(repository.resolve(Constants.HEAD));
    revWalk.sort(RevSort.COMMIT_TIME_DESC);
    revWalk.markStart(rootCommit);
    
    for (RevCommit revCommit : revWalk) {
        commits.add(new GitCommit(getRepository(), revCommit));
    }
    

    Using the LogCommand is even simpler, and looks like this:

    List commitsList = new ArrayList();
    
    Git git = new Git(repository);
    LogCommand logCommand = git.log()
            .add(git.getRepository().resolve(Constants.HEAD))
            .addPath();
    
    for (RevCommit revCommit : logCommand.call()) {
        commitsList.add(new GitCommit(this, revCommit));
    }
    

    Obviously you'd also check the commit dates, etc, as needed.

提交回复
热议问题