completion: handle '!f() { ... }; f' and "!sh -c '...' -" aliases
authorSteffen Prohaska <prohaska@zib.de>
Thu, 12 Jun 2014 18:49:29 +0000 (20:49 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 13 Jun 2014 20:37:43 +0000 (13:37 -0700)
'!f() { ... }; f' and "!sh -c '....' -" are recommended patterns for
declaring more complex aliases (see git wiki [1]). This commit teaches
the completion to handle them.

When determining which completion to use for an alias, an opening brace
or single quote is now skipped, and the search for a git command is
continued. For example, the aliases '!f() { git commit ... }' or "!sh
-c 'git commit ...'" now trigger commit completion. Previously, the
search stopped on the opening brace or quote, and the completion tried
it to determine how to complete, which obviously was useless.

The null command ':' is now skipped, so that it can be used as
a workaround to declare the desired completion style.

For example, the aliases

!f() { : git commit ; if ... } f
!sh -c ': git commit; if ...' -

now trigger commit completion.

Shell function declarations now work with or without space before
the parens, i.e. '!f() ...' and '!f () ...' both work.

[1] https://git.wiki.kernel.org/index.php/Aliases

Signed-off-by: Steffen Prohaska <prohaska@zib.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
contrib/completion/git-completion.bash
t/t9902-completion.sh
index 2c59a76bc2cf2d0fefb633efe805b6c7c15ad7ec..575f8f7a3fc08eeba1dde0b762ee105de05b031d 100644 (file)
 #        source ~/.git-completion.sh
 #    3) Consider changing your PS1 to also show the current branch,
 #       see git-prompt.sh for details.
+#
+# If you use complex aliases of form '!f() { ... }; f', you can use the null
+# command ':' as the first command in the function body to declare the desired
+# completion style.  For example '!f() { : git commit ; ... }; f' will
+# tell the completion to use commit completion.  This also works with aliases
+# of form "!sh -c '...'".  For example, "!sh -c ': git commit ; ... '".
 
 case "$COMP_WORDBREAKS" in
 *:*) : great ;;
@@ -781,6 +787,10 @@ __git_aliased_command ()
                -*)     : option ;;
                *=*)    : setting env ;;
                git)    : git itself ;;
+               \(\))   : skip parens of shell function definition ;;
+               {)      : skip start of shell helper function ;;
+               :)      : skip null command ;;
+               \'*)    : skip opening quote after sh -c ;;
                *)
                        echo "$word"
                        return
index 2d4beb5e50c257fed2f4b6475cb5834425b5a64f..1d1c1063a38866f1792f9b451a4942d06c91edb4 100755 (executable)
@@ -550,6 +550,33 @@ test_expect_success 'complete files' '
        test_completion "git add mom" "momified"
 '
 
+test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
+       test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
+       test_completion "git co m" <<-\EOF
+       master Z
+       mybranch Z
+       mytag Z
+       EOF
+'
+
+test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
+       test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
+       test_completion "git co m" <<-\EOF
+       master Z
+       mybranch Z
+       mytag Z
+       EOF
+'
+
+test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
+       test_config alias.co "!f() { : git checkout ; if ... } f" &&
+       test_completion "git co m" <<-\EOF
+       master Z
+       mybranch Z
+       mytag Z
+       EOF
+'
+
 test_expect_failure 'complete with tilde expansion' '
        git init tmp && cd tmp &&
        test_when_finished "cd .. && rm -rf tmp" &&