myshellconfig/bin/git-myshellconfig-checkout
2022-10-21 10:59:37 +02:00

150 lines
5.7 KiB
Bash
Executable file

#!/bin/bash
#################################################################################################
# title :git-myshellconfig-checkout #
# description :checkout git detached HEAD #
# author :Jakobus Schürz #
# changes by :Jakobus Schürz #
# created :17.01.2019 #
# updated :28.10.2020 #
# version :2.0 #
# usage :git myshellconfig-checkout [-h] #
# notes : #
#################################################################################################
[ -z "${SGIT+x}" ] && SGIT=git
[ -z "${MSC_LOGDIR+x}" ] && export MSC_LOGDIR="${HOME}/logs"
[ -z "${MSC_LOGFILE+x}" ] && export MSC_LOGFILE="${MSC_LOGDIR}/myshellconfig.log"
#MSC_LOGDIR="./logs"
#MSC_LOGFILE="${MSC_LOGDIR}/git.log"
[ -d "${MSC_LOGDIR}" ] || mkdir -p "${MSC_LOGDIR}"
[ -z "${MSC_GIT_REMOTE_NAME+x}" ] && export MSC_GIT_REMOTE_NAME=origin
[ -z "${MSC_GIT_BRANCH:+x}" ] && export MSC_GIT_BRANCH=master
gitupdateend() {
cat << EOF >> "${MSC_LOGFILE}"
+-----ENDE git update repo $(date) ---------------------------------+
EOF
}
ENTRY
cat << EOF >> "${MSC_LOGFILE}"
+-----BEGINN git update repo $(date) -------------------------------+
EOF
set -- $(getopt -u -o hrs --long headless,force-repo-update,force-submodules-update -- "$@" )
loginfo "git checkout-options: $@"
while [ $# -gt 0 ]; do
case $1 in
-h|--headless)
# Headless repo local
PRE="${MSC_GIT_REMOTE_NAME}"
shift
;;
-r|--force-repo-update)
dorepomodupdate="true"
shift
;;
-s|--force-submodules-update)
dosubmodupdate="true"
shift
;;
--)
shift
break
;;
*)
echo "1: $1"
PRE=""
echo bla
shift
;;
esac
done
# If MSC_GIT_TAG is set, checkout is always headless.
[ -z "${MSC_GIT_TAG:+x}" ] || { export MSC_GIT_BRANCH=${MSC_GIT_TAG}; export PRE=""; echo "DEBUG"; }
# Output
#echo -n " remote: ${MSC_GIT_REMOTE_NAME}, branch: ${MSC_GIT_BRANCH}: "
# Check for updates in submodules only if last check was INTERVALL hours in the past
#if [ $(cat ${MSC_BASE%/}${MSC_BASE:+/}.last_update_submodules 2>/dev/null || echo 0 ) \
[ -z "${MSC_GIT_UPD_REPO_STATFILE+x}" ] && export MSC_GIT_UPD_REPO_STATFILE="${MSC_BASE%/}${MSC_BASE:+/}.last_update_repo"
[ -z "${MSC_GIT_UPD_SUBMOD_STATFILE+x}" ] && export MSC_GIT_UPD_SUBMOD_STATFILE="${MSC_BASE%/}${MSC_BASE:+/}.last_update_submodules"
if [ $(stat --printf %Y ${MSC_GIT_UPD_REPO_STATFILE} 2>/dev/null || echo 0 ) \
-lt $(date --date="${MSC_GIT_REPO_UPDATE_INTERVALL-${MSC_GIT_REPO_UPDATE_INTERVALL_DEFAULT}} hours ago" "+%s") \
-a ${MSC_GIT_REPO_UPDATE-${MSC_GIT_REPO_UPDATE_DEFAULT-true}} ]; then
[ -z "${dorepoupdate+x}" ] && dorepoupdate="true"
fi
if [ $(stat --printf %Y ${MSC_GIT_UPD_SUBMOD_STATFILE} 2>/dev/null || echo 0 ) \
-lt $(date --date="${MSC_GIT_SUBMODULES_UPDATE_INTERVALL-${MSC_GIT_SUBMODULES_UPDATE_INTERVALL_DEFAULT}} hours ago" "+%s") \
-a ${MSC_GIT_SUBMODULES_UPDATE-${MSC_GIT_SUBMODULES_UPDATE_DEFAULT-true}} ]; then
[ -z "${dosubmodupdate+x}" ] && dosubmodupdate="true"
fi
#echo $SGIT fetch ${MSC_GIT_REMOTE_NAME} -p 1>&2
if $dorepoupdate; then
loginfo "git submodule sync"
logdebug "$(git submodule sync 2>&1)"
loginfo "fetch repo${dosubmodupdate:+ and submodules}"
$SGIT fetch ${dosubmodupdate:+--recurse-submodules} ${MSC_GIT_REMOTE_NAME} -p 2>>"${MSC_LOGFILE}"|| { logwarning fetch failed; gitupdateend; exit 1; }
if ${dosubmodupdate:-false}; then
lastupdatesubmodules
fi
echo "Check for local changes:" >> "${MSC_LOGFILE}"
if $SGIT diff-index --ignore-submodules --exit-code HEAD -- >> "${MSC_LOGFILE}" ; then
cat << EOF >> "${MSC_LOGFILE}"
no changes in local repo
$SGIT checkout ${PRE}${PRE:+/}${MSC_GIT_BRANCH}
EOF
$SGIT checkout ${PRE}${PRE:+/}${MSC_GIT_BRANCH} 1>>"${MSC_LOGFILE}" 2>>"${MSC_LOGFILE}"|| { gitupdateend; exit 2; }
$SGIT merge FETCH_HEAD 1>>"${MSC_LOGFILE}" 2>>"${MSC_LOGFILE}"|| { gitupdateend; exit 3; }
if ${dosubmodupdate:+false}; then
$SGIT submodule update --init --recursive
fi
touch $MSC_GIT_UPD_REPO_STATFILE
else
logwarning -n " Lokale Änderungen festgestellt:"
echo " Siehe Logfile ${MSC_LOGFILE}" >&2
cat << EOF >> "${MSC_LOGFILE}"
um die Änderung zurückzusetzen bitte
$SGIT checkout \$FILENAME
oder um alle lokalen Änderungen auf einmal zurückzusetzen:
$SGIT checkout .
ausführen
Die Änderungen sind:
$($SGIT diff-index HEAD --|awk '{print $5, $6}')
$($SGIT diff-index -p HEAD --)
EOF
gitupdateend
exit 4
fi
loginfo "repo${dosubmodupdate:+ and submodules} synced"
if ${dosubmodupdate:-false}; then
loginfo "update and commit submodules"
logdebug "$($SGIT submodule update --remote)"
logdebug "$($SGIT commit -a -m 'update submodules')"
touch $MSC_GIT_UPD_SUBMOD_STATFILE
logwarning "submodules synced"
fi
fi
EXIT
exit $rc