| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import os | |
| 6 import subprocess | |
| 7 import sys | |
| 8 | |
| 9 import breakpad # pylint: disable=W0611 | |
| 10 | |
| 11 from git_cl import git_cl | |
| 12 from git_cl import upload | |
| 13 | |
| 14 import presubmit_support | |
| 15 import scm | |
| 16 import watchlists | |
| 17 | |
| 18 # Really ugly hack to quiet upload.py | |
| 19 upload.verbosity = 0 | |
| 20 | |
| 21 def Backquote(cmd, cwd=None): | |
| 22 """Like running `cmd` in a shell script.""" | |
| 23 return subprocess.Popen(cmd, | |
| 24 cwd=cwd, | |
| 25 stdout=subprocess.PIPE).communicate()[0].strip() | |
| 26 | |
| 27 def ConvertToInteger(inputval): | |
| 28 """Convert a string to integer, but returns either an int or None.""" | |
| 29 try: | |
| 30 return int(inputval) | |
| 31 except (TypeError, ValueError): | |
| 32 return None | |
| 33 | |
| 34 | |
| 35 class ChangeOptions: | |
| 36 def __init__(self, commit=None, upstream_branch=None): | |
| 37 self.commit = commit | |
| 38 self.verbose = None | |
| 39 self.default_presubmit = None | |
| 40 self.may_prompt = None | |
| 41 | |
| 42 root = Backquote(['git', 'rev-parse', '--show-cdup']) | |
| 43 if not root: | |
| 44 root = "." | |
| 45 absroot = os.path.abspath(root) | |
| 46 if not root: | |
| 47 raise Exception("Could not get root directory.") | |
| 48 # We use the sha1 of HEAD as a name of this change. | |
| 49 name = Backquote(['git', 'rev-parse', 'HEAD']) | |
| 50 files = scm.GIT.CaptureStatus([root], upstream_branch) | |
| 51 cl = git_cl.Changelist() | |
| 52 issue = ConvertToInteger(cl.GetIssue()) | |
| 53 patchset = ConvertToInteger(cl.GetPatchset()) | |
| 54 if issue: | |
| 55 description = cl.GetDescription() | |
| 56 else: | |
| 57 # If the change was never uploaded, use the log messages of all commits | |
| 58 # up to the branch point, as git cl upload will prefill the description | |
| 59 # with these log messages. | |
| 60 description = Backquote(['git', 'log', '--pretty=format:%s%n%n%b', | |
| 61 '%s...' % (upstream_branch)]) | |
| 62 self.change = presubmit_support.GitChange(name, description, absroot, files, | |
| 63 issue, patchset) | |
| 64 | |
| 65 | |
| 66 def RunHooks(hook_name, upstream_branch): | |
| 67 commit = (hook_name == 'pre-cl-dcommit') | |
| 68 | |
| 69 # Create our options based on the command-line args and the current checkout. | |
| 70 options = ChangeOptions(commit=commit, upstream_branch=upstream_branch) | |
| 71 | |
| 72 # Apply watchlists on upload. | |
| 73 if not commit: | |
| 74 watchlist = watchlists.Watchlists(options.change.RepositoryRoot()) | |
| 75 files = [f.LocalPath() for f in options.change.AffectedFiles()] | |
| 76 watchers = watchlist.GetWatchersForPaths(files) | |
| 77 Backquote(['git', 'config', '--replace-all', | |
| 78 'rietveld.extracc', ','.join(watchers)]) | |
| 79 | |
| 80 # Run the presubmit checks. | |
| 81 if presubmit_support.DoPresubmitChecks(options.change, | |
| 82 options.commit, | |
| 83 options.verbose, | |
| 84 sys.stdout, | |
| 85 sys.stdin, | |
| 86 options.default_presubmit, | |
| 87 options.may_prompt): | |
| 88 sys.exit(0) | |
| 89 else: | |
| 90 sys.exit(1) | |
| OLD | NEW |