OLD | NEW |
1 # Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2009 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 re | 6 import re |
7 import subprocess | 7 import subprocess |
8 import sys | 8 import sys |
9 | 9 |
10 import breakpad | 10 import breakpad |
11 | 11 |
12 import gcl | 12 import gcl |
13 import presubmit_support | 13 import presubmit_support |
14 import scm | 14 import scm |
15 import watchlists | 15 import watchlists |
16 | 16 |
17 def Backquote(cmd, cwd=None): | 17 def Backquote(cmd, cwd=None): |
18 """Like running `cmd` in a shell script.""" | 18 """Like running `cmd` in a shell script.""" |
19 return subprocess.Popen(cmd, | 19 return subprocess.Popen(cmd, |
20 cwd=cwd, | 20 cwd=cwd, |
21 stdout=subprocess.PIPE).communicate()[0].strip() | 21 stdout=subprocess.PIPE).communicate()[0].strip() |
22 | 22 |
| 23 def BackquoteAsInteger(cmd, cwd=None): |
| 24 """Like Backquote, but returns either an int or None.""" |
| 25 try: |
| 26 return int(Backquote(cmd, cwd)) |
| 27 except ValueError: |
| 28 return None |
| 29 |
23 | 30 |
24 class ChangeOptions: | 31 class ChangeOptions: |
25 def __init__(self, commit=None, upstream_branch=None): | 32 def __init__(self, commit=None, upstream_branch=None): |
26 self.commit = commit | 33 self.commit = commit |
27 self.verbose = None | 34 self.verbose = None |
28 self.default_presubmit = None | 35 self.default_presubmit = None |
29 self.may_prompt = None | 36 self.may_prompt = None |
30 | 37 |
31 root = Backquote(['git', 'rev-parse', '--show-cdup']) | 38 root = Backquote(['git', 'rev-parse', '--show-cdup']) |
32 if not root: | 39 if not root: |
33 root = "." | 40 root = "." |
34 absroot = os.path.abspath(root) | 41 absroot = os.path.abspath(root) |
35 if not root: | 42 if not root: |
36 raise Exception("Could not get root directory.") | 43 raise Exception("Could not get root directory.") |
37 log = Backquote(['git', 'show', '--name-only', | 44 log = Backquote(['git', 'show', '--name-only', |
38 '--pretty=format:%H%n%s%n%n%b']) | 45 '--pretty=format:%H%n%s%n%n%b']) |
39 m = re.match(r'^(\w+)\n(.*)$', log, re.MULTILINE|re.DOTALL) | 46 m = re.match(r'^(\w+)\n(.*)$', log, re.MULTILINE|re.DOTALL) |
40 if not m: | 47 if not m: |
41 raise Exception("Could not parse log message: %s" % log) | 48 raise Exception("Could not parse log message: %s" % log) |
42 name = m.group(1) | 49 name = m.group(1) |
43 files = scm.GIT.CaptureStatus([root], upstream_branch) | 50 files = scm.GIT.CaptureStatus([root], upstream_branch) |
44 issue = Backquote(['git', 'cl', 'status', '--field=id']) | 51 issue = BackquoteAsInteger(['git', 'cl', 'status', '--field=id']) |
45 try: | 52 patchset = BackquoteAsInteger(['git', 'cl', 'status', '--field=patch']) |
46 description = gcl.GetIssueDescription(int(issue)) | 53 if issue: |
47 except ValueError: | 54 description = gcl.GetIssueDescription(issue) |
| 55 else: |
48 description = m.group(2) | 56 description = m.group(2) |
49 patchset = None | |
50 self.change = presubmit_support.GitChange(name, description, absroot, files, | 57 self.change = presubmit_support.GitChange(name, description, absroot, files, |
51 issue, patchset) | 58 issue, patchset) |
52 | 59 |
53 | 60 |
54 def RunHooks(hook_name, upstream_branch): | 61 def RunHooks(hook_name, upstream_branch): |
55 commit = (hook_name == 'pre-cl-dcommit') | 62 commit = (hook_name == 'pre-cl-dcommit') |
56 | 63 |
57 # Create our options based on the command-line args and the current checkout. | 64 # Create our options based on the command-line args and the current checkout. |
58 options = ChangeOptions(commit=commit, upstream_branch=upstream_branch) | 65 options = ChangeOptions(commit=commit, upstream_branch=upstream_branch) |
59 | 66 |
60 # Apply watchlists on upload. | 67 # Apply watchlists on upload. |
61 if not commit: | 68 if not commit: |
62 watchlist = watchlists.Watchlists(options.change.RepositoryRoot()) | 69 watchlist = watchlists.Watchlists(options.change.RepositoryRoot()) |
63 files = [f.LocalPath() for f in options.change.AffectedFiles()] | 70 files = [f.LocalPath() for f in options.change.AffectedFiles()] |
64 watchers = watchlist.GetWatchersForPaths(files) | 71 watchers = watchlist.GetWatchersForPaths(files) |
65 Backquote(['git', 'config', '--replace-all', | 72 Backquote(['git', 'config', '--replace-all', |
66 'rietveld.extracc', ','.join(watchers)]) | 73 'rietveld.extracc', ','.join(watchers)]) |
67 | 74 |
68 # Run the presubmit checks. | 75 # Run the presubmit checks. |
69 if presubmit_support.DoPresubmitChecks(options.change, | 76 if presubmit_support.DoPresubmitChecks(options.change, |
70 options.commit, | 77 options.commit, |
71 options.verbose, | 78 options.verbose, |
72 sys.stdout, | 79 sys.stdout, |
73 sys.stdin, | 80 sys.stdin, |
74 options.default_presubmit, | 81 options.default_presubmit, |
75 options.may_prompt): | 82 options.may_prompt): |
76 sys.exit(0) | 83 sys.exit(0) |
77 else: | 84 else: |
78 sys.exit(1) | 85 sys.exit(1) |
OLD | NEW |