Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame^] | 1 | # Copyright 2012 The ChromiumOS Authors |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Mike Frysinger | 61ef29a | 2014-12-17 02:05:27 -0500 | [diff] [blame] | 5 | """Developer helper tool for merging CLs from ToT to branches. |
| 6 | |
| 7 | This simple program takes changes from gerrit/gerrit-int and creates new |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 8 | changes for them on the desired branch using your gerrit/ssh credentials. To |
| 9 | specify a change on gerrit-int, you must prefix the change with a *. |
| 10 | |
| 11 | Note that this script is best used from within an existing checkout of |
| 12 | Chromium OS that already has the changes you want merged to the branch in it |
| 13 | i.e. if you want to push changes to crosutils.git, you must have src/scripts |
| 14 | checked out. If this isn't true e.g. you are running this script from a |
| 15 | minilayout or trying to upload an internal change from a non internal checkout, |
| 16 | you must specify some extra options: use the --nomirror option and use -e to |
| 17 | specify your email address. This tool will then checkout the git repo fresh |
| 18 | using the credentials for the -e/email you specified and upload the change. Note |
| 19 | you can always use this method but it's slower than the "mirrored" method and |
| 20 | requires more typing :(. |
| 21 | |
| 22 | Examples: |
| 23 | cros_merge_to_branch 32027 32030 32031 release-R22.2723.B |
| 24 | |
| 25 | This will create changes for 32027, 32030 and 32031 on the R22 branch. To look |
| 26 | up the name of a branch, go into a git sub-dir and type 'git branch -a' and the |
| 27 | find the branch you want to merge to. If you want to upload internal changes |
| 28 | from gerrit-int, you must prefix the gerrit change number with a * e.g. |
| 29 | |
| 30 | cros_merge_to_branch *26108 release-R22.2723.B |
| 31 | |
| 32 | For more information on how to do this yourself you can go here: |
Mike Frysinger | dcad4e0 | 2018-08-03 16:20:02 -0400 | [diff] [blame] | 33 | https://dev.chromium.org/chromium-os/how-tos-and-troubleshooting/working-on-a-br\ |
Mike Frysinger | 02e1e07 | 2013-11-10 22:11:34 -0500 | [diff] [blame] | 34 | anch |
| 35 | """ |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 36 | |
Mike Frysinger | 6fc4176 | 2013-02-27 16:36:54 -0500 | [diff] [blame] | 37 | import errno |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 38 | import logging |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 39 | import os |
Mike Frysinger | 187af41 | 2012-10-27 04:27:04 -0400 | [diff] [blame] | 40 | import re |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 41 | import shutil |
| 42 | import sys |
| 43 | import tempfile |
| 44 | |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 45 | from chromite.lib import commandline |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 46 | from chromite.lib import constants |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 47 | from chromite.lib import cros_build_lib |
Brian Harring | 511055e | 2012-10-10 02:58:59 -0700 | [diff] [blame] | 48 | from chromite.lib import gerrit |
David James | 97d9587 | 2012-11-16 15:09:56 -0800 | [diff] [blame] | 49 | from chromite.lib import git |
Brian Harring | 511055e | 2012-10-10 02:58:59 -0700 | [diff] [blame] | 50 | from chromite.lib import patch as cros_patch |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 51 | |
| 52 | |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 53 | def _GetParser(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 54 | """Returns the parser to use for this module.""" |
| 55 | parser = commandline.ArgumentParser(description=__doc__) |
| 56 | parser.add_argument( |
| 57 | "-d", |
| 58 | "--draft", |
| 59 | default=False, |
| 60 | action="store_true", |
| 61 | help="upload a draft to Gerrit rather than a change", |
| 62 | ) |
| 63 | parser.add_argument( |
| 64 | "-n", |
| 65 | "--dry-run", |
| 66 | default=False, |
| 67 | action="store_true", |
| 68 | dest="dryrun", |
| 69 | help="apply changes locally but do not upload them", |
| 70 | ) |
| 71 | parser.add_argument( |
| 72 | "-e", |
| 73 | "--email", |
| 74 | help="use this email instead of the email you would " |
| 75 | "upload changes as; required w/--nomirror", |
| 76 | ) |
| 77 | parser.add_argument( |
| 78 | "--nomirror", |
| 79 | default=True, |
| 80 | dest="mirror", |
| 81 | action="store_false", |
| 82 | help="checkout git repo directly; requires --email", |
| 83 | ) |
| 84 | parser.add_argument( |
| 85 | "--nowipe", |
| 86 | default=True, |
| 87 | dest="wipe", |
| 88 | action="store_false", |
| 89 | help="do not wipe the work directory after finishing", |
| 90 | ) |
| 91 | parser.add_argument("change", nargs="+", help="CLs to merge") |
| 92 | parser.add_argument("branch", help="the branch to merge to") |
| 93 | return parser |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 94 | |
| 95 | |
| 96 | def _UploadChangeToBranch(work_dir, patch, branch, draft, dryrun): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 97 | """Creates a new change from GerritPatch |patch| to |branch| from |work_dir|. |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 98 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 99 | Args: |
| 100 | patch: Instance of GerritPatch to upload. |
| 101 | branch: Branch to upload to. |
| 102 | work_dir: Local directory where repository is checked out in. |
| 103 | draft: If True, upload to refs/draft/|branch| rather than refs/for/|branch|. |
| 104 | dryrun: Don't actually upload a change but go through all the steps up to |
| 105 | and including git push --dry-run. |
Mike Frysinger | 1a736a8 | 2013-12-12 01:50:59 -0500 | [diff] [blame] | 106 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 107 | Returns: |
| 108 | A list of all the gerrit URLs found. |
| 109 | """ |
| 110 | upload_type = "drafts" if draft else "for" |
| 111 | # Download & setup the patch if need be. |
| 112 | patch.Fetch(work_dir) |
| 113 | # Apply the actual change. |
| 114 | patch.CherryPick(work_dir, inflight=True, leave_dirty=True) |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 115 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 116 | # Get the new sha1 after apply. |
| 117 | new_sha1 = git.GetGitRepoRevision(work_dir) |
| 118 | reviewers = set() |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 119 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 120 | # Filter out tags that are added by gerrit and chromite. |
| 121 | filter_re = re.compile( |
| 122 | r"((Commit|Trybot)-Ready|Commit-Queue|(Reviewed|Submitted|Tested)-by): " |
| 123 | ) |
Mike Frysinger | a7a2f00 | 2015-04-29 23:23:19 -0400 | [diff] [blame] | 124 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 125 | # Rewrite the commit message all the time. Latest gerrit doesn't seem |
| 126 | # to like it when you use the same ChangeId on different branches. |
| 127 | msg = [] |
| 128 | for line in patch.commit_message.splitlines(): |
| 129 | if line.startswith("Reviewed-on: "): |
| 130 | line = "Previous-" + line |
| 131 | elif filter_re.match(line): |
| 132 | # If the tag is malformed, or the person lacks a name, |
| 133 | # then that's just too bad -- throw it away. |
| 134 | ele = re.split(r"[<>@]+", line) |
| 135 | if len(ele) == 4: |
| 136 | reviewers.add("@".join(ele[-3:-1])) |
| 137 | continue |
| 138 | msg.append(line) |
| 139 | msg += ["(cherry picked from commit %s)" % patch.sha1] |
| 140 | git.RunGit( |
| 141 | work_dir, |
| 142 | ["commit", "--amend", "-F", "-"], |
| 143 | input="\n".join(msg).encode("utf8"), |
| 144 | ) |
Mike Frysinger | 187af41 | 2012-10-27 04:27:04 -0400 | [diff] [blame] | 145 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 146 | # Get the new sha1 after rewriting the commit message. |
| 147 | new_sha1 = git.GetGitRepoRevision(work_dir) |
Mike Frysinger | 187af41 | 2012-10-27 04:27:04 -0400 | [diff] [blame] | 148 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 149 | # Create and use a LocalPatch to Upload the change to Gerrit. |
| 150 | local_patch = cros_patch.LocalPatch( |
| 151 | work_dir, |
| 152 | patch.project_url, |
| 153 | constants.PATCH_BRANCH, |
| 154 | patch.tracking_branch, |
| 155 | patch.remote, |
| 156 | new_sha1, |
| 157 | ) |
| 158 | for reviewers in (reviewers, ()): |
| 159 | try: |
| 160 | return local_patch.Upload( |
| 161 | patch.project_url, |
| 162 | "refs/%s/%s" % (upload_type, branch), |
| 163 | carbon_copy=False, |
| 164 | dryrun=dryrun, |
| 165 | reviewers=reviewers, |
| 166 | ) |
| 167 | except cros_build_lib.RunCommandError as e: |
| 168 | if e.returncode == 128 and re.search( |
| 169 | r'fatal: user ".*?" not found', e.stderr |
| 170 | ): |
| 171 | logging.warning( |
| 172 | "Some reviewers were not found (%s); " |
| 173 | "dropping them & retrying upload", |
| 174 | " ".join(reviewers), |
| 175 | ) |
| 176 | continue |
| 177 | raise |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 178 | |
| 179 | |
| 180 | def _SetupWorkDirectoryForPatch(work_dir, patch, branch, manifest, email): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 181 | """Set up local dir for uploading changes to the given patch's project.""" |
| 182 | logging.notice( |
| 183 | "Setting up dir %s for uploading changes to %s", |
| 184 | work_dir, |
| 185 | patch.project_url, |
| 186 | ) |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 187 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 188 | # Clone the git repo from reference if we have a pointer to a |
| 189 | # ManifestCheckout object. |
| 190 | reference = None |
| 191 | if manifest: |
| 192 | # Get the path to the first checkout associated with this change. Since |
| 193 | # all of the checkouts share git objects, it doesn't matter which checkout |
| 194 | # we pick. |
| 195 | path = manifest.FindCheckouts(patch.project)[0]["path"] |
David James | afa4f5f | 2013-11-20 14:12:55 -0800 | [diff] [blame] | 196 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 197 | reference = os.path.join(constants.SOURCE_ROOT, path) |
| 198 | if not os.path.isdir(reference): |
| 199 | logging.error("Unable to locate git checkout: %s", reference) |
| 200 | logging.error("Did you mean to use --nomirror?") |
| 201 | # This will do an "raise OSError" with the right values. |
| 202 | os.open(reference, os.O_DIRECTORY) |
| 203 | # Use the email if email wasn't specified. |
| 204 | if not email: |
| 205 | email = git.GetProjectUserEmail(reference) |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 206 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 207 | git.Clone(work_dir, patch.project_url, reference=reference) |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 208 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 209 | # Set the git committer. |
| 210 | git.RunGit(work_dir, ["config", "--replace-all", "user.email", email]) |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 211 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 212 | mbranch = git.MatchSingleBranchName( |
| 213 | work_dir, branch, namespace="refs/remotes/origin/" |
| 214 | ) |
| 215 | if branch != mbranch: |
| 216 | logging.notice( |
| 217 | 'Auto resolved branch name "%s" to "%s"', branch, mbranch |
| 218 | ) |
| 219 | branch = mbranch |
Mike Frysinger | 94f91be | 2012-10-19 16:09:06 -0400 | [diff] [blame] | 220 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 221 | # Finally, create a local branch for uploading changes to the given remote |
| 222 | # branch. |
| 223 | git.CreatePushBranch( |
| 224 | constants.PATCH_BRANCH, |
| 225 | work_dir, |
| 226 | sync=False, |
| 227 | remote_push_branch=git.RemoteRef("ignore", "origin/%s" % branch), |
| 228 | ) |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 229 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 230 | return branch |
Mike Frysinger | 94f91be | 2012-10-19 16:09:06 -0400 | [diff] [blame] | 231 | |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 232 | |
| 233 | def _ManifestContainsAllPatches(manifest, patches): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 234 | """Returns true if the given manifest contains all the patches. |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 235 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 236 | Args: |
| 237 | manifest: an instance of git.Manifest |
| 238 | patches: a collection of GerritPatch objects. |
| 239 | """ |
| 240 | for patch in patches: |
| 241 | if not manifest.FindCheckouts(patch.project): |
| 242 | logging.error( |
| 243 | "Your manifest does not have the repository %s for " |
| 244 | "change %s. Please re-run with --nomirror and " |
| 245 | "--email set", |
| 246 | patch.project, |
| 247 | patch.gerrit_number, |
| 248 | ) |
| 249 | return False |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 250 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 251 | return True |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 252 | |
| 253 | |
| 254 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 255 | parser = _GetParser() |
| 256 | options = parser.parse_args(argv) |
| 257 | changes = options.change |
| 258 | branch = options.branch |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 259 | |
Mike Frysinger | 6fc4176 | 2013-02-27 16:36:54 -0500 | [diff] [blame] | 260 | try: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 261 | patches = gerrit.GetGerritPatchInfo(changes) |
| 262 | except ValueError as e: |
| 263 | logging.error("Invalid patch: %s", e) |
| 264 | cros_build_lib.Die("Did you swap the branch/gerrit number?") |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 265 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 266 | # Suppress all logging info output unless we're running debug. |
| 267 | if not options.debug: |
| 268 | logging.getLogger().setLevel(logging.NOTICE) |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 269 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 270 | # Get a pointer to your repo checkout to look up the local project paths for |
| 271 | # both email addresses and for using your checkout as a git mirror. |
| 272 | manifest = None |
| 273 | if options.mirror: |
| 274 | try: |
| 275 | manifest = git.ManifestCheckout.Cached(constants.SOURCE_ROOT) |
| 276 | except OSError as e: |
| 277 | if e.errno == errno.ENOENT: |
| 278 | logging.error( |
| 279 | "Unable to locate ChromiumOS checkout: %s", |
| 280 | constants.SOURCE_ROOT, |
| 281 | ) |
| 282 | logging.error("Did you mean to use --nomirror?") |
| 283 | return 1 |
| 284 | raise |
| 285 | if not _ManifestContainsAllPatches(manifest, patches): |
| 286 | return 1 |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 287 | else: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 288 | if not options.email: |
| 289 | chromium_email = "%s@chromium.org" % os.environ["USER"] |
| 290 | logging.notice( |
| 291 | "--nomirror set without email, using %s", chromium_email |
| 292 | ) |
| 293 | options.email = chromium_email |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 294 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 295 | index = 0 |
| 296 | work_dir = None |
| 297 | root_work_dir = tempfile.mkdtemp(prefix="cros_merge_to_branch") |
| 298 | try: |
| 299 | for index, (change, patch) in enumerate(zip(changes, patches)): |
| 300 | # We only clone the project and set the committer the first time. |
| 301 | work_dir = os.path.join(root_work_dir, patch.project) |
| 302 | if not os.path.isdir(work_dir): |
| 303 | branch = _SetupWorkDirectoryForPatch( |
| 304 | work_dir, patch, branch, manifest, options.email |
| 305 | ) |
| 306 | |
| 307 | # Now that we have the project checked out, let's apply our change and |
| 308 | # create a new change on Gerrit. |
| 309 | logging.notice("Uploading change %s to branch %s", change, branch) |
| 310 | urls = _UploadChangeToBranch( |
| 311 | work_dir, patch, branch, options.draft, options.dryrun |
| 312 | ) |
| 313 | logging.notice("Successfully uploaded %s to %s", change, branch) |
| 314 | for url in urls: |
| 315 | if url.endswith("\x1b[K"): |
| 316 | # Git will often times emit these escape sequences. |
| 317 | url = url[0:-3] |
| 318 | logging.notice(" URL: %s", url) |
| 319 | |
| 320 | except ( |
| 321 | cros_build_lib.RunCommandError, |
| 322 | cros_patch.ApplyPatchException, |
| 323 | git.AmbiguousBranchName, |
| 324 | OSError, |
| 325 | ) as e: |
| 326 | # Tell the user how far we got. |
| 327 | good_changes = changes[:index] |
| 328 | bad_changes = changes[index:] |
| 329 | |
| 330 | logging.warning( |
| 331 | "############## SOME CHANGES FAILED TO UPLOAD ############" |
| 332 | ) |
| 333 | |
| 334 | if good_changes: |
| 335 | logging.notice( |
| 336 | "Successfully uploaded change(s) %s", " ".join(good_changes) |
| 337 | ) |
| 338 | |
| 339 | # Printing out the error here so that we can see exactly what failed. This |
| 340 | # is especially useful to debug without using --debug. |
| 341 | logging.error("Upload failed with %s", str(e).strip()) |
| 342 | if not options.wipe: |
| 343 | logging.error( |
| 344 | "Not wiping the directory. You can inspect the failed " |
| 345 | "change at %s; After fixing the change (if trivial) you can" |
| 346 | " try to upload the change by running:\n" |
| 347 | "git commit -a -c CHERRY_PICK_HEAD\n" |
| 348 | "git push %s HEAD:refs/for/%s", |
| 349 | work_dir, |
| 350 | patch.project_url, |
| 351 | branch, |
| 352 | ) |
| 353 | else: |
| 354 | logging.error( |
| 355 | "--nowipe not set thus deleting the work directory. If you " |
| 356 | "wish to debug this, re-run the script with change(s) " |
| 357 | "%s and --nowipe by running:\n %s %s %s --nowipe", |
| 358 | " ".join(bad_changes), |
| 359 | sys.argv[0], |
| 360 | " ".join(bad_changes), |
| 361 | branch, |
| 362 | ) |
| 363 | |
| 364 | # Suppress the stack trace if we're not debugging. |
| 365 | if options.debug: |
| 366 | raise |
| 367 | else: |
| 368 | return 1 |
| 369 | |
| 370 | finally: |
| 371 | if options.wipe: |
| 372 | shutil.rmtree(root_work_dir) |
| 373 | |
| 374 | if options.dryrun: |
| 375 | logging.notice( |
| 376 | "Success! To actually upload changes, re-run without " "--dry-run." |
| 377 | ) |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 378 | else: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 379 | logging.notice("Successfully uploaded all changes requested.") |
Chris Sosa | efc3572 | 2012-09-11 18:55:37 -0700 | [diff] [blame] | 380 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 381 | return 0 |