szager@chromium.org | 03fd85b | 2014-06-09 23:43:33 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 2 | # Copyright 2015 The Chromium Authors. All rights reserved. |
szager@chromium.org | 03fd85b | 2014-06-09 23:43:33 +0000 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 6 | """Rolls DEPS controlled dependency. |
szager@chromium.org | 03fd85b | 2014-06-09 23:43:33 +0000 | [diff] [blame] | 7 | |
sbc@chromium.org | 30e5b23 | 2015-06-01 18:06:59 +0000 | [diff] [blame] | 8 | Works only with git checkout and git dependencies. Currently this |
| 9 | script will always roll to the tip of to origin/master. |
szager@chromium.org | 03fd85b | 2014-06-09 23:43:33 +0000 | [diff] [blame] | 10 | """ |
| 11 | |
sbc@chromium.org | 30e5b23 | 2015-06-01 18:06:59 +0000 | [diff] [blame] | 12 | import argparse |
szager@chromium.org | 03fd85b | 2014-06-09 23:43:33 +0000 | [diff] [blame] | 13 | import os |
| 14 | import re |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 15 | import subprocess |
szager@chromium.org | 03fd85b | 2014-06-09 23:43:33 +0000 | [diff] [blame] | 16 | import sys |
| 17 | |
scottmg@chromium.org | c20f470 | 2015-05-23 00:44:46 +0000 | [diff] [blame] | 18 | NEED_SHELL = sys.platform.startswith('win') |
| 19 | |
| 20 | |
sbc@chromium.org | e5d984b | 2015-05-29 22:09:39 +0000 | [diff] [blame] | 21 | class Error(Exception): |
| 22 | pass |
| 23 | |
| 24 | |
smut | 7036c4f | 2016-06-09 14:28:48 -0700 | [diff] [blame] | 25 | class AlreadyRolledError(Error): |
| 26 | pass |
| 27 | |
| 28 | |
scottmg@chromium.org | c20f470 | 2015-05-23 00:44:46 +0000 | [diff] [blame] | 29 | def 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 | |
| 35 | def 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 | |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 41 | def is_pristine(root, merge_base='origin/master'): |
| 42 | """Returns True if a git checkout is pristine.""" |
| 43 | cmd = ['git', 'diff', '--ignore-submodules', merge_base] |
scottmg@chromium.org | c20f470 | 2015-05-23 00:44:46 +0000 | [diff] [blame] | 44 | return not (check_output(cmd, cwd=root).strip() or |
| 45 | check_output(cmd + ['--cached'], cwd=root).strip()) |
szager@chromium.org | 03fd85b | 2014-06-09 23:43:33 +0000 | [diff] [blame] | 46 | |
| 47 | |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 48 | def 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 | |
| 61 | def 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. |
Eric Boren | 07efc5a | 2017-08-28 09:13:20 -0400 | [diff] [blame] | 64 | if upstream_url.endswith('/v8/v8.git'): |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 65 | return False |
| 66 | if 'webrtc' in upstream_url: |
| 67 | return False |
| 68 | return True |
| 69 | |
| 70 | |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 71 | def get_deps(root): |
| 72 | """Returns the path and the content of the DEPS file.""" |
| 73 | deps_path = os.path.join(root, 'DEPS') |
szager@chromium.org | 03fd85b | 2014-06-09 23:43:33 +0000 | [diff] [blame] | 74 | try: |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 75 | with open(deps_path, 'rb') as f: |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 76 | deps_content = f.read() |
maruel@chromium.org | a7a229f | 2015-05-22 21:34:46 +0000 | [diff] [blame] | 77 | except (IOError, OSError): |
sbc@chromium.org | e5d984b | 2015-05-29 22:09:39 +0000 | [diff] [blame] | 78 | raise Error('Ensure the script is run in the directory ' |
| 79 | 'containing DEPS file.') |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 80 | return deps_path, deps_content |
sbc@chromium.org | 9820112 | 2015-04-22 20:21:34 +0000 | [diff] [blame] | 81 | |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 82 | |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 83 | def generate_commit_message( |
| 84 | full_dir, dependency, head, roll_to, no_log, log_limit): |
| 85 | """Creates the commit message for this specific roll.""" |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 86 | commit_range = '%s..%s' % (head[:9], roll_to[:9]) |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 87 | upstream_url = check_output( |
| 88 | ['git', 'config', 'remote.origin.url'], cwd=full_dir).strip() |
| 89 | log_url = get_log_url(upstream_url, head, roll_to) |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 90 | cmd = ['git', 'log', commit_range, '--date=short', '--no-merges'] |
maruel@chromium.org | c6e39fe | 2015-09-23 13:45:52 +0000 | [diff] [blame] | 91 | logs = check_output( |
johannkoenig@google.com | 64f2fc3 | 2015-10-07 19:11:17 +0000 | [diff] [blame] | 92 | cmd + ['--format=%ad %ae %s'], # Args with '=' are automatically quoted. |
Marc-Antoine Ruel | 51104fe | 2017-03-01 17:57:41 -0500 | [diff] [blame] | 93 | cwd=full_dir).rstrip() |
maruel@chromium.org | c6e39fe | 2015-09-23 13:45:52 +0000 | [diff] [blame] | 94 | logs = re.sub(r'(?m)^(\d\d\d\d-\d\d-\d\d [^@]+)@[^ ]+( .*)$', r'\1\2', logs) |
Marc-Antoine Ruel | 51104fe | 2017-03-01 17:57:41 -0500 | [diff] [blame] | 95 | lines = logs.splitlines() |
| 96 | cleaned_lines = [ |
| 97 | l for l in lines |
| 98 | if not l.endswith('recipe-roller Roll recipe dependencies (trivial).') |
| 99 | ] |
| 100 | logs = '\n'.join(cleaned_lines) + '\n' |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 101 | |
Marc-Antoine Ruel | 51104fe | 2017-03-01 17:57:41 -0500 | [diff] [blame] | 102 | nb_commits = len(lines) |
| 103 | rolls = nb_commits - len(cleaned_lines) |
Marc-Antoine Ruel | 51104fe | 2017-03-01 17:57:41 -0500 | [diff] [blame] | 104 | header = 'Roll %s/ %s (%d commit%s%s)\n\n' % ( |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 105 | dependency, |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 106 | commit_range, |
| 107 | nb_commits, |
Marc-Antoine Ruel | 51104fe | 2017-03-01 17:57:41 -0500 | [diff] [blame] | 108 | 's' if nb_commits > 1 else '', |
| 109 | ('; %s trivial rolls' % rolls) if rolls else '') |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 110 | log_section = '' |
| 111 | if log_url: |
| 112 | log_section = log_url + '\n\n' |
johannkoenig@google.com | 64f2fc3 | 2015-10-07 19:11:17 +0000 | [diff] [blame] | 113 | log_section += '$ %s ' % ' '.join(cmd) |
| 114 | log_section += '--format=\'%ad %ae %s\'\n' |
Eric Boren | 3be96a8 | 2017-09-29 10:07:46 -0400 | [diff] [blame] | 115 | # It is important that --no-log continues to work, as it is used by |
| 116 | # internal -> external rollers. Please do not remove or break it. |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 117 | if not no_log and should_show_log(upstream_url): |
Marc-Antoine Ruel | 51104fe | 2017-03-01 17:57:41 -0500 | [diff] [blame] | 118 | if len(cleaned_lines) > log_limit: |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 119 | # Keep the first N log entries. |
| 120 | logs = ''.join(logs.splitlines(True)[:log_limit]) + '(...)\n' |
| 121 | log_section += logs |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 122 | return header + log_section |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 123 | |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 124 | |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 125 | def calculate_roll(full_dir, dependency, deps_content, roll_to, key): |
| 126 | """Calculates the roll for a dependency by processing deps_content, and |
| 127 | fetching the dependency via git. |
| 128 | """ |
| 129 | head = check_output(['git', 'rev-parse', 'HEAD'], cwd=full_dir).strip() |
| 130 | if not head in deps_content: |
| 131 | print('Warning: %s is not checked out at the expected revision in DEPS' % |
| 132 | dependency) |
| 133 | if not key: |
| 134 | key = dependency |
| 135 | |
| 136 | # It happens if the user checked out a branch in the dependency by himself. |
| 137 | # Fall back to reading the DEPS to figure out the original commit. |
| 138 | for i in deps_content.splitlines(): |
| 139 | m = re.match(r'\s+"' + key + '":.*"([a-z0-9]{40})",', i) |
| 140 | if m: |
| 141 | head = m.group(1) |
| 142 | break |
| 143 | else: |
| 144 | raise Error('Expected to find commit %s for %s in DEPS' % (head, key)) |
| 145 | |
| 146 | check_call(['git', 'fetch', 'origin', '--quiet'], cwd=full_dir) |
| 147 | roll_to = check_output(['git', 'rev-parse', roll_to], cwd=full_dir).strip() |
| 148 | return head, roll_to |
| 149 | |
| 150 | |
| 151 | def gen_commit_msg(logs, cmdline, rolls, reviewers, bug): |
| 152 | """Returns the final commit message.""" |
| 153 | commit_msg = '' |
| 154 | if len(logs) > 1: |
| 155 | commit_msg = 'Rolling %d dependencies\n\n' % len(logs) |
| 156 | commit_msg += '\n\n'.join(logs) |
Kenneth Russell | ebe839b | 2017-12-22 14:55:39 -0800 | [diff] [blame] | 157 | commit_msg += '\nCreated with:\n ' + cmdline + '\n' |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 158 | commit_msg += 'R=%s\n' % ','.join(reviewers) if reviewers else '' |
| 159 | commit_msg += 'BUG=%s\n' % bug if bug else '' |
| 160 | return commit_msg |
| 161 | |
| 162 | |
| 163 | def finalize(commit_msg, deps_path, deps_content, rolls): |
| 164 | """Edits the DEPS file, commits it, then uploads a CL.""" |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 165 | print('Commit message:') |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 166 | print('\n'.join(' ' + i for i in commit_msg.splitlines())) |
| 167 | |
| 168 | with open(deps_path, 'wb') as f: |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 169 | f.write(deps_content) |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 170 | root = os.path.dirname(deps_path) |
scottmg@chromium.org | c20f470 | 2015-05-23 00:44:46 +0000 | [diff] [blame] | 171 | check_call(['git', 'add', 'DEPS'], cwd=root) |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 172 | check_call(['git', 'commit', '--quiet', '-m', commit_msg], cwd=root) |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 173 | |
| 174 | # Pull the dependency to the right revision. This is surprising to users |
| 175 | # otherwise. |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 176 | for dependency, (_head, roll_to) in sorted(rolls.iteritems()): |
| 177 | full_dir = os.path.normpath( |
| 178 | os.path.join(os.path.dirname(root), dependency)) |
| 179 | check_call(['git', 'checkout', '--quiet', roll_to], cwd=full_dir) |
szager@chromium.org | 03fd85b | 2014-06-09 23:43:33 +0000 | [diff] [blame] | 180 | |
| 181 | |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 182 | def main(): |
sbc@chromium.org | 30e5b23 | 2015-06-01 18:06:59 +0000 | [diff] [blame] | 183 | parser = argparse.ArgumentParser(description=__doc__) |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 184 | parser.add_argument( |
smut@google.com | 02d2087 | 2015-11-14 00:46:41 +0000 | [diff] [blame] | 185 | '--ignore-dirty-tree', action='store_true', |
| 186 | help='Roll anyways, even if there is a diff.') |
| 187 | parser.add_argument( |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 188 | '-r', '--reviewer', |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 189 | help='To specify multiple reviewers, use comma separated list, e.g. ' |
scottmg@chromium.org | c20f470 | 2015-05-23 00:44:46 +0000 | [diff] [blame] | 190 | '-r joe,jane,john. Defaults to @chromium.org') |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 191 | parser.add_argument('-b', '--bug', help='Associate a bug number to the roll') |
Eric Boren | 3be96a8 | 2017-09-29 10:07:46 -0400 | [diff] [blame] | 192 | # It is important that --no-log continues to work, as it is used by |
| 193 | # internal -> external rollers. Please do not remove or break it. |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 194 | parser.add_argument( |
| 195 | '--no-log', action='store_true', |
| 196 | help='Do not include the short log in the commit message') |
| 197 | parser.add_argument( |
| 198 | '--log-limit', type=int, default=100, |
| 199 | help='Trim log after N commits (default: %(default)s)') |
| 200 | parser.add_argument( |
| 201 | '--roll-to', default='origin/master', |
| 202 | help='Specify the new commit to roll to (default: %(default)s)') |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 203 | parser.add_argument( |
| 204 | '--key', action='append', default=[], |
| 205 | help='Regex(es) for dependency in DEPS file') |
| 206 | parser.add_argument('dep_path', nargs='+', help='Path(s) to dependency') |
sbc@chromium.org | 30e5b23 | 2015-06-01 18:06:59 +0000 | [diff] [blame] | 207 | args = parser.parse_args() |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 208 | |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 209 | if len(args.dep_path) > 1: |
| 210 | if args.roll_to != 'origin/master': |
| 211 | parser.error( |
| 212 | 'Can\'t use multiple paths to roll simultaneously and --roll-to') |
| 213 | if args.key: |
| 214 | parser.error( |
| 215 | 'Can\'t use multiple paths to roll simultaneously and --key') |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 216 | reviewers = None |
sbc@chromium.org | 30e5b23 | 2015-06-01 18:06:59 +0000 | [diff] [blame] | 217 | if args.reviewer: |
| 218 | reviewers = args.reviewer.split(',') |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 219 | for i, r in enumerate(reviewers): |
| 220 | if not '@' in r: |
| 221 | reviewers[i] = r + '@chromium.org' |
| 222 | |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 223 | root = os.getcwd() |
| 224 | dependencies = sorted(d.rstrip('/').rstrip('\\') for d in args.dep_path) |
| 225 | cmdline = 'roll-dep ' + ' '.join(dependencies) + ''.join( |
| 226 | ' --key ' + k for k in args.key) |
sbc@chromium.org | e5d984b | 2015-05-29 22:09:39 +0000 | [diff] [blame] | 227 | try: |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 228 | if not args.ignore_dirty_tree and not is_pristine(root): |
| 229 | raise Error('Ensure %s is clean first (no non-merged commits).' % root) |
| 230 | # First gather all the information without modifying anything, except for a |
| 231 | # git fetch. |
| 232 | deps_path, deps_content = get_deps(root) |
| 233 | rolls = {} |
| 234 | for dependency in dependencies: |
| 235 | full_dir = os.path.normpath( |
| 236 | os.path.join(os.path.dirname(root), dependency)) |
| 237 | if not os.path.isdir(full_dir): |
| 238 | raise Error('Directory not found: %s (%s)' % (dependency, full_dir)) |
| 239 | head, roll_to = calculate_roll( |
| 240 | full_dir, dependency, deps_content, args.roll_to, args.key) |
| 241 | if roll_to == head: |
| 242 | if len(dependencies) == 1: |
| 243 | raise AlreadyRolledError('No revision to roll!') |
| 244 | print('%s: Already at latest commit %s' % (dependency, roll_to)) |
| 245 | else: |
| 246 | print( |
| 247 | '%s: Rolling from %s to %s' % (dependency, head[:10], roll_to[:10])) |
| 248 | rolls[dependency] = (head, roll_to) |
sbc@chromium.org | e5d984b | 2015-05-29 22:09:39 +0000 | [diff] [blame] | 249 | |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 250 | logs = [] |
| 251 | for dependency, (head, roll_to) in sorted(rolls.iteritems()): |
| 252 | full_dir = os.path.normpath( |
| 253 | os.path.join(os.path.dirname(root), dependency)) |
| 254 | log = generate_commit_message( |
| 255 | full_dir, dependency, head, roll_to, args.no_log, args.log_limit) |
| 256 | logs.append(log) |
| 257 | deps_content = deps_content.replace(head, roll_to) |
| 258 | |
| 259 | commit_msg = gen_commit_msg(logs, cmdline, rolls, reviewers, args.bug) |
| 260 | finalize(commit_msg, deps_path, deps_content, rolls) |
sbc@chromium.org | e5d984b | 2015-05-29 22:09:39 +0000 | [diff] [blame] | 261 | except Error as e: |
| 262 | sys.stderr.write('error: %s\n' % e) |
smut | 7036c4f | 2016-06-09 14:28:48 -0700 | [diff] [blame] | 263 | return 2 if isinstance(e, AlreadyRolledError) else 1 |
sbc@chromium.org | e5d984b | 2015-05-29 22:09:39 +0000 | [diff] [blame] | 264 | |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 265 | print('') |
| 266 | if not reviewers: |
| 267 | print('You forgot to pass -r, make sure to insert a R=foo@example.com line') |
| 268 | print('to the commit description before emailing.') |
| 269 | print('') |
| 270 | print('Run:') |
| 271 | print(' git cl upload --send-mail') |
sbc@chromium.org | e5d984b | 2015-05-29 22:09:39 +0000 | [diff] [blame] | 272 | return 0 |
szager@chromium.org | 03fd85b | 2014-06-09 23:43:33 +0000 | [diff] [blame] | 273 | |
sbc@chromium.org | 9820112 | 2015-04-22 20:21:34 +0000 | [diff] [blame] | 274 | |
szager@chromium.org | 03fd85b | 2014-06-09 23:43:33 +0000 | [diff] [blame] | 275 | if __name__ == '__main__': |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 276 | sys.exit(main()) |