如何在 JGit 中执行 git log –no-walk –tags。

huangapple go评论86阅读模式
英文:

How to do git log --no-walk --tags in JGit

问题

我想使用 JGit 中的 LogCommand 来执行所提到的命令。我该如何编写能够完成这个工作的自定义过滤器呢?我已经做了类似以下的工作:

RevFilter revFilter = new RevFilter() {
  @Override
  public boolean include(RevWalk revWalk, RevCommit revCommit) throws StopWalkException, MissingObjectException, IncorrectObjectTypeException, IOException {
    if (revCommit.getParentCount() > 0) { // 不遍历父提交 }
    return false;
  }

  @Override
  public RevFilter clone() {
    return this;
  }
}
英文:

I would like to use LogCommand from JGit to execute the mentioned command. How can I write the custom filter which can do the job? I have done something like:

RevFilter revFilter = new RevFilter() {
  @Override
  public boolean include(RevWalk revWalk, RevCommit revCommit) throws StopWalkException, MissingObjectException, IncorrectObjectTypeException, IOException {
    if (revCommit.getParentCount() > 0) { // do not traverse parent(s) }
    return false;
  }

  @Override
  public RevFilter clone() {
    return this;
  }
}

</details>


# 答案1
**得分**: 0

获取存储库中所有标签的HEAD提交您可以使用JGit返回所有标签然后解析每个标签的提交

可以查询`RefDatabase`以返回所有标签

```java
Map<String, Ref> allTags = repository.getRefDatabase().getRefs("refs/tags/");

然后使用RevWalk来解析每个标签引用:

for (Ref tagRef : allTags.values()) {
  try (RevWalk walk = new RevWalk(repository)) {
    RevCommit commit = walk.parseCommit(tagRef.getObjectId());
    // ...
  }
}
英文:

To get the HEAD commits of all tags in a repository, you can use JGit to return all tags and then resolve the commit of each tag.

The RefDatabase can be queried to return all tags:

Map&lt;String, Ref&gt; allTags repository.getRefDatabase().getRefs(&quot;refs/tags/&quot;);

Then use a RevWalk to resolve each tag ref:

for (Ref tagRef : allTags.values()) {
  try (RevWalk walk = new RevWalk(repository)) {
    RevCommit commit = walk.parseCommit(tagRef.getObjectId();
    // ...
  }
}

huangapple
  • 本文由 发表于 2020年4月6日 19:44:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/61059026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定