blob: 70013251607fd08193256c887e5190c38477fc3d [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
13 * Blue background - The currently checked out commit
14"""
15import sys
16
17import subprocess2
18
19from git_common import current_branch, branches, tags, config_list, GIT_EXE
20
21from third_party import colorama
22
23CYAN = colorama.Fore.CYAN
24GREEN = colorama.Fore.GREEN
25MAGENTA = colorama.Fore.MAGENTA
26RED = colorama.Fore.RED
27
28BLUEBAK = colorama.Back.BLUE
29
30BRIGHT = colorama.Style.BRIGHT
31RESET = colorama.Fore.RESET + colorama.Back.RESET + colorama.Style.RESET_ALL
32
33def main():
34 map_extra = config_list('depot_tools.map_extra')
35 fmt = '%C(red bold)%h%x09%Creset%C(green)%d%Creset %C(yellow)%ad%Creset ~ %s'
36 log_proc = subprocess2.Popen(
37 [GIT_EXE, 'log', '--graph', '--full-history', '--branches', '--tags',
38 '--remotes', '--color=always', '--date=short', ('--pretty=format:' + fmt)
39 ] + map_extra + sys.argv[1:],
40 stdout=subprocess2.PIPE,
41 shell=False)
42
43 current = current_branch()
44 all_branches = set(branches())
45 if current in all_branches:
46 all_branches.remove(current)
47 all_tags = set(tags())
48 try:
49 for line in log_proc.stdout.xreadlines():
50 start = line.find(GREEN+' (')
51 end = line.find(')', start)
52 if start != -1 and end != -1:
53 start += len(GREEN) + 2
54 branch_list = line[start:end].split(', ')
55 branches_str = ''
56 if branch_list:
57 colored_branches = []
58 head_marker = ''
59 for b in branch_list:
60 if b == "HEAD":
61 head_marker = BLUEBAK+BRIGHT+'*'
62 continue
63 if b == current:
64 colored_branches.append(CYAN+BRIGHT+b+RESET)
65 current = None
66 elif b in all_branches:
67 colored_branches.append(GREEN+BRIGHT+b+RESET)
68 all_branches.remove(b)
69 elif b in all_tags:
70 colored_branches.append(MAGENTA+BRIGHT+b+RESET)
71 elif b.startswith('tag: '):
72 colored_branches.append(MAGENTA+BRIGHT+b[5:]+RESET)
73 else:
74 colored_branches.append(RED+b)
75 branches_str = '(%s) ' % ((GREEN+", ").join(colored_branches)+GREEN)
76 line = "%s%s%s" % (line[:start-1], branches_str, line[end+5:])
77 if head_marker:
78 line = line.replace('*', head_marker, 1)
79 sys.stdout.write(line)
80 except (IOError, KeyboardInterrupt):
81 pass
82 finally:
83 sys.stderr.close()
84 sys.stdout.close()
85 return 0
86
87
88if __name__ == '__main__':
89 sys.exit(main())
90