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