2012-05-03 22 views
7

La documentazione JGit privi non sembrano dire nulla su come utilizzare/rilevare rami mentre si utilizza un RevWalk.JGit: Come arrivare Branch quando attraversano repos

This question dice praticamente la stessa cosa.

Quindi la mia domanda è: come ottengo il nome/id del ramo da un RevCommit? O come posso specificare quale ramo attraversare in anticipo?

risposta

5

Ha trovato un modo migliore per farlo collegando i rami.

ho in loop sui rami chiamando

for (Ref branch : git.branchList().call()){ 
    git.checkout().setName(branch.getName()).call(); 
    // Then just revwalk as normal. 
} 
+0

+1 no attraversando un singolo ramo. La mia risposta era più di trovare il ramo dato un impegno. – VonC

2

Considerando l'attuale implementazione di JGit (vedere its git repo e la sua classe RevCommit), non ho trovato l'equivalente di ciò che è elencato in "Git: Finding what branch a commit came from".
Ie:

git branch --contains <commit> 

solo alcune delle opzioni di git branch sono implementati (come in ListBranchCommand.java).

+0

Sì, sembra che non v'è alcun supporto per andare da commettere -> branch, ma sai se è possibile attraversare solo i commit di un singolo ramo? – Braden

+0

+1 perché la tua risposta mi ha aiutato ad andare nella giusta direzione. Interessante vedere esattamente cosa hanno implementato. – Braden

1

potrebbe utilizzare sotto codice per ottenere "da" ramo per commit:

/** 
    * find out which branch that specified commit come from. 
    * 
    * @param commit 
    * @return branch name. 
    * @throws GitException 
    */ 
    public String getFromBranch(RevCommit commit) throws GitException{ 
     try { 
      Collection<ReflogEntry> entries = git.reflog().call(); 
      for (ReflogEntry entry:entries){ 
       if (!entry.getOldId().getName().equals(commit.getName())){ 
        continue; 
       } 

       CheckoutEntry checkOutEntry = entry.parseCheckout(); 
       if (checkOutEntry != null){ 
        return checkOutEntry.getFromBranch(); 
       } 
      } 

      return null; 
     } catch (Exception e) { 
      throw new GitException("fail to get ref log.", e); 
     } 
    } 
+0

Molto interessante, ma ho il sospetto che non sarebbe in scala per un grande repository. –