You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.3 KiB

  1. #!/bin/sh
  2. set -eu
  3. brute_find() {
  4. local op="$1" cue="$2" dir="$3"
  5. while true; do
  6. if [ $op "$dir/$cue" ]; then
  7. echo "$dir"
  8. elif [ / != "${dir:-/}" ]; then
  9. dir="${dir%/*}"
  10. continue
  11. fi
  12. return
  13. done
  14. }
  15. find_run_sh() {
  16. local dir="${1:-$PWD}"
  17. local try_repo=true
  18. local ws=
  19. while true; do
  20. [ "$PWD" = "$dir" ] || cd "$dir"
  21. # find root of repo workspace
  22. if $try_repo; then
  23. ws="$(brute_find -d .repo "$PWD")"
  24. [ -d "$ws" ] || try_repo=false
  25. fi
  26. # or find root of .git repository
  27. if [ ! -d "$ws" ]; then
  28. ws="$(git rev-parse --show-superproject-working-tree 2> /dev/null || true)"
  29. [ -d "$ws" ] || ws="$(git rev-parse --show-toplevel 2> /dev/null || true)"
  30. fi
  31. if [ ! -d "$ws" ]; then
  32. # no repo? find anything then
  33. dir="$(brute_find -x run.sh "$PWD")"
  34. [ -z "$dir" ] || echo "$dir/run.sh"
  35. elif [ -x "$ws/run.sh" ]; then
  36. # found at repo's root
  37. echo "$ws/run.sh"
  38. elif [ / != "${ws:-/}" ]; then
  39. # check on the parent's then
  40. dir="${ws%/*}"
  41. ws=
  42. continue
  43. fi
  44. return
  45. done
  46. }
  47. run_sh="$(find_run_sh)"
  48. if test "x--root" = "x${1:-}"; then
  49. if [ -x "$run_sh" ]; then
  50. # found
  51. echo "$(dirname "$run_sh")"
  52. exit 0
  53. fi
  54. # failed to find workspace
  55. exit 1
  56. elif [ -x "$run_sh" ]; then
  57. exec "$run_sh" "$@"
  58. elif [ "$#" -gt 0 ]; then
  59. exec "$@"
  60. fi