blob: ed34a14d01f752434c05d8399585028432fc44a4 [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.
5
6"""Splits a branch into smaller branches and uploads CLs."""
7
Raul Tambre80ee78e2019-05-06 22:41:05 +00008from __future__ import print_function
9
Francois Dorayd42c6812017-05-30 15:10:20 -040010import collections
11import os
12import re
13import subprocess2
14import sys
15import tempfile
16
Edward Lemur1773f372020-02-22 00:27:14 +000017import gclient_utils
Francois Dorayd42c6812017-05-30 15:10:20 -040018import git_footers
Josip Sokcevic7958e302023-03-01 23:02:21 +000019import scm
Francois Dorayd42c6812017-05-30 15:10:20 -040020
21import git_common as git
22
23
Stephen Martinisf53f82c2018-09-07 20:58:05 +000024# If a call to `git cl split` will generate more than this number of CLs, the
25# command will prompt the user to make sure they know what they're doing. Large
26# numbers of CLs generated by `git cl split` have caused infrastructure issues
27# in the past.
28CL_SPLIT_FORCE_LIMIT = 10
29
Anne Redullab5509952023-07-27 01:27:02 +000030# The maximum number of top reviewers to list. `git cl split` may send many CLs
31# to a single reviewer, so the top reviewers with the most CLs sent to them
32# will be listed.
33CL_SPLIT_TOP_REVIEWERS = 5
34
Peter Kotwicz70d971a2023-08-01 22:26:14 +000035FilesAndOwnersDirectory = collections.namedtuple("FilesAndOwnersDirectory",
36 "files owners_directories")
37
Stephen Martinisf53f82c2018-09-07 20:58:05 +000038
Francois Dorayd42c6812017-05-30 15:10:20 -040039def EnsureInGitRepository():
40 """Throws an exception if the current directory is not a git repository."""
41 git.run('rev-parse')
42
43
Peter Kotwicz70d971a2023-08-01 22:26:14 +000044def CreateBranchForDirectories(prefix, directories, upstream):
45 """Creates a branch named |prefix| + "_" + |directories[0]| + "_split".
Francois Dorayd42c6812017-05-30 15:10:20 -040046
47 Return false if the branch already exists. |upstream| is used as upstream for
48 the created branch.
49 """
50 existing_branches = set(git.branches(use_limit = False))
Peter Kotwicz70d971a2023-08-01 22:26:14 +000051 branch_name = prefix + '_' + directories[0] + '_split'
Francois Dorayd42c6812017-05-30 15:10:20 -040052 if branch_name in existing_branches:
53 return False
54 git.run('checkout', '-t', upstream, '-b', branch_name)
55 return True
56
57
Peter Kotwicz70d971a2023-08-01 22:26:14 +000058def FormatDirectoriesForPrinting(directories, prefix=None):
59 """Formats directory list for printing
60
61 Uses dedicated format for single-item list."""
62
63 prefixed = directories
64 if prefix:
65 prefixed = [(prefix + d) for d in directories]
66
67 return str(prefixed) if len(prefixed) > 1 else str(prefixed[0])
68
69
70def FormatDescriptionOrComment(txt, directories):
71 """Replaces $directory with |directories| in |txt|."""
72 to_insert = FormatDirectoriesForPrinting(directories, prefix='/')
73 return txt.replace('$directory', to_insert)
Francois Dorayd42c6812017-05-30 15:10:20 -040074
75
76def AddUploadedByGitClSplitToDescription(description):
77 """Adds a 'This CL was uploaded by git cl split.' line to |description|.
78
79 The line is added before footers, or at the end of |description| if it has no
80 footers.
81 """
82 split_footers = git_footers.split_footers(description)
83 lines = split_footers[0]
Song Fangzhen534f5052021-06-23 08:51:34 +000084 if lines[-1] and not lines[-1].isspace():
Francois Dorayd42c6812017-05-30 15:10:20 -040085 lines = lines + ['']
86 lines = lines + ['This CL was uploaded by git cl split.']
87 if split_footers[1]:
88 lines += [''] + split_footers[1]
89 return '\n'.join(lines)
90
91
Peter Kotwicz70d971a2023-08-01 22:26:14 +000092def UploadCl(refactor_branch, refactor_branch_upstream, directories, files,
Edward Lemurac5c55f2020-02-29 00:17:16 +000093 description, comment, reviewers, changelist, cmd_upload,
Rachael Newitt03e49122023-06-28 21:39:21 +000094 cq_dry_run, enable_auto_submit, topic, repository_root):
Francois Dorayd42c6812017-05-30 15:10:20 -040095 """Uploads a CL with all changes to |files| in |refactor_branch|.
96
97 Args:
98 refactor_branch: Name of the branch that contains the changes to upload.
99 refactor_branch_upstream: Name of the upstream of |refactor_branch|.
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000100 directories: Paths to the directories that contain the OWNERS files for
101 which to upload a CL.
Francois Dorayd42c6812017-05-30 15:10:20 -0400102 files: List of AffectedFile instances to include in the uploaded CL.
Francois Dorayd42c6812017-05-30 15:10:20 -0400103 description: Description of the uploaded CL.
104 comment: Comment to post on the uploaded CL.
Edward Lemurac5c55f2020-02-29 00:17:16 +0000105 reviewers: A set of reviewers for the CL.
Francois Dorayd42c6812017-05-30 15:10:20 -0400106 changelist: The Changelist class.
107 cmd_upload: The function associated with the git cl upload command.
Stephen Martiniscb326682018-08-29 21:06:30 +0000108 cq_dry_run: If CL uploads should also do a cq dry run.
Takuto Ikuta51eca592019-02-14 19:40:52 +0000109 enable_auto_submit: If CL uploads should also enable auto submit.
Rachael Newitt03e49122023-06-28 21:39:21 +0000110 topic: Topic to associate with uploaded CLs.
Francois Dorayd42c6812017-05-30 15:10:20 -0400111 """
Francois Dorayd42c6812017-05-30 15:10:20 -0400112 # Create a branch.
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000113 if not CreateBranchForDirectories(refactor_branch, directories,
114 refactor_branch_upstream):
115 print('Skipping ' + FormatDirectoriesForPrinting(directories) +
116 ' for which a branch already exists.')
Francois Dorayd42c6812017-05-30 15:10:20 -0400117 return
118
119 # Checkout all changes to files in |files|.
Edward Lemur2c62b332020-03-12 22:12:33 +0000120 deleted_files = []
121 modified_files = []
122 for action, f in files:
123 abspath = os.path.abspath(os.path.join(repository_root, f))
124 if action == 'D':
125 deleted_files.append(abspath)
126 else:
127 modified_files.append(abspath)
128
Francois Dorayd42c6812017-05-30 15:10:20 -0400129 if deleted_files:
130 git.run(*['rm'] + deleted_files)
Francois Dorayd42c6812017-05-30 15:10:20 -0400131 if modified_files:
132 git.run(*['checkout', refactor_branch, '--'] + modified_files)
133
134 # Commit changes. The temporary file is created with delete=False so that it
135 # can be deleted manually after git has read it rather than automatically
136 # when it is closed.
Edward Lemur1773f372020-02-22 00:27:14 +0000137 with gclient_utils.temporary_file() as tmp_file:
138 gclient_utils.FileWrite(
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000139 tmp_file, FormatDescriptionOrComment(description, directories))
Edward Lemur1773f372020-02-22 00:27:14 +0000140 git.run('commit', '-F', tmp_file)
Francois Dorayd42c6812017-05-30 15:10:20 -0400141
142 # Upload a CL.
Anthony Politoc08c71b2020-08-26 23:45:30 +0000143 upload_args = ['-f']
144 if reviewers:
145 upload_args.extend(['-r', ','.join(reviewers)])
Stephen Martiniscb326682018-08-29 21:06:30 +0000146 if cq_dry_run:
147 upload_args.append('--cq-dry-run')
Francois Dorayd42c6812017-05-30 15:10:20 -0400148 if not comment:
Aaron Gablee5adf612017-07-14 10:43:58 -0700149 upload_args.append('--send-mail')
Takuto Ikuta51eca592019-02-14 19:40:52 +0000150 if enable_auto_submit:
151 upload_args.append('--enable-auto-submit')
Rachael Newitt03e49122023-06-28 21:39:21 +0000152 if topic:
153 upload_args.append('--topic={}'.format(topic))
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000154 print('Uploading CL for ' + FormatDirectoriesForPrinting(directories) + '...')
Olivier Li06145912021-05-12 23:59:24 +0000155
156 ret = cmd_upload(upload_args)
157 if ret != 0:
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000158 print('Uploading failed for ' + FormatDirectoriesForPrinting(directories) +
159 '.')
Olivier Li06145912021-05-12 23:59:24 +0000160 print('Note: git cl split has built-in resume capabilities.')
161 print('Delete ' + git.current_branch() +
162 ' then run git cl split again to resume uploading.')
163
Francois Dorayd42c6812017-05-30 15:10:20 -0400164 if comment:
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000165 changelist().AddComment(FormatDescriptionOrComment(comment, directories),
Edward Lemurac5c55f2020-02-29 00:17:16 +0000166 publish=True)
Francois Dorayd42c6812017-05-30 15:10:20 -0400167
168
Daniel Cheng403c44e2022-10-05 22:24:58 +0000169def GetFilesSplitByOwners(files, max_depth):
Francois Dorayd42c6812017-05-30 15:10:20 -0400170 """Returns a map of files split by OWNERS file.
171
172 Returns:
173 A map where keys are paths to directories containing an OWNERS file and
174 values are lists of files sharing an OWNERS file.
175 """
Edward Lesmesb1174d72021-02-02 20:31:34 +0000176 files_split_by_owners = {}
Edward Lesmes17ffd982020-03-31 17:33:16 +0000177 for action, path in files:
Daniel Cheng403c44e2022-10-05 22:24:58 +0000178 # normpath() is important to normalize separators here, in prepration for
179 # str.split() before. It would be nicer to use something like pathlib here
180 # but alas...
181 dir_with_owners = os.path.normpath(os.path.dirname(path))
182 if max_depth >= 1:
183 dir_with_owners = os.path.join(
184 *dir_with_owners.split(os.path.sep)[:max_depth])
Edward Lesmesb1174d72021-02-02 20:31:34 +0000185 # Find the closest parent directory with an OWNERS file.
186 while (dir_with_owners not in files_split_by_owners
187 and not os.path.isfile(os.path.join(dir_with_owners, 'OWNERS'))):
188 dir_with_owners = os.path.dirname(dir_with_owners)
189 files_split_by_owners.setdefault(dir_with_owners, []).append((action, path))
Edward Lemurac5c55f2020-02-29 00:17:16 +0000190 return files_split_by_owners
Francois Dorayd42c6812017-05-30 15:10:20 -0400191
192
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000193def PrintClInfo(cl_index, num_cls, directories, file_paths, description,
Anne Redulla072d06e2023-07-06 23:12:16 +0000194 reviewers, enable_auto_submit, topic):
Chris Watkinsba28e462017-12-13 11:22:17 +1100195 """Prints info about a CL.
196
197 Args:
198 cl_index: The index of this CL in the list of CLs to upload.
199 num_cls: The total number of CLs that will be uploaded.
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000200 directories: Paths to directories that contains the OWNERS files for which
Chris Watkinsba28e462017-12-13 11:22:17 +1100201 to upload a CL.
202 file_paths: A list of files in this CL.
203 description: The CL description.
Edward Lemurac5c55f2020-02-29 00:17:16 +0000204 reviewers: A set of reviewers for this CL.
Anne Redulla072d06e2023-07-06 23:12:16 +0000205 enable_auto_submit: If the CL should also have auto submit enabled.
Rachael Newitt03e49122023-06-28 21:39:21 +0000206 topic: Topic to set for this CL.
Chris Watkinsba28e462017-12-13 11:22:17 +1100207 """
Edward Lemurac5c55f2020-02-29 00:17:16 +0000208 description_lines = FormatDescriptionOrComment(description,
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000209 directories).splitlines()
Chris Watkinsba28e462017-12-13 11:22:17 +1100210 indented_description = '\n'.join([' ' + l for l in description_lines])
211
Raul Tambre80ee78e2019-05-06 22:41:05 +0000212 print('CL {}/{}'.format(cl_index, num_cls))
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000213 print('Paths: {}'.format(FormatDirectoriesForPrinting(directories)))
Edward Lemurac5c55f2020-02-29 00:17:16 +0000214 print('Reviewers: {}'.format(', '.join(reviewers)))
Anne Redulla072d06e2023-07-06 23:12:16 +0000215 print('Auto-Submit: {}'.format(enable_auto_submit))
Rachael Newitt03e49122023-06-28 21:39:21 +0000216 print('Topic: {}'.format(topic))
Raul Tambre80ee78e2019-05-06 22:41:05 +0000217 print('\n' + indented_description + '\n')
218 print('\n'.join(file_paths))
219 print()
Chris Watkinsba28e462017-12-13 11:22:17 +1100220
221
Stephen Martiniscb326682018-08-29 21:06:30 +0000222def SplitCl(description_file, comment_file, changelist, cmd_upload, dry_run,
Rachael Newitt03e49122023-06-28 21:39:21 +0000223 cq_dry_run, enable_auto_submit, max_depth, topic, repository_root):
Francois Dorayd42c6812017-05-30 15:10:20 -0400224 """"Splits a branch into smaller branches and uploads CLs.
225
226 Args:
227 description_file: File containing the description of uploaded CLs.
228 comment_file: File containing the comment of uploaded CLs.
229 changelist: The Changelist class.
230 cmd_upload: The function associated with the git cl upload command.
Chris Watkinsba28e462017-12-13 11:22:17 +1100231 dry_run: Whether this is a dry run (no branches or CLs created).
Stephen Martiniscb326682018-08-29 21:06:30 +0000232 cq_dry_run: If CL uploads should also do a cq dry run.
Takuto Ikuta51eca592019-02-14 19:40:52 +0000233 enable_auto_submit: If CL uploads should also enable auto submit.
Daniel Cheng403c44e2022-10-05 22:24:58 +0000234 max_depth: The maximum directory depth to search for OWNERS files. A value
235 less than 1 means no limit.
Rachael Newitt03e49122023-06-28 21:39:21 +0000236 topic: Topic to associate with split CLs.
Francois Dorayd42c6812017-05-30 15:10:20 -0400237
238 Returns:
239 0 in case of success. 1 in case of error.
240 """
Edward Lesmesb1174d72021-02-02 20:31:34 +0000241 description = AddUploadedByGitClSplitToDescription(
242 gclient_utils.FileRead(description_file))
243 comment = gclient_utils.FileRead(comment_file) if comment_file else None
Francois Dorayd42c6812017-05-30 15:10:20 -0400244
245 try:
Chris Watkinsba28e462017-12-13 11:22:17 +1100246 EnsureInGitRepository()
Francois Dorayd42c6812017-05-30 15:10:20 -0400247
248 cl = changelist()
Edward Lemur2c62b332020-03-12 22:12:33 +0000249 upstream = cl.GetCommonAncestorWithUpstream()
250 files = [
251 (action.strip(), f)
252 for action, f in scm.GIT.CaptureStatus(repository_root, upstream)
253 ]
Francois Dorayd42c6812017-05-30 15:10:20 -0400254
255 if not files:
Raul Tambre80ee78e2019-05-06 22:41:05 +0000256 print('Cannot split an empty CL.')
Francois Dorayd42c6812017-05-30 15:10:20 -0400257 return 1
258
259 author = git.run('config', 'user.email').strip() or None
260 refactor_branch = git.current_branch()
Gabriel Charette09baacd2017-11-09 13:30:41 -0500261 assert refactor_branch, "Can't run from detached branch."
Francois Dorayd42c6812017-05-30 15:10:20 -0400262 refactor_branch_upstream = git.upstream(refactor_branch)
Gabriel Charette09baacd2017-11-09 13:30:41 -0500263 assert refactor_branch_upstream, \
264 "Branch %s must have an upstream." % refactor_branch
Francois Dorayd42c6812017-05-30 15:10:20 -0400265
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000266 # Verify that the description contains a bug link. Examples:
267 # Bug: 123
268 # Bug: chromium:456
269 bug_pattern = re.compile(r"^Bug:\s*(?:[a-zA-Z]+:)?[0-9]+", re.MULTILINE)
270 matches = re.findall(bug_pattern, description)
271 answer = 'y'
272 if not matches:
273 answer = gclient_utils.AskForData(
274 'Description does not include a bug link. Proceed? (y/n):')
275 if answer.lower() != 'y':
276 return 0
Francois Dorayd42c6812017-05-30 15:10:20 -0400277
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000278 files_split_by_reviewers = SelectReviewersForFiles(cl, author, files,
279 max_depth)
280
281 num_cls = len(files_split_by_reviewers)
Edward Lemurac5c55f2020-02-29 00:17:16 +0000282 print('Will split current branch (' + refactor_branch + ') into ' +
283 str(num_cls) + ' CLs.\n')
Stephen Martinisf53f82c2018-09-07 20:58:05 +0000284 if cq_dry_run and num_cls > CL_SPLIT_FORCE_LIMIT:
Raul Tambre80ee78e2019-05-06 22:41:05 +0000285 print(
Stephen Martiniscb326682018-08-29 21:06:30 +0000286 'This will generate "%r" CLs. This many CLs can potentially generate'
287 ' too much load on the build infrastructure. Please email'
288 ' infra-dev@chromium.org to ensure that this won\'t break anything.'
289 ' The infra team reserves the right to cancel your jobs if they are'
Raul Tambre80ee78e2019-05-06 22:41:05 +0000290 ' overloading the CQ.' % num_cls)
Edward Lesmesae3586b2020-03-23 21:21:14 +0000291 answer = gclient_utils.AskForData('Proceed? (y/n):')
Stephen Martiniscb326682018-08-29 21:06:30 +0000292 if answer.lower() != 'y':
293 return 0
Francois Dorayd42c6812017-05-30 15:10:20 -0400294
Anne Redullab5509952023-07-27 01:27:02 +0000295 cls_per_reviewer = collections.defaultdict(int)
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000296 for cl_index, (reviewers, cl_info) in \
297 enumerate(files_split_by_reviewers.items(), 1):
298 # Convert reviewers from tuple to set.
299 reviewer_set = set(reviewers)
Chris Watkinsba28e462017-12-13 11:22:17 +1100300 if dry_run:
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000301 file_paths = [f for _, f in cl_info.files]
302 PrintClInfo(cl_index, num_cls, cl_info.owners_directories, file_paths,
303 description, reviewer_set, enable_auto_submit, topic)
Chris Watkinsba28e462017-12-13 11:22:17 +1100304 else:
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000305 UploadCl(refactor_branch, refactor_branch_upstream,
306 cl_info.owners_directories, cl_info.files, description,
307 comment, reviewer_set, changelist, cmd_upload, cq_dry_run,
308 enable_auto_submit, topic, repository_root)
Francois Dorayd42c6812017-05-30 15:10:20 -0400309
Anne Redullab5509952023-07-27 01:27:02 +0000310 for reviewer in reviewers:
311 cls_per_reviewer[reviewer] += 1
312
313 # List the top reviewers that will be sent the most CLs as a result of the
314 # split.
315 reviewer_rankings = sorted(cls_per_reviewer.items(),
316 key=lambda item: item[1],
317 reverse=True)
318 print('The top reviewers are:')
319 for reviewer, count in reviewer_rankings[:CL_SPLIT_TOP_REVIEWERS]:
320 print(f' {reviewer}: {count} CLs')
321
Francois Dorayd42c6812017-05-30 15:10:20 -0400322 # Go back to the original branch.
323 git.run('checkout', refactor_branch)
324
325 except subprocess2.CalledProcessError as cpe:
326 sys.stderr.write(cpe.stderr)
327 return 1
328 return 0
Peter Kotwicz70d971a2023-08-01 22:26:14 +0000329
330
331def SelectReviewersForFiles(cl, author, files, max_depth):
332 """Selects reviewers for passed-in files
333
334 Args:
335 cl: Changelist class instance
336 author: Email of person running 'git cl split'
337 files: List of files
338 max_depth: The maximum directory depth to search for OWNERS files. A value
339 less than 1 means no limit.
340 """
341 info_split_by_owners = GetFilesSplitByOwners(files, max_depth)
342
343 info_split_by_reviewers = {}
344
345 for (directory, split_files) in info_split_by_owners.items():
346 # Use '/' as a path separator in the branch name and the CL description
347 # and comment.
348 directory = directory.replace(os.path.sep, '/')
349 file_paths = [f for _, f in split_files]
350 # Convert reviewers list to tuple in order to use reviewers as key to
351 # dictionary.
352 reviewers = tuple(
353 cl.owners_client.SuggestOwners(
354 file_paths, exclude=[author, cl.owners_client.EVERYONE]))
355
356 if not reviewers in info_split_by_reviewers:
357 info_split_by_reviewers[reviewers] = FilesAndOwnersDirectory([], [])
358 info_split_by_reviewers[reviewers].files.extend(split_files)
359 info_split_by_reviewers[reviewers].owners_directories.append(directory)
360
361 return info_split_by_reviewers