JGit,删除不再在远程的标签

问题描述 投票:0回答:1

这是一个特定于 JGit api 的问题。
我想在 JGit 中复制以下 Git 命令的相同行为:

git fetch --prune --prune-tags
,基本上从本地存储库中删除远程中不再存在的标签。
我在 API

中看不到这个选项
jgit
1个回答
0
投票

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, ","));
  }
© www.soinside.com 2019 - 2024. All rights reserved.