Home > Programming > Find git commits which still need to be merged

Find git commits which still need to be merged

If you have a git branch which you are working on which is a clone from an upstream ‘official’ branch, and you sometimes submit your commits to be integrated upstream, there’ll surely be times when your patches are not accepted or fall between the cracks. After a while it can get a little hairy to figure out which commits are unique to your branch and which ones are incorporated in the main repo.

The ‘git cherry’ command can help with this. It lists all commits in one branch which are not in another. It works like this simply:

git cherry -v upstream my_branch

The man page describes the command succinctly:

Every commit that doesn’t exist in the <upstream> branch has its id (sha1) reported, prefixed by a symbol. The ones that have equivalent change already in the <upstream> branch are prefixed with a minus (-) sign, and those that only exist in the <head> branch are prefixed with a plus (+) symbol.
The problem is that sometimes the cherry command does not identify ‘equivalent changes’. I’m not sure why, but perhaps this happens when the main tree has cherry picked commits and made changes to whitespace or resolved merge conflicts. Or maybe it doesn’t work well at all with cherry pick. Either way, I had a list of changes which cherry returned which I knew had in fact been merged. So I whipped up up this silly little Python script to filter a cherry results list from a complete log of the upstream repo and to remove all commits with the same commit message. This is obviously not bullet proof as different commits may have the same message. But it was good enough for me in this case. So without further ado, here it is:
import sys

cherry = open(sys.argv[1], "r")
filtr = open(sys.argv[2], "r")
lines = filtr.readlines()
unmatched = []

for line in cherry.readlines():
  plus, sha, message = line.split(" ", 2)
  for existing in lines:
    esha,emessage = existing.split(" ", 1)
    if message == emessage:
      break
  else:
    unmatched.append((sha, message))

for sha, message in unmatched:
  print "+ %s %s" % (sha, message)

Usage:

$ git checkout upstream
$ git log --oneline >all_commits.txt
$ git cherry -v upstream my_branch >cherry_commits.txt
$ python filter_cherry.py cherry_commits.txt all_commits.txt

Be Sociable, Share!
Categories: Programming Tags: ,
  1. No comments yet.
  1. No trackbacks yet.