blob: 5f2e9b4156bc8b15f8e7c9f5a0c73b96edbc9c48 [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
21from git_common import current_branch, branches, tags, config_list, GIT_EXE
iannucci@chromium.orga8378e22014-03-26 18:53:32 +000022from git_common import branch_config_map, 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
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000040def 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.orga8378e22014-03-26 18:53:32 +000044 [GIT_EXE, 'log', '--graph', '--branches', '--tags', root(),
45 '--color=always', '--date=short', ('--pretty=format:' + fmt)
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000046 ] + map_extra + sys.argv[1:],
47 stdout=subprocess2.PIPE,
48 shell=False)
49
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000050 merge_base_map = branch_config_map('base')
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000051 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.orgc050a5b2014-03-26 06:18:50 +000058 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.org8bc9b5c2014-03-12 01:36:18 +000073 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
111if __name__ == '__main__':
112 sys.exit(main())
113