blob: 10b321bfbb62ac7a22f0d9d1536e30285ae9dac3 [file] [log] [blame]
Josip Sokcevic4de5dea2022-03-23 21:15:14 +00001#!/usr/bin/env python3
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"""
Edward Lemurb800fde2020-01-10 23:04:44 +00007usage: git map [-h] [--help] [<args>]
8
anatoly techtonikc878ba62017-04-03 06:08:38 +03009Enhances `git log --graph` view with information on commit branches + tags that
10point to them. Items are colorized as follows:
11
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000012 * Cyan - Currently checked out branch
13 * Green - Local branch
14 * Red - Remote branches
15 * Magenta - Tags
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000016 * White - Merge Base Markers
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000017 * Blue background - The currently checked out commit
18"""
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000019
Edward Lemurb800fde2020-01-10 23:04:44 +000020from __future__ import unicode_literals
21
22import os
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000023import sys
24
Edward Lemurb800fde2020-01-10 23:04:44 +000025import git_common
26import setup_color
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000027import subprocess2
28
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000029from third_party import colorama
30
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000031
Edward Lemurb800fde2020-01-10 23:04:44 +000032if sys.version_info.major == 2:
33 # On Python 3, BrokenPipeError is raised instead.
Aravind Vasudevanc5f0cbb2022-01-24 23:56:57 +000034 # pylint:disable=redefined-builtin
Edward Lemurb800fde2020-01-10 23:04:44 +000035 BrokenPipeError = IOError
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000036
Edward Lemurb800fde2020-01-10 23:04:44 +000037
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000038RESET = colorama.Fore.RESET + colorama.Back.RESET + colorama.Style.RESET_ALL
Edward Lemurb800fde2020-01-10 23:04:44 +000039BRIGHT = colorama.Style.BRIGHT
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000040
Edward Lemurb800fde2020-01-10 23:04:44 +000041BLUE_BACK = colorama.Back.BLUE + BRIGHT
42BRIGHT_RED = colorama.Fore.RED + BRIGHT
43CYAN = colorama.Fore.CYAN + BRIGHT
44GREEN = colorama.Fore.GREEN + BRIGHT
45MAGENTA = colorama.Fore.MAGENTA + BRIGHT
46RED = colorama.Fore.RED
47WHITE = colorama.Fore.WHITE + BRIGHT
48YELLOW = colorama.Fore.YELLOW
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000049
anatoly techtonikc878ba62017-04-03 06:08:38 +030050
Edward Lemurb800fde2020-01-10 23:04:44 +000051def _print_help(outbuf):
anatoly techtonikc878ba62017-04-03 06:08:38 +030052 names = {
53 'Cyan': CYAN,
54 'Green': GREEN,
55 'Magenta': MAGENTA,
56 'Red': RED,
57 'White': WHITE,
Edward Lemurb800fde2020-01-10 23:04:44 +000058 'Blue background': BLUE_BACK,
anatoly techtonikc878ba62017-04-03 06:08:38 +030059 }
Edward Lemurb800fde2020-01-10 23:04:44 +000060 msg = ''
anatoly techtonikc878ba62017-04-03 06:08:38 +030061 for line in __doc__.splitlines():
Edward Lemurb800fde2020-01-10 23:04:44 +000062 for name, color in names.items():
63 if name in line:
64 msg += line.replace('* ' + name, color + '* ' + name + RESET) + '\n'
anatoly techtonikc878ba62017-04-03 06:08:38 +030065 break
66 else:
67 msg += line + '\n'
Edward Lemurb800fde2020-01-10 23:04:44 +000068 outbuf.write(msg.encode('utf-8', 'replace'))
anatoly techtonikc878ba62017-04-03 06:08:38 +030069
70
Edward Lemurb800fde2020-01-10 23:04:44 +000071def _color_branch(branch, all_branches, all_tags, current):
Aravind Vasudevanc5f0cbb2022-01-24 23:56:57 +000072 if branch in (current, 'HEAD -> ' + current):
Edward Lemurb800fde2020-01-10 23:04:44 +000073 color = CYAN
74 current = None
75 elif branch in all_branches:
76 color = GREEN
77 all_branches.remove(branch)
78 elif branch in all_tags:
79 color = MAGENTA
80 elif branch.startswith('tag: '):
81 color = MAGENTA
82 branch = branch[len('tag: '):]
83 else:
84 color = RED
85 return color + branch + RESET
86
87
88def _color_branch_list(branch_list, all_branches, all_tags, current):
89 if not branch_list:
90 return ''
91 colored_branches = (GREEN + ', ').join(
92 _color_branch(branch, all_branches, all_tags, current)
93 for branch in branch_list if branch != 'HEAD')
94 return (GREEN + '(' + colored_branches + GREEN + ') ' + RESET)
95
96
97def _parse_log_line(line):
98 graph, branch_list, commit_date, subject = (
99 line.decode('utf-8', 'replace').strip().split('\x00'))
100 branch_list = [] if not branch_list else branch_list.split(', ')
101 commit = graph.split()[-1]
102 graph = graph[:-len(commit)]
103 return graph, commit, branch_list, commit_date, subject
104
105
106def main(argv, outbuf):
107 if '-h' in argv or '--help' in argv:
108 _print_help(outbuf)
anatoly techtonikc878ba62017-04-03 06:08:38 +0300109 return 0
110
Edward Lemurb800fde2020-01-10 23:04:44 +0000111 map_extra = git_common.get_config_list('depot_tools.map_extra')
112 cmd = [
113 git_common.GIT_EXE, 'log', git_common.root(),
114 '--graph', '--branches', '--tags', '--color=always', '--date=short',
115 '--pretty=format:%H%x00%D%x00%cd%x00%s'
116 ] + map_extra + argv
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +0000117
Edward Lemurb800fde2020-01-10 23:04:44 +0000118 log_proc = subprocess2.Popen(cmd, stdout=subprocess2.PIPE, shell=False)
119
120 current = git_common.current_branch()
121 all_tags = set(git_common.tags())
122 all_branches = set(git_common.branches())
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +0000123 if current in all_branches:
124 all_branches.remove(current)
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +0000125
Edward Lemurb800fde2020-01-10 23:04:44 +0000126 merge_base_map = {}
127 for branch in all_branches:
128 merge_base = git_common.get_or_create_merge_base(branch)
129 if merge_base:
130 merge_base_map.setdefault(merge_base, set()).add(branch)
131
132 for merge_base, branches in merge_base_map.items():
133 merge_base_map[merge_base] = ', '.join(branches)
134
135 try:
136 for line in log_proc.stdout:
137 if b'\x00' not in line:
138 outbuf.write(line)
139 continue
140
141 graph, commit, branch_list, commit_date, subject = _parse_log_line(line)
142
143 if 'HEAD' in branch_list:
144 graph = graph.replace('*', BLUE_BACK + '*')
145
146 line = '{graph}{commit}\t{branches}{date} ~ {subject}'.format(
147 graph=graph,
148 commit=BRIGHT_RED + commit[:10] + RESET,
149 branches=_color_branch_list(
150 branch_list, all_branches, all_tags, current),
151 date=YELLOW + commit_date + RESET,
152 subject=subject)
153
154 if commit in merge_base_map:
155 line += ' <({})'.format(WHITE + merge_base_map[commit] + RESET)
156
157 line += os.linesep
158 outbuf.write(line.encode('utf-8', 'replace'))
159 except (BrokenPipeError, KeyboardInterrupt):
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +0000160 pass
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +0000161 return 0
162
163
164if __name__ == '__main__':
Edward Lemurb800fde2020-01-10 23:04:44 +0000165 setup_color.init()
166 with git_common.less() as less_input:
167 sys.exit(main(sys.argv[1:], less_input))