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
On GitHub.com, you can access your project history by selecting the commit button from the code tab on your project. Locally, you can use git log . The git log command enables you to display a list of all of the commits on your current branch. By default, the git log command presents a lot of information all at once.
`git log` command is used to view the commit history and display the necessary information of the git repository. This command displays the latest git commits information in chronological order, and the last commit will be displayed first.
Use git log --all <filename> to view the commits influencing <filename> in all branches.
To confirm, you can run git branch . The branch that you are on will be the one with a * next to it. Git checkout might fail with an error message, e.g. if it would overwrite modified files. Git branch should show you the current branch and git log master allows you to view commit logs without changing the branch.
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
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());
}
}
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With