代码之家  ›  专栏  ›  技术社区  ›  hurik

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

  •  13
  • hurik  · 技术社区  · 12 年前

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

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

    在有砂砾的红宝石中,这很容易:

    repo = Grit::Repo.new(pathToRepo)
    
    repo.commits(branchName, false).each do |commit|
        doSomethingWithTheCommit
    end
    

    再见 胡里克

    6 回复  |  直到 11 年前
        1
  •  16
  •   howlger df778899    7 年前

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

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

    变体 treeName 将定义标记或分支。这 树名称 例如,是分支或标记的完整名称 refs/heads/master 对于 主人 分支机构或 refs/tags/v1.0 对于名为 版本1.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());
    }
    

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

        2
  •  16
  •   BullyWiiPlaza    7 年前

    先在这里尝试一下: 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());
                }
            }
        }
    }
    
        3
  •  4
  •   despadina    9 年前

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

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

    谢谢你的帮助

        4
  •  0
  •   Community CDub    8 年前

    你是对的,医生们 应该 真的会更好。

    你可以使用 JGit Log command 或者使用类似的库 gitective 这使得迭代提交变得容易。您可以查看来源以了解更多关于JGit的信息。

    其他(更复杂的)方法在 this SO-question .

        5
  •  0
  •   centic    9 年前

    通过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");
    

    请参阅 full snippet jgit-cookbook

        6
  •  0
  •   Peter Kahn    9 年前

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

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