使用jgit删除分支无法正常工作 [英] Delete branch with jgit not working as expected
问题描述
我正在尝试使用jgit删除我的仓库中的一个分支.
I am trying to delete a branch in my repo using jgit.
DeleteBranchCommand command = git.branchDelete();
command.setBranchNames("myBranch");
command.setForce(true);
try {
List<String> deletedBranches = new ArrayList<String>();
deletedBranches = command.call();
System.out.println(deletedBranches.toString());
} catch (GitAPIException e) {
throw new MyInternalErrorException("Error while deleting branch [" + branchName + "]", e);
}
deletedBranches
的值将为 [myBranch]
如果我检查分支是否仍在回购中:
If I check if the branch is still in the repo:
git.getRepository().getRef("myBranch");
我将得到 true
.这是因为cf to jgit javadoc:
I will get true
.
And this is because cf to jgit javadoc:
getRef(name)
名称要查找的引用的名称.可能是简写形式,例如如果出现以下情况,主"将自动扩展为参考/标题/主""refs/heads/master"已经存在.
name the name of the ref to lookup. May be a short-hand form, e.g. "master" which is is automatically expanded to "refs/heads/master" if "refs/heads/master" already exists.
它正在检查"refs/heads/myBranch"而不是"myBranch".
It is checking for "refs/heads/myBranch" instead of "myBranch".
此外,如果我第二次运行 deleteBranch
命令,则 deletedBranches
的值将为 [refs/heads/myBranch]
Moreover, if I will run the deleteBranch
command the second time, the value for deletedBranches
will be [refs/heads/myBranch]
.
有人可以解释为什么会这样吗,我该如何解决这个问题?谢谢.
Can someone explain why is this happening and how can I solve this issue? Thank you.
更新
在jgit代码中调试之后,我注意到了
After debugging inside the jgit code, I've noticed that
String fullName = currentRef.getName();
返回"myBranch"而不是"heads/refs/myBranch",因此它不执行以下代码:
returns "myBranch" instead of "heads/refs/myBranch" thus it is not executing this piece of code:
if (fullName.startsWith(Constants.R_HEADS)) {
String shortenedName = fullName
.substring(Constants.R_HEADS.length());
// remove upstream configuration if any
final StoredConfig cfg = repo.getConfig();
cfg.unsetSection(
ConfigConstants.CONFIG_BRANCH_SECTION,
shortenedName);
cfg.save();
}
推荐答案
它可能类似于 git branch -d
,它将拒绝删除未在其上游分支中完全合并的分支.
It could be similar to git branch -d
, which will refuse to delete a branch not fully merged in its upstream branch.
That is why the examples you see in org.eclipse.jgit.test/tst/org/eclipse/jgit/api/BranchCommandTest
are using setForce(true)
(like a git branch -D
):
List<String> deleted = git.branchDelete().setBranchNames("ForDelete")
.setForce(true).call();
这篇关于使用jgit删除分支无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!