Is there some convention for the order of a commit's parents?
Because one of the commit's parents should be to be to previous commit on the current branch that is being merged into and the rest are previous commits of the other merging branches.
I would like to identify the previous commit of the current branch, I'm using pygit
which returns a list of parents for a commit and intuitively I thought maybe the order of parents has significance but I have found no explicit mention of this.
I wrote this utility function, using first parent commit to traverse branch :
def walk_branch(pygit_repository, branch_oid):
"""
Walk a single branch
"""
from pygit2 import GIT_SORT_TOPOLOGICAL
previous_first_parent_oid = None
for commit in pygit_repository.walk(branch_oid, GIT_SORT_TOPOLOGICAL):
if previous_first_parent_oid is None or commit.oid == previous_first_parent_oid:
previous_first_parent_oid = commit.parents[0].oid if len(commit.parents) else None
yield commit
libgit2
and its bindings return the parents in the order that they're stored in the commit, which is the order of the commits as they were given on e.g. the command-line forgit merge
, and it's a constant that the first parent is the current commit when creating a new one (either through merge or normalgit commit
).In order to identify the commit previous commit (in time), then all you need to do is look at the first parent. Whether they're in the same branch or not is not something you can see from that, as a commit parent might be in a different branch (which is what causes branches), but you can draw a direct line (like those
git log --graph --oneline
draws).