Søren Gjesse | 1525df3 | 2022-06-30 14:04:44 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright (c) 2022, the R8 project authors. Please see the AUTHORS file |
| 3 | # for details. All rights reserved. Use of this source code is governed by a |
| 4 | # BSD-style license that can be found in the LICENSE file. |
| 5 | |
| 6 | import argparse |
| 7 | import os |
| 8 | import subprocess |
| 9 | import sys |
| 10 | import utils |
| 11 | |
Ian Zerny | 33f3d81 | 2022-09-06 10:59:05 +0200 | [diff] [blame] | 12 | VERSION_FILE = 'src/main/java/com/android/tools/r8/Version.java' |
| 13 | VERSION_PREFIX = 'String LABEL = "' |
| 14 | |
Søren Gjesse | 1525df3 | 2022-06-30 14:04:44 +0200 | [diff] [blame] | 15 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 16 | def parse_options(): |
| 17 | parser = argparse.ArgumentParser(description='Release r8') |
| 18 | parser.add_argument('--branch', |
| 19 | metavar=('<branch>'), |
| 20 | help='Branch to cherry-pick to') |
| 21 | parser.add_argument('--current-checkout', |
| 22 | '--current_checkout', |
| 23 | default=False, |
| 24 | action='store_true', |
| 25 | help='Perform cherry picks into the current checkout') |
| 26 | parser.add_argument('--no-upload', |
| 27 | '--no_upload', |
| 28 | default=False, |
| 29 | action='store_true', |
| 30 | help='Do not upload to Gerrit') |
| 31 | parser.add_argument('hashes', |
| 32 | metavar='<hash>', |
| 33 | nargs='+', |
| 34 | help='Hashed to merge') |
Søren Gjesse | 0aa74cd | 2023-11-15 09:32:23 +0100 | [diff] [blame] | 35 | parser.add_argument('--remote', |
Christoffer Quist Adamsen | 7daed62 | 2023-11-10 11:10:31 +0100 | [diff] [blame] | 36 | default='origin', |
| 37 | help='The remote name (defaults to "origin")') |
Søren Gjesse | 4ce6d8b | 2024-11-13 11:17:59 +0100 | [diff] [blame] | 38 | parser.add_argument('--yes', |
| 39 | default=False, |
| 40 | action='store_true', |
| 41 | help='Answer "yes" to all questions') |
Søren Gjesse | e1b1875 | 2025-01-13 11:56:52 +0100 | [diff] [blame] | 42 | parser.add_argument('--reviewer', |
| 43 | metavar=('<reviewer(s)>'), |
| 44 | default=[], |
| 45 | action='append', |
| 46 | help='Rewiever(s) for the cherry-pick(s)') |
| 47 | parser.add_argument('--send-mail', |
| 48 | '--send_mail', |
| 49 | default=False, |
| 50 | action='store_true', |
| 51 | help='Send Gerrit review request right away') |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 52 | return parser.parse_args() |
Søren Gjesse | 1525df3 | 2022-06-30 14:04:44 +0200 | [diff] [blame] | 53 | |
| 54 | |
| 55 | def run(args): |
Christoffer Quist Adamsen | 7daed62 | 2023-11-10 11:10:31 +0100 | [diff] [blame] | 56 | if args.current_checkout: |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 57 | for i in range(len(args.hashes) + 1): |
Christoffer Quist Adamsen | 7daed62 | 2023-11-10 11:10:31 +0100 | [diff] [blame] | 58 | branch = 'cherry-%s-%d' % (args.branch, i + 1) |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 59 | print('Deleting branch %s' % branch) |
| 60 | subprocess.run(['git', 'branch', branch, '-D']) |
Søren Gjesse | 1525df3 | 2022-06-30 14:04:44 +0200 | [diff] [blame] | 61 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 62 | bugs = set() |
Ian Zerny | 33f3d81 | 2022-09-06 10:59:05 +0200 | [diff] [blame] | 63 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 64 | count = 1 |
| 65 | for hash in args.hashes: |
Christoffer Quist Adamsen | 7daed62 | 2023-11-10 11:10:31 +0100 | [diff] [blame] | 66 | branch = 'cherry-%s-%d' % (args.branch, count) |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 67 | print('Cherry-picking %s in %s' % (hash, branch)) |
Christoffer Quist Adamsen | 7daed62 | 2023-11-10 11:10:31 +0100 | [diff] [blame] | 68 | if count == 1: |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 69 | subprocess.run([ |
| 70 | 'git', 'new-branch', branch, '--upstream', |
Christoffer Quist Adamsen | 7daed62 | 2023-11-10 11:10:31 +0100 | [diff] [blame] | 71 | '%s/%s' % (args.remote, args.branch) |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 72 | ]) |
| 73 | else: |
| 74 | subprocess.run(['git', 'new-branch', branch, '--upstream-current']) |
| 75 | |
| 76 | subprocess.run(['git', 'cherry-pick', hash]) |
| 77 | confirm_and_upload(branch, args, bugs) |
| 78 | count = count + 1 |
| 79 | |
Christoffer Quist Adamsen | 7daed62 | 2023-11-10 11:10:31 +0100 | [diff] [blame] | 80 | branch = 'cherry-%s-%d' % (args.branch, count) |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 81 | subprocess.run(['git', 'new-branch', branch, '--upstream-current']) |
| 82 | |
| 83 | old_version = 'unknown' |
| 84 | for line in open(VERSION_FILE, 'r'): |
| 85 | index = line.find(VERSION_PREFIX) |
| 86 | if index > 0: |
| 87 | index += len(VERSION_PREFIX) |
| 88 | subline = line[index:] |
| 89 | old_version = subline[:subline.index('"')] |
| 90 | break |
| 91 | |
| 92 | new_version = 'unknown' |
Søren Gjesse | ba542a6 | 2024-05-31 13:01:09 +0200 | [diff] [blame] | 93 | if old_version.find('.') > 0 and old_version.find('-') == -1: |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 94 | split_version = old_version.split('.') |
| 95 | new_version = '.'.join(split_version[:-1] + |
| 96 | [str(int(split_version[-1]) + 1)]) |
| 97 | subprocess.run([ |
| 98 | 'sed', '-i', |
| 99 | 's/%s/%s/' % (old_version, new_version), VERSION_FILE |
| 100 | ]) |
Søren Gjesse | 1525df3 | 2022-06-30 14:04:44 +0200 | [diff] [blame] | 101 | else: |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 102 | editor = os.environ.get('VISUAL') |
| 103 | if not editor: |
| 104 | editor = os.environ.get('EDITOR') |
| 105 | if not editor: |
| 106 | editor = 'vi' |
Søren Gjesse | f7997e0 | 2024-12-16 10:26:43 +0100 | [diff] [blame] | 107 | input("\nCannot automatically determine the new version.\n" |
| 108 | + "Press [enter] to edit %s for version update with editor '%s'." % |
Søren Gjesse | ba542a6 | 2024-05-31 13:01:09 +0200 | [diff] [blame] | 109 | (VERSION_FILE, editor)) |
| 110 | subprocess.run([editor, VERSION_FILE]) |
Søren Gjesse | f7997e0 | 2024-12-16 10:26:43 +0100 | [diff] [blame] | 111 | new_version = input('Please enter the new version for the commit message: ') |
Søren Gjesse | 1525df3 | 2022-06-30 14:04:44 +0200 | [diff] [blame] | 112 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 113 | message = ("Version %s\n\n" % new_version) |
| 114 | for bug in sorted(bugs): |
| 115 | message += 'Bug: b/%s\n' % bug |
Søren Gjesse | 1525df3 | 2022-06-30 14:04:44 +0200 | [diff] [blame] | 116 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 117 | subprocess.run(['git', 'commit', '-a', '-m', message]) |
| 118 | confirm_and_upload(branch, args, None) |
Søren Gjesse | 4ce6d8b | 2024-11-13 11:17:59 +0100 | [diff] [blame] | 119 | if not args.current_checkout and not args.yes: |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 120 | while True: |
| 121 | try: |
| 122 | answer = input( |
| 123 | "Type 'delete' to finish and delete checkout in %s: " % |
| 124 | os.getcwd()) |
| 125 | if answer == 'delete': |
| 126 | break |
| 127 | except KeyboardInterrupt: |
| 128 | pass |
Ian Zerny | 33f3d81 | 2022-09-06 10:59:05 +0200 | [diff] [blame] | 129 | |
Søren Gjesse | 1525df3 | 2022-06-30 14:04:44 +0200 | [diff] [blame] | 130 | |
Søren Gjesse | e1b1875 | 2025-01-13 11:56:52 +0100 | [diff] [blame] | 131 | def reviewer_arg(reviewer): |
| 132 | if reviewer.find('@') == -1: |
| 133 | reviewer = reviewer + "@google.com" |
| 134 | return '--reviewer=' + reviewer |
| 135 | |
| 136 | |
Ian Zerny | 8b9c0af | 2022-09-07 12:56:03 +0200 | [diff] [blame] | 137 | def confirm_and_upload(branch, args, bugs): |
Søren Gjesse | 4ce6d8b | 2024-11-13 11:17:59 +0100 | [diff] [blame] | 138 | if not args.yes: |
| 139 | question = ('Ready to continue (cwd %s, will not upload to Gerrit)' % |
| 140 | os.getcwd() if args.no_upload else |
| 141 | 'Ready to upload %s (cwd %s)' % (branch, os.getcwd())) |
Ian Zerny | 8b9c0af | 2022-09-07 12:56:03 +0200 | [diff] [blame] | 142 | |
Søren Gjesse | 4ce6d8b | 2024-11-13 11:17:59 +0100 | [diff] [blame] | 143 | while True: |
| 144 | try: |
| 145 | answer = input(question + ' [yes/abort]? ') |
| 146 | if answer == 'yes': |
| 147 | break |
| 148 | if answer == 'abort': |
| 149 | print('Aborting new branch for %s' % branch) |
| 150 | sys.exit(1) |
| 151 | except KeyboardInterrupt: |
| 152 | pass |
Ian Zerny | 8b9c0af | 2022-09-07 12:56:03 +0200 | [diff] [blame] | 153 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 154 | # Compute the set of bug refs from the commit message after confirmation. |
| 155 | # If done before a conflicting cherry-pick status will potentially include |
| 156 | # references that are orthogonal to the pick. |
| 157 | if bugs != None: |
| 158 | commit_message = subprocess.check_output( |
| 159 | ['git', 'log', '--format=%B', '-n', '1', 'HEAD']) |
| 160 | commit_lines = [ |
| 161 | l.strip() for l in commit_message.decode('UTF-8').split('\n') |
| 162 | ] |
| 163 | for line in commit_lines: |
Christoffer Quist Adamsen | 7daed62 | 2023-11-10 11:10:31 +0100 | [diff] [blame] | 164 | bug = None |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 165 | if line.startswith('Bug: '): |
Christoffer Quist Adamsen | 7daed62 | 2023-11-10 11:10:31 +0100 | [diff] [blame] | 166 | bug = line.replace('Bug: ', '') |
| 167 | elif line.startswith('Fixed: '): |
| 168 | bug = line.replace('Fixed: ', '') |
| 169 | elif line.startswith('Fixes: '): |
| 170 | bug = line.replace('Fixes: ', '') |
| 171 | if bug: |
| 172 | bugs.add(bug.replace('b/', '').strip()) |
Ian Zerny | 8b9c0af | 2022-09-07 12:56:03 +0200 | [diff] [blame] | 173 | |
Søren Gjesse | e1b1875 | 2025-01-13 11:56:52 +0100 | [diff] [blame] | 174 | cmd = ['git', 'cl', 'upload', '--bypass-hooks'] |
| 175 | if args.yes: |
| 176 | cmd.append('-f') |
| 177 | if args.reviewer: |
| 178 | cmd.extend(map(reviewer_arg, args.reviewer)) |
| 179 | if args.send_mail: |
| 180 | cmd.append('--send-mail') |
Christoffer Quist Adamsen | 7daed62 | 2023-11-10 11:10:31 +0100 | [diff] [blame] | 181 | if not args.no_upload: |
Søren Gjesse | 4ce6d8b | 2024-11-13 11:17:59 +0100 | [diff] [blame] | 182 | subprocess.run(cmd) |
Søren Gjesse | e1b1875 | 2025-01-13 11:56:52 +0100 | [diff] [blame] | 183 | else: |
| 184 | print('Not uploading, upload command was "%s"' % cmd) |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 185 | |
Ian Zerny | 33f3d81 | 2022-09-06 10:59:05 +0200 | [diff] [blame] | 186 | |
Søren Gjesse | 1525df3 | 2022-06-30 14:04:44 +0200 | [diff] [blame] | 187 | def main(): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 188 | args = parse_options() |
Søren Gjesse | 1525df3 | 2022-06-30 14:04:44 +0200 | [diff] [blame] | 189 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 190 | if (not args.current_checkout): |
| 191 | with utils.TempDir() as temp: |
| 192 | print("Performing cherry-picking in %s" % temp) |
| 193 | subprocess.check_call(['git', 'clone', utils.REPO_SOURCE, temp]) |
| 194 | with utils.ChangedWorkingDirectory(temp): |
| 195 | run(args) |
| 196 | else: |
| 197 | # Run in current directory. |
| 198 | print("Performing cherry-picking in %s" % os.getcwd()) |
| 199 | subprocess.check_output(['git', 'fetch', 'origin']) |
Søren Gjesse | 1525df3 | 2022-06-30 14:04:44 +0200 | [diff] [blame] | 200 | run(args) |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 201 | |
Søren Gjesse | 1525df3 | 2022-06-30 14:04:44 +0200 | [diff] [blame] | 202 | |
| 203 | if __name__ == '__main__': |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 204 | sys.exit(main()) |