gnodet commented on code in PR #1514:
URL: https://github.com/apache/maven-mvnd/pull/1514#discussion_r4793001464


##########
dist/src/main/distro/bin/mvnd-bash+zsh-completion.sh:
##########
@@ -0,0 +1,409 @@
+#
+# Copyright 2019 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# Adapted from 
https://github.com/juven/maven-bash-completion/blob/master/bash_completion.bash 
by Juven Xu and others
+# under Apache License Version 2.0
+
+# Detect shell type
+if [ -n "$ZSH_VERSION" ]; then
+    __MVND_SHELL="zsh"
+elif [ -n "$BASH_VERSION" ]; then
+    __MVND_SHELL="bash"
+else
+    __MVND_SHELL="unknown"
+fi
+
+# Check if the current shell is zsh
+__is_zsh()
+{
+    [ "$__MVND_SHELL" = "zsh" ]
+}
+
+# ZSH-specific setup
+if __is_zsh; then
+    # Enable bash compatibility
+    autoload -Uz compinit
+    compinit -u 2>/dev/null
+
+    # Load bashcompinit for bash-style completions
+    autoload -Uz bashcompinit
+    bashcompinit
+
+    # Set options for better compatibility
+    setopt NO_BEEP
+    setopt COMPLETE_IN_WORD
+    setopt ALWAYS_TO_END
+
+    # Define COMP_WORDBREAKS if not set (bash compatibility)
+    [[ -z "$COMP_WORDBREAKS" ]] && COMP_WORDBREAKS=$' \t\n"\'><=;|&(:'
+fi
+
+function_exists()
+{
+    if __is_zsh; then
+        # zsh uses 'typeset -f' or 'whence -f' to check function existence
+        typeset -f $1 > /dev/null 2>&1
+        return $?
+    else
+        declare -F $1 > /dev/null
+        return $?
+    fi
+}
+
+# This function can be used to access a tokenized list of words
+# on the command line:
+#
+#   __git_reassemble_comp_words_by_ref '=:'
+#   if test "${words_[cword_-1]}" = -w
+#   then
+#       ...
+#   fi
+#
+# The argument should be a collection of characters from the list of
+# word completion separators (COMP_WORDBREAKS) to treat as ordinary
+# characters.
+#
+# This is roughly equivalent to going back in time and setting
+# COMP_WORDBREAKS to exclude those characters.  The intent is to
+# make option types like --date=<type> and <rev>:<path> easy to
+# recognize by treating each shell word as a single token.
+#
+# It is best not to set COMP_WORDBREAKS directly because the value is
+# shared with other completion scripts.  By the time the completion
+# function gets called, COMP_WORDS has already been populated so local
+# changes to COMP_WORDBREAKS have no effect.
+#
+# Output: words_, cword_, cur_.
+function_exists __git_reassemble_comp_words_by_ref ||
+__git_reassemble_comp_words_by_ref()
+{
+    local exclude i j first
+    # Which word separators to exclude?
+    exclude="${1//[^$COMP_WORDBREAKS]}"
+    cword_=$COMP_CWORD
+    if [ -z "$exclude" ]; then
+        words_=("${COMP_WORDS[@]}")
+        return
+    fi
+    # List of word completion separators has shrunk;
+    # re-assemble words to complete.
+    for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
+        # Append each nonempty word consisting of just
+        # word separator characters to the current word.
+        first=t
+        while
+            [ $i -gt 0 ] &&
+            [ -n "${COMP_WORDS[$i]}" ] &&
+            # word consists of excluded word separators
+            [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
+        do
+            # Attach to the previous token,
+            # unless the previous token is the command name.
+            if [ $j -ge 2 ] && [ -n "$first" ]; then
+                ((j--))
+            fi
+            first=
+                words_[$j]=${words_[j]}${COMP_WORDS[i]}
+            if [ $i = $COMP_CWORD ]; then
+                cword_=$j
+            fi
+            if (($i < ${#COMP_WORDS[@]} - 1)); then
+                ((i++))
+            else
+                # Done.
+                return
+            fi
+        done
+        words_[$j]=${words_[j]}${COMP_WORDS[i]}
+        if [ $i = $COMP_CWORD ]; then
+            cword_=$j
+        fi
+    done
+}
+
+function_exists _get_comp_words_by_ref ||
+_get_comp_words_by_ref ()
+{
+    local exclude cur_ words_ cword_;
+    if [ "$1" = "-n" ]; then
+        exclude=$2;
+        shift 2;
+    fi;
+    __git_reassemble_comp_words_by_ref "$exclude";
+    cur_=${words_[cword_]};
+    while [ $# -gt 0 ]; do
+        case "$1" in
+            cur)
+                cur=$cur_
+            ;;
+            prev)
+                prev=${words_[$cword_-1]}
+            ;;
+            words)
+                words=("${words_[@]}")
+            ;;
+            cword)
+                cword=$cword_
+            ;;
+        esac;
+        shift;
+    done
+}
+
+function_exists __ltrim_colon_completions ||
+__ltrim_colon_completions()
+{
+    if [[ "$1" == *:* && "$COMP_WORDBREAKS" == *:* ]]; then
+        # Remove colon-word prefix from COMPREPLY items
+        local colon_word=${1%${1##*:}}
+        local i=${#COMPREPLY[*]}
+        while [[ $((--i)) -ge 0 ]]; do
+            COMPREPLY[$i]=${COMPREPLY[$i]#"$colon_word"}
+        done
+    fi
+}
+
+function_exists __find_mvn_projects ||
+__find_mvn_projects()
+{
+    find . -name 'pom.xml' -not -path '*/target/*' -prune | while read LINE ; 
do
+        local withoutPom=${LINE%/pom.xml}
+        local module=${withoutPom#./}
+        if [[ -z ${module} ]]; then
+            echo "."
+        else

Review Comment:
   **Important:** Unquoted variable expansion in echo. If module name contains 
spaces or special characters, word splitting occurs.
   
   **Failure scenario:** Module directory named 'my module' (with space) → echo 
${module} splits into two words → __find_mvn_projects outputs 'my' and 'module' 
as separate items → completion offers wrong modules
   
   **Fix:** Change 'echo ${module}' to 'echo "${module}"'



##########
dist/src/main/distro/bin/mvnd-bash+zsh-completion.sh:
##########
@@ -0,0 +1,409 @@
+#
+# Copyright 2019 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# Adapted from 
https://github.com/juven/maven-bash-completion/blob/master/bash_completion.bash 
by Juven Xu and others
+# under Apache License Version 2.0
+
+# Detect shell type
+if [ -n "$ZSH_VERSION" ]; then
+    __MVND_SHELL="zsh"
+elif [ -n "$BASH_VERSION" ]; then
+    __MVND_SHELL="bash"
+else
+    __MVND_SHELL="unknown"
+fi
+
+# Check if the current shell is zsh
+__is_zsh()
+{
+    [ "$__MVND_SHELL" = "zsh" ]
+}
+
+# ZSH-specific setup
+if __is_zsh; then
+    # Enable bash compatibility
+    autoload -Uz compinit
+    compinit -u 2>/dev/null
+
+    # Load bashcompinit for bash-style completions
+    autoload -Uz bashcompinit
+    bashcompinit
+
+    # Set options for better compatibility
+    setopt NO_BEEP
+    setopt COMPLETE_IN_WORD
+    setopt ALWAYS_TO_END
+
+    # Define COMP_WORDBREAKS if not set (bash compatibility)
+    [[ -z "$COMP_WORDBREAKS" ]] && COMP_WORDBREAKS=$' \t\n"\'><=;|&(:'
+fi
+
+function_exists()
+{
+    if __is_zsh; then
+        # zsh uses 'typeset -f' or 'whence -f' to check function existence
+        typeset -f $1 > /dev/null 2>&1
+        return $?
+    else
+        declare -F $1 > /dev/null
+        return $?
+    fi
+}
+
+# This function can be used to access a tokenized list of words
+# on the command line:
+#
+#   __git_reassemble_comp_words_by_ref '=:'
+#   if test "${words_[cword_-1]}" = -w
+#   then
+#       ...
+#   fi
+#
+# The argument should be a collection of characters from the list of
+# word completion separators (COMP_WORDBREAKS) to treat as ordinary
+# characters.
+#
+# This is roughly equivalent to going back in time and setting
+# COMP_WORDBREAKS to exclude those characters.  The intent is to
+# make option types like --date=<type> and <rev>:<path> easy to
+# recognize by treating each shell word as a single token.
+#
+# It is best not to set COMP_WORDBREAKS directly because the value is
+# shared with other completion scripts.  By the time the completion
+# function gets called, COMP_WORDS has already been populated so local
+# changes to COMP_WORDBREAKS have no effect.
+#
+# Output: words_, cword_, cur_.
+function_exists __git_reassemble_comp_words_by_ref ||
+__git_reassemble_comp_words_by_ref()
+{
+    local exclude i j first
+    # Which word separators to exclude?
+    exclude="${1//[^$COMP_WORDBREAKS]}"
+    cword_=$COMP_CWORD
+    if [ -z "$exclude" ]; then
+        words_=("${COMP_WORDS[@]}")
+        return
+    fi
+    # List of word completion separators has shrunk;
+    # re-assemble words to complete.
+    for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
+        # Append each nonempty word consisting of just
+        # word separator characters to the current word.
+        first=t
+        while
+            [ $i -gt 0 ] &&
+            [ -n "${COMP_WORDS[$i]}" ] &&
+            # word consists of excluded word separators
+            [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
+        do
+            # Attach to the previous token,
+            # unless the previous token is the command name.
+            if [ $j -ge 2 ] && [ -n "$first" ]; then
+                ((j--))
+            fi
+            first=
+                words_[$j]=${words_[j]}${COMP_WORDS[i]}
+            if [ $i = $COMP_CWORD ]; then
+                cword_=$j
+            fi
+            if (($i < ${#COMP_WORDS[@]} - 1)); then
+                ((i++))
+            else
+                # Done.
+                return
+            fi
+        done
+        words_[$j]=${words_[j]}${COMP_WORDS[i]}
+        if [ $i = $COMP_CWORD ]; then
+            cword_=$j
+        fi
+    done
+}
+
+function_exists _get_comp_words_by_ref ||
+_get_comp_words_by_ref ()
+{
+    local exclude cur_ words_ cword_;
+    if [ "$1" = "-n" ]; then
+        exclude=$2;
+        shift 2;
+    fi;
+    __git_reassemble_comp_words_by_ref "$exclude";
+    cur_=${words_[cword_]};
+    while [ $# -gt 0 ]; do
+        case "$1" in
+            cur)
+                cur=$cur_
+            ;;
+            prev)
+                prev=${words_[$cword_-1]}
+            ;;
+            words)
+                words=("${words_[@]}")
+            ;;
+            cword)
+                cword=$cword_
+            ;;
+        esac;
+        shift;
+    done
+}
+
+function_exists __ltrim_colon_completions ||
+__ltrim_colon_completions()
+{
+    if [[ "$1" == *:* && "$COMP_WORDBREAKS" == *:* ]]; then
+        # Remove colon-word prefix from COMPREPLY items
+        local colon_word=${1%${1##*:}}
+        local i=${#COMPREPLY[*]}
+        while [[ $((--i)) -ge 0 ]]; do
+            COMPREPLY[$i]=${COMPREPLY[$i]#"$colon_word"}
+        done
+    fi
+}
+
+function_exists __find_mvn_projects ||
+__find_mvn_projects()
+{
+    find . -name 'pom.xml' -not -path '*/target/*' -prune | while read LINE ; 
do
+        local withoutPom=${LINE%/pom.xml}
+        local module=${withoutPom#./}
+        if [[ -z ${module} ]]; then
+            echo "."
+        else
+            echo ${module}
+        fi
+    done
+}
+
+function_exists _realpath ||
+_realpath ()
+{
+    if [[ -f "$1" ]]

Review Comment:
   **Important:** Unnecessary echo command in cd path expansion. Using echo for 
string substitution is redundant and can fail with certain filenames (e.g., 
starting with dash).
   
   **Failure scenario:** File path is '-e /foo/bar.xml' → echo -e interprets 
flags → cd gets wrong path → _realpath fails → completion functions that need 
realpath break
   
   **Fix:** Change 'cd "$(echo "${1%/*}")"' to 'cd "${1%/*}"'



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to