iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
iannucci@chromium.org | a112f03 | 2014-03-13 07:47:50 +0000 | [diff] [blame] | 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 |
| 4 | # found in the LICENSE file. |
| 5 | |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 6 | """ |
| 7 | Provides an augmented `git log --graph` view. In particular, it also annotates |
| 8 | commits with branches + tags that point to them. Items are colorized as follows: |
| 9 | * Cyan - Currently checked out branch |
| 10 | * Green - Local branch |
| 11 | * Red - Remote branches |
| 12 | * Magenta - Tags |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 13 | * White - Merge Base Markers |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 14 | * Blue background - The currently checked out commit |
| 15 | """ |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 16 | |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 17 | import sys |
| 18 | |
| 19 | import subprocess2 |
| 20 | |
| 21 | from git_common import current_branch, branches, tags, config_list, GIT_EXE |
iannucci@chromium.org | a8378e2 | 2014-03-26 18:53:32 +0000 | [diff] [blame] | 22 | from git_common import branch_config_map, root |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 23 | |
| 24 | from third_party import colorama |
| 25 | |
| 26 | CYAN = colorama.Fore.CYAN |
| 27 | GREEN = colorama.Fore.GREEN |
| 28 | MAGENTA = colorama.Fore.MAGENTA |
| 29 | RED = colorama.Fore.RED |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 30 | WHITE = colorama.Fore.WHITE |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 31 | |
| 32 | BLUEBAK = colorama.Back.BLUE |
| 33 | |
| 34 | BRIGHT = colorama.Style.BRIGHT |
| 35 | RESET = colorama.Fore.RESET + colorama.Back.RESET + colorama.Style.RESET_ALL |
| 36 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 37 | # Git emits combined color |
| 38 | BRIGHT_RED = '\x1b[1;31m' |
| 39 | |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 40 | def main(): |
| 41 | map_extra = config_list('depot_tools.map_extra') |
| 42 | fmt = '%C(red bold)%h%x09%Creset%C(green)%d%Creset %C(yellow)%ad%Creset ~ %s' |
| 43 | log_proc = subprocess2.Popen( |
iannucci@chromium.org | a8378e2 | 2014-03-26 18:53:32 +0000 | [diff] [blame] | 44 | [GIT_EXE, 'log', '--graph', '--branches', '--tags', root(), |
| 45 | '--color=always', '--date=short', ('--pretty=format:' + fmt) |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 46 | ] + map_extra + sys.argv[1:], |
| 47 | stdout=subprocess2.PIPE, |
| 48 | shell=False) |
| 49 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 50 | merge_base_map = branch_config_map('base') |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 51 | current = current_branch() |
| 52 | all_branches = set(branches()) |
| 53 | if current in all_branches: |
| 54 | all_branches.remove(current) |
| 55 | all_tags = set(tags()) |
| 56 | try: |
| 57 | for line in log_proc.stdout.xreadlines(): |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 58 | if merge_base_map: |
| 59 | commit = line[line.find(BRIGHT_RED)+len(BRIGHT_RED):line.find('\t')] |
| 60 | base_for_branches = set() |
| 61 | for branch, sha in merge_base_map.iteritems(): |
| 62 | if sha.startswith(commit): |
| 63 | base_for_branches.add(branch) |
| 64 | if base_for_branches: |
| 65 | newline = '\r\n' if line.endswith('\r\n') else '\n' |
| 66 | line = line.rstrip(newline) |
| 67 | line += ''.join( |
| 68 | (BRIGHT, WHITE, ' <(%s)' % (', '.join(base_for_branches)), |
| 69 | newline)) |
| 70 | for b in base_for_branches: |
| 71 | del merge_base_map[b] |
| 72 | |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 73 | start = line.find(GREEN+' (') |
| 74 | end = line.find(')', start) |
| 75 | if start != -1 and end != -1: |
| 76 | start += len(GREEN) + 2 |
| 77 | branch_list = line[start:end].split(', ') |
| 78 | branches_str = '' |
| 79 | if branch_list: |
| 80 | colored_branches = [] |
| 81 | head_marker = '' |
| 82 | for b in branch_list: |
| 83 | if b == "HEAD": |
| 84 | head_marker = BLUEBAK+BRIGHT+'*' |
| 85 | continue |
| 86 | if b == current: |
| 87 | colored_branches.append(CYAN+BRIGHT+b+RESET) |
| 88 | current = None |
| 89 | elif b in all_branches: |
| 90 | colored_branches.append(GREEN+BRIGHT+b+RESET) |
| 91 | all_branches.remove(b) |
| 92 | elif b in all_tags: |
| 93 | colored_branches.append(MAGENTA+BRIGHT+b+RESET) |
| 94 | elif b.startswith('tag: '): |
| 95 | colored_branches.append(MAGENTA+BRIGHT+b[5:]+RESET) |
| 96 | else: |
| 97 | colored_branches.append(RED+b) |
| 98 | branches_str = '(%s) ' % ((GREEN+", ").join(colored_branches)+GREEN) |
| 99 | line = "%s%s%s" % (line[:start-1], branches_str, line[end+5:]) |
| 100 | if head_marker: |
| 101 | line = line.replace('*', head_marker, 1) |
| 102 | sys.stdout.write(line) |
| 103 | except (IOError, KeyboardInterrupt): |
| 104 | pass |
| 105 | finally: |
| 106 | sys.stderr.close() |
| 107 | sys.stdout.close() |
| 108 | return 0 |
| 109 | |
| 110 | |
| 111 | if __name__ == '__main__': |
| 112 | sys.exit(main()) |
| 113 | |