From 8f4771e20839b5ced15a722559d3b038235621b6 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Mon, 9 Nov 2020 01:16:07 +0000 Subject: [PATCH] x: be more flexible when finding run.sh Signed-off-by: Alejandro Mery --- files/bin/x | 74 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 19 deletions(-) diff --git a/files/bin/x b/files/bin/x index ca02537..215c2ac 100755 --- a/files/bin/x +++ b/files/bin/x @@ -2,28 +2,64 @@ set -eu -# find root of the "workspace" -find_repo_workspace_root() { - if [ -d "$1/.repo" ]; then - echo "$1" - elif [ "${1:-/}" != / ]; then - find_repo_workspace_root "${1%/*}" - fi +brute_find() { + local op="$1" cue="$2" dir="$3" + + while true; do + + if [ $op "$dir/$cue" ]; then + echo "$dir" + elif [ / != "${dir:-/}" ]; then + dir="${dir%/*}" + continue + fi + + return + done } -WS="$(find_repo_workspace_root "$PWD")" -if [ -z "$WS" ]; then - # find root of .git repository - WS="$(git rev-parse --show-superproject-working-tree 2> /dev/null || true)" - [ -d "$WS" ] || WS="$(git rev-parse --show-toplevel 2> /dev/null || true)" -fi +find_run_sh() { + local dir="${1:-$PWD}" + local try_repo=true + local ws= -if [ -d "$WS" ]; then - if [ -x "$WS/run.sh" ]; then - exec "$WS/run.sh" "$@" - fi -fi + while true; do + + [ "$PWD" = "$dir" ] || cd "$dir" + + # find root of repo workspace + if $try_repo; then + ws="$(brute_find -d .repo "$PWD")" + [ -d "$ws" ] || try_repo=false + fi + + # or find root of .git repository + if [ ! -d "$ws" ]; then + ws="$(git rev-parse --show-superproject-working-tree 2> /dev/null || true)" + [ -d "$ws" ] || ws="$(git rev-parse --show-toplevel 2> /dev/null || true)" + fi + + if [ ! -d "$ws" ]; then + # no repo? find anything then + dir="$(brute_find -x run.sh "$PWD")" + [ -z "$dir" ] || echo "$dir/run.sh" + elif [ -x "$ws/run.sh" ]; then + # found at repo's root + echo "$ws/run.sh" + elif [ / != "${ws:-/}" ]; then + # check on the parent's then + dir="${ws%/*}" + ws= + continue + fi + + return + done +} -if [ "$#" -gt 0 ]; then +run_sh="$(find_run_sh)" +if [ -x "$run_sh" ]; then + exec "$run_sh" "$@" +elif [ "$#" -gt 0 ]; then exec "$@" fi