Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: tools/push-to-trunk/merge_to_branch.py

Issue 163183004: Add merge-to-branch python port. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Addressed review comments. Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 = list(self._options.args)
102 port_revision_list = []
103 for revision in full_revision_list:
104 # Search for commits which matches the "Port rXXX" pattern.
105 args = ("log svn/bleeding_edge --reverse "
106 "--format=%%H --grep=\"Port r%d\"" % int(revision))
107 git_hashes = self.Git(args) or ""
108 for git_hash in git_hashes.strip().splitlines():
109 args = "svn find-rev %s svn/bleeding_edge" % git_hash
110 svn_revision = self.Git(args).strip()
111 if not svn_revision:
112 self.Die("Cannot determine svn revision for %s" % git_hash)
113 revision_title = self.Git("log -1 --format=%%s %s" % git_hash)
114
115 # Is this revision included in the original revision list?
116 if svn_revision in full_revision_list:
117 print("Found port of r%s -> r%s (already included): %s"
118 % (revision, svn_revision, revision_title))
119 else:
120 print("Found port of r%s -> r%s: %s"
121 % (revision, svn_revision, revision_title))
122 port_revision_list.append(svn_revision)
123
124 # Do we find any port?
125 if len(port_revision_list) > 0:
126 if self.Confirm("Automatically add corresponding ports (%s)?"
127 % ", ".join(port_revision_list)):
128 #: 'y': Add ports to revision list.
129 full_revision_list.extend(port_revision_list)
130 self.Persist("full_revision_list", ",".join(full_revision_list))
131
132
133 class FindGitRevisions(Step):
134 MESSAGE = "Find the git revisions associated with the patches."
135
136 def RunStep(self):
137 self.RestoreIfUnset("full_revision_list")
138 self.RestoreIfUnset("merge_to_branch")
139 full_revision_list = self._state["full_revision_list"].split(",")
140 patch_commit_hashes = []
141 for revision in full_revision_list:
142 next_hash = self.Git("svn find-rev \"r%s\" svn/bleeding_edge" % revision)
143 if not next_hash:
144 self.Die("Cannot determine git hash for r%s" % revision)
145 patch_commit_hashes.append(next_hash)
146
147 # Stringify: [123, 234] -> "r123, r234"
148 revision_list = ", ".join(map(lambda s: "r%s" % s, full_revision_list))
149
150 if not revision_list:
151 self.Die("Revision list is empty.")
152
153 if self._options.revert:
154 if not self._options.revert_bleeding_edge:
155 new_commit_msg = ("Rollback of %s in %s branch."
156 % (revision_list, self._state["merge_to_branch"]))
157 else:
158 new_commit_msg = "Revert %s ." % revision_list
159 else:
160 new_commit_msg = ("Merged %s into %s branch."
161 % (revision_list, self._state["merge_to_branch"]))
162 new_commit_msg += "\n\n"
163
164 for commit_hash in patch_commit_hashes:
165 patch_merge_desc = self.Git("log -1 --format=%%s %s" % commit_hash)
166 new_commit_msg += "%s\n\n" % patch_merge_desc.strip()
167
168 bugs = []
169 for commit_hash in patch_commit_hashes:
170 msg = self.Git("log -1 %s" % commit_hash)
171 for bug in re.findall(r"^[ \t]*BUG[ \t]*=[ \t]*(.*?)[ \t]*$", msg,
172 re.M):
173 bugs.extend(map(lambda s: s.strip(), bug.split(",")))
174 bug_aggregate = ",".join(sorted(bugs))
175 if bug_aggregate:
176 new_commit_msg += "BUG=%s\nLOG=N\n" % bug_aggregate
177 TextToFile(new_commit_msg, self.Config(COMMITMSG_FILE))
178 self.Persist("new_commit_msg", new_commit_msg)
179 self.Persist("revision_list", revision_list)
180 self._state["patch_commit_hashes"] = patch_commit_hashes
181 self.Persist("patch_commit_hashes_list", " ".join(patch_commit_hashes))
182
183
184 class ApplyPatches(Step):
185 MESSAGE = "Apply patches for selected revisions."
186
187 def RunStep(self):
188 self.RestoreIfUnset("merge_to_branch")
189 self.RestoreIfUnset("patch_commit_hashes_list")
190 patch_commit_hashes = self._state.get("patch_commit_hashes")
191 if not patch_commit_hashes:
192 patch_commit_hashes = (
193 self._state.get("patch_commit_hashes_list").strip().split(" "))
194 if not patch_commit_hashes and not options.patch:
195 self.Die("Variable patch_commit_hashes could not be restored.")
196 for commit_hash in patch_commit_hashes:
197 print("Applying patch for %s to %s..."
198 % (commit_hash, self._state["merge_to_branch"]))
199 patch = self.Git("log -1 -p %s" % commit_hash)
200 TextToFile(patch, self.Config(TEMPORARY_PATCH_FILE))
201 self.ApplyPatch(self.Config(TEMPORARY_PATCH_FILE), self._options.revert)
202 if self._options.patch:
203 self.ApplyPatch(self._options.patch, self._options.revert)
204
205
206 class PrepareVersion(Step):
207 MESSAGE = "Prepare version file."
208
209 def RunStep(self):
210 if self._options.revert_bleeding_edge:
211 return
212 # These version numbers are used again for creating the tag
213 self.ReadAndPersistVersion()
214
215
216 class IncrementVersion(Step):
217 MESSAGE = "Increment version number."
218
219 def RunStep(self):
220 if self._options.revert_bleeding_edge:
221 return
222 self.RestoreIfUnset("patch")
223 new_patch = str(int(self._state["patch"]) + 1)
224 if self.Confirm("Automatically increment PATCH_LEVEL? (Saying 'n' will "
225 "fire up your EDITOR on %s so you can make arbitrary "
226 "changes. When you're done, save the file and exit your "
227 "EDITOR.)" % self.Config(VERSION_FILE)):
228 text = FileToText(self.Config(VERSION_FILE))
229 text = MSub(r"(?<=#define PATCH_LEVEL)(?P<space>\s+)\d*$",
230 r"\g<space>%s" % new_patch,
231 text)
232 TextToFile(text, self.Config(VERSION_FILE))
233 else:
234 self.Editor(self.Config(VERSION_FILE))
235 self.ReadAndPersistVersion("new_")
236
237
238 class CommitLocal(Step):
239 MESSAGE = "Commit to local branch."
240
241 def RunStep(self):
242 if self.Git("commit -a -F \"%s\"" % self.Config(COMMITMSG_FILE)) is None:
243 self.Die("'git commit -a' failed.")
244
245
246 class CommitRepository(Step):
247 MESSAGE = "Commit to the repository."
248
249 def RunStep(self):
250 self.RestoreIfUnset("merge_to_branch")
251 if self.Git("checkout %s" % self.Config(BRANCHNAME)) is None:
252 self.Die("Cannot ensure that the current branch is %s"
253 % self.Config(BRANCHNAME))
254 self.WaitForLGTM()
255 if self.Git("cl presubmit", "PRESUBMIT_TREE_CHECK=\"skip\"") is None:
256 self.Die("Presubmit failed.")
257
258 if self.Git("cl dcommit -f --bypass-hooks",
259 retry_on=lambda x: x is None) is None:
260 self.Die("Failed to commit to %s" % self._status["merge_to_branch"])
261
262
263 class PrepareSVN(Step):
264 MESSAGE = "Determine svn commit revision."
265
266 def RunStep(self):
267 if self._options.revert_bleeding_edge:
268 return
269 self.RestoreIfUnset("new_commit_msg")
270 self.RestoreIfUnset("merge_to_branch")
271 if self.Git("svn fetch") is None:
272 self.Die("'git svn fetch' failed.")
273 args = ("log -1 --format=%%H --grep=\"%s\" svn/%s"
274 % (self._state["new_commit_msg"], self._state["merge_to_branch"]))
275 commit_hash = self.Git(args).strip()
276 if not commit_hash:
277 self.Die("Unable to map git commit to svn revision.")
278 svn_revision = self.Git("svn find-rev %s" % commit_hash).strip()
279 print "subversion revision number is r%s" % svn_revision
280 self.Persist("svn_revision", svn_revision)
281
282
283 class TagRevision(Step):
284 MESSAGE = "Create the tag."
285
286 def RunStep(self):
287 if self._options.revert_bleeding_edge:
288 return
289 self.RestoreVersionIfUnset("new_")
290 self.RestoreIfUnset("svn_revision")
291 self.RestoreIfUnset("merge_to_branch")
292 ver = "%s.%s.%s.%s" % (self._state["new_major"],
293 self._state["new_minor"],
294 self._state["new_build"],
295 self._state["new_patch"])
296 print "Creating tag svn/tags/%s" % ver
297 if self._state["merge_to_branch"] == "trunk":
298 to_url = "trunk"
299 else:
300 to_url = "branches/%s" % self._state["merge_to_branch"]
301 self.SVN("copy -r %s https://v8.googlecode.com/svn/%s "
302 "https://v8.googlecode.com/svn/tags/%s -m "
303 "\"Tagging version %s\""
304 % (self._state["svn_revision"], to_url, ver, ver))
305 self.Persist("to_url", to_url)
306
307
308 class CleanUp(Step):
309 MESSAGE = "Cleanup."
310
311 def RunStep(self):
312 self.RestoreIfUnset("svn_revision")
313 self.RestoreIfUnset("to_url")
314 self.RestoreIfUnset("revision_list")
315 self.RestoreVersionIfUnset("new_")
316 ver = "%s.%s.%s.%s" % (self._state["new_major"],
317 self._state["new_minor"],
318 self._state["new_build"],
319 self._state["new_patch"])
320 self.CommonCleanup()
321 if not self._options.revert_bleeding_edge:
322 print "*** SUMMARY ***"
323 print "version: %s" % ver
324 print "branch: %s" % self._state["to_url"]
325 print "svn revision: %s" % self._state["svn_revision"]
326 if self._state["revision_list"]:
327 print "patches: %s" % self._state["revision_list"]
328
329
330 def RunMergeToBranch(config,
331 options,
332 side_effect_handler=DEFAULT_SIDE_EFFECT_HANDLER):
333 step_classes = [
334 Preparation,
335 CreateBranch,
336 SearchArchitecturePorts,
337 FindGitRevisions,
338 ApplyPatches,
339 PrepareVersion,
340 IncrementVersion,
341 CommitLocal,
342 UploadStep,
343 CommitRepository,
344 PrepareSVN,
345 TagRevision,
346 CleanUp,
347 ]
348
349 RunScript(step_classes, config, options, side_effect_handler)
350
351
352 def BuildOptions():
353 result = optparse.OptionParser()
354 result.add_option("-f",
355 help="Delete sentinel file.",
356 default=False, action="store_true")
357 result.add_option("-m", "--message",
358 help="Specify a commit message for the patch.")
359 result.add_option("-r", "--revert",
360 help="Revert specified patches.",
361 default=False, action="store_true")
362 result.add_option("-R", "--revert-bleeding-edge",
363 help="Revert specified patches from bleeding edge.",
364 default=False, action="store_true")
365 result.add_option("-p", "--patch", dest="p",
366 help="Specify a patch file to apply as part of the merge.")
367 result.add_option("-s", "--step", dest="s",
368 help="Specify the step where to start work. Default: 0.",
369 default=0, type="int")
370 return result
371
372
373 def ProcessOptions(options, args):
374 revert_from_bleeding_edge = 1 if options.revert_bleeding_edge else 0
375 min_exp_args = 2 - revert_from_bleeding_edge
376 if len(args) < min_exp_args:
377 if not options.p:
378 print "Either a patch file or revision numbers must be specified"
379 return False
380 if not options.message:
381 print "You must specify a merge comment if no patches are specified"
382 return False
383 if options.s < 0:
384 print "Bad step number %d" % options.s
385 return False
386 return True
387
388
389 def Main():
390 parser = BuildOptions()
391 (options, args) = parser.parse_args()
392 if not ProcessOptions(options, args):
393 parser.print_help()
394 return 1
395 RunMergeToBranch(CONFIG, MergeToBranchOptions(options))
396
397 if __name__ == "__main__":
398 sys.exit(Main())
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698