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