[PATCH] Bash completion for extensions

Sándor Semsey semseysandor at gmail.com
Fri Jun 14 08:26:33 UTC 2024


Bash completion for extensions doesn't work.
Basically `pass.bash-completion` tries to call the extension's
completion function, but it's never sourced, so the function is not
defined, therefore not called.

There were a few attempts to fix this bug, but none of them got merged:

https://lists.zx2c4.com/pipermail/password-store/2019-January/003556.html
https://lists.zx2c4.com/pipermail/password-store/2019-February/003563.html
https://lists.zx2c4.com/pipermail/password-store/2019-April/003639.html
https://lists.zx2c4.com/pipermail/password-store/2022-December/004681.html

So, I'm submitting a new patch. It's simple and uses
`_completion_loader` from Bash to auto-load completion functions
(concept borrowed from git). Also I documented the (currently
undocumented) PASSWORD_STORE_EXTENSION_COMMANDS environment variable.


>From 9c17e2687b03bbf79359a7181c9639954a4d375b Mon Sep 17 00:00:00 2001
From: Sandor Semsey <sandor at es-progress.hu>
Date: Sun, 9 Jun 2024 14:43:56 +0200
Subject: [PATCH 1/2] bash completion: load extension completions dynamically


diff --git a/src/completion/pass.bash-completion
b/src/completion/pass.bash-completion
index 2d23cbf..443b17c 100644
--- a/src/completion/pass.bash-completion
+++ b/src/completion/pass.bash-completion
@@ -87,7 +87,8 @@ _pass()
    local commands="init ls find grep show insert generate edit rm mv
cp git help version ${PASSWORD_STORE_EXTENSION_COMMANDS[*]}"
    if [[ $COMP_CWORD -gt 1 ]]; then
        local lastarg="${COMP_WORDS[$COMP_CWORD-1]}"
-       case "${COMP_WORDS[1]}" in
+       local command="${COMP_WORDS[1]}"
+       case "${command}" in
            init)
                if [[ $lastarg == "-p" || $lastarg == "--path" ]]; then
                    _pass_complete_folders
@@ -123,18 +124,25 @@ _pass()
            git)
                COMPREPLY+=($(compgen -W "init push pull config log
reflog rebase" -- ${cur}))
                ;;
+           *)
+               # To add completion for an extension command define a
function like this:
+               # __password_store_extension_complete_<COMMAND>() {
+               #     COMPREPLY+=($(compgen -W "-o --option" -- ${cur}))
+               #     _pass_complete_entries 1
+               # }
+               #
+               # and add the command to the
$PASSWORD_STORE_EXTENSION_COMMANDS array
+               local
extension_completion_func="__password_store_extension_complete_${command}"
+               if [[ " ${PASSWORD_STORE_EXTENSION_COMMANDS[*]} " ==
*" ${command} "* ]]; then
+                   if ! type "${extension_completion_func}" &>
/dev/null && type _completion_loader &> /dev/null; then
+                       _completion_loader "pass-${command}"
+                   fi
+                   if type "${extension_completion_func}" &> /dev/null; then
+                       "${extension_completion_func}"
+                   fi
+               fi
+               ;;
        esac
-
-       # To add completion for an extension command define a function
like this:
-       # __password_store_extension_complete_<COMMAND>() {
-       #     COMPREPLY+=($(compgen -W "-o --option" -- ${cur}))
-       #     _pass_complete_entries 1
-       # }
-       #
-       # and add the command to the $PASSWORD_STORE_EXTENSION_COMMANDS array
-       if [[ " ${PASSWORD_STORE_EXTENSION_COMMANDS[*]} " == *"
${COMP_WORDS[1]} "* ]] && type
"__password_store_extension_complete_${COMP_WORDS[1]}" &> /dev/null;
then
-           "__password_store_extension_complete_${COMP_WORDS[1]}"
-       fi
    else
        COMPREPLY+=($(compgen -W "${commands}" -- ${cur}))
        _pass_complete_entries 1

>From a98cbd4ca2793be4f87abd2ec3f35fc2d669cf06 Mon Sep 17 00:00:00 2001
From: Sandor Semsey <sandor at es-progress.hu>
Date: Wed, 12 Jun 2024 03:42:47 +0200
Subject: [PATCH 2/2] man: document PASSWORD_STORE_EXTENSION_COMMANDS


diff --git a/man/pass.1 b/man/pass.1
index a555dcb..19a1dba 100644
--- a/man/pass.1
+++ b/man/pass.1
@@ -456,6 +456,9 @@ This environment variable must be set to "true"
for extensions to be enabled.
 The location to look for executable extension files, by default
 \fIPASSWORD_STORE_DIR/.extensions\fP.
 .TP
+.I PASSWORD_STORE_EXTENSION_COMMANDS
+Array to hold commands added by extensions.
+.TP
 .I PASSWORD_STORE_SIGNING_KEY
 If this environment variable is set, then all \fB.gpg-id\fP files and
non-system extension files
 must be signed using a detached signature using the GPG key specified
by the full 40 character


More information about the Password-Store mailing list