-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPRESUBMIT.py
97 lines (79 loc) · 3.31 KB
/
PRESUBMIT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Copyright 2013 Google Inc. All rights reserved.
# Use of this source code is governed by the Apache license that can be
# found in the LICENSE file.
"""Presubmit checks for tavern fork of pub subtree."""
import subprocess
import sys
from git_cl import Changelist
import tempfile
import os
def CheckChangeOnUpload(input_api, output_api):
"""Checks that the main dart2js target has been run"""
results = []
results += input_api.canned_checks.CheckLongLines(
input_api, output_api, 80)
results += input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol(
input_api, output_api)
results += input_api.canned_checks.CheckChangeTodoHasOwner(
input_api, output_api)
results += input_api.canned_checks.CheckChangeHasNoStrayWhitespace(
input_api, output_api)
results += input_api.canned_checks.CheckChangeHasNoTabs(
input_api, output_api)
try:
subprocess.check_output(['make', 'app'])
except subprocess.CalledProcessError:
results.append(output_api.PresubmitError(
'dart2js has errors or could not be run.'))
return results
def _WriteTemporaryFile(data):
f = tempfile.NamedTemporaryFile('w')
f.write(data)
f.flush()
os.fsync(f)
return f
def _CheckLocalBranchMatchesPatchset(input_api, output_api):
issue = input_api.change.issue
cl = Changelist(issue=issue)
patch_set = cl.GetMostRecentPatchset()
patch_set_diff = cl.GetPatchSetDiff(issue, patch_set)
local_diff = subprocess.check_output(['git', 'diff', 'origin/master'])
with _WriteTemporaryFile(patch_set_diff) as patch_set_diff_file:
with _WriteTemporaryFile(local_diff) as local_diff_file:
diff_diff = subprocess.check_output(
['interdiff', patch_set_diff_file.name, local_diff_file.name])
if diff_diff:
return [output_api.PresubmitError(
'Local branch does not match patch set %s for issue %s. '
'Revert or upload new changes to branch to resolve.\n\n%s' %
(patch_set, issue, diff_diff))]
return []
def CheckChangeOnCommit(input_api, output_api):
"""Checks that the CL got an lgtm, and sets the current branch's remote to
'origin' so that 'git cl push' pushes to the Github repo."""
results = []
checks = input_api.canned_checks
results.extend(checks.CheckChangeWasUploaded(input_api, output_api))
if not results:
results.extend(_CheckLocalBranchMatchesPatchset(input_api, output_api))
if not results:
results.extend(checks.CheckOwners(input_api, output_api))
if not results:
branch = subprocess.check_output(
['git', 'rev-parse', '--abbrev-ref', 'HEAD']).strip()
remote = subprocess.check_output(
['git', 'config', 'branch.%s.remote' % branch]).strip()
upstream = subprocess.check_output(
['git', 'config', 'branch.%s.merge' % branch]).strip()
if remote != 'origin' or upstream != 'refs/head/master':
set_remote = raw_input(
'Upstream should be set to \'origin/master\', set it now [y|n]?')
if (set_remote.startswith('y')):
subprocess.check_output(
['git', 'config', 'branch.%s.remote' % branch, 'origin'])
subprocess.check_output(
['git', 'config', 'branch.%s.merge' % branch, 'refs/heads/master'])
else:
results.append(output_api.PresubmitError(
'Upstream must be set to \'origin/master\' to push to Github.'))
return results