Add switch branch script
This commit is contained in:
@@ -75,14 +75,21 @@ if [ -d "${GIT_WEBHOOK_BASE_FOLDER}" ]; then
|
||||
echo "[TODO] -> Not implemented: check folder, check ACL";
|
||||
# copy scripts & default config
|
||||
echo "~ Copy basic script and config files";
|
||||
# git_sync.sh, init.sh, new_clone.sh, webhook.default.cfg
|
||||
cp "${BASE_FOLDER}new_clone.sh" "${BASE_FOLDER}init.sh" "${BASE_FOLDER}git_sync.sh" "${GIT_WEBHOOK_BASE_FOLDER}${CLONE_SCRIPTS_FOLDER}";
|
||||
cp "${CONFIG_BASE}/webhook.default.cfg" "${GIT_WEBHOOK_BASE_FOLDER}${CONFIG_FOLDER}";
|
||||
# git_sync.sh, init.sh, new_clone.sh, switch_branch.sh, webhook.default.cfg
|
||||
cp \
|
||||
"${BASE_FOLDER}new_clone.sh" \
|
||||
"${BASE_FOLDER}init.sh" \
|
||||
"${BASE_FOLDER}git_sync.sh" \
|
||||
"${BASE_FOLDER}switch_branch.sh" \
|
||||
"${GIT_WEBHOOK_BASE_FOLDER}${CLONE_SCRIPTS_FOLDER}";
|
||||
cp "${CONFIG_BASE}/webhook.default.cfg" \
|
||||
"${GIT_WEBHOOK_BASE_FOLDER}${CONFIG_FOLDER}";
|
||||
# and make sure they are all owned by the correct user
|
||||
chown "${SUDO_USER}" \
|
||||
"${BASE_FOLDER}new_clone.sh" \
|
||||
"${BASE_FOLDER}init.sh" \
|
||||
"${BASE_FOLDER}git_sync.sh" \
|
||||
"${BASE_FOLDER}switch_branch.sh" \
|
||||
"${CONFIG_BASE}/webhook.default.cfg";
|
||||
# check config entries missing
|
||||
exit;
|
||||
@@ -159,13 +166,22 @@ EOF
|
||||
# Copy files
|
||||
echo "+ Copy basic script and config files";
|
||||
# git_sync.sh, init.sh, new_clone.sh, webhook.default.cfg
|
||||
cp "${BASE_FOLDER}new_clone.sh" "${BASE_FOLDER}init.sh" "${BASE_FOLDER}git_sync.sh" "${GIT_WEBHOOK_BASE_FOLDER}${CLONE_SCRIPTS_FOLDER}";
|
||||
cp "${CONFIG_BASE}/webhook.cfg" "${CONFIG_BASE}/webhook.default.cfg" "${GIT_WEBHOOK_BASE_FOLDER}${CONFIG_FOLDER}";
|
||||
cp \
|
||||
"${BASE_FOLDER}new_clone.sh" \
|
||||
"${BASE_FOLDER}init.sh" \
|
||||
"${BASE_FOLDER}git_sync.sh" \
|
||||
"${BASE_FOLDER}switch_branch.sh" \
|
||||
"${GIT_WEBHOOK_BASE_FOLDER}${CLONE_SCRIPTS_FOLDER}";
|
||||
cp \
|
||||
"${CONFIG_BASE}/webhook.cfg" \
|
||||
"${CONFIG_BASE}/webhook.default.cfg" \
|
||||
"${GIT_WEBHOOK_BASE_FOLDER}${CONFIG_FOLDER}";
|
||||
# and make sure they are all owned by the correct user
|
||||
chown "${SUDO_USER}" \
|
||||
"${BASE_FOLDER}new_clone.sh" \
|
||||
"${BASE_FOLDER}init.sh" \
|
||||
"${BASE_FOLDER}git_sync.sh" \
|
||||
"${BASE_FOLDER}switch_branch.sh" \
|
||||
"${CONFIG_BASE}/webhook.cfg" \
|
||||
"${CONFIG_BASE}/webhook.default.cfg";
|
||||
fi;
|
||||
|
||||
43
src/bin/switch_branch.sh
Normal file
43
src/bin/switch_branch.sh
Normal file
@@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# AUTHOR: Clemens Schwaighofer
|
||||
# DATE: 2025/7/15
|
||||
# DESC: Switch a branch, run this script if we have to switch to a different branch
|
||||
# If not the new branch will merge into the branch that was originally selected
|
||||
|
||||
REPOSITORY="$1";
|
||||
BRANCH="$2";
|
||||
REMOTE_NAME="$3";
|
||||
if [ -z "${REMOTE_NAME}" ]; then
|
||||
REMOTE_NAME="origin"
|
||||
fi;
|
||||
BASE_FOLDER=$(dirname "$(readlink -f "$0")")"/";
|
||||
# shellcheck source=init.sh
|
||||
. "${BASE_FOLDER}init.sh";
|
||||
|
||||
GIT_REPOSITORY_FOLDER="${GIT_WEBHOOK_BASE_FOLDER}${CLONE_BASE}${REPOSITORY}";
|
||||
if [ ! -d "${GIT_REPOSITORY_FOLDER}" ]; then
|
||||
echo "[!] ${REPOSITORY} not found in clone folder";
|
||||
echo "[!] Full path: ${GIT_REPOSITORY_FOLDER}";
|
||||
exit;
|
||||
fi;
|
||||
LOG_FILE="${GIT_WEBHOOK_BASE_FOLDER}${LOG_FOLDER}${REPOSITORY}.log";
|
||||
unique_id=$(uuidgen | tr -d '-' | head -c 8);
|
||||
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] [${unique_id}] [START] On repository ${GIT_REPOSITORY_FOLDER} switch to branch ${REMOTE_NAME}/${BRANCH}" | tee -a "$LOG_FILE";
|
||||
# add new branch to remote
|
||||
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" "remote" "set-branches" "--add" "${REMOTE_NAME}" "${BRANCH}")
|
||||
"${GIT_COMMAND[@]}" 2>&1 | tee -a "$LOG_FILE";
|
||||
# fetch new branch
|
||||
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" "fetch" "--depth" "1" "${REMOTE_NAME}" "${BRANCH}")
|
||||
"${GIT_COMMAND[@]}" 2>&1 | tee -a "$LOG_FILE";
|
||||
# checkout new branch
|
||||
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" "checkout" "${BRANCH}");
|
||||
"${GIT_COMMAND[@]}" 2>&1 | tee -a "$LOG_FILE";
|
||||
# get the latest changes from branch
|
||||
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" "pull" "${REMOTE_NAME}" "${BRANCH}")
|
||||
"${GIT_COMMAND[@]}" 2>&1 | tee -a "$LOG_FILE";
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] [${unique_id}] [END] branch switch done" | tee -a "$LOG_FILE";
|
||||
|
||||
|
||||
# __END__
|
||||
Reference in New Issue
Block a user