{"thread":{"id":"31220","subject":"[PATCH] git bash completion handles alias options","startedAt":"2012-08-09T20:52:23Z","lastAt":"2012-08-09T20:52:24Z","messageCount":2,"participants":["Jerome Reybert"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"196755","messageId":"1344545544-8538-1-git-send-email-jreybert@gmail.com","threadId":"31220","inReplyTo":null,"subject":"[PATCH] git bash completion handles alias options","fromName":"Jerome Reybert","fromEmail":"jreybert@gmail.com","sentAt":"2012-08-09T20:52:23Z","receivedAt":"2012-08-09T20:52:23Z","isPatch":true,"sender":{"key":"jreybert@gmail.com","avatar":null},"body":"git bash completion now handles git alias options. It means that options which\nchange the completion behavior for complete commands will also change the\nbehavior of completion for aliased command.  \n\nbash completion now handle git alias options. I did a use case in another\ncommit: a checkout alias which only complete local branches, ie. only 'refs'\nones, not 'remotes'. With this new alias completion, I added an option to do\nthat in checkout command and hacked _git_checkout complete function.\n\nBest,\n  Jerome\n\nJerome Reybert (1):\n  git bash completion handles alias options\n\n contrib/completion/git-completion.bash | 31 +++++++++++++++++++++++++------\n 1 file changed, 25 insertions(+), 6 deletions(-)\n\n-- \n1.7.12.rc0.91.gf4edd99\n"},{"id":"196756","messageId":"1344545544-8538-2-git-send-email-jreybert@gmail.com","threadId":"31220","inReplyTo":"1344545544-8538-1-git-send-email-jreybert@gmail.com","subject":"[PATCH] git bash completion handles alias options","fromName":"Jerome Reybert","fromEmail":"jreybert@gmail.com","sentAt":"2012-08-09T20:52:24Z","receivedAt":"2012-08-09T20:52:24Z","isPatch":true,"sender":{"key":"jreybert@gmail.com","avatar":null},"body":"git bash completion now handles git alias options. It means that options\nwhich change the completion behavior for complete commands will also\nchange the behavior of completion for aliased command.\n\nbash completion now handle git alias options. I did a use case in another\ncommit: a checkout alias which only complete local branches, ie. only\n'refs' ones, not 'remotes'. With this new alias completion, I added an\noption to do that in checkout command and hacked _git_checkout complete\nfunction.\n\nThis is a draft version, bash usage should be improved. To achieve this:\n * a global array $alias_words is filled. As it is global and this file\n   sourced, this variable can be seen from the terminal\n * __git_aliased_command fills $alias_words\n * __git_aliased_command must be called differently (not in a subshell)\n   to fill the global variable. The return value $expansion is\n   passed by reference as a parameter\n * __git_find_on_cmdline now search in $words and $alias_words\n\nSigned-off-by: Jerome Reybert <jreybert@gmail.com>\n---\n contrib/completion/git-completion.bash | 31 +++++++++++++++++++++++++------\n 1 file changed, 25 insertions(+), 6 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex ffedce7..dfceda0 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -23,6 +23,9 @@\n #    3) Consider changing your PS1 to also show the current branch,\n #       see git-prompt.sh for details.\n \n+alias_words=()\n+alias_cword=0\n+\n if [[ -n ${ZSH_VERSION-} ]]; then\n \tautoload -U +X bashcompinit && bashcompinit\n fi\n@@ -708,19 +711,22 @@ __git_aliased_command ()\n {\n \tlocal word cmdline=$(git --git-dir=\"$(__gitdir)\" \\\n \t\tconfig --get \"alias.$1\")\n+\n \tfor word in $cmdline; do\n \t\tcase \"$word\" in\n \t\t\\!gitk|gitk)\n-\t\t\techo \"gitk\"\n+\t\t\teval \"$2='gitk'\"\n \t\t\treturn\n \t\t\t;;\n \t\t\\!*)\t: shell command alias ;;\n-\t\t-*)\t: option ;;\n+\t\t-*)\t: option\n+\t\t\talias_words=(\"${alias_words[@]}\" \"$word\")\n+\t\t\t((alias_cword++))\n+\t\t\t;;\n \t\t*=*)\t: setting env ;;\n \t\tgit)\t: git itself ;;\n \t\t*)\n-\t\t\techo \"$word\"\n-\t\t\treturn\n+\t\t\teval \"$2='$word'\"\n \t\tesac\n \tdone\n }\n@@ -739,6 +745,17 @@ __git_find_on_cmdline ()\n \t\tdone\n \t\t((c++))\n \tdone\n+\tc=0\n+\twhile [ $c -lt $alias_cword ]; do\n+\t\tword=\"${alias_words[c]}\"\n+\t\tfor subcommand in $1; do\n+\t\t\tif [ \"$subcommand\" = \"$word\" ]; then\n+\t\t\t\techo \"$subcommand\"\n+\t\t\t\treturn\n+\t\t\tfi\n+\t\tdone\n+\t\t((c++))\n+\tdone\n }\n \n __git_has_doubledash ()\n@@ -2353,6 +2370,8 @@ _git_whatchanged ()\n \n __git_main ()\n {\n+\talias_words=()\n+\talias_cword=0\n \tlocal i c=1 command __git_dir\n \n \twhile [ $c -lt $cword ]; do\n@@ -2394,8 +2413,8 @@ __git_main ()\n \n \tlocal completion_func=\"_git_${command//-/_}\"\n \tdeclare -f $completion_func >/dev/null && $completion_func && return\n-\n-\tlocal expansion=$(__git_aliased_command \"$command\")\n+\tlocal expansion=''\n+\t__git_aliased_command $command expansion\n \tif [ -n \"$expansion\" ]; then\n \t\tcompletion_func=\"_git_${expansion//-/_}\"\n \t\tdeclare -f $completion_func >/dev/null && $completion_func\n-- \n1.7.12.rc0.91.gf4edd99\n"}]}