2017-11-21 4 views
1

複雑なMercurialクエリをGitに変換しようとしています。 JGitを使用して、コード内のクエリを手作業で作成せずに同じことを達成できることがわかりました。目的は、ファイルがブランチで変更され、マージを除外した場合に、パスフィルタに基づいて最新のリビジョンIDを取得することです。私は一種のTreeWalkを取得した後にブロックされていますJGitを使用して特定のブランチでマージを除いた特定のパスの最新のリビジョン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 
      } 
     } 
    } 
} 
} 

:これは私がこれまで持っているものです。どんな助けもありがとう!

編集: これは私が変換に取り組んでいる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} 
+0

。 – kostix

+0

JGitの使い方は分かりませんが、MercurialクエリーはGitに直接変換することはできません。なぜなら、「ブランチ(...)」のコンセプトは正しく翻訳されていないからです。Mercurialでは、いくつかのブランチ上にあります。これは*これだけの*ブランチですが、Gitでは、コミットの変更を動的に含むブランチ(複数)のセットが動的に変化します。 min()、max()、およびpublic()のようなフェーズ・テストも変換不可能です。 – torek

答えて

1

ヨールRevWalkセットアップは私が考えるかなり近いです。欠落しているのは、新しいコミットを最初に表示し、ウォークを開始する場所から 'ポイント'を設定するようにソート順を設定することです。あなたの場合、出発点は問題の支店になります。パス/に/ A/file`(すなわち、ただしリネーム検出を係合しない) - --no-マージ `Gitのログであろう普通のGitで

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(); 
関連する問題