如何在JGit中复制git branch -a --contains <commit id> --merged
?
下面,我在没有--merged
的情况下进行复制,但我不确定如何将过滤器添加到仅报告合并更改的分支,而不包括合并点后创建的所有分支。
如果命令行可以,我希望JGit也可以,但我不确定如何。
注意:下面的代码使用我们的GitController,它是JGit的包装器,所以列出的一些方法不是纯JGit。
代码
public List<String> getBranchesContainingCommit(File localWorkingCopy, String id, boolean onlymerged = true) {
List<String> branches = new ArrayList<>()
// Access Repository
Git git = openExistingRepository(localWorkingCopy)
// Find commit
RevWalk walk = new RevWalk(git.repository)
ObjectId foundId = git.repository.resolve(id + "^0")
RevCommit commit = walk.parseCommit(foundId)
// For all Remote Branches
getBranches(git,true).each{ Ref ref ->
if (walk.isMergedInto(
walk.parseCommit(ref.getObjectId()), commit)) {
branches.add(branchNameFromRefName(ref.name))
}
}
walk.close()
walk.dispose()
return branches
}
测试错误
java.lang.AssertionError: Wrong number of branches found
Expected: [feature/branchContainsMerged, feature/branchContainsBase]
Actual: [feature/branchContainsAfterMerge, feature/branchContainsBase, feature/branchContainsMerged, master]
Expected :2
Actual :4
<Click to see difference>
测试用例
/**
* 1. Create baseBranch
* 2. branch branchMergedChangeSrc
* 3. Make change
* 4. merge branchMergedChangeSrc to baseBranch
* 5. branch branchUnmergedChangeSrc
* 6. Make change
* 7. Branch branchAfterMerge from baseBranch
*
* Expect: with merges filter
* idMerged on branchMergedChangeSrc, baseBranch
* idUnmerged on branchUnmergedChangeSrc
*/
String baseBranch="feature/branchContainsBase"
String branchMergedChangeSrc="feature/branchContainsMerged"
String idMerged = "f342575605aa297778047a940ac1f47b74c29d81"
String branchUnmergedChangeSrc="feature/branchContainsUnMerged"
String idUnmerged = "e1dbd1e371d60af235a1a0e82f95c25a9c0049b6"
String branchAfterMerge="feature/branchContainsAfterMerge"
@Before
void setup() {
controller = (GitController) getSCM(SCMType.Git)
workspace = createEmptyTestDir()
// Clone Skeleton
controller.cloneRepository(new File("testdata/skeleton.git").toURI().toString(), workspace)
}
@Test
void mergedBranchesForMergedCommitTest() {
List<String> containingBranches = controller.getBranchesContainingCommit(workspace, idMerged)
// Unmerged Change on Branch
checkBranches([branchMergedChangeSrc, baseBranch], containingBranches)
}
@Test
void unmergedBranchesForMergedCommitTest() {
List<String> containingBranches = controller.getBranchesContainingCommit(workspace, idUnmerged)
// Unmerged Change on Branch
checkBranches([branchUnmergedChangeSrc], containingBranches)
}
void checkBranches(List<String> expectedBranchNames, List<String> actual) {
assertEquals("""Wrong number of branches found
Expected: ${expectedBranchNames}
Actual: ${actual}""", expectedBranchNames.size(), actual.size())
expectedBranchNames.each { String branch ->
assertTrue("Expected ${branch} in containing: ${actual}", actual.contains(branch))
}
}
答案 0 :(得分:0)
缺陷听到的是认为可以将包含限制为直接合并发生的分支。所有git都可以判断哪些分支包含虚拟提交的历史记录,哪些不包含。
这似乎是通过从提交到分支的HEAD来完成的。分支头不断向前移动并使用“--merged”工作只是因为在某个时间点提交ID ==分支HEAD。
因此,尝试复制 - 合并是一个坏主意,因为它不会产生预期的结果。