OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 """ |
| 3 Provides an augmented `git log --graph` view. In particular, it also annotates |
| 4 commits with branches + tags that point to them. Items are colorized as follows: |
| 5 * Cyan - Currently checked out branch |
| 6 * Green - Local branch |
| 7 * Red - Remote branches |
| 8 * Magenta - Tags |
| 9 * Blue background - The currently checked out commit |
| 10 """ |
| 11 import sys |
| 12 |
| 13 import subprocess2 |
| 14 |
| 15 from git_common import current_branch, branches, tags, config_list, GIT_EXE |
| 16 |
| 17 from third_party import colorama |
| 18 |
| 19 CYAN = colorama.Fore.CYAN |
| 20 GREEN = colorama.Fore.GREEN |
| 21 MAGENTA = colorama.Fore.MAGENTA |
| 22 RED = colorama.Fore.RED |
| 23 |
| 24 BLUEBAK = colorama.Back.BLUE |
| 25 |
| 26 BRIGHT = colorama.Style.BRIGHT |
| 27 RESET = colorama.Fore.RESET + colorama.Back.RESET + colorama.Style.RESET_ALL |
| 28 |
| 29 def main(): |
| 30 map_extra = config_list('depot_tools.map_extra') |
| 31 fmt = '%C(red bold)%h%x09%Creset%C(green)%d%Creset %C(yellow)%ad%Creset ~ %s' |
| 32 log_proc = subprocess2.Popen( |
| 33 [GIT_EXE, 'log', '--graph', '--full-history', '--branches', '--tags', |
| 34 '--remotes', '--color=always', '--date=short', ('--pretty=format:' + fmt) |
| 35 ] + map_extra + sys.argv[1:], |
| 36 stdout=subprocess2.PIPE, |
| 37 shell=False) |
| 38 |
| 39 current = current_branch() |
| 40 all_branches = set(branches()) |
| 41 if current in all_branches: |
| 42 all_branches.remove(current) |
| 43 all_tags = set(tags()) |
| 44 try: |
| 45 for line in log_proc.stdout.xreadlines(): |
| 46 start = line.find(GREEN+' (') |
| 47 end = line.find(')', start) |
| 48 if start != -1 and end != -1: |
| 49 start += len(GREEN) + 2 |
| 50 branch_list = line[start:end].split(', ') |
| 51 branches_str = '' |
| 52 if branch_list: |
| 53 colored_branches = [] |
| 54 head_marker = '' |
| 55 for b in branch_list: |
| 56 if b == "HEAD": |
| 57 head_marker = BLUEBAK+BRIGHT+'*' |
| 58 continue |
| 59 if b == current: |
| 60 colored_branches.append(CYAN+BRIGHT+b+RESET) |
| 61 current = None |
| 62 elif b in all_branches: |
| 63 colored_branches.append(GREEN+BRIGHT+b+RESET) |
| 64 all_branches.remove(b) |
| 65 elif b in all_tags: |
| 66 colored_branches.append(MAGENTA+BRIGHT+b+RESET) |
| 67 elif b.startswith('tag: '): |
| 68 colored_branches.append(MAGENTA+BRIGHT+b[5:]+RESET) |
| 69 else: |
| 70 colored_branches.append(RED+b) |
| 71 branches_str = '(%s) ' % ((GREEN+", ").join(colored_branches)+GREEN) |
| 72 line = "%s%s%s" % (line[:start-1], branches_str, line[end+5:]) |
| 73 if head_marker: |
| 74 line = line.replace('*', head_marker, 1) |
| 75 sys.stdout.write(line) |
| 76 except (IOError, KeyboardInterrupt): |
| 77 pass |
| 78 finally: |
| 79 sys.stderr.close() |
| 80 sys.stdout.close() |
| 81 return 0 |
| 82 |
| 83 |
| 84 if __name__ == '__main__': |
| 85 sys.exit(main()) |
| 86 |
OLD | NEW |