The host name ise set as ssh config entry with ssh key, so we use the short name set for the remote host name
46 lines
1.6 KiB
Bash
Executable File
46 lines
1.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# AUTHOR: Clemens Schwaighofer
|
|
# DATE: 2025/6/27
|
|
# DESC: fetch and merge from a remote repositry
|
|
|
|
REPOSITORY="$1";
|
|
BRANCH="$2";
|
|
REMOTE_NAME="$3";
|
|
if [ "${REPOSITORY}" == "--help" ]; then
|
|
echo "$0 <Repo.git> <branch> [<remote name, defaults to origin>]";
|
|
exit;
|
|
fi;
|
|
if [ -z "${REMOTE_NAME}" ]; then
|
|
REMOTE_NAME="origin"
|
|
fi;
|
|
BASE_FOLDER=$(dirname "$(readlink -f "$0")")"/";
|
|
# shellcheck source=init.sh
|
|
. "${BASE_FOLDER}init.sh";
|
|
|
|
# check that repository path exists
|
|
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);
|
|
|
|
# fetch to for diff check
|
|
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" "fetch" "-q" "${REMOTE_NAME}" "${BRANCH}")
|
|
"${GIT_COMMAND[@]}"
|
|
# check diff for if we need to update
|
|
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" "diff" "--stat" "HEAD" "${REMOTE_NAME}/${BRANCH}")
|
|
changes=$("${GIT_COMMAND[@]}" 2>&1)
|
|
if [ -n "${changes}" ]; then
|
|
echo "[$(date +"%Y-%m-%d %H:%M:%S")] [${unique_id}] [START] git merge ${GIT_REPOSITORY_FOLDER} ${REMOTE_NAME}/${BRANCH}" &>> "$LOG_FILE";
|
|
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" merge "${REMOTE_NAME}/${BRANCH}")
|
|
log_data=$("${GIT_COMMAND[@]}" 2>&1);
|
|
echo "[$(date +"%Y-%m-%d %H:%M:%S")] [${unique_id}] ${log_data}" &>> "$LOG_FILE";
|
|
echo "[$(date +"%Y-%m-%d %H:%M:%S")] [${unique_id}] [END]" &>> "$LOG_FILE";
|
|
fi;
|
|
|
|
# __END__
|