|
1 | 1 | #! /usr/bin/env sh
|
| 2 | + |
| 3 | +# Remove trailing and squeeze multiple '/' |
| 4 | +normalize() { |
| 5 | + printf '%s' "$1" | tr -s '/' | sed 's^/$^^' |
| 6 | +} |
| 7 | + |
| 8 | +# Build relative path from arg1 to arg2 |
| 9 | +# Return arg2, if has not equal parts |
| 10 | +# Require absolute paths as args & confused with '.' & '..'! |
| 11 | +relpath () ( |
| 12 | + _from="$(normalize "$1")" |
| 13 | + _to="$(normalize "$2")" |
| 14 | + [ ! x"${_from#/}" = x"${_from}" ] && [ ! x"${_to#/}" = x"${_to}" ] || { printf 'relpath: %s\n' 'Absolute paths is required!' "arg1='${_from}'" "arg2='${_to}'" >&2; return 1; } |
| 15 | + [ x"$(printf '%s ' "${_from}" "${_to}" | grep -E '\./' 2>/dev/null)" = x ] || { printf 'relpath: %s\n' "Use of '.'/'..' in absolute path is confusing..." "arg1='${_from}'" "arg2='${_to}'" >&2; return 1; } |
| 16 | + _from="${_from#/}" |
| 17 | + _depth=$(printf '%s' "${_from}" | tr "/\n" "\n/" | wc -l) # depth to root |
| 18 | + # Eat equal parts |
| 19 | + _OIFS="${IFS}" |
| 20 | + IFS="/" |
| 21 | + set -- ${_to} |
| 22 | + IFS="${_OIFS}" |
| 23 | + while shift; do # First shift remove emty elem |
| 24 | + [ x"${_from#"$1"}" = x"${_from}" ] && break |
| 25 | + _from="${_from#"$1"/}" |
| 26 | + _to="${_to#/"$1"}" |
| 27 | + done |
| 28 | + _steps=$(printf '%s' "${_from}" | tr "/\n" "\n/" | wc -l) # depth remain |
| 29 | + [ ${_depth} -eq ${_steps} ] && printf '%s' "${_to}" && return 0 # no equal parts found |
| 30 | + _ret= |
| 31 | + for i in $(seq 1 ${_steps}); do |
| 32 | + _ret="../${_ret}" |
| 33 | + done |
| 34 | + printf '%s' "${_ret}${_to#/}" |
| 35 | +) |
| 36 | + |
2 | 37 | gitbin="$(which git)" || { printf '%s\n' 'Well, this is discouraging...' "Can't find git bin\! Is it installed?"; exit 1; }
|
3 | 38 | hooks_dest="${1:-$(cd "$(dirname -- "${self}")/.."; "${gitbin}" rev-parse --show-toplevel 2>/dev/null || pwd)/.git}/hooks"
|
4 | 39 | hooks_dest="$(normalize "${hooks_dest}")"
|
@@ -178,7 +213,6 @@ clear
|
178 | 213 | pheader
|
179 | 214 | phooks
|
180 | 215 | while :; do
|
181 |
| - _test2=$g_iHooksPage |
182 | 216 | pstatus "Please, enter number to operate on hook, 'n'/'p' for next/previous page or 'q' to exit setup" ""
|
183 | 217 | pstatus
|
184 | 218 | read ui
|
|
0 commit comments