【发布时间】:2014-07-26 04:20:17
【问题描述】:
我正在尝试使用 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]
如果我检查分支是否仍在仓库中:
git.getRepository().getRef("myBranch");
我会收到true。
这是因为 cf to jgit javadoc:
getRef(name)
name 要查找的 ref 的名称。可以是简写形式,例如 "master" 自动扩展为 "refs/heads/master" 如果 “refs/heads/master”已经存在。
它正在检查“refs/heads/myBranch”而不是“myBranch”。
此外,如果我第二次运行deleteBranch 命令,deletedBranches 的值将是[refs/heads/myBranch]。
有人可以解释为什么会发生这种情况,我该如何解决这个问题? 谢谢。
更新
在jgit代码里面调试之后,我注意到了
String fullName = currentRef.getName();
返回“myBranch”而不是“heads/refs/myBranch”,因此它没有执行这段代码:
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 git-branch jgit