blob: 96e1ad550c9b610daecb35ee7a4c038f31d29c65 [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.
Marc-Antoine Ruel51104fe2017-03-01 22:57:41127 cwd=full_dir).rstrip()
[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)
Marc-Antoine Ruel51104fe2017-03-01 22:57:41129 lines = logs.splitlines()
130 cleaned_lines = [
131 l for l in lines
132 if not l.endswith('recipe-roller Roll recipe dependencies (trivial).')
133 ]
134 logs = '\n'.join(cleaned_lines) + '\n'
135 nb_commits = len(lines)
136 rolls = nb_commits - len(cleaned_lines)
[email protected]398ed342015-09-29 12:27:25137
Marc-Antoine Ruel51104fe2017-03-01 22:57:41138 header = 'Roll %s/ %s (%d commit%s%s)\n\n' % (
[email protected]398ed342015-09-29 12:27:25139 deps_dir,
140 commit_range,
141 nb_commits,
Marc-Antoine Ruel51104fe2017-03-01 22:57:41142 's' if nb_commits > 1 else '',
143 ('; %s trivial rolls' % rolls) if rolls else '')
[email protected]398ed342015-09-29 12:27:25144
145 log_section = ''
146 if log_url:
147 log_section = log_url + '\n\n'
[email protected]64f2fc32015-10-07 19:11:17148 log_section += '$ %s ' % ' '.join(cmd)
149 log_section += '--format=\'%ad %ae %s\'\n'
[email protected]398ed342015-09-29 12:27:25150 if not no_log and should_show_log(upstream_url):
Marc-Antoine Ruel51104fe2017-03-01 22:57:41151 if len(cleaned_lines) > log_limit:
[email protected]398ed342015-09-29 12:27:25152 # Keep the first N log entries.
153 logs = ''.join(logs.splitlines(True)[:log_limit]) + '(...)\n'
154 log_section += logs
Marc-Antoine Ruel22e7a272017-02-08 01:09:37155 log_section += '\n\nCreated with:\n roll-dep %s\n' % deps_dir
[email protected]398ed342015-09-29 12:27:25156
[email protected]c6e39fe2015-09-23 13:45:52157 reviewer = 'R=%s\n' % ','.join(reviewers) if reviewers else ''
158 bug = 'BUG=%s\n' % bug if bug else ''
[email protected]398ed342015-09-29 12:27:25159 msg = header + log_section + reviewer + bug
[email protected]96550942015-05-22 18:46:51160
161 print('Commit message:')
162 print('\n'.join(' ' + i for i in msg.splitlines()))
[email protected]398ed342015-09-29 12:27:25163 deps_content = deps_content.replace(head, roll_to)
[email protected]96550942015-05-22 18:46:51164 with open(deps, 'wb') as f:
165 f.write(deps_content)
[email protected]c20f4702015-05-23 00:44:46166 check_call(['git', 'add', 'DEPS'], cwd=root)
[email protected]398ed342015-09-29 12:27:25167 check_call(['git', 'commit', '--quiet', '-m', msg], cwd=root)
168
169 # Pull the dependency to the right revision. This is surprising to users
170 # otherwise.
171 check_call(['git', 'checkout', '--quiet', roll_to], cwd=full_dir)
172
[email protected]96550942015-05-22 18:46:51173 print('')
174 if not reviewers:
175 print('You forgot to pass -r, make sure to insert a [email protected] line')
176 print('to the commit description before emailing.')
177 print('')
178 print('Run:')
179 print(' git cl upload --send-mail')
[email protected]03fd85b2014-06-09 23:43:33180
181
[email protected]96550942015-05-22 18:46:51182def main():
[email protected]30e5b232015-06-01 18:06:59183 parser = argparse.ArgumentParser(description=__doc__)
[email protected]398ed342015-09-29 12:27:25184 parser.add_argument(
[email protected]02d20872015-11-14 00:46:41185 '--ignore-dirty-tree', action='store_true',
186 help='Roll anyways, even if there is a diff.')
187 parser.add_argument(
[email protected]398ed342015-09-29 12:27:25188 '-r', '--reviewer',
[email protected]96550942015-05-22 18:46:51189 help='To specify multiple reviewers, use comma separated list, e.g. '
[email protected]c20f4702015-05-23 00:44:46190 '-r joe,jane,john. Defaults to @chromium.org')
[email protected]398ed342015-09-29 12:27:25191 parser.add_argument('-b', '--bug', help='Associate a bug number to the roll')
192 parser.add_argument(
193 '--no-log', action='store_true',
194 help='Do not include the short log in the commit message')
195 parser.add_argument(
196 '--log-limit', type=int, default=100,
197 help='Trim log after N commits (default: %(default)s)')
198 parser.add_argument(
199 '--roll-to', default='origin/master',
200 help='Specify the new commit to roll to (default: %(default)s)')
201 parser.add_argument('dep_path', help='Path to dependency')
[email protected]30e5b232015-06-01 18:06:59202 parser.add_argument('key', nargs='?',
[email protected]398ed342015-09-29 12:27:25203 help='Regexp for dependency in DEPS file')
[email protected]30e5b232015-06-01 18:06:59204 args = parser.parse_args()
[email protected]96550942015-05-22 18:46:51205
206 reviewers = None
[email protected]30e5b232015-06-01 18:06:59207 if args.reviewer:
208 reviewers = args.reviewer.split(',')
[email protected]96550942015-05-22 18:46:51209 for i, r in enumerate(reviewers):
210 if not '@' in r:
211 reviewers[i] = r + '@chromium.org'
212
[email protected]e5d984b2015-05-29 22:09:39213 try:
214 roll(
215 os.getcwd(),
[email protected]261fa7d2015-11-23 19:20:09216 args.dep_path.rstrip('/').rstrip('\\'),
[email protected]398ed342015-09-29 12:27:25217 args.roll_to,
[email protected]30e5b232015-06-01 18:06:59218 args.key,
[email protected]398ed342015-09-29 12:27:25219 reviewers,
220 args.bug,
221 args.no_log,
[email protected]02d20872015-11-14 00:46:41222 args.log_limit,
223 args.ignore_dirty_tree)
[email protected]e5d984b2015-05-29 22:09:39224
225 except Error as e:
226 sys.stderr.write('error: %s\n' % e)
smut7036c4f2016-06-09 21:28:48227 return 2 if isinstance(e, AlreadyRolledError) else 1
[email protected]e5d984b2015-05-29 22:09:39228
229 return 0
[email protected]03fd85b2014-06-09 23:43:33230
[email protected]98201122015-04-22 20:21:34231
[email protected]03fd85b2014-06-09 23:43:33232if __name__ == '__main__':
[email protected]96550942015-05-22 18:46:51233 sys.exit(main())