bash tab-completion:在 git 命令中的单个选项卡上完成完整路径?

bash tab-completion:在 git 命令中的单个选项卡上完成完整路径?

每当我在 bash 中的 git 命令之后用 Tab 键完成文件路径时,例如git add,我必须按<TAB>多次才能完成实际的文件名,具体取决于文件有多少个子文件夹。

示例:我想添加文件my/example/file,然后输入:

git add <TAB>
git add my/<TAB>
git add my/example/<TAB>
git add my/example/file

my/example/file即使是唯一更改的文件,因此也是当前唯一的制表符完成选项,情况也是如此。在这种情况下,bash 也可以用第一个路径完成完整路径<TAB>,但事实并非如此。

实际上默认情况下是这样做的:

git add <TAB>
git add my/example/file

有没有办法在 bash 中配置此行为?

我在 Ubuntu 20.04 上使用其默认的 bash 补全。

答案1

实现此目的的一种方法是将 git 完成脚本复制到 , /usr/share/bash-completion/completions/git~/.local/share/bash-completion/completions/修改它以使用完整索引路径完成:

mkdir -p ~/.local/share/bash-completion/completions
cd ~/.local/share/bash-completion/completions

cp /usr/share/bash-completion/completions/git .

# See diff below
patch -lp0 </tmp/index_paths.diff
echo GIT_COMPLETION_FULL_INDEX_PATHS=1 >>~/.bashrc

exec bash

据我所知,所需的更改是:

  1. 进行修改__git_index_files(),使其输出完整的索引路径,而不仅仅是第一个路径组件。

  2. 在 中__git_complete_index_file(),停止使用__gitcomp_file_directset COMPREPLY因为它使用compopt -o filenames,它仅输出基本名称。此选项还负责 shell 引用,因此现在手动执行此操作。

请注意,__git_complete_index_file用于完成除 、 、 和 之外的其他几个 git 命令addclean因此commit完整rm路径完成也适用于这些命令。以下是我尝试进行这些更改的差异,它添加了 shell 变量背后的功能 GIT_COMPLETION_FULL_INDEX_PATHS

--- git
+++ git
@@ -39,6 +39,11 @@
 #     When set to "1", do not include "DWIM" suggestions in git-checkout
 #     and git-switch completion (e.g., completing "foo" when "origin/foo"
 #     exists).
+#
+#   GIT_COMPLETION_FULL_INDEX_PATHS
+#
+#     Normally index path completions return only the next path component. When
+#     set to "1", the whole path will be completed.
 
 case "$COMP_WORDBREAKS" in
 *:*) : great ;;
@@ -435,6 +440,19 @@
    __gitcomp_nl_append "$@"
 }
 
+# Shell quotes each word and fills the COMPREPLY array.
+# 1: List of newline-separated completion words.
+__gitcomp_quote_direct ()
+{
+   local IFS=$'\n'
+   local quoted="$1"
+   [[ -n $1 ]] && quoted=$(printf '%q\n' $1)
+
+   COMPREPLY=($quoted)
+
+   compopt +o nospace 2>/dev/null || true
+}
+
 # Fills the COMPREPLY array with prefiltered paths without any additional
 # processing.
 # Callers must take care of providing only paths that match the current path
@@ -503,10 +521,12 @@
 __git_index_files ()
 {
    local root="$2" match="$3"
+   local field=1
+   [ "$GIT_COMPLETION_FULL_INDEX_PATHS" = "1" ] && field=0
 
    __git_ls_files_helper "$root" "$1" "$match" |
    awk -F / -v pfx="${2//\\/\\\\}" '{
-       paths[$1] = 1
+       paths[$f] = 1
    }
    END {
        for (p in paths) {
@@ -518,19 +538,13 @@
 
            # The path is quoted.
            p = dequote(p)
-           if (p == "")
-               continue
 
-           # Even when a directory name itself does not contain
-           # any special characters, it will still be quoted if
-           # any of its (stripped) trailing path components do.
-           # Because of this we may have seen the same directory
-           # both quoted and unquoted.
-           if (p in paths)
-               # We have seen the same directory unquoted,
-               # skip it.
-               continue
-           else
+           # When not using full index paths, p in paths is checked
+           # because the dequoted directory name may already be in
+           # paths. This is the case when the directory name itself
+           # does not contain special characters, but a (stripped)
+           # trailing path component does.
+           if (p != "" && (f == 0 || !(p in paths)))
                print pfx p
        }
    }
@@ -573,7 +587,7 @@
            out = out p
 
        return out
-   }'
+   }' "f=$field"
 }
 
 # __git_complete_index_file requires 1 argument:
@@ -595,7 +609,11 @@
        cur_="$dequoted_word"
    esac
 
-   __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")"
+   if [ "$GIT_COMPLETION_FULL_INDEX_PATHS" = "1" ]; then
+       __gitcomp_quote_direct "$(__git_index_files "$1" "$pfx" "$cur_")"
+   else
+       __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")"
+   fi
 }
 
 # Lists branches from the local repository.

相关内容