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

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

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

问题

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

  1. RevFilter revFilter = new RevFilter() {
  2. @Override
  3. public boolean include(RevWalk revWalk, RevCommit revCommit) throws StopWalkException, MissingObjectException, IncorrectObjectTypeException, IOException {
  4. if (revCommit.getParentCount() > 0) { // 不遍历父提交 }
  5. return false;
  6. }
  7. @Override
  8. public RevFilter clone() {
  9. return this;
  10. }
  11. }
英文:

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:

  1. RevFilter revFilter = new RevFilter() {
  2. @Override
  3. public boolean include(RevWalk revWalk, RevCommit revCommit) throws StopWalkException, MissingObjectException, IncorrectObjectTypeException, IOException {
  4. if (revCommit.getParentCount() > 0) { // do not traverse parent(s) }
  5. return false;
  6. }
  7. @Override
  8. public RevFilter clone() {
  9. return this;
  10. }
  11. }
  12. </details>
  13. # 答案1
  14. **得分**: 0
  15. 获取存储库中所有标签的HEAD提交您可以使用JGit返回所有标签然后解析每个标签的提交
  16. 可以查询`RefDatabase`以返回所有标签
  17. ```java
  18. Map<String, Ref> allTags = repository.getRefDatabase().getRefs("refs/tags/");

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

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

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:

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

Then use a RevWalk to resolve each tag ref:

  1. for (Ref tagRef : allTags.values()) {
  2. try (RevWalk walk = new RevWalk(repository)) {
  3. RevCommit commit = walk.parseCommit(tagRef.getObjectId();
  4. // ...
  5. }
  6. }

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:

确定