Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | # |
| 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. |
| 6 | """This is a tool for picking patches from upstream and applying them.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import argparse |
| 11 | import os |
| 12 | import re |
| 13 | import signal |
| 14 | import subprocess |
| 15 | import sys |
| 16 | |
| 17 | LINUX_URLS = ( |
| 18 | 'git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git', |
| 19 | 'https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git', |
| 20 | 'https://kernel.googlesource.com/pub/scm/linux/kernel/git/torvalds/linux.git', |
| 21 | ) |
| 22 | |
Guenter Roeck | bdbb9cc | 2018-04-19 10:05:08 -0700 | [diff] [blame] | 23 | def _get_conflicts(): |
| 24 | """Report conflicting files.""" |
| 25 | resolutions = ('DD', 'AU', 'UD', 'UA', 'DU', 'AA', 'UU') |
| 26 | conflicts = [] |
Douglas Anderson | 46287f9 | 2018-04-30 09:58:24 -0700 | [diff] [blame^] | 27 | lines = subprocess.check_output(['git', 'status', '--porcelain', |
Guenter Roeck | bdbb9cc | 2018-04-19 10:05:08 -0700 | [diff] [blame] | 28 | '--untracked-files=no']).split('\n') |
Douglas Anderson | 46287f9 | 2018-04-30 09:58:24 -0700 | [diff] [blame^] | 29 | for line in lines: |
| 30 | if not line: |
Guenter Roeck | bdbb9cc | 2018-04-19 10:05:08 -0700 | [diff] [blame] | 31 | continue |
Douglas Anderson | 46287f9 | 2018-04-30 09:58:24 -0700 | [diff] [blame^] | 32 | resolution, name = line.split(None, 1) |
Guenter Roeck | bdbb9cc | 2018-04-19 10:05:08 -0700 | [diff] [blame] | 33 | if resolution in resolutions: |
| 34 | conflicts.append(' ' + name) |
| 35 | if not conflicts: |
| 36 | return "" |
| 37 | return '\nConflicts:\n%s\n' % '\n'.join(conflicts) |
| 38 | |
Guenter Roeck | d66daa7 | 2018-04-19 10:31:25 -0700 | [diff] [blame] | 39 | def _find_linux_remote(): |
| 40 | """Find a remote pointing to a Linux upstream repository.""" |
| 41 | git_remote = subprocess.Popen(['git', 'remote'], stdout=subprocess.PIPE) |
| 42 | remotes = git_remote.communicate()[0].strip() |
| 43 | for remote in remotes.splitlines(): |
| 44 | rurl = subprocess.Popen(['git', 'remote', 'get-url', remote], |
| 45 | stdout=subprocess.PIPE) |
| 46 | url = rurl.communicate()[0].strip() |
| 47 | if not rurl.returncode and url in LINUX_URLS: |
| 48 | return remote |
| 49 | return None |
| 50 | |
Guenter Roeck | bdbb9cc | 2018-04-19 10:05:08 -0700 | [diff] [blame] | 51 | def _pause_for_merge(conflicts): |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 52 | """Pause and go in the background till user resolves the conflicts.""" |
| 53 | |
| 54 | git_root = subprocess.check_output(['git', 'rev-parse', |
| 55 | '--show-toplevel']).strip('\n') |
| 56 | |
| 57 | paths = ( |
| 58 | os.path.join(git_root, '.git', 'rebase-apply'), |
| 59 | os.path.join(git_root, '.git', 'CHERRY_PICK_HEAD'), |
| 60 | ) |
| 61 | for path in paths: |
| 62 | if os.path.exists(path): |
| 63 | sys.stderr.write('Found "%s".\n' % path) |
Guenter Roeck | bdbb9cc | 2018-04-19 10:05:08 -0700 | [diff] [blame] | 64 | sys.stderr.write(conflicts) |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 65 | sys.stderr.write('Please resolve the conflicts and restart the ' + |
| 66 | 'shell job when done. Kill this job if you ' + |
| 67 | 'aborted the conflict.\n') |
| 68 | os.kill(os.getpid(), signal.SIGTSTP) |
| 69 | # TODO: figure out what the state is after the merging, and go based on |
| 70 | # that (should we abort? skip? continue?) |
| 71 | # Perhaps check last commit message to see if it's the one we were using. |
| 72 | |
| 73 | def main(args): |
| 74 | """This is the main entrypoint for fromupstream. |
| 75 | |
| 76 | Args: |
| 77 | args: sys.argv[1:] |
| 78 | |
| 79 | Returns: |
| 80 | An int return code. |
| 81 | """ |
| 82 | parser = argparse.ArgumentParser() |
| 83 | |
| 84 | parser.add_argument('--bug', '-b', |
| 85 | type=str, default='None', help='BUG= line') |
| 86 | parser.add_argument('--test', '-t', |
| 87 | type=str, default='Build and boot', help='TEST= line') |
| 88 | parser.add_argument('--changeid', '-c', |
| 89 | help='Overrides the gerrit generated Change-Id line') |
| 90 | |
| 91 | parser.add_argument('--replace', |
| 92 | action='store_true', |
| 93 | help='Replaces the HEAD commit with this one, taking ' + |
| 94 | 'its properties(BUG, TEST, Change-Id). Useful for ' + |
| 95 | 'updating commits.') |
| 96 | parser.add_argument('--nosignoff', |
| 97 | dest='signoff', action='store_false') |
| 98 | |
| 99 | parser.add_argument('--tag', |
| 100 | help='Overrides the tag from the title') |
| 101 | parser.add_argument('--source', '-s', |
| 102 | dest='source_line', type=str, |
| 103 | help='Overrides the source line, last line, ex: ' + |
| 104 | '(am from http://....)') |
| 105 | parser.add_argument('locations', |
| 106 | nargs='*', |
| 107 | help='Patchwork url (either ' + |
| 108 | 'https://patchwork.kernel.org/patch/###/ or ' + |
| 109 | 'pw://###), linux commit like linux://HASH, git ' + |
| 110 | 'refrerence like fromgit://remote/branch/HASH') |
| 111 | |
| 112 | args = vars(parser.parse_args(args)) |
| 113 | |
| 114 | if args['replace']: |
| 115 | old_commit_message = subprocess.check_output( |
| 116 | ['git', 'show', '-s', '--format=%B', 'HEAD'] |
| 117 | ).strip('\n') |
| 118 | args['changeid'] = re.findall('Change-Id: (.*)$', |
| 119 | old_commit_message, re.MULTILINE)[0] |
| 120 | if args['bug'] == parser.get_default('bug'): |
| 121 | args['bug'] = '\nBUG='.join(re.findall('BUG=(.*)$', |
| 122 | old_commit_message, |
| 123 | re.MULTILINE)) |
| 124 | if args['test'] == parser.get_default('test'): |
| 125 | args['test'] = '\nTEST='.join(re.findall('TEST=(.*)$', |
| 126 | old_commit_message, |
| 127 | re.MULTILINE)) |
| 128 | # TODO: deal with multiline BUG/TEST better |
| 129 | subprocess.call(['git', 'reset', '--hard', 'HEAD~1']) |
| 130 | |
| 131 | while len(args['locations']) > 0: |
| 132 | location = args['locations'].pop(0) |
| 133 | |
| 134 | patchwork_match = re.match( |
| 135 | r'((pw://)|(https?://patchwork.kernel.org/patch/))(\d+)/?', location |
| 136 | ) |
| 137 | linux_match = re.match( |
| 138 | r'linux://([0-9a-f]+)', location |
| 139 | ) |
| 140 | fromgit_match = re.match( |
| 141 | r'fromgit://([^/]+)/(.+)/([0-9a-f]+)$', location |
| 142 | ) |
| 143 | |
| 144 | if patchwork_match is not None: |
| 145 | patch_id = int(patchwork_match.group(4)) |
| 146 | |
| 147 | if args['source_line'] is None: |
| 148 | args['source_line'] = \ |
| 149 | '(am from https://patchwork.kernel.org/patch/%d/)' % \ |
| 150 | patch_id |
| 151 | if args['tag'] is None: |
| 152 | args['tag'] = 'FROMLIST: ' |
| 153 | |
| 154 | pw_pipe = subprocess.Popen(['pwclient', 'view', str(patch_id)], |
| 155 | stdout=subprocess.PIPE) |
| 156 | s = pw_pipe.communicate()[0] |
| 157 | |
| 158 | if not s: |
| 159 | sys.stderr.write('Error: No patch content found\n') |
| 160 | sys.exit(1) |
| 161 | git_am = subprocess.Popen(['git', 'am', '-3'], stdin=subprocess.PIPE) |
| 162 | git_am.communicate(unicode(s).encode('utf-8')) |
| 163 | ret = git_am.returncode |
| 164 | elif linux_match: |
| 165 | commit = linux_match.group(1) |
| 166 | |
| 167 | # Confirm a 'linux' remote is setup. |
Guenter Roeck | d66daa7 | 2018-04-19 10:31:25 -0700 | [diff] [blame] | 168 | linux_remote = _find_linux_remote() |
| 169 | if not linux_remote: |
| 170 | sys.stderr.write('Error: need a valid upstream remote\n') |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 171 | sys.exit(1) |
| 172 | |
Guenter Roeck | d66daa7 | 2018-04-19 10:31:25 -0700 | [diff] [blame] | 173 | linux_master = '%s/master' % linux_remote |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 174 | ret = subprocess.call(['git', 'merge-base', '--is-ancestor', |
Guenter Roeck | d66daa7 | 2018-04-19 10:31:25 -0700 | [diff] [blame] | 175 | commit, linux_master]) |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 176 | if ret: |
Guenter Roeck | d66daa7 | 2018-04-19 10:31:25 -0700 | [diff] [blame] | 177 | sys.stderr.write('Error: Commit not in %s\n' % linux_master) |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 178 | sys.exit(1) |
| 179 | |
| 180 | if args['source_line'] is None: |
| 181 | git_pipe = subprocess.Popen(['git', 'rev-parse', commit], |
| 182 | stdout=subprocess.PIPE) |
| 183 | commit = git_pipe.communicate()[0].strip() |
| 184 | |
| 185 | args['source_line'] = ('(cherry picked from commit %s)' % |
| 186 | (commit)) |
| 187 | if args['tag'] is None: |
| 188 | args['tag'] = 'UPSTREAM: ' |
| 189 | |
| 190 | ret = subprocess.call(['git', 'cherry-pick', commit]) |
| 191 | elif fromgit_match is not None: |
| 192 | remote = fromgit_match.group(1) |
| 193 | branch = fromgit_match.group(2) |
| 194 | commit = fromgit_match.group(3) |
| 195 | |
| 196 | ret = subprocess.call(['git', 'merge-base', '--is-ancestor', |
| 197 | commit, '%s/%s' % (remote, branch)]) |
| 198 | if ret: |
| 199 | sys.stderr.write('Error: Commit not in %s/%s\n' % |
| 200 | (remote, branch)) |
| 201 | sys.exit(1) |
| 202 | |
| 203 | git_pipe = subprocess.Popen(['git', 'remote', 'get-url', remote], |
| 204 | stdout=subprocess.PIPE) |
| 205 | url = git_pipe.communicate()[0].strip() |
| 206 | |
| 207 | if args['source_line'] is None: |
| 208 | git_pipe = subprocess.Popen(['git', 'rev-parse', commit], |
| 209 | stdout=subprocess.PIPE) |
| 210 | commit = git_pipe.communicate()[0].strip() |
| 211 | |
| 212 | args['source_line'] = \ |
| 213 | '(cherry picked from commit %s\n %s %s)' % \ |
| 214 | (commit, url, branch) |
| 215 | if args['tag'] is None: |
| 216 | args['tag'] = 'FROMGIT: ' |
| 217 | |
| 218 | ret = subprocess.call(['git', 'cherry-pick', commit]) |
| 219 | else: |
| 220 | sys.stderr.write('Don\'t know what "%s" means.\n' % location) |
| 221 | sys.exit(1) |
| 222 | |
| 223 | if ret != 0: |
Guenter Roeck | bdbb9cc | 2018-04-19 10:05:08 -0700 | [diff] [blame] | 224 | conflicts = _get_conflicts() |
| 225 | args['tag'] = 'BACKPORT: ' |
| 226 | _pause_for_merge(conflicts) |
| 227 | else: |
| 228 | conflicts = "" |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 229 | |
| 230 | # extract commit message |
| 231 | commit_message = subprocess.check_output( |
| 232 | ['git', 'show', '-s', '--format=%B', 'HEAD'] |
| 233 | ).strip('\n') |
| 234 | |
Guenter Roeck | 2e4f251 | 2018-04-24 09:20:51 -0700 | [diff] [blame] | 235 | # Remove stray Change-Id, most likely from merge resolution |
| 236 | commit_message = re.sub(r'Change-Id:.*\n?', '', commit_message) |
| 237 | |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 238 | # add automatic Change ID, BUG, and TEST (and maybe signoff too) so |
| 239 | # next commands know where to work on |
| 240 | commit_message += '\n' |
Guenter Roeck | bdbb9cc | 2018-04-19 10:05:08 -0700 | [diff] [blame] | 241 | commit_message += conflicts |
Alexandru M Stan | fb5b5ee | 2014-12-04 13:32:55 -0800 | [diff] [blame] | 242 | commit_message += '\n' + 'BUG=' + args['bug'] |
| 243 | commit_message += '\n' + 'TEST=' + args['test'] |
| 244 | if args['signoff']: |
| 245 | extra = ['-s'] |
| 246 | else: |
| 247 | extra = [] |
| 248 | commit = subprocess.Popen( |
| 249 | ['git', 'commit'] + extra + ['--amend', '-F', '-'], |
| 250 | stdin=subprocess.PIPE |
| 251 | ).communicate(commit_message) |
| 252 | |
| 253 | # re-extract commit message |
| 254 | commit_message = subprocess.check_output( |
| 255 | ['git', 'show', '-s', '--format=%B', 'HEAD'] |
| 256 | ).strip('\n') |
| 257 | |
| 258 | # replace changeid if needed |
| 259 | if args['changeid'] is not None: |
| 260 | commit_message = re.sub(r'(Change-Id: )(\w+)', r'\1%s' % |
| 261 | args['changeid'], commit_message) |
| 262 | args['changeid'] = None |
| 263 | |
| 264 | # decorate it that it's from outside |
| 265 | commit_message = args['tag'] + commit_message |
| 266 | commit_message += '\n' + args['source_line'] |
| 267 | |
| 268 | # commit everything |
| 269 | commit = subprocess.Popen( |
| 270 | ['git', 'commit', '--amend', '-F', '-'], stdin=subprocess.PIPE |
| 271 | ).communicate(commit_message) |
| 272 | |
| 273 | return 0 |
| 274 | |
| 275 | if __name__ == '__main__': |
| 276 | sys.exit(main(sys.argv[1:])) |