我需要用 Java 编写一段代码来测试 Git 存储库中是否存在特定标签。
最明显的方法是这样的:
git = Git.cloneRepository()
.setURI(gitUri)
.setDirectory(dir)
.call();
git.checkout().setName(String.format("refs/tags/%s", version)).call();
如果标签version
不存在,则会抛出异常。
但是这种方式要求我有一个目录(dir
),可以在其中 check out 存储库。
是否可以在不检查磁盘的情况下找出远程存储库中是否存在标签?如果是,我该怎么做?
最佳答案
LsRemoteCommand
能够列出远程存储库拥有的引用。
该命令可以配置为包含如下标签:
LsRemoteCommand ls = Git.lsRemoteRepository();
Collection<Ref> remoteRefs = ls
.setRemote("url-to-remote-repo")
.setHeads(true) // true by default, set to false if not interested in refs/heads/*
.setTags(true) // include tags in result
.call();
如果您希望获取引用名称到 Ref
对象的映射,可以使用 callAsMap()
执行命令。
关于java - JGit:我可以在不检查的情况下找出 Git 存储库中是否存在特定标签吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60417504/