myshellconfig/bin/git-myshellconfig-checkout
2020-12-10 09:09:32 +01:00

137 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 "${MYSHELLCONFIG_LOGDIR+x}" ] && export MYSHELLCONFIG_LOGDIR="${HOME}/logs"
[ -z "${MYSHELLCONFIG_LOGFILE+x}" ] && export MYSHELLCONFIG_LOGFILE="${MYSHELLCONFIG_LOGDIR}/myshellconfig.log"
#MYSHELLCONFIG_LOGDIR="./logs"
#MYSHELLCONFIG_LOGFILE="${MYSHELLCONFIG_LOGDIR}/git.log"
[ -d "${MYSHELLCONFIG_LOGDIR}" ] || mkdir -p "${MYSHELLCONFIG_LOGDIR}"
[ -z "${MYSHELLCONFIG_GIT_REMOTE_NAME+x}" ] && export MYSHELLCONFIG_GIT_REMOTE_NAME=origin
[ -z "${MYSHELLCONFIG_GIT_BRANCH+x}" ] && export MYSHELLCONFIG_GIT_BRANCH=master
lastupdatesubmodules () {
date "+%s" > ${MYSHELLCONFIG_BASE%/}${MYSHELLCONFIG_BASE:+/}.last_update_submodules
}
lastupdaterepo () {
date "+%s" > ${MYSHELLCONFIG_BASE%/}${MYSHELLCONFIG_BASE:+/}.last_update_repo
}
gitupdateend() {
cat << EOF >> "${MYSHELLCONFIG_LOGFILE}"
+-----ENDE git update repo $(date) ---------------------------------+
EOF
}
ENTRY
cat << EOF >> "${MYSHELLCONFIG_LOGFILE}"
+-----BEGINN git update repo $(date) -------------------------------+
EOF
set -- $(getopt -u -o hs --long headless,submodules-update -- "$@" )
while [ $# -gt 0 ]; do
case $1 in
-h|--headless)
# Headless repo local
PRE="${MYSHELLCONFIG_GIT_REMOTE_NAME}"
shift
;;
-s|--submodules-update)
dosubmodupdate="true"
shift
;;
*)
PRE=""
shift
;;
esac
done
# If TAG is set, checkout is always headless.
[ -z "${MYSHELLCONFIG_GIT_TAG:+x}" ] || { export MYSHELLCONFIG_GIT_BRANCH=${MYSHELLCONFIG_GIT_TAG}; export PRE=""; }
# Output
#echo -n " remote: ${MYSHELLCONFIG_GIT_REMOTE_NAME}, branch: ${MYSHELLCONFIG_GIT_BRANCH}: "
# Check for updates in submodules only if last check was INTERVALL hours in the past
if [ $(cat ${MYSHELLCONFIG_BASE%/}${MYSHELLCONFIG_BASE:+/}.last_update_submodules 2>/dev/null || echo 0 ) \
-lt $(date --date="${MYSHELLCONFIG_GIT_SUBMODULES_UPDATE_INTERVALL} hours ago" "+%s") \
-a ${MYSHELLCONFIG_GIT_SUBMODULES_UPDATE-${MYSHELLCONFIG_GIT_SUBMODULES_UPDATE_DEFAULT-true}} ]; then
[ -z "${dosrepoupdate+x}" ] && dorepoupdate="true"
fi
if [ $(cat ${MYSHELLCONFIG_BASE%/}${MYSHELLCONFIG_BASE:+/}.last_update_submodules 2>/dev/null || echo 0 ) \
-lt $(date --date="${MYSHELLCONFIG_GIT_SUBMODULES_UPDATE_INTERVALL} hours ago" "+%s") \
-a ${MYSHELLCONFIG_GIT_SUBMODULES_UPDATE-${MYSHELLCONFIG_GIT_SUBMODULES_UPDATE_DEFAULT-true}} ]; then
[ -z "${dosubmodupdate+x}" ] && dosubmodupdate="true"
fi
#echo $SGIT fetch ${MYSHELLCONFIG_GIT_REMOTE_NAME} -p 1>&2
loginfo "fetch repo${dosubmodupdate:+ and submodules}"
$SGIT fetch ${dosubmodupdate:+--recurse-submodules} ${MYSHELLCONFIG_GIT_REMOTE_NAME} -p 2>>"${MYSHELLCONFIG_LOGFILE}"|| { logwarn fetch failed; gitupdateend; exit 1; }
if ${dosubmodupdate:-false}; then
lastupdatesubmodules
fi
echo "Check for local changes:" >> "${MYSHELLCONFIG_LOGFILE}"
if $SGIT diff-index --ignore-submodules --exit-code HEAD -- >> "${MYSHELLCONFIG_LOGFILE}" ; then
cat << EOF >> "${MYSHELLCONFIG_LOGFILE}"
no changes in local repo
$SGIT checkout repo ${PRE}${PRE:+/}${MYSHELLCONFIG_GIT_BRANCH}
EOF
$SGIT checkout ${PRE}${PRE:+/}${MYSHELLCONFIG_GIT_BRANCH} 1>>"${MYSHELLCONFIG_LOGFILE}" 2>>"${MYSHELLCONFIG_LOGFILE}"|| { gitupdateend; exit 2; }
$SGIT merge FETCH_HEAD 1>>"${MYSHELLCONFIG_LOGFILE}" 2>>"${MYSHELLCONFIG_LOGFILE}"|| { gitupdateend; exit 3; }
else
logwarn -n " Lokale Änderungen festgestellt:"
echo " Siehe Logfile ${MYSHELLCONFIG_LOGFILE}" >&2
cat << EOF >> "${MYSHELLCONFIG_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 ${MYSHELLCONFIG_GIT_SUBMODULES_UPDATE-${MYSHELLCONFIG_GIT_SUBMODULES_UPDATE_DEFAULT-true}}; then
if ${dosubmodupdate:-false}; then
loginfo "update and commit submodules"
logdebug "$($SGIT submodule update --remote)"
logdebug "$($SGIT commit -a -m 'update submodules')"
lastupdatesubmodules
logwarn "submodules synced"
fi
#else
# loginfo -n ", submodules not synced"
# logdebug "Do not update submodules. It's denied by environment MYSHELLCONFIG_GIT_UPDATE_SUBMODULES: ${MYSHELLCONFIG_GIT_SUBMODULES_UPDATE-${MYSHELLCONFIG_GIT_SUBMODULES_UPDATE_DEFAULT-true}}"
#fi
EXIT
exit $rc