From 51fa9dc879dbb51bea9287483afa154a54f10d50 Mon Sep 17 00:00:00 2001 From: Thomas Levine <_@thomaslevine.com> Date: Sun, 10 Apr 2016 21:17:19 +0000 Subject: [PATCH] localpath --- urchin | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/urchin b/urchin index ac68a03..7ba404b 100755 --- a/urchin +++ b/urchin @@ -241,6 +241,25 @@ fullpath() { readlink -f -- "${1}" } +# If 1 and 2 are the same path, echo "." +# If 1 is a parent of 2, echo the path of 2 relative 1. +# Otherwise, return with code 1. +localpath() { + parent="$(fullpath "${1}" | sed 's/\/*$//')" + child="$(fullpath "${2}" | sed 's/\/*$//')" + + if test "${parent}" = "${child}"; then + echo . + else + rel="${child##"${parent}/"}" + if test "${rel}" = "${parent}"; then + return 1 + else + echo "${rel}" + fi + fi +} + contains() { case "$#" in 1) grep "${1}" > /dev/null ;; @@ -438,10 +457,9 @@ format_urchin() { recurse() { test_suite_root="${1}" requested_path="${2}" - potential_test="$(fullpath "${3}")" - abs="${potential_test}" - rel="${potential_test##"${test_suite_root}/"}" + abs="$(fullpath "${3}")" + rel="${abs##"${test_suite_root}/"}" for ignore in setup_dir teardown_dir setup teardown; do if test "$(basename "${potential_test}")" = "${ignore}"; then