 Chromium Code Reviews
 Chromium Code Reviews Issue 163183004:
  Add merge-to-branch python port.  (Closed) 
  Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
    
  
    Issue 163183004:
  Add merge-to-branch python port.  (Closed) 
  Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge| OLD | NEW | 
|---|---|
| (Empty) | |
| 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 from common_includes import * | |
| 30 | |
| 31 ALREADY_MERGING_SENTINEL_FILE = "ALREADY_MERGING_SENTINEL_FILE" | |
| 32 COMMIT_HASHES_FILE = "COMMIT_HASHES_FILE" | |
| 33 TEMPORARY_PATCH_FILE = "TEMPORARY_PATCH_FILE" | |
| 34 | |
| 35 CONFIG = { | |
| 36 BRANCHNAME: "prepare-merge", | |
| 37 PERSISTFILE_BASENAME: "/tmp/v8-merge-to-branch-tempfile", | |
| 38 ALREADY_MERGING_SENTINEL_FILE: | |
| 39 "/tmp/v8-merge-to-branch-tempfile-already-merging", | |
| 40 TEMP_BRANCH: "prepare-merge-temporary-branch-created-by-script", | |
| 41 DOT_GIT_LOCATION: ".git", | |
| 42 VERSION_FILE: "src/version.cc", | |
| 43 TEMPORARY_PATCH_FILE: "/tmp/v8-prepare-merge-tempfile-temporary-patch", | |
| 44 COMMITMSG_FILE: "/tmp/v8-prepare-merge-tempfile-commitmsg", | |
| 45 COMMIT_HASHES_FILE: "/tmp/v8-merge-to-branch-tempfile-PATCH_COMMIT_HASHES", | |
| 46 } | |
| 47 | |
| 48 | |
| 49 class MergeToBranchOptions(CommonOptions): | |
| 50 def __init__(self, options, args): | |
| 51 super(MergeToBranchOptions, self).__init__(options, options.m) | |
| 52 self.requires_editor = True | |
| 53 self.wait_for_lgtm = True | |
| 54 self.delete_sentinel = options.f | |
| 55 self.message = options.m | |
| 56 self.revert = "--reverse" if getattr(options, "r", None) else "" | |
| 57 self.revert_bleeding_edge = getattr(options, "revert_bleeding_edge", False) | |
| 58 self.patch = getattr(options, "p", "") | |
| 59 self.args = args | |
| 60 | |
| 61 | |
| 62 class Preparation(Step): | |
| 63 MESSAGE = "Preparation." | |
| 64 | |
| 65 def RunStep(self): | |
| 66 if os.path.exists(self.Config(ALREADY_MERGING_SENTINEL_FILE)): | |
| 67 if self._options.delete_sentinel: | |
| 68 os.remove(self.Config(ALREADY_MERGING_SENTINEL_FILE)) | |
| 69 elif self._options.s == 0: | |
| 70 self.Die("A merge is already in progress") | |
| 71 open(self.Config(ALREADY_MERGING_SENTINEL_FILE), "a").close() | |
| 72 | |
| 73 self.InitialEnvironmentChecks() | |
| 74 if self._options.revert_bleeding_edge: | |
| 75 self.Persist("merge_to_branch", "bleeding_edge") | |
| 76 elif self._options.args[0]: | |
| 77 self.Persist("merge_to_branch", self._options.args[0]) | |
| 78 self._options.args = self._options.args[1:] | |
| 79 else: | |
| 80 self.Die("Please specify a branch to merge to") | |
| 81 | |
| 82 self.CommonPrepare() | |
| 83 self.PrepareBranch() | |
| 84 | |
| 85 | |
| 86 class CreateBranch(Step): | |
| 87 MESSAGE = "Create a fresh branch for the patch." | |
| 88 | |
| 89 def RunStep(self): | |
| 90 self.RestoreIfUnset("merge_to_branch") | |
| 91 args = "checkout -b %s svn/%s" % (self.Config(BRANCHNAME), | |
| 92 self._state["merge_to_branch"]) | |
| 93 if self.Git(args) is None: | |
| 94 self.die("Creating branch %s failed." % self.Config(BRANCHNAME)) | |
| 95 | |
| 96 | |
| 97 class SearchArchitecturePorts(Step): | |
| 98 MESSAGE = "Search for corresponding architecture ports." | |
| 99 | |
| 100 def RunStep(self): | |
| 101 full_revision_list = [] | |
| 102 port_revision_list = [] | |
| 103 for revision in self._options.args: | |
| 
ulan
2014/02/18 11:15:09
How about building full_revision_list from args fi
 
