zimfw/src/stage2/75_zimfw_tool_git.zsh.erb

94 lines
3.5 KiB
Plaintext

_zimfw_tool_git() {
# This runs in a subshell
readonly -i SUBMODULES=${5}
readonly DIR=${1} URL=${2} TYPE=${3} ONPULL=${6}
REV=${4}
case ${_zaction} in
install)
if ERR=$(command git clone ${REV:+-b} ${REV} -q --config core.autocrlf=false ${${SUBMODULES:#0}:+--recursive} -- ${URL} ${DIR} 2>&1); then
_zimfw_pull_print_okay Installed
else
_zimfw_print_error 'Error during git clone' ${ERR}
return 1
fi
;;
check|update)
if [[ ! -r ${DIR}/.git ]]; then
_zimfw_print_warn 'Module was not installed using git. Will not try to '${_zaction}$'. Use zmodule option <%= bold %>-z<%= normalyellow %>|<%= bold %>--frozen<%= normalyellow %> to disable this warning.'
return 0
fi
if [[ ${URL} != $(command git -C ${DIR} config --get remote.origin.url) ]]; then
_zimfw_print_error "URL does not match. Expected ${URL}. Will not try to ${_zaction}."
return 1
fi
if ! ERR=$(command git -C ${DIR} fetch -pqt origin 2>&1); then
_zimfw_print_error 'Error during git fetch' ${ERR}
return 1
fi
if [[ ${TYPE} == branch ]]; then
if [[ -z ${REV} ]]; then
# Get HEAD remote branch
if ! ERR=$(command git -C ${DIR} remote set-head origin -a 2>&1); then
_zimfw_print_error 'Error during git remote set-head' ${ERR}
return 1
fi
if REV=$(command git -C ${DIR} symbolic-ref --short refs/remotes/origin/HEAD 2>&1); then
REV=${REV#origin/}
else
_zimfw_print_error 'Error during git symbolic-ref' ${REV}
return 1
fi
fi
TO_REV=${REV}@{u}
if [[ ${_zaction} == check ]]; then
readonly -i BEHIND=$(command git -C ${DIR} rev-list --count ${REV}..${TO_REV} -- 2>/dev/null)
if (( BEHIND )); then
_zimfw_print_okay "Update available [behind ${BEHIND}]"
return 4
else
_zimfw_print_okay 'Already up to date' 1
return 0
fi
fi
else
if [[ ${REV} == $(command git -C ${DIR} describe --tags --exact-match 2>/dev/null) ]]; then
if [[ ${_zaction} == check ]]; then
_zimfw_print_okay 'Already up to date' 1
return 0
else
_zimfw_pull_print_okay 'Already up to date'
return ${?}
fi
fi
if [[ ${_zaction} == check ]]; then
_zimfw_print_okay 'Update available'
return 4
fi
TO_REV=${REV}
fi
LOG=$(command git -C ${DIR} log --graph --color --format='%C(yellow)%h%C(reset) %s %C(cyan)(%cr)%C(reset)' ..${TO_REV} -- 2>/dev/null)
if ! ERR=$(command git -C ${DIR} checkout -q ${REV} -- 2>&1); then
_zimfw_print_error 'Error during git checkout' ${ERR}
return 1
fi
if [[ ${TYPE} == branch ]]; then
if ! OUT=$(command git -C ${DIR} merge --ff-only --no-progress -n 2>&1); then
_zimfw_print_error 'Error during git merge' ${OUT}
return 1
fi
# keep just first line of OUT
OUT=${OUT%%($'\n'|$'\r')*}
else
OUT="Updating to ${TYPE} ${REV}"
fi
if (( SUBMODULES )); then
if ! ERR=$(command git -C ${DIR} submodule update --init --recursive -q -- 2>&1); then
_zimfw_print_error 'Error during git submodule update' ${ERR}
return 1
fi
fi
_zimfw_pull_print_okay ${OUT} 0 ${LOG}
;;
esac
}