blob: 22e02e74b6f411e223eeb54f5608e9af24fed3ec [file] [log] [blame]
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +00001#!/usr/bin/env python
iannucci@chromium.orga112f032014-03-13 07:47:50 +00002# 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.org8bc9b5c2014-03-12 01:36:18 +00006"""
7Provides an augmented `git log --graph` view. In particular, it also annotates
8commits 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.orgc050a5b2014-03-26 06:18:50 +000013 * White - Merge Base Markers
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000014 * Blue background - The currently checked out commit
15"""
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000016
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000017import sys
18
19import subprocess2
20
agable7aa2ddd2016-06-21 07:47:00 -070021from git_common import current_branch, branches, tags, get_config_list, GIT_EXE
iannucci@chromium.org6332b152014-05-15 03:16:10 +000022from git_common import get_or_create_merge_base, root
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000023
24from third_party import colorama
25
26CYAN = colorama.Fore.CYAN
27GREEN = colorama.Fore.GREEN
28MAGENTA = colorama.Fore.MAGENTA
29RED = colorama.Fore.RED
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000030WHITE = colorama.Fore.WHITE
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000031
32BLUEBAK = colorama.Back.BLUE
33
34BRIGHT = colorama.Style.BRIGHT
35RESET = colorama.Fore.RESET + colorama.Back.RESET + colorama.Style.RESET_ALL
36
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000037# Git emits combined color
38BRIGHT_RED = '\x1b[1;31m'
39
sbc@chromium.org013731e2015-02-26 18:28:43 +000040def main(argv):
agable7aa2ddd2016-06-21 07:47:00 -070041 map_extra = get_config_list('depot_tools.map_extra')
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000042 fmt = '%C(red bold)%h%x09%Creset%C(green)%d%Creset %C(yellow)%ad%Creset ~ %s'
43 log_proc = subprocess2.Popen(
iannucci@chromium.orga8378e22014-03-26 18:53:32 +000044 [GIT_EXE, 'log', '--graph', '--branches', '--tags', root(),
45 '--color=always', '--date=short', ('--pretty=format:' + fmt)
sbc@chromium.org013731e2015-02-26 18:28:43 +000046 ] + map_extra + argv,
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000047 stdout=subprocess2.PIPE,
48 shell=False)
49
50 current = current_branch()
51 all_branches = set(branches())
iannucci@chromium.org6332b152014-05-15 03:16:10 +000052 merge_base_map = {b: get_or_create_merge_base(b) for b in all_branches}
iannucci@chromium.org10fbe872014-05-16 22:31:13 +000053 merge_base_map = {b: v for b, v in merge_base_map.iteritems() if v}
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000054 if current in all_branches:
55 all_branches.remove(current)
56 all_tags = set(tags())
57 try:
58 for line in log_proc.stdout.xreadlines():
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000059 if merge_base_map:
60 commit = line[line.find(BRIGHT_RED)+len(BRIGHT_RED):line.find('\t')]
61 base_for_branches = set()
62 for branch, sha in merge_base_map.iteritems():
63 if sha.startswith(commit):
64 base_for_branches.add(branch)
65 if base_for_branches:
66 newline = '\r\n' if line.endswith('\r\n') else '\n'
67 line = line.rstrip(newline)
68 line += ''.join(
69 (BRIGHT, WHITE, ' <(%s)' % (', '.join(base_for_branches)),
iannucci@chromium.orgcfa516f2014-04-04 17:13:33 +000070 RESET, newline))
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000071 for b in base_for_branches:
72 del merge_base_map[b]
73
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000074 start = line.find(GREEN+' (')
75 end = line.find(')', start)
76 if start != -1 and end != -1:
77 start += len(GREEN) + 2
78 branch_list = line[start:end].split(', ')
79 branches_str = ''
80 if branch_list:
81 colored_branches = []
82 head_marker = ''
83 for b in branch_list:
84 if b == "HEAD":
85 head_marker = BLUEBAK+BRIGHT+'*'
86 continue
87 if b == current:
88 colored_branches.append(CYAN+BRIGHT+b+RESET)
89 current = None
90 elif b in all_branches:
91 colored_branches.append(GREEN+BRIGHT+b+RESET)
92 all_branches.remove(b)
93 elif b in all_tags:
94 colored_branches.append(MAGENTA+BRIGHT+b+RESET)
95 elif b.startswith('tag: '):
96 colored_branches.append(MAGENTA+BRIGHT+b[5:]+RESET)
97 else:
98 colored_branches.append(RED+b)
99 branches_str = '(%s) ' % ((GREEN+", ").join(colored_branches)+GREEN)
100 line = "%s%s%s" % (line[:start-1], branches_str, line[end+5:])
101 if head_marker:
102 line = line.replace('*', head_marker, 1)
103 sys.stdout.write(line)
104 except (IOError, KeyboardInterrupt):
105 pass
106 finally:
107 sys.stderr.close()
108 sys.stdout.close()
109 return 0
110
111
112if __name__ == '__main__':
sbc@chromium.org013731e2015-02-26 18:28:43 +0000113 try:
114 sys.exit(main(sys.argv[1:]))
115 except KeyboardInterrupt:
116 sys.stderr.write('interrupted\n')
117 sys.exit(1)