Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/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
| |
| 2 import collections | |
| 3 import sys | |
| 4 | |
| 5 from third_party import colorama | |
| 6 from third_party.colorama import Fore, Style | |
| 7 | |
| 8 from git_common import current_branch, branches, upstream, hash_one, hash_multi | |
| 9 | |
| 10 | |
| 11 def print_branch(cur, cur_hash, branch, branch_hashes, par_map, branch_map, | |
| 12 depth=0): | |
| 13 branch_hash = branch_hashes[branch] | |
| 14 if branch.startswith('origin'): | |
| 15 color = Fore.RED | |
| 16 elif branch_hash == cur_hash: | |
| 17 color = Fore.CYAN | |
| 18 else: | |
| 19 color = Fore.GREEN | |
| 20 | |
| 21 if branch_hash == cur_hash: | |
| 22 color += Style.BRIGHT | |
| 23 else: | |
| 24 color += Style.NORMAL | |
| 25 | |
| 26 print color + " "*depth + branch + (" *" if branch == cur else "") | |
| 27 for child in par_map.pop(branch, ()): | |
| 28 print_branch(cur, cur_hash, child, branch_hashes, par_map, branch_map, | |
| 29 depth=depth+1) | |
| 30 | |
| 31 | |
| 32 def main(argv): | |
| 33 colorama.init() | |
| 34 assert len(argv) == 1, "No arguments expected" | |
| 35 branch_map = {} | |
| 36 par_map = collections.defaultdict(list) | |
| 37 for branch in branches(): | |
| 38 par = upstream(branch) | |
| 39 branch_map[branch] = par | |
| 40 par_map[par].append(branch) | |
| 41 | |
| 42 current = current_branch() | |
| 43 hashes = hash_multi(current, *branch_map.keys()) | |
| 44 current_hash = hashes[0] | |
| 45 par_hashes = {k: hashes[i+1] for i, k in enumerate(branch_map.iterkeys())} | |
| 46 while par_map: | |
| 47 for parent in par_map: | |
| 48 if parent not in branch_map: | |
| 49 if parent not in par_hashes: | |
| 50 par_hashes[parent] = hash_one(parent) | |
| 51 print_branch(current, current_hash, parent, par_hashes, par_map, | |
| 52 branch_map) | |
| 53 break | |
| 54 | |
| 55 | |
| 56 if __name__ == '__main__': | |
| 57 sys.exit(main(sys.argv)) | |
| 58 | |
| OLD | NEW |