Cal*_*ers 5 java git version-control git-fetch jgit
我有一个位于 的裸仓库main.git,我试图foo在另一个仓库中获取一个分支(比如说)test,它刚刚被git init'd:
fetchtest/
|- main.git/
|- test/
|- .git/
Run Code Online (Sandbox Code Playgroud)
定期使用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();
Run Code Online (Sandbox Code Playgroud)
但它只是错误:
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)
Run Code Online (Sandbox Code Playgroud)
我如何让这个工作?
什么应该起作用:
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();
Run Code Online (Sandbox Code Playgroud)
注意RefSpec定义。
至少,在你的例子中尝试:
new RefSpec("refs/heads/foo:refs/heads/foo")
Run Code Online (Sandbox Code Playgroud)
在RefSpec类中提到:
/**
* 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.
*/
Run Code Online (Sandbox Code Playgroud)
所以“ refs/head/”似乎是强制性的。
原答案:
在setRemote()功能上api.FetchCommand需要一个名称或URI。
查看FetchCommandTestURI 定义,我更喜欢使远程更可见:
我宁愿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();
Run Code Online (Sandbox Code Playgroud)