可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
how do I get all commits of a branch with JGit, without changing the working directory?
Unfortunately the JGit docs are not very good ...
In ruby with grit it is very easy:
repo = Grit::Repo.new(pathToRepo) repo.commits(branchName, false).each do |commit| doSomethingWithTheCommit end
Bye, hurik
回答1:
With the code below you can get all commits of a branch or tag:
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()); }
The varialbe treeName will define the tag or branch. This treeName is the complete name of the branch or tag, for example refs/heads/master for the master branch or refs/tags/v1.0 for a tag called v1.0.
Alternatively, you can use the gitective API. The following code does the same as the code above:
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()); }
Some try/catch will be necessary, I hide them to make the code shorter. Good luck.
回答2:
Tried this here first: https://stackoverflow.com/a/13925765/2246865
But it wasn't was working always, so I found this here: http://www.eclipse.org/forums/index.php/t/280339/
Here my solution, it isn't really nice, but it's working ...
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()); } } } }
回答3:
I was just looking for a way to list all commits under each branch and I found this thread. I finally did something a bit different from some of the answers I found here and worked.
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(); }
Thanks for the help
回答4:
You are right, the docs should really be better.
You could use the JGit Log command or use a library like gitective which makes iterating over commits easy. You can look at the source to learn more about JGit.
Other (more complicated) ways are described in this SO-question.
回答5:
A much shorter and clean solution can be done via the "log" command that JGit provides:
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");
See the full snippet at the jgit-cookbook
回答6:
This seems to work but I thought add(ObjectId branch) to provide a gate to the active branch, but I appear to require a range
Git git = ... ObjectId master = git.repository.resolve("refs/heads/master") ObjectId branch = git.repository.resolve("refs/heads/mybranch") git.log().addRange(master,branch).call()