Michael Achenbach
2014/02/18 12:39:32
Done.
 | |
| 104 # Add the revision to the array if it isn't already added. | |
| 105 if revision not in full_revision_list: | |
| 106 full_revision_list.append(revision) | |
| 107 | |
| 108 # Search for commits which matches the "Port rXXX" pattern. | |
| 109 args = ("log svn/bleeding_edge --reverse " | |
| 110 "--format=%%H --grep=\"Port r%d\"" % int(revision)) | |
| 111 git_hashes = self.Git(args) or "" | |
| 112 for git_hash in git_hashes.strip().splitlines(): | |
| 113 args = "svn find-rev %s svn/bleeding_edge" % git_hash | |
| 114 svn_revision = self.Git(args).strip() | |
| 115 if not svn_revision: | |
| 116 self.Die("Cannot determine svn revision for %s" % git_hash) | |
| 117 if svn_revision not in full_revision_list: | |
| 118 full_revision_list.append(svn_revision) | |
| 
ulan
2014/02/18 11:15:09
See comment below, appending here can be avoided.
 
Michael Achenbach
2014/02/18 12:39:32
Done.
 | |
| 119 revision_title = self.Git("log -1 --format=%%s %s" % git_hash) | |
| 120 | |
| 121 # Is this revision included in the original revision list? | |
| 122 if svn_revision in self._options.args: | |
| 123 print("Found port of r%s -> r%s (already included): %s" | |
| 124 % (revision, svn_revision, revision_title)) | |
| 125 else: | |
| 126 print("Found port of r%s -> r%s: %s" | |
| 127 % (revision, svn_revision, revision_title)) | |
| 128 port_revision_list.append(svn_revision) | |
| 129 | |
| 130 # Do we find any port? | |
| 131 if len(port_revision_list) > 0: | |
| 132 if not self.Confirm("Automatically add corresponding ports (%s)?" | |
| 133 % ", ".join(port_revision_list)): | |
| 134 #: 'n': Restore the original revision list. | |
| 135 full_revision_list = self._options.args | |
| 
ulan
2014/02/18 11:15:09
It seems cleaner to append port_revision_list on '
 
Michael Achenbach
2014/02/18 12:39:32
Done.
 | |
| 136 self.Persist("full_revision_list", ",".join(full_revision_list)) | |
| 137 | |
| 138 | |
| 139 class FindGitRevisions(Step): | |
| 140 MESSAGE = "Find the git revisions associated with the patches." | |
| 141 | |
| 142 def RunStep(self): | |
| 143 self.RestoreIfUnset("full_revision_list") | |
| 144 self.RestoreIfUnset("merge_to_branch") | |
| 145 full_revision_list = self._state["full_revision_list"].split(",") | |
| 146 patch_commit_hashes = [] | |
| 147 for revision in full_revision_list: | |
| 148 next_hash = self.Git("svn find-rev \"r%s\" svn/bleeding_edge" % revision) | |
| 149 if not next_hash: | |
| 150 self.Die("Cannot determine git hash for r%s" % revision) | |
| 151 patch_commit_hashes.append(next_hash) | |
| 152 | |
| 153 # Stringify: [123, 234] -> "r123, r234" | |
| 154 revision_list = ", ".join(map(lambda s: "r%s" % s, full_revision_list)) | |
| 155 | |
| 156 if not revision_list: | |
| 157 self.Die("Revision list is empty.") | |
| 158 | |
| 159 if self._options.revert: | |
| 160 if not self._options.revert_bleeding_edge: | |
| 161 new_commit_msg = ("Rollback of %s in %s branch." | |
| 162 % (revision_list, self._state["merge_to_branch"])) | |
| 163 else: | |
| 164 new_commit_msg = "Revert %s ." % revision_list | |
| 165 else: | |
| 166 new_commit_msg = ("Merged %s into %s branch." | |
| 167 % (revision_list, self._state["merge_to_branch"])) | |
| 168 new_commit_msg += "\n\n" | |
| 169 | |
| 170 for commit_hash in patch_commit_hashes: | |
| 171 patch_merge_desc = self.Git("log -1 --format=%%s %s" % commit_hash) | |
| 172 new_commit_msg += "%s\n\n" % patch_merge_desc.strip() | |
| 173 | |
| 174 bugs = [] | |
| 175 for commit_hash in patch_commit_hashes: | |
| 176 msg = self.Git("log -1 %s" % commit_hash) | |
| 177 for bug in re.findall(r"^[ \t]*BUG[ \t]*=[ \t]*(.*?)[ \t]*$", msg, | |
| 178 re.M): | |
| 179 bugs.extend(map(lambda s: s.strip(), bug.split(","))) | |
| 180 bug_aggregate = ",".join(sorted(bugs)) | |
| 181 if bug_aggregate: | |
| 182 new_commit_msg += "BUG=%s\nLOG=N\n" % bug_aggregate | |
| 183 TextToFile(new_commit_msg, self.Config(COMMITMSG_FILE)) | |
| 184 self.Persist("new_commit_msg", new_commit_msg) | |
| 185 self.Persist("revision_list", revision_list) | |
| 186 self._state["patch_commit_hashes"] = patch_commit_hashes | |
| 
ulan
2014/02/18 11:15:09
Why isn't it persisted and restored like other sta
 
