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

Add `composure.sh` citation with examples and rewrite internal comments to describe the code flow.
pull/1952/head
John D Pell 2021-09-22 13:10:19 -07:00
parent 7ed12083f2
commit e8966ea2a5
1 changed files with 13 additions and 4 deletions

View File

@ -850,12 +850,21 @@ then
}
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() (
# 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.
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