From 1236a4dcdd99dd3f8e528f02bc835227749b527b Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Tue, 6 Feb 2018 05:18:42 -0800 Subject: [PATCH] shlib.sh: add v_get_proj_from_dir utility function Various scripts attempt to translate a directory path into a Girocco project. Create a common shared function to do this safely and avoid the code duplication. Signed-off-by: Kyle J. McKay --- shlib.sh | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/shlib.sh b/shlib.sh index 8f12ba4..30935bf 100644 --- a/shlib.sh +++ b/shlib.sh @@ -590,6 +590,42 @@ get_repo_list() { done } +# set the variable named by the first argument to the project part (i.e. WITH +# the trailing ".git" but WITHOUT the leading $cfg_reporoot) of the directory +# specified by the second argument. +# This function cannot be fooled by symbolic links. +# If the second argument is omitted (or empty) use $(pwd -P) instead. +# The directory specified by the second argument must exist. +v_get_proj_from_dir() { + [ -n "$2" ] || set -- "$1" "$(pwd -P)" + [ -d "$2" ] || return 1 + case "$2" in + "$cfg_reporoot/"?*) + # Simple case that does not need any fancy footwork + _projpart="${2#$cfg_reporoot/}" + ;; + *) + _absrr="$(cd "$cfg_reporoot" && pwd -P)" + _abspd="$(cd "$2" && pwd -P)" + case "$_abspd" in + "$_absrr/"?*) + # The normal case + _projpart="${_abspd#$_absrr/}" + ;; + *) + # Must have been reached via a symbolic link, but + # we have no way to know the source, so use a + # generic "_external" leader combined with just the + # trailing directory name + _abspd="${_abspd%/}" + _abspd="${_abspd%/.git}" + _projpart="_external/${_abspd##*/}" + ;; + esac + esac + eval "$1="'"$_projpart"' +} + # Return success if the given project name has at least one immediate child fork # that has a non-zero length alternates file has_forks_with_alternates() { -- 2.11.4.GIT