OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2009 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2009 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import os | 6 import os |
7 import re | |
8 import subprocess | |
9 import sys | 7 import sys |
10 | 8 |
11 # Find depot_tools in PATH, append it to sys.path so we can import. | 9 # Find depot_tools in PATH, append it to sys.path so we can import. |
12 paths = os.environ.get("PATH") | 10 paths = os.environ.get("PATH") |
13 for path in paths.split(':'): | 11 for path in paths.split(':'): |
14 if not path.endswith("depot_tools"): | 12 if not path.endswith("depot_tools"): |
15 continue | 13 continue |
16 sys.path.append(path) | 14 sys.path.append(path) |
17 break | 15 break |
18 | 16 |
19 import gclient_scm | 17 import git_cl_hooks |
20 import presubmit_support | |
21 | |
22 def Backquote(cmd, cwd=None): | |
23 """Like running `cmd` in a shell script.""" | |
24 return subprocess.Popen(cmd, | |
25 cwd=cwd, | |
26 stdout=subprocess.PIPE).communicate()[0].strip() | |
27 | |
28 | |
29 class ChangeOptions: | |
30 def __init__(self, commit=None, upstream_branch=None): | |
31 self.commit = commit | |
32 self.verbose = None | |
33 self.default_presubmit = None | |
34 self.may_prompt = None | |
35 | |
36 root = os.path.abspath(Backquote(['git', 'rev-parse', '--show-cdup'])) | |
37 if not root: | |
38 raise Exception("Could not get root directory.") | |
39 log = Backquote(['git', 'show', '--name-only', | |
40 '--pretty=format:%H%n%s%n%n%b']) | |
41 m = re.match(r'^(\w+)\n(.*)$', log, re.MULTILINE|re.DOTALL) | |
42 if not m: | |
43 raise Exception("Could not parse log message: %s" % log) | |
44 name = m.group(1) | |
45 description = m.group(2) | |
46 files = gclient_scm.CaptureGitStatus([root], upstream_branch) | |
47 issue = Backquote(['git', 'cl', 'status', '--field=id']) | |
48 patchset = None | |
49 self.change = presubmit_support.GitChange(name, description, root, files, | |
50 issue, patchset) | |
51 | |
52 | 18 |
53 # Ensure we were called with the necessary number of arguments. | 19 # Ensure we were called with the necessary number of arguments. |
54 program_name = os.path.basename(sys.argv[0]) | 20 program_name = os.path.basename(sys.argv[0]) |
55 if len(sys.argv) < 2: | 21 if len(sys.argv) != 2: |
56 raise Exception("usage: %s [upstream branch]" % program_name) | 22 raise Exception("usage: %s [upstream branch]" % program_name) |
57 | 23 |
58 # Get arguments from how we were called. | 24 # Run the hooks library with our arguments. |
59 commit = (program_name == 'pre-cl-dcommit') | 25 exec git_cl_hooks.RunHooks(hook_name=program_name, upstream_branch=sys.argv[1]) |
60 upstream_branch = sys.argv[1] | |
61 | |
62 # Create our options based on the command-line args and the current checkout. | |
63 options = ChangeOptions(commit=commit, upstream_branch=upstream_branch) | |
64 | |
65 # Run the presubmit checks. | |
66 if presubmit_support.DoPresubmitChecks(options.change, | |
67 options.commit, | |
68 options.verbose, | |
69 sys.stdout, | |
70 sys.stdin, | |
71 options.default_presubmit, | |
72 options.may_prompt): | |
73 sys.exit(0) | |
74 else: | |
75 sys.exit(1) | |
OLD | NEW |