blob: 65dede163e3486a11fda8c90163d5997ea0790e9 [file] [log] [blame]
Francois Dorayd42c6812017-05-30 15:10:20 -04001#!/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 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
19import owners
20import owners_finder
Edward Lesmes17ffd982020-03-31 17:33:16 +000021import scm
Francois Dorayd42c6812017-05-30 15:10:20 -040022
23import git_common as git
24
25
Stephen Martinisf53f82c2018-09-07 20:58:05 +000026# 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.
30CL_SPLIT_FORCE_LIMIT = 10
31
32
Francois Dorayd42c6812017-05-30 15:10:20 -040033def ReadFile(file_path):
34 """Returns the content of |file_path|."""
35 with open(file_path) as f:
36 content = f.read()
37 return content
38
39
40def EnsureInGitRepository():
41 """Throws an exception if the current directory is not a git repository."""
42 git.run('rev-parse')
43
44
Edward Lemurac5c55f2020-02-29 00:17:16 +000045def CreateBranchForDirectory(prefix, directory, upstream):
46 """Creates a branch named |prefix| + "_" + |directory| + "_split".
Francois Dorayd42c6812017-05-30 15:10:20 -040047
48 Return false if the branch already exists. |upstream| is used as upstream for
49 the created branch.
50 """
51 existing_branches = set(git.branches(use_limit = False))
Edward Lemurac5c55f2020-02-29 00:17:16 +000052 branch_name = prefix + '_' + directory + '_split'
Francois Dorayd42c6812017-05-30 15:10:20 -040053 if branch_name in existing_branches:
54 return False
55 git.run('checkout', '-t', upstream, '-b', branch_name)
56 return True
57
58
Edward Lemurac5c55f2020-02-29 00:17:16 +000059def FormatDescriptionOrComment(txt, directory):
60 """Replaces $directory with |directory| in |txt|."""
61 return txt.replace('$directory', '/' + directory)
Francois Dorayd42c6812017-05-30 15:10:20 -040062
63
64def AddUploadedByGitClSplitToDescription(description):
65 """Adds a 'This CL was uploaded by git cl split.' line to |description|.
66
67 The line is added before footers, or at the end of |description| if it has no
68 footers.
69 """
70 split_footers = git_footers.split_footers(description)
71 lines = split_footers[0]
72 if not lines[-1] or lines[-1].isspace():
73 lines = lines + ['']
74 lines = lines + ['This CL was uploaded by git cl split.']
75 if split_footers[1]:
76 lines += [''] + split_footers[1]
77 return '\n'.join(lines)
78
79
Edward Lemurac5c55f2020-02-29 00:17:16 +000080def UploadCl(refactor_branch, refactor_branch_upstream, directory, files,
81 description, comment, reviewers, changelist, cmd_upload,
Edward Lemur2c62b332020-03-12 22:12:33 +000082 cq_dry_run, enable_auto_submit, repository_root):
Francois Dorayd42c6812017-05-30 15:10:20 -040083 """Uploads a CL with all changes to |files| in |refactor_branch|.
84
85 Args:
86 refactor_branch: Name of the branch that contains the changes to upload.
87 refactor_branch_upstream: Name of the upstream of |refactor_branch|.
88 directory: Path to the directory that contains the OWNERS file for which
89 to upload a CL.
90 files: List of AffectedFile instances to include in the uploaded CL.
Francois Dorayd42c6812017-05-30 15:10:20 -040091 description: Description of the uploaded CL.
92 comment: Comment to post on the uploaded CL.
Edward Lemurac5c55f2020-02-29 00:17:16 +000093 reviewers: A set of reviewers for the CL.
Francois Dorayd42c6812017-05-30 15:10:20 -040094 changelist: The Changelist class.
95 cmd_upload: The function associated with the git cl upload command.
Stephen Martiniscb326682018-08-29 21:06:30 +000096 cq_dry_run: If CL uploads should also do a cq dry run.
Takuto Ikuta51eca592019-02-14 19:40:52 +000097 enable_auto_submit: If CL uploads should also enable auto submit.
Francois Dorayd42c6812017-05-30 15:10:20 -040098 """
Francois Dorayd42c6812017-05-30 15:10:20 -040099 # Create a branch.
Edward Lemurac5c55f2020-02-29 00:17:16 +0000100 if not CreateBranchForDirectory(
101 refactor_branch, directory, refactor_branch_upstream):
102 print('Skipping ' + directory + ' for which a branch already exists.')
Francois Dorayd42c6812017-05-30 15:10:20 -0400103 return
104
105 # Checkout all changes to files in |files|.
Edward Lemur2c62b332020-03-12 22:12:33 +0000106 deleted_files = []
107 modified_files = []
108 for action, f in files:
109 abspath = os.path.abspath(os.path.join(repository_root, f))
110 if action == 'D':
111 deleted_files.append(abspath)
112 else:
113 modified_files.append(abspath)
114
Francois Dorayd42c6812017-05-30 15:10:20 -0400115 if deleted_files:
116 git.run(*['rm'] + deleted_files)
Francois Dorayd42c6812017-05-30 15:10:20 -0400117 if modified_files:
118 git.run(*['checkout', refactor_branch, '--'] + modified_files)
119
120 # Commit changes. The temporary file is created with delete=False so that it
121 # can be deleted manually after git has read it rather than automatically
122 # when it is closed.
Edward Lemur1773f372020-02-22 00:27:14 +0000123 with gclient_utils.temporary_file() as tmp_file:
124 gclient_utils.FileWrite(
Edward Lemurac5c55f2020-02-29 00:17:16 +0000125 tmp_file, FormatDescriptionOrComment(description, directory))
Edward Lemur1773f372020-02-22 00:27:14 +0000126 git.run('commit', '-F', tmp_file)
Francois Dorayd42c6812017-05-30 15:10:20 -0400127
128 # Upload a CL.
Anthony Politoc08c71b2020-08-26 23:45:30 +0000129 upload_args = ['-f']
130 if reviewers:
131 upload_args.extend(['-r', ','.join(reviewers)])
Stephen Martiniscb326682018-08-29 21:06:30 +0000132 if cq_dry_run:
133 upload_args.append('--cq-dry-run')
Francois Dorayd42c6812017-05-30 15:10:20 -0400134 if not comment:
Aaron Gablee5adf612017-07-14 10:43:58 -0700135 upload_args.append('--send-mail')
Takuto Ikuta51eca592019-02-14 19:40:52 +0000136 if enable_auto_submit:
137 upload_args.append('--enable-auto-submit')
Raul Tambre80ee78e2019-05-06 22:41:05 +0000138 print('Uploading CL for ' + directory + '.')
Francois Dorayd42c6812017-05-30 15:10:20 -0400139 cmd_upload(upload_args)
140 if comment:
Edward Lemurac5c55f2020-02-29 00:17:16 +0000141 changelist().AddComment(FormatDescriptionOrComment(comment, directory),
142 publish=True)
Francois Dorayd42c6812017-05-30 15:10:20 -0400143
144
Edward Lemurac5c55f2020-02-29 00:17:16 +0000145def GetFilesSplitByOwners(owners_database, files):
Francois Dorayd42c6812017-05-30 15:10:20 -0400146 """Returns a map of files split by OWNERS file.
147
148 Returns:
149 A map where keys are paths to directories containing an OWNERS file and
150 values are lists of files sharing an OWNERS file.
151 """
Edward Lemurac5c55f2020-02-29 00:17:16 +0000152 files_split_by_owners = collections.defaultdict(list)
Edward Lesmes17ffd982020-03-31 17:33:16 +0000153 for action, path in files:
154 enclosing_dir = owners_database.enclosing_dir_with_owners(path)
155 files_split_by_owners[enclosing_dir].append((action, path))
Edward Lemurac5c55f2020-02-29 00:17:16 +0000156 return files_split_by_owners
Francois Dorayd42c6812017-05-30 15:10:20 -0400157
158
Chris Watkinsba28e462017-12-13 11:22:17 +1100159def PrintClInfo(cl_index, num_cls, directory, file_paths, description,
Edward Lemurac5c55f2020-02-29 00:17:16 +0000160 reviewers):
Chris Watkinsba28e462017-12-13 11:22:17 +1100161 """Prints info about a CL.
162
163 Args:
164 cl_index: The index of this CL in the list of CLs to upload.
165 num_cls: The total number of CLs that will be uploaded.
166 directory: Path to the directory that contains the OWNERS file for which
167 to upload a CL.
168 file_paths: A list of files in this CL.
169 description: The CL description.
Edward Lemurac5c55f2020-02-29 00:17:16 +0000170 reviewers: A set of reviewers for this CL.
Chris Watkinsba28e462017-12-13 11:22:17 +1100171 """
Edward Lemurac5c55f2020-02-29 00:17:16 +0000172 description_lines = FormatDescriptionOrComment(description,
173 directory).splitlines()
Chris Watkinsba28e462017-12-13 11:22:17 +1100174 indented_description = '\n'.join([' ' + l for l in description_lines])
175
Raul Tambre80ee78e2019-05-06 22:41:05 +0000176 print('CL {}/{}'.format(cl_index, num_cls))
177 print('Path: {}'.format(directory))
Edward Lemurac5c55f2020-02-29 00:17:16 +0000178 print('Reviewers: {}'.format(', '.join(reviewers)))
Raul Tambre80ee78e2019-05-06 22:41:05 +0000179 print('\n' + indented_description + '\n')
180 print('\n'.join(file_paths))
181 print()
Chris Watkinsba28e462017-12-13 11:22:17 +1100182
183
Stephen Martiniscb326682018-08-29 21:06:30 +0000184def SplitCl(description_file, comment_file, changelist, cmd_upload, dry_run,
Edward Lemur2c62b332020-03-12 22:12:33 +0000185 cq_dry_run, enable_auto_submit, repository_root):
Francois Dorayd42c6812017-05-30 15:10:20 -0400186 """"Splits a branch into smaller branches and uploads CLs.
187
188 Args:
189 description_file: File containing the description of uploaded CLs.
190 comment_file: File containing the comment of uploaded CLs.
191 changelist: The Changelist class.
192 cmd_upload: The function associated with the git cl upload command.
Chris Watkinsba28e462017-12-13 11:22:17 +1100193 dry_run: Whether this is a dry run (no branches or CLs created).
Stephen Martiniscb326682018-08-29 21:06:30 +0000194 cq_dry_run: If CL uploads should also do a cq dry run.
Takuto Ikuta51eca592019-02-14 19:40:52 +0000195 enable_auto_submit: If CL uploads should also enable auto submit.
Francois Dorayd42c6812017-05-30 15:10:20 -0400196
197 Returns:
198 0 in case of success. 1 in case of error.
199 """
200 description = AddUploadedByGitClSplitToDescription(ReadFile(description_file))
201 comment = ReadFile(comment_file) if comment_file else None
202
203 try:
Chris Watkinsba28e462017-12-13 11:22:17 +1100204 EnsureInGitRepository()
Francois Dorayd42c6812017-05-30 15:10:20 -0400205
206 cl = changelist()
Edward Lemur2c62b332020-03-12 22:12:33 +0000207 upstream = cl.GetCommonAncestorWithUpstream()
208 files = [
209 (action.strip(), f)
210 for action, f in scm.GIT.CaptureStatus(repository_root, upstream)
211 ]
Francois Dorayd42c6812017-05-30 15:10:20 -0400212
213 if not files:
Raul Tambre80ee78e2019-05-06 22:41:05 +0000214 print('Cannot split an empty CL.')
Francois Dorayd42c6812017-05-30 15:10:20 -0400215 return 1
216
217 author = git.run('config', 'user.email').strip() or None
218 refactor_branch = git.current_branch()
Gabriel Charette09baacd2017-11-09 13:30:41 -0500219 assert refactor_branch, "Can't run from detached branch."
Francois Dorayd42c6812017-05-30 15:10:20 -0400220 refactor_branch_upstream = git.upstream(refactor_branch)
Gabriel Charette09baacd2017-11-09 13:30:41 -0500221 assert refactor_branch_upstream, \
222 "Branch %s must have an upstream." % refactor_branch
Francois Dorayd42c6812017-05-30 15:10:20 -0400223
Edward Lemur2c62b332020-03-12 22:12:33 +0000224 owners_database = owners.Database(repository_root, open, os.path)
225 owners_database.load_data_needed_for([f for _, f in files])
Francois Dorayd42c6812017-05-30 15:10:20 -0400226
Edward Lemurac5c55f2020-02-29 00:17:16 +0000227 files_split_by_owners = GetFilesSplitByOwners(owners_database, files)
Francois Dorayd42c6812017-05-30 15:10:20 -0400228
Edward Lemurac5c55f2020-02-29 00:17:16 +0000229 num_cls = len(files_split_by_owners)
230 print('Will split current branch (' + refactor_branch + ') into ' +
231 str(num_cls) + ' CLs.\n')
Stephen Martinisf53f82c2018-09-07 20:58:05 +0000232 if cq_dry_run and num_cls > CL_SPLIT_FORCE_LIMIT:
Raul Tambre80ee78e2019-05-06 22:41:05 +0000233 print(
Stephen Martiniscb326682018-08-29 21:06:30 +0000234 'This will generate "%r" CLs. This many CLs can potentially generate'
235 ' too much load on the build infrastructure. Please email'
236 ' infra-dev@chromium.org to ensure that this won\'t break anything.'
237 ' The infra team reserves the right to cancel your jobs if they are'
Raul Tambre80ee78e2019-05-06 22:41:05 +0000238 ' overloading the CQ.' % num_cls)
Edward Lesmesae3586b2020-03-23 21:21:14 +0000239 answer = gclient_utils.AskForData('Proceed? (y/n):')
Stephen Martiniscb326682018-08-29 21:06:30 +0000240 if answer.lower() != 'y':
241 return 0
Francois Dorayd42c6812017-05-30 15:10:20 -0400242
Edward Lemurac5c55f2020-02-29 00:17:16 +0000243 for cl_index, (directory, files) in \
244 enumerate(files_split_by_owners.items(), 1):
Francois Dorayd42c6812017-05-30 15:10:20 -0400245 # Use '/' as a path separator in the branch name and the CL description
246 # and comment.
Edward Lemurac5c55f2020-02-29 00:17:16 +0000247 directory = directory.replace(os.path.sep, '/')
Edward Lemur2c62b332020-03-12 22:12:33 +0000248 file_paths = [f for _, f in files]
Edward Lemurac5c55f2020-02-29 00:17:16 +0000249 reviewers = owners_database.reviewers_for(file_paths, author)
Anthony Politoc08c71b2020-08-26 23:45:30 +0000250 reviewers.discard(owners.ANYONE)
Chris Watkinsba28e462017-12-13 11:22:17 +1100251 if dry_run:
252 PrintClInfo(cl_index, num_cls, directory, file_paths, description,
Edward Lemurac5c55f2020-02-29 00:17:16 +0000253 reviewers)
Chris Watkinsba28e462017-12-13 11:22:17 +1100254 else:
Edward Lemurac5c55f2020-02-29 00:17:16 +0000255 UploadCl(refactor_branch, refactor_branch_upstream, directory, files,
256 description, comment, reviewers, changelist, cmd_upload,
Edward Lemur2c62b332020-03-12 22:12:33 +0000257 cq_dry_run, enable_auto_submit, repository_root)
Francois Dorayd42c6812017-05-30 15:10:20 -0400258
259 # Go back to the original branch.
260 git.run('checkout', refactor_branch)
261
262 except subprocess2.CalledProcessError as cpe:
263 sys.stderr.write(cpe.stderr)
264 return 1
265 return 0