blob: c5cef840313277c0bd6caaa2d5775f6080dc0f3e [file] [log] [blame]
[email protected]03fd85b2014-06-09 23:43:331#!/usr/bin/env python
[email protected]96550942015-05-22 18:46:512# Copyright 2015 The Chromium Authors. All rights reserved.
[email protected]03fd85b2014-06-09 23:43:333# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
[email protected]96550942015-05-22 18:46:516"""Rolls DEPS controlled dependency.
[email protected]03fd85b2014-06-09 23:43:337
[email protected]30e5b232015-06-01 18:06:598Works only with git checkout and git dependencies. Currently this
9script will always roll to the tip of to origin/master.
[email protected]03fd85b2014-06-09 23:43:3310"""
11
[email protected]30e5b232015-06-01 18:06:5912import argparse
[email protected]03fd85b2014-06-09 23:43:3313import os
14import re
[email protected]96550942015-05-22 18:46:5115import subprocess
[email protected]03fd85b2014-06-09 23:43:3316import sys
17
[email protected]c20f4702015-05-23 00:44:4618NEED_SHELL = sys.platform.startswith('win')
19
20
[email protected]e5d984b2015-05-29 22:09:3921class Error(Exception):
22 pass
23
24
smut7036c4f2016-06-09 21:28:4825class AlreadyRolledError(Error):
26 pass
27
28
[email protected]c20f4702015-05-23 00:44:4629def check_output(*args, **kwargs):
30 """subprocess.check_output() passing shell=True on Windows for git."""
31 kwargs.setdefault('shell', NEED_SHELL)
32 return subprocess.check_output(*args, **kwargs)
33
34
35def check_call(*args, **kwargs):
36 """subprocess.check_call() passing shell=True on Windows for git."""
37 kwargs.setdefault('shell', NEED_SHELL)
38 subprocess.check_call(*args, **kwargs)
39
40
[email protected]96550942015-05-22 18:46:5141def is_pristine(root, merge_base='origin/master'):
42 """Returns True if a git checkout is pristine."""
43 cmd = ['git', 'diff', '--ignore-submodules', merge_base]
[email protected]c20f4702015-05-23 00:44:4644 return not (check_output(cmd, cwd=root).strip() or
45 check_output(cmd + ['--cached'], cwd=root).strip())
[email protected]03fd85b2014-06-09 23:43:3346
47
[email protected]398ed342015-09-29 12:27:2548def get_log_url(upstream_url, head, master):
49 """Returns an URL to read logs via a Web UI if applicable."""
50 if re.match(r'https://[^/]*\.googlesource\.com/', upstream_url):
51 # gitiles
52 return '%s/+log/%s..%s' % (upstream_url, head[:12], master[:12])
53 if upstream_url.startswith('https://github.com/'):
54 upstream_url = upstream_url.rstrip('/')
55 if upstream_url.endswith('.git'):
56 upstream_url = upstream_url[:-len('.git')]
57 return '%s/compare/%s...%s' % (upstream_url, head[:12], master[:12])
58 return None
59
60
61def should_show_log(upstream_url):
62 """Returns True if a short log should be included in the tree."""
63 # Skip logs for very active projects.
64 if upstream_url.endswith((
65 '/angle/angle.git',
[email protected]398ed342015-09-29 12:27:2566 '/v8/v8.git')):
67 return False
68 if 'webrtc' in upstream_url:
69 return False
70 return True
71
72
[email protected]02d20872015-11-14 00:46:4173def roll(root, deps_dir, roll_to, key, reviewers, bug, no_log, log_limit,
74 ignore_dirty_tree=False):
[email protected]96550942015-05-22 18:46:5175 deps = os.path.join(root, 'DEPS')
[email protected]03fd85b2014-06-09 23:43:3376 try:
[email protected]96550942015-05-22 18:46:5177 with open(deps, 'rb') as f:
78 deps_content = f.read()
[email protected]a7a229f2015-05-22 21:34:4679 except (IOError, OSError):
[email protected]e5d984b2015-05-29 22:09:3980 raise Error('Ensure the script is run in the directory '
81 'containing DEPS file.')
[email protected]98201122015-04-22 20:21:3482
[email protected]02d20872015-11-14 00:46:4183 if not ignore_dirty_tree and not is_pristine(root):
[email protected]8a6495c2015-12-07 21:46:4184 raise Error('Ensure %s is clean first (no non-merged commits).' % root)
[email protected]96550942015-05-22 18:46:5185
[email protected]c20f4702015-05-23 00:44:4686 full_dir = os.path.normpath(os.path.join(os.path.dirname(root), deps_dir))
[email protected]30e5b232015-06-01 18:06:5987 if not os.path.isdir(full_dir):
[email protected]8a6495c2015-12-07 21:46:4188 raise Error('Directory not found: %s (%s)' % (deps_dir, full_dir))
[email protected]e5d984b2015-05-29 22:09:3989 head = check_output(['git', 'rev-parse', 'HEAD'], cwd=full_dir).strip()
[email protected]96550942015-05-22 18:46:5190
91 if not head in deps_content:
92 print('Warning: %s is not checked out at the expected revision in DEPS' %
93 deps_dir)
[email protected]e5d984b2015-05-29 22:09:3994 if key is None:
95 print("Warning: no key specified. Using '%s'." % deps_dir)
96 key = deps_dir
97
[email protected]96550942015-05-22 18:46:5198 # It happens if the user checked out a branch in the dependency by himself.
99 # Fall back to reading the DEPS to figure out the original commit.
100 for i in deps_content.splitlines():
[email protected]d4ef5992016-02-18 00:05:22101 m = re.match(r'\s+"' + key + '":.*"([a-z0-9]{40})",', i)
[email protected]96550942015-05-22 18:46:51102 if m:
103 head = m.group(1)
104 break
105 else:
[email protected]e5d984b2015-05-29 22:09:39106 raise Error('Expected to find commit %s for %s in DEPS' % (head, key))
[email protected]96550942015-05-22 18:46:51107
108 print('Found old revision %s' % head)
109
[email protected]398ed342015-09-29 12:27:25110 check_call(['git', 'fetch', 'origin', '--quiet'], cwd=full_dir)
111 roll_to = check_output(['git', 'rev-parse', roll_to], cwd=full_dir).strip()
112 print('Found new revision %s' % roll_to)
[email protected]96550942015-05-22 18:46:51113
[email protected]398ed342015-09-29 12:27:25114 if roll_to == head:
smut7036c4f2016-06-09 21:28:48115 raise AlreadyRolledError('No revision to roll!')
[email protected]96550942015-05-22 18:46:51116
[email protected]398ed342015-09-29 12:27:25117 commit_range = '%s..%s' % (head[:9], roll_to[:9])
[email protected]96550942015-05-22 18:46:51118
[email protected]398ed342015-09-29 12:27:25119 upstream_url = check_output(
120 ['git', 'config', 'remote.origin.url'], cwd=full_dir).strip()
121 log_url = get_log_url(upstream_url, head, roll_to)
[email protected]64f2fc32015-10-07 19:11:17122 cmd = [
123 'git', 'log', commit_range, '--date=short', '--no-merges',
124 ]
[email protected]c6e39fe2015-09-23 13:45:52125 logs = check_output(
[email protected]64f2fc32015-10-07 19:11:17126 cmd + ['--format=%ad %ae %s'], # Args with '=' are automatically quoted.
[email protected]398ed342015-09-29 12:27:25127 cwd=full_dir)
[email protected]c6e39fe2015-09-23 13:45:52128 logs = re.sub(r'(?m)^(\d\d\d\d-\d\d-\d\d [^@]+)@[^ ]+( .*)$', r'\1\2', logs)
[email protected]398ed342015-09-29 12:27:25129 nb_commits = logs.count('\n')
130
131 header = 'Roll %s/ %s (%d commit%s).\n\n' % (
132 deps_dir,
133 commit_range,
134 nb_commits,
135 's' if nb_commits > 1 else '')
136
137 log_section = ''
138 if log_url:
139 log_section = log_url + '\n\n'
[email protected]64f2fc32015-10-07 19:11:17140 log_section += '$ %s ' % ' '.join(cmd)
141 log_section += '--format=\'%ad %ae %s\'\n'
[email protected]398ed342015-09-29 12:27:25142 if not no_log and should_show_log(upstream_url):
143 if logs.count('\n') > log_limit:
144 # Keep the first N log entries.
145 logs = ''.join(logs.splitlines(True)[:log_limit]) + '(...)\n'
146 log_section += logs
147 log_section += '\n'
148
[email protected]c6e39fe2015-09-23 13:45:52149 reviewer = 'R=%s\n' % ','.join(reviewers) if reviewers else ''
150 bug = 'BUG=%s\n' % bug if bug else ''
[email protected]398ed342015-09-29 12:27:25151 msg = header + log_section + reviewer + bug
[email protected]96550942015-05-22 18:46:51152
153 print('Commit message:')
154 print('\n'.join(' ' + i for i in msg.splitlines()))
[email protected]398ed342015-09-29 12:27:25155 deps_content = deps_content.replace(head, roll_to)
[email protected]96550942015-05-22 18:46:51156 with open(deps, 'wb') as f:
157 f.write(deps_content)
[email protected]c20f4702015-05-23 00:44:46158 check_call(['git', 'add', 'DEPS'], cwd=root)
[email protected]398ed342015-09-29 12:27:25159 check_call(['git', 'commit', '--quiet', '-m', msg], cwd=root)
160
161 # Pull the dependency to the right revision. This is surprising to users
162 # otherwise.
163 check_call(['git', 'checkout', '--quiet', roll_to], cwd=full_dir)
164
[email protected]96550942015-05-22 18:46:51165 print('')
166 if not reviewers:
167 print('You forgot to pass -r, make sure to insert a [email protected] line')
168 print('to the commit description before emailing.')
169 print('')
170 print('Run:')
171 print(' git cl upload --send-mail')
[email protected]03fd85b2014-06-09 23:43:33172
173
[email protected]96550942015-05-22 18:46:51174def main():
[email protected]30e5b232015-06-01 18:06:59175 parser = argparse.ArgumentParser(description=__doc__)
[email protected]398ed342015-09-29 12:27:25176 parser.add_argument(
[email protected]02d20872015-11-14 00:46:41177 '--ignore-dirty-tree', action='store_true',
178 help='Roll anyways, even if there is a diff.')
179 parser.add_argument(
[email protected]398ed342015-09-29 12:27:25180 '-r', '--reviewer',
[email protected]96550942015-05-22 18:46:51181 help='To specify multiple reviewers, use comma separated list, e.g. '
[email protected]c20f4702015-05-23 00:44:46182 '-r joe,jane,john. Defaults to @chromium.org')
[email protected]398ed342015-09-29 12:27:25183 parser.add_argument('-b', '--bug', help='Associate a bug number to the roll')
184 parser.add_argument(
185 '--no-log', action='store_true',
186 help='Do not include the short log in the commit message')
187 parser.add_argument(
188 '--log-limit', type=int, default=100,
189 help='Trim log after N commits (default: %(default)s)')
190 parser.add_argument(
191 '--roll-to', default='origin/master',
192 help='Specify the new commit to roll to (default: %(default)s)')
193 parser.add_argument('dep_path', help='Path to dependency')
[email protected]30e5b232015-06-01 18:06:59194 parser.add_argument('key', nargs='?',
[email protected]398ed342015-09-29 12:27:25195 help='Regexp for dependency in DEPS file')
[email protected]30e5b232015-06-01 18:06:59196 args = parser.parse_args()
[email protected]96550942015-05-22 18:46:51197
198 reviewers = None
[email protected]30e5b232015-06-01 18:06:59199 if args.reviewer:
200 reviewers = args.reviewer.split(',')
[email protected]96550942015-05-22 18:46:51201 for i, r in enumerate(reviewers):
202 if not '@' in r:
203 reviewers[i] = r + '@chromium.org'
204
[email protected]e5d984b2015-05-29 22:09:39205 try:
206 roll(
207 os.getcwd(),
[email protected]261fa7d2015-11-23 19:20:09208 args.dep_path.rstrip('/').rstrip('\\'),
[email protected]398ed342015-09-29 12:27:25209 args.roll_to,
[email protected]30e5b232015-06-01 18:06:59210 args.key,
[email protected]398ed342015-09-29 12:27:25211 reviewers,
212 args.bug,
213 args.no_log,
[email protected]02d20872015-11-14 00:46:41214 args.log_limit,
215 args.ignore_dirty_tree)
[email protected]e5d984b2015-05-29 22:09:39216
217 except Error as e:
218 sys.stderr.write('error: %s\n' % e)
smut7036c4f2016-06-09 21:28:48219 return 2 if isinstance(e, AlreadyRolledError) else 1
[email protected]e5d984b2015-05-29 22:09:39220
221 return 0
[email protected]03fd85b2014-06-09 23:43:33222
[email protected]98201122015-04-22 20:21:34223
[email protected]03fd85b2014-06-09 23:43:33224if __name__ == '__main__':
[email protected]96550942015-05-22 18:46:51225 sys.exit(main())