Merge pull request #1952 from gaelicWizard/uncle

lib/helpers: new function `_bash-it-find-in-ancestor()`
This commit is contained in:
Noah Gorny
2021-09-28 16:51:30 +03:00
committed by GitHub
3 changed files with 33 additions and 23 deletions

View File

@@ -849,3 +849,30 @@ then
fi
}
fi
# `_bash-it-find-in-ancestor` uses the shell's ability to run a function in
# a subshell to simplify our search to a simple `cd ..` and `[[ -r $1 ]]`
# without any external dependencies. Let the shell do what it's good at.
function _bash-it-find-in-ancestor() (
about 'searches parents of the current directory for any of the specified file names'
group 'helpers'
param '*: names of files or folders to search for'
returns '0: prints path of closest matching ancestor directory to stdout'
returns '1: no match found'
returns '2: improper usage of shell builtin' # uncommon
example '_bash-it-find-in-ancestor .git .hg'
example '_bash-it-find-in-ancestor GNUmakefile Makefile makefile'
local kin
# To keep things simple, we do not search the root dir.
while [[ "${PWD}" != '/' ]]; do
for kin in "$@"; do
if [[ -r "${PWD}/${kin}" ]]; then
printf '%s' "${PWD}"
return "$?"
fi
done
command cd .. || return "$?"
done
return 1
)