使用jgit Git获取失败:远程没有< branchname>可用于获取 [英] Git fetch failing using jgit: Remote does not have <branchname> available for fetch
问题描述
我有一个位于 main.git
的裸回购,并试图获取一个分支( foo
,让我们在另一个回购中, test
,它只是 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(新文件(fetchtest / test /))。call();
git.fetch()。setRemote(new File(../ main.git))
.setRefSpecs(new RefSpec(foo:foo))
。呼叫();
但只是出现错误:
org.eclipse.jgit.api.errors.TransportException:远程没有foo可用于提取。
在org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:137)
// ......
引起:org.eclipse.jgit.errors。 TransportException:远程没有foo可用于提取。
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)
RefSpec
class 提及:
/ **
*解析运输操作中使用的参考规格。
*< p>
*规格通常是以下形式之一:
*< 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
*描述规格的字符串。
* @throws IllegalArgumentException
*规范无效。
* /
所以 refs / head /
似乎是强制性的。
原始答案:
< api.FetchCommand 上的nofollow>
setRemote()
函数需要一个名称或URI。 / p> 然后查看 FetchCommandTest
URI定义,我更喜欢让远程更加可见:
我宁愿为你的第二个仓库(指你的第一个仓库)定义一个命名的远程仓库(这里下面是: test
),然后获取。
//设置第一个存储库以从第二个存储库中获取
final StoredConfig config = db.getConfig();
RemoteConfig remoteConfig =新的RemoteConfig(config,test);
URIish uri = new URIish(db2.getDirectory()。toURI()。toURL());
remoteConfig.addURI(uri);
remoteConfig.update(config);
config.save();
//通过提交和标记创建一些refs
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();
I have a bare repo located at main.git
and am trying to fetch a branch (foo
, let's say) in another repo, test
, which has only just been git init
'd:
fetchtest/
|- main.git/
|- test/
|- .git/
Using regular git commands, I can do a git fetch ../main.git foo:foo
and this will make a new branch foo
in test/
and fetch the objects required for the branch. I then want to do the same thing but programmatically using JGit, ie not using the git CLI but using only Java code. There is no way I can use the 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();
but it just errors with:
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)
How do I get this to work?
What should work:
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();
Note the RefSpec
definition.
At least, try in your example:
new RefSpec("refs/heads/foo:refs/heads/foo")
The RefSpec
class mentions:
/**
* 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.
*/
So "refs/head/
" seems mandatory.
Original answer:
The setRemote()
function on api.FetchCommand
takes a name or an URI.
And looking at the FetchCommandTest
URI definition, I prefer making the remote more visible:
I would rather define a named remote (here below: "test
") for your second repo (referring your first repo), and then fetch.
// 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();
这篇关于使用jgit Git获取失败:远程没有< branchname>可用于获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!