[email protected] | 03fd85b | 2014-06-09 23:43:33 | [diff] [blame] | 1 | #!/usr/bin/env python |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 2 | # Copyright 2015 The Chromium Authors. All rights reserved. |
[email protected] | 03fd85b | 2014-06-09 23:43:33 | [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 | |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 6 | """Rolls DEPS controlled dependency. |
[email protected] | 03fd85b | 2014-06-09 23:43:33 | [diff] [blame] | 7 | |
[email protected] | 30e5b23 | 2015-06-01 18:06:59 | [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. |
[email protected] | 03fd85b | 2014-06-09 23:43:33 | [diff] [blame] | 10 | """ |
| 11 | |
[email protected] | 30e5b23 | 2015-06-01 18:06:59 | [diff] [blame] | 12 | import argparse |
[email protected] | 03fd85b | 2014-06-09 23:43:33 | [diff] [blame] | 13 | import os |
| 14 | import re |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 15 | import subprocess |
[email protected] | 03fd85b | 2014-06-09 23:43:33 | [diff] [blame] | 16 | import sys |
| 17 | |
[email protected] | c20f470 | 2015-05-23 00:44:46 | [diff] [blame] | 18 | NEED_SHELL = sys.platform.startswith('win') |
| 19 | |
| 20 | |
[email protected] | e5d984b | 2015-05-29 22:09:39 | [diff] [blame] | 21 | class Error(Exception): |
| 22 | pass |
| 23 | |
| 24 | |
smut | 7036c4f | 2016-06-09 21:28:48 | [diff] [blame] | 25 | class AlreadyRolledError(Error): |
| 26 | pass |
| 27 | |
| 28 | |
[email protected] | c20f470 | 2015-05-23 00:44:46 | [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 | |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [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] |
[email protected] | c20f470 | 2015-05-23 00:44:46 | [diff] [blame] | 44 | return not (check_output(cmd, cwd=root).strip() or |
| 45 | check_output(cmd + ['--cached'], cwd=root).strip()) |
[email protected] | 03fd85b | 2014-06-09 23:43:33 | [diff] [blame] | 46 | |
| 47 | |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [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. |
| 64 | if upstream_url.endswith(( |
| 65 | '/angle/angle.git', |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 66 | '/v8/v8.git')): |
| 67 | return False |
| 68 | if 'webrtc' in upstream_url: |
| 69 | return False |
| 70 | return True |
| 71 | |
| 72 | |
[email protected] | 02d2087 | 2015-11-14 00:46:41 | [diff] [blame] | 73 | def roll(root, deps_dir, roll_to, key, reviewers, bug, no_log, log_limit, |
| 74 | ignore_dirty_tree=False): |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 75 | deps = os.path.join(root, 'DEPS') |
[email protected] | 03fd85b | 2014-06-09 23:43:33 | [diff] [blame] | 76 | try: |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 77 | with open(deps, 'rb') as f: |
| 78 | deps_content = f.read() |
[email protected] | a7a229f | 2015-05-22 21:34:46 | [diff] [blame] | 79 | except (IOError, OSError): |
[email protected] | e5d984b | 2015-05-29 22:09:39 | [diff] [blame] | 80 | raise Error('Ensure the script is run in the directory ' |
| 81 | 'containing DEPS file.') |
[email protected] | 9820112 | 2015-04-22 20:21:34 | [diff] [blame] | 82 | |
[email protected] | 02d2087 | 2015-11-14 00:46:41 | [diff] [blame] | 83 | if not ignore_dirty_tree and not is_pristine(root): |
[email protected] | 8a6495c | 2015-12-07 21:46:41 | [diff] [blame] | 84 | raise Error('Ensure %s is clean first (no non-merged commits).' % root) |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 85 | |
[email protected] | c20f470 | 2015-05-23 00:44:46 | [diff] [blame] | 86 | full_dir = os.path.normpath(os.path.join(os.path.dirname(root), deps_dir)) |
[email protected] | 30e5b23 | 2015-06-01 18:06:59 | [diff] [blame] | 87 | if not os.path.isdir(full_dir): |
[email protected] | 8a6495c | 2015-12-07 21:46:41 | [diff] [blame] | 88 | raise Error('Directory not found: %s (%s)' % (deps_dir, full_dir)) |
[email protected] | e5d984b | 2015-05-29 22:09:39 | [diff] [blame] | 89 | head = check_output(['git', 'rev-parse', 'HEAD'], cwd=full_dir).strip() |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 90 | |
| 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] | e5d984b | 2015-05-29 22:09:39 | [diff] [blame] | 94 | if key is None: |
| 95 | print("Warning: no key specified. Using '%s'." % deps_dir) |
| 96 | key = deps_dir |
| 97 | |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 98 | # 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] | d4ef599 | 2016-02-18 00:05:22 | [diff] [blame] | 101 | m = re.match(r'\s+"' + key + '":.*"([a-z0-9]{40})",', i) |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 102 | if m: |
| 103 | head = m.group(1) |
| 104 | break |
| 105 | else: |
[email protected] | e5d984b | 2015-05-29 22:09:39 | [diff] [blame] | 106 | raise Error('Expected to find commit %s for %s in DEPS' % (head, key)) |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 107 | |
| 108 | print('Found old revision %s' % head) |
| 109 | |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 110 | 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] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 113 | |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 114 | if roll_to == head: |
smut | 7036c4f | 2016-06-09 21:28:48 | [diff] [blame] | 115 | raise AlreadyRolledError('No revision to roll!') |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 116 | |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 117 | commit_range = '%s..%s' % (head[:9], roll_to[:9]) |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 118 | |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 119 | 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] | 64f2fc3 | 2015-10-07 19:11:17 | [diff] [blame] | 122 | cmd = [ |
| 123 | 'git', 'log', commit_range, '--date=short', '--no-merges', |
| 124 | ] |
[email protected] | c6e39fe | 2015-09-23 13:45:52 | [diff] [blame] | 125 | logs = check_output( |
[email protected] | 64f2fc3 | 2015-10-07 19:11:17 | [diff] [blame] | 126 | cmd + ['--format=%ad %ae %s'], # Args with '=' are automatically quoted. |
Marc-Antoine Ruel | 51104fe | 2017-03-01 22:57:41 | [diff] [blame] | 127 | cwd=full_dir).rstrip() |
[email protected] | c6e39fe | 2015-09-23 13:45:52 | [diff] [blame] | 128 | logs = re.sub(r'(?m)^(\d\d\d\d-\d\d-\d\d [^@]+)@[^ ]+( .*)$', r'\1\2', logs) |
Marc-Antoine Ruel | 51104fe | 2017-03-01 22:57:41 | [diff] [blame] | 129 | 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] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 137 | |
Marc-Antoine Ruel | 51104fe | 2017-03-01 22:57:41 | [diff] [blame] | 138 | header = 'Roll %s/ %s (%d commit%s%s)\n\n' % ( |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 139 | deps_dir, |
| 140 | commit_range, |
| 141 | nb_commits, |
Marc-Antoine Ruel | 51104fe | 2017-03-01 22:57:41 | [diff] [blame] | 142 | 's' if nb_commits > 1 else '', |
| 143 | ('; %s trivial rolls' % rolls) if rolls else '') |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 144 | |
| 145 | log_section = '' |
| 146 | if log_url: |
| 147 | log_section = log_url + '\n\n' |
[email protected] | 64f2fc3 | 2015-10-07 19:11:17 | [diff] [blame] | 148 | log_section += '$ %s ' % ' '.join(cmd) |
| 149 | log_section += '--format=\'%ad %ae %s\'\n' |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 150 | if not no_log and should_show_log(upstream_url): |
Marc-Antoine Ruel | 51104fe | 2017-03-01 22:57:41 | [diff] [blame] | 151 | if len(cleaned_lines) > log_limit: |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 152 | # Keep the first N log entries. |
| 153 | logs = ''.join(logs.splitlines(True)[:log_limit]) + '(...)\n' |
| 154 | log_section += logs |
Marc-Antoine Ruel | 22e7a27 | 2017-02-08 01:09:37 | [diff] [blame] | 155 | log_section += '\n\nCreated with:\n roll-dep %s\n' % deps_dir |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 156 | |
[email protected] | c6e39fe | 2015-09-23 13:45:52 | [diff] [blame] | 157 | reviewer = 'R=%s\n' % ','.join(reviewers) if reviewers else '' |
| 158 | bug = 'BUG=%s\n' % bug if bug else '' |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 159 | msg = header + log_section + reviewer + bug |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 160 | |
| 161 | print('Commit message:') |
| 162 | print('\n'.join(' ' + i for i in msg.splitlines())) |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 163 | deps_content = deps_content.replace(head, roll_to) |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 164 | with open(deps, 'wb') as f: |
| 165 | f.write(deps_content) |
[email protected] | c20f470 | 2015-05-23 00:44:46 | [diff] [blame] | 166 | check_call(['git', 'add', 'DEPS'], cwd=root) |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 167 | 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] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 173 | 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] | 03fd85b | 2014-06-09 23:43:33 | [diff] [blame] | 180 | |
| 181 | |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 182 | def main(): |
[email protected] | 30e5b23 | 2015-06-01 18:06:59 | [diff] [blame] | 183 | parser = argparse.ArgumentParser(description=__doc__) |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 184 | parser.add_argument( |
[email protected] | 02d2087 | 2015-11-14 00:46:41 | [diff] [blame] | 185 | '--ignore-dirty-tree', action='store_true', |
| 186 | help='Roll anyways, even if there is a diff.') |
| 187 | parser.add_argument( |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 188 | '-r', '--reviewer', |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 189 | help='To specify multiple reviewers, use comma separated list, e.g. ' |
[email protected] | c20f470 | 2015-05-23 00:44:46 | [diff] [blame] | 190 | '-r joe,jane,john. Defaults to @chromium.org') |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 191 | 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] | 30e5b23 | 2015-06-01 18:06:59 | [diff] [blame] | 202 | parser.add_argument('key', nargs='?', |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 203 | help='Regexp for dependency in DEPS file') |
[email protected] | 30e5b23 | 2015-06-01 18:06:59 | [diff] [blame] | 204 | args = parser.parse_args() |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 205 | |
| 206 | reviewers = None |
[email protected] | 30e5b23 | 2015-06-01 18:06:59 | [diff] [blame] | 207 | if args.reviewer: |
| 208 | reviewers = args.reviewer.split(',') |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 209 | for i, r in enumerate(reviewers): |
| 210 | if not '@' in r: |
| 211 | reviewers[i] = r + '@chromium.org' |
| 212 | |
[email protected] | e5d984b | 2015-05-29 22:09:39 | [diff] [blame] | 213 | try: |
| 214 | roll( |
| 215 | os.getcwd(), |
[email protected] | 261fa7d | 2015-11-23 19:20:09 | [diff] [blame] | 216 | args.dep_path.rstrip('/').rstrip('\\'), |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 217 | args.roll_to, |
[email protected] | 30e5b23 | 2015-06-01 18:06:59 | [diff] [blame] | 218 | args.key, |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 219 | reviewers, |
| 220 | args.bug, |
| 221 | args.no_log, |
[email protected] | 02d2087 | 2015-11-14 00:46:41 | [diff] [blame] | 222 | args.log_limit, |
| 223 | args.ignore_dirty_tree) |
[email protected] | e5d984b | 2015-05-29 22:09:39 | [diff] [blame] | 224 | |
| 225 | except Error as e: |
| 226 | sys.stderr.write('error: %s\n' % e) |
smut | 7036c4f | 2016-06-09 21:28:48 | [diff] [blame] | 227 | return 2 if isinstance(e, AlreadyRolledError) else 1 |
[email protected] | e5d984b | 2015-05-29 22:09:39 | [diff] [blame] | 228 | |
| 229 | return 0 |
[email protected] | 03fd85b | 2014-06-09 23:43:33 | [diff] [blame] | 230 | |
[email protected] | 9820112 | 2015-04-22 20:21:34 | [diff] [blame] | 231 | |
[email protected] | 03fd85b | 2014-06-09 23:43:33 | [diff] [blame] | 232 | if __name__ == '__main__': |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 233 | sys.exit(main()) |