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