使用 JGit 只获取一个标签
Fetch only a single tag with JGit
在 git 控制台上,我可以执行以下操作以仅从远程存储库中获取单个标签(未添加远程):
git fetch -n git@github.company.com:organization/repo.git tag mytag
我想用JGit试试,但是我解决不了。
fetcher.remote = remoteName // (in this case remote is already added)
fetcher.setRefSpecs("${opts.mytagname}")
fetcher.setTagOpt(TagOpt.NO_TAGS)
fetcher.call()
不幸的是,它不起作用。有什么建议吗?
为了获取特定标签,您需要提供一个明确要求此标签的引用规范。使用 JGit 也可以从未配置的远程获取。 FetchCommand::setRemote
方法接受已知(即已配置)远程的名称,如 origin
,或完整的 URL 到远程存储库。
两者都在以下测试中得到了说明:
public class FetchTagTest {
@Rule
public final TemporaryFolder tempFolder = new TemporaryFolder();
private Git remote;
private Git local;
@Before
public void setUp() throws Exception {
File remoteDirectory = tempFolder.newFolder( "remote" );
File localDirectory = tempFolder.newFolder( "local" );
remote = Git.init().setDirectory( remoteDirectory ).call();
local = Git.cloneRepository().setURI( remoteDirectory.getCanonicalPath() ).setDirectory( localDirectory ).call();
}
@After
public void tearDown() {
local.getRepository().close();
remote.getRepository().close();
}
@Test
public void testFetchTag() throws Exception {
remote.commit().setMessage( "tag me!" ).call();
Ref remoteTag = remote.tag().setName( "tag" ).call();
local.fetch()
.setRemote( remote.getRepository().getDirectory().getCanonicalPath() )
.setRefSpecs( new RefSpec( "refs/tags/tag:refs/tags/tag" ) )
.call();
List<Ref> localTags = local.tagList().call();
assertEquals( 1, localTags.size() );
assertEquals( remoteTag.getName(), localTags.get( 0 ).getName() );
assertEquals( remoteTag.getObjectId(), localTags.get( 0 ).getObjectId() );
}
}
在 git 控制台上,我可以执行以下操作以仅从远程存储库中获取单个标签(未添加远程):
git fetch -n git@github.company.com:organization/repo.git tag mytag
我想用JGit试试,但是我解决不了。
fetcher.remote = remoteName // (in this case remote is already added)
fetcher.setRefSpecs("${opts.mytagname}")
fetcher.setTagOpt(TagOpt.NO_TAGS)
fetcher.call()
不幸的是,它不起作用。有什么建议吗?
为了获取特定标签,您需要提供一个明确要求此标签的引用规范。使用 JGit 也可以从未配置的远程获取。 FetchCommand::setRemote
方法接受已知(即已配置)远程的名称,如 origin
,或完整的 URL 到远程存储库。
两者都在以下测试中得到了说明:
public class FetchTagTest {
@Rule
public final TemporaryFolder tempFolder = new TemporaryFolder();
private Git remote;
private Git local;
@Before
public void setUp() throws Exception {
File remoteDirectory = tempFolder.newFolder( "remote" );
File localDirectory = tempFolder.newFolder( "local" );
remote = Git.init().setDirectory( remoteDirectory ).call();
local = Git.cloneRepository().setURI( remoteDirectory.getCanonicalPath() ).setDirectory( localDirectory ).call();
}
@After
public void tearDown() {
local.getRepository().close();
remote.getRepository().close();
}
@Test
public void testFetchTag() throws Exception {
remote.commit().setMessage( "tag me!" ).call();
Ref remoteTag = remote.tag().setName( "tag" ).call();
local.fetch()
.setRemote( remote.getRepository().getDirectory().getCanonicalPath() )
.setRefSpecs( new RefSpec( "refs/tags/tag:refs/tags/tag" ) )
.call();
List<Ref> localTags = local.tagList().call();
assertEquals( 1, localTags.size() );
assertEquals( remoteTag.getName(), localTags.get( 0 ).getName() );
assertEquals( remoteTag.getObjectId(), localTags.get( 0 ).getObjectId() );
}
}