[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. |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 127 | cwd=full_dir) |
[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) |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 129 | 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] | 64f2fc3 | 2015-10-07 19:11:17 | [diff] [blame] | 140 | log_section += '$ %s ' % ' '.join(cmd) |
| 141 | log_section += '--format=\'%ad %ae %s\'\n' |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 142 | 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] | c6e39fe | 2015-09-23 13:45:52 | [diff] [blame] | 149 | reviewer = 'R=%s\n' % ','.join(reviewers) if reviewers else '' |
| 150 | bug = 'BUG=%s\n' % bug if bug else '' |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 151 | msg = header + log_section + reviewer + bug |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 152 | |
| 153 | print('Commit message:') |
| 154 | print('\n'.join(' ' + i for i in msg.splitlines())) |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 155 | deps_content = deps_content.replace(head, roll_to) |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 156 | with open(deps, 'wb') as f: |
| 157 | f.write(deps_content) |
[email protected] | c20f470 | 2015-05-23 00:44:46 | [diff] [blame] | 158 | check_call(['git', 'add', 'DEPS'], cwd=root) |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 159 | 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] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 165 | 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] | 03fd85b | 2014-06-09 23:43:33 | [diff] [blame] | 172 | |
| 173 | |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 174 | def main(): |
[email protected] | 30e5b23 | 2015-06-01 18:06:59 | [diff] [blame] | 175 | parser = argparse.ArgumentParser(description=__doc__) |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 176 | parser.add_argument( |
[email protected] | 02d2087 | 2015-11-14 00:46:41 | [diff] [blame] | 177 | '--ignore-dirty-tree', action='store_true', |
| 178 | help='Roll anyways, even if there is a diff.') |
| 179 | parser.add_argument( |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 180 | '-r', '--reviewer', |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 181 | help='To specify multiple reviewers, use comma separated list, e.g. ' |
[email protected] | c20f470 | 2015-05-23 00:44:46 | [diff] [blame] | 182 | '-r joe,jane,john. Defaults to @chromium.org') |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 183 | 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] | 30e5b23 | 2015-06-01 18:06:59 | [diff] [blame] | 194 | parser.add_argument('key', nargs='?', |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 195 | help='Regexp for dependency in DEPS file') |
[email protected] | 30e5b23 | 2015-06-01 18:06:59 | [diff] [blame] | 196 | args = parser.parse_args() |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 197 | |
| 198 | reviewers = None |
[email protected] | 30e5b23 | 2015-06-01 18:06:59 | [diff] [blame] | 199 | if args.reviewer: |
| 200 | reviewers = args.reviewer.split(',') |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 201 | for i, r in enumerate(reviewers): |
| 202 | if not '@' in r: |
| 203 | reviewers[i] = r + '@chromium.org' |
| 204 | |
[email protected] | e5d984b | 2015-05-29 22:09:39 | [diff] [blame] | 205 | try: |
| 206 | roll( |
| 207 | os.getcwd(), |
[email protected] | 261fa7d | 2015-11-23 19:20:09 | [diff] [blame] | 208 | args.dep_path.rstrip('/').rstrip('\\'), |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 209 | args.roll_to, |
[email protected] | 30e5b23 | 2015-06-01 18:06:59 | [diff] [blame] | 210 | args.key, |
[email protected] | 398ed34 | 2015-09-29 12:27:25 | [diff] [blame] | 211 | reviewers, |
| 212 | args.bug, |
| 213 | args.no_log, |
[email protected] | 02d2087 | 2015-11-14 00:46:41 | [diff] [blame] | 214 | args.log_limit, |
| 215 | args.ignore_dirty_tree) |
[email protected] | e5d984b | 2015-05-29 22:09:39 | [diff] [blame] | 216 | |
| 217 | except Error as e: |
| 218 | sys.stderr.write('error: %s\n' % e) |
smut | 7036c4f | 2016-06-09 21:28:48 | [diff] [blame] | 219 | return 2 if isinstance(e, AlreadyRolledError) else 1 |
[email protected] | e5d984b | 2015-05-29 22:09:39 | [diff] [blame] | 220 | |
| 221 | return 0 |
[email protected] | 03fd85b | 2014-06-09 23:43:33 | [diff] [blame] | 222 | |
[email protected] | 9820112 | 2015-04-22 20:21:34 | [diff] [blame] | 223 | |
[email protected] | 03fd85b | 2014-06-09 23:43:33 | [diff] [blame] | 224 | if __name__ == '__main__': |
[email protected] | 9655094 | 2015-05-22 18:46:51 | [diff] [blame] | 225 | sys.exit(main()) |