# This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os import re import subprocess import sys import tempfile
from six.moves import input from six.moves.urllib import parse as urlparse from wptrunner.update.base import Step, StepRunner, exit_clean, exit_unclean from wptrunner.update.tree import get_unique_name
from .github import GitHub from .tree import Commit, GitTree, Patch
def rewrite_patch(patch, strip_dir): """Take a Patch and convert to a different repository by stripping a prefix from the
file paths. Also rewrite the message to remove the bug number and reviewer, but add
a bugzilla link in the summary.
:param patch: the Patch to convert
:param strip_dir: the path prefix to remove """
def create(self, state):
self.logger.info("Looking for last sync commit")
state.sync_data_path = os.path.join(state.metadata_path, "mozilla-sync")
items = {} with open(state.sync_data_path) as f: for line in f.readlines():
key, value = [item.strip() for item in line.split(":", 1)]
items[key] = value
for i, commit in enumerate(state.source_commits[:]): if update_regexp.match(commit.message.text): # This is a previous update commit so ignore it
state.source_commits.remove(commit) continue
elif commit.message.backouts: # TODO: Add support for collapsing backouts
state.has_backouts = True
elifnot commit.message.bug:
self.logger.error( "Commit %i (%s) doesn't have an associated bug number."
% (i + 1, commit.sha1)
) return exit_unclean
class SelectCommits(Step): """Provide a UI to select which commits to upstream"""
def create(self, state): whileTrue:
commits = state.source_commits[:] for i, commit in enumerate(commits):
print("{}:\t{}".format(i, commit.message.summary))
remove = input( "Provide a space-separated list of any commits numbers " "to remove from the list to upstream:\n"
).strip()
remove_idx = set() for item in remove.split(" "): try:
item = int(item) except ValueError: continue if item < 0 or item >= len(commits): continue
remove_idx.add(item)
keep_commits = [
(i, cmt) for i, cmt in enumerate(commits) if i notin remove_idx
] # TODO: consider printed removed commits
print("Selected the following commits to keep:") for i, commit in keep_commits:
print("{}:\t{}".format(i, commit.message.summary))
confirm = input("Keep the above commits? y/n\n").strip().lower()
if confirm == "y":
state.source_commits = [item[1] for item in keep_commits] break
class MovePatches(Step): """Convert gecko commits into patches against upstream and commit these to the sync tree."""
for commit in state.source_commits[state.commits_loaded :]:
i = state.commits_loaded + 1
self.logger.info("Moving commit %i: %s" % (i, commit.message.full_summary))
stripped_patch = None if state.patch:
filename, stripped_patch = state.patch ifnot os.path.exists(filename):
stripped_patch = None else: with open(filename) as f:
stripped_patch.diff = f.read()
state.patch = None ifnot stripped_patch:
patch = commit.export_patch(state.tests_path)
stripped_patch = rewrite_patch(patch, strip_path) ifnot stripped_patch.diff:
self.logger.info("Skipping empty patch")
state.commits_loaded = i continue try:
state.sync_tree.import_patch(stripped_patch) except Exception: with tempfile.NamedTemporaryFile(delete=False, suffix=".diff") as f:
f.write(stripped_patch.diff)
print( """Patch failed to apply. Diff saved in {}
Fix this file so it applies and run with --continue""".format(
f.name
)
)
state.patch = (f.name, stripped_patch)
print(state.patch)
sys.exit(1)
state.commits_loaded = i
input("Check for differences with upstream")
class RebaseCommits(Step): """Rebase commits from the current branch on top of the upstream destination branch.
This step is particularly likely to fail if the rebase generates merge conflicts. In that case the conflicts can be fixed up locally and the sync process restarted with --continue. """
def create(self, state):
self.logger.info("Rebasing local commits")
continue_rebase = False # Check if there's a rebase in progress if os.path.exists(
os.path.join(state.sync_tree.root, ".git", "rebase-merge")
) or os.path.exists(os.path.join(state.sync_tree.root, ".git", "rebase-apply")):
continue_rebase = True
try:
state.sync_tree.rebase(state.base_commit, continue_rebase=continue_rebase) except subprocess.CalledProcessError:
self.logger.info( "Rebase failed, fix merge and run %s again with --continue"
% sys.argv[0]
) raise
self.logger.info("Rebase successful")
class CheckRebase(Step): """Check if there are any commits remaining after rebase"""
org, name = urlparse.urlsplit(state.sync["remote_url"]).path[1:].split("/") if name.endswith(".git"):
name = name[:-4]
state.gh_repo = gh.repo(org, name) for commit in state.rebased_commits[state.merge_index :]: with state.push(["gh_repo", "sync_tree"]):
state.commit = commit
pr_merger = PRMergeRunner(self.logger, state)
rv = pr_merger.run() if rv isnotNone: return rv
state.merge_index += 1
class UpdateLastSyncData(Step): """Update the gecko commit at which we last performed a sync with upstream."""
provides = []
def create(self, state):
self.logger.info("Updating last sync commit")
data = { "local": state.local_tree.rev_to_hg(state.local_tree.rev), "upstream": state.sync_tree.rev,
} with open(state.sync_data_path, "w") as f: for key, value in data.iteritems():
f.write("%s: %s\n" % (key, value)) # This gets added to the patch later on
class MergeLocalBranch(Step): """Create a local branch pointing at the commit to upstream"""
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.