Skip to content

Commit 0471a20

Browse files
committed
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.
1 parent 8c69771 commit 0471a20

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

lib/helpers.bash

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,3 +849,21 @@ then
849849
fi
850850
}
851851
fi
852+
853+
function _bash-it-find-in-ancestor() (
854+
# We're deliberately using a subshell for this entire function for simplicity.
855+
# By using a subshell, we can use ${PWD} without special handling, and can
856+
# just `cd ..` to move up the directory heirarchy.
857+
# Let the shell do the work.
858+
local kin
859+
while [[ "${PWD}" != '/' ]]; do
860+
for kin in "$@"; do
861+
if [[ -r "${PWD}/${kin}" ]]; then
862+
printf '%s' "${PWD}"
863+
return "$?"
864+
fi
865+
done
866+
command cd .. || return "$?"
867+
done
868+
return 1
869+
)

0 commit comments

Comments
 (0)