使用jgit删除分支不按预期工作

时间:2014-06-05 08:51:45

标签: git branch git-branch jgit

我正在尝试使用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到jgit javadoc:

getRef(name)

  

名称要查找的引用的名称。可能是一种简写形式,例如   &#34;主&#34;这是自动扩展到&#34; refs / heads / master&#34;如果   &#34;参考文献/头/主&#34;已经存在。

正在检查&#34; refs / heads / myBranch&#34;而不是&#34; myBranch&#34;。

此外,如果我第二次运行deleteBranch命令,deletedBranches的值将为[refs/heads/myBranch]

有人可以解释为什么会发生这种情况,我该如何解决这个问题? 谢谢。

更新

在jgit代码中调试之后,我注意到了

String fullName = currentRef.getName();

https://github.com/eclipse/jgit/blob/a76a4acf87952249b94f4be29614565541eb8c46/org.eclipse.jgit/src/org/eclipse/jgit/api/DeleteBranchCommand.java#L133

返回&#34; myBranch&#34;而不是&#34; heads / refs / myBranch&#34;因此它没有执行这段代码:

                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();
                }

https://github.com/eclipse/jgit/blob/a76a4acf87952249b94f4be29614565541eb8c46/org.eclipse.jgit/src/org/eclipse/jgit/api/DeleteBranchCommand.java#L158-L167

1 个答案:

答案 0 :(得分:0)

它可能类似于git branch -d,它将拒绝删除未在其上游分支中完全合并的分支。

这就是您在org.eclipse.jgit.test/tst/org/eclipse/jgit/api/BranchCommandTest中看到的示例正在使用setForce(true)(如git branch -D)的原因:

List<String> deleted = git.branchDelete().setBranchNames("ForDelete")
            .setForce(true).call();
相关问题