英文:
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<String, Ref> allTags repository.getRefDatabase().getRefs("refs/tags/");
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();
// ...
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论