commit 15782758c7c9e170fccc138a0c4897a602217641 Author: teor teor@torproject.org Date: Fri Aug 9 14:37:38 2019 +1000
scripts/git: Allow git-merge-forward.sh to re-use existing test branches
Add a -u argument to git-merge-forward.sh, so that the script can re-use existing test branches after a merge failure and fix.
Part of 31314. --- changes/ticket31314 | 3 +++ scripts/git/git-merge-forward.sh | 46 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-)
diff --git a/changes/ticket31314 b/changes/ticket31314 index e1d9fb767..293223117 100644 --- a/changes/ticket31314 +++ b/changes/ticket31314 @@ -4,3 +4,6 @@ push test branches. Closes ticket 31314. - Add a -r <remote-name> argument to git-push-all.sh, so the script can push test branches to a personal remote. Closes ticket 31314. + - Add a -u argument to git-merge-forward.sh, so that the script can re-use + existing test branches after a merge failure and fix. + Closes ticket 31314. diff --git a/scripts/git/git-merge-forward.sh b/scripts/git/git-merge-forward.sh index 1f4ddb85e..a7e797eaf 100755 --- a/scripts/git/git-merge-forward.sh +++ b/scripts/git/git-merge-forward.sh @@ -109,7 +109,12 @@ DRY_RUN=0 # <tbbn>_029, <tbbn>_035, ... , <tbbn>_master, and merge forward. TEST_BRANCH_PREFIX=
-while getopts "nt:" opt; do +# Controlled by the -u option. The use existing option checks for existing +# branches with the <test-branch-prefix>, and checks them out, rather than +# creating a new branch. +USE_EXISTING=0 + +while getopts "nt:u" opt; do case "$opt" in n) DRY_RUN=1 echo " *** DRY RUN MODE ***" @@ -117,6 +122,9 @@ while getopts "nt:" opt; do t) TEST_BRANCH_PREFIX="$OPTARG" echo " *** CREATING TEST BRANCHES: ${TEST_BRANCH_PREFIX}_nnn ***" ;; + u) USE_EXISTING=1 + echo " *** USE EXISTING TEST BRANCHES MODE ***" + ;; *) exit 1 ;; @@ -230,6 +238,32 @@ function new_branch fi }
+# Switch to an existing branch, or checkout a new branch with the given +# branch name. +function switch_or_new_branch +{ + local cmd="git rev-parse --verify $1" + if [ $DRY_RUN -eq 0 ]; then + # Call switch_branch if there is a branch, or new_branch if there is not + msg=$( eval "$cmd" 2>&1 ) + RET=$? + if [ $RET -eq 0 ]; then + # Branch: (commit id) + switch_branch "$1" + elif [ $RET -eq 128 ]; then + # Not a branch: "fatal: Needed a single revision" + new_branch "$1" + else + # Unexpected return value + validate_ret $RET "$msg" + fi + else + printf "\n %s\n" "${IWTH}$cmd${CNRM}, then depending on the result:" + switch_branch "$1" + new_branch "$1" + fi +} + # Pull the given branch name. function pull_branch { @@ -328,8 +362,14 @@ for ((i=0; i<COUNT; i++)); do # Go into the worktree to start merging. goto_repo "$repo_path" if [ "$test_current" ]; then - # Create a test branch from the currently checked-out branch/commit - new_branch "$test_current" + if [ $USE_EXISTING -eq 0 ]; then + # Create a test branch from the currently checked-out branch/commit + # Fail if it already exists + new_branch "$test_current" + else + # Switch if it exists, or create if it does not + switch_or_new_branch "$test_current" + fi fi # Checkout the current maint/release branch switch_branch "$current"