From 45716abe5c62d3131af4d75920edbcc00375064a Mon Sep 17 00:00:00 2001 From: Gennady Tsitovich Date: Thu, 23 Jan 2025 01:18:11 -0800 Subject: [PATCH] Allow using `git cl cherry-pick` in a non-git environment. This includes: - adding `--host` parameter to `git-cl cherry-pick` - adding `GetCommitMessage` to gerrit_util to be able to switch from `git show` used to get the commit message for a change Bug: b/391547354 Change-Id: I9af227a78af5aaa1bdc98fc2520d98b413f0f3ed Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6180633 Reviewed-by: Josip Sokcevic Commit-Queue: Gennady Tsitovich Reviewed-by: Gavin Mak --- git_cl.py | 46 +++++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/git_cl.py b/git_cl.py index b0341eda1..1867e3dc2 100755 --- a/git_cl.py +++ b/git_cl.py @@ -4568,13 +4568,9 @@ def _create_commit_message(orig_message, bug=None): def CMDcherry_pick(parser, args): """Upload a chain of cherry picks to Gerrit. - This must be run inside the git repo you're trying to make changes to. + This should either be run inside the git repo you're trying to make changes + to or "--host" should be provided. """ - if gclient_utils.IsEnvCog(): - print('cherry-pick command is not supported in non-git environment', - file=sys.stderr) - return 1 - parser.add_option('--branch', help='Gerrit branch, e.g. refs/heads/main') parser.add_option('--bug', help='Bug to add to the description of each change.') @@ -4582,16 +4578,36 @@ def CMDcherry_pick(parser, args): type='int', help='The parent change of the first cherry-pick CL, ' 'i.e. the start of the CL chain.') + parser.add_option('--host', + default=None, + help='Gerrit host, needed in case the command is used in ' + 'a non-git environment.') options, args = parser.parse_args(args) + host = None + if options.host: + try: + host = urllib.parse.urlparse(host).hostname + except ValueError as e: + print(f'Unable to parse host: {host}. Error: {e}', file=sys.stderr) + return 1 + else: + try: + host = Changelist().GetGerritHost() + except subprocess2.CalledProcessError: + pass + + if not host: + print('Unable to determine host. cherry-pick command is not supported\n' + 'in non-git environment without "--host" provided', + file=sys.stderr) + return 1 + if not options.branch: parser.error('Branch is required.') if not args: parser.error('No revisions to cherry pick.') - # TODO(b/341792235): Consider using GetCommitMessage after b/362567930 is - # fixed so this command can be run outside of a git workspace. - host = Changelist().GetGerritHost() change_ids_to_message = {} change_ids_to_commit = {} @@ -4601,16 +4617,20 @@ def CMDcherry_pick(parser, args): # unique. Gerrit will error with "Multiple changes found" if we use a # non-unique ID. Instead, query Gerrit with the hash and verify it # corresponds to a unique CL. - changes = gerrit_util.QueryChanges(host, [('commit', commit)]) + changes = gerrit_util.QueryChanges( + host=host, + params=[('commit', commit)], + o_params=['CURRENT_REVISION', 'CURRENT_COMMIT'], + ) if not changes: raise RuntimeError(f'No changes found for {commit}.') if len(changes) > 1: raise RuntimeError(f'Multiple changes found for {commit}.') - change_id = changes[0]['id'] + change = changes[0] + change_id = change['id'] + message = change['revisions'][commit]['commit']['message'] change_ids_to_commit[change_id] = commit - - message = git_common.run('show', '-s', '--format=%B', commit).strip() change_ids_to_message[change_id] = message print(f'Creating chain of {len(change_ids_to_message)} cherry pick(s)...')