1

我有一个开始提交,我想从中找到所有分支,直到我到达另一个没有找到提交的注释。

commit 1
|
commit 2
|         commit5
commit3  /
|       / 
commit 4
|
commit 6

在这种情况下,假设来自提交 1-5 的所有提交都有注释“查找分支”,而提交 6 没有带有该值的 not。

所以我将从提交 1 开始查找所有父项(即:提交 2)并尝试检查是否有此提交的分支(即:子项的数量大于 1)。如果有超过 1 个孩子

  1. getChildren()方法仅适用于 PlotCommit 对象,但该方法parentCommit.getParents()仅返回RevCommitObject。
  2. 我想找到特定提交中存在的分支名称

然后当提交上没有更多注释时(即提交 6 没有注释),逻辑将停在那里并返回分支名称的集合

    Repository repo;//will be set as part of some other logic
    private Set findBranchesForCommit(PlotCommit parentCommit, String note) throws ExecutionException, MissingObjectException, IncorrectObjectTypeException, IOException {
        Set branches = new HashSet();
        PlotCommit[] parents = (PlotCommit[]) parentCommit.getParents();//XXX will throw exception as this return RevCommit[]
        for (int i = 0; i < parents .length; i++) {
            PlotCommit commit = parents[i];
            String result = extractExistingMessage(repo, "refs/notes", commit);//will return the if the note available for particular commit
            if (result.trim().length() > 0 && result.equalsIgnoreCase(note)) {
                System.out.println("#########"+commit.getChildCount());
                //TODO need to add logic to find the branch of the particular commit
                branches.add(""); //add the branches available for the commit
                branches.addAll(findBranchesForCommit(commit, note));
            }
        }
        return branches;
    }

预期结果

我想找到包含特定 git note 的提交的分支名称。在上面的示例中,将返回 Commit 1 和 commit 5 的分支名称

4

1 回答 1

1

虽然这种请求的 git 命令(查找给定提交的分支)是:

git branch --contains <commit>

(如“ Git:查找提交来自哪个分支”和“如何列出包含给定提交的分支? ”)

它没有像在 JGit 中那样实现。

这个线程有这个建议:

' git branch --contains <commit>' 将报告包含此提交的每个分支。
在您从远程获得的所有提交的标准推送/获取工作流程中,仅origin/master报告“”。
甚至对于本地提交:假设您已将feature分支合并回master. 然后此命令还将报告合并后创建的master分支和所有分支。 Git 根本不存储创建它的分支的修订。feature

在这些警告之后:“git branch --contains”的粗略实现可能如下所示:

Repository repo = new FileRepository(args[0]);
RevWalk walk = new RevWalk(repo);
RevCommit commit = walk.parseCommit(repo.resolve(args[1] + "^0"));
for (Map.Entry<String, Ref> e : repo.getAllRefs().entrySet())
  if (e.getKey().startsWith(Constants.R_HEADS))
    if (walk.isMergedInto(commit,
        walk.parseCommit(e.getValue().getObjectId())))
      System.out.println("Ref " + e.getValue().getName()
                                + " contains commit " + commit);
于 2013-01-02T08:58:55.307 回答