这是一个特定于 JGit api 的问题。
我想在 JGit 中复制以下 Git 命令的相同行为:
git fetch --prune --prune-tags
,基本上从本地存储库中删除远程中不再存在的标签。JGit 有这个功能:
gitRepo.fetch().setRemoveDeletedRefs(true).call()
List<String> remoteTagsList = gitRepo.lsRemote().setTags(true)
.call()
.stream()
.map(rawTag -> rawTag.getName())
.collect(Collectors.toList());
List<String> localTagsList = gitRepo
.getRepository()
.getTags()
.values()
.stream().map(rawTag -> rawTag.getName())
.collect(Collectors.toList());
localTagsList.removeAll(remoteTagsList);
if (!localTagsList.isEmpty()) {
List<String> removedTags = gitRepo.tagDelete().setTags(localTagsList.toArray(new String[localTagsList.size()]))
.call();
logger.info("While pulling, the following tags have been removed: {}", StringUtils.join(removedTags, ","));
}