8

不足している JGit ドキュメントは、RevWalk の使用中にブランチを使用/検出する方法について何も述べていないようです。

この質問はほとんど同じことを言っています。

私の質問は次のとおりです: RevCommit からブランチ名/ID を取得するにはどうすればよいですか? または、事前にトラバースするブランチを指定するにはどうすればよいですか?

4

3 に答える 3

6

ブランチをループすることで、より良い方法を見つけました。

呼び出してブランチをループしました

for (Ref branch : git.branchList().call()){
    git.checkout().setName(branch.getName()).call();
    // Then just revwalk as normal.
}
于 2012-05-04T00:24:39.830 に答える
2

JGit の現在の実装 (その git リポジトリとそのRevCommitクラスを参照) を見ると、「 Git: コミットがどのブランチから来たかを見つける」にリストされているものと同等のものは見つかりませんでした。
すなわち:

git branch --contains <commit>

のオプションの一部のみgit branchが実装されています ( などListBranchCommand.java)。

于 2012-05-03T19:35:24.963 に答える
1

以下のコードを使用して、コミットによって「from」ブランチを取得できます。

/**
     * find out which branch that specified commit come from.
     * 
     * @param commit
     * @return branch name.
     * @throws GitException 
     */
    public String getFromBranch(RevCommit commit) throws GitException{
        try {
            Collection<ReflogEntry> entries = git.reflog().call();
            for (ReflogEntry entry:entries){
                if (!entry.getOldId().getName().equals(commit.getName())){
                    continue;
                }

                CheckoutEntry checkOutEntry = entry.parseCheckout();
                if (checkOutEntry != null){
                    return checkOutEntry.getFromBranch();
                }
            }

            return null;
        } catch (Exception e) {
            throw new GitException("fail to get ref log.", e);
        }
    }
于 2012-12-18T03:20:49.537 に答える