merge_to_branch.py 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#!/usr/bin/env python
# Copyright 2014 the V8 project authors. All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
#       copyright notice, this list of conditions and the following
#       disclaimer in the documentation and/or other materials provided
#       with the distribution.
#     * Neither the name of Google Inc. nor the names of its
#       contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

29
import argparse
30
from collections import OrderedDict
31
import sys
32 33 34

from common_includes import *

35 36 37
def IsSvnNumber(rev):
  return rev.isdigit() and len(rev) < 8

38 39 40 41
class Preparation(Step):
  MESSAGE = "Preparation."

  def RunStep(self):
42
    if os.path.exists(self.Config("ALREADY_MERGING_SENTINEL_FILE")):
43
      if self._options.force:
44
        os.remove(self.Config("ALREADY_MERGING_SENTINEL_FILE"))
45
      elif self._options.step == 0:  # pragma: no cover
46
        self.Die("A merge is already in progress")
47
    open(self.Config("ALREADY_MERGING_SENTINEL_FILE"), "a").close()
48

49
    self.InitialEnvironmentChecks(self.default_cwd)
50
    if self._options.branch:
51
      self["merge_to_branch"] = self._options.branch
52
    else:  # pragma: no cover
53 54 55 56 57 58 59 60 61 62
      self.Die("Please specify a branch to merge to")

    self.CommonPrepare()
    self.PrepareBranch()


class CreateBranch(Step):
  MESSAGE = "Create a fresh branch for the patch."

  def RunStep(self):
63
    self.GitCreateBranch(self.Config("BRANCHNAME"),
64
                         self.vc.RemoteBranch(self["merge_to_branch"]))
65 66 67 68 69 70


class SearchArchitecturePorts(Step):
  MESSAGE = "Search for corresponding architecture ports."

  def RunStep(self):
71 72
    self["full_revision_list"] = list(OrderedDict.fromkeys(
        self._options.revisions))
73
    port_revision_list = []
74
    for revision in self["full_revision_list"]:
75
      # Search for commits which matches the "Port XXX" pattern.
76
      git_hashes = self.GitLog(reverse=True, format="%H",
77
                               grep="Port %s" % revision,
78
                               branch=self.vc.RemoteMasterBranch())
79 80
      for git_hash in git_hashes.splitlines():
        revision_title = self.GitLog(n=1, format="%s", git_hash=git_hash)
81 82

        # Is this revision included in the original revision list?
83 84 85
        if git_hash in self["full_revision_list"]:
          print("Found port of %s -> %s (already included): %s"
                % (revision, git_hash, revision_title))
86
        else:
87 88 89
          print("Found port of %s -> %s: %s"
                % (revision, git_hash, revision_title))
          port_revision_list.append(git_hash)
90 91 92 93 94 95

    # Do we find any port?
    if len(port_revision_list) > 0:
      if self.Confirm("Automatically add corresponding ports (%s)?"
                      % ", ".join(port_revision_list)):
        #: 'y': Add ports to revision list.
96
        self["full_revision_list"].extend(port_revision_list)
97 98


99 100
class CreateCommitMessage(Step):
  MESSAGE = "Create commit message."
101 102 103

  def RunStep(self):

104 105
    # Stringify: ["abcde", "12345"] -> "abcde, 12345"
    self["revision_list"] = ", ".join(self["full_revision_list"])
106

107
    if not self["revision_list"]:  # pragma: no cover
108 109
      self.Die("Revision list is empty.")

110
    action_text = "Merged %s"
111

112
    # The commit message title is added below after the version is specified.
113 114 115 116
    msg_pieces = [
      "\n".join(action_text % s for s in self["full_revision_list"]),
    ]
    msg_pieces.append("\n\n")
117

118
    for commit_hash in self["full_revision_list"]:
119
      patch_merge_desc = self.GitLog(n=1, format="%s", git_hash=commit_hash)
120
      msg_pieces.append("%s\n\n" % patch_merge_desc)
121 122

    bugs = []
123
    for commit_hash in self["full_revision_list"]:
124
      msg = self.GitLog(n=1, git_hash=commit_hash)
125 126
      for bug in re.findall(r"^[ \t]*BUG[ \t]*=[ \t]*(.*?)[ \t]*$", msg, re.M):
        bugs.extend(s.strip() for s in bug.split(","))
127
    bug_aggregate = ",".join(sorted(filter(lambda s: s and s != "none", bugs)))
128
    if bug_aggregate:
129 130 131
      msg_pieces.append("BUG=%s\nLOG=N\n" % bug_aggregate)

    self["new_commit_msg"] = "".join(msg_pieces)
132 133 134 135 136 137


class ApplyPatches(Step):
  MESSAGE = "Apply patches for selected revisions."

  def RunStep(self):
138
    for commit_hash in self["full_revision_list"]:
139
      print("Applying patch for %s to %s..."
140
            % (commit_hash, self["merge_to_branch"]))
141
      patch = self.GitGetPatch(commit_hash)
142
      TextToFile(patch, self.Config("TEMPORARY_PATCH_FILE"))
143
      self.ApplyPatch(self.Config("TEMPORARY_PATCH_FILE"))
144
    if self._options.patch:
145
      self.ApplyPatch(self._options.patch)
146 147 148 149 150 151


class PrepareVersion(Step):
  MESSAGE = "Prepare version file."

  def RunStep(self):
152
    # This is used to calculate the patch level increment.
