Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2013-15 The CyanogenMod Project |
Dan Pasanen | 0fdc085 | 2016-12-27 10:32:16 -0600 | [diff] [blame] | 4 | # (C) 2017 The LineageOS Project |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | |
| 19 | # |
| 20 | # Run repopick.py -h for a description of this utility. |
| 21 | # |
| 22 | |
| 23 | from __future__ import print_function |
| 24 | |
| 25 | import sys |
| 26 | import json |
| 27 | import os |
| 28 | import subprocess |
| 29 | import re |
| 30 | import argparse |
| 31 | import textwrap |
Gabriele M | 5b610ae | 2018-03-31 14:26:59 +0200 | [diff] [blame] | 32 | from functools import cmp_to_key |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 33 | from xml.etree import ElementTree |
| 34 | |
| 35 | try: |
Dan Pasanen | 1cdd380 | 2017-01-23 15:08:52 -0600 | [diff] [blame] | 36 | import requests |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 37 | except ImportError: |
Dan Pasanen | 1cdd380 | 2017-01-23 15:08:52 -0600 | [diff] [blame] | 38 | try: |
| 39 | # For python3 |
| 40 | import urllib.error |
| 41 | import urllib.request |
| 42 | except ImportError: |
| 43 | # For python2 |
| 44 | import imp |
| 45 | import urllib2 |
| 46 | urllib = imp.new_module('urllib') |
| 47 | urllib.error = urllib2 |
| 48 | urllib.request = urllib2 |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 49 | |
| 50 | |
Luca Weiss | 5ee35ea | 2018-11-25 14:07:12 +0100 | [diff] [blame] | 51 | # cmp() is not available in Python 3, define it manually |
| 52 | # See https://docs.python.org/3.0/whatsnew/3.0.html#ordering-comparisons |
| 53 | def cmp(a, b): |
| 54 | return (a > b) - (a < b) |
| 55 | |
| 56 | |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 57 | # Verifies whether pathA is a subdirectory (or the same) as pathB |
| 58 | def is_subdir(a, b): |
| 59 | a = os.path.realpath(a) + '/' |
| 60 | b = os.path.realpath(b) + '/' |
| 61 | return b == a[:len(b)] |
| 62 | |
| 63 | |
| 64 | def fetch_query_via_ssh(remote_url, query): |
| 65 | """Given a remote_url and a query, return the list of changes that fit it |
| 66 | This function is slightly messy - the ssh api does not return data in the same structure as the HTTP REST API |
| 67 | We have to get the data, then transform it to match what we're expecting from the HTTP RESET API""" |
| 68 | if remote_url.count(':') == 2: |
| 69 | (uri, userhost, port) = remote_url.split(':') |
| 70 | userhost = userhost[2:] |
| 71 | elif remote_url.count(':') == 1: |
| 72 | (uri, userhost) = remote_url.split(':') |
| 73 | userhost = userhost[2:] |
| 74 | port = 29418 |
| 75 | else: |
| 76 | raise Exception('Malformed URI: Expecting ssh://[user@]host[:port]') |
| 77 | |
| 78 | |
| 79 | out = subprocess.check_output(['ssh', '-x', '-p{0}'.format(port), userhost, 'gerrit', 'query', '--format=JSON --patch-sets --current-patch-set', query]) |
| 80 | if not hasattr(out, 'encode'): |
| 81 | out = out.decode() |
| 82 | reviews = [] |
| 83 | for line in out.split('\n'): |
| 84 | try: |
| 85 | data = json.loads(line) |
| 86 | # make our data look like the http rest api data |
| 87 | review = { |
| 88 | 'branch': data['branch'], |
| 89 | 'change_id': data['id'], |
| 90 | 'current_revision': data['currentPatchSet']['revision'], |
| 91 | 'number': int(data['number']), |
| 92 | 'revisions': {patch_set['revision']: { |
Gabriele M | 0fcc122 | 2018-04-10 18:35:12 +0200 | [diff] [blame] | 93 | '_number': int(patch_set['number']), |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 94 | 'fetch': { |
| 95 | 'ssh': { |
| 96 | 'ref': patch_set['ref'], |
| 97 | 'url': 'ssh://{0}:{1}/{2}'.format(userhost, port, data['project']) |
| 98 | } |
Gabriele M | 0fcc122 | 2018-04-10 18:35:12 +0200 | [diff] [blame] | 99 | }, |
| 100 | 'commit': { |
| 101 | 'parents': [{ 'commit': parent } for parent in patch_set['parents']] |
| 102 | }, |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 103 | } for patch_set in data['patchSets']}, |
| 104 | 'subject': data['subject'], |
| 105 | 'project': data['project'], |
| 106 | 'status': data['status'] |
| 107 | } |
| 108 | reviews.append(review) |
| 109 | except: |
| 110 | pass |
| 111 | args.quiet or print('Found {0} reviews'.format(len(reviews))) |
| 112 | return reviews |
| 113 | |
| 114 | |
| 115 | def fetch_query_via_http(remote_url, query): |
Dan Pasanen | 1cdd380 | 2017-01-23 15:08:52 -0600 | [diff] [blame] | 116 | if "requests" in sys.modules: |
| 117 | auth = None |
| 118 | if os.path.isfile(os.getenv("HOME") + "/.gerritrc"): |
| 119 | f = open(os.getenv("HOME") + "/.gerritrc", "r") |
| 120 | for line in f: |
| 121 | parts = line.rstrip().split("|") |
| 122 | if parts[0] in remote_url: |
| 123 | auth = requests.auth.HTTPBasicAuth(username=parts[1], password=parts[2]) |
| 124 | statusCode = '-1' |
| 125 | if auth: |
Gabriele M | 5b610ae | 2018-03-31 14:26:59 +0200 | [diff] [blame] | 126 | url = '{0}/a/changes/?q={1}&o=CURRENT_REVISION&o=ALL_REVISIONS&o=ALL_COMMITS'.format(remote_url, query) |
Dan Pasanen | 1cdd380 | 2017-01-23 15:08:52 -0600 | [diff] [blame] | 127 | data = requests.get(url, auth=auth) |
| 128 | statusCode = str(data.status_code) |
| 129 | if statusCode != '200': |
| 130 | #They didn't get good authorization or data, Let's try the old way |
Gabriele M | 5b610ae | 2018-03-31 14:26:59 +0200 | [diff] [blame] | 131 | url = '{0}/changes/?q={1}&o=CURRENT_REVISION&o=ALL_REVISIONS&o=ALL_COMMITS'.format(remote_url, query) |
Dan Pasanen | 1cdd380 | 2017-01-23 15:08:52 -0600 | [diff] [blame] | 132 | data = requests.get(url) |
| 133 | reviews = json.loads(data.text[5:]) |
| 134 | else: |
| 135 | """Given a query, fetch the change numbers via http""" |
Gabriele M | 5b610ae | 2018-03-31 14:26:59 +0200 | [diff] [blame] | 136 | url = '{0}/changes/?q={1}&o=CURRENT_REVISION&o=ALL_REVISIONS&o=ALL_COMMITS'.format(remote_url, query) |
Dan Pasanen | 1cdd380 | 2017-01-23 15:08:52 -0600 | [diff] [blame] | 137 | data = urllib.request.urlopen(url).read().decode('utf-8') |
| 138 | reviews = json.loads(data[5:]) |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 139 | |
| 140 | for review in reviews: |
| 141 | review['number'] = review.pop('_number') |
| 142 | |
| 143 | return reviews |
| 144 | |
| 145 | |
| 146 | def fetch_query(remote_url, query): |
| 147 | """Wrapper for fetch_query_via_proto functions""" |
| 148 | if remote_url[0:3] == 'ssh': |
| 149 | return fetch_query_via_ssh(remote_url, query) |
| 150 | elif remote_url[0:4] == 'http': |
| 151 | return fetch_query_via_http(remote_url, query.replace(' ', '+')) |
| 152 | else: |
| 153 | raise Exception('Gerrit URL should be in the form http[s]://hostname/ or ssh://[user@]host[:port]') |
| 154 | |
| 155 | if __name__ == '__main__': |
Dan Pasanen | 0fdc085 | 2016-12-27 10:32:16 -0600 | [diff] [blame] | 156 | # Default to LineageOS Gerrit |
Dan Pasanen | 2965342 | 2017-12-12 15:07:09 -0600 | [diff] [blame] | 157 | default_gerrit = 'https://review.lineageos.org' |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 158 | |
| 159 | parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description=textwrap.dedent('''\ |
| 160 | repopick.py is a utility to simplify the process of cherry picking |
Dan Pasanen | 0fdc085 | 2016-12-27 10:32:16 -0600 | [diff] [blame] | 161 | patches from LineageOS's Gerrit instance (or any gerrit instance of your choosing) |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 162 | |
| 163 | Given a list of change numbers, repopick will cd into the project path |
| 164 | and cherry pick the latest patch available. |
| 165 | |
| 166 | With the --start-branch argument, the user can specify that a branch |
| 167 | should be created before cherry picking. This is useful for |
| 168 | cherry-picking many patches into a common branch which can be easily |
| 169 | abandoned later (good for testing other's changes.) |
| 170 | |
| 171 | The --abandon-first argument, when used in conjunction with the |
| 172 | --start-branch option, will cause repopick to abandon the specified |
| 173 | branch in all repos first before performing any cherry picks.''')) |
| 174 | parser.add_argument('change_number', nargs='*', help='change number to cherry pick. Use {change number}/{patchset number} to get a specific revision.') |
| 175 | parser.add_argument('-i', '--ignore-missing', action='store_true', help='do not error out if a patch applies to a missing directory') |
| 176 | parser.add_argument('-s', '--start-branch', nargs=1, help='start the specified branch before cherry picking') |
Harry Youd | 1c9c5a3 | 2017-07-18 18:52:42 +0100 | [diff] [blame] | 177 | parser.add_argument('-r', '--reset', action='store_true', help='reset to initial state (abort cherry-pick) if there is a conflict') |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 178 | parser.add_argument('-a', '--abandon-first', action='store_true', help='before cherry picking, abandon the branch specified in --start-branch') |
| 179 | parser.add_argument('-b', '--auto-branch', action='store_true', help='shortcut to "--start-branch auto --abandon-first --ignore-missing"') |
| 180 | parser.add_argument('-q', '--quiet', action='store_true', help='print as little as possible') |
| 181 | parser.add_argument('-v', '--verbose', action='store_true', help='print extra information to aid in debug') |
| 182 | parser.add_argument('-f', '--force', action='store_true', help='force cherry pick even if change is closed') |
| 183 | parser.add_argument('-p', '--pull', action='store_true', help='execute pull instead of cherry-pick') |
| 184 | parser.add_argument('-P', '--path', help='use the specified path for the change') |
| 185 | parser.add_argument('-t', '--topic', help='pick all commits from a specified topic') |
| 186 | parser.add_argument('-Q', '--query', help='pick all commits using the specified query') |
| 187 | parser.add_argument('-g', '--gerrit', default=default_gerrit, help='Gerrit Instance to use. Form proto://[user@]host[:port]') |
| 188 | parser.add_argument('-e', '--exclude', nargs=1, help='exclude a list of commit numbers separated by a ,') |
Adrian DC | df29022 | 2017-09-07 22:59:54 +0200 | [diff] [blame] | 189 | parser.add_argument('-c', '--check-picked', type=int, default=10, help='pass the amount of commits to check for already picked changes') |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 190 | args = parser.parse_args() |
| 191 | if not args.start_branch and args.abandon_first: |
| 192 | parser.error('if --abandon-first is set, you must also give the branch name with --start-branch') |
| 193 | if args.auto_branch: |
| 194 | args.abandon_first = True |
| 195 | args.ignore_missing = True |
| 196 | if not args.start_branch: |
| 197 | args.start_branch = ['auto'] |
| 198 | if args.quiet and args.verbose: |
| 199 | parser.error('--quiet and --verbose cannot be specified together') |
| 200 | |
| 201 | if (1 << bool(args.change_number) << bool(args.topic) << bool(args.query)) != 2: |
| 202 | parser.error('One (and only one) of change_number, topic, and query are allowed') |
| 203 | |
| 204 | # Change current directory to the top of the tree |
| 205 | if 'ANDROID_BUILD_TOP' in os.environ: |
| 206 | top = os.environ['ANDROID_BUILD_TOP'] |
| 207 | |
| 208 | if not is_subdir(os.getcwd(), top): |
| 209 | sys.stderr.write('ERROR: You must run this tool from within $ANDROID_BUILD_TOP!\n') |
| 210 | sys.exit(1) |
| 211 | os.chdir(os.environ['ANDROID_BUILD_TOP']) |
| 212 | |
| 213 | # Sanity check that we are being run from the top level of the tree |
| 214 | if not os.path.isdir('.repo'): |
| 215 | sys.stderr.write('ERROR: No .repo directory found. Please run this from the top of your tree.\n') |
| 216 | sys.exit(1) |
| 217 | |
| 218 | # If --abandon-first is given, abandon the branch before starting |
| 219 | if args.abandon_first: |
| 220 | # Determine if the branch already exists; skip the abandon if it does not |
| 221 | plist = subprocess.check_output(['repo', 'info']) |
| 222 | if not hasattr(plist, 'encode'): |
| 223 | plist = plist.decode() |
| 224 | needs_abandon = False |
| 225 | for pline in plist.splitlines(): |
| 226 | matchObj = re.match(r'Local Branches.*\[(.*)\]', pline) |
| 227 | if matchObj: |
| 228 | local_branches = re.split('\s*,\s*', matchObj.group(1)) |
| 229 | if any(args.start_branch[0] in s for s in local_branches): |
| 230 | needs_abandon = True |
| 231 | |
| 232 | if needs_abandon: |
| 233 | # Perform the abandon only if the branch already exists |
| 234 | if not args.quiet: |
| 235 | print('Abandoning branch: %s' % args.start_branch[0]) |
| 236 | subprocess.check_output(['repo', 'abandon', args.start_branch[0]]) |
| 237 | if not args.quiet: |
| 238 | print('') |
| 239 | |
| 240 | # Get the master manifest from repo |
| 241 | # - convert project name and revision to a path |
| 242 | project_name_to_data = {} |
| 243 | manifest = subprocess.check_output(['repo', 'manifest']) |
| 244 | xml_root = ElementTree.fromstring(manifest) |
| 245 | projects = xml_root.findall('project') |
| 246 | remotes = xml_root.findall('remote') |
| 247 | default_revision = xml_root.findall('default')[0].get('revision') |
| 248 | |
| 249 | #dump project data into the a list of dicts with the following data: |
| 250 | #{project: {path, revision}} |
| 251 | |
| 252 | for project in projects: |
| 253 | name = project.get('name') |
| 254 | path = project.get('path') |
| 255 | revision = project.get('revision') |
| 256 | if revision is None: |
| 257 | for remote in remotes: |
| 258 | if remote.get('name') == project.get('remote'): |
| 259 | revision = remote.get('revision') |
| 260 | if revision is None: |
Simon Shields | 2bdb18f | 2016-09-26 17:52:03 +1000 | [diff] [blame] | 261 | revision = default_revision |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 262 | |
| 263 | if not name in project_name_to_data: |
| 264 | project_name_to_data[name] = {} |
| 265 | revision = revision.split('refs/heads/')[-1] |
| 266 | project_name_to_data[name][revision] = path |
| 267 | |
| 268 | # get data on requested changes |
| 269 | reviews = [] |
| 270 | change_numbers = [] |
Gabriele M | 5b610ae | 2018-03-31 14:26:59 +0200 | [diff] [blame] | 271 | |
| 272 | def cmp_reviews(review_a, review_b): |
| 273 | current_a = review_a['current_revision'] |
| 274 | parents_a = [r['commit'] for r in review_a['revisions'][current_a]['commit']['parents']] |
| 275 | current_b = review_b['current_revision'] |
| 276 | parents_b = [r['commit'] for r in review_b['revisions'][current_b]['commit']['parents']] |
| 277 | if current_a in parents_b: |
| 278 | return -1 |
| 279 | elif current_b in parents_a: |
| 280 | return 1 |
| 281 | else: |
| 282 | return cmp(review_a['number'], review_b['number']) |
| 283 | |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 284 | if args.topic: |
| 285 | reviews = fetch_query(args.gerrit, 'topic:{0}'.format(args.topic)) |
Gabriele M | 5b610ae | 2018-03-31 14:26:59 +0200 | [diff] [blame] | 286 | change_numbers = [str(r['number']) for r in sorted(reviews, key=cmp_to_key(cmp_reviews))] |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 287 | if args.query: |
| 288 | reviews = fetch_query(args.gerrit, args.query) |
Gabriele M | 5b610ae | 2018-03-31 14:26:59 +0200 | [diff] [blame] | 289 | change_numbers = [str(r['number']) for r in sorted(reviews, key=cmp_to_key(cmp_reviews))] |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 290 | if args.change_number: |
Gabriele M | af970b6 | 2018-04-01 17:50:58 +0200 | [diff] [blame] | 291 | change_url_re = re.compile('https?://.+?/([0-9]+(?:/[0-9]+)?)/?') |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 292 | for c in args.change_number: |
Gabriele M | af970b6 | 2018-04-01 17:50:58 +0200 | [diff] [blame] | 293 | change_number = change_url_re.findall(c) |
| 294 | if change_number: |
| 295 | change_numbers.extend(change_number) |
| 296 | elif '-' in c: |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 297 | templist = c.split('-') |
| 298 | for i in range(int(templist[0]), int(templist[1]) + 1): |
| 299 | change_numbers.append(str(i)) |
| 300 | else: |
| 301 | change_numbers.append(c) |
| 302 | reviews = fetch_query(args.gerrit, ' OR '.join('change:{0}'.format(x.split('/')[0]) for x in change_numbers)) |
| 303 | |
| 304 | # make list of things to actually merge |
| 305 | mergables = [] |
| 306 | |
| 307 | # If --exclude is given, create the list of commits to ignore |
| 308 | exclude = [] |
| 309 | if args.exclude: |
| 310 | exclude = args.exclude[0].split(',') |
| 311 | |
| 312 | for change in change_numbers: |
| 313 | patchset = None |
| 314 | if '/' in change: |
| 315 | (change, patchset) = change.split('/') |
| 316 | |
| 317 | if change in exclude: |
| 318 | continue |
| 319 | |
| 320 | change = int(change) |
LuK1337 | ad5d9a0 | 2017-03-24 23:25:13 +0100 | [diff] [blame] | 321 | |
Gabriele M | de9e0ae | 2018-04-01 17:50:55 +0200 | [diff] [blame] | 322 | if patchset: |
LuK1337 | ad5d9a0 | 2017-03-24 23:25:13 +0100 | [diff] [blame] | 323 | patchset = int(patchset) |
| 324 | |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 325 | review = next((x for x in reviews if x['number'] == change), None) |
| 326 | if review is None: |
| 327 | print('Change %d not found, skipping' % change) |
| 328 | continue |
| 329 | |
| 330 | mergables.append({ |
| 331 | 'subject': review['subject'], |
| 332 | 'project': review['project'], |
| 333 | 'branch': review['branch'], |
| 334 | 'change_id': review['change_id'], |
| 335 | 'change_number': review['number'], |
| 336 | 'status': review['status'], |
Gabriele M | 88c0e5d | 2018-04-01 17:50:57 +0200 | [diff] [blame] | 337 | 'fetch': None, |
| 338 | 'patchset': review['revisions'][review['current_revision']]['_number'], |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 339 | }) |
Gabriele M | 88c0e5d | 2018-04-01 17:50:57 +0200 | [diff] [blame] | 340 | |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 341 | mergables[-1]['fetch'] = review['revisions'][review['current_revision']]['fetch'] |
| 342 | mergables[-1]['id'] = change |
| 343 | if patchset: |
| 344 | try: |
LuK1337 | 2756418 | 2017-03-24 20:00:48 +0100 | [diff] [blame] | 345 | mergables[-1]['fetch'] = [review['revisions'][x]['fetch'] for x in review['revisions'] if review['revisions'][x]['_number'] == patchset][0] |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 346 | mergables[-1]['id'] = '{0}/{1}'.format(change, patchset) |
Gabriele M | 88c0e5d | 2018-04-01 17:50:57 +0200 | [diff] [blame] | 347 | mergables[-1]['patchset'] = patchset |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 348 | except (IndexError, ValueError): |
| 349 | args.quiet or print('ERROR: The patch set {0}/{1} could not be found, using CURRENT_REVISION instead.'.format(change, patchset)) |
| 350 | |
| 351 | for item in mergables: |
| 352 | args.quiet or print('Applying change number {0}...'.format(item['id'])) |
| 353 | # Check if change is open and exit if it's not, unless -f is specified |
Dan Pasanen | 63f767e | 2017-07-09 09:41:33 -0500 | [diff] [blame] | 354 | if (item['status'] != 'OPEN' and item['status'] != 'NEW' and item['status'] != 'DRAFT') and not args.query: |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 355 | if args.force: |
| 356 | print('!! Force-picking a closed change !!\n') |
| 357 | else: |
| 358 | print('Change status is ' + item['status'] + '. Skipping the cherry pick.\nUse -f to force this pick.') |
| 359 | continue |
| 360 | |
| 361 | # Convert the project name to a project path |
| 362 | # - check that the project path exists |
| 363 | project_path = None |
| 364 | |
| 365 | if item['project'] in project_name_to_data and item['branch'] in project_name_to_data[item['project']]: |
| 366 | project_path = project_name_to_data[item['project']][item['branch']] |
| 367 | elif args.path: |
| 368 | project_path = args.path |
| 369 | elif args.ignore_missing: |
| 370 | print('WARNING: Skipping {0} since there is no project directory for: {1}\n'.format(item['id'], item['project'])) |
| 371 | continue |
| 372 | else: |
| 373 | sys.stderr.write('ERROR: For {0}, could not determine the project path for project {1}\n'.format(item['id'], item['project'])) |
| 374 | sys.exit(1) |
| 375 | |
| 376 | # If --start-branch is given, create the branch (more than once per path is okay; repo ignores gracefully) |
| 377 | if args.start_branch: |
| 378 | subprocess.check_output(['repo', 'start', args.start_branch[0], project_path]) |
| 379 | |
| 380 | # Determine the maximum commits to check already picked changes |
Adrian DC | df29022 | 2017-09-07 22:59:54 +0200 | [diff] [blame] | 381 | check_picked_count = args.check_picked |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 382 | branch_commits_count = int(subprocess.check_output(['git', 'rev-list', '--count', 'HEAD'], cwd=project_path)) |
| 383 | if branch_commits_count <= check_picked_count: |
| 384 | check_picked_count = branch_commits_count - 1 |
| 385 | |
| 386 | # Check if change is already picked to HEAD...HEAD~check_picked_count |
| 387 | found_change = False |
| 388 | for i in range(0, check_picked_count): |
Adrian DC | 13b02ff | 2016-12-04 12:30:26 +0100 | [diff] [blame] | 389 | if subprocess.call(['git', 'cat-file', '-e', 'HEAD~{0}'.format(i)], cwd=project_path, stderr=open(os.devnull, 'wb')): |
| 390 | continue |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 391 | output = subprocess.check_output(['git', 'show', '-q', 'HEAD~{0}'.format(i)], cwd=project_path).split() |
| 392 | if 'Change-Id:' in output: |
| 393 | head_change_id = '' |
| 394 | for j,t in enumerate(reversed(output)): |
| 395 | if t == 'Change-Id:': |
| 396 | head_change_id = output[len(output) - j] |
| 397 | break |
| 398 | if head_change_id.strip() == item['change_id']: |
| 399 | print('Skipping {0} - already picked in {1} as HEAD~{2}'.format(item['id'], project_path, i)) |
| 400 | found_change = True |
| 401 | break |
| 402 | if found_change: |
| 403 | continue |
| 404 | |
| 405 | # Print out some useful info |
| 406 | if not args.quiet: |
Dan Pasanen | d4ee2f6 | 2017-03-16 15:25:29 -0500 | [diff] [blame] | 407 | print('--> Subject: "{0}"'.format(item['subject'].encode('utf-8'))) |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 408 | print('--> Project path: {0}'.format(project_path)) |
Gabriele M | 88c0e5d | 2018-04-01 17:50:57 +0200 | [diff] [blame] | 409 | print('--> Change number: {0} (Patch Set {1})'.format(item['id'], item['patchset'])) |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 410 | |
| 411 | if 'anonymous http' in item['fetch']: |
| 412 | method = 'anonymous http' |
| 413 | else: |
| 414 | method = 'ssh' |
| 415 | |
| 416 | # Try fetching from GitHub first if using default gerrit |
| 417 | if args.gerrit == default_gerrit: |
| 418 | if args.verbose: |
| 419 | print('Trying to fetch the change from GitHub') |
| 420 | |
| 421 | if args.pull: |
| 422 | cmd = ['git pull --no-edit github', item['fetch'][method]['ref']] |
| 423 | else: |
| 424 | cmd = ['git fetch github', item['fetch'][method]['ref']] |
| 425 | if args.quiet: |
| 426 | cmd.append('--quiet') |
| 427 | else: |
| 428 | print(cmd) |
| 429 | result = subprocess.call([' '.join(cmd)], cwd=project_path, shell=True) |
| 430 | FETCH_HEAD = '{0}/.git/FETCH_HEAD'.format(project_path) |
| 431 | if result != 0 and os.stat(FETCH_HEAD).st_size != 0: |
| 432 | print('ERROR: git command failed') |
| 433 | sys.exit(result) |
| 434 | # Check if it worked |
| 435 | if args.gerrit != default_gerrit or os.stat(FETCH_HEAD).st_size == 0: |
| 436 | # If not using the default gerrit or github failed, fetch from gerrit. |
| 437 | if args.verbose: |
| 438 | if args.gerrit == default_gerrit: |
| 439 | print('Fetching from GitHub didn\'t work, trying to fetch the change from Gerrit') |
| 440 | else: |
| 441 | print('Fetching from {0}'.format(args.gerrit)) |
| 442 | |
| 443 | if args.pull: |
| 444 | cmd = ['git pull --no-edit', item['fetch'][method]['url'], item['fetch'][method]['ref']] |
| 445 | else: |
| 446 | cmd = ['git fetch', item['fetch'][method]['url'], item['fetch'][method]['ref']] |
| 447 | if args.quiet: |
| 448 | cmd.append('--quiet') |
| 449 | else: |
| 450 | print(cmd) |
| 451 | result = subprocess.call([' '.join(cmd)], cwd=project_path, shell=True) |
| 452 | if result != 0: |
| 453 | print('ERROR: git command failed') |
| 454 | sys.exit(result) |
| 455 | # Perform the cherry-pick |
| 456 | if not args.pull: |
Tim Schumacher | 49d26ba | 2018-10-13 14:37:54 +0200 | [diff] [blame] | 457 | cmd = ['git cherry-pick --ff FETCH_HEAD'] |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 458 | if args.quiet: |
| 459 | cmd_out = open(os.devnull, 'wb') |
| 460 | else: |
| 461 | cmd_out = None |
| 462 | result = subprocess.call(cmd, cwd=project_path, shell=True, stdout=cmd_out, stderr=cmd_out) |
| 463 | if result != 0: |
Adrian DC | 0f8230b | 2018-08-30 23:07:23 +0200 | [diff] [blame] | 464 | cmd = ['git diff-index --quiet HEAD --'] |
| 465 | result = subprocess.call(cmd, cwd=project_path, shell=True, stdout=cmd_out, stderr=cmd_out) |
| 466 | if result == 0: |
| 467 | print('WARNING: git command resulted with an empty commit, aborting cherry-pick') |
| 468 | cmd = ['git cherry-pick --abort'] |
| 469 | subprocess.call(cmd, cwd=project_path, shell=True, stdout=cmd_out, stderr=cmd_out) |
| 470 | elif args.reset: |
Harry Youd | 1c9c5a3 | 2017-07-18 18:52:42 +0100 | [diff] [blame] | 471 | print('ERROR: git command failed, aborting cherry-pick') |
| 472 | cmd = ['git cherry-pick --abort'] |
| 473 | subprocess.call(cmd, cwd=project_path, shell=True, stdout=cmd_out, stderr=cmd_out) |
Adrian DC | 0f8230b | 2018-08-30 23:07:23 +0200 | [diff] [blame] | 474 | sys.exit(result) |
Harry Youd | 1c9c5a3 | 2017-07-18 18:52:42 +0100 | [diff] [blame] | 475 | else: |
| 476 | print('ERROR: git command failed') |
Adrian DC | 0f8230b | 2018-08-30 23:07:23 +0200 | [diff] [blame] | 477 | sys.exit(result) |
Michael Bestas | 3952f6c | 2016-08-26 01:12:08 +0300 | [diff] [blame] | 478 | if not args.quiet: |
| 479 | print('') |