myshellconfig/myshell_git_sync
2022-10-31 03:08:04 +01:00

165 lines
7.2 KiB
Bash

#!/bin/bash
# Sync myshellconfig with configured git repo
if ! $SSHS; then
### set userspecific variables #######
[ -z "$PDSH_RCMD_TYPE" ] && export PDSH_RCMD_TYPE=ssh
[ -z "$XDG_RUNTIME_DIR" ] && export export XDG_RUNTIME_DIR=/run/user/$(id -u $USER)
######################################
MSC_GIT_PROTOCOL_GIT="git://"
MSC_GIT_PROTOCOL_HTTP="http://"
MSC_GIT_PROTOCOL_SSH="git@"
MSC_GIT_PROTOCOL_FILE=""
[ -z "${MSC_GIT_SERVER+x}" ] && MSC_GIT_SERVER="gitea.schuerz.at"
[ -z "${MSC_GIT_REPO_NAME+x}" ] && MSC_GIT_REPO_NAME="$(basename $MSC_BASE).git"
[ -z "${MSC_GIT_REPO_PATH+x}" ] && MSC_GIT_REPO_PATH="jakob"
# [ -z "${MSC_GIT_REPO_PATH_FILE+x}" ] && MSC_GIT_REPO_PATH_FILE="/media/$USER/gitstick/"
[ -z "${MSC_GIT_REPO_PATH_FILE+x}" ] && MSC_GIT_REPO_PATH_FILE="/srv/git/"
MSC_GIT_REMOTE_PUBLIC_GIT="${MSC_GIT_PROTOCOL_GIT}${MSC_GIT_SERVER}/${MSC_GIT_REPO_PATH}/"
MSC_GIT_REMOTE_PUBLIC_SSH="${MSC_GIT_PROTOCOL_SSH}${MSC_GIT_SERVER}:${MSC_GIT_REPO_PATH}/"
MSC_GIT_REMOTE_PUBLIC_HTTP="${MSC_GIT_PROTOCOL_HTTP}${MSC_GIT_SERVER}/${MSC_GIT_REPO_PATH}/"
MSC_GIT_REMOTE_PUBLIC_FILE="${MSC_GIT_PROTOCOL_FILE}${MSC_GIT_REPO_PATH_FILE}"
MSC_GIT_REMOTE_PUBLIC_DEFAULT="${MSC_GIT_REMOTE_PUBLIC_GIT}"
MSC_GIT_REMOTE_DEFAULT="${MSC_GIT_PROTOCOL_GIT}${MSC_GIT_SERVER}${MSC_GIT_REPO_PATH_GIT}"
# If MSC_GIT_REMOTE is set in ~/.bashrc before sourcing this file, take value from ~/.bashrc
# If set MSC_GIT_REMOTE_PROTOCOL in ~/.bashrc before sourcing this file, you cange choose one of the above
# defined values for a specific host
if [ -z ${MSC_GIT_REMOTE+x} ]; then
case $MSC_GIT_REMOTE_PROTOCOL in
git)
MSC_GIT_REMOTE_PUBLIC="${MSC_GIT_REMOTE_PUBLIC_GIT}"
;;
ssh)
MSC_GIT_REMOTE_PUBLIC="${MSC_GIT_REMOTE_PUBLIC_SSH}"
;;
http)
MSC_GIT_REMOTE_PUBLIC="${MSC_GIT_REMOTE_PUBLIC_HTTP}"
;;
file)
MSC_GIT_REMOTE_PUBLIC="${MSC_GIT_REMOTE_PUBLIC_FILE}"
MSC_GIT_SERVER="${MSC_GIT_REPO_PATH_FILE}"
;;
*)
MSC_GIT_REMOTE_PUBLIC="${MSC_GIT_REMOTE_DEFAULT}"
;;
esac
MSC_GIT_REMOTE="${MSC_GIT_REMOTE_PUBLIC}${MSC_GIT_REPO_NAME}"
fi
if [ -z ${MSC_GIT_REMOTE_PUSH+x} ]; then
case $MSC_GIT_REMOTE_PUSH_PROTOCOL in
git)
MSC_GIT_REMOTE_PUSH_PUBLIC="${MSC_GIT_REMOTE_PUBLIC_GIT}"
;;
ssh)
MSC_GIT_REMOTE_PUSH_PUBLIC="${MSC_GIT_REMOTE_PUBLIC_SSH}"
;;
http)
MSC_GIT_REMOTE_PUSH_PUBLIC="${MSC_GIT_REMOTE_PUBLIC_HTTP}"
;;
file)
MSC_GIT_REMOTE_PUSH_PUBLIC="${MSC_GIT_REMOTE_PUBLIC_FILE}"
;;
*)
MSC_GIT_REMOTE_PUSH_PUBLIC="${MSC_GIT_REMOTE_PUBLIC_DEFAULT}"
;;
esac
MSC_GIT_REMOTE_PUSH=${MSC_GIT_REMOTE_PUSH_PUBLIC}${MSC_GIT_REPO_NAME}
fi
# [ -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=$(git rev-parse --abbrev-ref HEAD)
[ -z "${MSC_GIT_REMOTE_NAME+x}" ] && export MSC_GIT_REMOTE_NAME=${MSC_GIT_REMOTE_NAME_DEFAULT}
[ -z "${MSC_GIT_BRANCH+x}" ] && export MSC_GIT_BRANCH=${MSC_GIT_BRANCH_DEFAULT}
# If TAG is set, checkout is always headless.
[ -z "${MSC_GIT_TAG:+x}" ] || { export MSC_GIT_BRANCH=${MSC_GIT_TAG}; export MSC_GIT_CHECKOUTSCRIPT_OPTIONS="-h"; }
#GIT_SSH_PATH="/srv/repos"
#echo "Run with $TERM" >&2
case $TERM in
#*term*|*linux*)
*term*)
if [ -d "${MSC_BASE}" -a $($MSC_GIT status 1>/dev/null 2>&1; echo $?) -eq 0 ]; then
# MSC_BASE exists and is a git repo
[ -d "${MSC_LOGDIR}" ] || mkdir -p "${MSC_LOGDIR}"
if ! $MSC_GIT rev-parse --git-dir > /dev/null 2>&1 ; then
# MSC_BASE is not a git-repo initialize it
loginfo "Init ${MSC_BASE} as git-repo" >&2
$MSC_GIT init
fi
# Update Userinformations for git
$MSC_GIT config user.email "${USERNAME}"
$MSC_GIT config user.name "${FULLNAME}"
# set upstream only if not detached
# [ $($MSC_GIT rev-parse --abbrev-ref HEAD) != "HEAD" ] && $MSC_GIT branch --set-upstream-to=${MSC_GIT_REMOTE_NAME}/${MSC_GIT_BRANCH}
# sync repo with origin if git is reachable
if ckrepo ; then
loginfo "git checkout" >&2
sync_config
. ${MSC_BASE}/functions.sh
create_symlinks "$MSC_BASE"
else
loginfo "${MSC_GIT_SERVER}" not reachable or sync-time not reached>&2;
loginfo "profile not syncing" >&2;
fi
else
loginfo "Clone ${MSC_GIT_REMOTE} and configure git" >&2
if $( timeout --foreground "${MSC_GIT_CHECKOUT_TIMEOUT-$MSC_GIT_CHECKOUT_TIMEOUT_DEFAULT}" \
git -C ${HOME} clone --recurse-submodules "${MSC_GIT_REMOTE}" "${MSC_BASE}" ); then
:
else
MSC_GIT_REMOTE="${MSC_GIT_REMOTE_PUBLIC_HTTP}${MSC_GIT_REPO_NAME}"
loginfo "Clone ${MSC_GIT_REMOTE} (http, because other protocol is not reachable) and configure git" >&2
timeout --foreground "${MSC_GIT_CHECKOUT_TIMEOUT-$MSC_GIT_CHECKOUT_TIMEOUT_DEFAULT}" \
git -C ${HOME} clone --recurse-submodules "${MSC_GIT_REMOTE}" "${MSC_BASE}" || return 1 2>/dev/null || exit 1
fi
[ -d "${MSC_BASE}" ] && { loginfo create ${MSC_LOGDIR} >&2; mkdir -p "${MSC_LOGDIR}"; }
$MSC_GIT config user.email "${USERNAME}"
$MSC_GIT config user.name "${FULLNAME}"
# Initialize Vundle as preconfigured Submodule
#$MSC_GIT submodule update --init --recursive
#$MSC_GIT submodule foreach 'git checkout master'
loginfo "sync config" >&2
sync_config
. ${MSC_BASE}/functions.sh
loginfo "config synced, functions.sh sourced" >&2
create_symlinks "$MSC_BASE"
# Install vim Plugins
# echo "Run Vim, and in Vim run :PluginInstall to install all Plugins"
# not necessary any more. Vim plugins are updated through git submodules
#vim -c "PluginInstall" -c ":qa!"
fi
#[ -z ${MSC_GIT_REMOTE_PUSH+x} ] || $MSC_GIT remote set-url --push ${MSC_GIT_REMOTE_NAME} "${MSC_GIT_REMOTE_PUSH}"
;;
*screen*)
loginfo "I'm in sceen/tmux now - no profily syncing" >&2
;;
*dumb*)
loginfo "Run with dumb terminal - no profily syncing" 1>&2
;;
*)
loginfo "Run with $TERM - no profily syncing" >&2
;;
esac
fi