Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Brian Norris | 6baeb2e | 2020-03-18 12:13:30 -0700 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
| 3 | # |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 4 | # Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 5 | # Use of this source code is governed by a BSD-style license that can be |
| 6 | # found in the LICENSE file. |
Mike Frysinger | f80ca21 | 2018-07-13 15:02:52 -0400 | [diff] [blame] | 7 | |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 8 | """This is a tool for picking patches from upstream and applying them.""" |
| 9 | |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 10 | import argparse |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 11 | import configparser |
Tzung-Bi Shih | 436fdba | 2019-09-04 19:05:00 +0800 | [diff] [blame] | 12 | import functools |
Brian Norris | c342104 | 2018-08-15 14:17:26 -0700 | [diff] [blame] | 13 | import mailbox |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 14 | import os |
Tzung-Bi Shih | 5100c74 | 2019-09-02 10:28:32 +0800 | [diff] [blame] | 15 | import pprint |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 16 | import re |
| 17 | import signal |
| 18 | import subprocess |
| 19 | import sys |
Harry Cutts | ae372f3 | 2019-02-12 18:01:14 -0800 | [diff] [blame] | 20 | import textwrap |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 21 | import urllib.request |
| 22 | import xmlrpc.client |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 23 | |
Tzung-Bi Shih | 436fdba | 2019-09-04 19:05:00 +0800 | [diff] [blame] | 24 | errprint = functools.partial(print, file=sys.stderr) |
| 25 | |
Abhishek Pandit-Subedi | aea8c50 | 2020-07-09 21:56:12 -0700 | [diff] [blame] | 26 | UPSTREAM_URLS = ( |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 27 | 'git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git', |
| 28 | 'https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git', |
| 29 | 'https://kernel.googlesource.com/pub/scm/linux/kernel/git/torvalds/linux.git', |
Brian Norris | 8043cfd | 2020-03-19 11:46:16 -0700 | [diff] [blame] | 30 | 'git://w1.fi/srv/git/hostap.git', |
Abhishek Pandit-Subedi | aea8c50 | 2020-07-09 21:56:12 -0700 | [diff] [blame] | 31 | 'git://git.kernel.org/pub/scm/bluetooth/bluez.git', |
Brian Norris | 8043cfd | 2020-03-19 11:46:16 -0700 | [diff] [blame] | 32 | ) |
| 33 | |
Stephen Boyd | b68c17a | 2019-09-26 15:08:02 -0700 | [diff] [blame] | 34 | PATCHWORK_URLS = ( |
| 35 | 'https://lore.kernel.org/patchwork', |
| 36 | 'https://patchwork.kernel.org', |
| 37 | 'https://patchwork.ozlabs.org', |
| 38 | 'https://patchwork.freedesktop.org', |
| 39 | 'https://patchwork.linux-mips.org', |
| 40 | ) |
| 41 | |
Harry Cutts | ae372f3 | 2019-02-12 18:01:14 -0800 | [diff] [blame] | 42 | COMMIT_MESSAGE_WIDTH = 75 |
| 43 | |
Brian Norris | 9f8a2be | 2018-06-01 11:14:08 -0700 | [diff] [blame] | 44 | _PWCLIENTRC = os.path.expanduser('~/.pwclientrc') |
| 45 | |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 46 | def _git(args, stdin=None, encoding='utf-8'): |
| 47 | """Calls a git subcommand. |
| 48 | |
| 49 | Similar to subprocess.check_output. |
| 50 | |
| 51 | Args: |
Brian Norris | 6baeb2e | 2020-03-18 12:13:30 -0700 | [diff] [blame] | 52 | args: subcommand + args passed to 'git'. |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 53 | stdin: a string or bytes (depending on encoding) that will be passed |
| 54 | to the git subcommand. |
| 55 | encoding: either 'utf-8' (default) or None. Override it to None if |
| 56 | you want both stdin and stdout to be raw bytes. |
| 57 | |
| 58 | Returns: |
| 59 | the stdout of the git subcommand, same type as stdin. The output is |
| 60 | also run through strip to make sure there's no extra whitespace. |
| 61 | |
| 62 | Raises: |
| 63 | subprocess.CalledProcessError: when return code is not zero. |
| 64 | The exception has a .returncode attribute. |
| 65 | """ |
| 66 | return subprocess.run( |
| 67 | ['git'] + args, |
| 68 | encoding=encoding, |
| 69 | input=stdin, |
| 70 | stdout=subprocess.PIPE, |
| 71 | check=True, |
| 72 | ).stdout.strip() |
| 73 | |
| 74 | def _git_returncode(*args, **kwargs): |
| 75 | """Same as _git, but return returncode instead of stdout. |
| 76 | |
| 77 | Similar to subprocess.call. |
| 78 | |
| 79 | Never raises subprocess.CalledProcessError. |
| 80 | """ |
| 81 | try: |
| 82 | _git(*args, **kwargs) |
| 83 | return 0 |
| 84 | except subprocess.CalledProcessError as e: |
| 85 | return e.returncode |
| 86 | |
Guenter Roeck | bdbb9cc | 2018-04-19 10:05:08 -0700 | [diff] [blame] | 87 | def _get_conflicts(): |
| 88 | """Report conflicting files.""" |
| 89 | resolutions = ('DD', 'AU', 'UD', 'UA', 'DU', 'AA', 'UU') |
| 90 | conflicts = [] |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 91 | output = _git(['status', '--porcelain', '--untracked-files=no']) |
| 92 | for line in output.splitlines(): |
Douglas Anderson | 46287f9 | 2018-04-30 09:58:24 -0700 | [diff] [blame] | 93 | if not line: |
Guenter Roeck | bdbb9cc | 2018-04-19 10:05:08 -0700 | [diff] [blame] | 94 | continue |
Douglas Anderson | 46287f9 | 2018-04-30 09:58:24 -0700 | [diff] [blame] | 95 | resolution, name = line.split(None, 1) |
Guenter Roeck | bdbb9cc | 2018-04-19 10:05:08 -0700 | [diff] [blame] | 96 | if resolution in resolutions: |
| 97 | conflicts.append(' ' + name) |
| 98 | if not conflicts: |
Douglas Anderson | b6a10fe | 2019-08-12 13:53:30 -0700 | [diff] [blame] | 99 | return '' |
Guenter Roeck | bdbb9cc | 2018-04-19 10:05:08 -0700 | [diff] [blame] | 100 | return '\nConflicts:\n%s\n' % '\n'.join(conflicts) |
| 101 | |
Brian Norris | 8043cfd | 2020-03-19 11:46:16 -0700 | [diff] [blame] | 102 | def _find_upstream_remote(urls): |
| 103 | """Find a remote pointing to an upstream repository.""" |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 104 | for remote in _git(['remote']).splitlines(): |
| 105 | try: |
Brian Norris | 8043cfd | 2020-03-19 11:46:16 -0700 | [diff] [blame] | 106 | if _git(['remote', 'get-url', remote]) in urls: |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 107 | return remote |
| 108 | except subprocess.CalledProcessError: |
| 109 | # Kinda weird, get-url failing on an item that git just gave us. |
| 110 | continue |
Guenter Roeck | d66daa7 | 2018-04-19 10:31:25 -0700 | [diff] [blame] | 111 | return None |
| 112 | |
Guenter Roeck | bdbb9cc | 2018-04-19 10:05:08 -0700 | [diff] [blame] | 113 | def _pause_for_merge(conflicts): |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 114 | """Pause and go in the background till user resolves the conflicts.""" |
| 115 | |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 116 | git_root = _git(['rev-parse', '--show-toplevel']) |
Harry Cutts | 2bcd9af | 2020-02-20 16:27:50 -0800 | [diff] [blame] | 117 | previous_head_hash = _git(['rev-parse', 'HEAD']) |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 118 | |
| 119 | paths = ( |
| 120 | os.path.join(git_root, '.git', 'rebase-apply'), |
| 121 | os.path.join(git_root, '.git', 'CHERRY_PICK_HEAD'), |
| 122 | ) |
| 123 | for path in paths: |
| 124 | if os.path.exists(path): |
Tzung-Bi Shih | 436fdba | 2019-09-04 19:05:00 +0800 | [diff] [blame] | 125 | errprint('Found "%s".' % path) |
| 126 | errprint(conflicts) |
| 127 | errprint('Please resolve the conflicts and restart the ' |
| 128 | 'shell job when done. Kill this job if you ' |
| 129 | 'aborted the conflict.') |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 130 | os.kill(os.getpid(), signal.SIGTSTP) |
Harry Cutts | 2bcd9af | 2020-02-20 16:27:50 -0800 | [diff] [blame] | 131 | |
| 132 | # Check the conflicts actually got resolved. Otherwise we'll end up |
| 133 | # modifying the wrong commit message and probably confusing people. |
| 134 | while previous_head_hash == _git(['rev-parse', 'HEAD']): |
| 135 | errprint('Error: no new commit has been made. Did you forget to run ' |
| 136 | '`git am --continue` or `git cherry-pick --continue`?') |
| 137 | errprint('Please create a new commit and restart the shell job (or kill' |
| 138 | ' it if you aborted the conflict).') |
| 139 | os.kill(os.getpid(), signal.SIGTSTP) |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 140 | |
Brian Norris | 9f8a2be | 2018-06-01 11:14:08 -0700 | [diff] [blame] | 141 | def _get_pw_url(project): |
| 142 | """Retrieve the patchwork server URL from .pwclientrc. |
| 143 | |
Mike Frysinger | f80ca21 | 2018-07-13 15:02:52 -0400 | [diff] [blame] | 144 | Args: |
| 145 | project: patchwork project name; if None, we retrieve the default |
| 146 | from pwclientrc |
Brian Norris | 9f8a2be | 2018-06-01 11:14:08 -0700 | [diff] [blame] | 147 | """ |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 148 | config = configparser.ConfigParser() |
Brian Norris | 9f8a2be | 2018-06-01 11:14:08 -0700 | [diff] [blame] | 149 | config.read([_PWCLIENTRC]) |
| 150 | |
| 151 | if project is None: |
| 152 | try: |
| 153 | project = config.get('options', 'default') |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 154 | except (configparser.NoSectionError, configparser.NoOptionError) as e: |
| 155 | errprint('Error: no default patchwork project found in %s. (%r)' |
| 156 | % (_PWCLIENTRC, e)) |
Brian Norris | 9f8a2be | 2018-06-01 11:14:08 -0700 | [diff] [blame] | 157 | sys.exit(1) |
| 158 | |
| 159 | if not config.has_option(project, 'url'): |
Tzung-Bi Shih | 436fdba | 2019-09-04 19:05:00 +0800 | [diff] [blame] | 160 | errprint("Error: patchwork URL not found for project '%s'" % project) |
Brian Norris | 9f8a2be | 2018-06-01 11:14:08 -0700 | [diff] [blame] | 161 | sys.exit(1) |
| 162 | |
| 163 | url = config.get(project, 'url') |
Brian Norris | 2d4e976 | 2018-08-15 13:11:47 -0700 | [diff] [blame] | 164 | # Strip trailing 'xmlrpc' and/or trailing slash. |
| 165 | return re.sub('/(xmlrpc/)?$', '', url) |
Brian Norris | 9f8a2be | 2018-06-01 11:14:08 -0700 | [diff] [blame] | 166 | |
Harry Cutts | ae372f3 | 2019-02-12 18:01:14 -0800 | [diff] [blame] | 167 | def _wrap_commit_line(prefix, content): |
| 168 | line = prefix + '=' + content |
| 169 | indent = ' ' * (len(prefix) + 1) |
| 170 | return textwrap.fill(line, COMMIT_MESSAGE_WIDTH, subsequent_indent=indent) |
| 171 | |
Stephen Boyd | b68c17a | 2019-09-26 15:08:02 -0700 | [diff] [blame] | 172 | def _pick_patchwork(url, patch_id, args): |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 173 | if args['tag'] is None: |
| 174 | args['tag'] = 'FROMLIST: ' |
| 175 | |
Brian Norris | 8553f03 | 2020-03-18 11:59:02 -0700 | [diff] [blame] | 176 | try: |
| 177 | opener = urllib.request.urlopen('%s/patch/%d/mbox' % (url, patch_id)) |
| 178 | except urllib.error.HTTPError as e: |
| 179 | errprint('Error: could not download patch: %s' % e) |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 180 | sys.exit(1) |
| 181 | patch_contents = opener.read() |
| 182 | |
| 183 | if not patch_contents: |
Tzung-Bi Shih | 436fdba | 2019-09-04 19:05:00 +0800 | [diff] [blame] | 184 | errprint('Error: No patch content found') |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 185 | sys.exit(1) |
| 186 | |
Douglas Anderson | becd4e6 | 2019-09-25 13:40:55 -0700 | [diff] [blame] | 187 | message_id = mailbox.Message(patch_contents)['Message-Id'] |
| 188 | message_id = re.sub('^<|>$', '', message_id.strip()) |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 189 | if args['source_line'] is None: |
| 190 | args['source_line'] = '(am from %s/patch/%d/)' % (url, patch_id) |
Brian Norris | 8553f03 | 2020-03-18 11:59:02 -0700 | [diff] [blame] | 191 | for url_template in [ |
Brian Norris | 655b5ce | 2020-05-08 11:37:38 -0700 | [diff] [blame] | 192 | 'https://lore.kernel.org/r/%s', |
Brian Norris | 8553f03 | 2020-03-18 11:59:02 -0700 | [diff] [blame] | 193 | # hostap project (and others) are here, but not kernel.org. |
| 194 | 'https://marc.info/?i=%s', |
| 195 | # public-inbox comes last as a "default"; it has a nice error page |
| 196 | # pointing to other redirectors, even if it doesn't have what |
| 197 | # you're looking for directly. |
| 198 | 'https://public-inbox.org/git/%s', |
| 199 | ]: |
| 200 | alt_url = url_template % message_id |
| 201 | if args['debug']: |
| 202 | print('Probing archive for message at: %s' % alt_url) |
| 203 | try: |
| 204 | urllib.request.urlopen(alt_url) |
| 205 | except urllib.error.HTTPError as e: |
| 206 | # Skip all HTTP errors. We can expect 404 for archives that |
| 207 | # don't have this MessageId, or 300 for public-inbox ("not |
| 208 | # found, but try these other redirects"). It's less clear what |
| 209 | # to do with transitory (or is it permanent?) server failures. |
| 210 | if args['debug']: |
| 211 | print('Skipping URL %s, error: %s' % (alt_url, e)) |
| 212 | continue |
| 213 | # Success! |
| 214 | if args['debug']: |
| 215 | print('Found at %s' % alt_url) |
| 216 | break |
| 217 | else: |
| 218 | errprint( |
| 219 | "WARNING: couldn't find working MessageId URL; " |
| 220 | 'defaulting to "%s"' % alt_url) |
| 221 | args['source_line'] += '\n(also found at %s)' % alt_url |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 222 | |
Douglas Anderson | becd4e6 | 2019-09-25 13:40:55 -0700 | [diff] [blame] | 223 | # Auto-snarf the Change-Id if it was encoded into the Message-Id. |
| 224 | mo = re.match(r'.*(I[a-f0-9]{40})@changeid$', message_id) |
| 225 | if mo and args['changeid'] is None: |
| 226 | args['changeid'] = mo.group(1) |
| 227 | |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 228 | if args['replace']: |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 229 | _git(['reset', '--hard', 'HEAD~1']) |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 230 | |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 231 | return _git_returncode(['am', '-3'], stdin=patch_contents, encoding=None) |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 232 | |
Stephen Boyd | b68c17a | 2019-09-26 15:08:02 -0700 | [diff] [blame] | 233 | def _match_patchwork(match, args): |
| 234 | """Match location: pw://### or pw://PROJECT/###.""" |
| 235 | pw_project = match.group(2) |
| 236 | patch_id = int(match.group(3)) |
| 237 | |
| 238 | if args['debug']: |
| 239 | print('_match_patchwork: pw_project=%s, patch_id=%d' % |
| 240 | (pw_project, patch_id)) |
| 241 | |
| 242 | url = _get_pw_url(pw_project) |
| 243 | return _pick_patchwork(url, patch_id, args) |
| 244 | |
| 245 | def _match_msgid(match, args): |
| 246 | """Match location: msgid://MSGID.""" |
| 247 | msgid = match.group(1) |
| 248 | |
| 249 | if args['debug']: |
| 250 | print('_match_msgid: message_id=%s' % (msgid)) |
| 251 | |
| 252 | # Patchwork requires the brackets so force it |
| 253 | msgid = '<' + msgid + '>' |
| 254 | url = None |
| 255 | for url in PATCHWORK_URLS: |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 256 | rpc = xmlrpc.client.ServerProxy(url + '/xmlrpc/') |
Stephen Boyd | b68c17a | 2019-09-26 15:08:02 -0700 | [diff] [blame] | 257 | res = rpc.patch_list({'msgid': msgid}) |
| 258 | if res: |
| 259 | patch_id = res[0]['id'] |
| 260 | break |
| 261 | else: |
| 262 | errprint('Error: could not find patch based on message id') |
| 263 | sys.exit(1) |
| 264 | |
| 265 | return _pick_patchwork(url, patch_id, args) |
| 266 | |
Abhishek Pandit-Subedi | aea8c50 | 2020-07-09 21:56:12 -0700 | [diff] [blame] | 267 | def _upstream(commit, urls, args): |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 268 | if args['debug']: |
Abhishek Pandit-Subedi | aea8c50 | 2020-07-09 21:56:12 -0700 | [diff] [blame] | 269 | print('_upstream: commit=%s' % commit) |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 270 | |
Brian Norris | 8043cfd | 2020-03-19 11:46:16 -0700 | [diff] [blame] | 271 | # Confirm an upstream remote is setup. |
| 272 | remote = _find_upstream_remote(urls) |
| 273 | if not remote: |
Tzung-Bi Shih | 436fdba | 2019-09-04 19:05:00 +0800 | [diff] [blame] | 274 | errprint('Error: need a valid upstream remote') |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 275 | sys.exit(1) |
| 276 | |
Brian Norris | 8043cfd | 2020-03-19 11:46:16 -0700 | [diff] [blame] | 277 | remote_ref = '%s/master' % remote |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 278 | try: |
Brian Norris | 8043cfd | 2020-03-19 11:46:16 -0700 | [diff] [blame] | 279 | _git(['merge-base', '--is-ancestor', commit, remote_ref]) |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 280 | except subprocess.CalledProcessError: |
Brian Norris | 8043cfd | 2020-03-19 11:46:16 -0700 | [diff] [blame] | 281 | errprint('Error: Commit not in %s' % remote_ref) |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 282 | sys.exit(1) |
| 283 | |
| 284 | if args['source_line'] is None: |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 285 | commit = _git(['rev-parse', commit]) |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 286 | args['source_line'] = ('(cherry picked from commit %s)' % |
| 287 | (commit)) |
| 288 | if args['tag'] is None: |
| 289 | args['tag'] = 'UPSTREAM: ' |
| 290 | |
| 291 | if args['replace']: |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 292 | _git(['reset', '--hard', 'HEAD~1']) |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 293 | |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 294 | return _git_returncode(['cherry-pick', commit]) |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 295 | |
Abhishek Pandit-Subedi | aea8c50 | 2020-07-09 21:56:12 -0700 | [diff] [blame] | 296 | def _match_upstream(match, args): |
| 297 | """Match location: linux://HASH and upstream://HASH.""" |
Brian Norris | 8043cfd | 2020-03-19 11:46:16 -0700 | [diff] [blame] | 298 | commit = match.group(1) |
Abhishek Pandit-Subedi | aea8c50 | 2020-07-09 21:56:12 -0700 | [diff] [blame] | 299 | return _upstream(commit, urls=UPSTREAM_URLS, args=args) |
Brian Norris | 8043cfd | 2020-03-19 11:46:16 -0700 | [diff] [blame] | 300 | |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 301 | def _match_fromgit(match, args): |
| 302 | """Match location: git://remote/branch/HASH.""" |
| 303 | remote = match.group(2) |
| 304 | branch = match.group(3) |
| 305 | commit = match.group(4) |
| 306 | |
| 307 | if args['debug']: |
| 308 | print('_match_fromgit: remote=%s branch=%s commit=%s' % |
| 309 | (remote, branch, commit)) |
| 310 | |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 311 | try: |
| 312 | _git(['merge-base', '--is-ancestor', commit, |
| 313 | '%s/%s' % (remote, branch)]) |
| 314 | except subprocess.CalledProcessError: |
Tzung-Bi Shih | 436fdba | 2019-09-04 19:05:00 +0800 | [diff] [blame] | 315 | errprint('Error: Commit not in %s/%s' % (remote, branch)) |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 316 | sys.exit(1) |
| 317 | |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 318 | url = _git(['remote', 'get-url', remote]) |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 319 | |
| 320 | if args['source_line'] is None: |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 321 | commit = _git(['rev-parse', commit]) |
Tzung-Bi Shih | a8f310d | 2019-09-04 19:10:10 +0800 | [diff] [blame] | 322 | args['source_line'] = ( |
| 323 | '(cherry picked from commit %s\n %s %s)' % (commit, url, branch)) |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 324 | if args['tag'] is None: |
| 325 | args['tag'] = 'FROMGIT: ' |
| 326 | |
| 327 | if args['replace']: |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 328 | _git(['reset', '--hard', 'HEAD~1']) |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 329 | |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 330 | return _git_returncode(['cherry-pick', commit]) |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 331 | |
| 332 | def _match_gitfetch(match, args): |
| 333 | """Match location: (git|https)://repoURL#branch/HASH.""" |
| 334 | remote = match.group(1) |
| 335 | branch = match.group(3) |
| 336 | commit = match.group(4) |
| 337 | |
| 338 | if args['debug']: |
| 339 | print('_match_gitfetch: remote=%s branch=%s commit=%s' % |
| 340 | (remote, branch, commit)) |
| 341 | |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 342 | try: |
| 343 | _git(['fetch', remote, branch]) |
| 344 | except subprocess.CalledProcessError: |
Tzung-Bi Shih | 436fdba | 2019-09-04 19:05:00 +0800 | [diff] [blame] | 345 | errprint('Error: Branch not in %s' % remote) |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 346 | sys.exit(1) |
| 347 | |
| 348 | url = remote |
| 349 | |
| 350 | if args['source_line'] is None: |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 351 | commit = _git(['rev-parse', commit]) |
Tzung-Bi Shih | a8f310d | 2019-09-04 19:10:10 +0800 | [diff] [blame] | 352 | args['source_line'] = ( |
| 353 | '(cherry picked from commit %s\n %s %s)' % (commit, url, branch)) |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 354 | if args['tag'] is None: |
| 355 | args['tag'] = 'FROMGIT: ' |
| 356 | |
Stephen Boyd | 4b3869a | 2020-01-24 15:35:37 -0800 | [diff] [blame] | 357 | if args['replace']: |
| 358 | _git(['reset', '--hard', 'HEAD~1']) |
| 359 | |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 360 | return _git_returncode(['cherry-pick', commit]) |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 361 | |
Stephen Boyd | 9639603 | 2020-02-25 10:12:59 -0800 | [diff] [blame] | 362 | def _match_gitweb(match, args): |
| 363 | """Match location: https://repoURL/commit/?h=branch&id=HASH.""" |
| 364 | remote = match.group(1) |
| 365 | branch = match.group(2) |
| 366 | commit = match.group(3) |
| 367 | |
| 368 | if args['debug']: |
| 369 | print('_match_gitweb: remote=%s branch=%s commit=%s' % |
| 370 | (remote, branch, commit)) |
| 371 | |
| 372 | try: |
| 373 | _git(['fetch', remote, branch]) |
| 374 | except subprocess.CalledProcessError: |
| 375 | errprint('Error: Branch not in %s' % remote) |
| 376 | sys.exit(1) |
| 377 | |
| 378 | url = remote |
| 379 | |
| 380 | if args['source_line'] is None: |
| 381 | commit = _git(['rev-parse', commit]) |
| 382 | args['source_line'] = ( |
| 383 | '(cherry picked from commit %s\n %s %s)' % (commit, url, branch)) |
| 384 | if args['tag'] is None: |
| 385 | args['tag'] = 'FROMGIT: ' |
| 386 | |
| 387 | if args['replace']: |
| 388 | _git(['reset', '--hard', 'HEAD~1']) |
| 389 | |
| 390 | return _git_returncode(['cherry-pick', commit]) |
| 391 | |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 392 | def main(args): |
| 393 | """This is the main entrypoint for fromupstream. |
| 394 | |
| 395 | Args: |
| 396 | args: sys.argv[1:] |
| 397 | |
| 398 | Returns: |
| 399 | An int return code. |
| 400 | """ |
| 401 | parser = argparse.ArgumentParser() |
| 402 | |
| 403 | parser.add_argument('--bug', '-b', |
Guenter Roeck | f47a50c | 2018-07-25 12:41:36 -0700 | [diff] [blame] | 404 | type=str, help='BUG= line') |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 405 | parser.add_argument('--test', '-t', |
Guenter Roeck | f47a50c | 2018-07-25 12:41:36 -0700 | [diff] [blame] | 406 | type=str, help='TEST= line') |
Stephen Boyd | 24b309b | 2018-11-06 22:11:00 -0800 | [diff] [blame] | 407 | parser.add_argument('--crbug', action='append', |
| 408 | type=int, help='BUG=chromium: line') |
| 409 | parser.add_argument('--buganizer', action='append', |
| 410 | type=int, help='BUG=b: line') |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 411 | parser.add_argument('--changeid', '-c', |
| 412 | help='Overrides the gerrit generated Change-Id line') |
| 413 | |
Tzung-Bi Shih | f5d25a8 | 2019-09-02 11:40:09 +0800 | [diff] [blame] | 414 | parser.add_argument('--replace', '-r', |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 415 | action='store_true', |
Tzung-Bi Shih | 196d31e | 2019-09-01 18:16:28 +0800 | [diff] [blame] | 416 | help='Replaces the HEAD commit with this one, taking ' |
| 417 | 'its properties(BUG, TEST, Change-Id). Useful for ' |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 418 | 'updating commits.') |
| 419 | parser.add_argument('--nosignoff', |
| 420 | dest='signoff', action='store_false') |
Tzung-Bi Shih | 5100c74 | 2019-09-02 10:28:32 +0800 | [diff] [blame] | 421 | parser.add_argument('--debug', '-d', action='store_true', |
| 422 | help='Prints more verbose logs.') |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 423 | |
| 424 | parser.add_argument('--tag', |
| 425 | help='Overrides the tag from the title') |
| 426 | parser.add_argument('--source', '-s', |
| 427 | dest='source_line', type=str, |
Tzung-Bi Shih | 196d31e | 2019-09-01 18:16:28 +0800 | [diff] [blame] | 428 | help='Overrides the source line, last line, ex: ' |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 429 | '(am from http://....)') |
| 430 | parser.add_argument('locations', |
Douglas Anderson | c77a8b8 | 2018-05-04 17:02:03 -0700 | [diff] [blame] | 431 | nargs='+', |
Tzung-Bi Shih | 196d31e | 2019-09-01 18:16:28 +0800 | [diff] [blame] | 432 | help='Patchwork ID (pw://### or pw://PROJECT/###, ' |
| 433 | 'where PROJECT is defined in ~/.pwclientrc; if no ' |
| 434 | 'PROJECT is specified, the default is retrieved from ' |
| 435 | '~/.pwclientrc), ' |
Stephen Boyd | b68c17a | 2019-09-26 15:08:02 -0700 | [diff] [blame] | 436 | 'Message-ID (msgid://MSGID), ' |
Brian Norris | 8043cfd | 2020-03-19 11:46:16 -0700 | [diff] [blame] | 437 | 'linux commit like linux://HASH, ' |
Abhishek Pandit-Subedi | aea8c50 | 2020-07-09 21:56:12 -0700 | [diff] [blame] | 438 | 'upstream commit like upstream://HASH, or ' |
Tzung-Bi Shih | 196d31e | 2019-09-01 18:16:28 +0800 | [diff] [blame] | 439 | 'git reference like git://remote/branch/HASH or ' |
| 440 | 'git://repoURL#branch/HASH or ' |
Stephen Boyd | 9639603 | 2020-02-25 10:12:59 -0800 | [diff] [blame] | 441 | 'https://repoURL#branch/HASH or ' |
| 442 | 'https://repoURL/commit/?h=branch&id=HASH') |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 443 | |
| 444 | args = vars(parser.parse_args(args)) |
| 445 | |
Stephen Boyd | 24b309b | 2018-11-06 22:11:00 -0800 | [diff] [blame] | 446 | buglist = [args['bug']] if args['bug'] else [] |
| 447 | if args['buganizer']: |
| 448 | buglist += ['b:{0}'.format(x) for x in args['buganizer']] |
| 449 | if args['crbug']: |
| 450 | buglist += ['chromium:{0}'.format(x) for x in args['crbug']] |
Brian Norris | 667a0cb | 2018-12-07 09:28:46 -0800 | [diff] [blame] | 451 | if buglist: |
| 452 | args['bug'] = ', '.join(buglist) |
Stephen Boyd | 24b309b | 2018-11-06 22:11:00 -0800 | [diff] [blame] | 453 | |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 454 | if args['replace']: |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 455 | old_commit_message = _git(['show', '-s', '--format=%B', 'HEAD']) |
Tzung-Bi Shih | 231fada | 2019-09-02 00:54:59 +0800 | [diff] [blame] | 456 | |
| 457 | # It is possible that multiple Change-Ids are in the commit message |
| 458 | # (due to cherry picking). We only want to pull out the first one. |
| 459 | changeid_match = re.search('^Change-Id: (.*)$', |
| 460 | old_commit_message, re.MULTILINE) |
| 461 | if changeid_match: |
| 462 | args['changeid'] = changeid_match.group(1) |
| 463 | |
Tzung-Bi Shih | e1e0e7d | 2019-09-02 15:04:38 +0800 | [diff] [blame] | 464 | bugs = re.findall('^BUG=(.*)$', old_commit_message, re.MULTILINE) |
Tzung-Bi Shih | 0434530 | 2019-09-02 12:04:01 +0800 | [diff] [blame] | 465 | if args['bug'] is None and bugs: |
| 466 | args['bug'] = '\nBUG='.join(bugs) |
| 467 | |
Tzung-Bi Shih | e1e0e7d | 2019-09-02 15:04:38 +0800 | [diff] [blame] | 468 | tests = re.findall('^TEST=(.*)$', old_commit_message, re.MULTILINE) |
Tzung-Bi Shih | 0434530 | 2019-09-02 12:04:01 +0800 | [diff] [blame] | 469 | if args['test'] is None and tests: |
| 470 | args['test'] = '\nTEST='.join(tests) |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 471 | # TODO: deal with multiline BUG/TEST better |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 472 | |
Guenter Roeck | f47a50c | 2018-07-25 12:41:36 -0700 | [diff] [blame] | 473 | if args['bug'] is None or args['test'] is None: |
Tzung-Bi Shih | 196d31e | 2019-09-01 18:16:28 +0800 | [diff] [blame] | 474 | parser.error('BUG=/TEST= lines are required; --replace can help ' |
Stephen Boyd | e6fdf91 | 2018-11-09 10:30:57 -0800 | [diff] [blame] | 475 | 'automate, or set via --bug/--test') |
Guenter Roeck | f47a50c | 2018-07-25 12:41:36 -0700 | [diff] [blame] | 476 | |
Tzung-Bi Shih | 5100c74 | 2019-09-02 10:28:32 +0800 | [diff] [blame] | 477 | if args['debug']: |
| 478 | pprint.pprint(args) |
| 479 | |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 480 | re_matches = ( |
Tzung-Bi Shih | e1e0e7d | 2019-09-02 15:04:38 +0800 | [diff] [blame] | 481 | (re.compile(r'^pw://(([^/]+)/)?(\d+)'), _match_patchwork), |
Stephen Boyd | b68c17a | 2019-09-26 15:08:02 -0700 | [diff] [blame] | 482 | (re.compile(r'^msgid://<?([^>]*)>?'), _match_msgid), |
Abhishek Pandit-Subedi | aea8c50 | 2020-07-09 21:56:12 -0700 | [diff] [blame] | 483 | (re.compile(r'^linux://([0-9a-f]+)'), _match_upstream), |
| 484 | (re.compile(r'^upstream://([0-9a-f]+)'), _match_upstream), |
Tzung-Bi Shih | e1e0e7d | 2019-09-02 15:04:38 +0800 | [diff] [blame] | 485 | (re.compile(r'^(from)?git://([^/\#]+)/([^#]+)/([0-9a-f]+)$'), |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 486 | _match_fromgit), |
Tzung-Bi Shih | e1e0e7d | 2019-09-02 15:04:38 +0800 | [diff] [blame] | 487 | (re.compile(r'^((git|https)://.+)#(.+)/([0-9a-f]+)$'), _match_gitfetch), |
Stephen Boyd | 9639603 | 2020-02-25 10:12:59 -0800 | [diff] [blame] | 488 | (re.compile(r'^(https://.+)/commit/\?h=(.+)\&id=([0-9a-f]+)$'), _match_gitweb), |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 489 | ) |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 490 | |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 491 | for location in args['locations']: |
Tzung-Bi Shih | 5100c74 | 2019-09-02 10:28:32 +0800 | [diff] [blame] | 492 | if args['debug']: |
| 493 | print('location=%s' % location) |
| 494 | |
Tzung-Bi Shih | 886c909 | 2019-09-02 12:46:16 +0800 | [diff] [blame] | 495 | for reg, handler in re_matches: |
| 496 | match = reg.match(location) |
| 497 | if match: |
| 498 | ret = handler(match, args) |
| 499 | break |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 500 | else: |
Tzung-Bi Shih | 436fdba | 2019-09-04 19:05:00 +0800 | [diff] [blame] | 501 | errprint('Don\'t know what "%s" means.' % location) |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 502 | sys.exit(1) |
| 503 | |
| 504 | if ret != 0: |
Guenter Roeck | bdbb9cc | 2018-04-19 10:05:08 -0700 | [diff] [blame] | 505 | conflicts = _get_conflicts() |
Douglas Anderson | 2108e53 | 2018-04-30 09:50:42 -0700 | [diff] [blame] | 506 | if args['tag'] == 'UPSTREAM: ': |
| 507 | args['tag'] = 'BACKPORT: ' |
| 508 | else: |
| 509 | args['tag'] = 'BACKPORT: ' + args['tag'] |
Guenter Roeck | bdbb9cc | 2018-04-19 10:05:08 -0700 | [diff] [blame] | 510 | _pause_for_merge(conflicts) |
| 511 | else: |
Douglas Anderson | b6a10fe | 2019-08-12 13:53:30 -0700 | [diff] [blame] | 512 | conflicts = '' |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 513 | |
| 514 | # extract commit message |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 515 | commit_message = _git(['show', '-s', '--format=%B', 'HEAD']) |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 516 | |
Guenter Roeck | 2e4f251 | 2018-04-24 09:20:51 -0700 | [diff] [blame] | 517 | # Remove stray Change-Id, most likely from merge resolution |
| 518 | commit_message = re.sub(r'Change-Id:.*\n?', '', commit_message) |
| 519 | |
Brian Norris | 7a41b98 | 2018-06-01 10:28:29 -0700 | [diff] [blame] | 520 | # Note the source location before tagging anything else |
| 521 | commit_message += '\n' + args['source_line'] |
| 522 | |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 523 | # add automatic Change ID, BUG, and TEST (and maybe signoff too) so |
| 524 | # next commands know where to work on |
| 525 | commit_message += '\n' |
Guenter Roeck | bdbb9cc | 2018-04-19 10:05:08 -0700 | [diff] [blame] | 526 | commit_message += conflicts |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 527 | commit_message += '\n' + 'BUG=' + args['bug'] |
Harry Cutts | ae372f3 | 2019-02-12 18:01:14 -0800 | [diff] [blame] | 528 | commit_message += '\n' + _wrap_commit_line('TEST', args['test']) |
Brian Norris | 674209e | 2020-04-22 15:33:53 -0700 | [diff] [blame] | 529 | |
| 530 | extra = [] |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 531 | if args['signoff']: |
Brian Norris | 674209e | 2020-04-22 15:33:53 -0700 | [diff] [blame] | 532 | signoff = 'Signed-off-by: %s <%s>' % ( |
| 533 | _git(['config', 'user.name']), |
| 534 | _git(['config', 'user.email'])) |
| 535 | if not signoff in commit_message.splitlines(): |
| 536 | extra += ['-s'] |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 537 | _git(['commit'] + extra + ['--amend', '-F', '-'], stdin=commit_message) |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 538 | |
| 539 | # re-extract commit message |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 540 | commit_message = _git(['show', '-s', '--format=%B', 'HEAD']) |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 541 | |
Douglas Anderson | becd4e6 | 2019-09-25 13:40:55 -0700 | [diff] [blame] | 542 | # If we see a "Link: " that seems to point to a Message-Id with an |
| 543 | # automatic Change-Id we'll snarf it out. |
| 544 | mo = re.search(r'^Link:.*(I[a-f0-9]{40})@changeid', commit_message, |
| 545 | re.MULTILINE) |
| 546 | if mo and args['changeid'] is None: |
| 547 | args['changeid'] = mo.group(1) |
| 548 | |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 549 | # replace changeid if needed |
| 550 | if args['changeid'] is not None: |
| 551 | commit_message = re.sub(r'(Change-Id: )(\w+)', r'\1%s' % |
| 552 | args['changeid'], commit_message) |
| 553 | args['changeid'] = None |
| 554 | |
| 555 | # decorate it that it's from outside |
| 556 | commit_message = args['tag'] + commit_message |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 557 | |
| 558 | # commit everything |
Alexandru M Stan | 725c71f | 2019-12-11 16:53:33 -0800 | [diff] [blame] | 559 | _git(['commit', '--amend', '-F', '-'], stdin=commit_message) |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 560 | |
Chirantan Ekbote | 4b08e71 | 2019-06-12 15:35:41 +0900 | [diff] [blame] | 561 | return 0 |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 562 | |
| 563 | if __name__ == '__main__': |
| 564 | sys.exit(main(sys.argv[1:])) |