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