Created
January 11, 2012 02:46
-
-
Save lost-theory/1592662 to your computer and use it in GitHub Desktop.
git-branched
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
git-branched - show metadata about where a branch came from (and optionally, | |
what commits were on the branch) | |
Based on the method here: | |
http://stackoverflow.com/a/4991675/75956 | |
Some examples: | |
git branched hotfix/2.31.1 master | |
git branched --rev-list=1 hotfix/2.31.1 master | |
git branched --log=1 hotfix/2.31.1 master | |
git branched --log=oneline hotfix/2.31.1 master | |
written by me | |
https://github.com/lost-theory | |
""" | |
import subprocess, sys, argparse, os | |
def get_oldest_ancestor(b1, b2): | |
"""Find the oldest common element between rev-lists b1 and b2.""" | |
b2 = set(b2) | |
last = None | |
for c in reversed(b1): | |
if c not in b2: | |
return last | |
last = c | |
return None | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='show metadata about where a branch came from (and optionally, what commits were on the branch).') | |
parser.add_argument('--rev-list', action='store', default=False, help='Show the rev-list instead of where the branch was cut from.') | |
parser.add_argument('--log', action='store', default=False, help='Show the commit log instead of where the branch was cut from.') | |
parser.add_argument('branch1', metavar='branch1', nargs=1, help='The branch you are interested in.') | |
parser.add_argument('branch2', metavar='branch2', nargs=1, help='The branch it was cut from.') | |
args = parser.parse_args(sys.argv[1:]) | |
p1 = subprocess.Popen("git rev-list --first-parent %s" % args.branch1[0], stdout=subprocess.PIPE, shell=True) | |
p2 = subprocess.Popen("git rev-list --first-parent %s" % args.branch2[0], stdout=subprocess.PIPE, shell=True) | |
(o1,e1) = p1.communicate() | |
(o2,e2) = p2.communicate() | |
p1.wait() | |
p2.wait() | |
b1_ancestors = o1.strip().split('\n') | |
b2_ancestors = o2.strip().split('\n') | |
oldest_ancestor = get_oldest_ancestor(b1_ancestors, b2_ancestors) | |
if not oldest_ancestor: | |
print "error: '%s' doesn't seem to be branched from '%s'" % (args.branch1[0], args.branch2[0]) | |
sys.exit(1) | |
if args.rev_list: | |
b2_ancestors = set(b2_ancestors) | |
for c in b1_ancestors: | |
if c not in b2_ancestors: | |
print c | |
elif args.log: | |
pretty = '--format="%h | %ad | %an | %s"' | |
if args.log in "oneline,short,medium,full,fuller,email,raw".split(","): | |
pretty = "--pretty=%s" % args.log | |
os.system("git log %s %s..%s" % (pretty, oldest_ancestor, b1_ancestors[0])) | |
else: | |
print "'%s' was branched from '%s' at:\t%s" % (args.branch1[0], args.branch2[0], oldest_ancestor) | |
os.system("git log -1 %s" % oldest_ancestor) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment