Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JGit: How to get all commits of a branch? (Without changes to the working directory ...)

Tags:

java

jgit

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

like image 784
hurik Avatar asked Apr 04 '13 22:04

hurik


People also ask

How do you find all the commits made on a branch?

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.

Which git command show all commits in the current branchs history?

`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.

How do you find all commits to a file in git?

Use git log --all <filename> to view the commits influencing <filename> in all branches.

How do I see commits in master branch?

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.


3 Answers

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

like image 93
despadina Avatar answered Oct 13 '22 13:10

despadina


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());
            }
        }
    }
}
like image 34
hurik Avatar answered Oct 13 '22 13:10

hurik


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.

like image 24
Felipe Gomes Avatar answered Oct 13 '22 13:10

Felipe Gomes