Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # 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 Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 10 | import collections |
| 11 | import os |
| 12 | import re |
| 13 | import subprocess2 |
| 14 | import sys |
| 15 | import tempfile |
| 16 | |
Edward Lemur | 1773f37 | 2020-02-22 00:27:14 +0000 | [diff] [blame] | 17 | import gclient_utils |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 18 | import git_footers |
| 19 | import owners |
| 20 | import owners_finder |
Edward Lesmes | 17ffd98 | 2020-03-31 17:33:16 +0000 | [diff] [blame] | 21 | import scm |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 22 | |
| 23 | import git_common as git |
| 24 | |
| 25 | |
Stephen Martinis | f53f82c | 2018-09-07 20:58:05 +0000 | [diff] [blame] | 26 | # If a call to `git cl split` will generate more than this number of CLs, the |
| 27 | # command will prompt the user to make sure they know what they're doing. Large |
| 28 | # numbers of CLs generated by `git cl split` have caused infrastructure issues |
| 29 | # in the past. |
| 30 | CL_SPLIT_FORCE_LIMIT = 10 |
| 31 | |
| 32 | |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 33 | def EnsureInGitRepository(): |
| 34 | """Throws an exception if the current directory is not a git repository.""" |
| 35 | git.run('rev-parse') |
| 36 | |
| 37 | |
Edward Lemur | ac5c55f | 2020-02-29 00:17:16 +0000 | [diff] [blame] | 38 | def CreateBranchForDirectory(prefix, directory, upstream): |
| 39 | """Creates a branch named |prefix| + "_" + |directory| + "_split". |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 40 | |
| 41 | Return false if the branch already exists. |upstream| is used as upstream for |
| 42 | the created branch. |
| 43 | """ |
| 44 | existing_branches = set(git.branches(use_limit = False)) |
Edward Lemur | ac5c55f | 2020-02-29 00:17:16 +0000 | [diff] [blame] | 45 | branch_name = prefix + '_' + directory + '_split' |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 46 | if branch_name in existing_branches: |
| 47 | return False |
| 48 | git.run('checkout', '-t', upstream, '-b', branch_name) |
| 49 | return True |
| 50 | |
| 51 | |
Edward Lemur | ac5c55f | 2020-02-29 00:17:16 +0000 | [diff] [blame] | 52 | def FormatDescriptionOrComment(txt, directory): |
| 53 | """Replaces $directory with |directory| in |txt|.""" |
| 54 | return txt.replace('$directory', '/' + directory) |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 55 | |
| 56 | |
| 57 | def AddUploadedByGitClSplitToDescription(description): |
| 58 | """Adds a 'This CL was uploaded by git cl split.' line to |description|. |
| 59 | |
| 60 | The line is added before footers, or at the end of |description| if it has no |
| 61 | footers. |
| 62 | """ |
| 63 | split_footers = git_footers.split_footers(description) |
| 64 | lines = split_footers[0] |
| 65 | if not lines[-1] or lines[-1].isspace(): |
| 66 | lines = lines + [''] |
| 67 | lines = lines + ['This CL was uploaded by git cl split.'] |
| 68 | if split_footers[1]: |
| 69 | lines += [''] + split_footers[1] |
| 70 | return '\n'.join(lines) |
| 71 | |
| 72 | |
Edward Lemur | ac5c55f | 2020-02-29 00:17:16 +0000 | [diff] [blame] | 73 | def UploadCl(refactor_branch, refactor_branch_upstream, directory, files, |
| 74 | description, comment, reviewers, changelist, cmd_upload, |
Edward Lemur | 2c62b33 | 2020-03-12 22:12:33 +0000 | [diff] [blame] | 75 | cq_dry_run, enable_auto_submit, repository_root): |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 76 | """Uploads a CL with all changes to |files| in |refactor_branch|. |
| 77 | |
| 78 | Args: |
| 79 | refactor_branch: Name of the branch that contains the changes to upload. |
| 80 | refactor_branch_upstream: Name of the upstream of |refactor_branch|. |
| 81 | directory: Path to the directory that contains the OWNERS file for which |
| 82 | to upload a CL. |
| 83 | files: List of AffectedFile instances to include in the uploaded CL. |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 84 | description: Description of the uploaded CL. |
| 85 | comment: Comment to post on the uploaded CL. |
Edward Lemur | ac5c55f | 2020-02-29 00:17:16 +0000 | [diff] [blame] | 86 | reviewers: A set of reviewers for the CL. |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 87 | changelist: The Changelist class. |
| 88 | cmd_upload: The function associated with the git cl upload command. |
Stephen Martinis | cb32668 | 2018-08-29 21:06:30 +0000 | [diff] [blame] | 89 | cq_dry_run: If CL uploads should also do a cq dry run. |
Takuto Ikuta | 51eca59 | 2019-02-14 19:40:52 +0000 | [diff] [blame] | 90 | enable_auto_submit: If CL uploads should also enable auto submit. |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 91 | """ |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 92 | # Create a branch. |
Edward Lemur | ac5c55f | 2020-02-29 00:17:16 +0000 | [diff] [blame] | 93 | if not CreateBranchForDirectory( |
| 94 | refactor_branch, directory, refactor_branch_upstream): |
| 95 | print('Skipping ' + directory + ' for which a branch already exists.') |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 96 | return |
| 97 | |
| 98 | # Checkout all changes to files in |files|. |
Edward Lemur | 2c62b33 | 2020-03-12 22:12:33 +0000 | [diff] [blame] | 99 | deleted_files = [] |
| 100 | modified_files = [] |
| 101 | for action, f in files: |
| 102 | abspath = os.path.abspath(os.path.join(repository_root, f)) |
| 103 | if action == 'D': |
| 104 | deleted_files.append(abspath) |
| 105 | else: |
| 106 | modified_files.append(abspath) |
| 107 | |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 108 | if deleted_files: |
| 109 | git.run(*['rm'] + deleted_files) |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 110 | if modified_files: |
| 111 | git.run(*['checkout', refactor_branch, '--'] + modified_files) |
| 112 | |
| 113 | # Commit changes. The temporary file is created with delete=False so that it |
| 114 | # can be deleted manually after git has read it rather than automatically |
| 115 | # when it is closed. |
Edward Lemur | 1773f37 | 2020-02-22 00:27:14 +0000 | [diff] [blame] | 116 | with gclient_utils.temporary_file() as tmp_file: |
| 117 | gclient_utils.FileWrite( |
Edward Lemur | ac5c55f | 2020-02-29 00:17:16 +0000 | [diff] [blame] | 118 | tmp_file, FormatDescriptionOrComment(description, directory)) |
Edward Lemur | 1773f37 | 2020-02-22 00:27:14 +0000 | [diff] [blame] | 119 | git.run('commit', '-F', tmp_file) |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 120 | |
| 121 | # Upload a CL. |
Anthony Polito | c08c71b | 2020-08-26 23:45:30 +0000 | [diff] [blame] | 122 | upload_args = ['-f'] |
| 123 | if reviewers: |
| 124 | upload_args.extend(['-r', ','.join(reviewers)]) |
Stephen Martinis | cb32668 | 2018-08-29 21:06:30 +0000 | [diff] [blame] | 125 | if cq_dry_run: |
| 126 | upload_args.append('--cq-dry-run') |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 127 | if not comment: |
Aaron Gable | e5adf61 | 2017-07-14 10:43:58 -0700 | [diff] [blame] | 128 | upload_args.append('--send-mail') |
Takuto Ikuta | 51eca59 | 2019-02-14 19:40:52 +0000 | [diff] [blame] | 129 | if enable_auto_submit: |
| 130 | upload_args.append('--enable-auto-submit') |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 131 | print('Uploading CL for ' + directory + '.') |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 132 | cmd_upload(upload_args) |
| 133 | if comment: |
Edward Lemur | ac5c55f | 2020-02-29 00:17:16 +0000 | [diff] [blame] | 134 | changelist().AddComment(FormatDescriptionOrComment(comment, directory), |
| 135 | publish=True) |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 136 | |
| 137 | |
Edward Lesmes | b1174d7 | 2021-02-02 20:31:34 +0000 | [diff] [blame^] | 138 | def GetFilesSplitByOwners(files): |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 139 | """Returns a map of files split by OWNERS file. |
| 140 | |
| 141 | Returns: |
| 142 | A map where keys are paths to directories containing an OWNERS file and |
| 143 | values are lists of files sharing an OWNERS file. |
| 144 | """ |
Edward Lesmes | b1174d7 | 2021-02-02 20:31:34 +0000 | [diff] [blame^] | 145 | files_split_by_owners = {} |
Edward Lesmes | 17ffd98 | 2020-03-31 17:33:16 +0000 | [diff] [blame] | 146 | for action, path in files: |
Edward Lesmes | b1174d7 | 2021-02-02 20:31:34 +0000 | [diff] [blame^] | 147 | dir_with_owners = os.path.dirname(path) |
| 148 | # Find the closest parent directory with an OWNERS file. |
| 149 | while (dir_with_owners not in files_split_by_owners |
| 150 | and not os.path.isfile(os.path.join(dir_with_owners, 'OWNERS'))): |
| 151 | dir_with_owners = os.path.dirname(dir_with_owners) |
| 152 | files_split_by_owners.setdefault(dir_with_owners, []).append((action, path)) |
Edward Lemur | ac5c55f | 2020-02-29 00:17:16 +0000 | [diff] [blame] | 153 | return files_split_by_owners |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 154 | |
| 155 | |
Chris Watkins | ba28e46 | 2017-12-13 11:22:17 +1100 | [diff] [blame] | 156 | def PrintClInfo(cl_index, num_cls, directory, file_paths, description, |
Edward Lemur | ac5c55f | 2020-02-29 00:17:16 +0000 | [diff] [blame] | 157 | reviewers): |
Chris Watkins | ba28e46 | 2017-12-13 11:22:17 +1100 | [diff] [blame] | 158 | """Prints info about a CL. |
| 159 | |
| 160 | Args: |
| 161 | cl_index: The index of this CL in the list of CLs to upload. |
| 162 | num_cls: The total number of CLs that will be uploaded. |
| 163 | directory: Path to the directory that contains the OWNERS file for which |
| 164 | to upload a CL. |
| 165 | file_paths: A list of files in this CL. |
| 166 | description: The CL description. |
Edward Lemur | ac5c55f | 2020-02-29 00:17:16 +0000 | [diff] [blame] | 167 | reviewers: A set of reviewers for this CL. |
Chris Watkins | ba28e46 | 2017-12-13 11:22:17 +1100 | [diff] [blame] | 168 | """ |
Edward Lemur | ac5c55f | 2020-02-29 00:17:16 +0000 | [diff] [blame] | 169 | description_lines = FormatDescriptionOrComment(description, |
| 170 | directory).splitlines() |
Chris Watkins | ba28e46 | 2017-12-13 11:22:17 +1100 | [diff] [blame] | 171 | indented_description = '\n'.join([' ' + l for l in description_lines]) |
| 172 | |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 173 | print('CL {}/{}'.format(cl_index, num_cls)) |
| 174 | print('Path: {}'.format(directory)) |
Edward Lemur | ac5c55f | 2020-02-29 00:17:16 +0000 | [diff] [blame] | 175 | print('Reviewers: {}'.format(', '.join(reviewers))) |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 176 | print('\n' + indented_description + '\n') |
| 177 | print('\n'.join(file_paths)) |
| 178 | print() |
Chris Watkins | ba28e46 | 2017-12-13 11:22:17 +1100 | [diff] [blame] | 179 | |
| 180 | |
Stephen Martinis | cb32668 | 2018-08-29 21:06:30 +0000 | [diff] [blame] | 181 | def SplitCl(description_file, comment_file, changelist, cmd_upload, dry_run, |
Edward Lemur | 2c62b33 | 2020-03-12 22:12:33 +0000 | [diff] [blame] | 182 | cq_dry_run, enable_auto_submit, repository_root): |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 183 | """"Splits a branch into smaller branches and uploads CLs. |
| 184 | |
| 185 | Args: |
| 186 | description_file: File containing the description of uploaded CLs. |
| 187 | comment_file: File containing the comment of uploaded CLs. |
| 188 | changelist: The Changelist class. |
| 189 | cmd_upload: The function associated with the git cl upload command. |
Chris Watkins | ba28e46 | 2017-12-13 11:22:17 +1100 | [diff] [blame] | 190 | dry_run: Whether this is a dry run (no branches or CLs created). |
Stephen Martinis | cb32668 | 2018-08-29 21:06:30 +0000 | [diff] [blame] | 191 | cq_dry_run: If CL uploads should also do a cq dry run. |
Takuto Ikuta | 51eca59 | 2019-02-14 19:40:52 +0000 | [diff] [blame] | 192 | enable_auto_submit: If CL uploads should also enable auto submit. |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 193 | |
| 194 | Returns: |
| 195 | 0 in case of success. 1 in case of error. |
| 196 | """ |
Edward Lesmes | b1174d7 | 2021-02-02 20:31:34 +0000 | [diff] [blame^] | 197 | description = AddUploadedByGitClSplitToDescription( |
| 198 | gclient_utils.FileRead(description_file)) |
| 199 | comment = gclient_utils.FileRead(comment_file) if comment_file else None |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 200 | |
| 201 | try: |
Chris Watkins | ba28e46 | 2017-12-13 11:22:17 +1100 | [diff] [blame] | 202 | EnsureInGitRepository() |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 203 | |
| 204 | cl = changelist() |
Edward Lemur | 2c62b33 | 2020-03-12 22:12:33 +0000 | [diff] [blame] | 205 | upstream = cl.GetCommonAncestorWithUpstream() |
| 206 | files = [ |
| 207 | (action.strip(), f) |
| 208 | for action, f in scm.GIT.CaptureStatus(repository_root, upstream) |
| 209 | ] |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 210 | |
| 211 | if not files: |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 212 | print('Cannot split an empty CL.') |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 213 | return 1 |
| 214 | |
| 215 | author = git.run('config', 'user.email').strip() or None |
| 216 | refactor_branch = git.current_branch() |
Gabriel Charette | 09baacd | 2017-11-09 13:30:41 -0500 | [diff] [blame] | 217 | assert refactor_branch, "Can't run from detached branch." |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 218 | refactor_branch_upstream = git.upstream(refactor_branch) |
Gabriel Charette | 09baacd | 2017-11-09 13:30:41 -0500 | [diff] [blame] | 219 | assert refactor_branch_upstream, \ |
| 220 | "Branch %s must have an upstream." % refactor_branch |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 221 | |
Edward Lemur | 2c62b33 | 2020-03-12 22:12:33 +0000 | [diff] [blame] | 222 | owners_database = owners.Database(repository_root, open, os.path) |
| 223 | owners_database.load_data_needed_for([f for _, f in files]) |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 224 | |
Edward Lesmes | b1174d7 | 2021-02-02 20:31:34 +0000 | [diff] [blame^] | 225 | files_split_by_owners = GetFilesSplitByOwners(files) |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 226 | |
Edward Lemur | ac5c55f | 2020-02-29 00:17:16 +0000 | [diff] [blame] | 227 | num_cls = len(files_split_by_owners) |
| 228 | print('Will split current branch (' + refactor_branch + ') into ' + |
| 229 | str(num_cls) + ' CLs.\n') |
Stephen Martinis | f53f82c | 2018-09-07 20:58:05 +0000 | [diff] [blame] | 230 | if cq_dry_run and num_cls > CL_SPLIT_FORCE_LIMIT: |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 231 | print( |
Stephen Martinis | cb32668 | 2018-08-29 21:06:30 +0000 | [diff] [blame] | 232 | 'This will generate "%r" CLs. This many CLs can potentially generate' |
| 233 | ' too much load on the build infrastructure. Please email' |
| 234 | ' infra-dev@chromium.org to ensure that this won\'t break anything.' |
| 235 | ' The infra team reserves the right to cancel your jobs if they are' |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 236 | ' overloading the CQ.' % num_cls) |
Edward Lesmes | ae3586b | 2020-03-23 21:21:14 +0000 | [diff] [blame] | 237 | answer = gclient_utils.AskForData('Proceed? (y/n):') |
Stephen Martinis | cb32668 | 2018-08-29 21:06:30 +0000 | [diff] [blame] | 238 | if answer.lower() != 'y': |
| 239 | return 0 |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 240 | |
Edward Lemur | ac5c55f | 2020-02-29 00:17:16 +0000 | [diff] [blame] | 241 | for cl_index, (directory, files) in \ |
| 242 | enumerate(files_split_by_owners.items(), 1): |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 243 | # Use '/' as a path separator in the branch name and the CL description |
| 244 | # and comment. |
Edward Lemur | ac5c55f | 2020-02-29 00:17:16 +0000 | [diff] [blame] | 245 | directory = directory.replace(os.path.sep, '/') |
Edward Lemur | 2c62b33 | 2020-03-12 22:12:33 +0000 | [diff] [blame] | 246 | file_paths = [f for _, f in files] |
Edward Lemur | ac5c55f | 2020-02-29 00:17:16 +0000 | [diff] [blame] | 247 | reviewers = owners_database.reviewers_for(file_paths, author) |
Anthony Polito | c08c71b | 2020-08-26 23:45:30 +0000 | [diff] [blame] | 248 | reviewers.discard(owners.ANYONE) |
Chris Watkins | ba28e46 | 2017-12-13 11:22:17 +1100 | [diff] [blame] | 249 | if dry_run: |
| 250 | PrintClInfo(cl_index, num_cls, directory, file_paths, description, |
Edward Lemur | ac5c55f | 2020-02-29 00:17:16 +0000 | [diff] [blame] | 251 | reviewers) |
Chris Watkins | ba28e46 | 2017-12-13 11:22:17 +1100 | [diff] [blame] | 252 | else: |
Edward Lemur | ac5c55f | 2020-02-29 00:17:16 +0000 | [diff] [blame] | 253 | UploadCl(refactor_branch, refactor_branch_upstream, directory, files, |
| 254 | description, comment, reviewers, changelist, cmd_upload, |
Edward Lemur | 2c62b33 | 2020-03-12 22:12:33 +0000 | [diff] [blame] | 255 | cq_dry_run, enable_auto_submit, repository_root) |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 256 | |
| 257 | # Go back to the original branch. |
| 258 | git.run('checkout', refactor_branch) |
| 259 | |
| 260 | except subprocess2.CalledProcessError as cpe: |
| 261 | sys.stderr.write(cpe.stderr) |
| 262 | return 1 |
| 263 | return 0 |