2013-04-04 50 views

回答

9

第一次嘗試這樣的位置:https://stackoverflow.com/a/13925765/2246865

但它並不總是工作,所以我發現這個在這裏:http://www.eclipse.org/forums/index.php/t/280339/

這裏我的解決方案,它是不是真的很好,但它的工作.. 。

public static void main(String[] args) throws IOException, GitAPIException { 
    Repository repo = new FileRepository("pathToRepo/.git"); 
    Git git = new Git(repo); 
    RevWalk walk = new RevWalk(repo); 

    List<Ref> branches = git.branchList().call(); 

    for (Ref branch : branches) { 
     String branchName = branch.getName(); 

     System.out.println("Commits of branch: " + branch.getName()); 
     System.out.println("-------------------------------------"); 

     Iterable<RevCommit> commits = git.log().all().call(); 

     for (RevCommit commit : commits) { 
      boolean foundInThisBranch = false; 

      RevCommit targetCommit = walk.parseCommit(repo.resolve(
        commit.getName())); 
      for (Map.Entry<String, Ref> e : repo.getAllRefs().entrySet()) { 
       if (e.getKey().startsWith(Constants.R_HEADS)) { 
        if (walk.isMergedInto(targetCommit, walk.parseCommit(
          e.getValue().getObjectId()))) { 
         String foundInBranch = e.getValue().getName(); 
         if (branchName.equals(foundInBranch)) { 
          foundInThisBranch = true; 
          break; 
         } 
        } 
       } 
      } 

      if (foundInThisBranch) { 
       System.out.println(commit.getName()); 
       System.out.println(commit.getAuthorIdent().getName()); 
       System.out.println(new Date(commit.getCommitTime() * 1000L)); 
       System.out.println(commit.getFullMessage()); 
      } 
     } 
    } 
} 
+0

我有這樣的解決方案,它需要大約五分鐘,芬蘭對我相當大的回購問題。 – user3495816 2016-02-02 09:51:07

+0

請嘗試一些其他解決方案,這裏張貼。 – hurik 2016-04-08 09:53:30

0

你是對的,文檔應該真的更好。

您可以使用JGit Log command或使用像gitective這樣的庫,這使得迭代提交更容易。您可以查看源代碼以瞭解有關JGit的更多信息。

this SO-question中描述了其他(更復雜的)方法。

8

有了下面你的代碼可以得到所有分支或標記的提交:

Repository repository = new FileRepository("/path/to/repository/.git"); 
String treeName = "refs/heads/master"; // tag or branch 
for (RevCommit commit : git.log().add(repository.resolve(treeName)).call()) { 
    System.out.println(commit.getName()); 
} 

Varialbe treeName將定義標籤或分支。此treeName是分支或標記的完整名稱,例如refs/heads/master代表代表分支或refs/tags/v1.0代表標記v1.0

或者,您可以使用gitective API。下面的代碼不一樣上面的代碼:

Repository repository = new FileRepository("/path/to/repository/.git"); 

AndCommitFilter filters = new AndCommitFilter(); 
CommitListFilter revCommits = new CommitListFilter(); 
filters.add(revCommits); 

CommitFinder finder = new CommitFinder(repository); 
finder.setFilter(filters); 
String treeName = "refs/heads/master"; // tag or branch 
finder.findFrom(treeName); 

for (RevCommit commit : revCommits) { 
    System.out.println(commit.getName()); 
} 

一些try/catch語句將是必要的,我隱藏起來,使代碼更短。 祝你好運。

+0

如果分支從一個以上的其他分支/提交中合併,這項工作是否可行? – tgkprog 2017-01-31 10:25:12

+0

工程,與合併。謝謝@ felipe-gomes – tgkprog 2017-01-31 13:53:05

+0

很高興我的解決方案爲您工作@ tgkprog。如果想要一些示例代碼來幫助您使用jgit,我會建議您查看此存儲庫https://github.com/centic9/jgit-cookbook。 – 2017-02-02 12:54:09

0

一個更短的和清晰的解決方案可以通過「日誌」命令JGit提供完成:

Repository repository = new FileRepository("pathToRepo/.git"); 
    logs = new Git(repository).log() 
      .add(repository.resolve("remotes/origin/testbranch")) 
      .call(); 
    count = 0; 
    for (RevCommit rev : logs) { 
     System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */); 
     count++; 
    } 
    System.out.println("Had " + count + " commits overall on test-branch"); 

full snippetjgit-cookbook

+0

當我爲我的分支做這件事時,我看到它和主人的提交。當使用主人時,我只看到它的提交。我期望「 - 停止複製」行爲,但這不是我所看到的git.log()。add(git.repository.resolve(「refs/heads/mybranch」))。call() – 2016-02-10 22:10:20

+0

好的,那麼答案http://stackoverflow.com/a/35327138/411846中列出的addRange()選項應該可以工作。 – centic 2016-02-11 11:20:16

+0

謝謝。我想我需要得到分支的頭,然後走回來,直到我找到一個RevCommit,其中所有的父母都來自另一個分支。沿途任何提交都可能有來自其他分支的多個父母,但只有來自另一個分支的父母的提交必須是分支的開頭。 – 2016-02-11 16:07:21

0

這似乎是工作,但我想補充(的ObjectId分支),以提供柵極到活躍的分支,但我似乎需要一系列

Git git = ... 
ObjectId master = git.repository.resolve("refs/heads/master") 
ObjectId branch = git.repository.resolve("refs/heads/mybranch") 

git.log().addRange(master,branch).call() 
1

我只是在尋找一種方式,列出在EA所有提交ch分支,我發現這個線程。我終於做了一些與我在這裏找到並且工作的一些答案有所不同的東西。

public static void main(String[] args) throws IOException, GitAPIException { 
    Repository repo = new FileRepository("pathToRepo/.git"); 
    Git git = new Git(repo); 

    List<Ref> branches = git.branchList().call(); 

    for (Ref branch : branches) { 
     String branchName = branch.getName(); 

     System.out.println("Commits of branch: " + branchName); 
     System.out.println("-------------------------------------"); 

     Iterable<RevCommit> commits = git.log().add(repo.resolve(branchName)).call(); 

     List<RevCommit> commitsList = Lists.newArrayList(commits.iterator()); 

     for (RevCommit commit : commitsList) { 
      System.out.println(commit.getName()); 
      System.out.println(commit.getAuthorIdent().getName()); 
      System.out.println(new Date(commit.getCommitTime() * 1000L)); 
      System.out.println(commit.getFullMessage()); 
     } 
    } 

    git.close(); 
} 

感謝您的幫助

相關問題