Josip Sokcevic | 4de5dea | 2022-03-23 21:15:14 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
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 | |
Josip Sokcevic | c1a06c1 | 2022-01-12 00:44:14 +0000 | [diff] [blame] | 8 | Works only with git checkout and git dependencies. Currently this script will |
| 9 | always roll to the tip of to origin/main. |
szager@chromium.org | 03fd85b | 2014-06-09 23:43:33 +0000 | [diff] [blame] | 10 | """ |
| 11 | |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 12 | from __future__ import print_function |
| 13 | |
sbc@chromium.org | 30e5b23 | 2015-06-01 18:06:59 +0000 | [diff] [blame] | 14 | import argparse |
Thiago Perrotta | a089281 | 2022-09-03 11:22:46 +0000 | [diff] [blame] | 15 | import itertools |
szager@chromium.org | 03fd85b | 2014-06-09 23:43:33 +0000 | [diff] [blame] | 16 | import os |
| 17 | import re |
Corentin Wallez | 5157fbf | 2020-11-12 22:31:35 +0000 | [diff] [blame] | 18 | import subprocess2 |
szager@chromium.org | 03fd85b | 2014-06-09 23:43:33 +0000 | [diff] [blame] | 19 | import sys |
Bruce Dawson | 03af44a | 2022-12-27 19:37:58 +0000 | [diff] [blame] | 20 | import tempfile |
szager@chromium.org | 03fd85b | 2014-06-09 23:43:33 +0000 | [diff] [blame] | 21 | |
scottmg@chromium.org | c20f470 | 2015-05-23 00:44:46 +0000 | [diff] [blame] | 22 | NEED_SHELL = sys.platform.startswith('win') |
Edward Lemur | faae42e | 2018-11-26 18:34:30 +0000 | [diff] [blame] | 23 | GCLIENT_PATH = os.path.join( |
| 24 | os.path.dirname(os.path.abspath(__file__)), 'gclient.py') |
scottmg@chromium.org | c20f470 | 2015-05-23 00:44:46 +0000 | [diff] [blame] | 25 | |
| 26 | |
Marc-Antoine Ruel | 1e2cb15 | 2019-04-17 17:32:52 +0000 | [diff] [blame] | 27 | # Commit subject that will be considered a roll. In the format generated by the |
| 28 | # git log used, so it's "<year>-<month>-<day> <author> <subject>" |
| 29 | _ROLL_SUBJECT = re.compile( |
| 30 | # Date |
| 31 | r'^\d\d\d\d-\d\d-\d\d ' |
| 32 | # Author |
| 33 | r'[^ ]+ ' |
| 34 | # Subject |
| 35 | r'(' |
| 36 | # Generated by |
Josip Sokcevic | 9c0dc30 | 2020-11-20 18:41:25 +0000 | [diff] [blame] | 37 | # https://skia.googlesource.com/buildbot/+/HEAdA/autoroll/go/repo_manager/deps_repo_manager.go |
Marc-Antoine Ruel | 1e2cb15 | 2019-04-17 17:32:52 +0000 | [diff] [blame] | 38 | r'Roll [^ ]+ [a-f0-9]+\.\.[a-f0-9]+ \(\d+ commits\)' |
| 39 | r'|' |
| 40 | # Generated by |
Josip Sokcevic | 9c0dc30 | 2020-11-20 18:41:25 +0000 | [diff] [blame] | 41 | # https://chromium.googlesource.com/infra/infra/+/HEAD/recipes/recipe_modules/recipe_autoroller/api.py |
Marc-Antoine Ruel | 1e2cb15 | 2019-04-17 17:32:52 +0000 | [diff] [blame] | 42 | r'Roll recipe dependencies \(trivial\)\.' |
| 43 | r')$') |
| 44 | |
| 45 | |
sbc@chromium.org | e5d984b | 2015-05-29 22:09:39 +0000 | [diff] [blame] | 46 | class Error(Exception): |
| 47 | pass |
| 48 | |
| 49 | |
smut | 7036c4f | 2016-06-09 14:28:48 -0700 | [diff] [blame] | 50 | class AlreadyRolledError(Error): |
| 51 | pass |
| 52 | |
| 53 | |
scottmg@chromium.org | c20f470 | 2015-05-23 00:44:46 +0000 | [diff] [blame] | 54 | def check_output(*args, **kwargs): |
Corentin Wallez | 8732c0e | 2020-12-09 17:43:46 +0000 | [diff] [blame] | 55 | """subprocess2.check_output() passing shell=True on Windows for git.""" |
scottmg@chromium.org | c20f470 | 2015-05-23 00:44:46 +0000 | [diff] [blame] | 56 | kwargs.setdefault('shell', NEED_SHELL) |
Corentin Wallez | 5157fbf | 2020-11-12 22:31:35 +0000 | [diff] [blame] | 57 | return subprocess2.check_output(*args, **kwargs).decode('utf-8') |
scottmg@chromium.org | c20f470 | 2015-05-23 00:44:46 +0000 | [diff] [blame] | 58 | |
| 59 | |
| 60 | def check_call(*args, **kwargs): |
Corentin Wallez | 8732c0e | 2020-12-09 17:43:46 +0000 | [diff] [blame] | 61 | """subprocess2.check_call() passing shell=True on Windows for git.""" |
scottmg@chromium.org | c20f470 | 2015-05-23 00:44:46 +0000 | [diff] [blame] | 62 | kwargs.setdefault('shell', NEED_SHELL) |
Corentin Wallez | 5157fbf | 2020-11-12 22:31:35 +0000 | [diff] [blame] | 63 | subprocess2.check_call(*args, **kwargs) |
scottmg@chromium.org | c20f470 | 2015-05-23 00:44:46 +0000 | [diff] [blame] | 64 | |
| 65 | |
Corentin Wallez | 5157fbf | 2020-11-12 22:31:35 +0000 | [diff] [blame] | 66 | def return_code(*args, **kwargs): |
Corentin Wallez | 8732c0e | 2020-12-09 17:43:46 +0000 | [diff] [blame] | 67 | """subprocess2.call() passing shell=True on Windows for git and |
Edward Lesmes | cf06cad | 2020-12-14 22:03:23 +0000 | [diff] [blame] | 68 | subprocess2.DEVNULL for stdout and stderr.""" |
Corentin Wallez | 5157fbf | 2020-11-12 22:31:35 +0000 | [diff] [blame] | 69 | kwargs.setdefault('shell', NEED_SHELL) |
Edward Lesmes | cf06cad | 2020-12-14 22:03:23 +0000 | [diff] [blame] | 70 | kwargs.setdefault('stdout', subprocess2.DEVNULL) |
| 71 | kwargs.setdefault('stderr', subprocess2.DEVNULL) |
Corentin Wallez | 5157fbf | 2020-11-12 22:31:35 +0000 | [diff] [blame] | 72 | return subprocess2.call(*args, **kwargs) |
| 73 | |
| 74 | |
| 75 | def is_pristine(root): |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 76 | """Returns True if a git checkout is pristine.""" |
Josip Sokcevic | c1a06c1 | 2022-01-12 00:44:14 +0000 | [diff] [blame] | 77 | # `git rev-parse --verify` has a non-zero return code if the revision |
| 78 | # doesn't exist. |
| 79 | diff_cmd = ['git', 'diff', '--ignore-submodules', 'origin/main'] |
| 80 | return (not check_output(diff_cmd, cwd=root).strip() and |
| 81 | not check_output(diff_cmd + ['--cached'], cwd=root).strip()) |
Corentin Wallez | 5157fbf | 2020-11-12 22:31:35 +0000 | [diff] [blame] | 82 | |
| 83 | |
szager@chromium.org | 03fd85b | 2014-06-09 23:43:33 +0000 | [diff] [blame] | 84 | |
Josip Sokcevic | 9c0dc30 | 2020-11-20 18:41:25 +0000 | [diff] [blame] | 85 | def get_log_url(upstream_url, head, tot): |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 86 | """Returns an URL to read logs via a Web UI if applicable.""" |
| 87 | if re.match(r'https://[^/]*\.googlesource\.com/', upstream_url): |
| 88 | # gitiles |
Josip Sokcevic | 9c0dc30 | 2020-11-20 18:41:25 +0000 | [diff] [blame] | 89 | return '%s/+log/%s..%s' % (upstream_url, head[:12], tot[:12]) |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 90 | if upstream_url.startswith('https://github.com/'): |
| 91 | upstream_url = upstream_url.rstrip('/') |
| 92 | if upstream_url.endswith('.git'): |
| 93 | upstream_url = upstream_url[:-len('.git')] |
Josip Sokcevic | 9c0dc30 | 2020-11-20 18:41:25 +0000 | [diff] [blame] | 94 | return '%s/compare/%s...%s' % (upstream_url, head[:12], tot[:12]) |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 95 | return None |
| 96 | |
| 97 | |
| 98 | def should_show_log(upstream_url): |
| 99 | """Returns True if a short log should be included in the tree.""" |
| 100 | # Skip logs for very active projects. |
Eric Boren | 07efc5a | 2017-08-28 09:13:20 -0400 | [diff] [blame] | 101 | if upstream_url.endswith('/v8/v8.git'): |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 102 | return False |
| 103 | if 'webrtc' in upstream_url: |
| 104 | return False |
| 105 | return True |
| 106 | |
| 107 | |
Edward Lemur | faae42e | 2018-11-26 18:34:30 +0000 | [diff] [blame] | 108 | def gclient(args): |
| 109 | """Executes gclient with the given args and returns the stdout.""" |
| 110 | return check_output([sys.executable, GCLIENT_PATH] + args).strip() |
sbc@chromium.org | 9820112 | 2015-04-22 20:21:34 +0000 | [diff] [blame] | 111 | |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 112 | |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 113 | def generate_commit_message( |
| 114 | full_dir, dependency, head, roll_to, no_log, log_limit): |
| 115 | """Creates the commit message for this specific roll.""" |
Sylvain Defresne | ae2b962 | 2020-01-31 09:46:06 +0000 | [diff] [blame] | 116 | commit_range = '%s..%s' % (head, roll_to) |
| 117 | commit_range_for_header = '%s..%s' % (head[:9], roll_to[:9]) |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 118 | upstream_url = check_output( |
| 119 | ['git', 'config', 'remote.origin.url'], cwd=full_dir).strip() |
| 120 | log_url = get_log_url(upstream_url, head, roll_to) |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 121 | cmd = ['git', 'log', commit_range, '--date=short', '--no-merges'] |
maruel@chromium.org | c6e39fe | 2015-09-23 13:45:52 +0000 | [diff] [blame] | 122 | logs = check_output( |
Sylvain Defresne | ae2b962 | 2020-01-31 09:46:06 +0000 | [diff] [blame] | 123 | # Args with '=' are automatically quoted. |
| 124 | cmd + ['--format=%ad %ae %s', '--'], |
Marc-Antoine Ruel | 51104fe | 2017-03-01 17:57:41 -0500 | [diff] [blame] | 125 | cwd=full_dir).rstrip() |
maruel@chromium.org | c6e39fe | 2015-09-23 13:45:52 +0000 | [diff] [blame] | 126 | 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] | 127 | lines = logs.splitlines() |
Marc-Antoine Ruel | 1e2cb15 | 2019-04-17 17:32:52 +0000 | [diff] [blame] | 128 | cleaned_lines = [l for l in lines if not _ROLL_SUBJECT.match(l)] |
Marc-Antoine Ruel | 51104fe | 2017-03-01 17:57:41 -0500 | [diff] [blame] | 129 | logs = '\n'.join(cleaned_lines) + '\n' |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 130 | |
Marc-Antoine Ruel | 51104fe | 2017-03-01 17:57:41 -0500 | [diff] [blame] | 131 | nb_commits = len(lines) |
| 132 | rolls = nb_commits - len(cleaned_lines) |
Marc-Antoine Ruel | 51104fe | 2017-03-01 17:57:41 -0500 | [diff] [blame] | 133 | header = 'Roll %s/ %s (%d commit%s%s)\n\n' % ( |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 134 | dependency, |
Sylvain Defresne | ae2b962 | 2020-01-31 09:46:06 +0000 | [diff] [blame] | 135 | commit_range_for_header, |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 136 | nb_commits, |
Marc-Antoine Ruel | 51104fe | 2017-03-01 17:57:41 -0500 | [diff] [blame] | 137 | 's' if nb_commits > 1 else '', |
| 138 | ('; %s trivial rolls' % rolls) if rolls else '') |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 139 | log_section = '' |
| 140 | if log_url: |
| 141 | log_section = log_url + '\n\n' |
johannkoenig@google.com | 64f2fc3 | 2015-10-07 19:11:17 +0000 | [diff] [blame] | 142 | log_section += '$ %s ' % ' '.join(cmd) |
| 143 | log_section += '--format=\'%ad %ae %s\'\n' |
Sylvain Defresne | 4ada8ab | 2020-01-31 17:22:56 +0000 | [diff] [blame] | 144 | log_section = log_section.replace(commit_range, commit_range_for_header) |
Eric Boren | 3be96a8 | 2017-09-29 10:07:46 -0400 | [diff] [blame] | 145 | # It is important that --no-log continues to work, as it is used by |
| 146 | # internal -> external rollers. Please do not remove or break it. |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 147 | if not no_log and should_show_log(upstream_url): |
Marc-Antoine Ruel | 51104fe | 2017-03-01 17:57:41 -0500 | [diff] [blame] | 148 | if len(cleaned_lines) > log_limit: |
Marc-Antoine Ruel | 6ce1822 | 2019-01-16 21:36:36 +0000 | [diff] [blame] | 149 | # Keep the first N/2 log entries and last N/2 entries. |
| 150 | lines = logs.splitlines(True) |
Edward Lesmes | 994bed5 | 2020-04-01 16:21:40 +0000 | [diff] [blame] | 151 | lines = lines[:log_limit//2] + ['(...)\n'] + lines[-log_limit//2:] |
Marc-Antoine Ruel | 6ce1822 | 2019-01-16 21:36:36 +0000 | [diff] [blame] | 152 | logs = ''.join(lines) |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 153 | log_section += logs |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 154 | return header + log_section |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 155 | |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 156 | |
Edward Lemur | faae42e | 2018-11-26 18:34:30 +0000 | [diff] [blame] | 157 | def calculate_roll(full_dir, dependency, roll_to): |
Edward Lesmes | c772cf7 | 2018-04-03 14:47:30 -0400 | [diff] [blame] | 158 | """Calculates the roll for a dependency by processing gclient_dict, and |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 159 | fetching the dependency via git. |
| 160 | """ |
Edward Lemur | faae42e | 2018-11-26 18:34:30 +0000 | [diff] [blame] | 161 | head = gclient(['getdep', '-r', dependency]) |
Edward Lesmes | c772cf7 | 2018-04-03 14:47:30 -0400 | [diff] [blame] | 162 | if not head: |
| 163 | raise Error('%s is unpinned.' % dependency) |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 164 | check_call(['git', 'fetch', 'origin', '--quiet'], cwd=full_dir) |
Josip Sokcevic | 69902d0 | 2021-02-02 21:57:55 +0000 | [diff] [blame] | 165 | if roll_to == 'origin/HEAD': |
| 166 | check_output(['git', 'remote', 'set-head', 'origin', '-a'], cwd=full_dir) |
| 167 | |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 168 | roll_to = check_output(['git', 'rev-parse', roll_to], cwd=full_dir).strip() |
| 169 | return head, roll_to |
| 170 | |
| 171 | |
Josip Sokcevic | 69902d0 | 2021-02-02 21:57:55 +0000 | [diff] [blame] | 172 | |
Robert Iannucci | c1e6594 | 2018-10-18 17:59:45 +0000 | [diff] [blame] | 173 | def gen_commit_msg(logs, cmdline, reviewers, bug): |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 174 | """Returns the final commit message.""" |
| 175 | commit_msg = '' |
| 176 | if len(logs) > 1: |
| 177 | commit_msg = 'Rolling %d dependencies\n\n' % len(logs) |
| 178 | commit_msg += '\n\n'.join(logs) |
Kenneth Russell | ebe839b | 2017-12-22 14:55:39 -0800 | [diff] [blame] | 179 | commit_msg += '\nCreated with:\n ' + cmdline + '\n' |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 180 | commit_msg += 'R=%s\n' % ','.join(reviewers) if reviewers else '' |
Robert Iannucci | c1e6594 | 2018-10-18 17:59:45 +0000 | [diff] [blame] | 181 | commit_msg += '\nBug: %s\n' % bug if bug else '' |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 182 | return commit_msg |
| 183 | |
| 184 | |
Edward Lemur | faae42e | 2018-11-26 18:34:30 +0000 | [diff] [blame] | 185 | def finalize(commit_msg, current_dir, rolls): |
| 186 | """Commits changes to the DEPS file, then uploads a CL.""" |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 187 | print('Commit message:') |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 188 | print('\n'.join(' ' + i for i in commit_msg.splitlines())) |
| 189 | |
Edward Lesmes | 3f277fc | 2018-04-06 15:32:51 -0400 | [diff] [blame] | 190 | check_call(['git', 'add', 'DEPS'], cwd=current_dir) |
Bruce Dawson | 03af44a | 2022-12-27 19:37:58 +0000 | [diff] [blame] | 191 | # We have to set delete=False and then let the object go out of scope so |
| 192 | # that the file can be opened by name on Windows. |
| 193 | with tempfile.NamedTemporaryFile('w+', newline='', delete=False) as f: |
| 194 | commit_filename = f.name |
| 195 | f.write(commit_msg) |
| 196 | check_call(['git', 'commit', '--quiet', '--file', commit_filename], |
| 197 | cwd=current_dir) |
| 198 | os.remove(commit_filename) |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 199 | |
| 200 | # Pull the dependency to the right revision. This is surprising to users |
| 201 | # otherwise. |
Marc-Antoine Ruel | 8e57b4b | 2019-10-11 01:01:36 +0000 | [diff] [blame] | 202 | for _head, roll_to, full_dir in sorted(rolls.values()): |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 203 | check_call(['git', 'checkout', '--quiet', roll_to], cwd=full_dir) |
szager@chromium.org | 03fd85b | 2014-06-09 23:43:33 +0000 | [diff] [blame] | 204 | |
| 205 | |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 206 | def main(): |
sbc@chromium.org | 30e5b23 | 2015-06-01 18:06:59 +0000 | [diff] [blame] | 207 | parser = argparse.ArgumentParser(description=__doc__) |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 208 | parser.add_argument( |
smut@google.com | 02d2087 | 2015-11-14 00:46:41 +0000 | [diff] [blame] | 209 | '--ignore-dirty-tree', action='store_true', |
| 210 | help='Roll anyways, even if there is a diff.') |
| 211 | parser.add_argument( |
Thiago Perrotta | a089281 | 2022-09-03 11:22:46 +0000 | [diff] [blame] | 212 | '-r', |
| 213 | '--reviewer', |
| 214 | action='append', |
| 215 | help= |
| 216 | 'To specify multiple reviewers, either use a comma separated list, e.g. ' |
| 217 | '-r joe,jane,john or provide the flag multiple times, e.g. ' |
| 218 | '-r joe -r jane. Defaults to @chromium.org') |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 219 | 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] | 220 | # It is important that --no-log continues to work, as it is used by |
| 221 | # internal -> external rollers. Please do not remove or break it. |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 222 | parser.add_argument( |
| 223 | '--no-log', action='store_true', |
| 224 | help='Do not include the short log in the commit message') |
| 225 | parser.add_argument( |
| 226 | '--log-limit', type=int, default=100, |
| 227 | help='Trim log after N commits (default: %(default)s)') |
| 228 | parser.add_argument( |
Josip Sokcevic | 69902d0 | 2021-02-02 21:57:55 +0000 | [diff] [blame] | 229 | '--roll-to', default='origin/HEAD', |
maruel@chromium.org | 398ed34 | 2015-09-29 12:27:25 +0000 | [diff] [blame] | 230 | help='Specify the new commit to roll to (default: %(default)s)') |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 231 | parser.add_argument( |
| 232 | '--key', action='append', default=[], |
| 233 | help='Regex(es) for dependency in DEPS file') |
| 234 | parser.add_argument('dep_path', nargs='+', help='Path(s) to dependency') |
sbc@chromium.org | 30e5b23 | 2015-06-01 18:06:59 +0000 | [diff] [blame] | 235 | args = parser.parse_args() |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 236 | |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 237 | if len(args.dep_path) > 1: |
Josip Sokcevic | 69902d0 | 2021-02-02 21:57:55 +0000 | [diff] [blame] | 238 | if args.roll_to != 'origin/HEAD': |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 239 | parser.error( |
| 240 | 'Can\'t use multiple paths to roll simultaneously and --roll-to') |
| 241 | if args.key: |
| 242 | parser.error( |
| 243 | 'Can\'t use multiple paths to roll simultaneously and --key') |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 244 | reviewers = None |
sbc@chromium.org | 30e5b23 | 2015-06-01 18:06:59 +0000 | [diff] [blame] | 245 | if args.reviewer: |
Thiago Perrotta | a089281 | 2022-09-03 11:22:46 +0000 | [diff] [blame] | 246 | reviewers = list(itertools.chain(*[r.split(',') for r in args.reviewer])) |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 247 | for i, r in enumerate(reviewers): |
| 248 | if not '@' in r: |
| 249 | reviewers[i] = r + '@chromium.org' |
| 250 | |
Edward Lemur | faae42e | 2018-11-26 18:34:30 +0000 | [diff] [blame] | 251 | gclient_root = gclient(['root']) |
Edward Lesmes | 3f277fc | 2018-04-06 15:32:51 -0400 | [diff] [blame] | 252 | current_dir = os.getcwd() |
Bruce Dawson | 37b62e5 | 2020-06-23 18:06:00 +0000 | [diff] [blame] | 253 | dependencies = sorted(d.replace('\\', '/').rstrip('/') for d in args.dep_path) |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 254 | cmdline = 'roll-dep ' + ' '.join(dependencies) + ''.join( |
| 255 | ' --key ' + k for k in args.key) |
sbc@chromium.org | e5d984b | 2015-05-29 22:09:39 +0000 | [diff] [blame] | 256 | try: |
Edward Lesmes | 3f277fc | 2018-04-06 15:32:51 -0400 | [diff] [blame] | 257 | if not args.ignore_dirty_tree and not is_pristine(current_dir): |
| 258 | raise Error( |
| 259 | 'Ensure %s is clean first (no non-merged commits).' % current_dir) |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 260 | # First gather all the information without modifying anything, except for a |
| 261 | # git fetch. |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 262 | rolls = {} |
| 263 | for dependency in dependencies: |
Edward Lemur | faae42e | 2018-11-26 18:34:30 +0000 | [diff] [blame] | 264 | full_dir = os.path.normpath(os.path.join(gclient_root, dependency)) |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 265 | if not os.path.isdir(full_dir): |
Edward Lemur | faae42e | 2018-11-26 18:34:30 +0000 | [diff] [blame] | 266 | print('Dependency %s not found at %s' % (dependency, full_dir)) |
| 267 | full_dir = os.path.normpath(os.path.join(current_dir, dependency)) |
| 268 | print('Will look for relative dependency at %s' % full_dir) |
| 269 | if not os.path.isdir(full_dir): |
| 270 | raise Error('Directory not found: %s (%s)' % (dependency, full_dir)) |
| 271 | |
| 272 | head, roll_to = calculate_roll(full_dir, dependency, args.roll_to) |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 273 | if roll_to == head: |
| 274 | if len(dependencies) == 1: |
| 275 | raise AlreadyRolledError('No revision to roll!') |
| 276 | print('%s: Already at latest commit %s' % (dependency, roll_to)) |
| 277 | else: |
| 278 | print( |
| 279 | '%s: Rolling from %s to %s' % (dependency, head[:10], roll_to[:10])) |
Edward Lemur | faae42e | 2018-11-26 18:34:30 +0000 | [diff] [blame] | 280 | rolls[dependency] = (head, roll_to, full_dir) |
sbc@chromium.org | e5d984b | 2015-05-29 22:09:39 +0000 | [diff] [blame] | 281 | |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 282 | logs = [] |
Edward Lemur | faae42e | 2018-11-26 18:34:30 +0000 | [diff] [blame] | 283 | setdep_args = [] |
Marc-Antoine Ruel | 8e57b4b | 2019-10-11 01:01:36 +0000 | [diff] [blame] | 284 | for dependency, (head, roll_to, full_dir) in sorted(rolls.items()): |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 285 | log = generate_commit_message( |
| 286 | full_dir, dependency, head, roll_to, args.no_log, args.log_limit) |
| 287 | logs.append(log) |
Edward Lemur | faae42e | 2018-11-26 18:34:30 +0000 | [diff] [blame] | 288 | setdep_args.extend(['-r', '{}@{}'.format(dependency, roll_to)]) |
Edward Lesmes | c772cf7 | 2018-04-03 14:47:30 -0400 | [diff] [blame] | 289 | |
Edward Lemur | faae42e | 2018-11-26 18:34:30 +0000 | [diff] [blame] | 290 | gclient(['setdep'] + setdep_args) |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 291 | |
Robert Iannucci | c1e6594 | 2018-10-18 17:59:45 +0000 | [diff] [blame] | 292 | commit_msg = gen_commit_msg(logs, cmdline, reviewers, args.bug) |
Edward Lemur | faae42e | 2018-11-26 18:34:30 +0000 | [diff] [blame] | 293 | finalize(commit_msg, current_dir, rolls) |
sbc@chromium.org | e5d984b | 2015-05-29 22:09:39 +0000 | [diff] [blame] | 294 | except Error as e: |
| 295 | sys.stderr.write('error: %s\n' % e) |
smut | 7036c4f | 2016-06-09 14:28:48 -0700 | [diff] [blame] | 296 | return 2 if isinstance(e, AlreadyRolledError) else 1 |
Corentin Wallez | 8732c0e | 2020-12-09 17:43:46 +0000 | [diff] [blame] | 297 | except subprocess2.CalledProcessError: |
Edward Lemur | 3c11794 | 2020-03-12 17:21:12 +0000 | [diff] [blame] | 298 | return 1 |
sbc@chromium.org | e5d984b | 2015-05-29 22:09:39 +0000 | [diff] [blame] | 299 | |
Marc-Antoine Ruel | 85a8c10 | 2017-12-12 15:42:25 -0500 | [diff] [blame] | 300 | print('') |
| 301 | if not reviewers: |
| 302 | print('You forgot to pass -r, make sure to insert a R=foo@example.com line') |
| 303 | print('to the commit description before emailing.') |
| 304 | print('') |
| 305 | print('Run:') |
| 306 | print(' git cl upload --send-mail') |
sbc@chromium.org | e5d984b | 2015-05-29 22:09:39 +0000 | [diff] [blame] | 307 | return 0 |
szager@chromium.org | 03fd85b | 2014-06-09 23:43:33 +0000 | [diff] [blame] | 308 | |
sbc@chromium.org | 9820112 | 2015-04-22 20:21:34 +0000 | [diff] [blame] | 309 | |
szager@chromium.org | 03fd85b | 2014-06-09 23:43:33 +0000 | [diff] [blame] | 310 | if __name__ == '__main__': |
maruel@chromium.org | 9655094 | 2015-05-22 18:46:51 +0000 | [diff] [blame] | 311 | sys.exit(main()) |