123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- #!/usr/bin/env bash
- SCRIPT_NAME=`basename $0`
- function usage()
- {
- echo "$SCRIPT_NAME [-h] [-n] [-t <test-branch-prefix> [-u]]"
- echo
- echo " arguments:"
- echo " -h: show this help text"
- echo " -n: dry run mode"
- echo " (default: run commands)"
- echo " -t: test branch mode: create new branches from the commits checked"
- echo " out in each maint directory. Call these branches prefix_029,"
- echo " prefix_035, ... , prefix_master."
- echo " (default: merge forward maint-*, release-*, and master)"
- echo " -u: in test branch mode, if a prefix_* branch already exists,"
- echo " skip creating that branch. Use after a merge error, to"
- echo " restart the merge forward at the first unmerged branch."
- echo " (default: if a prefix_* branch already exists, fail and exit)"
- echo
- echo " env vars:"
- echo " required:"
- echo " TOR_FULL_GIT_PATH: where the git repository directories reside."
- echo " You must set this env var, we recommend \$HOME/git/"
- echo " (default: fail if this env var is not set;"
- echo " current: $GIT_PATH)"
- echo
- echo " optional:"
- echo " TOR_MASTER: the name of the directory containing the tor.git clone"
- echo " The tor master git directory is \$GIT_PATH/\$TOR_MASTER"
- echo " (default: tor; current: $TOR_MASTER_NAME)"
- echo " TOR_WKT_NAME: the name of the directory containing the tor"
- echo " worktrees. The tor worktrees are:"
- echo " \$GIT_PATH/\$TOR_WKT_NAME/{maint-*,release-*}"
- echo " (default: tor-wkt; current: $TOR_WKT_NAME)"
- echo " we recommend that you set these env vars in your ~/.profile"
- }
- GIT_PATH=${TOR_FULL_GIT_PATH:-"FULL_PATH_TO_GIT_REPOSITORY_DIRECTORY"}
- TOR_MASTER_NAME=${TOR_MASTER_NAME:-"tor"}
- TOR_WKT_NAME=${TOR_WKT_NAME:-"tor-wkt"}
- MAINT_029_TB=( "maint-0.2.9" "" "$GIT_PATH/$TOR_WKT_NAME/maint-0.2.9" \
- "_029" "")
- MAINT_035=( "maint-0.3.5" "maint-0.2.9" "$GIT_PATH/$TOR_WKT_NAME/maint-0.3.5" \
- "_035" "_029")
- MAINT_040=( "maint-0.4.0" "maint-0.3.5" "$GIT_PATH/$TOR_WKT_NAME/maint-0.4.0" \
- "_040" "_035")
- MAINT_041=( "maint-0.4.1" "maint-0.4.0" "$GIT_PATH/$TOR_WKT_NAME/maint-0.4.1" \
- "_041" "_040")
- MAINT_MASTER=( "master" "maint-0.4.1" "$GIT_PATH/$TOR_MASTER_NAME" \
- "_master" "_041")
- RELEASE_029=( "release-0.2.9" "maint-0.2.9" "$GIT_PATH/$TOR_WKT_NAME/release-0.2.9" )
- RELEASE_035=( "release-0.3.5" "maint-0.3.5" "$GIT_PATH/$TOR_WKT_NAME/release-0.3.5" )
- RELEASE_040=( "release-0.4.0" "maint-0.4.0" "$GIT_PATH/$TOR_WKT_NAME/release-0.4.0" )
- RELEASE_041=( "release-0.4.1" "maint-0.4.1" "$GIT_PATH/$TOR_WKT_NAME/release-0.4.1" )
- ORIGIN_PATH="$GIT_PATH/$TOR_MASTER_NAME"
- ACTUALLY_THESE_ARE_USED=<<EOF
- ${MAINT_029_TB[0]}
- ${MAINT_035[0]}
- ${MAINT_040[0]}
- ${MAINT_041[0]}
- ${MAINT_MASTER[0]}
- ${RELEASE_029[0]}
- ${RELEASE_035[0]}
- ${RELEASE_040[0]}
- ${RELEASE_041[0]}
- EOF
- DRY_RUN=0
- TEST_BRANCH_PREFIX=
- USE_EXISTING=0
- while getopts "hnt:u" opt; do
- case "$opt" in
- h) usage
- exit 0
- ;;
- n) DRY_RUN=1
- echo " *** DRY RUN MODE ***"
- ;;
- t) TEST_BRANCH_PREFIX="$OPTARG"
- echo " *** CREATING TEST BRANCHES: ${TEST_BRANCH_PREFIX}_nnn ***"
- ;;
- u) USE_EXISTING=1
- echo " *** USE EXISTING TEST BRANCHES MODE ***"
- ;;
- *)
- echo
- usage
- exit 1
- ;;
- esac
- done
- if [ -z "$TEST_BRANCH_PREFIX" ]; then
-
-
-
-
- WORKTREE=(
- RELEASE_029[@]
- MAINT_035[@]
- RELEASE_035[@]
- MAINT_040[@]
- RELEASE_040[@]
- MAINT_041[@]
- RELEASE_041[@]
- MAINT_MASTER[@]
- )
- else
-
- WORKTREE=(
- MAINT_029_TB[@]
- MAINT_035[@]
- MAINT_040[@]
- MAINT_041[@]
- MAINT_MASTER[@]
- )
- fi
- COUNT=${#WORKTREE[@]}
- CNRM=$'\x1b[0;0m'
- BGRN=$'\x1b[1;32m'
- BBLU=$'\x1b[1;34m'
- BRED=$'\x1b[1;31m'
- BYEL=$'\x1b[1;33m'
- IWTH=$'\x1b[3;37m'
- MARKER="${BBLU}[${BGRN}+${BBLU}]${CNRM}"
- SUCCESS="${BGRN}success${CNRM}"
- FAILED="${BRED}failed${CNRM}"
- function validate_ret
- {
- if [ "$1" -eq 0 ]; then
- printf "%s\\n" "$SUCCESS"
- else
- printf "%s\\n" "$FAILED"
- printf " %s" "$2"
- exit 1
- fi
- }
- function switch_branch
- {
- local cmd="git checkout '$1'"
- printf " %s Switching branch to %s..." "$MARKER" "$1"
- if [ $DRY_RUN -eq 0 ]; then
- msg=$( eval "$cmd" 2>&1 )
- validate_ret $? "$msg"
- else
- printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
- fi
- }
- function new_branch
- {
- local cmd="git checkout -b '$1'"
- printf " %s Creating new branch %s..." "$MARKER" "$1"
- if [ $DRY_RUN -eq 0 ]; then
- msg=$( eval "$cmd" 2>&1 )
- validate_ret $? "$msg"
- else
- printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
- fi
- }
- function switch_or_new_branch
- {
- local cmd="git rev-parse --verify '$1'"
- if [ $DRY_RUN -eq 0 ]; then
-
- msg=$( eval "$cmd" 2>&1 )
- RET=$?
- if [ $RET -eq 0 ]; then
-
- switch_branch "$1"
- elif [ $RET -eq 128 ]; then
-
- new_branch "$1"
- else
-
- 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
- }
- function pull_branch
- {
- local cmd="git pull"
- printf " %s Pulling branch %s..." "$MARKER" "$1"
- if [ $DRY_RUN -eq 0 ]; then
- msg=$( eval "$cmd" 2>&1 )
- validate_ret $? "$msg"
- else
- printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
- fi
- }
- function merge_branch
- {
- local cmd="git merge --no-edit '$1'"
- printf " %s Merging branch %s into %s..." "$MARKER" "$1" "$2"
- if [ $DRY_RUN -eq 0 ]; then
- msg=$( eval "$cmd" 2>&1 )
- validate_ret $? "$msg"
- else
- printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
- fi
- }
- function merge_branch_origin
- {
- local cmd="git merge --ff-only 'origin/$1'"
- printf " %s Merging branch origin/%s..." "$MARKER" "$1"
- if [ $DRY_RUN -eq 0 ]; then
- msg=$( eval "$cmd" 2>&1 )
- validate_ret $? "$msg"
- else
- printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
- fi
- }
- function goto_repo
- {
- if [ ! -d "$1" ]; then
- echo " $1: Not found. Stopping."
- exit 1
- fi
- cd "$1" || exit
- }
- function fetch_origin
- {
- local cmd="git fetch origin"
- printf " %s Fetching origin..." "$MARKER"
- if [ $DRY_RUN -eq 0 ]; then
- msg=$( eval "$cmd" 2>&1 )
- validate_ret $? "$msg"
- else
- printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
- fi
- }
- goto_repo "$ORIGIN_PATH"
- fetch_origin
- for ((i=0; i<COUNT; i++)); do
- current=${!WORKTREE[$i]:0:1}
- previous=${!WORKTREE[$i]:1:1}
- repo_path=${!WORKTREE[$i]:2:1}
-
- test_current=
- test_previous=
- target_current="$current"
- target_previous="$previous"
- if [ "$TEST_BRANCH_PREFIX" ]; then
- test_current_suffix=${!WORKTREE[$i]:3:1}
- test_current=${TEST_BRANCH_PREFIX}${test_current_suffix}
-
- target_current="$test_current"
- test_previous_suffix=${!WORKTREE[$i]:4:1}
- if [ "$test_previous_suffix" ]; then
- test_previous=${TEST_BRANCH_PREFIX}${test_previous_suffix}
-
- target_previous="$test_previous"
- fi
- fi
- printf "%s Handling branch \\n" "$MARKER" "${BYEL}$target_current${CNRM}"
-
- goto_repo "$repo_path"
- if [ "$test_current" ]; then
- if [ $USE_EXISTING -eq 0 ]; then
-
-
- new_branch "$test_current"
- else
-
- switch_or_new_branch "$test_current"
- fi
- fi
-
- switch_branch "$current"
-
-
- merge_branch_origin "$current"
- if [ "$test_current" ]; then
-
- switch_branch "$test_current"
-
- merge_branch "$current" "$test_current"
- fi
-
-
-
-
-
-
-
- if [ "$target_previous" ]; then
- merge_branch "$target_previous" "$target_current"
- fi
- done
|