[英]finding the branches that are available between 2 git commits
我有一个开始提交,我想从中找到所有分支,直到到达另一个找不到提交的注释。
commit 1
|
commit 2
| commit5
commit3 /
| /
commit 4
|
commit 6
在这种情况下,说提交1-5的所有提交都具有注释“ find branch”,而提交6的值不等于该值。
因此,我将从提交1开始,找到所有父级(即:提交2),然后尝试检查是否有该提交的分支(即:子代数大于1)。 如果有一个以上的孩子
getChildren()
方法仅适用于PlotCommit对象,但方法parentCommit.getParents()
仅返回RevCommit
对象。 然后,当提交中没有更多注释时(即,提交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的分支名称
对于这种请求的git命令(为给定提交查找分支)是:
git branch --contains <commit>
(如“ Git:查找提交来自哪个分支 ”和“ 如何列出包含给定提交的分支? ”中所示)
它没有像JGit中那样实现。
该线程提出以下建议:
' git branch --contains <commit>
'将报告包含该提交的每个分支。
在您从远程获得的所有提交的标准推送/获取工作流中,仅报告“ origin/master
”。
甚至对于本地提交:假设您已将feature
分支合并回master
。 然后,此命令还将报告合并后创建的master
分支和所有feature
分支。
Git根本不存储在哪个分支上创建的修订版。
在这些警告之后:“ 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.