From 48ab12ea772a1db34558da1cd83ed5cb12e1e4af Mon Sep 17 00:00:00 2001 From: Sergey Sharybin Date: Mon, 27 Feb 2023 10:46:14 +0100 Subject: [PATCH] Fix `make update` when local branch exists Apparently `git checkout -t` is only allowed to happen for new branches. Added a code which checks whether the branch already exists and it so uses the `git checkout `. --- build_files/utils/make_update.py | 8 +++++++- build_files/utils/make_utils.py | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/build_files/utils/make_update.py b/build_files/utils/make_update.py index 076a6cdf782..a1597fe7d9c 100755 --- a/build_files/utils/make_update.py +++ b/build_files/utils/make_update.py @@ -432,7 +432,13 @@ def external_scripts_update(args: argparse.Namespace, # Switch to branch and pull. if submodule_branch: if make_utils.git_branch(args.git_command) != submodule_branch: - if make_utils.git_remote_exist(args.git_command, "origin"): + # If the local branch exists just check out to it. + # If there is no local branch but only remote specify an explicit remote. + # Without this explicit specification Git attempts to set-up tracking + # automatically and fails when the branch is available in multiple remotes. + if make_utils.git_local_branch_exists(args.git_command, submodule_branch): + call([args.git_command, "checkout", submodule_branch]) + elif make_utils.git_remote_exist(args.git_command, "origin"): call([args.git_command, "checkout", "-t", f"origin/{submodule_branch}"]) elif make_utils.git_remote_exist(args.git_command, "upstream"): call([args.git_command, "checkout", "-t", f"upstream/{submodule_branch}"]) diff --git a/build_files/utils/make_utils.py b/build_files/utils/make_utils.py index ebc111081fe..7ef03de2656 100755 --- a/build_files/utils/make_utils.py +++ b/build_files/utils/make_utils.py @@ -54,9 +54,15 @@ def check_output(cmd: Sequence[str], exit_on_error: bool = True) -> str: return output.strip() +def git_local_branch_exists(git_command: str, branch: str) -> bool: + return ( + call([git_command, "rev-parse", "--verify", branch], exit_on_error=False, silent=True) == 0 + ) + + def git_branch_exists(git_command: str, branch: str) -> bool: return ( - call([git_command, "rev-parse", "--verify", branch], exit_on_error=False, silent=True) == 0 or + git_local_branch_exists(git_command, branch) or call([git_command, "rev-parse", "--verify", "remotes/upstream/" + branch], exit_on_error=False, silent=True) == 0 or call([git_command, "rev-parse", "--verify", "remotes/origin/" + branch], exit_on_error=False, silent=True) == 0 ) -- 2.30.2