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-09-22 00:19:30 +02:00
|
|
|
ENTRY
|
2020-01-14 10:11:36 +01:00
|
|
|
cat << EOF >> "${MYSHELLCONFIG_LOGFILE}"
|
|
|
|
+-----BEGINN $(date) -------------------------------+
|
|
|
|
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
|
|
|
;;
|
|
|
|
*)
|
|
|
|
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-08-17 17:54:15 +02:00
|
|
|
#echo $SGIT fetch ${MYSHELLCONFIG_GIT_REMOTE_NAME} -p 1>&2
|
2020-09-22 00:23:21 +02:00
|
|
|
$SGIT fetch ${MYSHELLCONFIG_GIT_REMOTE_NAME} -p 2>>"${MYSHELLCONFIG_LOGFILE}"|| { logwarn fetch failed; exit 1; }
|
2020-01-14 10:11:36 +01:00
|
|
|
|
|
|
|
if $SGIT diff-index --ignore-submodules --exit-code HEAD -- >> "${MYSHELLCONFIG_LOGFILE}" ; then
|
|
|
|
cat << EOF >> "${MYSHELLCONFIG_LOGFILE}"
|
|
|
|
Check for local changes:
|
|
|
|
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
|
|
|
|
#echo "checkout origin/master as detached HEAD"
|
2020-01-17 15:23:49 +01:00
|
|
|
$SGIT checkout ${PRE}${PRE:+/}${MYSHELLCONFIG_GIT_BRANCH} 1>>"${MYSHELLCONFIG_LOGFILE}" 2>>"${MYSHELLCONFIG_LOGFILE}"|| exit 2
|
2020-01-14 10:11:36 +01:00
|
|
|
$SGIT merge FETCH_HEAD 1>>"${MYSHELLCONFIG_LOGFILE}" 2>>"${MYSHELLCONFIG_LOGFILE}"|| exit 3
|
|
|
|
else
|
|
|
|
cat << EOF >> "${MYSHELLCONFIG_LOGFILE}"
|
|
|
|
Check for local changes:
|
|
|
|
Ich habe lokale Änderungen festgestellt
|
|
|
|
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:
|
|
|
|
EOF
|
|
|
|
$SGIT diff-index HEAD --|awk '{print $5, $6}' >> "${MYSHELLCONFIG_LOGFILE}"
|
|
|
|
$SGIT diff-index -p HEAD -- >> "${MYSHELLCONFIG_LOGFILE}"
|
|
|
|
|
2020-09-22 00:23:21 +02:00
|
|
|
logwarn "Lokale Änderungen festgestellt: Siehe Logfile ${MYSHELLCONFIG_LOGFILE}" >&2
|
2020-01-14 10:11:36 +01:00
|
|
|
cat << EOF >> "${MYSHELLCONFIG_LOGFILE}"
|
|
|
|
|
|
|
|
+-----ENDE $(date) ---------------------------------+
|
|
|
|
|
|
|
|
EOF
|
|
|
|
exit 4
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
2020-10-28 23:20:37 +01:00
|
|
|
loginfo -n "repo synced"
|
2020-10-28 23:13:36 +01:00
|
|
|
|
2020-10-26 01:16:57 +01:00
|
|
|
if ${MYSHELLCONFIG_GIT_SUBMODULES_UPDATE-${MYSHELLCONFIG_GIT_SUBMODULES_UPDATE_DEFAULT-true}}; then
|
2020-10-25 11:08:37 +01:00
|
|
|
logdebug "update submodules"
|
|
|
|
rc=0
|
2020-10-28 22:11:53 +01:00
|
|
|
# srv="${MYSHELLCONFIG_GIT_SUBMODULES_SERVER-${MYSHELLCONFIG_GIT_SUBMODULES_SERVER_DEFAULT}}"
|
|
|
|
# logwarn "srv $srv"
|
|
|
|
# $SGIT config --file=.gitmodules -l | \
|
|
|
|
# grep url= | \
|
|
|
|
# awk -F "=" -v srv="${srv%/}${srv:+/}" \
|
|
|
|
# '{gsub(/^.*github.com\//,srv,$2); print $1" "$2}' | \
|
|
|
|
# while read a b; do
|
|
|
|
# logdebug "$SGIT config --file=.gitmodules ${a} ${b}"
|
|
|
|
# $SGIT config --file=.gitmodules "${a}" "${b}"
|
|
|
|
# done
|
2020-10-25 23:04:48 +01:00
|
|
|
|
2020-10-25 11:08:37 +01:00
|
|
|
# Submodules always have origin and master as repo and branch to track
|
2020-10-25 23:04:48 +01:00
|
|
|
#$SGIT submodule init 1>>"${MYSHELLCONFIG_LOGFILE}" 2>&1|| { logwarn "update submodules failed on ini"; rc=5; }
|
|
|
|
#$SGIT submodule foreach "git checkout master" 1>>"${MYSHELLCONFIG_LOGFILE}" 2>&1 || { logwarn "checkout master submodules failed on set upstream"; rc=7; }
|
|
|
|
#$SGIT submodule foreach "git branch -u origin/master master" 1>>"${MYSHELLCONFIG_LOGFILE}" 2>&1|| { logwarn "set-upstream submodules failed on set upstream"; rc=8; }
|
2020-10-28 21:17:57 +01:00
|
|
|
#$SGIT submodule update --init --recursive --remote --merge 1>>"${MYSHELLCONFIG_LOGFILE}" 2>&1 || { logwarn " update submodules failed on update"; rc=9; }
|
2020-10-28 22:11:53 +01:00
|
|
|
|
|
|
|
# run git submodule sync only ich local repo should use submodule urls from main-project
|
|
|
|
# so don't use it here
|
|
|
|
# logdebug "$($SGIT submodule sync)" || { logwarn "sync submodules failed on sync"; rc=6; }
|
2020-10-28 23:05:19 +01:00
|
|
|
logdebug "$($SGIT submodule update --remote)"
|
2020-10-28 22:11:53 +01:00
|
|
|
logdebug "$($SGIT submodule update --init --recursive)"
|
2020-10-29 00:18:57 +01:00
|
|
|
logdebug "$($SGIT commit .gitmodules -m 'update submodules')"
|
2020-10-28 22:11:53 +01:00
|
|
|
#$SGIT submodule update --init --remote --merge 1>>"${MYSHELLCONFIG_LOGFILE}" 2>&1 || { logwarn " update submodules failed on update"; rc=9; }
|
|
|
|
#$SGIT checkout .gitmodules
|
2020-10-28 23:13:36 +01:00
|
|
|
logdebug "submodules updated successfully"
|
2020-10-28 23:18:54 +01:00
|
|
|
loginfo -n ", submodules synced"
|
2020-01-14 10:11:36 +01:00
|
|
|
|
2020-10-25 11:15:26 +01:00
|
|
|
else
|
2020-10-28 23:18:54 +01:00
|
|
|
loginfo -n ", submodules not synced"
|
2020-10-26 01:16:57 +01:00
|
|
|
logdebug "Do not update submodules. It's denied by environment MYSHELLCONFIG_GIT_UPDATE_SUBMODULES: ${MYSHELLCONFIG_GIT_SUBMODULES_UPDATE-${MYSHELLCONFIG_GIT_SUBMODULES_UPDATE_DEFAULT-true}}"
|
2020-10-25 11:15:26 +01:00
|
|
|
fi
|
2020-10-28 23:19:55 +01:00
|
|
|
loginfo -n ": "
|
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
|
|
|
|
|