From fb3e0f2ac105621058e717e92005d5a2e246fc18 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Sat, 18 Nov 2023 22:11:49 +0100 Subject: [PATCH] Improve git subcommand detection (#4121) * refactor: improve git subcommand detection * fix: remove redundant case --- projects/git-scm.org/git-shim | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/projects/git-scm.org/git-shim b/projects/git-scm.org/git-shim index 9d16d537..aec29ead 100755 --- a/projects/git-scm.org/git-shim +++ b/projects/git-scm.org/git-shim @@ -2,26 +2,40 @@ 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 - if test "$SKIP" = 1; then - SKIP=0 + # If the skip_next flag is set, skip this argument. + if [ "$skip_next" -eq 1 ]; then + skip_next=0 continue fi + + # Check for options that require skipping the next argument. case $arg in - -C) SKIP=1; continue;; - -*);; - *) - echo "$arg" - return # found the command! + -C | --git-dir | --work-tree | --namespace | --super-prefix | --config-env | -c) + skip_next=1 # The next argument is a value for these options. + continue + ;; + -*) + continue # Skip other options that start with '-'. + ;; esac + + # If we reach this point, it's likely the git subcommand. + echo "$arg" + return done } libexec="$(cd "$(dirname "$0")/.." && pwd)"/libexec # extract the git subcommand -cmd=$(find_git_command "$@") +cmd=$(find_git_subcommand "$@") _provides() { if foo="$(pkgx --provider git-"$cmd")"; then