java – Git fetch使用jgit失败:Remote没有可用于获取
作者:互联网
我有一个位于main.git的裸仓库,我试图在另一个仓库中测试一个分支(foo,让我们说),测试,它只是刚刚被git init’d:
fetchtest/
|- main.git/
|- test/
|- .git/
使用常规git命令,我可以执行git fetch ../main.git foo:foo,这将在test /中创建一个新的分支foo并获取分支所需的对象.然后我想做同样的事情但是以编程方式使用JGit,即不使用git CLI但只使用Java代码.我无法使用git CLI:
Git git = Git.init().setDirectory(new File("fetchtest/test/")).call();
git.fetch().setRemote(new File("../main.git"))
.setRefSpecs(new RefSpec("foo:foo"))
.call();
但它只是错误:
org.eclipse.jgit.api.errors.TransportException: Remote does not have foo available for fetch.
at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:137)
// ......
Caused by: org.eclipse.jgit.errors.TransportException: Remote does not have foo available for fetch.
at org.eclipse.jgit.transport.FetchProcess.expandSingle(FetchProcess.java:349)
at org.eclipse.jgit.transport.FetchProcess.executeImp(FetchProcess.java:139)
at org.eclipse.jgit.transport.FetchProcess.execute(FetchProcess.java:113)
at org.eclipse.jgit.transport.Transport.fetch(Transport.java:1069)
at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:128)
我如何让它工作?
解决方法:
应该做些什么:
Git git = Git.init().setDirectory(new File("fetchtest/test/")).call();
git.fetch().setRemote(new File("../main.git"))
.setRefSpecs(new RefSpec("refs/heads/foo:refs/heads/foo"))
.call();
请注意RefSpec定义.
至少,试试你的例子:
new RefSpec("refs/heads/foo:refs/heads/foo")
/**
* Parse a ref specification for use during transport operations.
* <p>
* Specifications are typically one of the following forms:
* <ul>
* <li><code>refs/head/master</code></li>
* <li><code>refs/head/master:refs/remotes/origin/master</code></li>
* <li><code>refs/head/*:refs/remotes/origin/*</code></li>
* <li><code>+refs/head/master</code></li>
* <li><code>+refs/head/master:refs/remotes/origin/master</code></li>
* <li><code>+refs/head/*:refs/remotes/origin/*</code></li>
* <li><code>:refs/head/master</code></li>
* </ul>
*
* @param spec
* string describing the specification.
* @throws IllegalArgumentException
* the specification is invalid.
*/
所以“refs / head /”似乎是强制性的.
原始答案:
setRemote()
function on api.FetchCommand
采用名称或URI.
看一下FetchCommandTest
的URI定义,我更喜欢让遥控器更加明显:
我宁愿为你的第二个回购(引用你的第一个回购)定义一个命名的遥控器(在下面:“test”),然后获取.
// setup the first repository to fetch from the second repository
final StoredConfig config = db.getConfig();
RemoteConfig remoteConfig = new RemoteConfig(config, "test");
URIish uri = new URIish(db2.getDirectory().toURI().toURL());
remoteConfig.addURI(uri);
remoteConfig.update(config);
config.save();
// create some refs via commits and tag
RevCommit commit = git2.commit().setMessage("initial commit").call();
Ref tagRef = git2.tag().setName("tag").call();
Git git1 = new Git(db);
RefSpec spec = new RefSpec("refs/heads/master:refs/heads/x");
git1.fetch().setRemote("test").setRefSpecs(spec)
.call();
标签:java,git,version-control,jgit,git-fetch 来源: https://codeday.me/bug/20190626/1289703.html