{"thread":{"id":"48377","subject":"[PATCH v3 0/1] completion: dynamic completion loading","startedAt":"2018-04-29T16:42:53Z","lastAt":"2018-05-07T10:39:08Z","messageCount":3,"participants":["Florian Gamböck","SZEDER Gábor"],"isPatch":true,"patchVersion":3,"patchTotal":1},"messages":[{"id":"346079","messageId":"20180429164232.29337-1-mail@floga.de","threadId":"48377","inReplyTo":null,"subject":"[PATCH v3 0/1] completion: dynamic completion loading","fromName":"Florian Gamböck","fromEmail":"mail@floga.de","sentAt":"2018-04-29T16:42:39Z","receivedAt":"2018-04-29T16:42:53Z","isPatch":true,"sender":{"key":"mail@floga.de","avatar":"https://avatars.githubusercontent.com/u/633683?v=4"},"body":"In this small patch I want to introduce a way to dynamically load completion \nscripts for external subcommands.\n\nA few years ago, you would put a completion script (which defines a Bash \nfunction _git_foo for a custom git subcommand foo) into \n/etc/bash_completion.d, or save it somewhere in your $HOME and source it \nmanually in your .bashrc.\n\nStarting with bash-completion v2.0 (or, to be absolutely exact, the preview \nversions started at v1.90), completion scripts are not sourced at bash startup \nanymore. Rather, when typing in a command and trigger completion by pressing \nthe TAB key, the new bash-completion's main script looks for a script with the \nsame name as the typed command in the completion directory, sources it, and \nprovides the completions defined in this script.\n\nHowever, that only works for top level commands. After a completion script has \nbeen found, the logic is delegated to this script. This means, that the \ncompletion of subcommands must be handled by the corresponding completion \nscript.\n\nAs it is now, git is perfectly able to call custom completion functions, iff \nthey are already defined when calling the git completion. With my patch, git \nuses an already defined loader function of bash-completion (or continues \nsilently if this function is not found), loads an external completion script, \nand provides its completions.\n\nAn example for an external completion script would be \n/usr/share/bash-completion/completions/git-foo for a git subcommand foo.\n\nChanges since v2:\n\n-   Replaced __load_completion with _completion_loader, which was introduced \n    way before the former and should therefor be available even in older \n    distributions\n\n-   Updated commit message with explanations from Szeder Gábor\n\nChanges since v1 (RFC):\n\n-   Re-arranged if-fi statement to increase readability (suggested by Junio C \n    Hamano)\n\n-   Re-worded commit message to include the exakt version of bashcomp that \n    introduced dynamic loading (suggested by Stefan Beller)\n\nFlorian Gamböck (1):\n  completion: load completion file for external subcommand\n\n contrib/completion/git-completion.bash | 10 ++++++++++\n 1 file changed, 10 insertions(+)\n\n-- \n2.16.1\n\n"},{"id":"346080","messageId":"20180429164232.29337-2-mail@floga.de","threadId":"48377","inReplyTo":"20180429164232.29337-1-mail@floga.de","subject":"[PATCH v3 1/1] completion: load completion file for external subcommand","fromName":"Florian Gamböck","fromEmail":"mail@floga.de","sentAt":"2018-04-29T16:42:43Z","receivedAt":"2018-04-29T16:42:59Z","isPatch":true,"sender":{"key":"mail@floga.de","avatar":"https://avatars.githubusercontent.com/u/633683?v=4"},"body":"Adding external subcommands to Git is as easy as to put an executable\nfile git-foo into PATH. Packaging such subcommands for a Linux\ndistribution can be achieved by unpacking the executable into /usr/bin\nof the user's system. Adding system-wide completion scripts for new\nsubcommands, however, can be a bit tricky.\n\nSince bash-completion started to use dynamical loading of completion\nscripts since v1.90 (preview of v2.0), it is no longer sufficient to\ndrop a completion script of a subcommand into the standard completions\npath, /usr/share/bash-completion/completions, since this script will not\nbe loaded if called as a git subcommand.\n\nFor example, look at https://bugs.gentoo.org/544722. To give a short\nsummary: The popular git-flow subcommand provides a completion script,\nwhich gets installed as /usr/share/bash-completion/completions/git-flow.\n\nIf you now type into a Bash shell:\n\n    git flow <TAB>\n\nYou will not get any completions, because bash-completion only loads\ncompletions for git and git has no idea that git-flow is defined in\nanother file. You have to load this script manually or trigger the\ndynamic loader with:\n\n    git-flow <TAB> # Please notice the dash instead of whitespace\n\nThis will not complete anything either, because it only defines a Bash\nfunction, without generating completions. But now the correct completion\nscript has been loaded and the first command can use the completions.\n\nSo, the goal is now to teach the git completion script to consider the\npossibility of external completion scripts for subcommands, but of\ncourse without breaking current workflows.\n\nI think the easiest method is to use a function that was defined by\nbash-completion v1.90, namely _completion_loader. It will take care of\nloading the correct script if present. Afterwards, the git completion\nscript behaves as usual.\n\n_completion_loader was introduced in commit 20c05b43 of bash-completion\n(https://github.com/scop/bash-completion.git) back in 2011, so it should\nbe available in even older LTS distributions. This function searches for\nexternal completion scripts not only in the default path\n/usr/share/bash-completion/completions, but also in the user's home\ndirectory via $XDG_DATA_HOME and in a user specified directory via\n$BASH_COMPLETION_USER_DIR.\n\nThe only \"drawback\" (if it even can be called as such) is, that if\n_completion_loader does not find a completion script, it automatically\nregisters a minimal function for basic path completion. In practice,\nhowever, this will not matter, because in this case the given command is\na git command in its dashed form, e.g. 'git-diff-index', and those have\nbeen deprecated for a long time.\n\nThis way we can leverage bash-completion's dynamic loading for git\nsubcommands and make it easier for developers to distribute custom\ncompletion scripts.\n\nSigned-off-by: Florian Gamböck <mail@floga.de>\n---\n contrib/completion/git-completion.bash | 10 ++++++++++\n 1 file changed, 10 insertions(+)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex b09c8a236..604ba2b03 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -3096,12 +3096,22 @@ __git_main ()\n \tfi\n \n \tlocal completion_func=\"_git_${command//-/_}\"\n+\tif ! declare -f $completion_func >/dev/null 2>/dev/null &&\n+\t\tdeclare -f _completion_loader >/dev/null 2>/dev/null\n+\tthen\n+\t\t_completion_loader \"git-$command\"\n+\tfi\n \tdeclare -f $completion_func >/dev/null 2>/dev/null && $completion_func && return\n \n \tlocal expansion=$(__git_aliased_command \"$command\")\n \tif [ -n \"$expansion\" ]; then\n \t\twords[1]=$expansion\n \t\tcompletion_func=\"_git_${expansion//-/_}\"\n+\t\tif ! declare -f $completion_func >/dev/null 2>/dev/null &&\n+\t\t\tdeclare -f _completion_loader >/dev/null 2>/dev/null\n+\t\tthen\n+\t\t\t_completion_loader \"git-$expansion\"\n+\t\tfi\n \t\tdeclare -f $completion_func >/dev/null 2>/dev/null && $completion_func\n \tfi\n }\n-- \n2.16.1\n\n"},{"id":"346861","messageId":"CAM0VKjkTu+OkLM3gvX73mWugxArCVmqRBmWGHiKuLiLRNkkNow@mail.gmail.com","threadId":"48377","inReplyTo":"20180429164232.29337-2-mail@floga.de","subject":"Re: [PATCH v3 1/1] completion: load completion file for external subcommand","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2018-05-07T10:39:04Z","receivedAt":"2018-05-07T10:39:08Z","isPatch":true,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"On Sun, Apr 29, 2018 at 6:42 PM, Florian Gamböck <mail@floga.de> wrote:\n> This way we can leverage bash-completion's dynamic loading for git\n> subcommands and make it easier for developers to distribute custom\n> completion scripts.\n\nThe patch and the updated commit message both look good to me.\nThanks for following through.\n"}]}