Josip Sokcevic | 4de5dea | 2022-03-23 21:15:14 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
iannucci@chromium.org | a112f03 | 2014-03-13 07:47:50 +0000 | [diff] [blame] | 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 |
| 4 | # found in the LICENSE file. |
anatoly techtonik | 222840f | 2017-04-15 16:25:57 +0300 | [diff] [blame] | 5 | """Print dependency tree of branches in local repo. |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 6 | |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 7 | Example: |
Josip Sokcevic | 9c0dc30 | 2020-11-20 18:41:25 +0000 | [diff] [blame] | 8 | origin/main |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 9 | cool_feature |
| 10 | dependent_feature |
| 11 | other_dependent_feature |
| 12 | other_feature |
| 13 | |
| 14 | Branches are colorized as follows: |
| 15 | * Red - a remote branch (usually the root of all local branches) |
| 16 | * Cyan - a local branch which is the same as HEAD |
| 17 | * Note that multiple branches may be Cyan, if they are all on the same |
| 18 | commit, and you have that commit checked out. |
| 19 | * Green - a local branch |
calamity@chromium.org | 4cd0a8b | 2014-09-23 03:30:50 +0000 | [diff] [blame] | 20 | * Blue - a 'branch-heads' branch |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 21 | * Magenta - a tag |
| 22 | * Magenta '{NO UPSTREAM}' - If you have local branches which do not track any |
| 23 | upstream, then you will see this. |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 24 | """ |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 25 | |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 26 | import argparse |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 27 | import collections |
Josip Sokcevic | 306b03b | 2022-02-23 03:25:23 +0000 | [diff] [blame] | 28 | import metrics |
calamity@chromium.org | 4cd0a8b | 2014-09-23 03:30:50 +0000 | [diff] [blame] | 29 | import subprocess2 |
iannucci@chromium.org | 596cd5c | 2016-04-04 21:34:39 +0000 | [diff] [blame] | 30 | import sys |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 31 | |
calamity@chromium.org | 745ffa6 | 2014-09-08 01:03:19 +0000 | [diff] [blame] | 32 | from git_common import current_branch, upstream, tags, get_branches_info |
iannucci@chromium.org | 4c82eb5 | 2014-09-08 02:12:24 +0000 | [diff] [blame] | 33 | from git_common import get_git_version, MIN_UPSTREAM_TRACK_GIT_VERSION, hash_one |
borenet@google.com | 09156ec | 2015-03-26 14:10:06 +0000 | [diff] [blame] | 34 | from git_common import run |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 35 | |
iannucci@chromium.org | 596cd5c | 2016-04-04 21:34:39 +0000 | [diff] [blame] | 36 | import setup_color |
| 37 | |
| 38 | from third_party.colorama import Fore, Style |
| 39 | |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 40 | DEFAULT_SEPARATOR = ' ' * 4 |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 41 | |
| 42 | |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 43 | class OutputManager(object): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 44 | """Manages a number of OutputLines and formats them into aligned columns.""" |
| 45 | def __init__(self): |
| 46 | self.lines = [] |
| 47 | self.nocolor = False |
| 48 | self.max_column_lengths = [] |
| 49 | self.num_columns = None |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 50 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 51 | 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) |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 57 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 58 | if self.nocolor: |
| 59 | line.colors = [''] * self.num_columns |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 60 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 61 | self.lines.append(line) |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 62 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 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], |
| 66 | len(col)) |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 67 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 68 | def merge(self, other): |
| 69 | for line in other.lines: |
| 70 | self.append(line) |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 71 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 72 | def as_formatted_string(self): |
| 73 | return '\n'.join( |
| 74 | l.as_padded_string(self.max_column_lengths) for l in self.lines) |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 75 | |
| 76 | |
| 77 | class OutputLine(object): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 78 | """A single line of data. |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 79 | |
| 80 | This consists of an equal number of columns, colors and separators.""" |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 81 | def __init__(self): |
| 82 | self.columns = [] |
| 83 | self.separators = [] |
| 84 | self.colors = [] |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 85 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 86 | def append(self, data, separator=DEFAULT_SEPARATOR, color=Fore.WHITE): |
| 87 | self.columns.append(data) |
| 88 | self.separators.append(separator) |
| 89 | self.colors.append(color) |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 90 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 91 | def as_padded_string(self, max_column_lengths): |
| 92 | """"Returns the data as a string with each column padded to |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 93 | |max_column_lengths|.""" |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 94 | output_string = '' |
| 95 | for i, (color, data, separator) in enumerate( |
| 96 | zip(self.colors, self.columns, self.separators)): |
| 97 | if max_column_lengths[i] == 0: |
| 98 | continue |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 99 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 100 | padding = (max_column_lengths[i] - len(data)) * ' ' |
| 101 | output_string += color + data + padding + separator |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 102 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 103 | return output_string.rstrip() |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 104 | |
| 105 | |
| 106 | class BranchMapper(object): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 107 | """A class which constructs output representing the tree's branch structure. |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 108 | |
| 109 | Attributes: |
calamity@chromium.org | 745ffa6 | 2014-09-08 01:03:19 +0000 | [diff] [blame] | 110 | __branches_info: a map of branches to their BranchesInfo objects which |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 111 | consist of the branch hash, upstream and ahead/behind status. |
| 112 | __gone_branches: a set of upstreams which are not fetchable by git""" |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 113 | def __init__(self): |
| 114 | self.verbosity = 0 |
| 115 | self.maxjobs = 0 |
| 116 | self.show_subject = False |
| 117 | self.hide_dormant = False |
| 118 | self.output = OutputManager() |
| 119 | self.__gone_branches = set() |
| 120 | self.__branches_info = None |
| 121 | self.__parent_map = collections.defaultdict(list) |
| 122 | self.__current_branch = None |
| 123 | self.__current_hash = None |
| 124 | self.__tag_set = None |
| 125 | self.__status_info = {} |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 126 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 127 | def start(self): |
| 128 | self.__branches_info = get_branches_info( |
| 129 | include_tracking_status=self.verbosity >= 1) |
| 130 | if (self.verbosity >= 2): |
| 131 | # Avoid heavy import unless necessary. |
| 132 | from git_cl import get_cl_statuses, color_for_status, Changelist |
calamity@chromium.org | 745ffa6 | 2014-09-08 01:03:19 +0000 | [diff] [blame] | 133 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 134 | change_cls = [ |
| 135 | Changelist(branchref='refs/heads/' + b) |
| 136 | for b in self.__branches_info.keys() if b |
| 137 | ] |
| 138 | status_info = get_cl_statuses(change_cls, |
| 139 | fine_grained=self.verbosity > 2, |
| 140 | max_processes=self.maxjobs) |
calamity@chromium.org | ffde55c | 2015-03-12 00:44:17 +0000 | [diff] [blame] | 141 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 142 | # This is a blocking get which waits for the remote CL status to be |
| 143 | # retrieved. |
| 144 | for cl, status in status_info: |
| 145 | self.__status_info[cl.GetBranch()] = (cl.GetIssueURL( |
| 146 | short=True), color_for_status(status), status) |
calamity@chromium.org | ffde55c | 2015-03-12 00:44:17 +0000 | [diff] [blame] | 147 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 148 | roots = set() |
calamity@chromium.org | ffde55c | 2015-03-12 00:44:17 +0000 | [diff] [blame] | 149 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 150 | # A map of parents to a list of their children. |
| 151 | for branch, branch_info in self.__branches_info.items(): |
| 152 | if not branch_info: |
| 153 | continue |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 154 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 155 | parent = branch_info.upstream |
| 156 | if self.__check_cycle(branch): |
| 157 | continue |
| 158 | if not self.__branches_info[parent]: |
| 159 | branch_upstream = upstream(branch) |
| 160 | # If git can't find the upstream, mark the upstream as gone. |
| 161 | if branch_upstream: |
| 162 | parent = branch_upstream |
| 163 | else: |
| 164 | self.__gone_branches.add(parent) |
| 165 | # A parent that isn't in the branches info is a root. |
| 166 | roots.add(parent) |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 167 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 168 | self.__parent_map[parent].append(branch) |
| 169 | |
| 170 | self.__current_branch = current_branch() |
| 171 | self.__current_hash = hash_one('HEAD', short=True) |
| 172 | self.__tag_set = tags() |
| 173 | |
| 174 | if roots: |
| 175 | for root in sorted(roots): |
| 176 | self.__append_branch(root, self.output) |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 177 | else: |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 178 | no_branches = OutputLine() |
| 179 | no_branches.append('No User Branches') |
| 180 | self.output.append(no_branches) |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 181 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 182 | def __check_cycle(self, branch): |
| 183 | # Maximum length of the cycle is `num_branches`. This limit avoids |
| 184 | # running into a cycle which does *not* contain `branch`. |
| 185 | num_branches = len(self.__branches_info) |
| 186 | cycle = [branch] |
| 187 | while len(cycle) < num_branches and self.__branches_info[cycle[-1]]: |
| 188 | parent = self.__branches_info[cycle[-1]].upstream |
| 189 | cycle.append(parent) |
| 190 | if parent == branch: |
| 191 | print('Warning: Detected cycle in branches: {}'.format( |
| 192 | ' -> '.join(cycle)), |
| 193 | file=sys.stderr) |
| 194 | return True |
| 195 | return False |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 196 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 197 | def __is_invalid_parent(self, parent): |
| 198 | return not parent or parent in self.__gone_branches |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 199 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 200 | def __color_for_branch(self, branch, branch_hash): |
| 201 | if branch.startswith('origin/'): |
| 202 | color = Fore.RED |
| 203 | elif branch.startswith('branch-heads'): |
| 204 | color = Fore.BLUE |
| 205 | elif self.__is_invalid_parent(branch) or branch in self.__tag_set: |
| 206 | color = Fore.MAGENTA |
| 207 | elif self.__current_hash.startswith(branch_hash): |
| 208 | color = Fore.CYAN |
| 209 | else: |
| 210 | color = Fore.GREEN |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 211 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 212 | if branch_hash and self.__current_hash.startswith(branch_hash): |
| 213 | color += Style.BRIGHT |
| 214 | else: |
| 215 | color += Style.NORMAL |
Clemens Hammacher | 793183d | 2019-03-22 01:12:46 +0000 | [diff] [blame] | 216 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 217 | return color |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 218 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 219 | def __is_dormant_branch(self, branch): |
| 220 | if '/' in branch: |
| 221 | return False |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 222 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 223 | is_dormant = run('config', |
| 224 | '--get', |
| 225 | 'branch.{}.dormant'.format(branch), |
| 226 | accepted_retcodes=[0, 1]) |
| 227 | return is_dormant == 'true' |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 228 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 229 | def __append_branch(self, branch, output, depth=0): |
| 230 | """Recurses through the tree structure and appends an OutputLine to the |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 231 | OutputManager for each branch.""" |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 232 | child_output = OutputManager() |
| 233 | for child in sorted(self.__parent_map.pop(branch, ())): |
| 234 | self.__append_branch(child, child_output, depth=depth + 1) |
Orr Bernstein | b7e16d2 | 2023-07-14 11:00:55 +0000 | [diff] [blame] | 235 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 236 | is_dormant_branch = self.__is_dormant_branch(branch) |
| 237 | if self.hide_dormant and is_dormant_branch and not child_output.lines: |
| 238 | return |
Orr Bernstein | b7e16d2 | 2023-07-14 11:00:55 +0000 | [diff] [blame] | 239 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 240 | branch_info = self.__branches_info[branch] |
| 241 | if branch_info: |
| 242 | branch_hash = branch_info.hash |
| 243 | else: |
| 244 | try: |
| 245 | branch_hash = hash_one(branch, short=True) |
| 246 | except subprocess2.CalledProcessError: |
| 247 | branch_hash = None |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 248 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 249 | line = OutputLine() |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 250 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 251 | # The branch name with appropriate indentation. |
| 252 | suffix = '' |
| 253 | if branch == self.__current_branch or (self.__current_branch == 'HEAD' |
| 254 | and branch |
| 255 | == self.__current_hash): |
| 256 | suffix = ' *' |
| 257 | branch_string = branch |
| 258 | if branch in self.__gone_branches: |
| 259 | branch_string = '{%s:GONE}' % branch |
| 260 | if not branch: |
| 261 | branch_string = '{NO_UPSTREAM}' |
| 262 | main_string = ' ' * depth + branch_string + suffix |
| 263 | line.append(main_string, |
| 264 | color=self.__color_for_branch(branch, branch_hash)) |
iannucci@chromium.org | a112f03 | 2014-03-13 07:47:50 +0000 | [diff] [blame] | 265 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 266 | # The branch hash. |
| 267 | if self.verbosity >= 2: |
| 268 | line.append(branch_hash or '', separator=' ', color=Fore.RED) |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 269 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 270 | # The branch tracking status. |
| 271 | if self.verbosity >= 1: |
| 272 | commits_string = '' |
| 273 | behind_string = '' |
| 274 | front_separator = '' |
| 275 | center_separator = '' |
| 276 | back_separator = '' |
| 277 | if branch_info and not self.__is_invalid_parent( |
| 278 | branch_info.upstream): |
| 279 | behind = branch_info.behind |
| 280 | commits = branch_info.commits |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 281 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 282 | if commits: |
| 283 | commits_string = '%d commit' % commits |
| 284 | commits_string += 's' if commits > 1 else ' ' |
| 285 | if behind: |
| 286 | behind_string = 'behind %d' % behind |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 287 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 288 | if commits or behind: |
| 289 | front_separator = '[' |
| 290 | back_separator = ']' |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 291 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 292 | if commits and behind: |
| 293 | center_separator = '|' |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 294 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 295 | line.append(front_separator, separator=' ') |
| 296 | line.append(commits_string, separator=' ', color=Fore.MAGENTA) |
| 297 | line.append(center_separator, separator=' ') |
| 298 | line.append(behind_string, separator=' ', color=Fore.MAGENTA) |
| 299 | line.append(back_separator) |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 300 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 301 | if self.verbosity >= 4: |
| 302 | line.append(' (dormant)' if is_dormant_branch else ' ', |
| 303 | separator=' ', |
| 304 | color=Fore.RED) |
Orr Bernstein | b7e16d2 | 2023-07-14 11:00:55 +0000 | [diff] [blame] | 305 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 306 | # The Rietveld issue associated with the branch. |
| 307 | if self.verbosity >= 2: |
| 308 | (url, color, |
| 309 | status) = (('', '', '') if self.__is_invalid_parent(branch) else |
| 310 | self.__status_info[branch]) |
| 311 | if self.verbosity > 2: |
| 312 | line.append('{} ({})'.format(url, status) if url else '', |
| 313 | color=color) |
| 314 | else: |
| 315 | line.append(url or '', color=color) |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 316 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 317 | # The subject of the most recent commit on the branch. |
| 318 | if self.show_subject: |
| 319 | if not self.__is_invalid_parent(branch): |
| 320 | line.append(run('log', '-n1', '--format=%s', branch, '--')) |
| 321 | else: |
| 322 | line.append('') |
borenet@google.com | 09156ec | 2015-03-26 14:10:06 +0000 | [diff] [blame] | 323 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 324 | output.append(line) |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 325 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 326 | output.merge(child_output) |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 327 | |
| 328 | |
anatoly techtonik | 222840f | 2017-04-15 16:25:57 +0300 | [diff] [blame] | 329 | def print_desc(): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 330 | for line in __doc__.splitlines(): |
| 331 | starpos = line.find('* ') |
| 332 | if starpos == -1 or '-' not in line: |
| 333 | print(line) |
| 334 | else: |
| 335 | _, color, rest = line.split(None, 2) |
| 336 | outline = line[:starpos + 1] |
| 337 | outline += getattr(Fore, |
| 338 | color.upper()) + " " + color + " " + Fore.RESET |
| 339 | outline += rest |
| 340 | print(outline) |
| 341 | print('') |
| 342 | |
anatoly techtonik | 222840f | 2017-04-15 16:25:57 +0300 | [diff] [blame] | 343 | |
Josip Sokcevic | 306b03b | 2022-02-23 03:25:23 +0000 | [diff] [blame] | 344 | @metrics.collector.collect_metrics('git map-branches') |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 345 | def main(argv): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 346 | setup_color.init() |
| 347 | if get_git_version() < MIN_UPSTREAM_TRACK_GIT_VERSION: |
| 348 | print( |
| 349 | 'This tool will not show all tracking information for git version ' |
| 350 | 'earlier than ' + |
| 351 | '.'.join(str(x) for x in MIN_UPSTREAM_TRACK_GIT_VERSION) + |
| 352 | '. Please consider upgrading.', |
| 353 | file=sys.stderr) |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 354 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 355 | if '-h' in argv: |
| 356 | print_desc() |
anatoly techtonik | 222840f | 2017-04-15 16:25:57 +0300 | [diff] [blame] | 357 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 358 | parser = argparse.ArgumentParser() |
| 359 | parser.add_argument('-v', |
| 360 | action='count', |
| 361 | default=0, |
| 362 | help=('Pass once to show tracking info, ' |
| 363 | 'twice for hash and review url, ' |
| 364 | 'thrice for review status, ' |
| 365 | 'four times to mark dormant branches')) |
| 366 | parser.add_argument('--no-color', |
| 367 | action='store_true', |
| 368 | dest='nocolor', |
| 369 | help='Turn off colors.') |
| 370 | parser.add_argument( |
| 371 | '-j', |
| 372 | '--maxjobs', |
| 373 | action='store', |
| 374 | type=int, |
| 375 | help='The number of jobs to use when retrieving review status') |
| 376 | parser.add_argument('--show-subject', |
| 377 | action='store_true', |
| 378 | dest='show_subject', |
| 379 | help='Show the commit subject.') |
| 380 | parser.add_argument('--hide-dormant', |
| 381 | action='store_true', |
| 382 | dest='hide_dormant', |
| 383 | help='Hides dormant branches.') |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 384 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 385 | opts = parser.parse_args(argv) |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 386 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 387 | mapper = BranchMapper() |
| 388 | mapper.verbosity = opts.v |
| 389 | mapper.output.nocolor = opts.nocolor |
| 390 | mapper.maxjobs = opts.maxjobs |
| 391 | mapper.show_subject = opts.show_subject |
| 392 | mapper.hide_dormant = opts.hide_dormant |
| 393 | mapper.start() |
| 394 | print(mapper.output.as_formatted_string()) |
| 395 | return 0 |
| 396 | |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 397 | |
| 398 | if __name__ == '__main__': |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 399 | try: |
| 400 | with metrics.collector.print_notice_and_exit(): |
| 401 | sys.exit(main(sys.argv[1:])) |
| 402 | except KeyboardInterrupt: |
| 403 | sys.stderr.write('interrupted\n') |
| 404 | sys.exit(1) |