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 | |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 116 | def main(args): |
| 117 | """This is the main entrypoint for fromupstream. |
| 118 | |
| 119 | Args: |
| 120 | args: sys.argv[1:] |
| 121 | |
| 122 | Returns: |
| 123 | An int return code. |
| 124 | """ |
| 125 | parser = argparse.ArgumentParser() |
| 126 | |
| 127 | parser.add_argument('--bug', '-b', |
Guenter Roeck | f47a50c | 2018-07-25 12:41:36 -0700 | [diff] [blame] | 128 | type=str, help='BUG= line') |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 129 | parser.add_argument('--test', '-t', |
Guenter Roeck | f47a50c | 2018-07-25 12:41:36 -0700 | [diff] [blame] | 130 | type=str, help='TEST= line') |
Stephen Boyd | 24b309b | 2018-11-06 22:11:00 -0800 | [diff] [blame] | 131 | parser.add_argument('--crbug', action='append', |
| 132 | type=int, help='BUG=chromium: line') |
| 133 | parser.add_argument('--buganizer', action='append', |
| 134 | type=int, help='BUG=b: line') |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 135 | parser.add_argument('--changeid', '-c', |
| 136 | help='Overrides the gerrit generated Change-Id line') |
| 137 | |
Tzung-Bi Shih | f5d25a8 | 2019-09-02 11:40:09 +0800 | [diff] [blame] | 138 | parser.add_argument('--replace', '-r', |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 139 | action='store_true', |
Tzung-Bi Shih | 196d31e | 2019-09-01 18:16:28 +0800 | [diff] [blame] | 140 | help='Replaces the HEAD commit with this one, taking ' |
| 141 | 'its properties(BUG, TEST, Change-Id). Useful for ' |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 142 | 'updating commits.') |
| 143 | parser.add_argument('--nosignoff', |
| 144 | dest='signoff', action='store_false') |
Tzung-Bi Shih | 5100c74 | 2019-09-02 10:28:32 +0800 | [diff] [blame] | 145 | parser.add_argument('--debug', '-d', action='store_true', |
| 146 | help='Prints more verbose logs.') |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 147 | |
| 148 | parser.add_argument('--tag', |
| 149 | help='Overrides the tag from the title') |
| 150 | parser.add_argument('--source', '-s', |
| 151 | dest='source_line', type=str, |
Tzung-Bi Shih | 196d31e | 2019-09-01 18:16:28 +0800 | [diff] [blame] | 152 | help='Overrides the source line, last line, ex: ' |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 153 | '(am from http://....)') |
| 154 | parser.add_argument('locations', |
Douglas Anderson | c77a8b8 | 2018-05-04 17:02:03 -0700 | [diff] [blame] | 155 | nargs='+', |
Tzung-Bi Shih | 196d31e | 2019-09-01 18:16:28 +0800 | [diff] [blame] | 156 | help='Patchwork ID (pw://### or pw://PROJECT/###, ' |
| 157 | 'where PROJECT is defined in ~/.pwclientrc; if no ' |
| 158 | 'PROJECT is specified, the default is retrieved from ' |
| 159 | '~/.pwclientrc), ' |
| 160 | 'linux commit like linux://HASH, or ' |
| 161 | 'git reference like git://remote/branch/HASH or ' |
| 162 | 'git://repoURL#branch/HASH or ' |
Stephen Boyd | 66ea191 | 2018-10-30 11:26:54 -0700 | [diff] [blame] | 163 | 'https://repoURL#branch/HASH') |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 164 | |
| 165 | args = vars(parser.parse_args(args)) |
| 166 | |
Stephen Boyd | 24b309b | 2018-11-06 22:11:00 -0800 | [diff] [blame] | 167 | buglist = [args['bug']] if args['bug'] else [] |
| 168 | if args['buganizer']: |
| 169 | buglist += ['b:{0}'.format(x) for x in args['buganizer']] |
| 170 | if args['crbug']: |
| 171 | buglist += ['chromium:{0}'.format(x) for x in args['crbug']] |
Brian Norris | 667a0cb | 2018-12-07 09:28:46 -0800 | [diff] [blame] | 172 | if buglist: |
| 173 | args['bug'] = ', '.join(buglist) |
Stephen Boyd | 24b309b | 2018-11-06 22:11:00 -0800 | [diff] [blame] | 174 | |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 175 | if args['replace']: |
| 176 | old_commit_message = subprocess.check_output( |
| 177 | ['git', 'show', '-s', '--format=%B', 'HEAD'] |
| 178 | ).strip('\n') |
Tzung-Bi Shih | 231fada | 2019-09-02 00:54:59 +0800 | [diff] [blame^] | 179 | |
| 180 | # It is possible that multiple Change-Ids are in the commit message |
| 181 | # (due to cherry picking). We only want to pull out the first one. |
| 182 | changeid_match = re.search('^Change-Id: (.*)$', |
| 183 | old_commit_message, re.MULTILINE) |
| 184 | if changeid_match: |
| 185 | args['changeid'] = changeid_match.group(1) |
| 186 | |
Guenter Roeck | f47a50c | 2018-07-25 12:41:36 -0700 | [diff] [blame] | 187 | if args['bug'] == parser.get_default('bug') and \ |
| 188 | re.findall('BUG=(.*)$', old_commit_message, re.MULTILINE): |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 189 | args['bug'] = '\nBUG='.join(re.findall('BUG=(.*)$', |
| 190 | old_commit_message, |
| 191 | re.MULTILINE)) |
Guenter Roeck | f47a50c | 2018-07-25 12:41:36 -0700 | [diff] [blame] | 192 | if args['test'] == parser.get_default('test') and \ |
| 193 | re.findall('TEST=(.*)$', old_commit_message, re.MULTILINE): |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 194 | args['test'] = '\nTEST='.join(re.findall('TEST=(.*)$', |
| 195 | old_commit_message, |
| 196 | re.MULTILINE)) |
| 197 | # TODO: deal with multiline BUG/TEST better |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 198 | |
Guenter Roeck | f47a50c | 2018-07-25 12:41:36 -0700 | [diff] [blame] | 199 | if args['bug'] is None or args['test'] is None: |
Tzung-Bi Shih | 196d31e | 2019-09-01 18:16:28 +0800 | [diff] [blame] | 200 | parser.error('BUG=/TEST= lines are required; --replace can help ' |
Stephen Boyd | e6fdf91 | 2018-11-09 10:30:57 -0800 | [diff] [blame] | 201 | 'automate, or set via --bug/--test') |
Guenter Roeck | f47a50c | 2018-07-25 12:41:36 -0700 | [diff] [blame] | 202 | |
Tzung-Bi Shih | 5100c74 | 2019-09-02 10:28:32 +0800 | [diff] [blame] | 203 | if args['debug']: |
| 204 | pprint.pprint(args) |
| 205 | |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 206 | while len(args['locations']) > 0: |
| 207 | location = args['locations'].pop(0) |
| 208 | |
Tzung-Bi Shih | 5100c74 | 2019-09-02 10:28:32 +0800 | [diff] [blame] | 209 | if args['debug']: |
| 210 | print('location=%s' % location) |
| 211 | |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 212 | patchwork_match = re.match( |
Douglas Anderson | 4a974f0 | 2019-06-03 08:57:15 -0700 | [diff] [blame] | 213 | r'pw://(([^/]+)/)?(\d+)', location |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 214 | ) |
| 215 | linux_match = re.match( |
| 216 | r'linux://([0-9a-f]+)', location |
| 217 | ) |
| 218 | fromgit_match = re.match( |
Stephen Boyd | 66ea191 | 2018-10-30 11:26:54 -0700 | [diff] [blame] | 219 | r'(from)?git://([^/\#]+)/([^#]+)/([0-9a-f]+)$', location |
| 220 | ) |
| 221 | gitfetch_match = re.match( |
| 222 | r'((git|https)://.+)#(.+)/([0-9a-f]+)$', location |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 223 | ) |
| 224 | |
| 225 | if patchwork_match is not None: |
Brian Norris | 9f8a2be | 2018-06-01 11:14:08 -0700 | [diff] [blame] | 226 | pw_project = patchwork_match.group(2) |
| 227 | patch_id = int(patchwork_match.group(3)) |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 228 | |
Tzung-Bi Shih | 5100c74 | 2019-09-02 10:28:32 +0800 | [diff] [blame] | 229 | if args['debug']: |
| 230 | print('patchwork match: pw_project=%s, patch_id=%d' % |
| 231 | (pw_project, patch_id)) |
| 232 | |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 233 | if args['tag'] is None: |
| 234 | args['tag'] = 'FROMLIST: ' |
| 235 | |
Brian Norris | 2d4e976 | 2018-08-15 13:11:47 -0700 | [diff] [blame] | 236 | url = _get_pw_url(pw_project) |
Douglas Anderson | b6a10fe | 2019-08-12 13:53:30 -0700 | [diff] [blame] | 237 | opener = urllib.urlopen('%s/patch/%d/mbox' % (url, patch_id)) |
Brian Norris | 2d4e976 | 2018-08-15 13:11:47 -0700 | [diff] [blame] | 238 | if opener.getcode() != 200: |
| 239 | sys.stderr.write('Error: could not download patch - error code %d\n' \ |
| 240 | % opener.getcode()) |
| 241 | sys.exit(1) |
| 242 | patch_contents = opener.read() |
Brian Norris | 9f8a2be | 2018-06-01 11:14:08 -0700 | [diff] [blame] | 243 | |
Brian Norris | 2d4e976 | 2018-08-15 13:11:47 -0700 | [diff] [blame] | 244 | if not patch_contents: |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 245 | sys.stderr.write('Error: No patch content found\n') |
| 246 | sys.exit(1) |
Guenter Roeck | abe49d8 | 2018-07-25 14:11:48 -0700 | [diff] [blame] | 247 | |
Brian Norris | 2d4e976 | 2018-08-15 13:11:47 -0700 | [diff] [blame] | 248 | if args['source_line'] is None: |
| 249 | args['source_line'] = '(am from %s/patch/%d/)' % (url, patch_id) |
Brian Norris | c342104 | 2018-08-15 14:17:26 -0700 | [diff] [blame] | 250 | message_id = mailbox.Message(patch_contents)['Message-Id'] |
| 251 | message_id = re.sub('^<|>$', '', message_id.strip()) |
| 252 | args['source_line'] += \ |
| 253 | '\n(also found at https://lkml.kernel.org/r/%s)' % \ |
| 254 | message_id |
Brian Norris | 2d4e976 | 2018-08-15 13:11:47 -0700 | [diff] [blame] | 255 | |
Guenter Roeck | abe49d8 | 2018-07-25 14:11:48 -0700 | [diff] [blame] | 256 | if args['replace']: |
| 257 | subprocess.call(['git', 'reset', '--hard', 'HEAD~1']) |
| 258 | |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 259 | git_am = subprocess.Popen(['git', 'am', '-3'], stdin=subprocess.PIPE) |
Brian Norris | 2d4e976 | 2018-08-15 13:11:47 -0700 | [diff] [blame] | 260 | git_am.communicate(patch_contents) |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 261 | ret = git_am.returncode |
| 262 | elif linux_match: |
| 263 | commit = linux_match.group(1) |
| 264 | |
Tzung-Bi Shih | 5100c74 | 2019-09-02 10:28:32 +0800 | [diff] [blame] | 265 | if args['debug']: |
| 266 | print('linux match: commit=%s' % commit) |
| 267 | |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 268 | # Confirm a 'linux' remote is setup. |
Guenter Roeck | d66daa7 | 2018-04-19 10:31:25 -0700 | [diff] [blame] | 269 | linux_remote = _find_linux_remote() |
| 270 | if not linux_remote: |
| 271 | sys.stderr.write('Error: need a valid upstream remote\n') |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 272 | sys.exit(1) |
| 273 | |
Guenter Roeck | d66daa7 | 2018-04-19 10:31:25 -0700 | [diff] [blame] | 274 | linux_master = '%s/master' % linux_remote |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 275 | ret = subprocess.call(['git', 'merge-base', '--is-ancestor', |
Guenter Roeck | d66daa7 | 2018-04-19 10:31:25 -0700 | [diff] [blame] | 276 | commit, linux_master]) |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 277 | if ret: |
Guenter Roeck | d66daa7 | 2018-04-19 10:31:25 -0700 | [diff] [blame] | 278 | sys.stderr.write('Error: Commit not in %s\n' % linux_master) |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 279 | sys.exit(1) |
| 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 | |
| 286 | args['source_line'] = ('(cherry picked from commit %s)' % |
| 287 | (commit)) |
| 288 | if args['tag'] is None: |
| 289 | args['tag'] = 'UPSTREAM: ' |
| 290 | |
Guenter Roeck | abe49d8 | 2018-07-25 14:11:48 -0700 | [diff] [blame] | 291 | if args['replace']: |
| 292 | subprocess.call(['git', 'reset', '--hard', 'HEAD~1']) |
| 293 | |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 294 | ret = subprocess.call(['git', 'cherry-pick', commit]) |
| 295 | elif fromgit_match is not None: |
Stephen Boyd | 66ea191 | 2018-10-30 11:26:54 -0700 | [diff] [blame] | 296 | remote = fromgit_match.group(2) |
| 297 | branch = fromgit_match.group(3) |
| 298 | commit = fromgit_match.group(4) |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 299 | |
Tzung-Bi Shih | 5100c74 | 2019-09-02 10:28:32 +0800 | [diff] [blame] | 300 | if args['debug']: |
| 301 | print('fromgit match: remote=%s branch=%s commit=%s' % |
| 302 | (remote, branch, commit)) |
| 303 | |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 304 | ret = subprocess.call(['git', 'merge-base', '--is-ancestor', |
| 305 | commit, '%s/%s' % (remote, branch)]) |
| 306 | if ret: |
| 307 | sys.stderr.write('Error: Commit not in %s/%s\n' % |
| 308 | (remote, branch)) |
| 309 | sys.exit(1) |
| 310 | |
| 311 | git_pipe = subprocess.Popen(['git', 'remote', 'get-url', remote], |
| 312 | stdout=subprocess.PIPE) |
| 313 | url = git_pipe.communicate()[0].strip() |
| 314 | |
| 315 | if args['source_line'] is None: |
| 316 | git_pipe = subprocess.Popen(['git', 'rev-parse', commit], |
| 317 | stdout=subprocess.PIPE) |
| 318 | commit = git_pipe.communicate()[0].strip() |
| 319 | |
| 320 | args['source_line'] = \ |
| 321 | '(cherry picked from commit %s\n %s %s)' % \ |
| 322 | (commit, url, branch) |
| 323 | if args['tag'] is None: |
| 324 | args['tag'] = 'FROMGIT: ' |
| 325 | |
Guenter Roeck | abe49d8 | 2018-07-25 14:11:48 -0700 | [diff] [blame] | 326 | if args['replace']: |
| 327 | subprocess.call(['git', 'reset', '--hard', 'HEAD~1']) |
| 328 | |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 329 | ret = subprocess.call(['git', 'cherry-pick', commit]) |
Stephen Boyd | 66ea191 | 2018-10-30 11:26:54 -0700 | [diff] [blame] | 330 | elif gitfetch_match is not None: |
| 331 | remote = gitfetch_match.group(1) |
| 332 | branch = gitfetch_match.group(3) |
| 333 | commit = gitfetch_match.group(4) |
| 334 | |
Tzung-Bi Shih | 5100c74 | 2019-09-02 10:28:32 +0800 | [diff] [blame] | 335 | if args['debug']: |
| 336 | print('gitfetch match: remote=%s branch=%s commit=%s' % |
| 337 | (remote, branch, commit)) |
| 338 | |
Stephen Boyd | 66ea191 | 2018-10-30 11:26:54 -0700 | [diff] [blame] | 339 | ret = subprocess.call(['git', 'fetch', remote, branch]) |
| 340 | if ret: |
| 341 | sys.stderr.write('Error: Branch not in %s\n' % remote) |
| 342 | sys.exit(1) |
| 343 | |
| 344 | url = remote |
| 345 | |
| 346 | if args['source_line'] is None: |
| 347 | git_pipe = subprocess.Popen(['git', 'rev-parse', commit], |
| 348 | stdout=subprocess.PIPE) |
| 349 | commit = git_pipe.communicate()[0].strip() |
| 350 | |
| 351 | args['source_line'] = \ |
| 352 | '(cherry picked from commit %s\n %s %s)' % \ |
| 353 | (commit, url, branch) |
| 354 | if args['tag'] is None: |
| 355 | args['tag'] = 'FROMGIT: ' |
| 356 | |
| 357 | ret = subprocess.call(['git', 'cherry-pick', commit]) |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 358 | else: |
| 359 | sys.stderr.write('Don\'t know what "%s" means.\n' % location) |
| 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:])) |