2019-06-11 06:29:10 +02:00
|
|
|
#!/usr/bin/env bash
|
2019-02-14 20:27:40 +01:00
|
|
|
|
2019-11-06 06:52:18 +01:00
|
|
|
SCRIPT_NAME=$(basename "$0")
|
2019-02-14 20:27:40 +01:00
|
|
|
|
2020-02-11 18:17:03 +01:00
|
|
|
usage()
|
2019-09-10 04:14:49 +02:00
|
|
|
{
|
|
|
|
echo "$SCRIPT_NAME [-h] [-n]"
|
|
|
|
echo
|
|
|
|
echo " arguments:"
|
|
|
|
echo " -h: show this help text"
|
|
|
|
echo " -n: dry run mode"
|
|
|
|
echo " (default: run commands)"
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
|
|
|
#################
|
|
|
|
# Configuration #
|
|
|
|
#################
|
|
|
|
|
|
|
|
# Don't change this configuration - set the env vars in your .profile
|
2019-02-14 20:27:40 +01:00
|
|
|
|
|
|
|
# Where are all those git repositories?
|
2019-06-10 15:05:26 +02:00
|
|
|
GIT_PATH=${TOR_FULL_GIT_PATH:-"FULL_PATH_TO_GIT_REPOSITORY_DIRECTORY"}
|
2019-02-14 20:27:40 +01:00
|
|
|
# The tor master git repository directory from which all the worktree have
|
|
|
|
# been created.
|
2019-06-11 06:34:44 +02:00
|
|
|
TOR_MASTER_NAME=${TOR_MASTER_NAME:-"tor"}
|
2019-02-14 20:27:40 +01:00
|
|
|
# The worktrees location (directory).
|
2019-06-11 06:34:44 +02:00
|
|
|
TOR_WKT_NAME=${TOR_WKT_NAME:-"tor-wkt"}
|
2019-02-14 20:27:40 +01:00
|
|
|
|
2019-09-10 04:14:00 +02:00
|
|
|
##########################
|
|
|
|
# Git branches to manage #
|
|
|
|
##########################
|
2019-02-14 20:27:40 +01:00
|
|
|
|
2020-02-11 18:17:03 +01:00
|
|
|
set -e
|
|
|
|
eval "$(git-list-tor-branches.sh -b)"
|
|
|
|
set +e
|
2019-02-14 20:27:40 +01:00
|
|
|
|
2019-02-28 19:23:23 +01:00
|
|
|
# The master branch path has to be the main repository thus contains the
|
|
|
|
# origin that will be used to fetch the updates. All the worktrees are created
|
|
|
|
# from that repository.
|
|
|
|
ORIGIN_PATH="$GIT_PATH/$TOR_MASTER_NAME"
|
|
|
|
|
2019-02-14 20:27:40 +01:00
|
|
|
COUNT=${#WORKTREE[@]}
|
|
|
|
|
2019-09-10 04:14:49 +02:00
|
|
|
#######################
|
|
|
|
# Argument processing #
|
|
|
|
#######################
|
|
|
|
|
2019-02-14 20:27:40 +01:00
|
|
|
# Controlled by the -n option. The dry run option will just output the command
|
|
|
|
# that would have been executed for each worktree.
|
|
|
|
DRY_RUN=0
|
|
|
|
|
2019-09-10 04:14:49 +02:00
|
|
|
while getopts "hn" opt; do
|
|
|
|
case "$opt" in
|
|
|
|
h) usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
n) DRY_RUN=1
|
|
|
|
echo " *** DRY DRUN MODE ***"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2019-09-10 04:14:00 +02:00
|
|
|
#############
|
|
|
|
# Constants #
|
|
|
|
#############
|
|
|
|
|
2019-02-14 20:27:40 +01:00
|
|
|
# Control characters
|
|
|
|
CNRM=$'\x1b[0;0m' # Clear color
|
|
|
|
|
|
|
|
# Bright color
|
|
|
|
BGRN=$'\x1b[1;32m'
|
|
|
|
BBLU=$'\x1b[1;34m'
|
|
|
|
BRED=$'\x1b[1;31m'
|
|
|
|
BYEL=$'\x1b[1;33m'
|
|
|
|
IWTH=$'\x1b[3;37m'
|
|
|
|
|
|
|
|
# Strings for the pretty print.
|
|
|
|
MARKER="${BBLU}[${BGRN}+${BBLU}]${CNRM}"
|
|
|
|
SUCCESS="${BGRN}ok${CNRM}"
|
|
|
|
FAILED="${BRED}failed${CNRM}"
|
|
|
|
|
|
|
|
####################
|
|
|
|
# Helper functions #
|
|
|
|
####################
|
|
|
|
|
|
|
|
# Validate the given returned value (error code), print success or failed. The
|
|
|
|
# second argument is the error output in case of failure, it is printed out.
|
|
|
|
# On failure, this function exits.
|
|
|
|
function validate_ret
|
|
|
|
{
|
|
|
|
if [ "$1" -eq 0 ]; then
|
|
|
|
printf "%s\\n" "$SUCCESS"
|
|
|
|
else
|
|
|
|
printf "%s\\n" "$FAILED"
|
|
|
|
printf " %s" "$2"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Switch to the given branch name.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
# Pull the given branch name.
|
2019-02-28 19:23:23 +01:00
|
|
|
function merge_branch
|
2019-02-14 20:27:40 +01:00
|
|
|
{
|
2019-02-28 19:23:23 +01:00
|
|
|
local cmd="git merge --ff-only origin/$1"
|
|
|
|
printf " %s Merging branch origin/%s..." "$MARKER" "$1"
|
2019-02-14 20:27:40 +01:00
|
|
|
if [ $DRY_RUN -eq 0 ]; then
|
|
|
|
msg=$( eval "$cmd" 2>&1 )
|
|
|
|
validate_ret $? "$msg"
|
|
|
|
else
|
|
|
|
printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Go into the worktree repository.
|
|
|
|
function goto_repo
|
|
|
|
{
|
|
|
|
if [ ! -d "$1" ]; then
|
|
|
|
echo " $1: Not found. Stopping."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
cd "$1" || exit
|
|
|
|
}
|
|
|
|
|
2019-02-28 19:23:23 +01:00
|
|
|
# Fetch the origin. No arguments.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-04-10 11:26:47 +02:00
|
|
|
# Fetch tor-github pull requests. No arguments.
|
|
|
|
function fetch_tor_github
|
|
|
|
{
|
|
|
|
local cmd="git fetch tor-github"
|
|
|
|
printf " %s Fetching tor-github..." "$MARKER"
|
|
|
|
if [ $DRY_RUN -eq 0 ]; then
|
|
|
|
msg=$( eval "$cmd" 2>&1 )
|
|
|
|
validate_ret $? "$msg"
|
|
|
|
else
|
|
|
|
printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-07-09 19:54:28 +02:00
|
|
|
# Fetch tor-gitlab pull requests. No arguments.
|
|
|
|
function fetch_tor_gitlab
|
|
|
|
{
|
|
|
|
local cmd="git fetch tor-gitlab"
|
|
|
|
printf " %s Fetching tor-gitlab..." "$MARKER"
|
|
|
|
if [ $DRY_RUN -eq 0 ]; then
|
|
|
|
msg=$( eval "$cmd" 2>&1 )
|
|
|
|
validate_ret $? "$msg"
|
|
|
|
else
|
|
|
|
printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2019-02-14 20:27:40 +01:00
|
|
|
###############
|
|
|
|
# Entry point #
|
|
|
|
###############
|
|
|
|
|
2019-04-10 11:26:47 +02:00
|
|
|
# First, fetch tor-github.
|
2019-02-28 19:23:23 +01:00
|
|
|
goto_repo "$ORIGIN_PATH"
|
2019-04-10 11:26:47 +02:00
|
|
|
fetch_tor_github
|
|
|
|
|
2020-07-09 19:54:28 +02:00
|
|
|
# Then tor-gitlab
|
|
|
|
fetch_tor_gitlab
|
|
|
|
|
2019-04-10 11:26:47 +02:00
|
|
|
# Then, fetch the origin.
|
2019-02-28 19:23:23 +01:00
|
|
|
fetch_origin
|
|
|
|
|
2019-02-14 20:27:40 +01:00
|
|
|
# Go over all configured worktree.
|
|
|
|
for ((i=0; i<COUNT; i++)); do
|
|
|
|
current=${!WORKTREE[$i]:0:1}
|
|
|
|
repo_path=${!WORKTREE[$i]:1:1}
|
|
|
|
|
|
|
|
printf "%s Handling branch %s\\n" "$MARKER" "${BYEL}$current${CNRM}"
|
|
|
|
|
|
|
|
# Go into the worktree to start merging.
|
|
|
|
goto_repo "$repo_path"
|
|
|
|
# Checkout the current branch
|
|
|
|
switch_branch "$current"
|
2019-02-28 19:23:23 +01:00
|
|
|
# Update the current branch by merging the origin to get the latest.
|
|
|
|
merge_branch "$current"
|
2019-02-14 20:27:40 +01:00
|
|
|
done
|