{"thread":{"id":"43699","subject":"[PATCH 2/2] Fix broken bash completion of local refs.","startedAt":"2006-11-28T17:12:26Z","lastAt":"2006-11-28T17:12:26Z","messageCount":1,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"296894","messageId":"20061128171226.GB28830@spearce.org","threadId":"43699","inReplyTo":"a7f722557e34d86c38da7163392e3be8021692e2.1164733875.git.spearce@spearce.org","subject":"[PATCH 2/2] Fix broken bash completion of local refs.","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2006-11-28T17:12:26Z","receivedAt":"2006-11-28T17:12:26Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Commit 35e65ecc broke completion of local refs, e.g. \"git pull . fo<tab>\"\nno longer would complete to \"foo\".  Instead it printed out an internal\ngit error (\"fatal: Not a git repository: '.'\").\n\nThe break occurred when I tried to improve performance by switching from\ngit-peek-remote to git-for-each-ref.  Apparently git-peek-remote will\ndrop into directory \"$1/.git\" (where $1 is its first parameter) if it\nis given a repository with a working directory.  This allowed the bash\ncompletion code to work properly even though it was not handing over\nthe true repository directory.\n\nSo now we do a stat in bash to see if we need to add \"/.git\" to the\npath string before running any command with --git-dir.\n\nI also tried to optimize away two \"git rev-parse --git-dir\" invocations\nin common cases like \"git log fo<tab>\" as typically the user is in the\ntop level directory of their project and therefore the .git subdirectory\nis in the current working directory.  This should make a difference on\nsystems where fork+exec might take a little while.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n This fixes the breakage noticed by Junio.\n\n contrib/completion/git-completion.bash |   37 ++++++++++++++++---------------\n 1 files changed, 19 insertions(+), 18 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex be978cf..447ec20 100755\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -34,7 +34,19 @@\n \n __gitdir ()\n {\n-\techo \"${__git_dir:-$(git rev-parse --git-dir 2>/dev/null)}\"\n+\tif [ -z \"$1\" ]; then\n+\t\tif [ -n \"$__git_dir\" ]; then\n+\t\t\techo \"$__git_dir\"\n+\t\telif [ -d .git ]; then\n+\t\t\techo .git\n+\t\telse\n+\t\t\tgit rev-parse --git-dir 2>/dev/null\n+\t\tfi\n+\telif [ -d \"$1/.git\" ]; then\n+\t\techo \"$1/.git\"\n+\telse\n+\t\techo \"$1\"\n+\tfi\n }\n \n __git_ps1 ()\n@@ -51,7 +63,7 @@ __git_ps1 ()\n \n __git_heads ()\n {\n-\tlocal cmd i is_hash=y dir=\"${1:-$(__gitdir)}\"\n+\tlocal cmd i is_hash=y dir=\"$(__gitdir \"$1\")\"\n \tif [ -d \"$dir\" ]; then\n \t\tfor i in $(git --git-dir=\"$dir\" \\\n \t\t\tfor-each-ref --format='%(refname)' \\\n@@ -60,7 +72,7 @@ __git_heads ()\n \t\tdone\n \t\treturn\n \tfi\n-\tfor i in $(git-ls-remote \"$dir\" 2>/dev/null); do\n+\tfor i in $(git-ls-remote \"$1\" 2>/dev/null); do\n \t\tcase \"$is_hash,$i\" in\n \t\ty,*) is_hash=n ;;\n \t\tn,*^{}) is_hash=y ;;\n@@ -72,7 +84,7 @@ __git_heads ()\n \n __git_refs ()\n {\n-\tlocal cmd i is_hash=y dir=\"${1:-$(__gitdir)}\"\n+\tlocal cmd i is_hash=y dir=\"$(__gitdir \"$1\")\"\n \tif [ -d \"$dir\" ]; then\n \t\tif [ -e \"$dir/HEAD\" ]; then echo HEAD; fi\n \t\tfor i in $(git --git-dir=\"$dir\" \\\n@@ -101,20 +113,9 @@ __git_refs ()\n \n __git_refs2 ()\n {\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 \"$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-\t\tn,refs/tags/*) is_hash=y; echo \"${i#refs/tags/}:${i#refs/tags/}\" ;;\n-\t\tn,refs/heads/*) is_hash=y; echo \"${i#refs/heads/}:${i#refs/heads/}\" ;;\n-\t\tn,*) is_hash=y; echo \"$i:$i\" ;;\n-\t\tesac\n+\tlocal i\n+\tfor i in $(__git_refs \"$1\"); do\n+\t\techo \"$i:$i\"\n \tdone\n }\n \n-- \n"}]}