Question

I would like to do the following command from Python script using dulwich:

$ git branch --contains <myCommitSha> | wc -l

What I intend is to check if a particular commit (sha) is placed in more than one branches.

Of course I thought that I can execute the above command from Python and parse the output (parse the number of branches), but that's the last resort solution.

Any other ideas/comments? Thanks in advance.

Was it helpful?

Solution 3

There is no built-in function for this, but you can of course implement this yourself.

You can also just do something like this (untested):

branches = [ref for ref in repo.refs.keys("refs/heads/") if
            any((True for commit in repo.get_walker(include=[repo.refs[ref]])
                 if commit.id == YOURSHA))]

This will give you a list of all the branch heads that contain the given commit, but will have a runtime of O(n*m), n beeing the amount of commits in your repo, m beeing the amount of branches. The git implementation probably has a runtime of O(n).

OTHER TIPS

Just in case someone was wondering how to do this now using gitpython:

repo.git.branch('--contains', YOURSHA)

Since branches are just pointers to random commits and they don't "describe" trees in any way, there is nothing linking some random commit TO a branch.

The only sensible way I would take to look up if a given commit is an ancestor of a commit to which some branch points is to traverse all ancestor chains from branch-top commit down.

In other words, in dulwich I would iterate over branches and traverse backwards to see if a sha is on the chain.

I am rather certain that's exactly what git branch --contains <myCommitSha> does as I am not aware of any other shortcut.

Since your choice is (a) make python do the iteration or (b) make C do same iteration, I'd just go with C. :)

In case anyone uses GitPython and wants all branches

import git
gLocal = git.Git("<LocalRepoLocation>")
gLocal.branch('-a','--contains', '<CommitSHA>').split('\n')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top