Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2014 the V8 project authors. All rights reserved. |
| 3 | # Redistribution and use in source and binary forms, with or without |
| 4 | # modification, are permitted provided that the following conditions are |
| 5 | # met: |
| 6 | # |
| 7 | # * Redistributions of source code must retain the above copyright |
| 8 | # notice, this list of conditions and the following disclaimer. |
| 9 | # * Redistributions in binary form must reproduce the above |
| 10 | # copyright notice, this list of conditions and the following |
| 11 | # disclaimer in the documentation and/or other materials provided |
| 12 | # with the distribution. |
| 13 | # * Neither the name of Google Inc. nor the names of its |
| 14 | # contributors may be used to endorse or promote products derived |
| 15 | # from this software without specific prior written permission. |
| 16 | # |
| 17 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 21 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 23 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | |
| 29 | import argparse |
| 30 | from collections import OrderedDict |
| 31 | import sys |
| 32 | |
| 33 | from common_includes import * |
Rubin Xu | 2894c6a | 2019-02-07 16:01:35 +0000 | [diff] [blame] | 34 | from git_recipes import GetCommitMessageFooterMap |
Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 35 | |
| 36 | def IsSvnNumber(rev): |
| 37 | return rev.isdigit() and len(rev) < 8 |
| 38 | |
| 39 | class Preparation(Step): |
| 40 | MESSAGE = "Preparation." |
| 41 | |
| 42 | def RunStep(self): |
| 43 | if os.path.exists(self.Config("ALREADY_MERGING_SENTINEL_FILE")): |
| 44 | if self._options.force: |
| 45 | os.remove(self.Config("ALREADY_MERGING_SENTINEL_FILE")) |
| 46 | elif self._options.step == 0: # pragma: no cover |
| 47 | self.Die("A merge is already in progress") |
| 48 | open(self.Config("ALREADY_MERGING_SENTINEL_FILE"), "a").close() |
| 49 | |
| 50 | self.InitialEnvironmentChecks(self.default_cwd) |
Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 51 | |
| 52 | self["merge_to_branch"] = self._options.branch |
Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 53 | |
| 54 | self.CommonPrepare() |
| 55 | self.PrepareBranch() |
| 56 | |
| 57 | |
| 58 | class CreateBranch(Step): |
| 59 | MESSAGE = "Create a fresh branch for the patch." |
| 60 | |
| 61 | def RunStep(self): |
| 62 | self.GitCreateBranch(self.Config("BRANCHNAME"), |
| 63 | self.vc.RemoteBranch(self["merge_to_branch"])) |
| 64 | |
| 65 | |
| 66 | class SearchArchitecturePorts(Step): |
| 67 | MESSAGE = "Search for corresponding architecture ports." |
| 68 | |
| 69 | def RunStep(self): |
| 70 | self["full_revision_list"] = list(OrderedDict.fromkeys( |
| 71 | self._options.revisions)) |
| 72 | port_revision_list = [] |
| 73 | for revision in self["full_revision_list"]: |
| 74 | # Search for commits which matches the "Port XXX" pattern. |
| 75 | git_hashes = self.GitLog(reverse=True, format="%H", |
Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 76 | grep="^[Pp]ort %s" % revision, |
Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 77 | branch=self.vc.RemoteMasterBranch()) |
| 78 | for git_hash in git_hashes.splitlines(): |
| 79 | revision_title = self.GitLog(n=1, format="%s", git_hash=git_hash) |
| 80 | |
| 81 | # Is this revision included in the original revision list? |
| 82 | if git_hash in self["full_revision_list"]: |
| 83 | print("Found port of %s -> %s (already included): %s" |
| 84 | % (revision, git_hash, revision_title)) |
| 85 | else: |
| 86 | print("Found port of %s -> %s: %s" |
| 87 | % (revision, git_hash, revision_title)) |
| 88 | port_revision_list.append(git_hash) |
| 89 | |
| 90 | # Do we find any port? |
| 91 | if len(port_revision_list) > 0: |
| 92 | if self.Confirm("Automatically add corresponding ports (%s)?" |
| 93 | % ", ".join(port_revision_list)): |
| 94 | #: 'y': Add ports to revision list. |
| 95 | self["full_revision_list"].extend(port_revision_list) |
| 96 | |
| 97 | |
| 98 | class CreateCommitMessage(Step): |
| 99 | MESSAGE = "Create commit message." |
| 100 | |
Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 101 | def _create_commit_description(self, commit_hash): |
| 102 | patch_merge_desc = self.GitLog(n=1, format="%s", git_hash=commit_hash) |
| 103 | description = "Merged: " + patch_merge_desc + "\n" |
| 104 | description += "Revision: " + commit_hash + "\n\n" |
| 105 | return description |
| 106 | |
Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 107 | def RunStep(self): |
| 108 | |
| 109 | # Stringify: ["abcde", "12345"] -> "abcde, 12345" |
| 110 | self["revision_list"] = ", ".join(self["full_revision_list"]) |
| 111 | |
| 112 | if not self["revision_list"]: # pragma: no cover |
| 113 | self.Die("Revision list is empty.") |
| 114 | |
Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 115 | msg_pieces = [] |
Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 116 | |
Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 117 | if len(self["full_revision_list"]) > 1: |
| 118 | self["commit_title"] = "Merged: Squashed multiple commits." |
| 119 | for commit_hash in self["full_revision_list"]: |
| 120 | msg_pieces.append(self._create_commit_description(commit_hash)) |
| 121 | else: |
| 122 | commit_hash = self["full_revision_list"][0] |
| 123 | full_description = self._create_commit_description(commit_hash).split("\n") |
Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 124 | |
Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 125 | #Truncate title because of code review tool |
| 126 | title = full_description[0] |
| 127 | if len(title) > 100: |
| 128 | title = title[:96] + " ..." |
| 129 | |
| 130 | self["commit_title"] = title |
| 131 | msg_pieces.append(full_description[1] + "\n\n") |
Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 132 | |
| 133 | bugs = [] |
| 134 | for commit_hash in self["full_revision_list"]: |
| 135 | msg = self.GitLog(n=1, git_hash=commit_hash) |
| 136 | for bug in re.findall(r"^[ \t]*BUG[ \t]*=[ \t]*(.*?)[ \t]*$", msg, re.M): |
| 137 | bugs.extend(s.strip() for s in bug.split(",")) |
Rubin Xu | 2894c6a | 2019-02-07 16:01:35 +0000 | [diff] [blame] | 138 | gerrit_bug = GetCommitMessageFooterMap(msg).get('Bug', '') |
| 139 | bugs.extend(s.strip() for s in gerrit_bug.split(",")) |
| 140 | bug_aggregate = ",".join( |
| 141 | sorted(filter(lambda s: s and s != "none", set(bugs)))) |
Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 142 | if bug_aggregate: |
Rubin Xu | 2894c6a | 2019-02-07 16:01:35 +0000 | [diff] [blame] | 143 | # TODO(machenbach): Use proper gerrit footer for bug after switch to |
| 144 | # gerrit. Keep BUG= for now for backwards-compatibility. |
Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 145 | msg_pieces.append("BUG=%s\nLOG=N\n" % bug_aggregate) |
| 146 | |
Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 147 | msg_pieces.append("NOTRY=true\nNOPRESUBMIT=true\nNOTREECHECKS=true\n") |
| 148 | |
Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 149 | self["new_commit_msg"] = "".join(msg_pieces) |
| 150 | |
| 151 | |
| 152 | class ApplyPatches(Step): |
| 153 | MESSAGE = "Apply patches for selected revisions." |
| 154 | |
| 155 | def RunStep(self): |
| 156 | for commit_hash in self["full_revision_list"]: |
| 157 | print("Applying patch for %s to %s..." |
| 158 | % (commit_hash, self["merge_to_branch"])) |
| 159 | patch = self.GitGetPatch(commit_hash) |
| 160 | TextToFile(patch, self.Config("TEMPORARY_PATCH_FILE")) |
| 161 | self.ApplyPatch(self.Config("TEMPORARY_PATCH_FILE")) |
| 162 | if self._options.patch: |
| 163 | self.ApplyPatch(self._options.patch) |
| 164 | |
Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 165 | class CommitLocal(Step): |
| 166 | MESSAGE = "Commit to local branch." |
| 167 | |
| 168 | def RunStep(self): |
| 169 | # Add a commit message title. |
Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 170 | self["new_commit_msg"] = "%s\n\n%s" % (self["commit_title"], |
| 171 | self["new_commit_msg"]) |
| 172 | TextToFile(self["new_commit_msg"], self.Config("COMMITMSG_FILE")) |
| 173 | self.GitCommit(file_name=self.Config("COMMITMSG_FILE")) |
| 174 | |
Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 175 | class CommitRepository(Step): |
| 176 | MESSAGE = "Commit to the repository." |
| 177 | |
| 178 | def RunStep(self): |
| 179 | self.GitCheckout(self.Config("BRANCHNAME")) |
| 180 | self.WaitForLGTM() |
| 181 | self.GitPresubmit() |
| 182 | self.vc.CLLand() |
| 183 | |
Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 184 | class CleanUp(Step): |
| 185 | MESSAGE = "Cleanup." |
| 186 | |
| 187 | def RunStep(self): |
| 188 | self.CommonCleanup() |
| 189 | print "*** SUMMARY ***" |
Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 190 | print "branch: %s" % self["merge_to_branch"] |
| 191 | if self["revision_list"]: |
| 192 | print "patches: %s" % self["revision_list"] |
| 193 | |
| 194 | |
| 195 | class MergeToBranch(ScriptsBase): |
| 196 | def _Description(self): |
| 197 | return ("Performs the necessary steps to merge revisions from " |
Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 198 | "master to release branches like 4.5. This script does not " |
| 199 | "version the commit. See http://goo.gl/9ke2Vw for more " |
| 200 | "information.") |
Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 201 | |
| 202 | def _PrepareOptions(self, parser): |
| 203 | group = parser.add_mutually_exclusive_group(required=True) |
| 204 | group.add_argument("--branch", help="The branch to merge to.") |
| 205 | parser.add_argument("revisions", nargs="*", |
| 206 | help="The revisions to merge.") |
| 207 | parser.add_argument("-f", "--force", |
| 208 | help="Delete sentinel file.", |
| 209 | default=False, action="store_true") |
| 210 | parser.add_argument("-m", "--message", |
| 211 | help="A commit message for the patch.") |
| 212 | parser.add_argument("-p", "--patch", |
| 213 | help="A patch file to apply as part of the merge.") |
| 214 | |
| 215 | def _ProcessOptions(self, options): |
| 216 | if len(options.revisions) < 1: |
| 217 | if not options.patch: |
| 218 | print "Either a patch file or revision numbers must be specified" |
| 219 | return False |
| 220 | if not options.message: |
| 221 | print "You must specify a merge comment if no patches are specified" |
| 222 | return False |
| 223 | options.bypass_upload_hooks = True |
| 224 | # CC ulan to make sure that fixes are merged to Google3. |
| 225 | options.cc = "[email protected]" |
| 226 | |
Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 227 | if len(options.branch.split('.')) > 2: |
| 228 | print ("This script does not support merging to roll branches. " |
| 229 | "Please use tools/release/roll_merge.py for this use case.") |
| 230 | return False |
| 231 | |
Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 232 | # Make sure to use git hashes in the new workflows. |
| 233 | for revision in options.revisions: |
| 234 | if (IsSvnNumber(revision) or |
| 235 | (revision[0:1] == "r" and IsSvnNumber(revision[1:]))): |
| 236 | print "Please provide full git hashes of the patches to merge." |
| 237 | print "Got: %s" % revision |
| 238 | return False |
| 239 | return True |
| 240 | |
| 241 | def _Config(self): |
| 242 | return { |
| 243 | "BRANCHNAME": "prepare-merge", |
Rubin Xu | 2894c6a | 2019-02-07 16:01:35 +0000 | [diff] [blame] | 244 | "PERSISTFILE_BASENAME": RELEASE_WORKDIR + "v8-merge-to-branch-tempfile", |
Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 245 | "ALREADY_MERGING_SENTINEL_FILE": |
Rubin Xu | 2894c6a | 2019-02-07 16:01:35 +0000 | [diff] [blame] | 246 | RELEASE_WORKDIR + "v8-merge-to-branch-tempfile-already-merging", |
| 247 | "TEMPORARY_PATCH_FILE": |
| 248 | RELEASE_WORKDIR + "v8-prepare-merge-tempfile-temporary-patch", |
| 249 | "COMMITMSG_FILE": RELEASE_WORKDIR + "v8-prepare-merge-tempfile-commitmsg", |
Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | def _Steps(self): |
| 253 | return [ |
| 254 | Preparation, |
| 255 | CreateBranch, |
| 256 | SearchArchitecturePorts, |
| 257 | CreateCommitMessage, |
| 258 | ApplyPatches, |
Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 259 | CommitLocal, |
| 260 | UploadStep, |
| 261 | CommitRepository, |
Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 262 | CleanUp, |
| 263 | ] |
| 264 | |
| 265 | |
| 266 | if __name__ == "__main__": # pragma: no cover |
| 267 | sys.exit(MergeToBranch().Run()) |