OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 """ |
| 3 Provides a short mapping of all the branches in your local repo, organized by |
| 4 their upstream ('tracking branch') layout. Example: |
| 5 |
| 6 origin/master |
| 7 cool_feature |
| 8 dependent_feature |
| 9 other_dependent_feature |
| 10 other_feature |
| 11 |
| 12 Branches are colorized as follows: |
| 13 * Red - a remote branch (usually the root of all local branches) |
| 14 * Cyan - a local branch which is the same as HEAD |
| 15 * Note that multiple branches may be Cyan, if they are all on the same |
| 16 commit, and you have that commit checked out. |
| 17 * Green - a local branch |
| 18 * Magenta - a placeholder for the '{NO UPSTREAM}' "branch". If you have |
| 19 local branches which do not track any upstream, then you will see this. |
| 20 """ |
| 21 import collections |
| 22 import sys |
| 23 |
| 24 from third_party import colorama |
| 25 from third_party.colorama import Fore, Style |
| 26 |
| 27 from git_common import current_branch, branches, upstream, hash_one, hash_multi |
| 28 |
| 29 NO_UPSTREAM = '{NO UPSTREAM}' |
| 30 |
| 31 def print_branch(cur, cur_hash, branch, branch_hashes, par_map, branch_map, |
| 32 depth=0): |
| 33 branch_hash = branch_hashes[branch] |
| 34 if branch.startswith('origin'): |
| 35 color = Fore.RED |
| 36 elif branch == NO_UPSTREAM: |
| 37 color = Fore.MAGENTA |
| 38 elif branch_hash == cur_hash: |
| 39 color = Fore.CYAN |
| 40 else: |
| 41 color = Fore.GREEN |
| 42 |
| 43 if branch_hash == cur_hash: |
| 44 color += Style.BRIGHT |
| 45 else: |
| 46 color += Style.NORMAL |
| 47 |
| 48 print color + " "*depth + branch + (" *" if branch == cur else "") |
| 49 for child in par_map.pop(branch, ()): |
| 50 print_branch(cur, cur_hash, child, branch_hashes, par_map, branch_map, |
| 51 depth=depth+1) |
| 52 |
| 53 |
| 54 def main(argv): |
| 55 colorama.init() |
| 56 assert len(argv) == 1, "No arguments expected" |
| 57 branch_map = {} |
| 58 par_map = collections.defaultdict(list) |
| 59 for branch in branches(): |
| 60 par = upstream(branch) or NO_UPSTREAM |
| 61 branch_map[branch] = par |
| 62 par_map[par].append(branch) |
| 63 |
| 64 current = current_branch() |
| 65 hashes = hash_multi(current, *branch_map.keys()) |
| 66 current_hash = hashes[0] |
| 67 par_hashes = {k: hashes[i+1] for i, k in enumerate(branch_map.iterkeys())} |
| 68 par_hashes[NO_UPSTREAM] = 0 |
| 69 while par_map: |
| 70 for parent in par_map: |
| 71 if parent not in branch_map: |
| 72 if parent not in par_hashes: |
| 73 par_hashes[parent] = hash_one(parent) |
| 74 print_branch(current, current_hash, parent, par_hashes, par_map, |
| 75 branch_map) |
| 76 break |
| 77 |
| 78 |
| 79 if __name__ == '__main__': |
| 80 sys.exit(main(sys.argv)) |
| 81 |
OLD | NEW |