我有一个位于main.git
的裸仓,我正试图在另一个仓库foo
中取一个分支(test
,让我们说),这只是git init
}}'d:
fetchtest/
|- main.git/
|- test/
|- .git/
使用常规git命令,我可以执行git fetch ../main.git foo:foo
,这将在foo
中创建一个新分支test/
并获取分支所需的对象。 然后我想做同样的事情但是以编程方式使用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)
如何让它发挥作用?
答案 0 :(得分:4)
应该做些什么:
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();