2012-12-27 50 views
1

我有一個開始提交,我想從中找到所有分支,直到達到另一個沒有找到提交提示。找到2個git提交之間可用的分支

commit 1 
| 
commit 2 
|   commit5 
commit3/
|  /
commit 4 
| 
commit 6 
在這種情況下

說所有提交的提交1-5有筆記「中找到分支」 ,並承諾6沒有一個不與價值。

因此,我會從提交1開始,找到所有的父(即:提交2),並嘗試檢查是否有一個分支爲此提交(即:孩子的數量超過1)。如果有多於1名兒童的

  1. getChildren()方法有僅PlotCommit對象,但該方法parentCommit.getParents()僅返回RevCommit對象。
  2. 我想找到存在於特定的分支名提交

那麼當有對提交沒有更多的票據(即提交6可是沒有註釋)的邏輯將停在那裏和分行名稱的集合返回

Repository repo;//will be set as part of some other logic 
    private Set findBranchesForCommit(PlotCommit parentCommit, String note) throws ExecutionException, MissingObjectException, IncorrectObjectTypeException, IOException { 
     Set branches = new HashSet(); 
     PlotCommit[] parents = (PlotCommit[]) parentCommit.getParents();//XXX will throw exception as this return RevCommit[] 
     for (int i = 0; i < parents .length; i++) { 
      PlotCommit commit = parents[i]; 
      String result = extractExistingMessage(repo, "refs/notes", commit);//will return the if the note available for particular commit 
      if (result.trim().length() > 0 && result.equalsIgnoreCase(note)) { 
       System.out.println("#########"+commit.getChildCount()); 
       //TODO need to add logic to find the branch of the particular commit 
       branches.add(""); //add the branches available for the commit 
       branches.addAll(findBranchesForCommit(commit, note)); 
      } 
     } 
     return branches; 
    } 

預期的結果

我想找到包含特定的git記提交的分支名稱。在上述例子中的分支名提交1和提交5將返回

+1

我理解你的問題的問題。你只是在尋找'git branch --contains commit6'? – michas

+0

總之。我想找到包含特定git note的提交的分支名稱。在上面的例子中,將返回Commit 1和Commit 5的分支名稱 –

回答

1

儘管這種要求git的命令(查找對於給定的一個分支提交)是:

git branch --contains <commit> 

(如在「 Git: Finding what branch a commit came from「和」How to list branches that contain a given commit?「)

它沒有在JGit中實現。

This thread有此建議:

git branch --contains <commit>」將包含該提交每個分局報案。
在所有提交的標準推/取工作流程中,您只能從遠程獲得「origin/master」報告。
甚至對於本地提交:想象一下,您已將feature分支合併回master。然後,此命令還將報告合併後創建的master分支和所有feature分支。
Git根本不存儲它創建在哪個分支上的修訂。

這些警告後:粗糙實施的「混帳分支--contains」看起來是這樣的:

Repository repo = new FileRepository(args[0]); 
RevWalk walk = new RevWalk(repo); 
RevCommit commit = walk.parseCommit(repo.resolve(args[1] + "^0")); 
for (Map.Entry<String, Ref> e : repo.getAllRefs().entrySet()) 
    if (e.getKey().startsWith(Constants.R_HEADS)) 
    if (walk.isMergedInto(commit, 
     walk.parseCommit(e.getValue().getObjectId()))) 
     System.out.println("Ref " + e.getValue().getName() 
           + " contains commit " + commit);