这是解决问题的一种方法。注意,我使用Gradle / Groovy编写,因此代码可能看起来有些时髦。
- getAll分支
- 为每个分支获取合并基础提交-由于所有分支都来自主节点,因此所有人都会找到一个
- 遍历提交并停止在合并基础列表中找到的第一个
`
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)}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}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}ntB: ${revB.name}ntM: ${mergebase.name}" if (! mergebase) { return null } return mergebase.name } catch(Exception e) { project.logger.error("GetMergebase Failed: ${commitIdA}, ${commitIdB} because ${e.message}") } return null}private List<Ref> getBranches(Git git) { List<Ref> branchRefs = git.branchList().call() return branchRefs} `


