myshellconfig/bin/git-myshellconfig-checkout

150 lines
6 KiB
Text
Raw Permalink 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 : #
#################################################################################################
2022-10-31 03:08:04 +01:00
[ -z "${MSC_GIT+x}" ] && MSC_GIT=git
[ -z "${MSC_LOGDIR+x}" ] && export MSC_LOGDIR="${HOME}/logs"
[ -z "${MSC_LOGFILE+x}" ] && export MSC_LOGFILE="${MSC_LOGDIR}/myshellconfig.log"
[ -d "${MSC_LOGDIR}" ] || mkdir -p "${MSC_LOGDIR}"
[ -z "${MSC_GIT_REMOTE_NAME+x}" ] && export MSC_GIT_REMOTE_NAME=$($MSC_GIT rev-parse --abbrev-ref @{u} 2>/dev/null | cut -d'/' -f1)
[ -z "${MSC_GIT_BRANCH:+x}" ] && export MSC_GIT_BRANCH=$($MSC_GIT rev-parse --abbrev-ref @{u} 2>/dev/null | cut -d'/' -f2)
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 -- "$@" )
2021-09-25 21:41:01 +02:00
loginfo "git checkout-options: $@"
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
2022-10-21 10:58:20 +02:00
#echo $MSC_GIT fetch ${MSC_GIT_REMOTE_NAME} -p 1>&2
2020-12-23 14:56:15 +01:00
if $dorepoupdate; then
2022-10-25 06:35:24 +02:00
loginfo "$MSC_GIT submodule sync"
logdebug "$($MSC_GIT submodule sync 2>&1)"
2020-12-23 14:56:15 +01:00
loginfo "fetch repo${dosubmodupdate:+ and submodules}"
2022-10-31 03:08:04 +01:00
# $MSC_GIT fetch ${dosubmodupdate:+--recurse-submodules} ${MSC_GIT_REMOTE_NAME} -p 2>>"${MSC_LOGFILE}"|| { logwarning fetch failed; gitupdateend; exit 1; }
$MSC_GIT fetch -p || { logwarning 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}"
2022-10-21 10:58:20 +02:00
if $MSC_GIT diff-index --ignore-submodules --exit-code HEAD -- >> "${MSC_LOGFILE}" ; then
2020-12-23 14:56:15 +01:00
cat << EOF >> "${MSC_LOGFILE}"
no changes in local repo
2022-10-21 10:58:20 +02:00
$MSC_GIT checkout ${PRE}${PRE:+/}${MSC_GIT_BRANCH}
2021-02-02 11:14:06 +01:00
EOF
2022-10-25 07:08:19 +02:00
$MSC_GIT checkout ${PRE}${PRE:+/}${MSC_GIT_BRANCH} 1>>"${MSC_LOGFILE}" 2>>"${MSC_LOGFILE}"|| { gitupdateend; exit 2; }
$MSC_GIT merge FETCH_HEAD 1>>"${MSC_LOGFILE}" 2>>"${MSC_LOGFILE}"|| { gitupdateend; exit 3; }
2022-10-11 14:50:49 +02:00
if ${dosubmodupdate:+false}; then
2022-10-25 07:08:19 +02:00
$MSC_GIT submodule update --init --recursive
2022-10-11 14:50:49 +02:00
fi
2020-12-23 14:56:15 +01:00
touch $MSC_GIT_UPD_REPO_STATFILE
else
2022-09-14 14:43:45 +02:00
logwarning -n " Lokale Änderungen festgestellt:"
2020-12-23 14:56:15 +01:00
echo " Siehe Logfile ${MSC_LOGFILE}" >&2
cat << EOF >> "${MSC_LOGFILE}"
um die Änderung zurückzusetzen bitte
2022-10-21 10:58:20 +02:00
$MSC_GIT checkout \$FILENAME
2020-12-23 14:56:15 +01:00
oder um alle lokalen Änderungen auf einmal zurückzusetzen:
2022-10-21 10:58:20 +02:00
$MSC_GIT checkout .
2020-12-23 14:56:15 +01:00
ausführen
Die Änderungen sind:
2022-10-21 10:58:20 +02:00
$($MSC_GIT diff-index HEAD --|awk '{print $5, $6}')
$($MSC_GIT 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"
2022-10-31 03:08:04 +01:00
logdebug "$($MSC_GIT submodule sync --recursive)"
logdebug "$($MSC_GIT submodule update --init --recursive)"
#logdebug "$($MSC_GIT commit -a -m 'update submodules')"
2020-12-23 14:56:15 +01:00
touch $MSC_GIT_UPD_SUBMOD_STATFILE
2022-09-14 14:43:45 +02:00
logwarning "submodules synced"
2020-12-23 14:56:15 +01:00
fi
fi
2020-09-22 00:19:30 +02:00
EXIT
exit $rc