From 704bf01fabf37c67d4394c20bf9ececa82551dda Mon Sep 17 00:00:00 2001 From: Bridget McErlean Date: Tue, 13 Oct 2020 16:02:21 -0400 Subject: [PATCH] Check existing remote branches in release script (#2951) The command to check for an existing release branch only checked for local branches. We should be considering both local and remote branches before cherry-picking commits for the new release. This change checks for existing local and remote release branches and creates or updates them accordingly. * If a remote branch exists, but a local branch does not, checkout the remote branch and track it. * If the remote branch and local branch exists, checkout the local branch and ensure that the latest commits from the remote are pulled. * Otherwise, if the remote branch does not exist, create it locally if needed. This also handles the case where an existing release branch may be tracked in multiple remotes as the remote to use is explicitly stated. Signed-off-by: Bridget McErlean --- hack/release-tools/tag-release.sh | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/hack/release-tools/tag-release.sh b/hack/release-tools/tag-release.sh index 05780ae2c..5301dd1de 100755 --- a/hack/release-tools/tag-release.sh +++ b/hack/release-tools/tag-release.sh @@ -121,15 +121,29 @@ git fetch "$remote" --tags # If we've got a patch release, we'll need to create a release branch for it. if [[ "$VELERO_PATCH" > 0 ]]; then release_branch_name=release-$VELERO_MAJOR.$VELERO_MINOR - - # Check if the branch exists, creating it if not. - # The fetch command above should have gotten all the upstream branches, so we can safely assume this check is local & upstream branches. - if [[ -z $(git branch | grep $release_branch_name) ]]; then - git checkout -b $release_branch_name - echo "Release branch made." + remote_release_branch_name="$remote/$release_branch_name" + + # Determine whether the local and remote release branches already exist + local_branch=$(git branch | grep "$release_branch_name") + remote_branch=$(git branch -r | grep "$remote_release_branch_name") + + if [[ -n $remote_branch ]]; then + if [[ -z $local_branch ]]; then + # Remote branch exists, but does not exist locally. Checkout and track the remote branch. + git checkout --track "$remote_release_branch_name" + else + # Checkout the local release branch and ensure it is up to date with the remote + git checkout "$release_branch_name" + git pull --set-upstream "$remote" "$release_branch_name" + fi else - echo "Release branch $release_branch_name exists already." + if [[ -z $local_branch ]]; then + # Neither the remote or local release branch exists, create it + git checkout -b $release_branch_name + else + # The local branch exists so check it out. git checkout $release_branch_name + fi fi echo "Now you'll need to cherry-pick any relevant git commits into this release branch." @@ -146,7 +160,7 @@ if [[ "$VELERO_PATCH" > 0 ]]; then echo "Pushing $release_branch_name to \"$remote\" remote" git push --set-upstream "$remote" $release_branch_name fi - + tag_and_push else echo "Checking out $remote/main."