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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15822544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:58:43  来源:igfitidea点击:

JGit: How to get all commits of a branch? (Without changes to the working directory ...)

javajgit

提问by hurik

how do I get all commits of a branch with JGit, without changing the working directory?

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

Unfortunately the JGit docs are not very good ...

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

In ruby with grit it is very easy:

在 ruby​​ with grit 中很容易:

repo = Grit::Repo.new(pathToRepo)

repo.commits(branchName, false).each do |commit|
    doSomethingWithTheCommit
end

Bye, hurik

再见,胡里克

回答by hurik

Tried this here first: https://stackoverflow.com/a/13925765/2246865

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

But it wasn't was working always, so I found this here: http://www.eclipse.org/forums/index.php/t/280339/

但它并不总是有效,所以我在这里找到了这个:http: //www.eclipse.org/forums/index.php/t/280339/

Here my solution, it isn't really nice, but it's working ...

这是我的解决方案,它不是很好,但它有效......

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());
            }
        }
    }
}

回答by Felipe Gomes

With the code below you can get all commits of a branch or tag:

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

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());
}

The varialbe treeNamewill define the tag or branch. This treeNameis the complete name of the branch or tag, for example refs/heads/masterfor the masterbranch or refs/tags/v1.0for a tag called v1.0.

变量treeName将定义标签或分支。这treeName是分支或标签的完整名称,例如refs/heads/master对于分支或refs/tags/v1.0一个标签叫V1.0

Alternatively, you can use the gitectiveAPI. The following code does the same as the code above:

或者,您可以使用GitectiveAPI。下面的代码和上面的代码一样:

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());
}

Some try/catch will be necessary, I hide them to make the code shorter. Good luck.

一些 try/catch 将是必要的,我隐藏它们以使代码更短。祝你好运。

回答by despadina

I was just looking for a way to list all commits under each branch and I found this thread. I finally did something a bit different from some of the answers I found here and worked.

我只是在寻找一种方法来列出每个分支下的所有提交,我找到了这个线程。我终于做了一些与我在这里找到并工作的答案有点不同的事情。

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

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

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

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

        Iterable<RevCommit> commits = git.log().add(repo.resolve(branchName)).call();

        List<RevCommit> commitsList = Lists.newArrayList(commits.iterator());

        for (RevCommit commit : commitsList) {
            System.out.println(commit.getName());
            System.out.println(commit.getAuthorIdent().getName());
            System.out.println(new Date(commit.getCommitTime() * 1000L));
            System.out.println(commit.getFullMessage());
        }
    }

    git.close();
}

Thanks for the help

谢谢您的帮助

回答by Sonson123

You are right, the docs shouldreally be better.

你是对的,文档应该更好。

You could use the JGit Log commandor use a library like gitectivewhich makes iterating over commits easy. You can look at the source to learn more about JGit.

您可以使用JGit Log 命令或使用像gitective这样的库,这使得迭代提交变得容易。您可以查看源代码以了解有关 JGit 的更多信息。

Other (more complicated) ways are described in this SO-question.

其他(更复杂的)方法在这个 SO-question中描述。

回答by centic

A much shorter and clean solution can be done via the "log" command that JGit provides:

可以通过 JGit 提供的“log”命令来完成一个更短更干净的解决方案:

    Repository repository = new FileRepository("pathToRepo/.git");
    logs = new Git(repository).log()
            .add(repository.resolve("remotes/origin/testbranch"))
            .call();
    count = 0;
    for (RevCommit rev : logs) {
        System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
        count++;
    }
    System.out.println("Had " + count + " commits overall on test-branch");

See the full snippetat the jgit-cookbook

查看完整片段jgit,食谱

回答by Peter Kahn

This seems to work but I thought add(ObjectId branch) to provide a gate to the active branch, but I appear to require a range

这似乎有效,但我认为 add(ObjectId branch) 为活动分支提供了一个门,但我似乎需要一个范围

Git git = ...
ObjectId master = git.repository.resolve("refs/heads/master")
ObjectId branch = git.repository.resolve("refs/heads/mybranch")

git.log().addRange(master,branch).call()