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-12-10 09:31:38 +01:00
|
|
|
[ -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
|
2020-01-14 10:11:36 +01:00
|
|
|
|
2020-12-14 13:59:03 +01:00
|
|
|
|
2020-12-10 09:09:32 +01:00
|
|
|
|
2020-10-31 08:10:01 +01:00
|
|
|
gitupdateend() {
|
2020-12-10 09:31:38 +01:00
|
|
|
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
|
2020-12-10 09:31:38 +01:00
|
|
|
cat << EOF >> "${MSC_LOGFILE}"
|
2020-10-31 07:29:05 +01:00
|
|
|
+-----BEGINN git update repo $(date) -------------------------------+
|
2020-01-14 10:11:36 +01:00
|
|
|
EOF
|
|
|
|
|
2020-12-23 15:00:36 +01:00
|
|
|
set -- $(getopt -u -o hrs --long headless,force-repo-update,force-submodules-update -- "$@" )
|
2020-11-15 22:10:29 +01:00
|
|
|
|
|
|
|
while [ $# -gt 0 ]; do
|
|
|
|
case $1 in
|
|
|
|
-h|--headless)
|
|
|
|
# Headless repo local
|
2020-12-10 09:31:38 +01:00
|
|
|
PRE="${MSC_GIT_REMOTE_NAME}"
|
2020-11-15 22:10:29 +01:00
|
|
|
shift
|
|
|
|
;;
|
2020-12-23 14:56:15 +01:00
|
|
|
-r|--force-repo-update)
|
|
|
|
dosubmodupdate="true"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-s|--force-submodules-update)
|
2020-11-15 22:10:29 +01:00
|
|
|
dosubmodupdate="true"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
PRE=""
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
2020-01-17 15:23:49 +01:00
|
|
|
# If TAG is set, checkout is always headless.
|
2020-12-10 09:31:38 +01:00
|
|
|
[ -z "${MSC_GIT_TAG:+x}" ] || { export MSC_GIT_BRANCH=${MSC_GIT_TAG}; export PRE=""; }
|
2020-01-17 15:23:49 +01:00
|
|
|
|
2020-01-17 15:41:31 +01:00
|
|
|
# Output
|
2020-12-10 09:31:38 +01:00
|
|
|
#echo -n " remote: ${MSC_GIT_REMOTE_NAME}, branch: ${MSC_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-12-23 12:51:04 +01:00
|
|
|
#if [ $(cat ${MSC_BASE%/}${MSC_BASE:+/}.last_update_submodules 2>/dev/null || echo 0 ) \
|
|
|
|
[ -z "${MSC_GIT_UPD_SUBMOD_STATFILE+x}" ] && export MSC_GIT_UPD_SUBMOD_STATFILE="${MSC_BASE%/}${MSC_BASE:+/}.last_update_submodules"
|
|
|
|
[ -z "${MSC_GIT_UPD_REPO_STATFILE+x}" ] && export MSC_GIT_UPD_REPO_STATFILE="${MSC_BASE%/}${MSC_BASE:+/}.last_update_repo"
|
|
|
|
|
|
|
|
if [ $(stat --printf %Y ${MSC_GIT_UPD_SUBMOD_STATFILE} 2>/dev/null || echo 0 ) \
|
2020-12-23 13:27:31 +01:00
|
|
|
-lt $(date --date="${MSC_GIT_SUBMODULES_UPDATE_INTERVALL-${MSC_GIT_SUBMODULES_UPDATE_INTERVALL_DEFAULT}} hours ago" "+%s") \
|
2020-12-10 09:31:38 +01:00
|
|
|
-a ${MSC_GIT_SUBMODULES_UPDATE-${MSC_GIT_SUBMODULES_UPDATE_DEFAULT-true}} ]; then
|
2020-12-23 14:56:15 +01:00
|
|
|
[ -z "${dorepoupdate+x}" ] && dorepoupdate="true"
|
2020-12-10 08:57:30 +01:00
|
|
|
fi
|
|
|
|
|
2020-12-23 12:51:04 +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") \
|
2020-12-23 12:51:04 +01:00
|
|
|
-a ${MSC_GIT_REPO_UPDATE-${MSC_GIT_REPO_UPDATE_DEFAULT-true}} ]; then
|
2020-11-10 21:31:50 +01:00
|
|
|
[ -z "${dosubmodupdate+x}" ] && dosubmodupdate="true"
|
2020-10-31 07:29:05 +01:00
|
|
|
fi
|
|
|
|
|
2020-12-10 09:31:38 +01:00
|
|
|
#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}"
|
|
|
|
$SGIT fetch ${dosubmodupdate:+--recurse-submodules} ${MSC_GIT_REMOTE_NAME} -p 2>>"${MSC_LOGFILE}"|| { logwarn 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 repo ${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; }
|
|
|
|
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 --)
|
2020-01-14 10:11:36 +01:00
|
|
|
EOF
|
|
|
|
|
2020-12-23 14:56:15 +01:00
|
|
|
gitupdateend
|
|
|
|
exit 4
|
2020-01-14 10:11:36 +01:00
|
|
|
|
2020-12-23 14:56:15 +01:00
|
|
|
fi
|
2020-01-14 10:11:36 +01:00
|
|
|
|
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
|
2020-12-23 12:51:04 +01:00
|
|
|
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
|
|
|
|
|