blob: 82331a8047b950aaa5e59ed79e302733eca8542d [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
Edward Lesmesa1df57c2018-04-04 00:00:0713import collections
Edward Lesmesc772cf72018-04-03 18:47:3014import gclient_eval
[email protected]03fd85b2014-06-09 23:43:3315import os
16import re
[email protected]96550942015-05-22 18:46:5117import subprocess
[email protected]03fd85b2014-06-09 23:43:3318import sys
19
[email protected]c20f4702015-05-23 00:44:4620NEED_SHELL = sys.platform.startswith('win')
21
22
[email protected]e5d984b2015-05-29 22:09:3923class Error(Exception):
24 pass
25
26
smut7036c4f2016-06-09 21:28:4827class AlreadyRolledError(Error):
28 pass
29
30
[email protected]c20f4702015-05-23 00:44:4631def check_output(*args, **kwargs):
32 """subprocess.check_output() passing shell=True on Windows for git."""
33 kwargs.setdefault('shell', NEED_SHELL)
34 return subprocess.check_output(*args, **kwargs)
35
36
37def check_call(*args, **kwargs):
38 """subprocess.check_call() passing shell=True on Windows for git."""
39 kwargs.setdefault('shell', NEED_SHELL)
40 subprocess.check_call(*args, **kwargs)
41
42
[email protected]96550942015-05-22 18:46:5143def is_pristine(root, merge_base='origin/master'):
44 """Returns True if a git checkout is pristine."""
45 cmd = ['git', 'diff', '--ignore-submodules', merge_base]
[email protected]c20f4702015-05-23 00:44:4646 return not (check_output(cmd, cwd=root).strip() or
47 check_output(cmd + ['--cached'], cwd=root).strip())
[email protected]03fd85b2014-06-09 23:43:3348
49
[email protected]398ed342015-09-29 12:27:2550def get_log_url(upstream_url, head, master):
51 """Returns an URL to read logs via a Web UI if applicable."""
52 if re.match(r'https://[^/]*\.googlesource\.com/', upstream_url):
53 # gitiles
54 return '%s/+log/%s..%s' % (upstream_url, head[:12], master[:12])
55 if upstream_url.startswith('https://github.com/'):
56 upstream_url = upstream_url.rstrip('/')
57 if upstream_url.endswith('.git'):
58 upstream_url = upstream_url[:-len('.git')]
59 return '%s/compare/%s...%s' % (upstream_url, head[:12], master[:12])
60 return None
61
62
63def should_show_log(upstream_url):
64 """Returns True if a short log should be included in the tree."""
65 # Skip logs for very active projects.
Eric Boren07efc5a2017-08-28 13:13:2066 if upstream_url.endswith('/v8/v8.git'):
[email protected]398ed342015-09-29 12:27:2567 return False
68 if 'webrtc' in upstream_url:
69 return False
70 return True
71
72
Edward Lesmes3f277fc2018-04-06 19:32:5173def get_gclient_root():
Eric Boren5888d6f2018-04-09 17:20:3974 gclient = os.path.join(
75 os.path.dirname(os.path.abspath(__file__)), 'gclient.py')
76 return check_output([sys.executable, gclient, 'root']).strip()
Edward Lesmes3f277fc2018-04-06 19:32:5177
78
Marc-Antoine Ruel85a8c102017-12-12 20:42:2579def get_deps(root):
80 """Returns the path and the content of the DEPS file."""
81 deps_path = os.path.join(root, 'DEPS')
[email protected]03fd85b2014-06-09 23:43:3382 try:
Marc-Antoine Ruel85a8c102017-12-12 20:42:2583 with open(deps_path, 'rb') as f:
[email protected]96550942015-05-22 18:46:5184 deps_content = f.read()
[email protected]a7a229f2015-05-22 21:34:4685 except (IOError, OSError):
[email protected]e5d984b2015-05-29 22:09:3986 raise Error('Ensure the script is run in the directory '
87 'containing DEPS file.')
Marc-Antoine Ruel85a8c102017-12-12 20:42:2588 return deps_path, deps_content
[email protected]98201122015-04-22 20:21:3489
[email protected]96550942015-05-22 18:46:5190
Marc-Antoine Ruel85a8c102017-12-12 20:42:2591def generate_commit_message(
92 full_dir, dependency, head, roll_to, no_log, log_limit):
93 """Creates the commit message for this specific roll."""
[email protected]398ed342015-09-29 12:27:2594 commit_range = '%s..%s' % (head[:9], roll_to[:9])
[email protected]398ed342015-09-29 12:27:2595 upstream_url = check_output(
96 ['git', 'config', 'remote.origin.url'], cwd=full_dir).strip()
97 log_url = get_log_url(upstream_url, head, roll_to)
Marc-Antoine Ruel85a8c102017-12-12 20:42:2598 cmd = ['git', 'log', commit_range, '--date=short', '--no-merges']
[email protected]c6e39fe2015-09-23 13:45:5299 logs = check_output(
[email protected]64f2fc32015-10-07 19:11:17100 cmd + ['--format=%ad %ae %s'], # Args with '=' are automatically quoted.
Marc-Antoine Ruel51104fe2017-03-01 22:57:41101 cwd=full_dir).rstrip()
[email protected]c6e39fe2015-09-23 13:45:52102 logs = re.sub(r'(?m)^(\d\d\d\d-\d\d-\d\d [^@]+)@[^ ]+( .*)$', r'\1\2', logs)
Marc-Antoine Ruel51104fe2017-03-01 22:57:41103 lines = logs.splitlines()
104 cleaned_lines = [
105 l for l in lines
106 if not l.endswith('recipe-roller Roll recipe dependencies (trivial).')
107 ]
108 logs = '\n'.join(cleaned_lines) + '\n'
Marc-Antoine Ruel85a8c102017-12-12 20:42:25109
Marc-Antoine Ruel51104fe2017-03-01 22:57:41110 nb_commits = len(lines)
111 rolls = nb_commits - len(cleaned_lines)
Marc-Antoine Ruel51104fe2017-03-01 22:57:41112 header = 'Roll %s/ %s (%d commit%s%s)\n\n' % (
Marc-Antoine Ruel85a8c102017-12-12 20:42:25113 dependency,
[email protected]398ed342015-09-29 12:27:25114 commit_range,
115 nb_commits,
Marc-Antoine Ruel51104fe2017-03-01 22:57:41116 's' if nb_commits > 1 else '',
117 ('; %s trivial rolls' % rolls) if rolls else '')
[email protected]398ed342015-09-29 12:27:25118 log_section = ''
119 if log_url:
120 log_section = log_url + '\n\n'
[email protected]64f2fc32015-10-07 19:11:17121 log_section += '$ %s ' % ' '.join(cmd)
122 log_section += '--format=\'%ad %ae %s\'\n'
Eric Boren3be96a82017-09-29 14:07:46123 # It is important that --no-log continues to work, as it is used by
124 # internal -> external rollers. Please do not remove or break it.
[email protected]398ed342015-09-29 12:27:25125 if not no_log and should_show_log(upstream_url):
Marc-Antoine Ruel51104fe2017-03-01 22:57:41126 if len(cleaned_lines) > log_limit:
[email protected]398ed342015-09-29 12:27:25127 # Keep the first N log entries.
128 logs = ''.join(logs.splitlines(True)[:log_limit]) + '(...)\n'
129 log_section += logs
Marc-Antoine Ruel85a8c102017-12-12 20:42:25130 return header + log_section
[email protected]398ed342015-09-29 12:27:25131
[email protected]96550942015-05-22 18:46:51132
Edward Lesmesc772cf72018-04-03 18:47:30133def calculate_roll(full_dir, dependency, gclient_dict, roll_to):
134 """Calculates the roll for a dependency by processing gclient_dict, and
Marc-Antoine Ruel85a8c102017-12-12 20:42:25135 fetching the dependency via git.
136 """
Edward Lesmes58330b02018-04-06 23:53:55137 head = gclient_eval.GetRevision(gclient_dict, dependency)
Edward Lesmesc772cf72018-04-03 18:47:30138 if not head:
139 raise Error('%s is unpinned.' % dependency)
Marc-Antoine Ruel85a8c102017-12-12 20:42:25140 check_call(['git', 'fetch', 'origin', '--quiet'], cwd=full_dir)
141 roll_to = check_output(['git', 'rev-parse', roll_to], cwd=full_dir).strip()
142 return head, roll_to
143
144
145def gen_commit_msg(logs, cmdline, rolls, reviewers, bug):
146 """Returns the final commit message."""
147 commit_msg = ''
148 if len(logs) > 1:
149 commit_msg = 'Rolling %d dependencies\n\n' % len(logs)
150 commit_msg += '\n\n'.join(logs)
Kenneth Russellebe839b2017-12-22 22:55:39151 commit_msg += '\nCreated with:\n ' + cmdline + '\n'
Marc-Antoine Ruel85a8c102017-12-12 20:42:25152 commit_msg += 'R=%s\n' % ','.join(reviewers) if reviewers else ''
153 commit_msg += 'BUG=%s\n' % bug if bug else ''
154 return commit_msg
155
156
Edward Lesmes3f277fc2018-04-06 19:32:51157def finalize(commit_msg, deps_path, deps_content, rolls, is_relative, root_dir):
Marc-Antoine Ruel85a8c102017-12-12 20:42:25158 """Edits the DEPS file, commits it, then uploads a CL."""
[email protected]96550942015-05-22 18:46:51159 print('Commit message:')
Marc-Antoine Ruel85a8c102017-12-12 20:42:25160 print('\n'.join(' ' + i for i in commit_msg.splitlines()))
161
162 with open(deps_path, 'wb') as f:
[email protected]96550942015-05-22 18:46:51163 f.write(deps_content)
Edward Lesmes3f277fc2018-04-06 19:32:51164 current_dir = os.path.dirname(deps_path)
165 check_call(['git', 'add', 'DEPS'], cwd=current_dir)
166 check_call(['git', 'commit', '--quiet', '-m', commit_msg], cwd=current_dir)
[email protected]398ed342015-09-29 12:27:25167
168 # Pull the dependency to the right revision. This is surprising to users
169 # otherwise.
Marc-Antoine Ruel85a8c102017-12-12 20:42:25170 for dependency, (_head, roll_to) in sorted(rolls.iteritems()):
Edward Lesmes3f277fc2018-04-06 19:32:51171 full_dir = os.path.normpath(os.path.join(root_dir, dependency))
Marc-Antoine Ruel85a8c102017-12-12 20:42:25172 check_call(['git', 'checkout', '--quiet', roll_to], cwd=full_dir)
[email protected]03fd85b2014-06-09 23:43:33173
174
[email protected]96550942015-05-22 18:46:51175def main():
[email protected]30e5b232015-06-01 18:06:59176 parser = argparse.ArgumentParser(description=__doc__)
[email protected]398ed342015-09-29 12:27:25177 parser.add_argument(
[email protected]02d20872015-11-14 00:46:41178 '--ignore-dirty-tree', action='store_true',
179 help='Roll anyways, even if there is a diff.')
180 parser.add_argument(
[email protected]398ed342015-09-29 12:27:25181 '-r', '--reviewer',
[email protected]96550942015-05-22 18:46:51182 help='To specify multiple reviewers, use comma separated list, e.g. '
[email protected]c20f4702015-05-23 00:44:46183 '-r joe,jane,john. Defaults to @chromium.org')
[email protected]398ed342015-09-29 12:27:25184 parser.add_argument('-b', '--bug', help='Associate a bug number to the roll')
Eric Boren3be96a82017-09-29 14:07:46185 # It is important that --no-log continues to work, as it is used by
186 # internal -> external rollers. Please do not remove or break it.
[email protected]398ed342015-09-29 12:27:25187 parser.add_argument(
188 '--no-log', action='store_true',
189 help='Do not include the short log in the commit message')
190 parser.add_argument(
191 '--log-limit', type=int, default=100,
192 help='Trim log after N commits (default: %(default)s)')
193 parser.add_argument(
194 '--roll-to', default='origin/master',
195 help='Specify the new commit to roll to (default: %(default)s)')
Marc-Antoine Ruel85a8c102017-12-12 20:42:25196 parser.add_argument(
197 '--key', action='append', default=[],
198 help='Regex(es) for dependency in DEPS file')
199 parser.add_argument('dep_path', nargs='+', help='Path(s) to dependency')
[email protected]30e5b232015-06-01 18:06:59200 args = parser.parse_args()
[email protected]96550942015-05-22 18:46:51201
Marc-Antoine Ruel85a8c102017-12-12 20:42:25202 if len(args.dep_path) > 1:
203 if args.roll_to != 'origin/master':
204 parser.error(
205 'Can\'t use multiple paths to roll simultaneously and --roll-to')
206 if args.key:
207 parser.error(
208 'Can\'t use multiple paths to roll simultaneously and --key')
[email protected]96550942015-05-22 18:46:51209 reviewers = None
[email protected]30e5b232015-06-01 18:06:59210 if args.reviewer:
211 reviewers = args.reviewer.split(',')
[email protected]96550942015-05-22 18:46:51212 for i, r in enumerate(reviewers):
213 if not '@' in r:
214 reviewers[i] = r + '@chromium.org'
215
Edward Lesmes3f277fc2018-04-06 19:32:51216 gclient_root = get_gclient_root()
217 current_dir = os.getcwd()
Marc-Antoine Ruel85a8c102017-12-12 20:42:25218 dependencies = sorted(d.rstrip('/').rstrip('\\') for d in args.dep_path)
219 cmdline = 'roll-dep ' + ' '.join(dependencies) + ''.join(
220 ' --key ' + k for k in args.key)
[email protected]e5d984b2015-05-29 22:09:39221 try:
Edward Lesmes3f277fc2018-04-06 19:32:51222 if not args.ignore_dirty_tree and not is_pristine(current_dir):
223 raise Error(
224 'Ensure %s is clean first (no non-merged commits).' % current_dir)
Marc-Antoine Ruel85a8c102017-12-12 20:42:25225 # First gather all the information without modifying anything, except for a
226 # git fetch.
Edward Lesmes3f277fc2018-04-06 19:32:51227 deps_path, deps_content = get_deps(current_dir)
Edward Lemure05f18d2018-06-08 17:36:53228 gclient_dict = gclient_eval.Exec(deps_content, deps_path)
Eric Boren0eb39562018-04-05 15:34:09229 is_relative = gclient_dict.get('use_relative_paths', False)
Edward Lesmes3f277fc2018-04-06 19:32:51230 root_dir = current_dir if is_relative else gclient_root
Marc-Antoine Ruel85a8c102017-12-12 20:42:25231 rolls = {}
232 for dependency in dependencies:
Edward Lesmes3f277fc2018-04-06 19:32:51233 full_dir = os.path.normpath(os.path.join(root_dir, dependency))
Marc-Antoine Ruel85a8c102017-12-12 20:42:25234 if not os.path.isdir(full_dir):
235 raise Error('Directory not found: %s (%s)' % (dependency, full_dir))
236 head, roll_to = calculate_roll(
Edward Lesmesc772cf72018-04-03 18:47:30237 full_dir, dependency, gclient_dict, args.roll_to)
Marc-Antoine Ruel85a8c102017-12-12 20:42:25238 if roll_to == head:
239 if len(dependencies) == 1:
240 raise AlreadyRolledError('No revision to roll!')
241 print('%s: Already at latest commit %s' % (dependency, roll_to))
242 else:
243 print(
244 '%s: Rolling from %s to %s' % (dependency, head[:10], roll_to[:10]))
245 rolls[dependency] = (head, roll_to)
[email protected]e5d984b2015-05-29 22:09:39246
Marc-Antoine Ruel85a8c102017-12-12 20:42:25247 logs = []
248 for dependency, (head, roll_to) in sorted(rolls.iteritems()):
Edward Lesmes3f277fc2018-04-06 19:32:51249 full_dir = os.path.normpath(os.path.join(root_dir, dependency))
Marc-Antoine Ruel85a8c102017-12-12 20:42:25250 log = generate_commit_message(
251 full_dir, dependency, head, roll_to, args.no_log, args.log_limit)
252 logs.append(log)
Edward Lesmesc772cf72018-04-03 18:47:30253 gclient_eval.SetRevision(gclient_dict, dependency, roll_to)
254
255 deps_content = gclient_eval.RenderDEPSFile(gclient_dict)
Marc-Antoine Ruel85a8c102017-12-12 20:42:25256
257 commit_msg = gen_commit_msg(logs, cmdline, rolls, reviewers, args.bug)
Edward Lesmes3f277fc2018-04-06 19:32:51258 finalize(commit_msg, deps_path, deps_content, rolls, is_relative, root_dir)
[email protected]e5d984b2015-05-29 22:09:39259 except Error as e:
260 sys.stderr.write('error: %s\n' % e)
smut7036c4f2016-06-09 21:28:48261 return 2 if isinstance(e, AlreadyRolledError) else 1
[email protected]e5d984b2015-05-29 22:09:39262
Marc-Antoine Ruel85a8c102017-12-12 20:42:25263 print('')
264 if not reviewers:
265 print('You forgot to pass -r, make sure to insert a [email protected] line')
266 print('to the commit description before emailing.')
267 print('')
268 print('Run:')
269 print(' git cl upload --send-mail')
[email protected]e5d984b2015-05-29 22:09:39270 return 0
[email protected]03fd85b2014-06-09 23:43:33271
[email protected]98201122015-04-22 20:21:34272
[email protected]03fd85b2014-06-09 23:43:33273if __name__ == '__main__':
[email protected]96550942015-05-22 18:46:51274 sys.exit(main())