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