OLD | NEW |
1 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import os | 5 import os |
6 import subprocess | 6 import subprocess |
7 import sys | 7 import sys |
8 | 8 |
9 import breakpad # pylint: disable=W0611 | 9 import breakpad # pylint: disable=W0611 |
10 | 10 |
(...skipping 14 matching lines...) Expand all Loading... |
25 stdout=subprocess.PIPE).communicate()[0].strip() | 25 stdout=subprocess.PIPE).communicate()[0].strip() |
26 | 26 |
27 def ConvertToInteger(inputval): | 27 def ConvertToInteger(inputval): |
28 """Convert a string to integer, but returns either an int or None.""" | 28 """Convert a string to integer, but returns either an int or None.""" |
29 try: | 29 try: |
30 return int(inputval) | 30 return int(inputval) |
31 except (TypeError, ValueError): | 31 except (TypeError, ValueError): |
32 return None | 32 return None |
33 | 33 |
34 | 34 |
35 class ChangeOptions: | 35 class ChangeOptions(object): |
36 def __init__(self, commit=None, upstream_branch=None): | 36 def __init__(self, commit, upstream_branch, cmd_line_options): |
37 self.commit = commit | 37 self.commit = commit |
38 self.verbose = None | 38 self.verbose = None |
39 self.default_presubmit = None | 39 self.default_presubmit = None |
40 self.may_prompt = None | 40 self.may_prompt = None |
| 41 self.host_url = cmd_line_options.host_url |
| 42 self.tbr = cmd_line_options.tbr |
41 | 43 |
42 root = Backquote(['git', 'rev-parse', '--show-cdup']) | 44 root = Backquote(['git', 'rev-parse', '--show-cdup']) |
43 if not root: | 45 if not root: |
44 root = "." | 46 root = "." |
45 absroot = os.path.abspath(root) | 47 absroot = os.path.abspath(root) |
46 if not root: | 48 if not root: |
47 raise Exception("Could not get root directory.") | 49 raise Exception("Could not get root directory.") |
48 # We use the sha1 of HEAD as a name of this change. | 50 # We use the sha1 of HEAD as a name of this change. |
49 name = Backquote(['git', 'rev-parse', 'HEAD']) | 51 name = Backquote(['git', 'rev-parse', 'HEAD']) |
50 files = scm.GIT.CaptureStatus([root], upstream_branch) | 52 files = scm.GIT.CaptureStatus([root], upstream_branch) |
51 cl = git_cl.Changelist() | 53 cl = git_cl.Changelist() |
52 issue = ConvertToInteger(cl.GetIssue()) | 54 issue = ConvertToInteger(cl.GetIssue()) |
53 patchset = ConvertToInteger(cl.GetPatchset()) | 55 patchset = ConvertToInteger(cl.GetPatchset()) |
54 if issue: | 56 if issue: |
55 description = cl.GetDescription() | 57 description = cl.GetDescription() |
56 else: | 58 else: |
57 # If the change was never uploaded, use the log messages of all commits | 59 # 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 | 60 # up to the branch point, as git cl upload will prefill the description |
59 # with these log messages. | 61 # with these log messages. |
60 description = Backquote(['git', 'log', '--pretty=format:%s%n%n%b', | 62 description = Backquote(['git', 'log', '--pretty=format:%s%n%n%b', |
61 '%s...' % (upstream_branch)]) | 63 '%s...' % (upstream_branch)]) |
62 self.change = presubmit_support.GitChange(name, description, absroot, files, | 64 self.change = presubmit_support.GitChange(name, description, absroot, files, |
63 issue, patchset) | 65 issue, patchset) |
64 | 66 |
| 67 # TODO(dpranke): remove when we make cmd_line_options mandatory in RunHooks(). |
| 68 class DefaultOptions(object): |
| 69 tbr = False |
| 70 host_url = None |
65 | 71 |
66 def RunHooks(hook_name, upstream_branch): | 72 |
| 73 def RunHooks(hook_name, upstream_branch, cmd_line_options=None): |
| 74 # TODO(dpranke): make cmd_line_options mandatory |
| 75 cmd_line_options = cmd_line_options or DefaultOptions() |
| 76 |
67 commit = (hook_name == 'pre-cl-dcommit') | 77 commit = (hook_name == 'pre-cl-dcommit') |
68 | 78 |
69 # Create our options based on the command-line args and the current checkout. | 79 # Create our options based on the command-line args and the current checkout. |
70 options = ChangeOptions(commit=commit, upstream_branch=upstream_branch) | 80 options = ChangeOptions(commit, upstream_branch, cmd_line_options) |
71 | 81 |
72 # Apply watchlists on upload. | 82 # Apply watchlists on upload. |
73 if not commit: | 83 if not commit: |
74 watchlist = watchlists.Watchlists(options.change.RepositoryRoot()) | 84 watchlist = watchlists.Watchlists(options.change.RepositoryRoot()) |
75 files = [f.LocalPath() for f in options.change.AffectedFiles()] | 85 files = [f.LocalPath() for f in options.change.AffectedFiles()] |
76 watchers = watchlist.GetWatchersForPaths(files) | 86 watchers = watchlist.GetWatchersForPaths(files) |
77 Backquote(['git', 'config', '--replace-all', | 87 Backquote(['git', 'config', '--replace-all', |
78 'rietveld.extracc', ','.join(watchers)]) | 88 'rietveld.extracc', ','.join(watchers)]) |
79 | 89 |
80 # Run the presubmit checks. | 90 # Run the presubmit checks. |
| 91 # TODO(dpranke): just pass the whole options object? |
81 if presubmit_support.DoPresubmitChecks(options.change, | 92 if presubmit_support.DoPresubmitChecks(options.change, |
82 options.commit, | 93 options.commit, |
83 options.verbose, | 94 options.verbose, |
84 sys.stdout, | 95 sys.stdout, |
85 sys.stdin, | 96 sys.stdin, |
86 options.default_presubmit, | 97 options.default_presubmit, |
87 options.may_prompt): | 98 options.may_prompt, |
| 99 options.tbr, |
| 100 options.host_url): |
88 sys.exit(0) | 101 sys.exit(0) |
89 else: | 102 else: |
90 sys.exit(1) | 103 sys.exit(1) |
OLD | NEW |