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