myshellconfig/bin/git-myshellconfig-checkout

146 lines
5.6 KiB
Text
Raw Normal View History

#!/bin/bash
#################################################################################################
2020-10-28 23:13:36 +01:00
# title :git-myshellconfig-checkout #
# 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] #
# 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
2021-09-09 16:06:16 +02:00
[ -z "${MSC_GIT_BRANCH:+x}" ] && export MSC_GIT_BRANCH=master
2020-12-10 09:09:32 +01:00
2020-10-31 08:10:01 +01:00
gitupdateend() {
cat << EOF >> "${MSC_LOGFILE}"
2020-10-31 08:10:01 +01:00
+-----ENDE git update repo $(date) ---------------------------------+
EOF
}
2020-09-22 00:19:30 +02:00
ENTRY
cat << EOF >> "${MSC_LOGFILE}"
2020-10-31 07:29:05 +01:00
+-----BEGINN git update repo $(date) -------------------------------+
EOF
2020-12-23 15:00:36 +01:00
set -- $(getopt -u -o hrs --long headless,force-repo-update,force-submodules-update -- "$@" )
while [ $# -gt 0 ]; do
case $1 in
-h|--headless)
# Headless repo local
PRE="${MSC_GIT_REMOTE_NAME}"
shift
;;
2020-12-23 14:56:15 +01:00
-r|--force-repo-update)
2021-02-02 10:53:45 +01:00
dorepomodupdate="true"
2020-12-23 14:56:15 +01:00
shift
;;
-s|--force-submodules-update)
dosubmodupdate="true"
shift
;;
2021-02-02 11:30:09 +01:00
--)
shift
break
;;
*)
2021-02-02 11:28:34 +01:00
echo "1: $1"
PRE=""
2021-02-02 11:27:39 +01:00
echo bla
shift
;;
esac
done
2021-02-02 11:09:41 +01:00
# If MSC_GIT_TAG is set, checkout is always headless.
2021-02-02 11:20:49 +01:00
[ -z "${MSC_GIT_TAG:+x}" ] || { export MSC_GIT_BRANCH=${MSC_GIT_TAG}; export PRE=""; echo "DEBUG"; }
2020-01-17 15:41:31 +01:00
# Output
#echo -n " remote: ${MSC_GIT_REMOTE_NAME}, branch: ${MSC_GIT_BRANCH}: "
2020-10-31 07:29:05 +01:00
# 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"
2021-02-02 10:53:45 +01:00
[ -z "${MSC_GIT_UPD_SUBMOD_STATFILE+x}" ] && export MSC_GIT_UPD_SUBMOD_STATFILE="${MSC_BASE%/}${MSC_BASE:+/}.last_update_submodules"
2020-12-10 08:57:30 +01:00
if [ $(stat --printf %Y ${MSC_GIT_UPD_REPO_STATFILE} 2>/dev/null || echo 0 ) \
2020-12-23 13:27:31 +01:00
-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
2021-01-12 11:03:26 +01:00
[ -z "${dorepoupdate+x}" ] && dorepoupdate="true"
2020-10-31 07:29:05 +01:00
fi
2021-02-02 10:53:45 +01:00
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
2020-12-23 14:56:15 +01:00
if $dorepoupdate; then
loginfo "fetch repo${dosubmodupdate:+ and submodules}"
2021-09-09 16:01:16 +02:00
#$SGIT fetch ${dosubmodupdate:+--recurse-submodules} ${MSC_GIT_REMOTE_NAME} -p 2>>"${MSC_LOGFILE}"|| { logwarn fetch failed; gitupdateend; exit 1; }
2021-09-09 16:08:04 +02:00
$SGIT fetch ${dosubmodupdate:+--recurse-submodules} ${MSC_GIT_REMOTE_NAME} -p 2>>"${MSC_LOGFILE}"|| { logwarn fetch failed; gitupdateend; exit 1; }
2020-12-23 14:56:15 +01:00
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
2021-09-09 16:06:16 +02:00
$SGIT checkout ${PRE}${PRE:+/}${MSC_GIT_BRANCH}
2021-02-02 11:14:06 +01:00
EOF
2020-12-23 14:56:15 +01:00
$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; }
touch $MSC_GIT_UPD_REPO_STATFILE
else
logwarn -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
2020-12-23 14:56:15 +01:00
gitupdateend
exit 4
2020-12-23 14:56:15 +01:00
fi
2020-12-23 14:56:15 +01:00
loginfo "repo${dosubmodupdate:+ and submodules} synced"
2020-10-28 23:13:36 +01:00
2020-12-23 14:56:15 +01:00
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
logwarn "submodules synced"
fi
fi
2020-09-22 00:19:30 +02:00
EXIT
exit $rc