Skip to content

Commit 7b6caac

Browse files
committed
lib/helpers: new function _bash_it_find_kin()
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 77e9404 commit 7b6caac

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

lib/helpers.bash

100644100755
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,3 +837,23 @@ then
837837
fi
838838
}
839839
fi
840+
841+
function _bash_it_find_kin()
842+
(
843+
# We're deliberately using a subshell for this entire function for simplicity.
844+
# By using a subshell, we can use ${PWD} without special handling, and can
845+
# just `cd ..` to move up the directory heirarchy. Let the shell do the work.
846+
local common_ancestor="${PWD}"
847+
local uncle="${1?${FUNCNAME[0]}: a file to locate must be specified}" aunt="${2:-}"
848+
while [[ "${PWD:-/}" != '/' ]]
849+
do
850+
if [[ -r "${PWD}/${uncle}" || -r "${aunt:+${PWD}/}${aunt:-}" ]]
851+
then
852+
common_ancestor="${PWD}"
853+
echo "${common_ancestor}"
854+
return 0
855+
fi
856+
command cd .. || return 2
857+
done
858+
return 1
859+
)

0 commit comments

Comments
 (0)