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
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.
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.
31 SHA1_RE = re.compile('^[a-fA-F0-9]{40}$')
32 ROLL_DEPS_GIT_SVN_ID_RE = re.compile('^git-svn-id: .*@([0-9]+) .*$')
34 # Regular expression that matches a single commit footer line.
35 COMMIT_FOOTER_ENTRY_RE = re.compile(r'([^:]+):\s+(.+)')
37 # Footer metadata key for commit position.
38 COMMIT_POSITION_FOOTER_KEY = 'Cr-Commit-Position'
40 # Regular expression to parse a commit position
41 COMMIT_POSITION_RE = re.compile(r'(.+)@\{#(\d+)\}')
43 # Key for the 'git-svn' ID metadata commit footer entry.
44 GIT_SVN_ID_FOOTER_KEY = 'git-svn-id'
46 # e.g., git-svn-id: https://v8.googlecode.com/svn/trunk@23117
47 # ce2b1a6d-e550-0410-aec6-3dcde31c8c00
48 GIT_SVN_ID_RE = re.compile(r'((?:\w+)://[^@]+)@(\d+)\s+(?:[a-zA-Z0-9\-]+)')
51 # Copied from bot_update.py.
52 def GetCommitMessageFooterMap(message):
53 """Returns: (dict) A dictionary of commit message footer entries.
57 # Extract the lines in the footer block.
59 for line in message.strip().splitlines():
68 m = COMMIT_FOOTER_ENTRY_RE.match(line)
70 # If any single line isn't valid, the entire footer is invalid.
73 footers[m.group(1)] = m.group(2).strip()
77 class GitFailedException(Exception):
82 def new_f(*args, **kwargs):
83 return f(*args, **kwargs).strip()
88 """['-a', '', 'abc', ''] -> '-a abc'"""
89 return " ".join(filter(None, l))
96 class GitRecipesMixin(object):
97 def GitIsWorkdirClean(self, **kwargs):
98 return self.Git("status -s -uno", **kwargs).strip() == ""
101 def GitBranch(self, **kwargs):
102 return self.Git("branch", **kwargs)
104 def GitCreateBranch(self, name, branch="", **kwargs):
106 self.Git(MakeArgs(["checkout -b", name, branch]), **kwargs)
108 def GitDeleteBranch(self, name, **kwargs):
110 self.Git(MakeArgs(["branch -D", name]), **kwargs)
112 def GitReset(self, name, **kwargs):
114 self.Git(MakeArgs(["reset --hard", name]), **kwargs)
116 def GitStash(self, **kwargs):
117 self.Git(MakeArgs(["stash"]), **kwargs)
119 def GitRemotes(self, **kwargs):
120 return map(str.strip,
121 self.Git(MakeArgs(["branch -r"]), **kwargs).splitlines())
123 def GitCheckout(self, name, **kwargs):
125 self.Git(MakeArgs(["checkout -f", name]), **kwargs)
127 def GitCheckoutFile(self, name, branch_or_hash, **kwargs):
129 assert branch_or_hash
130 self.Git(MakeArgs(["checkout -f", branch_or_hash, "--", name]), **kwargs)
132 def GitCheckoutFileSafe(self, name, branch_or_hash, **kwargs):
134 self.GitCheckoutFile(name, branch_or_hash, **kwargs)
135 except GitFailedException: # pragma: no cover
136 # The file doesn't exist in that revision.
140 def GitChangedFiles(self, git_hash, **kwargs):
143 files = self.Git(MakeArgs(["diff --name-only",
145 "%s^" % git_hash]), **kwargs)
146 return map(str.strip, files.splitlines())
147 except GitFailedException: # pragma: no cover
148 # Git fails using "^" at branch roots.
153 def GitCurrentBranch(self, **kwargs):
154 for line in self.Git("status -s -b -uno", **kwargs).strip().splitlines():
155 match = re.match(r"^## (.+)", line)
156 if match: return match.group(1)
157 raise Exception("Couldn't find curent branch.") # pragma: no cover
160 def GitLog(self, n=0, format="", grep="", git_hash="", parent_hash="",
161 branch="", reverse=False, **kwargs):
162 assert not (git_hash and parent_hash)
165 args.append("-%d" % n)
167 args.append("--format=%s" % format)
169 args.append("--grep=\"%s\"" % grep.replace("\"", "\\\""))
171 args.append("--reverse")
173 args.append(git_hash)
175 args.append("%s^" % parent_hash)
177 return self.Git(MakeArgs(args), **kwargs)
179 def GitGetPatch(self, git_hash, **kwargs):
181 return self.Git(MakeArgs(["log", "-1", "-p", git_hash]), **kwargs)
183 # TODO(machenbach): Unused? Remove.
184 def GitAdd(self, name, **kwargs):
186 self.Git(MakeArgs(["add", Quoted(name)]), **kwargs)
188 def GitApplyPatch(self, patch_file, reverse=False, **kwargs):
190 args = ["apply --index --reject"]
192 args.append("--reverse")
193 args.append(Quoted(patch_file))
194 self.Git(MakeArgs(args), **kwargs)
196 def GitUpload(self, reviewer="", author="", force=False, cq=False,
197 bypass_hooks=False, **kwargs):
198 args = ["cl upload --send-mail"]
200 args += ["--email", Quoted(author)]
202 args += ["-r", Quoted(reviewer)]
206 args.append("--use-commit-queue")
208 args.append("--bypass-hooks")
209 # TODO(machenbach): Check output in forced mode. Verify that all required
210 # base files were uploaded, if not retry.
211 self.Git(MakeArgs(args), pipe=False, **kwargs)
213 def GitCommit(self, message="", file_name="", author=None, **kwargs):
214 assert message or file_name
217 args += ["-aF", Quoted(file_name)]
219 args += ["-am", Quoted(message)]
221 args += ["--author", "\"%s <%s>\"" % (author, author)]
222 self.Git(MakeArgs(args), **kwargs)
224 def GitPresubmit(self, **kwargs):
225 self.Git("cl presubmit", "PRESUBMIT_TREE_CHECK=\"skip\"", **kwargs)
227 def GitDCommit(self, **kwargs):
229 "cl dcommit -f --bypass-hooks", retry_on=lambda x: x is None, **kwargs)
231 def GitDiff(self, loc1, loc2, **kwargs):
232 return self.Git(MakeArgs(["diff", loc1, loc2]), **kwargs)
234 def GitPull(self, **kwargs):
235 self.Git("pull", **kwargs)
237 def GitFetchOrigin(self, **kwargs):
238 self.Git("fetch origin", **kwargs)
240 def GitConvertToSVNRevision(self, git_hash, **kwargs):
241 result = self.Git(MakeArgs(["rev-list", "-n", "1", git_hash]), **kwargs)
242 if not result or not SHA1_RE.match(result):
243 raise GitFailedException("Git hash %s is unknown." % git_hash)
244 log = self.GitLog(n=1, format="%B", git_hash=git_hash, **kwargs)
245 for line in reversed(log.splitlines()):
246 match = ROLL_DEPS_GIT_SVN_ID_RE.match(line.strip())
248 return match.group(1)
249 raise GitFailedException("Couldn't convert %s to SVN." % git_hash)
252 # Copied from bot_update.py and modified for svn-like numbers only.
253 def GetCommitPositionNumber(self, git_hash, **kwargs):
254 """Dumps the 'git' log for a specific revision and parses out the commit
257 If a commit position metadata key is found, its number will be returned.
259 Otherwise, we will search for a 'git-svn' metadata entry. If one is found,
260 its SVN revision value is returned.
262 git_log = self.GitLog(format='%B', n=1, git_hash=git_hash, **kwargs)
263 footer_map = GetCommitMessageFooterMap(git_log)
265 # Search for commit position metadata
266 value = footer_map.get(COMMIT_POSITION_FOOTER_KEY)
268 match = COMMIT_POSITION_RE.match(value)
270 return match.group(2)
272 # Extract the svn revision from 'git-svn' metadata
273 value = footer_map.get(GIT_SVN_ID_FOOTER_KEY)
275 match = GIT_SVN_ID_RE.match(value)
277 return match.group(2)
282 def GitSVNFetch(self, **kwargs):
283 self.Git("svn fetch", **kwargs)
285 def GitSVNRebase(self, **kwargs):
286 self.Git("svn rebase", **kwargs)
288 # TODO(machenbach): Unused? Remove.
290 def GitSVNLog(self, **kwargs):
291 return self.Git("svn log -1 --oneline", **kwargs)
294 def GitSVNFindGitHash(self, revision, branch="", **kwargs):
297 MakeArgs(["svn find-rev", "r%s" % revision, branch]), **kwargs)
300 def GitSVNFindSVNRev(self, git_hash, branch="", **kwargs):
301 return self.Git(MakeArgs(["svn find-rev", git_hash, branch]), **kwargs)
303 def GitSVNDCommit(self, **kwargs):
304 return self.Git("svn dcommit 2>&1", retry_on=lambda x: x is None, **kwargs)
306 def GitSVNTag(self, version, **kwargs):
307 self.Git(("svn tag %s -m \"Tagging version %s\"" % (version, version)),
308 retry_on=lambda x: x is None,