From ddd4d8792bc8dd3cef510feba926340de5adcfd8 Mon Sep 17 00:00:00 2001 From: Scott Little Date: Sat, 23 Sep 2023 18:51:20 -0400 Subject: [PATCH] fix several issues with branching tools - force review method to 'default' if --bypass-gerrit is selected. - Allow local branch creation even if remote tracvking branch is undetermined. - allow git-utils to call into git-repo-utils to determine the prefered remote rather than guessing without repo's help. - allow git_local_branch to return 'HEAD' if there is no better answer. - git_remote must never return multiple remotes when falling back to git_list_remotes. - git_remote_branch must never return a tag. Signed-off-by: Scott Little Change-Id: Ib1ea9d4ced601e48ac6b5f41c629c9dae82e8108 --- build-tools/branching/create_branches_and_tags.sh | 13 +++++++------ build-tools/branching/push_branches_tags.sh | 14 +++++++++----- build-tools/git-repo-utils.sh | 3 +++ build-tools/git-utils.sh | 13 ++++++++++--- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/build-tools/branching/create_branches_and_tags.sh b/build-tools/branching/create_branches_and_tags.sh index c71eadbd..b67ce35e 100755 --- a/build-tools/branching/create_branches_and_tags.sh +++ b/build-tools/branching/create_branches_and_tags.sh @@ -383,18 +383,19 @@ for subgit in $SUBGITS; do remote_branch=$(git_repo_remote_branch) if [ "${remote_branch}" == "" ]; then remote_branch=$(git_remote_branch) - if [ "${remote_branch}" == "" ]; then - echo_stderr "ERROR: failed to determine remote branch in ${subgit}" - exit 1 - fi + fi + + extra_args="" + if [ "${remote_branch}" != "" ]; then + extra_args="-t ${remote}/${remote_branch}" fi # check if destination branch already exists branch_check=$(git branch -a --list $branch) if [ -z "$branch_check" ]; then echo "Creating branch $branch in $subgit" - echo " git checkout -t ${remote}/${remote_branch} -b $branch" - git checkout -t ${remote}/${remote_branch} -b $branch + echo " git checkout ${extra_args} -b $branch" + git checkout ${extra_args} -b $branch if [ $? != 0 ] ; then echo_stderr "ERROR: failed to create branch '$branch' in ${subgit}" exit 1 diff --git a/build-tools/branching/push_branches_tags.sh b/build-tools/branching/push_branches_tags.sh index 4597cf6d..f55ca8d3 100755 --- a/build-tools/branching/push_branches_tags.sh +++ b/build-tools/branching/push_branches_tags.sh @@ -215,13 +215,17 @@ for subgit in $SUBGITS; do exit 1 fi - review_method=$(git_repo_review_method) - if [ "${review_method}" == "" ]; then - echo_stderr "ERROR: Failed to determine review method in ${subgit}" - exit 1 + if [ $BYPASS_GERRIT -eq 0 ]; then + review_method=$(git_repo_review_method) + if [ "${review_method}" == "" ]; then + echo_stderr "ERROR: Failed to determine review method in ${subgit}" + exit 1 + fi + else + review_method='default' fi - remote=$(git_remote) + remote=$(git_repo_remote) if [ "${remote}" == "" ]; then echo_stderr "ERROR: Failed to determine remote in ${manifest_dir}" exit 1 diff --git a/build-tools/git-repo-utils.sh b/build-tools/git-repo-utils.sh index 395a343e..31b362c4 100644 --- a/build-tools/git-repo-utils.sh +++ b/build-tools/git-repo-utils.sh @@ -24,6 +24,9 @@ source ${GIT_REPO_UTILS_DIR}/repo-utils.sh source ${GIT_REPO_UTILS_DIR}/git-utils.sh source ${GIT_REPO_UTILS_DIR}/url_utils.sh +git_remote_fp="git_repo_remote" + + # # git_repo_rel_dir []: # Return the relative directory of a git within a repo. diff --git a/build-tools/git-utils.sh b/build-tools/git-utils.sh index 4e22035a..f947eaf3 100755 --- a/build-tools/git-utils.sh +++ b/build-tools/git-utils.sh @@ -15,6 +15,10 @@ GIT_UTILS_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}" )" )" source ${GIT_UTILS_DIR}/utils.sh +# pseudo function pointer for git_remote (fallback) or git_repo_remote (preferred) +if [ -z git_remote_fp ]; then + git_remote_fp="git_remote" +fi git_ctx_root_dir () { dirname "${MY_REPO}" @@ -427,6 +431,7 @@ git_local_branch () { # This used to work on older git versions result=$(git name-rev --name-only HEAD) if [ "$result" == "" ] || [ "$result" == "undefined" ]; then + echo "HEAD" return 1 fi @@ -436,6 +441,7 @@ git_local_branch () { while git_is_tag $result; do result=$(git name-rev --name-only $result^1 ) if [ "$result" == "" ] || [ "$result" == "undefined" ]; then + echo "HEAD" return 1 fi done @@ -447,6 +453,7 @@ git_list_remotes () { git remote | grep -v gerrit } + git_remote () { local DIR="${1:-${PWD}}" @@ -455,7 +462,7 @@ git_remote () { local_branch=$(git_local_branch) || return 1 # Return remote of current local branch, else default remote. - git config branch.${local_branch}.remote || git_list_remotes + git config branch.${local_branch}.remote || git_list_remotes | head -n 1 ) } @@ -473,13 +480,13 @@ git_remote_branch () { # Our best bet is if the git config shows the local # branch is tracking a remote branch. local_branch=$(git_local_branch) || return 1 - git config branch.${local_branch}.merge | sed 's#^refs/heads/##' + git config branch.${local_branch}.merge | grep -v '^refs/tags/' | sed 's#^refs/heads/##' if [ ${PIPESTATUS[0]} -eq 0 ]; then return 0 fi # Before we can select a remote branch, we need to know which remote. - remote=$(git_remote) + remote=$($git_remote_fp) if [ $? -ne 0 ] || [ "$remote" == "" ]; then return 1 fi