Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / v8 / tools / push-to-trunk / merge_to_branch.py
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 *
34
35 class Preparation(Step):
36   MESSAGE = "Preparation."
37
38   def RunStep(self):
39     if os.path.exists(self.Config("ALREADY_MERGING_SENTINEL_FILE")):
40       if self._options.force:
41         os.remove(self.Config("ALREADY_MERGING_SENTINEL_FILE"))
42       elif self._options.step == 0:  # pragma: no cover
43         self.Die("A merge is already in progress")
44     open(self.Config("ALREADY_MERGING_SENTINEL_FILE"), "a").close()
45
46     self.InitialEnvironmentChecks(self.default_cwd)
47     if self._options.revert_bleeding_edge:
48       # FIXME(machenbach): Make revert bleeding_edge obsolete?
49       self["merge_to_branch"] = "bleeding_edge"
50     elif self._options.branch:
51       self["merge_to_branch"] = self._options.branch
52     else:  # pragma: no cover
53       self.Die("Please specify a branch to merge to")
54
55     self.CommonPrepare()
56     self.PrepareBranch()
57
58
59 class CreateBranch(Step):
60   MESSAGE = "Create a fresh branch for the patch."
61
62   def RunStep(self):
63     self.GitCreateBranch(self.Config("BRANCHNAME"),
64                          self.vc.RemoteBranch(self["merge_to_branch"]))
65
66
67 class SearchArchitecturePorts(Step):
68   MESSAGE = "Search for corresponding architecture ports."
69
70   def RunStep(self):
71     self["full_revision_list"] = list(OrderedDict.fromkeys(
72         self._options.revisions))
73     port_revision_list = []
74     for revision in self["full_revision_list"]:
75       # Search for commits which matches the "Port rXXX" pattern.
76       git_hashes = self.GitLog(reverse=True, format="%H",
77                                grep="Port r%d" % int(revision),
78                                branch=self.vc.RemoteMasterBranch())
79       for git_hash in git_hashes.splitlines():
80         svn_revision = self.vc.GitSvn(git_hash, self.vc.RemoteMasterBranch())
81         if not svn_revision:  # pragma: no cover
82           self.Die("Cannot determine svn revision for %s" % git_hash)
83         revision_title = self.GitLog(n=1, format="%s", git_hash=git_hash)
84
85         # Is this revision included in the original revision list?
86         if svn_revision in self["full_revision_list"]:
87           print("Found port of r%s -> r%s (already included): %s"
88                 % (revision, svn_revision, revision_title))
89         else:
90           print("Found port of r%s -> r%s: %s"
91                 % (revision, svn_revision, revision_title))
92           port_revision_list.append(svn_revision)
93
94     # Do we find any port?
95     if len(port_revision_list) > 0:
96       if self.Confirm("Automatically add corresponding ports (%s)?"
97                       % ", ".join(port_revision_list)):
98         #: 'y': Add ports to revision list.
99         self["full_revision_list"].extend(port_revision_list)
100
101
102 class FindGitRevisions(Step):
103   MESSAGE = "Find the git revisions associated with the patches."
104
105   def RunStep(self):
106     self["patch_commit_hashes"] = []
107     for revision in self["full_revision_list"]:
108       next_hash = self.vc.SvnGit(revision, self.vc.RemoteMasterBranch())
109       if not next_hash:  # pragma: no cover
110         self.Die("Cannot determine git hash for r%s" % revision)
111       self["patch_commit_hashes"].append(next_hash)
112
113     # Stringify: [123, 234] -> "r123, r234"
114     self["revision_list"] = ", ".join(map(lambda s: "r%s" % s,
115                                       self["full_revision_list"]))
116
117     if not self["revision_list"]:  # pragma: no cover
118       self.Die("Revision list is empty.")
119
120     # The commit message title is added below after the version is specified.
121     self["new_commit_msg"] = ""
122
123     for commit_hash in self["patch_commit_hashes"]:
124       patch_merge_desc = self.GitLog(n=1, format="%s", git_hash=commit_hash)
125       self["new_commit_msg"] += "%s\n\n" % patch_merge_desc
126
127     bugs = []
128     for commit_hash in self["patch_commit_hashes"]:
129       msg = self.GitLog(n=1, git_hash=commit_hash)
130       for bug in re.findall(r"^[ \t]*BUG[ \t]*=[ \t]*(.*?)[ \t]*$", msg,
131                             re.M):
132         bugs.extend(map(lambda s: s.strip(), bug.split(",")))
133     bug_aggregate = ",".join(sorted(filter(lambda s: s and s != "none", bugs)))
134     if bug_aggregate:
135       self["new_commit_msg"] += "BUG=%s\nLOG=N\n" % bug_aggregate
136
137
138 class ApplyPatches(Step):
139   MESSAGE = "Apply patches for selected revisions."
140
141   def RunStep(self):
142     for commit_hash in self["patch_commit_hashes"]:
143       print("Applying patch for %s to %s..."
144             % (commit_hash, self["merge_to_branch"]))
145       patch = self.GitGetPatch(commit_hash)
146       TextToFile(patch, self.Config("TEMPORARY_PATCH_FILE"))
147       self.ApplyPatch(self.Config("TEMPORARY_PATCH_FILE"), self._options.revert)
148     if self._options.patch:
149       self.ApplyPatch(self._options.patch, self._options.revert)
150
151
152 class PrepareVersion(Step):
153   MESSAGE = "Prepare version file."
154
155   def RunStep(self):
156     if self._options.revert_bleeding_edge:
157       return
158     # This is used to calculate the patch level increment.
159     self.ReadAndPersistVersion()
160
161
162 class IncrementVersion(Step):
163   MESSAGE = "Increment version number."
164
165   def RunStep(self):
166     if self._options.revert_bleeding_edge:
167       return
168     new_patch = str(int(self["patch"]) + 1)
169     if self.Confirm("Automatically increment PATCH_LEVEL? (Saying 'n' will "
170                     "fire up your EDITOR on %s so you can make arbitrary "
171                     "changes. When you're done, save the file and exit your "
172                     "EDITOR.)" % VERSION_FILE):
173       text = FileToText(os.path.join(self.default_cwd, VERSION_FILE))
174       text = MSub(r"(?<=#define PATCH_LEVEL)(?P<space>\s+)\d*$",
175                   r"\g<space>%s" % new_patch,
176                   text)
177       TextToFile(text, os.path.join(self.default_cwd, VERSION_FILE))
178     else:
179       self.Editor(os.path.join(self.default_cwd, VERSION_FILE))
180     self.ReadAndPersistVersion("new_")
181     self["version"] = "%s.%s.%s.%s" % (self["new_major"],
182                                        self["new_minor"],
183                                        self["new_build"],
184                                        self["new_patch"])
185
186
187 class CommitLocal(Step):
188   MESSAGE = "Commit to local branch."
189
190   def RunStep(self):
191     # Add a commit message title.
192     if self._options.revert:
193       if not self._options.revert_bleeding_edge:
194         title = ("Version %s (rollback of %s)"
195                  % (self["version"], self["revision_list"]))
196       else:
197         title = "Revert %s." % self["revision_list"]
198     else:
199       title = ("Version %s (merged %s)"
200                % (self["version"], self["revision_list"]))
201     self["commit_title"] = title
202     self["new_commit_msg"] = "%s\n\n%s" % (title, self["new_commit_msg"])
203     TextToFile(self["new_commit_msg"], self.Config("COMMITMSG_FILE"))
204     self.GitCommit(file_name=self.Config("COMMITMSG_FILE"))
205
206
207 class CommitRepository(Step):
208   MESSAGE = "Commit to the repository."
209
210   def RunStep(self):
211     self.GitCheckout(self.Config("BRANCHNAME"))
212     self.WaitForLGTM()
213     self.GitPresubmit()
214     self.vc.CLLand()
215
216
217 class TagRevision(Step):
218   MESSAGE = "Create the tag."
219
220   def RunStep(self):
221     if self._options.revert_bleeding_edge:
222       return
223     print "Creating tag %s" % self["version"]
224     self.vc.Tag(self["version"],
225                 self.vc.RemoteBranch(self["merge_to_branch"]),
226                 self["commit_title"])
227
228
229 class CleanUp(Step):
230   MESSAGE = "Cleanup."
231
232   def RunStep(self):
233     self.CommonCleanup()
234     if not self._options.revert_bleeding_edge:
235       print "*** SUMMARY ***"
236       print "version: %s" % self["version"]
237       print "branch: %s" % self["merge_to_branch"]
238       if self["revision_list"]:
239         print "patches: %s" % self["revision_list"]
240
241
242 class MergeToBranch(ScriptsBase):
243   def _Description(self):
244     return ("Performs the necessary steps to merge revisions from "
245             "bleeding_edge to other branches, including trunk.")
246
247   def _PrepareOptions(self, parser):
248     group = parser.add_mutually_exclusive_group(required=True)
249     group.add_argument("--branch", help="The branch to merge to.")
250     group.add_argument("-R", "--revert-bleeding-edge",
251                        help="Revert specified patches from bleeding edge.",
252                        default=False, action="store_true")
253     parser.add_argument("revisions", nargs="*",
254                         help="The revisions to merge.")
255     parser.add_argument("-f", "--force",
256                         help="Delete sentinel file.",
257                         default=False, action="store_true")
258     parser.add_argument("-m", "--message",
259                         help="A commit message for the patch.")
260     parser.add_argument("--revert",
261                         help="Revert specified patches.",
262                         default=False, action="store_true")
263     parser.add_argument("-p", "--patch",
264                         help="A patch file to apply as part of the merge.")
265
266   def _ProcessOptions(self, options):
267     # TODO(machenbach): Add a test that covers revert from bleeding_edge
268     if len(options.revisions) < 1:
269       if not options.patch:
270         print "Either a patch file or revision numbers must be specified"
271         return False
272       if not options.message:
273         print "You must specify a merge comment if no patches are specified"
274         return False
275     options.bypass_upload_hooks = True
276     # CC ulan to make sure that fixes are merged to Google3.
277     options.cc = "ulan@chromium.org"
278     return True
279
280   def _Config(self):
281     return {
282       "BRANCHNAME": "prepare-merge",
283       "PERSISTFILE_BASENAME": "/tmp/v8-merge-to-branch-tempfile",
284       "ALREADY_MERGING_SENTINEL_FILE":
285           "/tmp/v8-merge-to-branch-tempfile-already-merging",
286       "TEMPORARY_PATCH_FILE": "/tmp/v8-prepare-merge-tempfile-temporary-patch",
287       "COMMITMSG_FILE": "/tmp/v8-prepare-merge-tempfile-commitmsg",
288     }
289
290   def _Steps(self):
291     return [
292       Preparation,
293       CreateBranch,
294       SearchArchitecturePorts,
295       FindGitRevisions,
296       ApplyPatches,
297       PrepareVersion,
298       IncrementVersion,
299       CommitLocal,
300       UploadStep,
301       CommitRepository,
302       TagRevision,
303       CleanUp,
304     ]
305
306
307 if __name__ == "__main__":  # pragma: no cover
308   sys.exit(MergeToBranch().Run())