153 154 155 156 157 158 159
    self.ReadAndPersistVersion()


class IncrementVersion(Step):
  MESSAGE = "Increment version number."

  def RunStep(self):
160
    new_patch = str(int(self["patch"]) + 1)
161
    if self.Confirm("Automatically increment V8_PATCH_LEVEL? (Saying 'n' will "
162 163
                    "fire up your EDITOR on %s so you can make arbitrary "
                    "changes. When you're done, save the file and exit your "
164 165
                    "EDITOR.)" % VERSION_FILE):
      text = FileToText(os.path.join(self.default_cwd, VERSION_FILE))
166
      text = MSub(r"(?<=#define V8_PATCH_LEVEL)(?P<space>\s+)\d*$",
167 168
                  r"\g<space>%s" % new_patch,
                  text)
169
      TextToFile(text, os.path.join(self.default_cwd, VERSION_FILE))
170
    else:
171
      self.Editor(os.path.join(self.default_cwd, VERSION_FILE))
172
    self.ReadAndPersistVersion("new_")
173 174 175 176
    self["version"] = "%s.%s.%s.%s" % (self["new_major"],
                                       self["new_minor"],
                                       self["new_build"],
                                       self["new_patch"])
177 178 179 180 181 182


class CommitLocal(Step):
  MESSAGE = "Commit to local branch."

  def RunStep(self):
183
    # Add a commit message title.
184
    self["commit_title"] = "Version %s (cherry-pick)" % self["version"]
185 186
    self["new_commit_msg"] = "%s\n\n%s" % (self["commit_title"],
                                           self["new_commit_msg"])
187 188
    TextToFile(self["new_commit_msg"], self.Config("COMMITMSG_FILE"))
    self.GitCommit(file_name=self.Config("COMMITMSG_FILE"))
189 190 191 192 193 194


class CommitRepository(Step):
  MESSAGE = "Commit to the repository."

  def RunStep(self):
195
    self.GitCheckout(self.Config("BRANCHNAME"))
196
    self.WaitForLGTM()
197
    self.GitPresubmit()
198
    self.vc.CLLand()
199 200 201 202 203 204


class TagRevision(Step):
  MESSAGE = "Create the tag."

  def RunStep(self):
205 206 207 208
    print "Creating tag %s" % self["version"]
    self.vc.Tag(self["version"],
                self.vc.RemoteBranch(self["merge_to_branch"]),
                self["commit_title"])
209 210 211 212 213 214 215


class CleanUp(Step):
  MESSAGE = "Cleanup."

  def RunStep(self):
    self.CommonCleanup()
216 217 218 219 220
    print "*** SUMMARY ***"
    print "version: %s" % self["version"]
    print "branch: %s" % self["merge_to_branch"]
    if self["revision_list"]:
      print "patches: %s" % self["revision_list"]
221 222


223 224 225
class MergeToBranch(ScriptsBase):
  def _Description(self):
    return ("Performs the necessary steps to merge revisions from "
machenbach's avatar
machenbach committed
226
            "master to other branches, including candidates.")
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

  def _PrepareOptions(self, parser):
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument("--branch", help="The branch to merge to.")
    parser.add_argument("revisions", nargs="*",
                        help="The revisions to merge.")
    parser.add_argument("-f", "--force",
                        help="Delete sentinel file.",
                        default=False, action="store_true")
    parser.add_argument("-m", "--message",
                        help="A commit message for the patch.")
    parser.add_argument("-p", "--patch",
                        help="A patch file to apply as part of the merge.")

  def _ProcessOptions(self, options):
    if len(options.revisions) < 1:
      if not options.patch:
        print "Either a patch file or revision numbers must be specified"
        return False
      if not options.message:
        print "You must specify a merge comment if no patches are specified"
        return False
249
    options.bypass_upload_hooks = True
250 251
    # CC ulan to make sure that fixes are merged to Google3.
    options.cc = "ulan@chromium.org"
252 253 254 255 256 257 258 259

    # Make sure to use git hashes in the new workflows.
    for revision in options.revisions:
      if (IsSvnNumber(revision) or
          (revision[0:1] == "r" and IsSvnNumber(revision[1:]))):
        print "Please provide full git hashes of the patches to merge."
        print "Got: %s" % revision
        return False
260 261
    return True

262 263 264 265 266 267 268 269 270 271
  def _Config(self):
    return {
      "BRANCHNAME": "prepare-merge",
      "PERSISTFILE_BASENAME": "/tmp/v8-merge-to-branch-tempfile",
      "ALREADY_MERGING_SENTINEL_FILE":
          "/tmp/v8-merge-to-branch-tempfile-already-merging",
      "TEMPORARY_PATCH_FILE": "/tmp/v8-prepare-merge-tempfile-temporary-patch",
      "COMMITMSG_FILE": "/tmp/v8-prepare-merge-tempfile-commitmsg",
    }

272 273 274 275 276
  def _Steps(self):
    return [
      Preparation,
      CreateBranch,
      SearchArchitecturePorts,
277
      CreateCommitMessage,
278 279 280 281 282 283 284 285 286 287
      ApplyPatches,
      PrepareVersion,
      IncrementVersion,
      CommitLocal,
      UploadStep,
      CommitRepository,
      TagRevision,
      CleanUp,
    ]

288

289
if __name__ == "__main__":  # pragma: no cover
290
  sys.exit(MergeToBranch().Run())