JGit:如何获得分支的所有提交?(无需更改工作目录......)

hur*_*rik 11 java jgit

如何在不更改工作目录的情况下获取JGit分支的所有提交?

不幸的是JGit文档不是很好......

用砂砾红宝石很容易:

repo = Grit::Repo.new(pathToRepo)

repo.commits(branchName, false).each do |commit|
    doSomethingWithTheCommit
end
Run Code Online (Sandbox Code Playgroud)

再见,hurik

hur*_*rik 15

首先在这里尝试:https://stackoverflow.com/a/13925765/2246865

但它总是不起作用,所以我在这里找到了:http://www.eclipse.org/forums/index.php/t/280339/

在这里,我的解决方案,它不是很好,但它的工作......

public static void main(String[] args) throws IOException, GitAPIException {
    Repository repo = new FileRepository("pathToRepo/.git");
    Git git = new Git(repo);
    RevWalk walk = new RevWalk(repo);

    List<Ref> branches = git.branchList().call();

    for (Ref branch : branches) {
        String branchName = branch.getName();

        System.out.println("Commits of branch: " + branch.getName());
        System.out.println("-------------------------------------");

        Iterable<RevCommit> commits = git.log().all().call();

        for (RevCommit commit : commits) {
            boolean foundInThisBranch = false;

            RevCommit targetCommit = walk.parseCommit(repo.resolve(
                    commit.getName()));
            for (Map.Entry<String, Ref> e : repo.getAllRefs().entrySet()) {
                if (e.getKey().startsWith(Constants.R_HEADS)) {
                    if (walk.isMergedInto(targetCommit, walk.parseCommit(
                            e.getValue().getObjectId()))) {
                        String foundInBranch = e.getValue().getName();
                        if (branchName.equals(foundInBranch)) {
                            foundInThisBranch = true;
                            break;
                        }
                    }
                }
            }

            if (foundInThisBranch) {
                System.out.println(commit.getName());
                System.out.println(commit.getAuthorIdent().getName());
                System.out.println(new Date(commit.getCommitTime() * 1000L));
                System.out.println(commit.getFullMessage());
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 14

使用下面的代码,您可以获得分支或标记的所有提交:

Repository repository = new FileRepository("/path/to/repository/.git");
String treeName = "refs/heads/master"; // tag or branch
for (RevCommit commit : git.log().add(repository.resolve(treeName)).call()) {
    System.out.println(commit.getName());
}
Run Code Online (Sandbox Code Playgroud)

varialbe treeName将定义标签或分支.这treeName是分支或标记的完整名称,例如refs/heads/master,对于分支或refs/tags/v1.0名为v1.0的标记.

或者,您可以使用gitective API.以下代码与上面的代码相同:

Repository repository = new FileRepository("/path/to/repository/.git");

AndCommitFilter filters = new AndCommitFilter();
CommitListFilter revCommits = new CommitListFilter();
filters.add(revCommits);

CommitFinder finder = new CommitFinder(repository);
finder.setFilter(filters);
String treeName = "refs/heads/master"; // tag or branch
finder.findFrom(treeName);

for (RevCommit commit : revCommits) {
    System.out.println(commit.getName());
}
Run Code Online (Sandbox Code Playgroud)

有些try/catch是必要的,我隐藏它们以使代码更短.祝好运.