Re: [PATCH 5/5] completion: fix completion of certain aliases

2 messages, 2 authors, 2016-06-15 · open the first message on its own page

Re: [PATCH 5/5] completion: fix completion of certain aliases

From: Junio C Hamano <hidden>
Date: 2016-06-15 23:00:39

Felipe Contreras [off-list ref] writes:
Some commands need the first word to determine the actual action that is
being executed, however, the command is wrong when we use an alias, for
example 'alias.p=push', if we try to complete 'git p origin ', the
result would be wrong because __git_complete_remote_or_refspec() doesn't
know where it come from.

So let's override words[1], so the alias 'p' is override by the actual
command, 'push'.

Reported-by: Aymeric Beaumet <redacted>
Signed-off-by: Felipe Contreras <redacted>
---
Does "some commands" above refer to anything that uses
__git_complete_remote_or_refspec, or is the set of commands larger
than that?

I am wondering if it is safer to introduce a new "local" variable
that is set by the caller of __git_complete_remote_or_refspec and
inspected by __git_complete_remote_or_refspec (instead of words[1])
to communiate the real name of the git subcommand being completed,
without touching words[] in place.

That way, we wouldn't have to worry about all the other references
of words[c], words[i], words[CURRENT] etc. in the script seeing the
word that the end-user did not type and did not actually appear on
the command line.

But perhaps we muck with the contents of words[] in a similar way in
many different places in the existing completion code often enough
that such an attempt not to touch the words[] array does not buy us
much safety anyway.  I didn't check (and that is why I am asking
with "I am wondering...").

Thanks, will queue.

[Ram and Szeder CC'ed as they appear in shortlog for the past 12
months].
quoted hunk
 contrib/completion/git-completion.bash | 1 +
 contrib/completion/git-completion.zsh  | 1 +
 2 files changed, 2 insertions(+)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 9525343..893ae5d 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2547,6 +2547,7 @@ __git_main ()
 
 	local expansion=$(__git_aliased_command "$command")
 	if [ -n "$expansion" ]; then
+		words[1]=$expansion
 		completion_func="_git_${expansion//-/_}"
 		declare -f $completion_func >/dev/null && $completion_func
 	fi
diff --git a/contrib/completion/git-completion.zsh b/contrib/completion/git-completion.zsh
index 6b77968..9f6f0fa 100644
--- a/contrib/completion/git-completion.zsh
+++ b/contrib/completion/git-completion.zsh
@@ -104,6 +104,7 @@ __git_zsh_bash_func ()
 
 	local expansion=$(__git_aliased_command "$command")
 	if [ -n "$expansion" ]; then
+		words[1]=$expansion
 		completion_func="_git_${expansion//-/_}"
 		declare -f $completion_func >/dev/null && $completion_func
 	fi

Re: [PATCH 5/5] completion: fix completion of certain aliases

From: Felipe Contreras <hidden>
Date: 2016-06-15 23:00:39

Junio C Hamano wrote:
Felipe Contreras [off-list ref] writes:
quoted
Some commands need the first word to determine the actual action that is
being executed, however, the command is wrong when we use an alias, for
example 'alias.p=push', if we try to complete 'git p origin ', the
result would be wrong because __git_complete_remote_or_refspec() doesn't
know where it come from.

So let's override words[1], so the alias 'p' is override by the actual
command, 'push'.

Reported-by: Aymeric Beaumet <redacted>
Signed-off-by: Felipe Contreras <redacted>
---
Does "some commands" above refer to anything that uses
__git_complete_remote_or_refspec, or is the set of commands larger than that?
For this particular issue, yes, the former.
But perhaps we muck with the contents of words[] in a similar way in many
different places in the existing completion code often enough that such an
attempt not to touch the words[] array does not buy us much safety anyway.  I
didn't check (and that is why I am asking with "I am wondering...").
The 'words' array is already messed up and not used correctly, so I wouldn't
worry too much about this patch messing it more (I don't see how that can be).

For example:
  % git --git-dir=$PWD/.git fetch or<tab>

-- 
Felipe Contreras
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help