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