From c2ab8410c6f5e44d73a9e3f997b784d21bf150d5 Mon Sep 17 00:00:00 2001 From: Thomas Levine <_@thomaslevine.com> Date: Sun, 28 Feb 2016 11:22:51 +0000 Subject: [PATCH] urchin_root don't need it just yet --- urchin | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/urchin b/urchin index bbefd1b..998c96c 100755 --- a/urchin +++ b/urchin @@ -35,8 +35,36 @@ urchin_path() { fullpath "$1" } -urchin_root() { +remove_trailing_slash() { + echo "$1" | sed s/\\/$// +} +urchin_root() { + # Call recursively but remember the original argument. + current="$(remove_trailing_slash "$1")" + if test -n "$2"; then + orig="$2" + else + orig="$1" + fi + + if test "$(readlink -f -- "$1")" = /; then + # Stop traversing upwards at / + if test -d "$orig"; then + echo "$orig" + else + dirname "$orig" + fi + elif ! test -e "$current"; then + echo "$current: No such file or directory">&2 + return 1 + elif test -f "$current"; then + urchin_root "$(dirname "$current")" "$orig" + elif test -d "$current"/.urchin; then + remove_trailing_slash "$current" + else + urchin_root "$current"/.. "$orig" + fi } # Urchin version number