lib/helpers: new function `_bash-it-find-in-ancestor()`

New function to do a search looking for a sibling to a parent of the current directory, for example to find `../../.git` to indicate that `$PWD` is inside a git repository.
pull/1952/head
John D Pell 2021-09-22 13:00:36 -07:00
parent 8c697715eb
commit 0471a20c7c
1 changed files with 18 additions and 0 deletions

View File

@ -849,3 +849,21 @@ then
fi
}
fi
function _bash-it-find-in-ancestor() (
# We're deliberately using a subshell for this entire function for simplicity.
# By using a subshell, we can use ${PWD} without special handling, and can
# just `cd ..` to move up the directory heirarchy.
# Let the shell do the work.
local kin
while [[ "${PWD}" != '/' ]]; do
for kin in "$@"; do
if [[ -r "${PWD}/${kin}" ]]; then
printf '%s' "${PWD}"
return "$?"
fi
done
command cd .. || return "$?"
done
return 1
)