pythongitpython-3.xpython-2.7dulwich

Check which branches contain a specific git commit sha using Python dulwich?


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.


Solution

  • 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).