2020-01-14 10:11:36 +01:00
#!/bin/bash
#################################################################################################
2020-10-28 23:13:36 +01:00
# title :git-myshellconfig-checkout #
2020-01-14 10:11:36 +01:00
# description :checkout git detached HEAD #
# author :Jakobus Schürz #
# changes by :Jakobus Schürz #
# created :17.01.2019 #
2020-10-28 23:13:36 +01:00
# updated :28.10.2020 #
# version :2.0 #
# usage :git myshellconfig-checkout [-h] #
2020-01-14 10:11:36 +01:00
# notes : #
#################################################################################################
[ -z "${SGIT+x}" ] && SGIT=git
2020-09-22 00:18:50 +02:00
[ -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"
2020-01-14 10:11:36 +01:00
[ -d "${MYSHELLCONFIG_LOGDIR}" ] || mkdir -p "${MYSHELLCONFIG_LOGDIR}"
2020-01-17 16:03:54 +01:00
[ -z "${MYSHELLCONFIG_GIT_REMOTE_NAME+x}" ] && export MYSHELLCONFIG_GIT_REMOTE_NAME=origin
[ -z "${MYSHELLCONFIG_GIT_BRANCH+x}" ] && export MYSHELLCONFIG_GIT_BRANCH=master
2020-01-14 10:11:36 +01:00
2020-10-31 08:10:01 +01:00
gitupdateend() {
cat << EOF >> "${MYSHELLCONFIG_LOGFILE}"
+-----ENDE git update repo $(date) ---------------------------------+
EOF
}
2020-09-22 00:19:30 +02:00
ENTRY
2020-01-14 10:11:36 +01:00
cat << EOF >> "${MYSHELLCONFIG_LOGFILE}"
2020-10-31 07:29:05 +01:00
+-----BEGINN git update repo $(date) -------------------------------+
2020-01-14 10:11:36 +01:00
EOF
case $1 in
-h)
# Headless repo local
2020-01-17 15:23:49 +01:00
PRE="${MYSHELLCONFIG_GIT_REMOTE_NAME}"
2020-01-14 10:11:36 +01:00
;;
2020-10-31 07:29:05 +01:00
-s)
2020-11-05 21:57:41 +01:00
dosubmodupdate="true"
2020-10-31 07:29:05 +01:00
;;
2020-01-14 10:11:36 +01:00
*)
PRE=""
;;
esac
2020-01-17 15:23:49 +01:00
# If TAG is set, checkout is always headless.
2020-01-17 16:08:02 +01:00
[ -z "${MYSHELLCONFIG_GIT_TAG:+x}" ] || { export MYSHELLCONFIG_GIT_BRANCH=${MYSHELLCONFIG_GIT_TAG}; export PRE=""; }
2020-01-17 15:23:49 +01:00
2020-01-17 15:41:31 +01:00
# Output
2020-01-17 16:12:06 +01:00
#echo -n " remote: ${MYSHELLCONFIG_GIT_REMOTE_NAME}, branch: ${MYSHELLCONFIG_GIT_BRANCH}: "
2020-01-17 15:23:49 +01:00
2020-10-31 07:29:05 +01:00
# Check for updates in submodules only if last check was INTERVALL hours in the past
2020-11-03 10:13:40 +01:00
updated=$(cat ${MYSHELLCONFIG_BASE%/}${MYSHELLCONFIG_BASE:+/}.last_update_submodules 2>/dev/null || echo 0 )
2020-11-10 21:35:28 +01:00
logtrace "dosubmodupdate before: $dosubmodupdate"
2020-11-10 21:31:50 +01:00
if [ $updated -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"
2020-10-31 07:29:05 +01:00
fi
2020-11-10 21:35:28 +01:00
logtrace "dosubmodupdate after: $dosubmodupdate"
2020-10-31 07:29:05 +01:00
2020-08-17 17:54:15 +02:00
#echo $SGIT fetch ${MYSHELLCONFIG_GIT_REMOTE_NAME} -p 1>&2
2020-11-10 21:22:08 +01:00
loginfo "fetch repo${dosubmodupdate:+ and submodules}"
2020-11-05 21:57:41 +01:00
$SGIT fetch ${dosubmodupdate:+--recurse-submodules} ${MYSHELLCONFIG_GIT_REMOTE_NAME} -p 2>>"${MYSHELLCONFIG_LOGFILE}"|| { logwarn fetch failed; gitupdateend; exit 1; }
if ${dosubmodupdate:-false}; then
date "+%s" > ${MYSHELLCONFIG_BASE%/}${MYSHELLCONFIG_BASE:+/}.last_update_submodules
fi
2020-01-14 10:11:36 +01:00
2020-10-31 07:32:47 +01:00
echo "Check for local changes:" >> "${MYSHELLCONFIG_LOGFILE}"
2020-01-14 10:11:36 +01:00
if $SGIT diff-index --ignore-submodules --exit-code HEAD -- >> "${MYSHELLCONFIG_LOGFILE}" ; then
cat << EOF >> "${MYSHELLCONFIG_LOGFILE}"
no changes in local repo
2020-01-17 15:23:49 +01:00
$SGIT checkout repo ${PRE}${PRE:+/}${MYSHELLCONFIG_GIT_BRANCH}
2020-01-14 10:11:36 +01:00
EOF
2020-10-31 08:10:01 +01:00
$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; }
2020-01-14 10:11:36 +01:00
else
2020-10-31 07:29:05 +01:00
logwarn -n " Lokale Änderungen festgestellt:"
echo " Siehe Logfile ${MYSHELLCONFIG_LOGFILE}" >&2
2020-01-14 10:11:36 +01:00
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:
2020-10-31 07:29:05 +01:00
$($SGIT diff-index HEAD --|awk '{print $5, $6}')
$($SGIT diff-index -p HEAD --)
2020-01-14 10:11:36 +01:00
EOF
2020-10-31 08:10:01 +01:00
gitupdateend
2020-01-14 10:11:36 +01:00
exit 4
fi
2020-11-10 21:22:08 +01:00
loginfo "repo${dosubmodupdate:+ and submodules} synced"
2020-10-28 23:13:36 +01:00
2020-11-10 21:22:08 +01:00
#if ${MYSHELLCONFIG_GIT_SUBMODULES_UPDATE-${MYSHELLCONFIG_GIT_SUBMODULES_UPDATE_DEFAULT-true}}; then
2020-11-05 21:57:41 +01:00
if ${dosubmodupdate:-false}; then
2020-11-10 21:14:10 +01:00
loginfo "update and commit submodules"
2020-10-31 07:31:06 +01:00
logdebug "$($SGIT submodule update --remote)"
2020-10-31 07:41:20 +01:00
logdebug "$($SGIT commit -a -m 'update submodules')"
2020-11-04 23:50:18 +01:00
date "+%s" > ${MYSHELLCONFIG_BASE%/}${MYSHELLCONFIG_BASE:+/}.last_update_submodules
2020-11-10 21:14:10 +01:00
logwarn "submodules synced"
2020-10-31 08:13:02 +01:00
fi
2020-11-10 21:22:08 +01:00
#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
2020-01-14 10:11:36 +01:00
2020-09-22 00:19:30 +02:00
EXIT
2020-01-14 10:11:36 +01:00
exit $rc