Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(232)

Side by Side Diff: git_map.py

Issue 2157763002: git map: Add -h, --help option (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools@master
Patch Set: Do not use less for --help output Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « git-map ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright 2014 The Chromium Authors. All rights reserved. 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 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """ 6 """
7 Provides an augmented `git log --graph` view. In particular, it also annotates 7 Enhances `git log --graph` view with information on commit branches + tags that
8 commits with branches + tags that point to them. Items are colorized as follows: 8 point to them. Items are colorized as follows:
9 * Cyan - Currently checked out branch 9 * Cyan - Currently checked out branch
10 * Green - Local branch 10 * Green - Local branch
11 * Red - Remote branches 11 * Red - Remote branches
12 * Magenta - Tags 12 * Magenta - Tags
13 * White - Merge Base Markers 13 * White - Merge Base Markers
14 * Blue background - The currently checked out commit 14 * Blue background - The currently checked out commit
15 """ 15 """
16 16
17 import sys 17 import sys
18 18
19 import subprocess2 19 import subprocess2
20 20
21 from git_common import current_branch, branches, tags, get_config_list, GIT_EXE 21 from git_common import current_branch, branches, tags, get_config_list, GIT_EXE
22 from git_common import get_or_create_merge_base, root 22 from git_common import get_or_create_merge_base, root, ROOT, IS_WIN
23 23
24 from third_party import colorama 24 from third_party import colorama
25 25
26 CYAN = colorama.Fore.CYAN 26 CYAN = colorama.Fore.CYAN
27 GREEN = colorama.Fore.GREEN 27 GREEN = colorama.Fore.GREEN
28 MAGENTA = colorama.Fore.MAGENTA 28 MAGENTA = colorama.Fore.MAGENTA
29 RED = colorama.Fore.RED 29 RED = colorama.Fore.RED
30 WHITE = colorama.Fore.WHITE 30 WHITE = colorama.Fore.WHITE
31 31
32 BLUEBAK = colorama.Back.BLUE 32 BLUEBAK = colorama.Back.BLUE
33 33
34 BRIGHT = colorama.Style.BRIGHT 34 BRIGHT = colorama.Style.BRIGHT
35 RESET = colorama.Fore.RESET + colorama.Back.RESET + colorama.Style.RESET_ALL 35 RESET = colorama.Fore.RESET + colorama.Back.RESET + colorama.Style.RESET_ALL
36 36
37 # Git emits combined color 37 # Git emits combined color
38 BRIGHT_RED = '\x1b[1;31m' 38 BRIGHT_RED = '\x1b[1;31m'
39 39
40 def main(argv): 40 def main(argv):
41 if '-h' in argv or '--help' in argv:
42 sys.stdout.write(__doc__)
43 return 0
44
41 map_extra = get_config_list('depot_tools.map_extra') 45 map_extra = get_config_list('depot_tools.map_extra')
42 fmt = '%C(red bold)%h%x09%Creset%C(green)%d%Creset %C(yellow)%ad%Creset ~ %s' 46 fmt = '%C(red bold)%h%x09%Creset%C(green)%d%Creset %C(yellow)%ad%Creset ~ %s'
43 log_proc = subprocess2.Popen( 47 log_proc = subprocess2.Popen(
44 [GIT_EXE, 'log', '--graph', '--branches', '--tags', root(), 48 [GIT_EXE, 'log', '--graph', '--branches', '--tags', root(),
45 '--color=always', '--date=short', ('--pretty=format:' + fmt) 49 '--color=always', '--date=short', ('--pretty=format:' + fmt)
46 ] + map_extra + argv, 50 ] + map_extra + argv,
47 stdout=subprocess2.PIPE, 51 stdout=subprocess2.PIPE,
48 shell=False) 52 shell=False)
49 53
54 # prepare pager
55 less = 'less'
56 if IS_WIN:
57 with open(ROOT + '\\git.bat') as r:
58 for line in r:
59 start = line.find('%~dp0')
60 if start != -1:
61 GIT_DIR = line[start+5:line.find('\cmd')]
62 less = ROOT + '\\' + GIT_DIR + '\\usr\\bin\\less'
63 break
64 less_proc = subprocess2.Popen([less, '-R'], stdin=subprocess2.PIPE)
iannucci 2016/07/19 01:07:55 please make this a separate CL. That said, this a
65
50 current = current_branch() 66 current = current_branch()
51 all_branches = set(branches()) 67 all_branches = set(branches())
52 merge_base_map = {b: get_or_create_merge_base(b) for b in all_branches} 68 merge_base_map = {b: get_or_create_merge_base(b) for b in all_branches}
53 merge_base_map = {b: v for b, v in merge_base_map.iteritems() if v} 69 merge_base_map = {b: v for b, v in merge_base_map.iteritems() if v}
54 if current in all_branches: 70 if current in all_branches:
55 all_branches.remove(current) 71 all_branches.remove(current)
56 all_tags = set(tags()) 72 all_tags = set(tags())
57 try: 73 try:
58 for line in log_proc.stdout.xreadlines(): 74 for line in log_proc.stdout.xreadlines():
59 if merge_base_map: 75 if merge_base_map:
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 elif b in all_tags: 109 elif b in all_tags:
94 colored_branches.append(MAGENTA+BRIGHT+b+RESET) 110 colored_branches.append(MAGENTA+BRIGHT+b+RESET)
95 elif b.startswith('tag: '): 111 elif b.startswith('tag: '):
96 colored_branches.append(MAGENTA+BRIGHT+b[5:]+RESET) 112 colored_branches.append(MAGENTA+BRIGHT+b[5:]+RESET)
97 else: 113 else:
98 colored_branches.append(RED+b) 114 colored_branches.append(RED+b)
99 branches_str = '(%s) ' % ((GREEN+", ").join(colored_branches)+GREEN) 115 branches_str = '(%s) ' % ((GREEN+", ").join(colored_branches)+GREEN)
100 line = "%s%s%s" % (line[:start-1], branches_str, line[end+5:]) 116 line = "%s%s%s" % (line[:start-1], branches_str, line[end+5:])
101 if head_marker: 117 if head_marker:
102 line = line.replace('*', head_marker, 1) 118 line = line.replace('*', head_marker, 1)
103 sys.stdout.write(line) 119 less_proc.stdin.write(line)
104 except (IOError, KeyboardInterrupt): 120 except (IOError, KeyboardInterrupt):
105 pass 121 pass
106 finally: 122 finally:
123 # wait for less to exit
124 less_proc.stdin.close()
125 less_proc.wait()
107 sys.stderr.close() 126 sys.stderr.close()
108 sys.stdout.close() 127 sys.stdout.close()
109 return 0 128 return 0
110 129
111 130
112 if __name__ == '__main__': 131 if __name__ == '__main__':
113 try: 132 try:
114 sys.exit(main(sys.argv[1:])) 133 sys.exit(main(sys.argv[1:]))
115 except KeyboardInterrupt: 134 except KeyboardInterrupt:
116 sys.stderr.write('interrupted\n') 135 sys.stderr.write('interrupted\n')
117 sys.exit(1) 136 sys.exit(1)
OLDNEW
« no previous file with comments | « git-map ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698