From 5547b89557f76002471f8941158eea7ebe4d70f8 Mon Sep 17 00:00:00 2001 From: Jerry Zhang Date: Wed, 13 Mar 2024 14:49:47 -0700 Subject: [PATCH] cherry-pick: Only fetch if remote branch doesn't exist Cherry-pick was updated to fetch if the branch doesn't exist, but this could fetch unnecessarily if the remote branch exists locally but namespaced in the remote. Make this more clear with 3 paths: - branch exists locally - branch doesn't exist but remote version of it does (no fetch needed) - neither of the above exists, will attempt to fetch --- revup/cherry_pick.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/revup/cherry_pick.py b/revup/cherry_pick.py index c11c783..344f370 100644 --- a/revup/cherry_pick.py +++ b/revup/cherry_pick.py @@ -1,4 +1,5 @@ import argparse +import asyncio import logging from revup import git @@ -14,11 +15,19 @@ async def find_branch_fetch_if_necessary(git_ctx: git.Git, branch_to_pick: str) Returns the ref for the local or remote branch """ remote_branch_to_pick = git_ctx.ensure_branch_prefix(branch_to_pick) - branch_exists = await git_ctx.is_branch_or_commit(branch_to_pick) - - if not branch_exists: + branch_exists, remote_branch_exists = await asyncio.gather( + git_ctx.is_branch_or_commit(branch_to_pick), + git_ctx.is_branch_or_commit(remote_branch_to_pick), + ) + if branch_exists: + return branch_to_pick + elif remote_branch_exists: + logging.info(f"Couldn't find '{branch_to_pick}', assuming '{remote_branch_to_pick}'") + return remote_branch_to_pick + else: logging.info( - f"Couldn't find '{branch_to_pick}', trying to fetch from remote '{git_ctx.remote_name}'" + f"Couldn't find '{branch_to_pick}', assuming '{remote_branch_to_pick}'" + " and trying to fetch from remote" ) await git_ctx.git( @@ -32,12 +41,9 @@ async def find_branch_fetch_if_necessary(git_ctx: git.Git, branch_to_pick: str) ) if await git_ctx.is_branch_or_commit(remote_branch_to_pick): - logging.info(f"Found '{remote_branch_to_pick}'") - branch_to_pick = remote_branch_to_pick + return remote_branch_to_pick else: - raise RevupUsageException(f"Couldn't find ref '{branch_to_pick}'") - - return branch_to_pick + raise RevupUsageException(f"Couldn't find ref '{branch_to_pick}' in local or remote!") async def main(args: argparse.Namespace, git_ctx: git.Git) -> int: