2012-08-09 129 views
1

我无法弄清楚如何删除远程分支。如何使用JGit删除远程分支

我试图模仿下面的GIT命令: 混帐推产地:branchToDelete

下面的代码,并将其与空的源变化:

RefSpec refSpec = new RefSpec(); 
refSpec = refSpec.setSource(""); 
// remove branch from origin: 
git.push().setRefSpecs(refSpec).add(branchToDelete).call(); 

抛出和异常,如:

org.eclipse.jgit.api.errors.JGitInternalException: Exception caught during execution of push command 
    at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:175) 
    at org.gitscripts.DeleteBranchOperation.execute(DeleteBranchOperation.java:27) 
    at org.gitscripts.Main.main(Main.java:27) 
Caused by: java.io.IOException: Source ref doesnt resolve to any object. 
    at org.eclipse.jgit.transport.RemoteRefUpdate.<init>(RemoteRefUpdate.java:285) 
    at org.eclipse.jgit.transport.RemoteRefUpdate.<init>(RemoteRefUpdate.java:189) 
    at org.eclipse.jgit.transport.Transport.findRemoteRefUpdatesFor(Transport.java:612) 
    at org.eclipse.jgit.transport.Transport.findRemoteRefUpdatesFor(Transport.java:1150) 
    at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:149) 
    ... 2 more 

在此先感谢您的想法和解决方案。

+0

从你的错误看来你的refSpec有问题。你确定它是正确的? – 2012-08-09 22:18:43

回答

2

根据定期的git语法,你的RefSpec()不应该是::branchToDelete

+0

是的,使用'new RefSpec(“:branchToDelete”)或'new RefSpec()。setSource(“”)。setDestination(“branchToDelete”)'。 – robinst 2012-08-10 08:09:09

+0

@Vince不,如果源为空,则意味着应该在远程删除目标分支。 (这就是问题所在。) – robinst 2012-08-10 10:55:57

+0

好吧,我宁愿删除注释以避免错误,然后 – Vince 2012-08-10 11:24:29

1

我从来没有这样做过,但是您是否简单地通过指定origin/branchToDelete来尝试一个DeleteBranchCommand?编辑:我特别指Git/JGit通过结构<remote name>/<branch name>引用远程分支(并且使用ListBranchCommand将帮助您确保您得到正确的拼写)。

要知道分支名称的确切拼写,您可以使用ListBranchCommand(不要忘记拨打setListMode(REMOTE))。

注意:Git允许比JGit更奇怪的行为,所以除非写入某处,否则不要期待它们。编辑:我的意思是一个refspec应该有以下语法:<remote branch>:<local branch>(或可能相反),但不要指望它在JGit中的作品,如果你错过了一端,即使它在Git中工作。

+0

OP不想只在本地删除远程跟踪分支,而是推送分支删除。 – robinst 2012-08-10 08:06:30

+0

是的,我明白了。我的意思是你可以显示远程分支(不同于本地分支跟踪它),然后删除远程分支。顺便说一下,refspec会指定本地跟踪分支和远程分支之间的链接。我编辑了我的答案,以便更好地理解 – Vince 2012-08-10 08:23:02

+0

辉煌!将'origin/branchToDelete'(或者确切地说,'refs/remotes/origin/branchToDelete')传递给'DeleteBranchCommand'工作。 与JGit一起工作的关键是:不要试图模仿GIT命令 – 2012-08-13 14:11:24

6

这应该做您排忧解难:

//delete branch 'branchToDelete' locally 
git.branchDelete().setBranchNames('refs/heads/branchToDelete').call(); 

//delete branch 'branchToDelete' on remote 'origin' 
RefSpec refSpec = new RefSpec() 
     .setSource(null) 
     .setDestination("refs/heads/branchToDelete"); 
git.push().setRefSpecs(refSpec).setRemote('origin').call(); 

与jgit 2.0.0.201206130900-R测试

0

我可以使它与这方面的工作:

StoredConfig config = git.getRepository().getConfig(); 
config.unsetSection("remote", "origin"); 
try { 
    config.save(); 
} catch (IOException e) { 
    logger.error(e.getMessage()); 
} 

希望它可以帮助。

相关问题