Chromium Code Reviews| Index: git_short_map.py |
| diff --git a/git_short_map.py b/git_short_map.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..66ebac5c6f195fe29a779ba0fb4150cb75c7b4f4 |
| --- /dev/null |
| +++ b/git_short_map.py |
| @@ -0,0 +1,58 @@ |
| +#!/usr/bin/env python |
|
agable
2014/02/28 19:54:58
I'd call this 'git-map-branches', not 'git-short-m
iannucci
2014/03/06 00:18:39
Done
|
| +import collections |
| +import sys |
| + |
| +from third_party import colorama |
| +from third_party.colorama import Fore, Style |
| + |
| +from git_common import current_branch, branches, upstream, hash_one, hash_multi |
| + |
| + |
| +def print_branch(cur, cur_hash, branch, branch_hashes, par_map, branch_map, |
| + depth=0): |
| + branch_hash = branch_hashes[branch] |
| + if branch.startswith('origin'): |
| + color = Fore.RED |
| + elif branch_hash == cur_hash: |
| + color = Fore.CYAN |
| + else: |
| + color = Fore.GREEN |
| + |
| + if branch_hash == cur_hash: |
| + color += Style.BRIGHT |
| + else: |
| + color += Style.NORMAL |
| + |
| + print color + " "*depth + branch + (" *" if branch == cur else "") |
| + for child in par_map.pop(branch, ()): |
| + print_branch(cur, cur_hash, child, branch_hashes, par_map, branch_map, |
| + depth=depth+1) |
| + |
| + |
| +def main(argv): |
| + colorama.init() |
| + assert len(argv) == 1, "No arguments expected" |
| + branch_map = {} |
| + par_map = collections.defaultdict(list) |
| + for branch in branches(): |
| + par = upstream(branch) |
| + branch_map[branch] = par |
| + par_map[par].append(branch) |
| + |
| + current = current_branch() |
| + hashes = hash_multi(current, *branch_map.keys()) |
| + current_hash = hashes[0] |
| + par_hashes = {k: hashes[i+1] for i, k in enumerate(branch_map.iterkeys())} |
| + while par_map: |
| + for parent in par_map: |
| + if parent not in branch_map: |
| + if parent not in par_hashes: |
| + par_hashes[parent] = hash_one(parent) |
| + print_branch(current, current_hash, parent, par_hashes, par_map, |
| + branch_map) |
| + break |
| + |
| + |
| +if __name__ == '__main__': |
| + sys.exit(main(sys.argv)) |
| + |