Michael Achenbach
2014/02/18 12:39:32
Just to be similar with the bash script. Improved
 | |
| 187 TextToFile(" ".join(patch_commit_hashes), self.Config(COMMIT_HASHES_FILE)) | |
| 188 | |
| 189 | |
| 190 class ApplyPatches(Step): | |
| 191 MESSAGE = "Apply patches for selected revisions." | |
| 192 | |
| 193 def RunStep(self): | |
| 194 self.RestoreIfUnset("merge_to_branch") | |
| 195 patch_commit_hashes = self._state.get("patch_commit_hashes") | |
| 196 if not patch_commit_hashes: | |
| 197 patch_commit_hashes = FileToText(self.Config(COMMIT_HASHES_FILE)) | |
| 198 patch_commit_hashes = patch_commit_hashes.strip().split(" ") | |
| 199 if not patch_commit_hashes and not options.patch: | |
| 200 self.Die("Variable patch_commit_hashes could not be restored.") | |
| 201 for commit_hash in patch_commit_hashes: | |
| 202 print("Applying patch for %s to %s..." | |
| 203 % (commit_hash, self._state["merge_to_branch"])) | |
| 204 patch = self.Git("log -1 -p %s" % commit_hash) | |
| 205 TextToFile(patch, self.Config(TEMPORARY_PATCH_FILE)) | |
| 206 self.ApplyPatch(self.Config(TEMPORARY_PATCH_FILE), self._options.revert) | |
| 207 if self._options.patch: | |
| 208 self.ApplyPatch(self._options.patch, self._options.revert) | |
| 209 | |
| 210 | |
| 211 class PrepareVersion(Step): | |
| 212 MESSAGE = "Prepare version file." | |
| 213 | |
| 214 def RunStep(self): | |
| 215 if self._options.revert_bleeding_edge: | |
| 216 return | |
| 217 # These version numbers are used again for creating the tag | |
| 218 self.ReadAndPersistVersion() | |
| 219 | |
| 220 | |
| 221 class IncrementVersion(Step): | |
| 222 MESSAGE = "Increment version number." | |
| 223 | |
| 224 def RunStep(self): | |
| 225 if self._options.revert_bleeding_edge: | |
| 226 return | |
| 227 self.RestoreIfUnset("patch") | |
| 228 new_patch = str(int(self._state["patch"]) + 1) | |
| 229 if self.Confirm("Automatically increment PATCH_LEVEL? (Saying 'n' will " | |
| 230 "fire up your EDITOR on %s so you can make arbitrary " | |
| 231 "changes. When you're done, save the file and exit your " | |
| 232 "EDITOR.)" % self.Config(VERSION_FILE)): | |
| 233 text = FileToText(self.Config(VERSION_FILE)) | |
| 234 text = MSub(r"(?<=#define PATCH_LEVEL)(?P<space>\s+)\d*$", | |
| 235 r"\g<space>%s" % new_patch, | |
| 236 text) | |
| 237 TextToFile(text, self.Config(VERSION_FILE)) | |
| 238 else: | |
| 239 self.Editor(self.Config(VERSION_FILE)) | |
| 240 self.ReadAndPersistVersion("new_") | |
| 241 | |
| 242 | |
| 243 class CommitLocal(Step): | |
| 244 MESSAGE = "Commit to local branch." | |
| 245 | |
| 246 def RunStep(self): | |
| 247 if self.Git("commit -a -F \"%s\"" % self.Config(COMMITMSG_FILE)) is None: | |
| 248 self.Die("'git commit -a' failed.") | |
| 249 | |
| 250 | |
| 251 class CommitRepository(Step): | |
| 252 MESSAGE = "Commit to the repository." | |
| 253 | |
| 254 def RunStep(self): | |
| 255 self.RestoreIfUnset("merge_to_branch") | |
| 256 if self.Git("checkout %s" % self.Config(BRANCHNAME)) is None: | |
| 257 self.Die("Cannot ensure that the current branch is %s" | |
| 258 % self.Config(BRANCHNAME)) | |
| 259 self.WaitForLGTM() | |
| 260 if self.Git("cl presubmit", "PRESUBMIT_TREE_CHECK=\"skip\"") is None: | |
| 261 self.Die("Presubmit failed.") | |
| 262 | |
| 263 if self.Git("cl dcommit -f --bypass-hooks", | |
| 264 retry_on=lambda x: x is None) is None: | |
| 265 self.Die("Failed to commit to %s" % self._status["merge_to_branch"]) | |
| 266 | |
| 267 | |
| 268 class PrepareSVN(Step): | |
| 269 MESSAGE = "Determine svn commit revision." | |
| 270 | |
| 271 def RunStep(self): | |
| 272 if self._options.revert_bleeding_edge: | |
| 273 return | |
| 274 self.RestoreIfUnset("new_commit_msg") | |
| 275 self.RestoreIfUnset("merge_to_branch") | |
| 276 if self.Git("svn fetch") is None: | |
| 277 self.Die("'git svn fetch' failed.") | |
| 278 args = ("log -1 --format=%%H --grep=\"%s\" svn/%s" | |
| 279 % (self._state["new_commit_msg"], self._state["merge_to_branch"])) | |
| 280 commit_hash = self.Git(args).strip() | |
| 281 if not commit_hash: | |
| 282 self.Die("Unable to map git commit to svn revision.") | |
| 283 svn_revision = self.Git("svn find-rev %s" % commit_hash).strip() | |
| 284 print "subversion revision number is r%s" % svn_revision | |
| 285 self.Persist("svn_revision", svn_revision) | |
| 286 | |
| 287 | |
| 288 class TagRevision(Step): | |
| 289 MESSAGE = "Create the tag." | |
| 290 | |
| 291 def RunStep(self): | |
| 292 if self._options.revert_bleeding_edge: | |
| 293 return | |
| 294 self.RestoreVersionIfUnset("new_") | |
| 295 self.RestoreIfUnset("svn_revision") | |
| 296 self.RestoreIfUnset("merge_to_branch") | |
| 297 ver = "%s.%s.%s.%s" % (self._state["new_major"], | |
| 298 self._state["new_minor"], | |
| 299 self._state["new_build"], | |
| 300 self._state["new_patch"]) | |
| 301 print "Creating tag svn/tags/%s" % ver | |
| 302 if self._state["merge_to_branch"] == "trunk": | |
| 303 to_url = "trunk" | |
| 304 else: | |
| 305 to_url = "branches/%s" % self._state["merge_to_branch"] | |
| 306 self.SVN("copy -r %s https://v8.googlecode.com/svn/%s " | |
| 307 "https://v8.googlecode.com/svn/tags/%s -m " | |
| 308 "\"Tagging version %s\"" | |
| 309 % (self._state["svn_revision"], to_url, ver, ver)) | |
| 310 self.Persist("to_url", to_url) | |
| 311 | |
| 312 | |
| 313 class CleanUp(Step): | |
| 314 MESSAGE = "Cleanup." | |
| 315 | |
| 316 def RunStep(self): | |
| 317 self.RestoreIfUnset("svn_revision") | |
| 318 self.RestoreIfUnset("to_url") | |
| 319 self.RestoreIfUnset("revision_list") | |
| 320 self.RestoreVersionIfUnset("new_") | |
| 321 ver = "%s.%s.%s.%s" % (self._state["new_major"], | |
| 322 self._state["new_minor"], | |
| 323 self._state["new_build"], | |
| 324 self._state["new_patch"]) | |
| 325 self.CommonCleanup() | |
| 326 if not self._options.revert_bleeding_edge: | |
| 327 print "*** SUMMARY ***" | |
| 328 print "version: %s" % ver | |
| 329 print "branch: %s" % self._state["to_url"] | |
| 330 print "svn revision: %s" % self._state["svn_revision"] | |
| 331 if self._state["revision_list"]: | |
| 332 print "patches: %s" % self._state["revision_list"] | |
| 333 | |
| 334 | |
| 335 def RunMergeToBranch(config, | |
| 336 options, | |
| 337 side_effect_handler=DEFAULT_SIDE_EFFECT_HANDLER): | |
| 338 step_classes = [ | |
| 339 Preparation, | |
| 340 CreateBranch, | |
| 341 SearchArchitecturePorts, | |
| 342 FindGitRevisions, | |
| 343 ApplyPatches, | |
| 344 PrepareVersion, | |
| 345 IncrementVersion, | |
| 346 CommitLocal, | |
| 347 UploadStep, | |
| 348 CommitRepository, | |
| 349 PrepareSVN, | |
| 350 TagRevision, | |
| 351 CleanUp, | |
| 352 ] | |
| 353 | |
| 354 RunScript(step_classes, config, options, side_effect_handler) | |
| 355 | |
| 356 | |
| 357 def BuildOptions(): | |
| 358 result = optparse.OptionParser() | |
| 359 result.add_option("-f", | |
| 360 help="Delete sentinel file.", | |
| 361 default=False, action="store_true") | |
| 362 result.add_option("-m", "--message", | |
| 363 help="Specify a commit message for the patch.") | |
| 364 result.add_option("-r", "--revert", | |
| 365 help="Revert specified patches.", | |
| 366 default=False, action="store_true") | |
| 367 result.add_option("-R", "--revert-bleeding-edge", | |
| 368 help="Revert specified patches from bleeding edge.", | |
| 369 default=False, action="store_true") | |
| 370 result.add_option("-p", "--patch", dest="p", | |
| 371 help="Specify a patch file to apply as part of the merge.") | |
| 372 result.add_option("-s", "--step", dest="s", | |
| 373 help="Specify the step where to start work. Default: 0.", | |
| 374 default=0, type="int") | |
| 375 return result | |
| 376 | |
| 377 | |
| 378 def ProcessOptions(options, args): | |
| 379 revert_from_bleeding_edge = 1 if options.revert_bleeding_edge else 0 | |
| 380 min_exp_args = 2 - revert_from_bleeding_edge | |
| 381 if len(args) < min_exp_args: | |
| 382 if not options.p: | |
| 383 print "Either a patch file or revision numbers must be specified" | |
| 384 return False | |
| 385 if not options.message: | |
| 386 print "You must specify a merge comment if no patches are specified" | |
| 387 return False | |
| 388 if options.s < 0: | |
| 389 print "Bad step number %d" % options.s | |
| 390 return False | |
| 391 return True | |
| 392 | |
| 393 | |
| 394 def Main(): | |
| 395 parser = BuildOptions() | |
| 396 (options, args) = parser.parse_args() | |
| 397 if not ProcessOptions(options, args): | |
| 398 parser.print_help() | |
| 399 return 1 | |
| 400 RunMergeToBranch(CONFIG, MergeToBranchOptions(options)) | |
| 401 | |
| 402 if __name__ == "__main__": | |
| 403 sys.exit(Main()) | |
| OLD | NEW |