gpt4 book ai didi

java - 如何使用 JGit 获取特定路径的最新修订 ID(不包括给定分支中的 merge )?

转载 作者:行者123 更新时间:2023-11-30 06:24:49 25 4
gpt4 key购买 nike

我正在努力将复杂的 Mercurial 查询转换为 Git。我发现 JGit 可以用来实现相同的目的,而无需在代码中手工制作查询。目标是如果文件在分支中被修改并排除 merge ,则根据路径过滤器获取最新的修订 ID。这是我到目前为止所拥有的:

public static void main(String[] args) throws IOException {
try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
Ref head = repository.getRef("HEAD");
//Use pathFilter to filter this just for maps directory
PathFilter pathFilter = PathFilter.create("directory/path/*")

RevWalk walk = new RevWalk(repository)

walk.setRevFilter(RevFilter.NO_MERGES)
walk.setTreeFilter(AndTreeFilter.create(PathFilterGroup.create(pathFilter))

RevCommit commit = walk.parseCommit(${REVISION});
RevTree tree = commit.getTree();
// now use a TreeWalk to iterate over all files in the Tree recursively and you can set Filters to narrow down the results if needed
try (TreeWalk treeWalk = new TreeWalk(repository)) {
treeWalk.addTree(tree);
while (treeWalk.next()) {
// Some custom logic here
}
}
}
}
}

在获得TreeWalk后我有点受阻。如有任何帮助,我们将不胜感激!

编辑:这是我正在转换的 Mercurial 查询:

hg log -r max((merge() and branch(${REVISION}) and ancestors(${REVISION}) " \
"and descendants(not branch(${REVISION}) and file('directory/path/*') " \
"and not ancestors(min(branch(${REVISION}))))) or max( file('directory/path/*') " \
"and branch(${REVISION}) and ancestors(${REVISION})) " \
"or min(branch(${REVISION})) and public() or p1(min(branch(${REVISION})))) --template {node}

最佳答案

我认为您的 RevWalk 设置非常接近。缺少的是设置排序顺序以首先显示较新的提交,并设置从何处开始遍历的“点”。在您的情况下,起点将是有问题的分支。

RevWalk walk = new RevWalk( repository )
walk.setRevFilter( RevFilter.NO_MERGES )
walk.setTreeFilter( AndTreeFilter.create( PathFilterGroup.create( pathFilter ) );

walk.sort( RevSort.COMMIT_TIME_DESC, true );
walk.markStart( walk.parseCommit( repository.resolve( "refs/heads/branch-name" ) );
if( walk.next() != null ) {
// now the RevWalk points to the newest commit in which the
// file was modified in the branch, excluding merges.
}

walk.close();

关于java - 如何使用 JGit 获取特定路径的最新修订 ID(不包括给定分支中的 merge )?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47416919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com