1

I have a start commit from which i want to find all the branches till i reach another dont find a note for the commit.

commit 1
|
commit 2
|         commit5
commit3  /
|       / 
commit 4
|
commit 6

in this case say all the commit from commit 1-5 have notes "find branch" and commit 6 doesn't have a not with that value.

So i will start with commit 1 find all the parent (ie: commit 2) and try to check if there is an branch for this commit (ie: number of children is more than 1). if there is more than 1 childs

  1. the getChildren() method is there only for PlotCommit object, but the method parentCommit.getParents() return only RevCommit Object.
  2. i want to find the branch name present in the particular commit

then when there is no more notes on the commit (ie commit 6 doesnt have notes) the logic will stop there and the collection of Branch name is returned

    Repository repo;//will be set as part of some other logic
    private Set findBranchesForCommit(PlotCommit parentCommit, String note) throws ExecutionException, MissingObjectException, IncorrectObjectTypeException, IOException {
        Set branches = new HashSet();
        PlotCommit[] parents = (PlotCommit[]) parentCommit.getParents();//XXX will throw exception as this return RevCommit[]
        for (int i = 0; i < parents .length; i++) {
            PlotCommit commit = parents[i];
            String result = extractExistingMessage(repo, "refs/notes", commit);//will return the if the note available for particular commit
            if (result.trim().length() > 0 && result.equalsIgnoreCase(note)) {
                System.out.println("#########"+commit.getChildCount());
                //TODO need to add logic to find the branch of the particular commit
                branches.add(""); //add the branches available for the commit
                branches.addAll(findBranchesForCommit(commit, note));
            }
        }
        return branches;
    }

expected result

i want to find the branch names of commits that contain a particular git note. in the above example the branch name of Commit 1 and commit 5 will be returned

Naveen Babu
  • 1,584
  • 1
  • 14
  • 35
  • 1
    I have problems understanding your question. Are you just looking for `git branch --contains commit6`? – michas Dec 28 '12 at 07:42
  • In short. i want to find the branch names of commits that contain a particular git note. in the above example the branch name of Commit 1 and commit 5 will be returned – Naveen Babu Dec 28 '12 at 07:56

1 Answers1

2

While the git command for this kind of request (finding branches for a given commit) is:

git branch --contains <commit>

(as in "Git: Finding what branch a commit came from", and "How to list branches that contain a given commit?")

it isn't implemented as is in JGit.

This thread has this to propose:

'git branch --contains <commit>' will report every branch which contains this commit.
In the standard push/fetch workflows for all commits you get from remote only "origin/master" is reported.
And even for local commits: Imagine you have merged your feature branch back to master. Then this command will report also the master branch and all feature branches created after merging.
Git simply doesn't store for a revision on which branch it was created.

After these warnings: a rough implementation of "git branch --contains " could look like this:

Repository repo = new FileRepository(args[0]);
RevWalk walk = new RevWalk(repo);
RevCommit commit = walk.parseCommit(repo.resolve(args[1] + "^0"));
for (Map.Entry<String, Ref> e : repo.getAllRefs().entrySet())
  if (e.getKey().startsWith(Constants.R_HEADS))
    if (walk.isMergedInto(commit,
        walk.parseCommit(e.getValue().getObjectId())))
      System.out.println("Ref " + e.getValue().getName()
                                + " contains commit " + commit);
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250