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