1
0
Fork 0
mirror of synced 2024-11-26 09:15:34 -05:00

Fix degit update

Had to remove ${DIR} before renaming ${dir_new} to ${DIR}.

Also also add ${REV} to the info file, in case we need it in the future,
when extracting Zim's degit as a standalone tool for example.
This commit is contained in:
Eric Nielsen 2021-04-08 16:26:33 -05:00
parent cffd456556
commit 2869851379
No known key found for this signature in database
GPG key ID: 47D1DBFA0765A1FB
2 changed files with 45 additions and 45 deletions

View file

@ -6,9 +6,9 @@ readonly URL=${4}
readonly REV=${6} readonly REV=${6}
readonly -i PRINTLEVEL=${7} readonly -i PRINTLEVEL=${7}
readonly CLEAR_LINE=<%= clear_line %> readonly CLEAR_LINE=<%= clear_line %>
readonly PREFIX=.zim_degit readonly PREFIX=.zim_degit_${RANDOM}
readonly TARBALL_TARGET=${DIR}/${PREFIX}_tarball.tar.gz readonly TARBALL_TARGET=${DIR}/${PREFIX}_tarball.tar.gz
readonly INFO_TARGET=${DIR}/${PREFIX}_info readonly INFO_TARGET=${DIR}/.zim_degit_info
print_error() { print_error() {
print -u2 -PR ${CLEAR_LINE}"%F{red}<%= error %>%B${MODULE}:%b ${1}%f"${2:+$'\n'${(F):- ${(f)^2}}} print -u2 -PR ${CLEAR_LINE}"%F{red}<%= error %>%B${MODULE}:%b ${1}%f"${2:+$'\n'${(F):- ${(f)^2}}}
@ -26,9 +26,10 @@ print_done() {
download_tarball() { download_tarball() {
setopt LOCAL_OPTIONS EXTENDED_GLOB setopt LOCAL_OPTIONS EXTENDED_GLOB
local host repo
if [[ ${URL} =~ <%= url_regex %> ]]; then if [[ ${URL} =~ <%= url_regex %> ]]; then
local -r host=${match[3]} host=${match[3]}
local -r repo=${match[4]%.git} repo=${match[4]%.git}
fi fi
if [[ ${host} != github.com || -z ${repo} ]]; then if [[ ${host} != github.com || -z ${repo} ]]; then
print_error "${URL} is not a valid github.com URL. Will not try to ${ACTION}." print_error "${URL} is not a valid github.com URL. Will not try to ${ACTION}."
@ -36,32 +37,34 @@ download_tarball() {
fi fi
local -r headers_target=${DIR}/${PREFIX}_headers local -r headers_target=${DIR}/${PREFIX}_headers
{ {
local -i http_code local info_header
local -r tarball_url=https://api.github.com/repos/${repo}/tarball/${REV}
local header etag_in etag_out
if [[ -r ${INFO_TARGET} ]]; then if [[ -r ${INFO_TARGET} ]]; then
local -r info=(${(f)"$(<${INFO_TARGET})"}) local -r info=("${(@f)"$(<${INFO_TARGET})"}")
if [[ ${URL} != ${info[1]} ]]; then if [[ ${URL} != ${info[1]} ]]; then
print_error "URL does not match. Expected ${URL}. Will not try to ${ACTION}." print_error "URL does not match. Expected ${URL}. Will not try to ${ACTION}."
return 1 return 1
fi fi
etag_out=${info[2]} # Previous REV is in line 2, reserved for future use.
info_header=${info[3]}
fi fi
local -r tarball_url=https://api.github.com/repos/${repo}/tarball/${REV}
if (( ${+commands[curl]} )); then if (( ${+commands[curl]} )); then
if ! ERR=$(command curl -fsSL ${etag_out:+-H} ${etag_out} -o ${TARBALL_TARGET} -D ${headers_target} ${tarball_url} 2>&1); then if ! ERR=$(command curl -fsSL ${info_header:+-H} ${info_header} -o ${TARBALL_TARGET} -D ${headers_target} ${tarball_url} 2>&1); then
print_error "Error downloading ${tarball_url} with curl" ${ERR} print_error "Error downloading ${tarball_url} with curl" ${ERR}
return 1 return 1
fi fi
else else
# wget returns 8 when 304 Not Modified, so we cannot use wget's error codes # wget returns 8 when 304 Not Modified, so we cannot use wget's error codes
command wget -q ${etag_out:+--header=${etag_out}} -O ${TARBALL_TARGET} -S ${tarball_url} 2>${headers_target} command wget -q ${info_header:+--header=${info_header}} -O ${TARBALL_TARGET} -S ${tarball_url} 2>${headers_target}
fi fi
local header etag
local -i http_code
while IFS= read -r header; do while IFS= read -r header; do
header=${${header## ##}%%$'\r'##} header=${${header## ##}%%$'\r'##}
if [[ ${header} == HTTP/* ]]; then if [[ ${header} == HTTP/* ]]; then
http_code=${${(s: :)header}[2]} http_code=${${(s: :)header}[2]}
elif [[ ${${(L)header%%:*}%% ##} == 'etag' ]]; then elif [[ ${${(L)header%%:*}%% ##} == 'etag' ]]; then
etag_in=${${header#*:}## ##} etag=${${header#*:}## ##}
fi fi
done < ${headers_target} done < ${headers_target}
if (( http_code == 304 )); then if (( http_code == 304 )); then
@ -72,11 +75,11 @@ download_tarball() {
print_error "Error downloading ${tarball_url}, HTTP code ${http_code}" print_error "Error downloading ${tarball_url}, HTTP code ${http_code}"
return 1 return 1
fi fi
if [[ -z ${etag_in} ]]; then if [[ -z ${etag} ]]; then
print_error "Error downloading ${tarball_url}, no ETag header found in response" print_error "Error downloading ${tarball_url}, no ETag header found in response"
return 1 return 1
fi fi
if ! print -R ${URL}$'\n'"If-None-Match: ${etag_in}" >! ${INFO_TARGET} 2>/dev/null; then if ! print -R ${URL}$'\n'${REV}$'\n'"If-None-Match: ${etag}" >! ${INFO_TARGET} 2>/dev/null; then
print_error "Error creating or updating ${INFO_TARGET}" print_error "Error creating or updating ${INFO_TARGET}"
return 1 return 1
fi fi
@ -118,7 +121,7 @@ create_dir() {
print_error "Module was not installed using Zim's degit. Will not try to update. You can disable this with the zmodule option -z|--frozen." print_error "Module was not installed using Zim's degit. Will not try to update. You can disable this with the zmodule option -z|--frozen."
return 1 return 1
fi fi
local -r dir_new=${DIR}${PREFIX}_${RANDOM} local -r dir_new=${DIR}${PREFIX}
{ {
download_tarball || return 1 download_tarball || return 1
if [[ ! -e ${TARBALL_TARGET} ]]; then if [[ ! -e ${TARBALL_TARGET} ]]; then
@ -126,18 +129,15 @@ create_dir() {
return 0 return 0
fi fi
create_dir ${dir_new} && untar_tarball ${dir_new} || return 1 create_dir ${dir_new} && untar_tarball ${dir_new} || return 1
if ! ERR=$({ command mv -f ${INFO_TARGET} ${dir_new} && command mv -f ${dir_new} ${DIR} } 2>&1); then if ! ERR=$({ command cp -f ${INFO_TARGET} ${dir_new} && \
command rm -rf ${DIR} && command mv -f ${dir_new} ${DIR} } 2>&1); then
print_error "Error updating ${DIR}" ${ERR} print_error "Error updating ${DIR}" ${ERR}
return 1 return 1
fi fi
print_done updated print_done updated
} always { } always {
(( TRY_BLOCK_ERROR = ? )) command rm -f ${TARBALL_TARGET} 2>/dev/null
if (( TRY_BLOCK_ERROR )); then command rm -rf ${dir_new} 2>/dev/null
command rm -f ${TARBALL_TARGET} 2>/dev/null
if [[ -f ${dir_new}/${INFO_TARGET:t} ]] command mv -f ${dir_new}/${INFO_TARGET:t} ${DIR}
command rm -rf ${dir_new} 2>/dev/null
fi
} }
;; ;;
esac esac

View file

@ -335,7 +335,7 @@ _zimfw_compile() {
} }
_zimfw_info() { _zimfw_info() {
print -R 'zimfw version: '${_zversion}' (built at 2021-04-07 22:49:03 UTC, previous commit is 263f8b0)' print -R 'zimfw version: '${_zversion}' (built at 2021-04-08 16:37:09 UTC, previous commit is cffd456)'
print -R 'ZIM_HOME: '${ZIM_HOME} print -R 'ZIM_HOME: '${ZIM_HOME}
print -R 'Zsh version: '${ZSH_VERSION} print -R 'Zsh version: '${ZSH_VERSION}
print -R 'System info: '$(command uname -a) print -R 'System info: '$(command uname -a)
@ -420,9 +420,9 @@ readonly URL=\${4}
readonly REV=\${6} readonly REV=\${6}
readonly -i PRINTLEVEL=\${7} readonly -i PRINTLEVEL=\${7}
readonly CLEAR_LINE=$'\E[2K\r' readonly CLEAR_LINE=$'\E[2K\r'
readonly PREFIX=.zim_degit readonly PREFIX=.zim_degit_\${RANDOM}
readonly TARBALL_TARGET=\${DIR}/\${PREFIX}_tarball.tar.gz readonly TARBALL_TARGET=\${DIR}/\${PREFIX}_tarball.tar.gz
readonly INFO_TARGET=\${DIR}/\${PREFIX}_info readonly INFO_TARGET=\${DIR}/.zim_degit_info
print_error() { print_error() {
print -u2 -PR \${CLEAR_LINE}\"%F{red}x %B\${MODULE}:%b \${1}%f\"\${2:+$'\n'\${(F):- \${(f)^2}}} print -u2 -PR \${CLEAR_LINE}\"%F{red}x %B\${MODULE}:%b \${1}%f\"\${2:+$'\n'\${(F):- \${(f)^2}}}
@ -440,9 +440,10 @@ print_done() {
download_tarball() { download_tarball() {
setopt LOCAL_OPTIONS EXTENDED_GLOB setopt LOCAL_OPTIONS EXTENDED_GLOB
local host repo
if [[ \${URL} =~ ^([^:@/]+://)?([^@]+@)?([^:/]+)[:/]([^/]+/[^/]+)/?\$ ]]; then if [[ \${URL} =~ ^([^:@/]+://)?([^@]+@)?([^:/]+)[:/]([^/]+/[^/]+)/?\$ ]]; then
local -r host=\${match[3]} host=\${match[3]}
local -r repo=\${match[4]%.git} repo=\${match[4]%.git}
fi fi
if [[ \${host} != github.com || -z \${repo} ]]; then if [[ \${host} != github.com || -z \${repo} ]]; then
print_error \"\${URL} is not a valid github.com URL. Will not try to \${ACTION}.\" print_error \"\${URL} is not a valid github.com URL. Will not try to \${ACTION}.\"
@ -450,32 +451,34 @@ download_tarball() {
fi fi
local -r headers_target=\${DIR}/\${PREFIX}_headers local -r headers_target=\${DIR}/\${PREFIX}_headers
{ {
local -i http_code local info_header
local -r tarball_url=https://api.github.com/repos/\${repo}/tarball/\${REV}
local header etag_in etag_out
if [[ -r \${INFO_TARGET} ]]; then if [[ -r \${INFO_TARGET} ]]; then
local -r info=(\${(f)\"\$(<\${INFO_TARGET})\"}) local -r info=(\"\${(@f)\"\$(<\${INFO_TARGET})\"}\")
if [[ \${URL} != \${info[1]} ]]; then if [[ \${URL} != \${info[1]} ]]; then
print_error \"URL does not match. Expected \${URL}. Will not try to \${ACTION}.\" print_error \"URL does not match. Expected \${URL}. Will not try to \${ACTION}.\"
return 1 return 1
fi fi
etag_out=\${info[2]} # Previous REV is in line 2, reserved for future use.
info_header=\${info[3]}
fi fi
local -r tarball_url=https://api.github.com/repos/\${repo}/tarball/\${REV}
if (( \${+commands[curl]} )); then if (( \${+commands[curl]} )); then
if ! ERR=\$(command curl -fsSL \${etag_out:+-H} \${etag_out} -o \${TARBALL_TARGET} -D \${headers_target} \${tarball_url} 2>&1); then if ! ERR=\$(command curl -fsSL \${info_header:+-H} \${info_header} -o \${TARBALL_TARGET} -D \${headers_target} \${tarball_url} 2>&1); then
print_error \"Error downloading \${tarball_url} with curl\" \${ERR} print_error \"Error downloading \${tarball_url} with curl\" \${ERR}
return 1 return 1
fi fi
else else
# wget returns 8 when 304 Not Modified, so we cannot use wget's error codes # wget returns 8 when 304 Not Modified, so we cannot use wget's error codes
command wget -q \${etag_out:+--header=\${etag_out}} -O \${TARBALL_TARGET} -S \${tarball_url} 2>\${headers_target} command wget -q \${info_header:+--header=\${info_header}} -O \${TARBALL_TARGET} -S \${tarball_url} 2>\${headers_target}
fi fi
local header etag
local -i http_code
while IFS= read -r header; do while IFS= read -r header; do
header=\${\${header## ##}%%$'\r'##} header=\${\${header## ##}%%$'\r'##}
if [[ \${header} == HTTP/* ]]; then if [[ \${header} == HTTP/* ]]; then
http_code=\${\${(s: :)header}[2]} http_code=\${\${(s: :)header}[2]}
elif [[ \${\${(L)header%%:*}%% ##} == 'etag' ]]; then elif [[ \${\${(L)header%%:*}%% ##} == 'etag' ]]; then
etag_in=\${\${header#*:}## ##} etag=\${\${header#*:}## ##}
fi fi
done < \${headers_target} done < \${headers_target}
if (( http_code == 304 )); then if (( http_code == 304 )); then
@ -486,11 +489,11 @@ download_tarball() {
print_error \"Error downloading \${tarball_url}, HTTP code \${http_code}\" print_error \"Error downloading \${tarball_url}, HTTP code \${http_code}\"
return 1 return 1
fi fi
if [[ -z \${etag_in} ]]; then if [[ -z \${etag} ]]; then
print_error \"Error downloading \${tarball_url}, no ETag header found in response\" print_error \"Error downloading \${tarball_url}, no ETag header found in response\"
return 1 return 1
fi fi
if ! print -R \${URL}$'\n'\"If-None-Match: \${etag_in}\" >! \${INFO_TARGET} 2>/dev/null; then if ! print -R \${URL}$'\n'\${REV}$'\n'\"If-None-Match: \${etag}\" >! \${INFO_TARGET} 2>/dev/null; then
print_error \"Error creating or updating \${INFO_TARGET}\" print_error \"Error creating or updating \${INFO_TARGET}\"
return 1 return 1
fi fi
@ -532,7 +535,7 @@ create_dir() {
print_error \"Module was not installed using Zim's degit. Will not try to update. You can disable this with the zmodule option -z|--frozen.\" print_error \"Module was not installed using Zim's degit. Will not try to update. You can disable this with the zmodule option -z|--frozen.\"
return 1 return 1
fi fi
local -r dir_new=\${DIR}\${PREFIX}_\${RANDOM} local -r dir_new=\${DIR}\${PREFIX}
{ {
download_tarball || return 1 download_tarball || return 1
if [[ ! -e \${TARBALL_TARGET} ]]; then if [[ ! -e \${TARBALL_TARGET} ]]; then
@ -540,18 +543,15 @@ create_dir() {
return 0 return 0
fi fi
create_dir \${dir_new} && untar_tarball \${dir_new} || return 1 create_dir \${dir_new} && untar_tarball \${dir_new} || return 1
if ! ERR=\$({ command mv -f \${INFO_TARGET} \${dir_new} && command mv -f \${dir_new} \${DIR} } 2>&1); then if ! ERR=\$({ command cp -f \${INFO_TARGET} \${dir_new} && \
command rm -rf \${DIR} && command mv -f \${dir_new} \${DIR} } 2>&1); then
print_error \"Error updating \${DIR}\" \${ERR} print_error \"Error updating \${DIR}\" \${ERR}
return 1 return 1
fi fi
print_done updated print_done updated
} always { } always {
(( TRY_BLOCK_ERROR = ? )) command rm -f \${TARBALL_TARGET} 2>/dev/null
if (( TRY_BLOCK_ERROR )); then command rm -rf \${dir_new} 2>/dev/null
command rm -f \${TARBALL_TARGET} 2>/dev/null
if [[ -f \${dir_new}/\${INFO_TARGET:t} ]] command mv -f \${dir_new}/\${INFO_TARGET:t} \${DIR}
command rm -rf \${dir_new} 2>/dev/null
fi
} }
;; ;;
esac esac