Pergunta

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
Foi útil?

Solução 2

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 for git merge, and it's a constant that the first parent is the current commit when creating a new one (either through merge or normal git 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).

Outras dicas

Yes, if you merge A into B, the first parent is B and the second is A.

Therefore you can do things like gitk --first-parent to show the history of only the current branch without details of the merged-in branches.

Any parent after the first parent represents a merge from another branch with at least one exception.

If you merge a branch into itself, which usually happens when it is worked on in two places separately then later integrated, one of those "branches" in a branch, will appear as if it comes from nowhere.

This will show up if you traverse all parents of all leaf branches but it such branches within branches wont show up if you traverse the linear history of all leaf branches. This is at least still correct in the sense that you'll only be traversing the linear history of commits that were merged into. What will be hidden is the fact that there's a branch within a branch so it wont be showing all the history exclusive to that branch.

I do not know if deleting branches has the same effect.

The first parent not always what you expected as the previous current branch. Please see the link

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top