"git merge-base --fork-point branchA branchB"的jgit等效项是什么? [英] What's the jgit equivalent for "git merge-base --fork-point branchA branchB"
问题描述
"git merge-base --fork-point branchA branchB"的等效jgit编码是什么?
What's the jgit equivalent codeo for "git merge-base --fork-point branchA branchB"?
我尝试了下面的代码,但没有得到正确的答案.我正在用它来寻找分支的起源. foreach.branch(git merge-base --fork-point mybranch theirbranch)将仅针对原点产生一个提交ID.
I tried the code below but I'm not getting the correct answer. I'm using this to hunt for branch origin. foreach.branch (git merge-base --fork-point mybranch theirbranch) will yield null a commit id only for point of origin.
所以,我所需要做的就是弄清楚如何在jgit中做到这一点,并且当我不知道它的时候,我有一种计算分支原点的方法.
So, all I need to do is to figure out how to do this in jgit and I have a method for calculating branch origin when I don't already know it.
private String getMergeBase(Repository repository, ObjectId commitIdA, ObjectId commitIdB) {
RevWalk walk = new RevWalk(repository)
try {
RevCommit revA = walk.lookupCommit(commitIdA)
RevCommit revB = walk.lookupCommit(commitIdB)
walk.setRevFilter(RevFilter.MERGE_BASE)
walk.markStart([revA,revB])
RevCommit mergeBase = walk.next()
if (! mergeBase) { return null }
return mergeBase.name
} catch(Exception e) {
project.logger.error("GetMergeBase Failed: ${commitIdA}, ${commitIdB} because ${e.message}")
}
return null
}
推荐答案
这是解决问题的一种方法.请注意,我使用Gradle/Groovy编写,因此代码可能看起来有些时髦.
here's a way to solve it. note, I'm writing in Gradle/Groovy so the code may look a little funky.
- getAll分支机构
- 为每个分支获取合并基础提交-由于所有分支都来自主节点,因此所有人都会找到一个
- 遍历提交并停止在合并基础列表中找到的第一个
`
/**
* Obtain GitWorkingCopyLog for all changes on branch with (svn --stop-on-copy equivalent)
* @param repoDir
* @return GitWorkingCopyLog
*/
private GitWorkingCopyLog getLogs(File repoDir) {
List<RevCommit> commits = []
Git git = openExistingRepository(repoDir)
ObjectId head = getHead(git.repository) // tip of current branch
// find common merge ancestor branch points for all branches
List<String> forkCandidates = mergeBaseForAllBranches(git, head)
for (RevCommit r in git.log().call()) {
if (r.name in forkCandidates) {
break // stop looping on first rev in common merge base
}
commits.add(r)
}
return new GitWorkingCopyLog(git.repository, commits)
}
/**
* Generate list of commit ids for merge base with all candidates. All branches come from master
* so all branches share a common point of origin even if unrelated.
* @param git jgit controller
* @param head head revision for target branch
* @return list of commit ids
*/
private ArrayList mergeBaseForAllBranches(Git git, ObjectId head) {
def baseCandidates = []
getBranches(git).each { Ref other ->
if (other.objectId != head) {
String base = getMergeBase(git.repository, head, other.objectId)
baseCandidates.add(base)
}
}
baseCandidates
}
/**
*
* @param repository
* @param commitIdA
* @param commitIdB
* @return divergence point between the two branches (even if seemingly unrelated all must come back to master)
*/
private String getMergeBase(Repository repository, ObjectId commitIdA, ObjectId commitIdB) {
RevWalk walk = new RevWalk(repository)
try {
RevCommit revA = walk.lookupCommit(commitIdA)
RevCommit revB = walk.lookupCommit(commitIdB)
walk.setRevFilter(RevFilter.MERGE_BASE)
walk.markStart(revA)
walk.markStart(revB)
RevCommit mergeBase = walk.next()
println "\tA: ${revA.name}\n\tB: ${revB.name}\n\tM: ${mergeBase.name}"
if (! mergeBase) { return null }
return mergeBase.name
} catch(Exception e) {
project.logger.error("GetMergeBase Failed: ${commitIdA}, ${commitIdB} because ${e.message}")
}
return null
}
/**
* Get Refs for all branches
* @param git
* @return Ref list
*/
private List<Ref> getBranches(Git git) {
List<Ref> branchRefs = git.branchList().call()
return branchRefs
} `
这篇关于"git merge-base --fork-point branchA branchB"的jgit等效项是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!