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
This commit is contained in:
@@ -33,9 +33,28 @@ GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${GIT_REPOSITORY_FOLDER}" "fetch" "-
|
||||
"${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}")
|
||||
# In general all data that should not be checked in should be in the .gitignore file
|
||||
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";
|
||||
# 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}")
|
||||
log_data=$("${GIT_COMMAND[@]}" 2>&1);
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] [${unique_id}] ${log_data}" &>> "$LOG_FILE";
|
||||
|
||||
Reference in New Issue
Block a user