blob: a4e2007de62cf0a2dc3bac83dee18bbe3130c378 [file] [log] [blame]
Josip Sokcevic4de5dea2022-03-23 21:15:14 +00001#!/usr/bin/env python3
Francois Dorayd42c6812017-05-30 15:10:20 -04002# Copyright 2017 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.
Francois Dorayd42c6812017-05-30 15:10:20 -04005"""Splits a branch into smaller branches and uploads CLs."""
6
7import collections
8import os
9import re
10import subprocess2
11import sys
Francois Dorayd42c6812017-05-30 15:10:20 -040012
Edward Lemur1773f372020-02-22 00:27:14 +000013import gclient_utils
Francois Dorayd42c6812017-05-30 15:10:20 -040014import git_footers
Josip Sokcevic7958e302023-03-01 23:02:21 +000015import scm
Francois Dorayd42c6812017-05-30 15:10:20 -040016
17import git_common as git
18
Stephen Martinisf53f82c2018-09-07 20:58:05 +000019# If a call to `git cl split` will generate more than this number of CLs, the
20# command will prompt the user to make sure they know what they're doing. Large
21# numbers of CLs generated by `git cl split` have caused infrastructure issues
22# in the past.
23CL_SPLIT_FORCE_LIMIT = 10
24
Anne Redullab5509952023-07-27 01:27:02 +000025# The maximum number of top reviewers to list. `git cl split` may send many CLs
26# to a single reviewer, so the top reviewers with the most CLs sent to them
27# will be listed.
28CL_SPLIT_TOP_REVIEWERS = 5
29
Peter Kotwicz70d971a2023-08-01 22:26:14 +000030FilesAndOwnersDirectory = collections.namedtuple("FilesAndOwnersDirectory",
31 "files owners_directories")
32
Stephen Martinisf53f82c2018-09-07 20:58:05 +000033
Francois Dorayd42c6812017-05-30 15:10:20 -040034def EnsureInGitRepository():
Mike Frysinger124bb8e2023-09-06 05:48:55 +000035 """Throws an exception if the current directory is not a git repository."""
36 git.run('rev-parse')
Francois Dorayd42c6812017-05-30 15:10:20 -040037
38
Peter Kotwicz70d971a2023-08-01 22:26:14 +000039def CreateBranchForDirectories(prefix, directories, upstream):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000040 """Creates a branch named |prefix| + "_" + |directories[0]| + "_split".
Francois Dorayd42c6812017-05-30 15:10:20 -040041
42 Return false if the branch already exists. |upstream| is used as upstream for
43 the created branch.
44 """
Mike Frysinger124bb8e2023-09-06 05:48:55 +000045 existing_branches = set(git.branches(use_limit=False))
46 branch_name = prefix + '_' + directories[0] + '_split'
47 if branch_name in existing_branches:
48 return False
49 git.run('checkout', '-t', upstream, '-b', branch_name)
50 return True
Francois Dorayd42c6812017-05-30 15:10:20 -040051
52
Peter Kotwicz70d971a2023-08-01 22:26:14 +000053def FormatDirectoriesForPrinting(directories, prefix=None):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000054 """Formats directory list for printing
Peter Kotwicz70d971a2023-08-01 22:26:14 +000055
56 Uses dedicated format for single-item list."""
57
Mike Frysinger124bb8e2023-09-06 05:48:55 +000058 prefixed = directories
59 if prefix:
60 prefixed = [(prefix + d) for d in directories]
Peter Kotwicz70d971a2023-08-01 22:26:14 +000061
Mike Frysinger124bb8e2023-09-06 05:48:55 +000062 return str(prefixed) if len(prefixed) > 1 else str(prefixed[0])
Peter Kotwicz70d971a2023-08-01 22:26:14 +000063
64
65def FormatDescriptionOrComment(txt, directories):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000066 """Replaces $directory with |directories| in |txt|."""
67 to_insert = FormatDirectoriesForPrinting(directories, prefix='/')
68 return txt.replace('$directory', to_insert)
Francois Dorayd42c6812017-05-30 15:10:20 -040069
70
71def AddUploadedByGitClSplitToDescription(description):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000072 """Adds a 'This CL was uploaded by git cl split.' line to |description|.
Francois Dorayd42c6812017-05-30 15:10:20 -040073
74 The line is added before footers, or at the end of |description| if it has no
75 footers.
76 """
Mike Frysinger124bb8e2023-09-06 05:48:55 +000077 split_footers = git_footers.split_footers(description)
78 lines = split_footers[0]
79 if lines[-1] and not lines[-1].isspace():
80 lines = lines + ['']
81 lines = lines + ['This CL was uploaded by git cl split.']
82 if split_footers[1]:
83 lines += [''] + split_footers[1]
84 return '\n'.join(lines)
Francois Dorayd42c6812017-05-30 15:10:20 -040085
86
Peter Kotwicz70d971a2023-08-01 22:26:14 +000087def UploadCl(refactor_branch, refactor_branch_upstream, directories, files,
Edward Lemurac5c55f2020-02-29 00:17:16 +000088 description, comment, reviewers, changelist, cmd_upload,
Rachael Newitt03e49122023-06-28 21:39:21 +000089 cq_dry_run, enable_auto_submit, topic, repository_root):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000090 """Uploads a CL with all changes to |files| in |refactor_branch|.
Francois Dorayd42c6812017-05-30 15:10:20 -040091
92 Args:
93 refactor_branch: Name of the branch that contains the changes to upload.
94 refactor_branch_upstream: Name of the upstream of |refactor_branch|.
Peter Kotwicz70d971a2023-08-01 22:26:14 +000095 directories: Paths to the directories that contain the OWNERS files for
96 which to upload a CL.
Francois Dorayd42c6812017-05-30 15:10:20 -040097 files: List of AffectedFile instances to include in the uploaded CL.
Francois Dorayd42c6812017-05-30 15:10:20 -040098 description: Description of the uploaded CL.
99 comment: Comment to post on the uploaded CL.
Edward Lemurac5c55f2020-02-29 00:17:16 +0000100 reviewers: A set of reviewers for the CL.
Francois Dorayd42c6812017-05-30 15:10:20 -0400101 changelist: The Changelist class.
102 cmd_upload: The function associated with the git cl upload command.
Stephen Martiniscb326682018-08-29 21:06:30 +0000103 cq_dry_run: If CL uploads should also do a cq dry run.
Takuto Ikuta51eca592019-02-14 19:40:52 +0000104 enable_auto_submit: If CL uploads should also enable auto submit.
Rachael Newitt03e49122023-06-28 21:39:21 +0000105 topic: Topic to associate with uploaded CLs.
Francois Dorayd42c6812017-05-30 15:10:20 -0400106 """
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000107 # Create a branch.
108 if not CreateBranchForDirectories(refactor_branch, directories,
109 refactor_branch_upstream):
110 print('Skipping ' + FormatDirectoriesForPrinting(directories) +
111 ' for which a branch already exists.')
112 return
Francois Dorayd42c6812017-05-30 15:10:20 -0400113
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000114 # Checkout all changes to files in |files|.
115 deleted_files = []
116 modified_files = []
117 for action, f in files:
118 abspath = os.path.abspath(os.path.join(repository_root, f))
119 if action == 'D':
120 deleted_files.append(abspath)
121 else:
122 modified_files.append(abspath)
Edward Lemur2c62b332020-03-12 22:12:33 +0000123
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000124 if deleted_files:
125 git.run(*['rm'] + deleted_files)
126 if modified_files:
127 git.run(*['checkout', refactor_branch, '--'] + modified_files)
Francois Dorayd42c6812017-05-30 15:10:20 -0400128
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000129 # Commit changes. The temporary file is created with delete=False so that it
130 # can be deleted manually after git has read it rather than automatically
131 # when it is closed.
132 with gclient_utils.temporary_file() as tmp_file:
133 gclient_utils.FileWrite(
134 tmp_file, FormatDescriptionOrComment(description, directories))
135 git.run('commit', '-F', tmp_file)
Francois Dorayd42c6812017-05-30 15:10:20 -0400136
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000137 # Upload a CL.
138 upload_args = ['-f']
139 if reviewers:
140 upload_args.extend(['-r', ','.join(sorted(reviewers))])
141 if cq_dry_run:
142 upload_args.append('--cq-dry-run')
143 if not comment:
144 upload_args.append('--send-mail')
145 if enable_auto_submit:
146 upload_args.append('--enable-auto-submit')
147 if topic:
148 upload_args.append('--topic={}'.format(topic))
149 print('Uploading CL for ' + FormatDirectoriesForPrinting(directories) +
150 '...')
Olivier Li06145912021-05-12 23:59:24 +0000151
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000152 ret = cmd_upload(upload_args)
153 if ret != 0:
154 print('Uploading failed.')
155 print('Note: git cl split has built-in resume capabilities.')
156 print('Delete ' + git.current_branch() +
157 ' then run git cl split again to resume uploading.')
Olivier Li06145912021-05-12 23:59:24 +0000158
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000159 if comment:
160 changelist().AddComment(FormatDescriptionOrComment(
161 comment, directories),
162 publish=True)
Francois Dorayd42c6812017-05-30 15:10:20 -0400163
164
Daniel Cheng403c44e2022-10-05 22:24:58 +0000165def GetFilesSplitByOwners(files, max_depth):
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000166 """Returns a map of files split by OWNERS file.
Francois Dorayd42c6812017-05-30 15:10:20 -0400167
168 Returns:
169 A map where keys are paths to directories containing an OWNERS file and
170 values are lists of files sharing an OWNERS file.
171 """
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000172 files_split_by_owners = {}
173 for action, path in files:
174 # normpath() is important to normalize separators here, in prepration
175 # for str.split() before. It would be nicer to use something like
176 # pathlib here but alas...
177 dir_with_owners = os.path.normpath(os.path.dirname(path))
178 if max_depth >= 1:
179 dir_with_owners = os.path.join(
180 *dir_with_owners.split(os.path.sep)[:max_depth])
181 # Find the closest parent directory with an OWNERS file.
182 while (dir_with_owners not in files_split_by_owners
183 and not os.path.isfile(os.path.join(dir_with_owners, 'OWNERS'))):
184 dir_with_owners = os.path.dirname(dir_with_owners)
185 files_split_by_owners.setdefault(dir_with_owners, []).append(
186 (action, path))
187 return files_split_by_owners
Francois Dorayd42c6812017-05-30 15:10:20 -0400188
189
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000190def PrintClInfo(cl_index, num_cls, directories, file_paths, description,
Anne Redulla072d06e2023-07-06 23:12:16 +0000191 reviewers, enable_auto_submit, topic):
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000192 """Prints info about a CL.
Chris Watkinsba28e462017-12-13 11:22:17 +1100193
194 Args:
195 cl_index: The index of this CL in the list of CLs to upload.
196 num_cls: The total number of CLs that will be uploaded.
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000197 directories: Paths to directories that contains the OWNERS files for which
Chris Watkinsba28e462017-12-13 11:22:17 +1100198 to upload a CL.
199 file_paths: A list of files in this CL.
200 description: The CL description.
Edward Lemurac5c55f2020-02-29 00:17:16 +0000201 reviewers: A set of reviewers for this CL.
Anne Redulla072d06e2023-07-06 23:12:16 +0000202 enable_auto_submit: If the CL should also have auto submit enabled.
Rachael Newitt03e49122023-06-28 21:39:21 +0000203 topic: Topic to set for this CL.
Chris Watkinsba28e462017-12-13 11:22:17 +1100204 """
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000205 description_lines = FormatDescriptionOrComment(description,
206 directories).splitlines()
207 indented_description = '\n'.join([' ' + l for l in description_lines])
Chris Watkinsba28e462017-12-13 11:22:17 +1100208
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000209 print('CL {}/{}'.format(cl_index, num_cls))
210 print('Paths: {}'.format(FormatDirectoriesForPrinting(directories)))
211 print('Reviewers: {}'.format(', '.join(reviewers)))
212 print('Auto-Submit: {}'.format(enable_auto_submit))
213 print('Topic: {}'.format(topic))
214 print('\n' + indented_description + '\n')
215 print('\n'.join(file_paths))
216 print()
Chris Watkinsba28e462017-12-13 11:22:17 +1100217
218
Stephen Martiniscb326682018-08-29 21:06:30 +0000219def SplitCl(description_file, comment_file, changelist, cmd_upload, dry_run,
Rachael Newitt03e49122023-06-28 21:39:21 +0000220 cq_dry_run, enable_auto_submit, max_depth, topic, repository_root):
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000221 """"Splits a branch into smaller branches and uploads CLs.
Francois Dorayd42c6812017-05-30 15:10:20 -0400222
223 Args:
224 description_file: File containing the description of uploaded CLs.
225 comment_file: File containing the comment of uploaded CLs.
226 changelist: The Changelist class.
227 cmd_upload: The function associated with the git cl upload command.
Chris Watkinsba28e462017-12-13 11:22:17 +1100228 dry_run: Whether this is a dry run (no branches or CLs created).
Stephen Martiniscb326682018-08-29 21:06:30 +0000229 cq_dry_run: If CL uploads should also do a cq dry run.
Takuto Ikuta51eca592019-02-14 19:40:52 +0000230 enable_auto_submit: If CL uploads should also enable auto submit.
Daniel Cheng403c44e2022-10-05 22:24:58 +0000231 max_depth: The maximum directory depth to search for OWNERS files. A value
232 less than 1 means no limit.
Rachael Newitt03e49122023-06-28 21:39:21 +0000233 topic: Topic to associate with split CLs.
Francois Dorayd42c6812017-05-30 15:10:20 -0400234
235 Returns:
236 0 in case of success. 1 in case of error.
237 """
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000238 description = AddUploadedByGitClSplitToDescription(
239 gclient_utils.FileRead(description_file))
240 comment = gclient_utils.FileRead(comment_file) if comment_file else None
Francois Dorayd42c6812017-05-30 15:10:20 -0400241
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000242 try:
243 EnsureInGitRepository()
Francois Dorayd42c6812017-05-30 15:10:20 -0400244
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000245 cl = changelist()
246 upstream = cl.GetCommonAncestorWithUpstream()
247 files = [
248 (action.strip(), f)
249 for action, f in scm.GIT.CaptureStatus(repository_root, upstream)
250 ]
Francois Dorayd42c6812017-05-30 15:10:20 -0400251
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000252 if not files:
253 print('Cannot split an empty CL.')
254 return 1
Francois Dorayd42c6812017-05-30 15:10:20 -0400255
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000256 author = git.run('config', 'user.email').strip() or None
257 refactor_branch = git.current_branch()
258 assert refactor_branch, "Can't run from detached branch."
259 refactor_branch_upstream = git.upstream(refactor_branch)
260 assert refactor_branch_upstream, \
261 "Branch %s must have an upstream." % refactor_branch
Francois Dorayd42c6812017-05-30 15:10:20 -0400262
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000263 if not CheckDescriptionBugLink(description):
264 return 0
Francois Dorayd42c6812017-05-30 15:10:20 -0400265
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000266 files_split_by_reviewers = SelectReviewersForFiles(
267 cl, author, files, max_depth)
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000268
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000269 num_cls = len(files_split_by_reviewers)
270 print('Will split current branch (' + refactor_branch + ') into ' +
271 str(num_cls) + ' CLs.\n')
272 if cq_dry_run and num_cls > CL_SPLIT_FORCE_LIMIT:
273 print(
274 'This will generate "%r" CLs. This many CLs can potentially'
275 ' generate too much load on the build infrastructure. Please'
276 ' email infra-dev@chromium.org to ensure that this won\'t break'
277 ' anything. The infra team reserves the right to cancel your'
278 ' jobs if they are overloading the CQ.' % num_cls)
279 answer = gclient_utils.AskForData('Proceed? (y/n):')
280 if answer.lower() != 'y':
281 return 0
Francois Dorayd42c6812017-05-30 15:10:20 -0400282
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000283 cls_per_reviewer = collections.defaultdict(int)
284 for cl_index, (reviewers, cl_info) in \
285 enumerate(files_split_by_reviewers.items(), 1):
286 # Convert reviewers from tuple to set.
287 reviewer_set = set(reviewers)
288 if dry_run:
289 file_paths = [f for _, f in cl_info.files]
290 PrintClInfo(cl_index, num_cls, cl_info.owners_directories,
291 file_paths, description, reviewer_set,
292 enable_auto_submit, topic)
293 else:
294 UploadCl(refactor_branch, refactor_branch_upstream,
295 cl_info.owners_directories, cl_info.files, description,
296 comment, reviewer_set, changelist, cmd_upload,
297 cq_dry_run, enable_auto_submit, topic, repository_root)
Francois Dorayd42c6812017-05-30 15:10:20 -0400298
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000299 for reviewer in reviewers:
300 cls_per_reviewer[reviewer] += 1
Anne Redullab5509952023-07-27 01:27:02 +0000301
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000302 # List the top reviewers that will be sent the most CLs as a result of
303 # the split.
304 reviewer_rankings = sorted(cls_per_reviewer.items(),
305 key=lambda item: item[1],
306 reverse=True)
307 print('The top reviewers are:')
308 for reviewer, count in reviewer_rankings[:CL_SPLIT_TOP_REVIEWERS]:
309 print(f' {reviewer}: {count} CLs')
Anne Redullab5509952023-07-27 01:27:02 +0000310
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000311 # Go back to the original branch.
312 git.run('checkout', refactor_branch)
Francois Dorayd42c6812017-05-30 15:10:20 -0400313
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000314 except subprocess2.CalledProcessError as cpe:
315 sys.stderr.write(cpe.stderr)
316 return 1
317 return 0
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000318
319
Peter Kotwiczcaeef7b2023-08-24 02:34:52 +0000320def CheckDescriptionBugLink(description):
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000321 """Verifies that the description contains a bug link.
Peter Kotwiczcaeef7b2023-08-24 02:34:52 +0000322
323 Examples:
324 Bug: 123
325 Bug: chromium:456
326
327 Prompts user if the description does not contain a bug link.
328 """
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000329 bug_pattern = re.compile(r"^Bug:\s*(?:[a-zA-Z]+:)?[0-9]+", re.MULTILINE)
330 matches = re.findall(bug_pattern, description)
331 answer = 'y'
332 if not matches:
333 answer = gclient_utils.AskForData(
334 'Description does not include a bug link. Proceed? (y/n):')
335 return answer.lower() == 'y'
Peter Kotwiczcaeef7b2023-08-24 02:34:52 +0000336
337
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000338def SelectReviewersForFiles(cl, author, files, max_depth):
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000339 """Selects reviewers for passed-in files
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000340
341 Args:
342 cl: Changelist class instance
343 author: Email of person running 'git cl split'
344 files: List of files
345 max_depth: The maximum directory depth to search for OWNERS files. A value
346 less than 1 means no limit.
347 """
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000348 info_split_by_owners = GetFilesSplitByOwners(files, max_depth)
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000349
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000350 info_split_by_reviewers = {}
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000351
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000352 for (directory, split_files) in info_split_by_owners.items():
353 # Use '/' as a path separator in the branch name and the CL description
354 # and comment.
355 directory = directory.replace(os.path.sep, '/')
356 file_paths = [f for _, f in split_files]
357 # Convert reviewers list to tuple in order to use reviewers as key to
358 # dictionary.
359 reviewers = tuple(
360 cl.owners_client.SuggestOwners(
361 file_paths, exclude=[author, cl.owners_client.EVERYONE]))
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000362
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000363 if not reviewers in info_split_by_reviewers:
364 info_split_by_reviewers[reviewers] = FilesAndOwnersDirectory([], [])
365 info_split_by_reviewers[reviewers].files.extend(split_files)
366 info_split_by_reviewers[reviewers].owners_directories.append(directory)
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000367
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000368 return info_split_by_reviewers