How to find all unmerged commits in master grouped by the branches they were created in?

后端 未结 4 1104
一个人的身影
一个人的身影 2020-12-15 20:09

I have to create some code review from unmerged branches.

In finding solutions, let\'s not go to local-branch context problem as this will run on a server; there wil

4条回答
  •  隐瞒了意图╮
    2020-12-15 20:54

    I would suggest doing it kind of the way you described it. But I would work on the output of git log --format="%H:%P:%s" ^origin/master origin/branch1 origin/branch2, so you can do better tree-walking.

    1. Build a proper tree structure from the output, marking parents and children.
    2. Start walking from the heads (get their SHAs from git rev-parse). Mark every commit with the names of the head you came from and its distance.
      • For not-first-parent steps (the other part of the merge), I would add 100 to the distance.
      • If you meet a merge commit, check what it says about which branch was merged into which. Use this information when following the two parent links: If the parsed name of the branch you are going to does not match your current HEAD, add 10000 to the distance.
      • For both of the parents: you now know their name. Add all their children that they are first-parent to to a dict: commit -> known-name.
    3. Take your dict of known-named commits and start walking up the tree (towards the children, not the parents). Substract 10000 from the distance from the merged-into branch. While doing this walk to not go to commits that you are not first-parent to and stop as soon as you hit a branch-point (a commit that has two children). Also stop if you hit one of your branch-heads.

    Now for each of your commits, you will have a list of distance values (that might be negative) to your branch heads. For each commit, the branch with the least distance is the one the commit was most likely created on.

    If you have time, you might want to walk the whole history and then substract the history of master – that might give slightly better results if your branches have been merged into master before.


    Couldn’t resist: Made a python script that does what I described. But with one change: with every normal step, the distance is not increased, but decreased. This has the effect that branches that lived longer after a merge-point are preferred, which I personally like more. Here it is: https://gist.github.com/Chronial/5275577

    Usage: simply run git-annotate-log.py ^origin/master origin/branch1 origin/branch2 check the quality of the results (will output a git log tree with annotations).

提交回复
热议问题