{"thread":{"id":"43691","subject":"[PATCH 4/6] Take --git-dir into consideration during bash completion.","startedAt":"2006-11-05T11:21:57Z","lastAt":"2006-11-05T11:21:57Z","messageCount":1,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":6},"messages":[{"id":"296743","messageId":"20061105112157.GD20495@spearce.org","threadId":"43691","inReplyTo":null,"subject":"[PATCH 4/6] Take --git-dir into consideration during bash completion.","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2006-11-05T11:21:57Z","receivedAt":"2006-11-05T11:21:57Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"If the user has setup a command line of \"git --git-dir=baz\" then\nanything we complete must be performed within the scope of \"baz\"\nand not the current working directory.\n\nThis is useful with commands such as \"git --git-dir=git.git log m\"\nto complete out \"master\" and view the log for the master branch of\nthe git.git repository.  As a nice side effect this also works for\naliases within the target repository, just as git would honor them.\n\nUnfortunately because we still examine arguments by absolute position\nin most of the more complex commands (e.g. git push) using --git-dir\nwith those commands will probably still cause completion to fail.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n contrib/completion/git-completion.bash |  123 ++++++++++++++++++--------------\n 1 files changed, 70 insertions(+), 53 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 5f1be46..f258f2f 100755\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -19,15 +19,20 @@\n #        source ~/.git-completion.sh\n #\n \n+__gitdir ()\n+{\n+\techo \"${__git_dir:-$(git rev-parse --git-dir 2>/dev/null)}\"\n+}\n+\n __git_refs ()\n {\n-\tlocal cmd i is_hash=y\n-\tif [ -d \"$1\" ]; then\n+\tlocal cmd i is_hash=y dir=\"${1:-$(__gitdir)}\"\n+\tif [ -d \"$dir\" ]; then\n \t\tcmd=git-peek-remote\n \telse\n \t\tcmd=git-ls-remote\n \tfi\n-\tfor i in $($cmd \"$1\" 2>/dev/null); do\n+\tfor i in $($cmd \"$dir\" 2>/dev/null); do\n \t\tcase \"$is_hash,$i\" in\n \t\ty,*) is_hash=n ;;\n \t\tn,*^{}) is_hash=y ;;\n@@ -40,13 +45,13 @@ __git_refs ()\n \n __git_refs2 ()\n {\n-\tlocal cmd i is_hash=y\n-\tif [ -d \"$1\" ]; then\n+\tlocal cmd i is_hash=y dir=\"${1:-$(__gitdir)}\"\n+\tif [ -d \"$dir\" ]; then\n \t\tcmd=git-peek-remote\n \telse\n \t\tcmd=git-ls-remote\n \tfi\n-\tfor i in $($cmd \"$1\" 2>/dev/null); do\n+\tfor i in $($cmd \"$dir\" 2>/dev/null); do\n \t\tcase \"$is_hash,$i\" in\n \t\ty,*) is_hash=n ;;\n \t\tn,*^{}) is_hash=y ;;\n@@ -59,14 +64,14 @@ __git_refs2 ()\n \n __git_remotes ()\n {\n-\tlocal i ngoff IFS=$'\\n'\n+\tlocal i ngoff IFS=$'\\n' d=\"$(__gitdir)\"\n \tshopt -q nullglob || ngoff=1\n \tshopt -s nullglob\n-\tfor i in .git/remotes/*; do\n-\t\techo ${i#.git/remotes/}\n+\tfor i in \"$d/remotes\"/*; do\n+\t\techo ${i#$d/remotes/}\n \tdone\n \t[ \"$ngoff\" ] && shopt -u nullglob\n-\tfor i in $(git repo-config --list); do\n+\tfor i in $(git --git-dir=\"$d\" repo-config --list); do\n \t\tcase \"$i\" in\n \t\tremote.*.url=*)\n \t\t\ti=\"${i#remote.}\"\n@@ -95,7 +100,7 @@ __git_complete_file ()\n \t\t\t;;\n \t    esac\n \t\tCOMPREPLY=($(compgen -P \"$pfx\" \\\n-\t\t\t-W \"$(git-ls-tree \"$ls\" \\\n+\t\t\t-W \"$(git --git-dir=\"$(__gitdir)\" ls-tree \"$ls\" \\\n \t\t\t\t| sed '/^100... blob /s,^.*\t,,\n \t\t\t\t       /^040000 tree /{\n \t\t\t\t           s,^.*\t,,\n@@ -105,7 +110,7 @@ __git_complete_file ()\n \t\t\t-- \"$cur\"))\n \t\t;;\n \t*)\n-\t\tCOMPREPLY=($(compgen -W \"$(__git_refs .)\" -- \"$cur\"))\n+\t\tCOMPREPLY=($(compgen -W \"$(__git_refs)\" -- \"$cur\"))\n \t\t;;\n \tesac\n }\n@@ -113,7 +118,7 @@ __git_complete_file ()\n __git_aliases ()\n {\n \tlocal i IFS=$'\\n'\n-\tfor i in $(git repo-config --list); do\n+\tfor i in $(git --git-dir=\"$(__gitdir)\" repo-config --list); do\n \t\tcase \"$i\" in\n \t\talias.*)\n \t\t\ti=\"${i#alias.}\"\n@@ -125,7 +130,8 @@ __git_aliases ()\n \n __git_aliased_command ()\n {\n-\tlocal word cmdline=$(git repo-config --get \"alias.$1\")\n+\tlocal word cmdline=$(git --git-dir=\"$(__gitdir)\" \\\n+\t\trepo-config --get \"alias.$1\")\n \tfor word in $cmdline; do\n \t\tif [ \"${word##-*}\" ]; then\n \t\t\techo $word\n@@ -137,7 +143,7 @@ __git_aliased_command ()\n _git_branch ()\n {\n \tlocal cur=\"${COMP_WORDS[COMP_CWORD]}\"\n-\tCOMPREPLY=($(compgen -W \"-l -f -d -D $(__git_refs .)\" -- \"$cur\"))\n+\tCOMPREPLY=($(compgen -W \"-l -f -d -D $(__git_refs)\" -- \"$cur\"))\n }\n \n _git_cat_file ()\n@@ -159,7 +165,7 @@ _git_cat_file ()\n _git_checkout ()\n {\n \tlocal cur=\"${COMP_WORDS[COMP_CWORD]}\"\n-\tCOMPREPLY=($(compgen -W \"-l -b $(__git_refs .)\" -- \"$cur\"))\n+\tCOMPREPLY=($(compgen -W \"-l -b $(__git_refs)\" -- \"$cur\"))\n }\n \n _git_diff ()\n@@ -170,7 +176,7 @@ _git_diff ()\n _git_diff_tree ()\n {\n \tlocal cur=\"${COMP_WORDS[COMP_CWORD]}\"\n-\tCOMPREPLY=($(compgen -W \"-r -p -M $(__git_refs .)\" -- \"$cur\"))\n+\tCOMPREPLY=($(compgen -W \"-r -p -M $(__git_refs)\" -- \"$cur\"))\n }\n \n _git_fetch ()\n@@ -188,7 +194,7 @@ _git_fetch ()\n \t\tcase \"$cur\" in\n \t\t*:*)\n \t        cur=$(echo \"$cur\" | sed 's/^.*://')\n-\t\t\tCOMPREPLY=($(compgen -W \"$(__git_refs .)\" -- \"$cur\"))\n+\t\t\tCOMPREPLY=($(compgen -W \"$(__git_refs)\" -- \"$cur\"))\n \t\t\t;;\n \t\t*)\n \t\t\tlocal remote\n@@ -221,10 +227,10 @@ _git_log ()\n \t*..*)\n \t\tlocal pfx=$(echo \"$cur\" | sed 's/\\.\\..*$/../')\n \t\tcur=$(echo \"$cur\" | sed 's/^.*\\.\\.//')\n-\t\tCOMPREPLY=($(compgen -P \"$pfx\" -W \"$(__git_refs .)\" -- \"$cur\"))\n+\t\tCOMPREPLY=($(compgen -P \"$pfx\" -W \"$(__git_refs)\" -- \"$cur\"))\n \t\t;;\n \t*)\n-\t\tCOMPREPLY=($(compgen -W \"$(__git_refs .)\" -- \"$cur\"))\n+\t\tCOMPREPLY=($(compgen -W \"$(__git_refs)\" -- \"$cur\"))\n \t\t;;\n \tesac\n }\n@@ -232,7 +238,7 @@ _git_log ()\n _git_merge_base ()\n {\n \tlocal cur=\"${COMP_WORDS[COMP_CWORD]}\"\n-\tCOMPREPLY=($(compgen -W \"$(__git_refs .)\" -- \"$cur\"))\n+\tCOMPREPLY=($(compgen -W \"$(__git_refs)\" -- \"$cur\"))\n }\n \n _git_pull ()\n@@ -280,7 +286,7 @@ _git_push ()\n \t\t\tCOMPREPLY=($(compgen -W \"$(__git_refs \"$remote\")\" -- \"$cur\"))\n \t\t\t;;\n \t\t*)\n-\t\t\tCOMPREPLY=($(compgen -W \"$(__git_refs2 .)\" -- \"$cur\"))\n+\t\t\tCOMPREPLY=($(compgen -W \"$(__git_refs2)\" -- \"$cur\"))\n \t\t\t;;\n \t\tesac\n \t\t;;\n@@ -291,56 +297,67 @@ _git_reset ()\n {\n \tlocal cur=\"${COMP_WORDS[COMP_CWORD]}\"\n \tlocal opt=\"--mixed --hard --soft\"\n-\tCOMPREPLY=($(compgen -W \"$opt $(__git_refs .)\" -- \"$cur\"))\n+\tCOMPREPLY=($(compgen -W \"$opt $(__git_refs)\" -- \"$cur\"))\n }\n \n _git_show ()\n {\n \tlocal cur=\"${COMP_WORDS[COMP_CWORD]}\"\n-\tCOMPREPLY=($(compgen -W \"$(__git_refs .)\" -- \"$cur\"))\n+\tCOMPREPLY=($(compgen -W \"$(__git_refs)\" -- \"$cur\"))\n }\n \n _git ()\n {\n-\tif [ $COMP_CWORD = 1 ]; then\n+\tlocal i c=1 command __git_dir\n+\n+\twhile [ $c -lt $COMP_CWORD ]; do\n+\t\ti=\"${COMP_WORDS[c]}\"\n+\t\tcase \"$i\" in\n+\t\t--git-dir=*) __git_dir=\"${i#--git-dir=}\" ;;\n+\t\t--bare)      __git_dir=\".\" ;;\n+\t\t--version|--help|-p|--paginate) ;;\n+\t\t*) command=\"$i\"; break ;;\n+\t\tesac\n+\t\tc=$((++c))\n+\tdone\n+\n+\tif [ $c -eq $COMP_CWORD -a -z \"$command\" ]; then\n \t\tCOMPREPLY=($(compgen \\\n-\t\t\t-W \"--version $(git help -a|egrep '^ ') \\\n+\t\t\t-W \"--git-dir= --version \\\n+\t\t\t\t$(git help -a|egrep '^ ') \\\n \t\t\t    $(__git_aliases)\" \\\n \t\t\t-- \"${COMP_WORDS[COMP_CWORD]}\"))\n-\telse\n-\t\tlocal command=\"${COMP_WORDS[1]}\"\n-\t\tlocal expansion=$(__git_aliased_command \"$command\")\n+\t\treturn;\n+\tfi\n \n-\t\tif [ \"$expansion\" ]; then\n-\t\t\tcommand=\"$expansion\"\n-\t\tfi\n+\tlocal expansion=$(__git_aliased_command \"$command\")\n+\t[ \"$expansion\" ] && command=\"$expansion\"\n \n-\t\tcase \"$command\" in\n-\t\tbranch)      _git_branch ;;\n-\t\tcat-file)    _git_cat_file ;;\n-\t\tcheckout)    _git_checkout ;;\n-\t\tdiff)        _git_diff ;;\n-\t\tdiff-tree)   _git_diff_tree ;;\n-\t\tfetch)       _git_fetch ;;\n-\t\tlog)         _git_log ;;\n-\t\tls-remote)   _git_ls_remote ;;\n-\t\tls-tree)     _git_ls_tree ;;\n-\t\tmerge-base)  _git_merge_base ;;\n-\t\tpull)        _git_pull ;;\n-\t\tpush)        _git_push ;;\n-\t\treset)       _git_reset ;;\n-\t\tshow)        _git_show ;;\n-\t\tshow-branch) _git_log ;;\n-\t\twhatchanged) _git_log ;;\n-\t\t*)           COMPREPLY=() ;;\n-\t\tesac\n-\tfi\n+\tcase \"$command\" in\n+\tbranch)      _git_branch ;;\n+\tcat-file)    _git_cat_file ;;\n+\tcheckout)    _git_checkout ;;\n+\tdiff)        _git_diff ;;\n+\tdiff-tree)   _git_diff_tree ;;\n+\tfetch)       _git_fetch ;;\n+\tlog)         _git_log ;;\n+\tls-remote)   _git_ls_remote ;;\n+\tls-tree)     _git_ls_tree ;;\n+\tmerge-base)  _git_merge_base ;;\n+\tpull)        _git_pull ;;\n+\tpush)        _git_push ;;\n+\treset)       _git_reset ;;\n+\tshow)        _git_show ;;\n+\tshow-branch) _git_log ;;\n+\twhatchanged) _git_log ;;\n+\t*)           COMPREPLY=() ;;\n+\tesac\n }\n \n _gitk ()\n {\n \tlocal cur=\"${COMP_WORDS[COMP_CWORD]}\"\n-\tCOMPREPLY=($(compgen -W \"--all $(__git_refs .)\" -- \"$cur\"))\n+\tCOMPREPLY=($(compgen -W \"--all $(__git_refs)\" -- \"$cur\"))\n }\n \n complete -o default -o nospace -F _git git\n-- \n1.4.3.3.g9621\n"}]}