2014-02-20 16:39:41 +00:00
|
|
|
#!/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.
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
2014-08-25 13:39:43 +00:00
|
|
|
SHA1_RE = re.compile('^[a-fA-F0-9]{40}$')
|
2014-09-04 08:42:21 +00:00
|
|
|
ROLL_DEPS_GIT_SVN_ID_RE = re.compile('^git-svn-id: .*@([0-9]+) .*$')
|
|
|
|
|
|
|
|
# Regular expression that matches a single commit footer line.
|
|
|
|
COMMIT_FOOTER_ENTRY_RE = re.compile(r'([^:]+):\s+(.+)')
|
|
|
|
|
|
|
|
# Footer metadata key for commit position.
|
|
|
|
COMMIT_POSITION_FOOTER_KEY = 'Cr-Commit-Position'
|
|
|
|
|
|
|
|
# Regular expression to parse a commit position
|
|
|
|
COMMIT_POSITION_RE = re.compile(r'(.+)@\{#(\d+)\}')
|
|
|
|
|
|
|
|
# Key for the 'git-svn' ID metadata commit footer entry.
|
|
|
|
GIT_SVN_ID_FOOTER_KEY = 'git-svn-id'
|
|
|
|
|
|
|
|
# e.g., git-svn-id: https://v8.googlecode.com/svn/trunk@23117
|
|
|
|
# ce2b1a6d-e550-0410-aec6-3dcde31c8c00
|
2014-11-12 17:54:11 +00:00
|
|
|
GIT_SVN_ID_RE = re.compile(r'[^@]+@(\d+)\s+(?:[a-zA-Z0-9\-]+)')
|
2014-09-04 08:42:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Copied from bot_update.py.
|
|
|
|
def GetCommitMessageFooterMap(message):
|
|
|
|
"""Returns: (dict) A dictionary of commit message footer entries.
|
|
|
|
"""
|
|
|
|
footers = {}
|
|
|
|
|
|
|
|
# Extract the lines in the footer block.
|
|
|
|
lines = []
|
|
|
|
for line in message.strip().splitlines():
|
|
|
|
line = line.strip()
|
|
|
|
if len(line) == 0:
|
|
|
|
del(lines[:])
|
|
|
|
continue
|
|
|
|
lines.append(line)
|
|
|
|
|
|
|
|
# Parse the footer
|
|
|
|
for line in lines:
|
|
|
|
m = COMMIT_FOOTER_ENTRY_RE.match(line)
|
|
|
|
if not m:
|
|
|
|
# If any single line isn't valid, the entire footer is invalid.
|
|
|
|
footers.clear()
|
|
|
|
return footers
|
|
|
|
footers[m.group(1)] = m.group(2).strip()
|
|
|
|
return footers
|
2014-08-25 13:39:43 +00:00
|
|
|
|
2014-04-09 14:30:02 +00:00
|
|
|
|
|
|
|
class GitFailedException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-02-20 16:39:41 +00:00
|
|
|
def Strip(f):
|
|
|
|
def new_f(*args, **kwargs):
|
2014-10-07 10:46:04 +00:00
|
|
|
result = f(*args, **kwargs)
|
|
|
|
if result is None:
|
|
|
|
return result
|
|
|
|
else:
|
|
|
|
return result.strip()
|
2014-02-20 16:39:41 +00:00
|
|
|
return new_f
|
|
|
|
|
|
|
|
|
|
|
|
def MakeArgs(l):
|
|
|
|
"""['-a', '', 'abc', ''] -> '-a abc'"""
|
|
|
|
return " ".join(filter(None, l))
|
|
|
|
|
|
|
|
|
|
|
|
def Quoted(s):
|
|
|
|
return "\"%s\"" % s
|
|
|
|
|
|
|
|
|
|
|
|
class GitRecipesMixin(object):
|
2014-09-05 09:19:48 +00:00
|
|
|
def GitIsWorkdirClean(self, **kwargs):
|
|
|
|
return self.Git("status -s -uno", **kwargs).strip() == ""
|
2014-02-20 16:39:41 +00:00
|
|
|
|
|
|
|
@Strip
|
2014-09-05 09:19:48 +00:00
|
|
|
def GitBranch(self, **kwargs):
|
|
|
|
return self.Git("branch", **kwargs)
|
2014-02-20 16:39:41 +00:00
|
|
|
|
2014-10-28 13:42:58 +00:00
|
|
|
def GitCreateBranch(self, name, remote="", **kwargs):
|
2014-02-20 16:39:41 +00:00
|
|
|
assert name
|
2014-10-28 13:42:58 +00:00
|
|
|
remote_args = ["--upstream", remote] if remote else []
|
|
|
|
self.Git(MakeArgs(["new-branch", name] + remote_args), **kwargs)
|
2014-02-20 16:39:41 +00:00
|
|
|
|
2014-09-05 09:19:48 +00:00
|
|
|
def GitDeleteBranch(self, name, **kwargs):
|
2014-02-20 16:39:41 +00:00
|
|
|
assert name
|
2014-09-05 09:19:48 +00:00
|
|
|
self.Git(MakeArgs(["branch -D", name]), **kwargs)
|
2014-02-20 16:39:41 +00:00
|
|
|
|
2014-09-05 09:19:48 +00:00
|
|
|
def GitReset(self, name, **kwargs):
|
2014-04-09 14:30:02 +00:00
|
|
|
assert name
|
2014-09-05 09:19:48 +00:00
|
|
|
self.Git(MakeArgs(["reset --hard", name]), **kwargs)
|
2014-04-09 14:30:02 +00:00
|
|
|
|
2014-09-05 09:19:48 +00:00
|
|
|
def GitStash(self, **kwargs):
|
|
|
|
self.Git(MakeArgs(["stash"]), **kwargs)
|
2014-07-16 07:57:13 +00:00
|
|
|
|
2014-09-05 09:19:48 +00:00
|
|
|
def GitRemotes(self, **kwargs):
|
|
|
|
return map(str.strip,
|
|
|
|
self.Git(MakeArgs(["branch -r"]), **kwargs).splitlines())
|
2014-04-09 14:30:02 +00:00
|
|
|
|
2014-09-05 09:19:48 +00:00
|
|
|
def GitCheckout(self, name, **kwargs):
|
2014-02-20 16:39:41 +00:00
|
|
|
assert name
|
2014-09-05 09:19:48 +00:00
|
|
|
self.Git(MakeArgs(["checkout -f", name]), **kwargs)
|
2014-02-20 16:39:41 +00:00
|
|
|
|
2014-09-05 09:19:48 +00:00
|
|
|
def GitCheckoutFile(self, name, branch_or_hash, **kwargs):
|
2014-03-15 13:06:08 +00:00
|
|
|
assert name
|
2014-03-19 13:10:41 +00:00
|
|
|
assert branch_or_hash
|
2014-09-05 09:19:48 +00:00
|
|
|
self.Git(MakeArgs(["checkout -f", branch_or_hash, "--", name]), **kwargs)
|
2014-03-15 13:06:08 +00:00
|
|
|
|
2014-09-05 09:19:48 +00:00
|
|
|
def GitCheckoutFileSafe(self, name, branch_or_hash, **kwargs):
|
2014-04-09 14:30:02 +00:00
|
|
|
try:
|
2014-09-05 09:19:48 +00:00
|
|
|
self.GitCheckoutFile(name, branch_or_hash, **kwargs)
|
2014-04-09 14:30:02 +00:00
|
|
|
except GitFailedException: # pragma: no cover
|
|
|
|
# The file doesn't exist in that revision.
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2014-09-05 09:19:48 +00:00
|
|
|
def GitChangedFiles(self, git_hash, **kwargs):
|
2014-04-09 14:30:02 +00:00
|
|
|
assert git_hash
|
|
|
|
try:
|
|
|
|
files = self.Git(MakeArgs(["diff --name-only",
|
|
|
|
git_hash,
|
2014-09-05 09:19:48 +00:00
|
|
|
"%s^" % git_hash]), **kwargs)
|
2014-04-09 14:30:02 +00:00
|
|
|
return map(str.strip, files.splitlines())
|
|
|
|
except GitFailedException: # pragma: no cover
|
|
|
|
# Git fails using "^" at branch roots.
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
2014-02-20 16:39:41 +00:00
|
|
|
@Strip
|
2014-09-05 09:19:48 +00:00
|
|
|
def GitCurrentBranch(self, **kwargs):
|
|
|
|
for line in self.Git("status -s -b -uno", **kwargs).strip().splitlines():
|
2014-02-20 16:39:41 +00:00
|
|
|
match = re.match(r"^## (.+)", line)
|
|
|
|
if match: return match.group(1)
|
2014-03-12 10:45:23 +00:00
|
|
|
raise Exception("Couldn't find curent branch.") # pragma: no cover
|
2014-02-20 16:39:41 +00:00
|
|
|
|
|
|
|
@Strip
|
|
|
|
def GitLog(self, n=0, format="", grep="", git_hash="", parent_hash="",
|
2014-09-05 09:19:48 +00:00
|
|
|
branch="", reverse=False, **kwargs):
|
2014-02-20 16:39:41 +00:00
|
|
|
assert not (git_hash and parent_hash)
|
|
|
|
args = ["log"]
|
|
|
|
if n > 0:
|
|
|
|
args.append("-%d" % n)
|
|
|
|
if format:
|
|
|
|
args.append("--format=%s" % format)
|
|
|
|
if grep:
|
2014-04-03 13:01:22 +00:00
|
|
|
args.append("--grep=\"%s\"" % grep.replace("\"", "\\\""))
|
2014-02-20 16:39:41 +00:00
|
|
|
if reverse:
|
|
|
|
args.append("--reverse")
|
|
|
|
if git_hash:
|
|
|
|
args.append(git_hash)
|
|
|
|
if parent_hash:
|
|
|
|
args.append("%s^" % parent_hash)
|
|
|
|
args.append(branch)
|
2014-09-05 09:19:48 +00:00
|
|
|
return self.Git(MakeArgs(args), **kwargs)
|
2014-02-20 16:39:41 +00:00
|
|
|
|
2014-09-05 09:19:48 +00:00
|
|
|
def GitGetPatch(self, git_hash, **kwargs):
|
2014-02-26 16:12:32 +00:00
|
|
|
assert git_hash
|
2014-09-05 09:19:48 +00:00
|
|
|
return self.Git(MakeArgs(["log", "-1", "-p", git_hash]), **kwargs)
|
2014-02-26 16:12:32 +00:00
|
|
|
|
2014-04-09 14:30:02 +00:00
|
|
|
# TODO(machenbach): Unused? Remove.
|
2014-09-05 09:19:48 +00:00
|
|
|
def GitAdd(self, name, **kwargs):
|
2014-02-20 16:39:41 +00:00
|
|
|
assert name
|
2014-09-05 09:19:48 +00:00
|
|
|
self.Git(MakeArgs(["add", Quoted(name)]), **kwargs)
|
2014-02-20 16:39:41 +00:00
|
|
|
|
2014-09-05 09:19:48 +00:00
|
|
|
def GitApplyPatch(self, patch_file, reverse=False, **kwargs):
|
2014-02-20 16:39:41 +00:00
|
|
|
assert patch_file
|
|
|
|
args = ["apply --index --reject"]
|
|
|
|
if reverse:
|
|
|
|
args.append("--reverse")
|
|
|
|
args.append(Quoted(patch_file))
|
2014-09-05 09:19:48 +00:00
|
|
|
self.Git(MakeArgs(args), **kwargs)
|
2014-02-20 16:39:41 +00:00
|
|
|
|
2014-07-21 12:13:36 +00:00
|
|
|
def GitUpload(self, reviewer="", author="", force=False, cq=False,
|
2014-09-25 11:28:27 +00:00
|
|
|
bypass_hooks=False, cc="", **kwargs):
|
2014-02-20 16:39:41 +00:00
|
|
|
args = ["cl upload --send-mail"]
|
|
|
|
if author:
|
|
|
|
args += ["--email", Quoted(author)]
|
|
|
|
if reviewer:
|
|
|
|
args += ["-r", Quoted(reviewer)]
|
|
|
|
if force:
|
|
|
|
args.append("-f")
|
2014-06-25 08:17:45 +00:00
|
|
|
if cq:
|
|
|
|
args.append("--use-commit-queue")
|
2014-07-21 12:13:36 +00:00
|
|
|
if bypass_hooks:
|
|
|
|
args.append("--bypass-hooks")
|
2014-09-25 11:28:27 +00:00
|
|
|
if cc:
|
2014-09-26 08:55:25 +00:00
|
|
|
args += ["--cc", Quoted(cc)]
|
2014-02-20 16:39:41 +00:00
|
|
|
# TODO(machenbach): Check output in forced mode. Verify that all required
|
|
|
|
# base files were uploaded, if not retry.
|
2014-09-05 09:19:48 +00:00
|
|
|
self.Git(MakeArgs(args), pipe=False, **kwargs)
|
2014-02-20 16:39:41 +00:00
|
|
|
|
2014-09-05 09:19:48 +00:00
|
|
|
def GitCommit(self, message="", file_name="", author=None, **kwargs):
|
2014-02-20 16:39:41 +00:00
|
|
|
assert message or file_name
|
|
|
|
args = ["commit"]
|
|
|
|
if file_name:
|
|
|
|
args += ["-aF", Quoted(file_name)]
|
|
|
|
if message:
|
|
|
|
args += ["-am", Quoted(message)]
|
2014-09-03 11:59:43 +00:00
|
|
|
if author:
|
|
|
|
args += ["--author", "\"%s <%s>\"" % (author, author)]
|
2014-09-05 09:19:48 +00:00
|
|
|
self.Git(MakeArgs(args), **kwargs)
|
2014-02-20 16:39:41 +00:00
|
|
|
|
2014-09-05 09:19:48 +00:00
|
|
|
def GitPresubmit(self, **kwargs):
|
|
|
|
self.Git("cl presubmit", "PRESUBMIT_TREE_CHECK=\"skip\"", **kwargs)
|
2014-02-20 16:39:41 +00:00
|
|
|
|
2014-11-01 21:53:13 +00:00
|
|
|
def GitCLLand(self, **kwargs):
|
|
|
|
self.Git(
|
|
|
|
"cl land -f --bypass-hooks", retry_on=lambda x: x is None, **kwargs)
|
|
|
|
|
2014-09-05 09:19:48 +00:00
|
|
|
def GitDiff(self, loc1, loc2, **kwargs):
|
|
|
|
return self.Git(MakeArgs(["diff", loc1, loc2]), **kwargs)
|
2014-02-20 16:39:41 +00:00
|
|
|
|
2014-09-05 09:19:48 +00:00
|
|
|
def GitPull(self, **kwargs):
|
|
|
|
self.Git("pull", **kwargs)
|
2014-02-20 16:39:41 +00:00
|
|
|
|
2014-09-05 09:19:48 +00:00
|
|
|
def GitFetchOrigin(self, **kwargs):
|
|
|
|
self.Git("fetch origin", **kwargs)
|
2014-08-25 13:39:43 +00:00
|
|
|
|
2014-09-04 08:42:21 +00:00
|
|
|
@Strip
|
|
|
|
# Copied from bot_update.py and modified for svn-like numbers only.
|
2014-09-05 09:19:48 +00:00
|
|
|
def GetCommitPositionNumber(self, git_hash, **kwargs):
|
2014-09-04 08:42:21 +00:00
|
|
|
"""Dumps the 'git' log for a specific revision and parses out the commit
|
|
|
|
position number.
|
|
|
|
|
|
|
|
If a commit position metadata key is found, its number will be returned.
|
|
|
|
|
|
|
|
Otherwise, we will search for a 'git-svn' metadata entry. If one is found,
|
|
|
|
its SVN revision value is returned.
|
|
|
|
"""
|
2014-09-05 09:19:48 +00:00
|
|
|
git_log = self.GitLog(format='%B', n=1, git_hash=git_hash, **kwargs)
|
2014-09-04 08:42:21 +00:00
|
|
|
footer_map = GetCommitMessageFooterMap(git_log)
|
|
|
|
|
|
|
|
# Search for commit position metadata
|
|
|
|
value = footer_map.get(COMMIT_POSITION_FOOTER_KEY)
|
|
|
|
if value:
|
|
|
|
match = COMMIT_POSITION_RE.match(value)
|
|
|
|
if match:
|
|
|
|
return match.group(2)
|
|
|
|
|
|
|
|
# Extract the svn revision from 'git-svn' metadata
|
|
|
|
value = footer_map.get(GIT_SVN_ID_FOOTER_KEY)
|
|
|
|
if value:
|
|
|
|
match = GIT_SVN_ID_RE.match(value)
|
|
|
|
if match:
|
2014-11-12 17:54:11 +00:00
|
|
|
return match.group(1)
|
|
|
|
raise GitFailedException("Couldn't determine commit position for %s" %
|
|
|
|
git_hash)
|