Improve git subcommand detection (#4121)

* refactor: improve git subcommand detection

* fix: remove redundant case
This commit is contained in:
Fredrik Averpil 2023-11-18 22:11:49 +01:00 committed by GitHub
parent 6bd1cee2f3
commit fb3e0f2ac1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,26 +2,40 @@
set -e set -e
find_git_command() { # This function searches through the provided arguments
# to find the first non-option argument, which is assumed
# to be the git subcommand.
find_git_subcommand() {
skip_next=0 # Flag to indicate if the next argument should be skipped.
for arg in "$@"; do for arg in "$@"; do
if test "$SKIP" = 1; then # If the skip_next flag is set, skip this argument.
SKIP=0 if [ "$skip_next" -eq 1 ]; then
skip_next=0
continue continue
fi fi
# Check for options that require skipping the next argument.
case $arg in case $arg in
-C) SKIP=1; continue;; -C | --git-dir | --work-tree | --namespace | --super-prefix | --config-env | -c)
-*);; skip_next=1 # The next argument is a value for these options.
*) continue
echo "$arg" ;;
return # found the command! -*)
continue # Skip other options that start with '-'.
;;
esac esac
# If we reach this point, it's likely the git subcommand.
echo "$arg"
return
done done
} }
libexec="$(cd "$(dirname "$0")/.." && pwd)"/libexec libexec="$(cd "$(dirname "$0")/.." && pwd)"/libexec
# extract the git subcommand # extract the git subcommand
cmd=$(find_git_command "$@") cmd=$(find_git_subcommand "$@")
_provides() { _provides() {
if foo="$(pkgx --provider git-"$cmd")"; then if foo="$(pkgx --provider git-"$cmd")"; then