3 Commits

Author SHA1 Message Date
c612c6c762 README update 2026-01-22 14:37:30 +09:00
3265d75b7d Add reset of current changes before running merge in git_sync.sh
Will create a backup branch of all the changes and then reset the working copy
2026-01-22 14:33:40 +09:00
50f53d3a35 Fix REMOTE HOST in new clone for host in ssh config check 2026-01-09 14:07:30 +09:00
3 changed files with 38 additions and 2 deletions

View File

@@ -84,6 +84,23 @@ Sample
[2025-07-04 16:06:31] [<uniq id>] [END] [2025-07-04 16:06:31] [<uniq id>] [END]
``` ```
#### What happens to local changes
Any local changes are stored in a backup branch and then reset and cleaned up. No local changes should be commited or synced up, this would break the pull only flow.
If there are changes and they have been reset it will show up in the log like this
```log
...
[2026-01-22 14:31:02] [F4222EC4] [!] Local or cached changes detected, creating backup branch and resetting changes
[2026-01-22 14:31:02] [F4222EC4] Reset log: HEAD is now at 633f6de Uppdate 14
...
```
If a clean up was run too it will show up as "Clean log" after the Reset log.
The reset will only reset local or cached changs (added) but not changed that have been commited. If changes have been commited a manual reset has to be done.
## TODO ## TODO
Future versions will hold an incoming webhook handler and a polling scripts (systemd based) Future versions will hold an incoming webhook handler and a polling scripts (systemd based)

View File

@@ -33,9 +33,28 @@ GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" "fetch" "-
"${GIT_COMMAND[@]}" "${GIT_COMMAND[@]}"
# check diff for if we need to update # check diff for if we need to update
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" "diff" "--stat" "HEAD" "${REMOTE_NAME}/${BRANCH}") GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" "diff" "--stat" "HEAD" "${REMOTE_NAME}/${BRANCH}")
# In general all data that should not be checked in should be in the .gitignore file
changes=$("${GIT_COMMAND[@]}" 2>&1) changes=$("${GIT_COMMAND[@]}" 2>&1)
if [ -n "${changes}" ]; then 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"; echo "[$(date +"%Y-%m-%d %H:%M:%S")] [${unique_id}] [START] git merge ${GIT_REPOSITORY_FOLDER} ${REMOTE_NAME}/${BRANCH}" &>> "$LOG_FILE";
# check if there are local changes, make a backup branch and reset them
local_changes=$("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" "diff" "--quiet");
cached_changed=$("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" "diff" "--quiet" "--cached");
if $local_changes || $cached_changed; then
echo "[$(date +"%Y-%m-%d %H:%M:%S")] [${unique_id}] [!] Local or cached changes detected, creating backup branch and resetting changes" &>> "$LOG_FILE";
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" "branch" "backup-$(date +%Y%m%d-%H%M%S)")
"${GIT_COMMAND[@]}";
# Reset everything
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" "reset" "--hard" "HEAD")
log_data=$("${GIT_COMMAND[@]}" 2>&1);
echo "[$(date +"%Y-%m-%d %H:%M:%S")] [${unique_id}] Reset log: ${log_data}" &>> "$LOG_FILE";
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" "clean" "-fd")
log_data=$("${GIT_COMMAND[@]}" 2>&1);
if [ -n "${log_data}" ]; then
echo "[$(date +"%Y-%m-%d %H:%M:%S")] [${unique_id}] Clean log: ${log_data}" &>> "$LOG_FILE";
fi;
fi;
# MERGE
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" merge "${REMOTE_NAME}/${BRANCH}") GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" merge "${REMOTE_NAME}/${BRANCH}")
log_data=$("${GIT_COMMAND[@]}" 2>&1); 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}] ${log_data}" &>> "$LOG_FILE";

View File

@@ -122,7 +122,7 @@ else
fi; fi;
# set remote host if not set to the ssh config name # set remote host if not set to the ssh config name
if [ -z "${REMOTE_HOST}" ]; then if [ -z "${REMOTE_HOST}" ]; then
REMOTE_HOST="${GIT_REPOSITORY_NAME}"; REMOTE_HOST="$(basename "${REPOSITORY}" .git)";
fi; fi;
if [ $error -eq 1 ]; then if [ $error -eq 1 ]; then
exit; exit;
@@ -131,7 +131,7 @@ fi;
error=0 error=0
echo "* Validate SSH PEM Key exist and SSH config"; echo "* Validate SSH PEM Key exist and SSH config";
if ! grep "Host ${REMOTE_HOST}" "${GIT_WEBHOOK_BASE_FOLDER}"/.ssh/config; then if ! grep "Host ${REMOTE_HOST}" "${GIT_WEBHOOK_BASE_FOLDER}"/.ssh/config; then
echo "[!] ssh config entry for Host ${REMOTE_HOST} is missing"; echo "[!] ssh config entry for Host ${REMOTE_HOST} is missing in ${GIT_WEBHOOK_BASE_FOLDER}/.ssh/config";
error=1; error=1;
else else
# make sure the identiy file is there # make sure the identiy file is there