blob: 44f13c3bd5abc010269e48d05d6d82a7374ddf65 [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
calamity@chromium.org9d2c8802014-09-03 02:04:46 +00006"""Provides a short mapping of all the branches in your local repo, organized
7by their upstream ('tracking branch') layout.
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +00008
calamity@chromium.org9d2c8802014-09-03 02:04:46 +00009Example:
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000010origin/master
11 cool_feature
12 dependent_feature
13 other_dependent_feature
14 other_feature
15
16Branches are colorized as follows:
17 * Red - a remote branch (usually the root of all local branches)
18 * Cyan - a local branch which is the same as HEAD
19 * Note that multiple branches may be Cyan, if they are all on the same
20 commit, and you have that commit checked out.
21 * Green - a local branch
calamity@chromium.org4cd0a8b2014-09-23 03:30:50 +000022 * Blue - a 'branch-heads' branch
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000023 * Magenta - a tag
24 * Magenta '{NO UPSTREAM}' - If you have local branches which do not track any
25 upstream, then you will see this.
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000026"""
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000027
calamity@chromium.org9d2c8802014-09-03 02:04:46 +000028import argparse
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000029import collections
30import sys
calamity@chromium.org4cd0a8b2014-09-23 03:30:50 +000031import subprocess2
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000032
33from third_party import colorama
34from third_party.colorama import Fore, Style
35
calamity@chromium.org745ffa62014-09-08 01:03:19 +000036from git_common import current_branch, upstream, tags, get_branches_info
iannucci@chromium.org4c82eb52014-09-08 02:12:24 +000037from git_common import get_git_version, MIN_UPSTREAM_TRACK_GIT_VERSION, hash_one
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000038
calamity@chromium.org9d2c8802014-09-03 02:04:46 +000039DEFAULT_SEPARATOR = ' ' * 4
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000040
41
calamity@chromium.org9d2c8802014-09-03 02:04:46 +000042class OutputManager(object):
43 """Manages a number of OutputLines and formats them into aligned columns."""
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000044
calamity@chromium.org9d2c8802014-09-03 02:04:46 +000045 def __init__(self):
46 self.lines = []
47 self.nocolor = False
48 self.max_column_lengths = []
49 self.num_columns = None
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000050
calamity@chromium.org9d2c8802014-09-03 02:04:46 +000051 def append(self, line):
52 # All lines must have the same number of columns.
53 if not self.num_columns:
54 self.num_columns = len(line.columns)
55 self.max_column_lengths = [0] * self.num_columns
56 assert self.num_columns == len(line.columns)
57
58 if self.nocolor:
59 line.colors = [''] * self.num_columns
60
61 self.lines.append(line)
62
63 # Update maximum column lengths.
64 for i, col in enumerate(line.columns):
65 self.max_column_lengths[i] = max(self.max_column_lengths[i], len(col))
66
67 def as_formatted_string(self):
68 return '\n'.join(
69 l.as_padded_string(self.max_column_lengths) for l in self.lines)
70
71
72class OutputLine(object):
73 """A single line of data.
74
75 This consists of an equal number of columns, colors and separators."""
76
77 def __init__(self):
78 self.columns = []
79 self.separators = []
80 self.colors = []
81
82 def append(self, data, separator=DEFAULT_SEPARATOR, color=Fore.WHITE):
83 self.columns.append(data)
84 self.separators.append(separator)
85 self.colors.append(color)
86
87 def as_padded_string(self, max_column_lengths):
88 """"Returns the data as a string with each column padded to
89 |max_column_lengths|."""
90 output_string = ''
91 for i, (color, data, separator) in enumerate(
92 zip(self.colors, self.columns, self.separators)):
93 if max_column_lengths[i] == 0:
94 continue
95
96 padding = (max_column_lengths[i] - len(data)) * ' '
97 output_string += color + data + padding + separator
98
99 return output_string.rstrip()
100
101
102class BranchMapper(object):
103 """A class which constructs output representing the tree's branch structure.
104
105 Attributes:
calamity@chromium.org745ffa62014-09-08 01:03:19 +0000106 __branches_info: a map of branches to their BranchesInfo objects which
calamity@chromium.org9d2c8802014-09-03 02:04:46 +0000107 consist of the branch hash, upstream and ahead/behind status.
108 __gone_branches: a set of upstreams which are not fetchable by git"""
109
110 def __init__(self):
111 self.verbosity = 0
112 self.output = OutputManager()
calamity@chromium.org9d2c8802014-09-03 02:04:46 +0000113 self.__gone_branches = set()
calamity@chromium.org745ffa62014-09-08 01:03:19 +0000114 self.__branches_info = None
115 self.__parent_map = collections.defaultdict(list)
116 self.__current_branch = None
117 self.__current_hash = None
118 self.__tag_set = None
119
120 def start(self):
121 self.__branches_info = get_branches_info(
122 include_tracking_status=self.verbosity >= 1)
123 roots = set()
calamity@chromium.org9d2c8802014-09-03 02:04:46 +0000124
125 # A map of parents to a list of their children.
calamity@chromium.org745ffa62014-09-08 01:03:19 +0000126 for branch, branch_info in self.__branches_info.iteritems():
calamity@chromium.org9d2c8802014-09-03 02:04:46 +0000127 if not branch_info:
128 continue
129
130 parent = branch_info.upstream
calamity@chromium.org4cd0a8b2014-09-23 03:30:50 +0000131 if not self.__branches_info[parent]:
calamity@chromium.org9d2c8802014-09-03 02:04:46 +0000132 branch_upstream = upstream(branch)
133 # If git can't find the upstream, mark the upstream as gone.
134 if branch_upstream:
135 parent = branch_upstream
136 else:
137 self.__gone_branches.add(parent)
calamity@chromium.org745ffa62014-09-08 01:03:19 +0000138 # A parent that isn't in the branches info is a root.
139 roots.add(parent)
calamity@chromium.org9d2c8802014-09-03 02:04:46 +0000140
calamity@chromium.org745ffa62014-09-08 01:03:19 +0000141 self.__parent_map[parent].append(branch)
calamity@chromium.org9d2c8802014-09-03 02:04:46 +0000142
143 self.__current_branch = current_branch()
iannucci@chromium.org4c82eb52014-09-08 02:12:24 +0000144 self.__current_hash = hash_one('HEAD', short=True)
calamity@chromium.org9d2c8802014-09-03 02:04:46 +0000145 self.__tag_set = tags()
146
iannucci@chromium.org4c82eb52014-09-08 02:12:24 +0000147 if roots:
148 for root in sorted(roots):
149 self.__append_branch(root)
150 else:
151 no_branches = OutputLine()
152 no_branches.append('No User Branches')
153 self.output.append(no_branches)
calamity@chromium.org9d2c8802014-09-03 02:04:46 +0000154
155 def __is_invalid_parent(self, parent):
156 return not parent or parent in self.__gone_branches
157
158 def __color_for_branch(self, branch, branch_hash):
159 if branch.startswith('origin'):
160 color = Fore.RED
calamity@chromium.org4cd0a8b2014-09-23 03:30:50 +0000161 elif branch.startswith('branch-heads'):
162 color = Fore.BLUE
calamity@chromium.org9d2c8802014-09-03 02:04:46 +0000163 elif self.__is_invalid_parent(branch) or branch in self.__tag_set:
164 color = Fore.MAGENTA
iannucci@chromium.org4c82eb52014-09-08 02:12:24 +0000165 elif self.__current_hash.startswith(branch_hash):
calamity@chromium.org9d2c8802014-09-03 02:04:46 +0000166 color = Fore.CYAN
167 else:
168 color = Fore.GREEN
169
calamity@chromium.org4cd0a8b2014-09-23 03:30:50 +0000170 if branch_hash and self.__current_hash.startswith(branch_hash):
calamity@chromium.org9d2c8802014-09-03 02:04:46 +0000171 color += Style.BRIGHT
172 else:
173 color += Style.NORMAL
174
175 return color
176
177 def __append_branch(self, branch, depth=0):
178 """Recurses through the tree structure and appends an OutputLine to the
179 OutputManager for each branch."""
calamity@chromium.org745ffa62014-09-08 01:03:19 +0000180 branch_info = self.__branches_info[branch]
iannucci@chromium.org4c82eb52014-09-08 02:12:24 +0000181 if branch_info:
182 branch_hash = branch_info.hash
183 else:
calamity@chromium.org4cd0a8b2014-09-23 03:30:50 +0000184 try:
185 branch_hash = hash_one(branch, short=True)
186 except subprocess2.CalledProcessError:
187 branch_hash = None
calamity@chromium.org9d2c8802014-09-03 02:04:46 +0000188
189 line = OutputLine()
190
191 # The branch name with appropriate indentation.
192 suffix = ''
193 if branch == self.__current_branch or (
194 self.__current_branch == 'HEAD' and branch == self.__current_hash):
iannucci@chromium.orga112f032014-03-13 07:47:50 +0000195 suffix = ' *'
calamity@chromium.org9d2c8802014-09-03 02:04:46 +0000196 branch_string = branch
197 if branch in self.__gone_branches:
198 branch_string = '{%s:GONE}' % branch
199 if not branch:
200 branch_string = '{NO_UPSTREAM}'
201 main_string = ' ' * depth + branch_string + suffix
202 line.append(
203 main_string,
204 color=self.__color_for_branch(branch, branch_hash))
iannucci@chromium.orga112f032014-03-13 07:47:50 +0000205
calamity@chromium.org9d2c8802014-09-03 02:04:46 +0000206 # The branch hash.
207 if self.verbosity >= 2:
208 line.append(branch_hash or '', separator=' ', color=Fore.RED)
209
210 # The branch tracking status.
211 if self.verbosity >= 1:
212 ahead_string = ''
213 behind_string = ''
214 front_separator = ''
215 center_separator = ''
216 back_separator = ''
217 if branch_info and not self.__is_invalid_parent(branch_info.upstream):
218 ahead = branch_info.ahead
219 behind = branch_info.behind
220
221 if ahead:
222 ahead_string = 'ahead %d' % ahead
223 if behind:
224 behind_string = 'behind %d' % behind
225
226 if ahead or behind:
227 front_separator = '['
228 back_separator = ']'
229
230 if ahead and behind:
231 center_separator = '|'
232
233 line.append(front_separator, separator=' ')
234 line.append(ahead_string, separator=' ', color=Fore.MAGENTA)
235 line.append(center_separator, separator=' ')
236 line.append(behind_string, separator=' ', color=Fore.MAGENTA)
237 line.append(back_separator)
238
239 # The Rietveld issue associated with the branch.
240 if self.verbosity >= 2:
iannucci@chromium.org4c82eb52014-09-08 02:12:24 +0000241 import git_cl # avoid heavy import cost unless we need it
calamity@chromium.org9d2c8802014-09-03 02:04:46 +0000242 none_text = '' if self.__is_invalid_parent(branch) else 'None'
calamity@chromium.org4cd0a8b2014-09-23 03:30:50 +0000243 url = git_cl.Changelist(
244 branchref=branch).GetIssueURL() if branch_hash else None
calamity@chromium.org9d2c8802014-09-03 02:04:46 +0000245 line.append(url or none_text, color=Fore.BLUE if url else Fore.WHITE)
246
247 self.output.append(line)
248
calamity@chromium.org745ffa62014-09-08 01:03:19 +0000249 for child in sorted(self.__parent_map.pop(branch, ())):
calamity@chromium.org9d2c8802014-09-03 02:04:46 +0000250 self.__append_branch(child, depth=depth + 1)
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +0000251
252
253def main(argv):
254 colorama.init()
calamity@chromium.org9d2c8802014-09-03 02:04:46 +0000255 if get_git_version() < MIN_UPSTREAM_TRACK_GIT_VERSION:
256 print >> sys.stderr, (
257 'This tool will not show all tracking information for git version '
258 'earlier than ' +
259 '.'.join(str(x) for x in MIN_UPSTREAM_TRACK_GIT_VERSION) +
260 '. Please consider upgrading.')
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +0000261
calamity@chromium.org9d2c8802014-09-03 02:04:46 +0000262 parser = argparse.ArgumentParser(
263 description='Print a a tree of all branches parented by their upstreams')
264 parser.add_argument('-v', action='count',
265 help='Display branch hash and Rietveld URL')
266 parser.add_argument('--no-color', action='store_true', dest='nocolor',
267 help='Turn off colors.')
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +0000268
calamity@chromium.org9d2c8802014-09-03 02:04:46 +0000269 opts = parser.parse_args(argv[1:])
270
271 mapper = BranchMapper()
272 mapper.verbosity = opts.v
273 mapper.output.nocolor = opts.nocolor
274 mapper.start()
275 print mapper.output.as_formatted_string()
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +0000276
277if __name__ == '__main__':
278 sys.exit(main(sys.argv))