{"thread":{"id":"45030","subject":"[PATCHv2 00/21] completion: various __gitdir()-related improvements","startedAt":"2017-02-03T02:49:16Z","lastAt":"2017-02-15T13:07:02Z","messageCount":28,"participants":["SZEDER Gábor","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"310748","messageId":"20170203024829.8071-1-szeder.dev@gmail.com","threadId":"45030","inReplyTo":null,"subject":"[PATCHv2 00/21] completion: various __gitdir()-related improvements","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-03T02:48:08Z","receivedAt":"2017-02-03T02:49:16Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"This is a long overdue reroll of a bunch of bugfixes, cleanups and\noptimizations related to how the completion script finds the path to\nthe repository and how it uses that path.  Most importantly this\nseries adds support for completion following 'git -C path', and it\neliminates a few subshells and git processes, for the sake of\nfork()+exec() challenged OSes.\n\nThe first round is at [1].  It made its way to pu back then, but since\nthe reroll didn't come it was eventually discarded.\n\nWhat did NOT change since v1 is that the new option added to 'git\nrev-parse' is still called '--absolute-git-dir'.  There was a\nsuggestion [2] to turn it into an orthogonal '--absolute-dir' option\nthat works with other path-querying options, too, but I really doubt\nit's worth it.  In short, regular scripts don't care, because a\nrelative path doesn't make any difference for them, and before we do\nthis orthogonal thing we have to decide a bunch of questions first,\nsee [3].\n\nChanges since v1:\n\n - Use our real_path() instead of system realpath() to implement 'git\n   rev-parse --absolute-git-dir'.\n - Refactored a bit how __git_refs() determines where it should list\n   refs from.\n - Renamed a few refnames and remote in the tests (this accounts for\n   the bulk of the interdiff).\n - Misc small adjustments: a few more comments, removed unnecessary\n   disambiguating '--', typofix and more consistent quoting.\n - Improved commit messages.\n - Rebased to current master.\n\nThe interdiff below is compared to v1 rebased on top of current master.\n\nThis series is also available at\n\n  https://github.com/szeder/git completion-gitdir-improvements\n\n\n[1] - http://public-inbox.org/git/1456440650-32623-1-git-send-email-szeder@ira.uka.de/T/\n\n[2] - http://public-inbox.org/git/CANoM8SXO_Rz_CVOz9ptsaVCzcQ2D1FQrSuFFW4vZ4SdRYMzD=w@mail.gmail.com/\n\n[3] - http://public-inbox.org/git/20160518185825.Horde.EPd2nJNvqEW_VX4b01yWdIr@webmail.informatik.kit.edu/\n\nSZEDER Gábor (21):\n  completion: improve __git_refs()'s in-code documentation\n  completion tests: don't add test cruft to the test repository\n  completion tests: make the $cur variable local to the test helper\n    functions\n  completion tests: consolidate getting path of current working\n    directory\n  completion tests: check __gitdir()'s output in the error cases\n  completion tests: add tests for the __git_refs() helper function\n  completion: ensure that the repository path given on the command line\n    exists\n  completion: fix most spots not respecting 'git --git-dir=<path>'\n  completion: respect 'git --git-dir=<path>' when listing remote refs\n  completion: list refs from remote when remote's name matches a\n    directory\n  completion: don't list 'HEAD' when trying refs completion outside of a\n    repo\n  completion: list short refs from a remote given as a URL\n  completion: don't offer commands when 'git --opt' needs an argument\n  completion: fix completion after 'git -C <path>'\n  rev-parse: add '--absolute-git-dir' option\n  completion: respect 'git -C <path>'\n  completion: don't use __gitdir() for git commands\n  completion: consolidate silencing errors from git commands\n  completion: don't guard git executions with __gitdir()\n  completion: extract repository discovery from __gitdir()\n  completion: cache the path to the repository\n\n Documentation/git-rev-parse.txt        |   4 +\n builtin/rev-parse.c                    |  26 +-\n contrib/completion/git-completion.bash | 250 ++++++++++-----\n t/t1500-rev-parse.sh                   |  17 +-\n t/t9902-completion.sh                  | 561 +++++++++++++++++++++++++++++----\n 5 files changed, 690 insertions(+), 168 deletions(-)\n\n-- \n2.11.0.555.g967c1bcb3\n\ndiff --git a/builtin/rev-parse.c b/builtin/rev-parse.c\nindex 4040b3c86..1967bafba 100644\n--- a/builtin/rev-parse.c\n+++ b/builtin/rev-parse.c\n@@ -820,10 +820,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)\n \t\t\t\t\tif (!gitdir && !prefix)\n \t\t\t\t\t\tgitdir = \".git\";\n \t\t\t\t\tif (gitdir) {\n-\t\t\t\t\t\tchar absolute_path[PATH_MAX];\n-\t\t\t\t\t\tif (!realpath(gitdir, absolute_path))\n-\t\t\t\t\t\t\tdie_errno(_(\"unable to get absolute path\"));\n-\t\t\t\t\t\tputs(absolute_path);\n+\t\t\t\t\t\tputs(real_path(gitdir));\n \t\t\t\t\t\tcontinue;\n \t\t\t\t\t}\n \t\t\t\t}\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 0184d0ebc..ed06cb621 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -350,41 +350,38 @@ __git_tags ()\n \n # Lists refs from the local (by default) or from a remote repository.\n # It accepts 0, 1 or 2 arguments:\n-# 1: The remote to lists refs from (optional; ignored, if set but empty).\n+# 1: The remote to list refs from (optional; ignored, if set but empty).\n #    Can be the name of a configured remote, a path, or a URL.\n # 2: In addition to local refs, list unique branches from refs/remotes/ for\n #    'git checkout's tracking DWIMery (optional; ignored, if set but empty).\n __git_refs ()\n {\n \tlocal i hash dir track=\"${2-}\"\n-\tlocal from_local=y remote=\"${1-}\" named_remote=n\n+\tlocal list_refs_from=path remote=\"${1-}\"\n \tlocal format refs pfx\n \n \t__git_find_repo_path\n \tdir=\"$__git_repo_path\"\n \n-\tif [ -z \"$dir\" ] && [ -z \"$remote\" ]; then\n-\t\treturn\n-\tfi\n-\n-\tif [ -n \"$remote\" ]; then\n+\tif [ -z \"$remote\" ]; then\n+\t\tif [ -z \"$dir\" ]; then\n+\t\t\treturn\n+\t\tfi\n+\telse\n \t\tif __git_is_configured_remote \"$remote\"; then\n \t\t\t# configured remote takes precedence over a\n \t\t\t# local directory with the same name\n-\t\t\tfrom_local=n\n-\t\t\tnamed_remote=y\n+\t\t\tlist_refs_from=remote\n+\t\telif [ -d \"$remote/.git\" ]; then\n+\t\t\tdir=\"$remote/.git\"\n+\t\telif [ -d \"$remote\" ]; then\n+\t\t\tdir=\"$remote\"\n \t\telse\n-\t\t\tif [ -d \"$remote/.git\" ]; then\n-\t\t\t\tdir=\"$remote/.git\"\n-\t\t\telif [ -d \"$remote\" ]; then\n-\t\t\t\tdir=\"$remote\"\n-\t\t\telse\n-\t\t\t\tfrom_local=n\n-\t\t\tfi\n+\t\t\tlist_refs_from=url\n \t\tfi\n \tfi\n \n-\tif [ \"$from_local\" = y ] && [ -d \"$dir\" ]; then\n+\tif [ \"$list_refs_from\" = path ]; then\n \t\tcase \"$cur\" in\n \t\trefs|refs/*)\n \t\t\tformat=\"refname\"\n@@ -430,18 +427,18 @@ __git_refs ()\n \t\tdone\n \t\t;;\n \t*)\n-\t\tif [ \"$named_remote\" = y ]; then\n+\t\tif [ \"$list_refs_from\" = remote ]; then\n \t\t\techo \"HEAD\"\n-\t\t\t__git for-each-ref --format=\"%(refname:short)\" -- \\\n+\t\t\t__git for-each-ref --format=\"%(refname:short)\" \\\n \t\t\t\t\"refs/remotes/$remote/\" | sed -e \"s#^$remote/##\"\n \t\telse\n \t\t\t__git ls-remote \"$remote\" HEAD \\\n-\t\t\t\t'refs/tags/*' 'refs/heads/*' 'refs/remotes/*' |\n+\t\t\t\t\"refs/tags/*\" \"refs/heads/*\" \"refs/remotes/*\" |\n \t\t\twhile read -r hash i; do\n \t\t\t\tcase \"$i\" in\n \t\t\t\t*^{})\t;;\n \t\t\t\trefs/*)\techo \"${i#refs/*/}\" ;;\n-\t\t\t\t*)\techo \"$i\" ;;\n+\t\t\t\t*)\techo \"$i\" ;;  # symbolic refs\n \t\t\t\tesac\n \t\t\tdone\n \t\tfi\n@@ -910,7 +907,7 @@ __git_get_option_value ()\n \tdone\n \n \tif [ -n \"$config_key\" ] && [ -z \"$result\" ]; then\n-\t\tresult=\"$(git --git-dir=\"$(__gitdir)\" config \"$config_key\")\"\n+\t\tresult=\"$(__git config \"$config_key\")\"\n \tfi\n \n \techo \"$result\"\n@@ -2830,9 +2827,12 @@ __git_main ()\n \tif [ -z \"$command\" ]; then\n \t\tcase \"$prev\" in\n \t\t--git-dir|-C|--work-tree)\n+\t\t\t# these need a path argument, let's fall back to\n+\t\t\t# Bash filename completion\n \t\t\treturn\n \t\t\t;;\n \t\t-c|--namespace)\n+\t\t\t# we don't support completing these options' arguments\n \t\t\treturn\n \t\t\t;;\n \t\tesac\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex 1660759e3..d711bef24 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -519,22 +519,19 @@ test_expect_success '__git_is_configured_remote' '\n '\n \n test_expect_success 'setup for ref completion' '\n-\techo content >file1 &&\n-\techo more >file2 &&\n-\tgit add file1 file2 &&\n-\tgit commit -m one &&\n-\tgit branch mybranch &&\n-\tgit tag mytag &&\n+\tgit commit --allow-empty -m initial &&\n+\tgit branch matching-branch &&\n+\tgit tag matching-tag &&\n \t(\n \t\tcd otherrepo &&\n-\t\t>file &&\n-\t\tgit add file &&\n-\t\tgit commit -m initial &&\n-\t\tgit branch branch\n+\t\tgit commit --allow-empty -m initial &&\n+\t\tgit branch -m master master-in-other &&\n+\t\tgit branch branch-in-other &&\n+\t\tgit tag tag-in-other\n \t) &&\n-\tgit remote add remote \"$ROOT/otherrepo/.git\" &&\n-\tgit update-ref refs/remotes/remote/branch master &&\n-\tgit update-ref refs/remotes/remote/master master &&\n+\tgit remote add other \"$ROOT/otherrepo/.git\" &&\n+\tgit fetch --no-tags other &&\n+\trm -f .git/FETCH_HEAD &&\n \tgit init thirdrepo\n '\n \n@@ -542,10 +539,10 @@ test_expect_success '__git_refs - simple' '\n \tcat >expected <<-EOF &&\n \tHEAD\n \tmaster\n-\tmybranch\n-\tremote/branch\n-\tremote/master\n-\tmytag\n+\tmatching-branch\n+\tother/branch-in-other\n+\tother/master-in-other\n+\tmatching-tag\n \tEOF\n \t(\n \t\tcur= &&\n@@ -557,7 +554,7 @@ test_expect_success '__git_refs - simple' '\n test_expect_success '__git_refs - full refs' '\n \tcat >expected <<-EOF &&\n \trefs/heads/master\n-\trefs/heads/mybranch\n+\trefs/heads/matching-branch\n \tEOF\n \t(\n \t\tcur=refs/heads/ &&\n@@ -569,8 +566,9 @@ test_expect_success '__git_refs - full refs' '\n test_expect_success '__git_refs - repo given on the command line' '\n \tcat >expected <<-EOF &&\n \tHEAD\n-\tbranch\n-\tmaster\n+\tbranch-in-other\n+\tmaster-in-other\n+\ttag-in-other\n \tEOF\n \t(\n \t\t__git_dir=\"$ROOT/otherrepo/.git\" &&\n@@ -583,8 +581,9 @@ test_expect_success '__git_refs - repo given on the command line' '\n test_expect_success '__git_refs - remote on local file system' '\n \tcat >expected <<-EOF &&\n \tHEAD\n-\tbranch\n-\tmaster\n+\tbranch-in-other\n+\tmaster-in-other\n+\ttag-in-other\n \tEOF\n \t(\n \t\tcur= &&\n@@ -595,11 +594,12 @@ test_expect_success '__git_refs - remote on local file system' '\n \n test_expect_success '__git_refs - remote on local file system - full refs' '\n \tcat >expected <<-EOF &&\n-\trefs/heads/branch\n-\trefs/heads/master\n+\trefs/heads/branch-in-other\n+\trefs/heads/master-in-other\n+\trefs/tags/tag-in-other\n \tEOF\n \t(\n-\t\tcur=refs/heads/ &&\n+\t\tcur=refs/ &&\n \t\t__git_refs otherrepo >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n@@ -608,24 +608,25 @@ test_expect_success '__git_refs - remote on local file system - full refs' '\n test_expect_success '__git_refs - configured remote' '\n \tcat >expected <<-EOF &&\n \tHEAD\n-\tbranch\n-\tmaster\n+\tbranch-in-other\n+\tmaster-in-other\n \tEOF\n \t(\n \t\tcur= &&\n-\t\t__git_refs remote >\"$actual\"\n+\t\t__git_refs other >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n '\n \n test_expect_success '__git_refs - configured remote - full refs' '\n \tcat >expected <<-EOF &&\n-\trefs/heads/branch\n-\trefs/heads/master\n+\trefs/heads/branch-in-other\n+\trefs/heads/master-in-other\n+\trefs/tags/tag-in-other\n \tEOF\n \t(\n-\t\tcur=refs/heads/ &&\n-\t\t__git_refs remote >\"$actual\"\n+\t\tcur=refs/ &&\n+\t\t__git_refs other >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n '\n@@ -633,28 +634,29 @@ test_expect_success '__git_refs - configured remote - full refs' '\n test_expect_success '__git_refs - configured remote - repo given on the command line' '\n \tcat >expected <<-EOF &&\n \tHEAD\n-\tbranch\n-\tmaster\n+\tbranch-in-other\n+\tmaster-in-other\n \tEOF\n \t(\n \t\tcd thirdrepo &&\n \t\t__git_dir=\"$ROOT/.git\" &&\n \t\tcur= &&\n-\t\t__git_refs remote >\"$actual\"\n+\t\t__git_refs other >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n '\n \n test_expect_success '__git_refs - configured remote - full refs - repo given on the command line' '\n \tcat >expected <<-EOF &&\n-\trefs/heads/branch\n-\trefs/heads/master\n+\trefs/heads/branch-in-other\n+\trefs/heads/master-in-other\n+\trefs/tags/tag-in-other\n \tEOF\n \t(\n \t\tcd thirdrepo &&\n \t\t__git_dir=\"$ROOT/.git\" &&\n-\t\tcur=refs/heads/ &&\n-\t\t__git_refs remote >\"$actual\"\n+\t\tcur=refs/ &&\n+\t\t__git_refs other >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n '\n@@ -662,14 +664,14 @@ test_expect_success '__git_refs - configured remote - full refs - repo given on\n test_expect_success '__git_refs - configured remote - remote name matches a directory' '\n \tcat >expected <<-EOF &&\n \tHEAD\n-\tbranch\n-\tmaster\n+\tbranch-in-other\n+\tmaster-in-other\n \tEOF\n-\tmkdir remote &&\n-\ttest_when_finished \"rm -rf remote\" &&\n+\tmkdir other &&\n+\ttest_when_finished \"rm -rf other\" &&\n \t(\n \t\tcur= &&\n-\t\t__git_refs remote >\"$actual\"\n+\t\t__git_refs other >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n '\n@@ -677,8 +679,9 @@ test_expect_success '__git_refs - configured remote - remote name matches a dire\n test_expect_success '__git_refs - URL remote' '\n \tcat >expected <<-EOF &&\n \tHEAD\n-\tbranch\n-\tmaster\n+\tbranch-in-other\n+\tmaster-in-other\n+\ttag-in-other\n \tEOF\n \t(\n \t\tcur= &&\n@@ -689,11 +692,12 @@ test_expect_success '__git_refs - URL remote' '\n \n test_expect_success '__git_refs - URL remote - full refs' '\n \tcat >expected <<-EOF &&\n-\trefs/heads/branch\n-\trefs/heads/master\n+\trefs/heads/branch-in-other\n+\trefs/heads/master-in-other\n+\trefs/tags/tag-in-other\n \tEOF\n \t(\n-\t\tcur=refs/heads/ &&\n+\t\tcur=refs/ &&\n \t\t__git_refs \"file://$ROOT/otherrepo/.git\" >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n@@ -709,7 +713,7 @@ test_expect_success '__git_refs - non-existing remote' '\n \n test_expect_success '__git_refs - non-existing remote - full refs' '\n \t(\n-\t\tcur=refs/heads/ &&\n+\t\tcur=refs/ &&\n \t\t__git_refs non-existing >\"$actual\"\n \t) &&\n \ttest_must_be_empty \"$actual\"\n@@ -725,30 +729,41 @@ test_expect_success '__git_refs - non-existing URL remote' '\n \n test_expect_success '__git_refs - non-existing URL remote - full refs' '\n \t(\n-\t\tcur=refs/heads/ &&\n+\t\tcur=refs/ &&\n \t\t__git_refs \"file://$ROOT/non-existing\" >\"$actual\"\n \t) &&\n \ttest_must_be_empty \"$actual\"\n '\n \n+test_expect_success '__git_refs - not in a git repository' '\n+\t(\n+\t\tGIT_CEILING_DIRECTORIES=\"$ROOT\" &&\n+\t\texport GIT_CEILING_DIRECTORIES &&\n+\t\tcd subdir &&\n+\t\tcur= &&\n+\t\t__git_refs >\"$actual\"\n+\t) &&\n+\ttest_must_be_empty \"$actual\"\n+'\n+\n test_expect_success '__git_refs - unique remote branches for git checkout DWIMery' '\n \tcat >expected <<-EOF &&\n \tHEAD\n \tmaster\n-\tmybranch\n-\totherremote/ambiguous\n-\totherremote/otherbranch\n+\tmatching-branch\n+\tother/ambiguous\n+\tother/branch-in-other\n+\tother/master-in-other\n \tremote/ambiguous\n-\tremote/branch\n-\tremote/master\n-\tmytag\n-\tbranch\n-\tmaster\n-\totherbranch\n+\tremote/branch-in-remote\n+\tmatching-tag\n+\tbranch-in-other\n+\tbranch-in-remote\n+\tmaster-in-other\n \tEOF\n-\tfor remote_ref in refs/remotes/remote/ambiguous \\\n-\t\trefs/remotes/otherremote/ambiguous \\\n-\t\trefs/remotes/otherremote/otherbranch\n+\tfor remote_ref in refs/remotes/other/ambiguous \\\n+\t\trefs/remotes/remote/ambiguous \\\n+\t\trefs/remotes/remote/branch-in-remote\n \tdo\n \t\tgit update-ref $remote_ref master &&\n \t\ttest_when_finished \"git update-ref -d $remote_ref\"\n@@ -760,19 +775,10 @@ test_expect_success '__git_refs - unique remote branches for git checkout DWIMer\n \ttest_cmp expected \"$actual\"\n '\n \n-test_expect_success '__git_refs - not in a git repository' '\n-\t(\n-\t\tGIT_CEILING_DIRECTORIES=\"$ROOT\" &&\n-\t\texport GIT_CEILING_DIRECTORIES &&\n-\t\tcd subdir &&\n-\t\tcur= &&\n-\t\t__git_refs >\"$actual\"\n-\t) &&\n-\ttest_must_be_empty \"$actual\"\n-'\n-\n-test_expect_success 'remove configured remote used for refs completion' '\n-\tgit remote remove remote\n+test_expect_success 'teardown after ref completion' '\n+\tgit branch -d matching-branch &&\n+\tgit tag -d matching-tag &&\n+\tgit remote remove other\n '\n \n test_expect_success '__git_get_config_variables' '\n@@ -898,6 +904,15 @@ test_expect_success 'git --help completion' '\n \ttest_completion \"git --help core\" \"core-tutorial \"\n '\n \n+test_expect_success 'setup for integration tests' '\n+\techo content >file1 &&\n+\techo more >file2 &&\n+\tgit add file1 file2 &&\n+\tgit commit -m one &&\n+\tgit branch mybranch &&\n+\tgit tag mytag\n+'\n+\n test_expect_success 'checkout completes ref names' '\n \ttest_completion \"git checkout m\" <<-\\EOF\n \tmaster Z\n@@ -908,7 +923,7 @@ test_expect_success 'checkout completes ref names' '\n \n test_expect_success 'git -C <path> checkout uses the right repo' '\n \ttest_completion \"git -C subdir -C subsubdir -C .. -C ../otherrepo checkout b\" <<-\\EOF\n-\tbranch Z\n+\tbranch-in-other Z\n \tEOF\n '\n \n"},{"id":"310749","messageId":"20170203024829.8071-3-szeder.dev@gmail.com","threadId":"45030","inReplyTo":"20170203024829.8071-1-szeder.dev@gmail.com","subject":"[PATCHv2 02/21] completion tests: don't add test cruft to the test repository","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-03T02:48:10Z","receivedAt":"2017-02-03T02:49:21Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"While preparing commits, three tests added newly created files to the\nindex using 'git add .', which added not only the files in question\nbut leftover test cruft from previous tests like the files 'expected'\nand 'actual' as well.  Luckily, this had no effect on the tests'\ncorrectness.\n\nAdd only the files we are actually interested in.\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n t/t9902-completion.sh | 6 +++---\n 1 file changed, 3 insertions(+), 3 deletions(-)\n\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex a34e55f87..f490c1d05 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -486,7 +486,7 @@ test_expect_success 'git --help completion' '\n test_expect_success 'setup for ref completion' '\n \techo content >file1 &&\n \techo more >file2 &&\n-\tgit add . &&\n+\tgit add file1 file2 &&\n \tgit commit -m one &&\n \tgit branch mybranch &&\n \tgit tag mytag\n@@ -517,7 +517,7 @@ test_expect_success '<ref>: completes paths' '\n \n test_expect_success 'complete tree filename with spaces' '\n \techo content >\"name with spaces\" &&\n-\tgit add . &&\n+\tgit add \"name with spaces\" &&\n \tgit commit -m spaces &&\n \ttest_completion \"git show HEAD:nam\" <<-\\EOF\n \tname with spaces Z\n@@ -526,7 +526,7 @@ test_expect_success 'complete tree filename with spaces' '\n \n test_expect_success 'complete tree filename with metacharacters' '\n \techo content >\"name with \\${meta}\" &&\n-\tgit add . &&\n+\tgit add \"name with \\${meta}\" &&\n \tgit commit -m meta &&\n \ttest_completion \"git show HEAD:nam\" <<-\\EOF\n \tname with ${meta} Z\n-- \n2.11.0.555.g967c1bcb3\n\n"},{"id":"310750","messageId":"20170203024829.8071-5-szeder.dev@gmail.com","threadId":"45030","inReplyTo":"20170203024829.8071-1-szeder.dev@gmail.com","subject":"[PATCHv2 04/21] completion tests: consolidate getting path of current working directory","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-03T02:48:12Z","receivedAt":"2017-02-03T02:49:29Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"Some tests of the __gitdir() helper function use the $TRASH_DIRECTORY\nvariable in direct path comparisons.  In general this should be\navoided, because it might contain symbolic links.  There happens to be\nno issues with this here, however, because those tests use\n$TRASH_DIRECTORY both for specifying the expected result and for\nspecifying input which in turn is just 'echo'ed verbatim.\n\nOther __gitdir() tests ask for the path of the trash directory by\nrunning $(pwd -P) in each test, sometimes even twice in a single test.\n\nRun $(pwd) only once at the beginning of the test script to store the\npath of the trash directory in a variable, and use that variable in\nall __gitdir() tests.\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n t/t9902-completion.sh | 44 +++++++++++++++++++++-----------------------\n 1 file changed, 21 insertions(+), 23 deletions(-)\n\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex 294a08cfe..030a16e77 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -124,15 +124,22 @@ invalid_variable_name='${foo.bar}'\n \n actual=\"$TRASH_DIRECTORY/actual\"\n \n+if test_have_prereq MINGW\n+then\n+\tROOT=\"$(pwd -W)\"\n+else\n+\tROOT=\"$(pwd)\"\n+fi\n+\n test_expect_success 'setup for __gitdir tests' '\n \tmkdir -p subdir/subsubdir &&\n \tgit init otherrepo\n '\n \n test_expect_success '__gitdir - from command line (through $__git_dir)' '\n-\techo \"$TRASH_DIRECTORY/otherrepo/.git\" >expected &&\n+\techo \"$ROOT/otherrepo/.git\" >expected &&\n \t(\n-\t\t__git_dir=\"$TRASH_DIRECTORY/otherrepo/.git\" &&\n+\t\t__git_dir=\"$ROOT/otherrepo/.git\" &&\n \t\t__gitdir >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n@@ -157,7 +164,7 @@ test_expect_success '__gitdir - .git directory in cwd' '\n '\n \n test_expect_success '__gitdir - .git directory in parent' '\n-\techo \"$(pwd -P)/.git\" >expected &&\n+\techo \"$ROOT/.git\" >expected &&\n \t(\n \t\tcd subdir/subsubdir &&\n \t\t__gitdir >\"$actual\"\n@@ -175,7 +182,7 @@ test_expect_success '__gitdir - cwd is a .git directory' '\n '\n \n test_expect_success '__gitdir - parent is a .git directory' '\n-\techo \"$(pwd -P)/.git\" >expected &&\n+\techo \"$ROOT/.git\" >expected &&\n \t(\n \t\tcd .git/refs/heads &&\n \t\t__gitdir >\"$actual\"\n@@ -184,9 +191,9 @@ test_expect_success '__gitdir - parent is a .git directory' '\n '\n \n test_expect_success '__gitdir - $GIT_DIR set while .git directory in cwd' '\n-\techo \"$TRASH_DIRECTORY/otherrepo/.git\" >expected &&\n+\techo \"$ROOT/otherrepo/.git\" >expected &&\n \t(\n-\t\tGIT_DIR=\"$TRASH_DIRECTORY/otherrepo/.git\" &&\n+\t\tGIT_DIR=\"$ROOT/otherrepo/.git\" &&\n \t\texport GIT_DIR &&\n \t\t__gitdir >\"$actual\"\n \t) &&\n@@ -194,9 +201,9 @@ test_expect_success '__gitdir - $GIT_DIR set while .git directory in cwd' '\n '\n \n test_expect_success '__gitdir - $GIT_DIR set while .git directory in parent' '\n-\techo \"$TRASH_DIRECTORY/otherrepo/.git\" >expected &&\n+\techo \"$ROOT/otherrepo/.git\" >expected &&\n \t(\n-\t\tGIT_DIR=\"$TRASH_DIRECTORY/otherrepo/.git\" &&\n+\t\tGIT_DIR=\"$ROOT/otherrepo/.git\" &&\n \t\texport GIT_DIR &&\n \t\tcd subdir &&\n \t\t__gitdir >\"$actual\"\n@@ -206,24 +213,15 @@ test_expect_success '__gitdir - $GIT_DIR set while .git directory in parent' '\n \n test_expect_success '__gitdir - non-existing $GIT_DIR' '\n \t(\n-\t\tGIT_DIR=\"$TRASH_DIRECTORY/non-existing\" &&\n+\t\tGIT_DIR=\"$ROOT/non-existing\" &&\n \t\texport GIT_DIR &&\n \t\ttest_must_fail __gitdir\n \t)\n '\n \n-function pwd_P_W () {\n-\tif test_have_prereq MINGW\n-\tthen\n-\t\tpwd -W\n-\telse\n-\t\tpwd -P\n-\tfi\n-}\n-\n test_expect_success '__gitdir - gitfile in cwd' '\n-\techo \"$(pwd_P_W)/otherrepo/.git\" >expected &&\n-\techo \"gitdir: $(pwd_P_W)/otherrepo/.git\" >subdir/.git &&\n+\techo \"$ROOT/otherrepo/.git\" >expected &&\n+\techo \"gitdir: $ROOT/otherrepo/.git\" >subdir/.git &&\n \ttest_when_finished \"rm -f subdir/.git\" &&\n \t(\n \t\tcd subdir &&\n@@ -233,8 +231,8 @@ test_expect_success '__gitdir - gitfile in cwd' '\n '\n \n test_expect_success '__gitdir - gitfile in parent' '\n-\techo \"$(pwd_P_W)/otherrepo/.git\" >expected &&\n-\techo \"gitdir: $(pwd_P_W)/otherrepo/.git\" >subdir/.git &&\n+\techo \"$ROOT/otherrepo/.git\" >expected &&\n+\techo \"gitdir: $ROOT/otherrepo/.git\" >subdir/.git &&\n \ttest_when_finished \"rm -f subdir/.git\" &&\n \t(\n \t\tcd subdir/subsubdir &&\n@@ -244,7 +242,7 @@ test_expect_success '__gitdir - gitfile in parent' '\n '\n \n test_expect_success SYMLINKS '__gitdir - resulting path avoids symlinks' '\n-\techo \"$(pwd -P)/otherrepo/.git\" >expected &&\n+\techo \"$ROOT/otherrepo/.git\" >expected &&\n \tmkdir otherrepo/dir &&\n \ttest_when_finished \"rm -rf otherrepo/dir\" &&\n \tln -s otherrepo/dir link &&\n-- \n2.11.0.555.g967c1bcb3\n\n"},{"id":"310751","messageId":"20170203024829.8071-6-szeder.dev@gmail.com","threadId":"45030","inReplyTo":"20170203024829.8071-1-szeder.dev@gmail.com","subject":"[PATCHv2 05/21] completion tests: check __gitdir()'s output in the error cases","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-03T02:48:13Z","receivedAt":"2017-02-03T02:49:32Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"The __gitdir() helper function shouldn't output anything if not in a\ngit repository.  The relevant tests only checked its error code, so\nextend them to ensure that there's no output.\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n t/t9902-completion.sh | 8 +++++---\n 1 file changed, 5 insertions(+), 3 deletions(-)\n\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex 030a16e77..f7f7d49fb 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -215,8 +215,9 @@ test_expect_success '__gitdir - non-existing $GIT_DIR' '\n \t(\n \t\tGIT_DIR=\"$ROOT/non-existing\" &&\n \t\texport GIT_DIR &&\n-\t\ttest_must_fail __gitdir\n-\t)\n+\t\ttest_must_fail __gitdir >\"$actual\"\n+\t) &&\n+\ttest_must_be_empty \"$actual\"\n '\n \n test_expect_success '__gitdir - gitfile in cwd' '\n@@ -255,7 +256,8 @@ test_expect_success SYMLINKS '__gitdir - resulting path avoids symlinks' '\n '\n \n test_expect_success '__gitdir - not a git repository' '\n-\tnongit test_must_fail __gitdir\n+\tnongit test_must_fail __gitdir >\"$actual\" &&\n+\ttest_must_be_empty \"$actual\"\n '\n \n test_expect_success '__gitcomp - trailing space - options' '\n-- \n2.11.0.555.g967c1bcb3\n\n"},{"id":"310752","messageId":"20170203024829.8071-4-szeder.dev@gmail.com","threadId":"45030","inReplyTo":"20170203024829.8071-1-szeder.dev@gmail.com","subject":"[PATCHv2 03/21] completion tests: make the $cur variable local to the test helper functions","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-03T02:48:11Z","receivedAt":"2017-02-03T02:49:33Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"The test helper functions test_gitcomp() and test_gitcomp_nl() leak\nthe $cur variable into the test environment.  Since this variable has\na special role in the Bash completion script (it holds the word\ncurrently being completed) it influences the behavior of most\ncompletion functions and thus this leakage could interfere with\nsubsequent tests.  Although there are no such issues in the current\ntests, early versions of the new tests that will be added later in\nthis series suffered because of this.\n\nIt's better to play safe and declare $cur local in those test helper\nfunctions.  'local' is bashism, of course, but the tests of the Bash\ncompletion script are run under Bash anyway, and there are already\nother variables declared local in this test script.\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n t/t9902-completion.sh | 4 ++--\n 1 file changed, 2 insertions(+), 2 deletions(-)\n\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex f490c1d05..294a08cfe 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -98,7 +98,7 @@ test_gitcomp ()\n {\n \tlocal -a COMPREPLY &&\n \tsed -e 's/Z$//' >expected &&\n-\tcur=\"$1\" &&\n+\tlocal cur=\"$1\" &&\n \tshift &&\n \t__gitcomp \"$@\" &&\n \tprint_comp &&\n@@ -113,7 +113,7 @@ test_gitcomp_nl ()\n {\n \tlocal -a COMPREPLY &&\n \tsed -e 's/Z$//' >expected &&\n-\tcur=\"$1\" &&\n+\tlocal cur=\"$1\" &&\n \tshift &&\n \t__gitcomp_nl \"$@\" &&\n \tprint_comp &&\n-- \n2.11.0.555.g967c1bcb3\n\n"},{"id":"310753","messageId":"20170203024829.8071-12-szeder.dev@gmail.com","threadId":"45030","inReplyTo":"20170203024829.8071-1-szeder.dev@gmail.com","subject":"[PATCHv2 11/21] completion: don't list 'HEAD' when trying refs completion outside of a repo","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-03T02:48:19Z","receivedAt":"2017-02-03T02:49:35Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"When refs completion is attempted while not in a git repository, the\ncompletion script offers 'HEAD' erroneously.\n\nCheck early in __git_refs() that there is either a repository or a\nremote to work on, and return early if neither is given.\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n contrib/completion/git-completion.bash | 8 ++++++--\n t/t9902-completion.sh                  | 2 +-\n 2 files changed, 7 insertions(+), 3 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex fd0ba1f3b..6b489b7c8 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -346,7 +346,11 @@ __git_refs ()\n \tlocal list_refs_from=path remote=\"${1-}\"\n \tlocal format refs pfx\n \n-\tif [ -n \"$remote\" ]; then\n+\tif [ -z \"$remote\" ]; then\n+\t\tif [ -z \"$dir\" ]; then\n+\t\t\treturn\n+\t\tfi\n+\telse\n \t\tif __git_is_configured_remote \"$remote\"; then\n \t\t\t# configured remote takes precedence over a\n \t\t\t# local directory with the same name\n@@ -360,7 +364,7 @@ __git_refs ()\n \t\tfi\n \tfi\n \n-\tif [ \"$list_refs_from\" = path ] && [ -d \"$dir\" ]; then\n+\tif [ \"$list_refs_from\" = path ]; then\n \t\tcase \"$cur\" in\n \t\trefs|refs/*)\n \t\t\tformat=\"refname\"\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex a201b5212..5b4defaa5 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -599,7 +599,7 @@ test_expect_success '__git_refs - non-existing URL remote - full refs' '\n \ttest_must_be_empty \"$actual\"\n '\n \n-test_expect_failure '__git_refs - not in a git repository' '\n+test_expect_success '__git_refs - not in a git repository' '\n \t(\n \t\tGIT_CEILING_DIRECTORIES=\"$ROOT\" &&\n \t\texport GIT_CEILING_DIRECTORIES &&\n-- \n2.11.0.555.g967c1bcb3\n\n"},{"id":"310754","messageId":"20170203024829.8071-13-szeder.dev@gmail.com","threadId":"45030","inReplyTo":"20170203024829.8071-1-szeder.dev@gmail.com","subject":"[PATCHv2 12/21] completion: list short refs from a remote given as a URL","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-03T02:48:20Z","receivedAt":"2017-02-03T02:49:36Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"e832f5c09680 (completion: avoid ls-remote in certain scenarios,\n2013-05-28) turned a 'git ls-remote <remote>' query into a 'git\nfor-each-ref refs/remotes/<remote>/' to improve responsiveness of\nremote refs completion by avoiding potential network communication.\nHowever, it inadvertently made impossible to complete short refs from\na remote given as a URL, e.g. 'git fetch git://server.com/repo.git\n<TAB>', because there is, of course, no such thing as\n'refs/remotes/git://server.com/repo.git'.\n\nSince the previous commit we tell apart configured remotes, i.e. those\nthat can have a hierarchy under 'refs/remotes/', from others that\ndon't, including remotes given as URL, so we know when we can't use\nthe faster 'git for-each-ref'-based approach.\n\nResurrect the old, pre-e832f5c09680 'git ls-remote'-based code for the\nlatter case to support listing short refs from remotes given as a URL.\nThe code is slightly updated from the original to\n\n  - take into account the path to the repository given on the command\n    line (if any), and\n  - omit 'ORIG_HEAD' from the query, as 'git ls-remote' will never\n    list it anyway.\n\nWhen the remote given to __git_refs() doesn't exist, then it will be\nhandled by this resurrected 'git ls-remote' query.  This code path\ndoesn't list 'HEAD' unconditionally, which has the nice side effect of\nfixing two more expected test failures.\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n contrib/completion/git-completion.bash | 19 ++++++++++++++++---\n t/t9902-completion.sh                  |  6 +++---\n 2 files changed, 19 insertions(+), 6 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 6b489b7c8..4ded44977 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -338,6 +338,7 @@ __git_tags ()\n # Lists refs from the local (by default) or from a remote repository.\n # It accepts 0, 1 or 2 arguments:\n # 1: The remote to list refs from (optional; ignored, if set but empty).\n+#    Can be the name of a configured remote, a path, or a URL.\n # 2: In addition to local refs, list unique branches from refs/remotes/ for\n #    'git checkout's tracking DWIMery (optional; ignored, if set but empty).\n __git_refs ()\n@@ -410,9 +411,21 @@ __git_refs ()\n \t\tdone\n \t\t;;\n \t*)\n-\t\techo \"HEAD\"\n-\t\tgit --git-dir=\"$dir\" for-each-ref --format=\"%(refname:short)\" \\\n-\t\t\t\"refs/remotes/$remote/\" 2>/dev/null | sed -e \"s#^$remote/##\"\n+\t\tif [ \"$list_refs_from\" = remote ]; then\n+\t\t\techo \"HEAD\"\n+\t\t\tgit --git-dir=\"$dir\" for-each-ref --format=\"%(refname:short)\" \\\n+\t\t\t\t\"refs/remotes/$remote/\" 2>/dev/null | sed -e \"s#^$remote/##\"\n+\t\telse\n+\t\t\tgit --git-dir=\"$dir\" ls-remote \"$remote\" HEAD \\\n+\t\t\t\t\"refs/tags/*\" \"refs/heads/*\" \"refs/remotes/*\" 2>/dev/null |\n+\t\t\twhile read -r hash i; do\n+\t\t\t\tcase \"$i\" in\n+\t\t\t\t*^{})\t;;\n+\t\t\t\trefs/*)\techo \"${i#refs/*/}\" ;;\n+\t\t\t\t*)\techo \"$i\" ;;  # symbolic refs\n+\t\t\t\tesac\n+\t\t\tdone\n+\t\tfi\n \t\t;;\n \tesac\n }\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex 5b4defaa5..500505dca 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -540,7 +540,7 @@ test_expect_success '__git_refs - configured remote - remote name matches a dire\n \ttest_cmp expected \"$actual\"\n '\n \n-test_expect_failure '__git_refs - URL remote' '\n+test_expect_success '__git_refs - URL remote' '\n \tcat >expected <<-EOF &&\n \tHEAD\n \tbranch-in-other\n@@ -567,7 +567,7 @@ test_expect_success '__git_refs - URL remote - full refs' '\n \ttest_cmp expected \"$actual\"\n '\n \n-test_expect_failure '__git_refs - non-existing remote' '\n+test_expect_success '__git_refs - non-existing remote' '\n \t(\n \t\tcur= &&\n \t\t__git_refs non-existing >\"$actual\"\n@@ -583,7 +583,7 @@ test_expect_success '__git_refs - non-existing remote - full refs' '\n \ttest_must_be_empty \"$actual\"\n '\n \n-test_expect_failure '__git_refs - non-existing URL remote' '\n+test_expect_success '__git_refs - non-existing URL remote' '\n \t(\n \t\tcur= &&\n \t\t__git_refs \"file://$ROOT/non-existing\" >\"$actual\"\n-- \n2.11.0.555.g967c1bcb3\n\n"},{"id":"310755","messageId":"20170203024829.8071-8-szeder.dev@gmail.com","threadId":"45030","inReplyTo":"20170203024829.8071-1-szeder.dev@gmail.com","subject":"[PATCHv2 07/21] completion: ensure that the repository path given on the command line exists","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-03T02:48:15Z","receivedAt":"2017-02-03T02:49:38Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"The __gitdir() helper function prints the path to the git repository\nto its stdout or stays silent and returns with error when it can't\nfind a repository or when the repository given via $GIT_DIR doesn't\nexist.\n\nThis is not the case, however, when the path in $__git_dir, i.e. the\npath to the repository specified on the command line via 'git\n--git-dir=<path>', doesn't exist: __gitdir() still outputs it as if it\nwere a real existing repository, making some completion functions\nbelieve that they operate on an existing repository.\n\nCheck that the path in $__git_dir exists and return with error without\nprinting anything to stdout if it doesn't.\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n contrib/completion/git-completion.bash | 1 +\n t/t9902-completion.sh                  | 8 ++++++++\n 2 files changed, 9 insertions(+)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex ee6fb2259..5b2bd6721 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -40,6 +40,7 @@ __gitdir ()\n {\n \tif [ -z \"${1-}\" ]; then\n \t\tif [ -n \"${__git_dir-}\" ]; then\n+\t\t\ttest -d \"$__git_dir\" || return 1\n \t\t\techo \"$__git_dir\"\n \t\telif [ -n \"${GIT_DIR-}\" ]; then\n \t\t\ttest -d \"${GIT_DIR-}\" || return 1\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex 7956cb9b1..7667baabf 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -211,6 +211,14 @@ test_expect_success '__gitdir - $GIT_DIR set while .git directory in parent' '\n \ttest_cmp expected \"$actual\"\n '\n \n+test_expect_success '__gitdir - non-existing path in $__git_dir' '\n+\t(\n+\t\t__git_dir=\"non-existing\" &&\n+\t\ttest_must_fail __gitdir >\"$actual\"\n+\t) &&\n+\ttest_must_be_empty \"$actual\"\n+'\n+\n test_expect_success '__gitdir - non-existing $GIT_DIR' '\n \t(\n \t\tGIT_DIR=\"$ROOT/non-existing\" &&\n-- \n2.11.0.555.g967c1bcb3\n\n"},{"id":"310756","messageId":"20170203024829.8071-17-szeder.dev@gmail.com","threadId":"45030","inReplyTo":"20170203024829.8071-1-szeder.dev@gmail.com","subject":"[PATCHv2 16/21] completion: respect 'git -C <path>'","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-03T02:48:24Z","receivedAt":"2017-02-03T02:49:41Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"'git -C <path>' option(s) on the command line should be taken into\naccount during completion, because\n\n  - like '--git-dir=<path>', it can lead us to a different repository,\n\n  - a few git commands executed in the completion script do care about\n    in which directory they are executed, and\n\n  - the command for which we are providing completion might care about\n    in which directory it will be executed.\n\nHowever, unlike '--git-dir=<path>', the '-C <path>' option can be\nspecified multiple times and their effect is cumulative, so we can't\njust store a single '<path>' in a variable.  Nor can we simply\nconcatenate a path from '-C <path1> -C <path2> ...', because e.g. (in\nan arguably pathological corner case) a relative path might be\nfollowed by an absolute path.\n\nInstead, store all '-C <path>' options word by word in the\n$__git_C_args array in the main git completion function, and pass this\narray, if present, to 'git rev-parse --absolute-git-dir' when\ndiscovering the repository in __gitdir(), and let it take care of\nmultiple options, relative paths, absolute paths and everything.\n\nAlso pass all '-C <path> options via the $__git_C_args array to those\ngit executions which require a worktree and for which it matters from\nwhich directory they are executed from.  There are only three such\ncases:\n\n  - 'git diff-index' and 'git ls-files' in __git_ls_files_helper()\n    used for git-aware filename completion, and\n\n  - the 'git ls-tree' used for completing the 'ref:path' notation.\n\nThe other git commands executed in the completion script don't need\nthese '-C <path>' options, because __gitdir() already took those\noptions into account.  It would not hurt them, either, but let's not\ninduce unnecessary code churn.\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n contrib/completion/git-completion.bash | 19 ++++++--\n t/t9902-completion.sh                  | 87 ++++++++++++++++++++++++++++++++++\n 2 files changed, 101 insertions(+), 5 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex ac5eb9ced..e003afd54 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -39,7 +39,11 @@ esac\n __gitdir ()\n {\n \tif [ -z \"${1-}\" ]; then\n-\t\tif [ -n \"${__git_dir-}\" ]; then\n+\t\tif [ -n \"${__git_C_args-}\" ]; then\n+\t\t\tgit \"${__git_C_args[@]}\" \\\n+\t\t\t\t${__git_dir:+--git-dir=\"$__git_dir\"} \\\n+\t\t\t\trev-parse --absolute-git-dir 2>/dev/null\n+\t\telif [ -n \"${__git_dir-}\" ]; then\n \t\t\ttest -d \"$__git_dir\" || return 1\n \t\t\techo \"$__git_dir\"\n \t\telif [ -n \"${GIT_DIR-}\" ]; then\n@@ -286,10 +290,10 @@ __git_ls_files_helper ()\n \tlocal dir=\"$(__gitdir)\"\n \n \tif [ \"$2\" == \"--committable\" ]; then\n-\t\tgit --git-dir=\"$dir\" -C \"$1\" diff-index --name-only --relative HEAD\n+\t\tgit ${__git_C_args:+\"${__git_C_args[@]}\"} --git-dir=\"$dir\" -C \"$1\" diff-index --name-only --relative HEAD\n \telse\n \t\t# NOTE: $2 is not quoted in order to support multiple options\n-\t\tgit --git-dir=\"$dir\" -C \"$1\" ls-files --exclude-standard $2\n+\t\tgit ${__git_C_args:+\"${__git_C_args[@]}\"} --git-dir=\"$dir\" -C \"$1\" ls-files --exclude-standard $2\n \tfi 2>/dev/null\n }\n \n@@ -519,7 +523,7 @@ __git_complete_revlist_file ()\n \t\t*)   pfx=\"$ref:$pfx\" ;;\n \t\tesac\n \n-\t\t__gitcomp_nl \"$(git --git-dir=\"$(__gitdir)\" ls-tree \"$ls\" 2>/dev/null \\\n+\t\t__gitcomp_nl \"$(git ${__git_C_args:+\"${__git_C_args[@]}\"} --git-dir=\"$(__gitdir)\" ls-tree \"$ls\" 2>/dev/null \\\n \t\t\t\t| sed '/^100... blob /{\n \t\t\t\t           s,^.*\t,,\n \t\t\t\t           s,$, ,\n@@ -2792,6 +2796,7 @@ _git_worktree ()\n __git_main ()\n {\n \tlocal i c=1 command __git_dir\n+\tlocal __git_C_args C_args_count=0\n \n \twhile [ $c -lt $cword ]; do\n \t\ti=\"${words[c]}\"\n@@ -2800,7 +2805,11 @@ __git_main ()\n \t\t--git-dir)   ((c++)) ; __git_dir=\"${words[c]}\" ;;\n \t\t--bare)      __git_dir=\".\" ;;\n \t\t--help) command=\"help\"; break ;;\n-\t\t-c|-C|--work-tree|--namespace) ((c++)) ;;\n+\t\t-c|--work-tree|--namespace) ((c++)) ;;\n+\t\t-C)\t__git_C_args[C_args_count++]=-C\n+\t\t\t((c++))\n+\t\t\t__git_C_args[C_args_count++]=\"${words[c]}\"\n+\t\t\t;;\n \t\t-*) ;;\n \t\t*) command=\"$i\"; break ;;\n \t\tesac\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex be2ed8704..984df05b2 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -211,6 +211,87 @@ test_expect_success '__gitdir - $GIT_DIR set while .git directory in parent' '\n \ttest_cmp expected \"$actual\"\n '\n \n+test_expect_success '__gitdir - from command line while \"git -C\"' '\n+\techo \"$ROOT/.git\" >expected &&\n+\t(\n+\t\t__git_dir=\"$ROOT/.git\" &&\n+\t\t__git_C_args=(-C otherrepo) &&\n+\t\t__gitdir >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success '__gitdir - relative dir from command line and \"git -C\"' '\n+\techo \"$ROOT/otherrepo/.git\" >expected &&\n+\t(\n+\t\tcd subdir &&\n+\t\t__git_dir=\"otherrepo/.git\" &&\n+\t\t__git_C_args=(-C ..) &&\n+\t\t__gitdir >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success '__gitdir - $GIT_DIR set while \"git -C\"' '\n+\techo \"$ROOT/.git\" >expected &&\n+\t(\n+\t\tGIT_DIR=\"$ROOT/.git\" &&\n+\t\texport GIT_DIR &&\n+\t\t__git_C_args=(-C otherrepo) &&\n+\t\t__gitdir >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success '__gitdir - relative dir in $GIT_DIR and \"git -C\"' '\n+\techo \"$ROOT/otherrepo/.git\" >expected &&\n+\t(\n+\t\tcd subdir &&\n+\t\tGIT_DIR=\"otherrepo/.git\" &&\n+\t\texport GIT_DIR &&\n+\t\t__git_C_args=(-C ..) &&\n+\t\t__gitdir >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success '__gitdir - \"git -C\" while .git directory in cwd' '\n+\techo \"$ROOT/otherrepo/.git\" >expected &&\n+\t(\n+\t\t__git_C_args=(-C otherrepo) &&\n+\t\t__gitdir >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success '__gitdir - \"git -C\" while cwd is a .git directory' '\n+\techo \"$ROOT/otherrepo/.git\" >expected &&\n+\t(\n+\t\tcd .git &&\n+\t\t__git_C_args=(-C .. -C otherrepo) &&\n+\t\t__gitdir >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success '__gitdir - \"git -C\" while .git directory in parent' '\n+\techo \"$ROOT/otherrepo/.git\" >expected &&\n+\t(\n+\t\tcd subdir &&\n+\t\t__git_C_args=(-C .. -C otherrepo) &&\n+\t\t__gitdir >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success '__gitdir - non-existing path in \"git -C\"' '\n+\t(\n+\t\t__git_C_args=(-C non-existing) &&\n+\t\ttest_must_fail __gitdir >\"$actual\"\n+\t) &&\n+\ttest_must_be_empty \"$actual\"\n+'\n+\n test_expect_success '__gitdir - non-existing path in $__git_dir' '\n \t(\n \t\t__git_dir=\"non-existing\" &&\n@@ -785,6 +866,12 @@ test_expect_success 'checkout completes ref names' '\n \tEOF\n '\n \n+test_expect_success 'git -C <path> checkout uses the right repo' '\n+\ttest_completion \"git -C subdir -C subsubdir -C .. -C ../otherrepo checkout b\" <<-\\EOF\n+\tbranch-in-other Z\n+\tEOF\n+'\n+\n test_expect_success 'show completes all refs' '\n \ttest_completion \"git show m\" <<-\\EOF\n \tmaster Z\n-- \n2.11.0.555.g967c1bcb3\n\n"},{"id":"310757","messageId":"20170203024829.8071-9-szeder.dev@gmail.com","threadId":"45030","inReplyTo":"20170203024829.8071-1-szeder.dev@gmail.com","subject":"[PATCHv2 08/21] completion: fix most spots not respecting 'git --git-dir=<path>'","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-03T02:48:16Z","receivedAt":"2017-02-03T02:49:45Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"The completion script already respects the path to the repository\nspecified on the command line most of the time, here we add the\nnecessary '--git-dir=$(__gitdir)' options to most of the places where\ngit was executed without it.  The exceptions where said option is not\nadded are the git invocations:\n\n  - in __git_refs() which are non-trivial and will be the subject of\n    the following patch,\n\n  - getting the list of git commands, merge strategies and archive\n    formats, because these are independent from the repository and\n    thus don't need it, and\n\n  - the 'git rev-parse --git-dir' in __gitdir() itself.\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n contrib/completion/git-completion.bash | 12 +++++++-----\n 1 file changed, 7 insertions(+), 5 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 5b2bd6721..295f6de24 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -283,11 +283,13 @@ __gitcomp_file ()\n # argument, and using the options specified in the second argument.\n __git_ls_files_helper ()\n {\n+\tlocal dir=\"$(__gitdir)\"\n+\n \tif [ \"$2\" == \"--committable\" ]; then\n-\t\tgit -C \"$1\" diff-index --name-only --relative HEAD\n+\t\tgit --git-dir=\"$dir\" -C \"$1\" diff-index --name-only --relative HEAD\n \telse\n \t\t# NOTE: $2 is not quoted in order to support multiple options\n-\t\tgit -C \"$1\" ls-files --exclude-standard $2\n+\t\tgit --git-dir=\"$dir\" -C \"$1\" ls-files --exclude-standard $2\n \tfi 2>/dev/null\n }\n \n@@ -408,7 +410,7 @@ __git_refs2 ()\n __git_refs_remotes ()\n {\n \tlocal i hash\n-\tgit ls-remote \"$1\" 'refs/heads/*' 2>/dev/null | \\\n+\tgit --git-dir=\"$(__gitdir)\" ls-remote \"$1\" 'refs/heads/*' 2>/dev/null | \\\n \twhile read -r hash i; do\n \t\techo \"$i:refs/remotes/$1/${i#refs/heads/}\"\n \tdone\n@@ -1186,7 +1188,7 @@ _git_commit ()\n \t\treturn\n \tesac\n \n-\tif git rev-parse --verify --quiet HEAD >/dev/null; then\n+\tif git --git-dir=\"$(__gitdir)\" rev-parse --verify --quiet HEAD >/dev/null; then\n \t\t__git_complete_index_file \"--committable\"\n \telse\n \t\t# This is the first commit\n@@ -1486,7 +1488,7 @@ _git_log ()\n {\n \t__git_has_doubledash && return\n \n-\tlocal g=\"$(git rev-parse --git-dir 2>/dev/null)\"\n+\tlocal g=\"$(__gitdir)\"\n \tlocal merge=\"\"\n \tif [ -f \"$g/MERGE_HEAD\" ]; then\n \t\tmerge=\"--merge\"\n-- \n2.11.0.555.g967c1bcb3\n\n"},{"id":"310758","messageId":"20170203024829.8071-16-szeder.dev@gmail.com","threadId":"45030","inReplyTo":"20170203024829.8071-1-szeder.dev@gmail.com","subject":"[PATCHv2 15/21] rev-parse: add '--absolute-git-dir' option","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-03T02:48:23Z","receivedAt":"2017-02-03T02:49:48Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"The output of 'git rev-parse --git-dir' can be either a relative or an\nabsolute path, depending on whether the current working directory is\nat the top of the worktree or the .git directory or not, or how the\npath to the repository is specified via the '--git-dir=<path>' option\nor the $GIT_DIR environment variable.  And if that output is a\nrelative path, then it is relative to the directory where any 'git\n-C <path>' options might have led us.\n\nThis doesn't matter at all for regular scripts, because the git\nwrapper automatically takes care of changing directories according to\nthe '-C <path>' options, and the scripts can then simply follow any\npath returned by 'git rev-parse --git-dir', even if it's a relative\npath.\n\nOur Bash completion script, however, is unique in that it must run\ndirectly in the user's interactive shell environment.  This means that\nit's not executed through the git wrapper and would have to take care\nof any '-C <path> options on its own, and it can't just change\ndirectories as it pleases.  Consequently, adding support for taking\nany '-C <path>' options on the command line into account during\ncompletion turned out to be considerably more difficult, error prone\nand required more subshells and git processes when it had to cope with\na relative path to the .git directory.\n\nHelp this rather special use case and teach 'git rev-parse' a new\n'--absolute-git-dir' option which always outputs a canonicalized\nabsolute path to the .git directory, regardless of whether the path is\ndiscovered automatically or is specified via $GIT_DIR or 'git\n--git-dir=<path>'.\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n Documentation/git-rev-parse.txt |  4 ++++\n builtin/rev-parse.c             | 26 ++++++++++++++++++--------\n t/t1500-rev-parse.sh            | 17 +++++++++--------\n 3 files changed, 31 insertions(+), 16 deletions(-)\n\ndiff --git a/Documentation/git-rev-parse.txt b/Documentation/git-rev-parse.txt\nindex 7241e9689..91c02b8c8 100644\n--- a/Documentation/git-rev-parse.txt\n+++ b/Documentation/git-rev-parse.txt\n@@ -217,6 +217,10 @@ If `$GIT_DIR` is not defined and the current directory\n is not detected to lie in a Git repository or work tree\n print a message to stderr and exit with nonzero status.\n \n+--absolute-git-dir::\n+\tLike `--git-dir`, but its output is always the canonicalized\n+\tabsolute path.\n+\n --git-common-dir::\n \tShow `$GIT_COMMON_DIR` if defined, else `$GIT_DIR`.\n \ndiff --git a/builtin/rev-parse.c b/builtin/rev-parse.c\nindex ff13e59e1..1967bafba 100644\n--- a/builtin/rev-parse.c\n+++ b/builtin/rev-parse.c\n@@ -802,17 +802,27 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)\n \t\t\t\tputchar('\\n');\n \t\t\t\tcontinue;\n \t\t\t}\n-\t\t\tif (!strcmp(arg, \"--git-dir\")) {\n+\t\t\tif (!strcmp(arg, \"--git-dir\") ||\n+\t\t\t    !strcmp(arg, \"--absolute-git-dir\")) {\n \t\t\t\tconst char *gitdir = getenv(GIT_DIR_ENVIRONMENT);\n \t\t\t\tchar *cwd;\n \t\t\t\tint len;\n-\t\t\t\tif (gitdir) {\n-\t\t\t\t\tputs(gitdir);\n-\t\t\t\t\tcontinue;\n-\t\t\t\t}\n-\t\t\t\tif (!prefix) {\n-\t\t\t\t\tputs(\".git\");\n-\t\t\t\t\tcontinue;\n+\t\t\t\tif (arg[2] == 'g') {\t/* --git-dir */\n+\t\t\t\t\tif (gitdir) {\n+\t\t\t\t\t\tputs(gitdir);\n+\t\t\t\t\t\tcontinue;\n+\t\t\t\t\t}\n+\t\t\t\t\tif (!prefix) {\n+\t\t\t\t\t\tputs(\".git\");\n+\t\t\t\t\t\tcontinue;\n+\t\t\t\t\t}\n+\t\t\t\t} else {\t\t/* --absolute-git-dir */\n+\t\t\t\t\tif (!gitdir && !prefix)\n+\t\t\t\t\t\tgitdir = \".git\";\n+\t\t\t\t\tif (gitdir) {\n+\t\t\t\t\t\tputs(real_path(gitdir));\n+\t\t\t\t\t\tcontinue;\n+\t\t\t\t\t}\n \t\t\t\t}\n \t\t\t\tcwd = xgetcwd();\n \t\t\t\tlen = strlen(cwd);\ndiff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh\nindex 038e24c40..8b62ed85b 100755\n--- a/t/t1500-rev-parse.sh\n+++ b/t/t1500-rev-parse.sh\n@@ -3,7 +3,7 @@\n test_description='test git rev-parse'\n . ./test-lib.sh\n \n-# usage: [options] label is-bare is-inside-git is-inside-work prefix git-dir\n+# usage: [options] label is-bare is-inside-git is-inside-work prefix git-dir absolute-git-dir\n test_rev_parse () {\n \td=\n \tbare=\n@@ -29,7 +29,8 @@ test_rev_parse () {\n \t\t --is-inside-git-dir \\\n \t\t --is-inside-work-tree \\\n \t\t --show-prefix \\\n-\t\t --git-dir\n+\t\t --git-dir \\\n+\t\t --absolute-git-dir\n \tdo\n \t\ttest $# -eq 0 && break\n \t\texpect=\"$1\"\n@@ -62,26 +63,26 @@ test_expect_success 'setup' '\n \tcp -R .git repo.git\n '\n \n-test_rev_parse toplevel false false true '' .git\n+test_rev_parse toplevel false false true '' .git \"$ROOT/.git\"\n \n-test_rev_parse -C .git .git/ false true false '' .\n-test_rev_parse -C .git/objects .git/objects/ false true false '' \"$ROOT/.git\"\n+test_rev_parse -C .git .git/ false true false '' . \"$ROOT/.git\"\n+test_rev_parse -C .git/objects .git/objects/ false true false '' \"$ROOT/.git\" \"$ROOT/.git\"\n \n-test_rev_parse -C sub/dir subdirectory false false true sub/dir/ \"$ROOT/.git\"\n+test_rev_parse -C sub/dir subdirectory false false true sub/dir/ \"$ROOT/.git\" \"$ROOT/.git\"\n \n test_rev_parse -b t 'core.bare = true' true false false\n \n test_rev_parse -b u 'core.bare undefined' false false true\n \n \n-test_rev_parse -C work -g ../.git -b f 'GIT_DIR=../.git, core.bare = false' false false true ''\n+test_rev_parse -C work -g ../.git -b f 'GIT_DIR=../.git, core.bare = false' false false true '' \"../.git\" \"$ROOT/.git\"\n \n test_rev_parse -C work -g ../.git -b t 'GIT_DIR=../.git, core.bare = true' true false false ''\n \n test_rev_parse -C work -g ../.git -b u 'GIT_DIR=../.git, core.bare undefined' false false true ''\n \n \n-test_rev_parse -C work -g ../repo.git -b f 'GIT_DIR=../repo.git, core.bare = false' false false true ''\n+test_rev_parse -C work -g ../repo.git -b f 'GIT_DIR=../repo.git, core.bare = false' false false true '' \"../repo.git\" \"$ROOT/repo.git\"\n \n test_rev_parse -C work -g ../repo.git -b t 'GIT_DIR=../repo.git, core.bare = true' true false false ''\n \n-- \n2.11.0.555.g967c1bcb3\n\n"},{"id":"310759","messageId":"20170203024829.8071-19-szeder.dev@gmail.com","threadId":"45030","inReplyTo":"20170203024829.8071-1-szeder.dev@gmail.com","subject":"[PATCHv2 18/21] completion: consolidate silencing errors from git commands","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-03T02:48:26Z","receivedAt":"2017-02-03T02:49:50Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"Outputting error messages during completion is bad: they disrupt the\ncommand line, can't be deleted, and the user is forced to Ctrl-C and\nstart over most of the time.  We already silence stderr of many git\ncommands in our Bash completion script, but there are still some in\nthere that can spew error messages when something goes wrong.\n\nWe could add the missing stderr redirections to all the remaining\nplaces, but instead let's leverage that git commands are now executed\nthrough the previously introduced __git() wrapper function, and\nredirect standard error to /dev/null only in that function.  This way\nwe need only one redirection to take care of errors from almost all\ngit commands.  Redirecting standard error of the __git() wrapper\nfunction thus became redundant, remove them.\n\nThe exceptions, i.e. the repo-independent git executions and those in\nthe __gitdir() function that don't go through __git() already have\ntheir standard error silenced.\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n contrib/completion/git-completion.bash | 28 ++++++++++++----------------\n 1 file changed, 12 insertions(+), 16 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex e7c0b56ea..1a849761f 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -66,7 +66,7 @@ __gitdir ()\n __git ()\n {\n \tgit ${__git_C_args:+\"${__git_C_args[@]}\"} \\\n-\t\t${__git_dir:+--git-dir=\"$__git_dir\"} \"$@\"\n+\t\t${__git_dir:+--git-dir=\"$__git_dir\"} \"$@\" 2>/dev/null\n }\n \n # The following function is based on code from:\n@@ -300,7 +300,7 @@ __git_ls_files_helper ()\n \telse\n \t\t# NOTE: $2 is not quoted in order to support multiple options\n \t\t__git -C \"$1\" ls-files --exclude-standard $2\n-\tfi 2>/dev/null\n+\tfi\n }\n \n \n@@ -410,7 +410,7 @@ __git_refs ()\n \tfi\n \tcase \"$cur\" in\n \trefs|refs/*)\n-\t\t__git ls-remote \"$remote\" \"$cur*\" 2>/dev/null | \\\n+\t\t__git ls-remote \"$remote\" \"$cur*\" | \\\n \t\twhile read -r hash i; do\n \t\t\tcase \"$i\" in\n \t\t\t*^{}) ;;\n@@ -422,10 +422,10 @@ __git_refs ()\n \t\tif [ \"$list_refs_from\" = remote ]; then\n \t\t\techo \"HEAD\"\n \t\t\t__git for-each-ref --format=\"%(refname:short)\" \\\n-\t\t\t\t\"refs/remotes/$remote/\" 2>/dev/null | sed -e \"s#^$remote/##\"\n+\t\t\t\t\"refs/remotes/$remote/\" | sed -e \"s#^$remote/##\"\n \t\telse\n \t\t\t__git ls-remote \"$remote\" HEAD \\\n-\t\t\t\t\"refs/tags/*\" \"refs/heads/*\" \"refs/remotes/*\" 2>/dev/null |\n+\t\t\t\t\"refs/tags/*\" \"refs/heads/*\" \"refs/remotes/*\" |\n \t\t\twhile read -r hash i; do\n \t\t\t\tcase \"$i\" in\n \t\t\t\t*^{})\t;;\n@@ -451,7 +451,7 @@ __git_refs2 ()\n __git_refs_remotes ()\n {\n \tlocal i hash\n-\t__git ls-remote \"$1\" 'refs/heads/*' 2>/dev/null | \\\n+\t__git ls-remote \"$1\" 'refs/heads/*' | \\\n \twhile read -r hash i; do\n \t\techo \"$i:refs/remotes/$1/${i#refs/heads/}\"\n \tdone\n@@ -527,7 +527,7 @@ __git_complete_revlist_file ()\n \t\t*)   pfx=\"$ref:$pfx\" ;;\n \t\tesac\n \n-\t\t__gitcomp_nl \"$(__git ls-tree \"$ls\" 2>/dev/null \\\n+\t\t__gitcomp_nl \"$(__git ls-tree \"$ls\" \\\n \t\t\t\t| sed '/^100... blob /{\n \t\t\t\t           s,^.*\t,,\n \t\t\t\t           s,$, ,\n@@ -805,7 +805,7 @@ __git_compute_porcelain_commands ()\n __git_get_config_variables ()\n {\n \tlocal section=\"$1\" i IFS=$'\\n'\n-\tfor i in $(__git config --name-only --get-regexp \"^$section\\..*\" 2>/dev/null); do\n+\tfor i in $(__git config --name-only --get-regexp \"^$section\\..*\"); do\n \t\techo \"${i#$section.}\"\n \tdone\n }\n@@ -823,7 +823,7 @@ __git_aliases ()\n # __git_aliased_command requires 1 argument\n __git_aliased_command ()\n {\n-\tlocal word cmdline=$(__git config --get \"alias.$1\" 2>/dev/null)\n+\tlocal word cmdline=$(__git config --get \"alias.$1\")\n \tfor word in $cmdline; do\n \t\tcase \"$word\" in\n \t\t\\!gitk|gitk)\n@@ -1841,9 +1841,7 @@ _git_send_email ()\n {\n \tcase \"$prev\" in\n \t--to|--cc|--bcc|--from)\n-\t\t__gitcomp \"\n-\t\t$(__git send-email --dump-aliases 2>/dev/null)\n-\t\t\"\n+\t\t__gitcomp \"$(__git send-email --dump-aliases)\"\n \t\treturn\n \t\t;;\n \tesac\n@@ -1873,9 +1871,7 @@ _git_send_email ()\n \t\treturn\n \t\t;;\n \t--to=*|--cc=*|--bcc=*|--from=*)\n-\t\t__gitcomp \"\n-\t\t$(__git send-email --dump-aliases 2>/dev/null)\n-\t\t\" \"\" \"${cur#--*=}\"\n+\t\t__gitcomp \"$(__git send-email --dump-aliases)\" \"\" \"${cur#--*=}\"\n \t\treturn\n \t\t;;\n \t--*)\n@@ -1969,7 +1965,7 @@ __git_config_get_set_variables ()\n \t\tc=$((--c))\n \tdone\n \n-\t__git config $config_file --name-only --list 2>/dev/null\n+\t__git config $config_file --name-only --list\n }\n \n _git_config ()\n-- \n2.11.0.555.g967c1bcb3\n\n"},{"id":"310760","messageId":"20170203024829.8071-20-szeder.dev@gmail.com","threadId":"45030","inReplyTo":"20170203024829.8071-1-szeder.dev@gmail.com","subject":"[PATCHv2 19/21] completion: don't guard git executions with __gitdir()","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-03T02:48:27Z","receivedAt":"2017-02-03T02:49:53Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"Three completion functions, namely __git_index_files(), __git_heads()\nand __git_tags(), first run __gitdir() and check that the path it\noutputs exists, i.e. that there is a git repository, and run a git\ncommand only if there is one.\n\nAfter the previous changes in this series there are no further uses of\n__gitdir()'s output in these functions besides those checks.  And\nthose checks are unnecessary, because we can just execute those git\ncommands outside of a repository and let them error out.  We don't\nperform such a check in other places either.\n\nRemove this check and the __gitdir() call from these functions,\nsparing the fork()+exec() overhead of the command substitution and the\npotential 'git rev-parse' execution.\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n contrib/completion/git-completion.bash | 32 +++++++++++---------------------\n 1 file changed, 11 insertions(+), 21 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 1a849761f..1c7493ff2 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -312,35 +312,25 @@ __git_ls_files_helper ()\n #    slash.\n __git_index_files ()\n {\n-\tlocal dir=\"$(__gitdir)\" root=\"${2-.}\" file\n-\n-\tif [ -d \"$dir\" ]; then\n-\t\t__git_ls_files_helper \"$root\" \"$1\" |\n-\t\twhile read -r file; do\n-\t\t\tcase \"$file\" in\n-\t\t\t?*/*) echo \"${file%%/*}\" ;;\n-\t\t\t*) echo \"$file\" ;;\n-\t\t\tesac\n-\t\tdone | sort | uniq\n-\tfi\n+\tlocal root=\"${2-.}\" file\n+\n+\t__git_ls_files_helper \"$root\" \"$1\" |\n+\twhile read -r file; do\n+\t\tcase \"$file\" in\n+\t\t?*/*) echo \"${file%%/*}\" ;;\n+\t\t*) echo \"$file\" ;;\n+\t\tesac\n+\tdone | sort | uniq\n }\n \n __git_heads ()\n {\n-\tlocal dir=\"$(__gitdir)\"\n-\tif [ -d \"$dir\" ]; then\n-\t\t__git for-each-ref --format='%(refname:short)' refs/heads\n-\t\treturn\n-\tfi\n+\t__git for-each-ref --format='%(refname:short)' refs/heads\n }\n \n __git_tags ()\n {\n-\tlocal dir=\"$(__gitdir)\"\n-\tif [ -d \"$dir\" ]; then\n-\t\t__git for-each-ref --format='%(refname:short)' refs/tags\n-\t\treturn\n-\tfi\n+\t__git for-each-ref --format='%(refname:short)' refs/tags\n }\n \n # Lists refs from the local (by default) or from a remote repository.\n-- \n2.11.0.555.g967c1bcb3\n\n"},{"id":"310761","messageId":"20170203024829.8071-11-szeder.dev@gmail.com","threadId":"45030","inReplyTo":"20170203024829.8071-1-szeder.dev@gmail.com","subject":"[PATCHv2 10/21] completion: list refs from remote when remote's name matches a directory","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-03T02:48:18Z","receivedAt":"2017-02-03T02:49:54Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"If the remote given to __git_refs() happens to match both the name of\na configured remote and the name of a directory in the current working\ndirectory, then that directory is assumed to be a git repository, and\nlisting refs from that directory will be attempted.  This is wrong,\nbecause in such a situation git commands (e.g. 'git fetch|pull|push\n<remote>' whom these refs will eventually be passed to) give\nprecedence to the configured remote.  Therefore, __git_refs() should\nlist refs from the configured remote as well.\n\nAdd the helper function __git_is_configured_remote() that checks\nwhether its argument matches the name of a configured remote.  Use\nthis helper to decide how to handle the remote passed to __git_refs().\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n contrib/completion/git-completion.bash | 20 ++++++++++++++++++--\n t/t9902-completion.sh                  | 11 ++++++++++-\n 2 files changed, 28 insertions(+), 3 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 7d7e8b9d9..fd0ba1f3b 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -347,12 +347,16 @@ __git_refs ()\n \tlocal format refs pfx\n \n \tif [ -n \"$remote\" ]; then\n-\t\tif [ -d \"$remote/.git\" ]; then\n+\t\tif __git_is_configured_remote \"$remote\"; then\n+\t\t\t# configured remote takes precedence over a\n+\t\t\t# local directory with the same name\n+\t\t\tlist_refs_from=remote\n+\t\telif [ -d \"$remote/.git\" ]; then\n \t\t\tdir=\"$remote/.git\"\n \t\telif [ -d \"$remote\" ]; then\n \t\t\tdir=\"$remote\"\n \t\telse\n-\t\t\tlist_refs_from=remote\n+\t\t\tlist_refs_from=url\n \t\tfi\n \tfi\n \n@@ -435,6 +439,18 @@ __git_remotes ()\n \tgit --git-dir=\"$d\" remote\n }\n \n+# Returns true if $1 matches the name of a configured remote, false otherwise.\n+__git_is_configured_remote ()\n+{\n+\tlocal remote\n+\tfor remote in $(__git_remotes); do\n+\t\tif [ \"$remote\" = \"$1\" ]; then\n+\t\t\treturn 0\n+\t\tfi\n+\tdone\n+\treturn 1\n+}\n+\n __git_list_merge_strategies ()\n {\n \tgit merge -s help 2>&1 |\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex 6e64cd6ba..a201b5212 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -373,6 +373,15 @@ test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from\n \ttest_cmp expect actual\n '\n \n+test_expect_success '__git_is_configured_remote' '\n+\ttest_when_finished \"git remote remove remote_1\" &&\n+\tgit remote add remote_1 git://remote_1 &&\n+\ttest_when_finished \"git remote remove remote_2\" &&\n+\tgit remote add remote_2 git://remote_2 &&\n+\tverbose __git_is_configured_remote remote_2 &&\n+\ttest_must_fail __git_is_configured_remote non-existent\n+'\n+\n test_expect_success 'setup for ref completion' '\n \tgit commit --allow-empty -m initial &&\n \tgit branch matching-branch &&\n@@ -516,7 +525,7 @@ test_expect_success '__git_refs - configured remote - full refs - repo given on\n \ttest_cmp expected \"$actual\"\n '\n \n-test_expect_failure '__git_refs - configured remote - remote name matches a directory' '\n+test_expect_success '__git_refs - configured remote - remote name matches a directory' '\n \tcat >expected <<-EOF &&\n \tHEAD\n \tbranch-in-other\n-- \n2.11.0.555.g967c1bcb3\n\n"},{"id":"310762","messageId":"20170203024829.8071-18-szeder.dev@gmail.com","threadId":"45030","inReplyTo":"20170203024829.8071-1-szeder.dev@gmail.com","subject":"[PATCHv2 17/21] completion: don't use __gitdir() for git commands","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-03T02:48:25Z","receivedAt":"2017-02-03T02:49:56Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"Several completion functions contain the following pattern to run git\ncommands respecting the path to the repository specified on the\ncommand line:\n\n  git --git-dir=\"$(__gitdir)\" <cmd> <options>\n\nThis imposes the overhead of fork()ing a subshell for the command\nsubstitution and potentially fork()+exec()ing 'git rev-parse' inside\n__gitdir().\n\nNow, if neither '--gitdir=<path>' nor '-C <path>' options are\nspecified on the command line, then those git commands are perfectly\ncapable to discover the repository on their own.  If either one or\nboth of those options are specified on the command line, then, again,\nthe git commands could discover the repository, if we pass them all of\nthose options from the command line.\n\nThis means we don't have to run __gitdir() at all for git commands and\ncan spare its fork()+exec() overhead.\n\nUse Bash parameter expansions to check the $__git_dir variable and\n$__git_C_args array and to assemble the appropriate '--git-dir=<path>'\nand '-C <path>' options if either one or both are present on the\ncommand line.  These parameter expansions are, however, rather long,\nso instead of changing all git executions and make already long lines\neven longer, encapsulate running git with '--git-dir=<path> -C <path>'\noptions into the new __git() wrapper function.  Furthermore, this\nwrapper function will also enable us to silence error messages from\ngit commands uniformly in one place in a later commit.\n\nThere's one tricky case, though: in __git_refs() local refs are listed\nwith 'git for-each-ref', where \"local\" is not necessarily the\nrepository we are currently in, but it might mean a remote repository\nin the filesystem (e.g. listing refs for 'git fetch /some/other/repo\n<TAB>').  Use one-shot variable assignment to override $__git_dir with\nthe path of the repository where the refs should come from.  Although\none-shot variable assignments in front of shell functions are to be\navoided in our scripts in general, in the Bash completion script we\ncan do that safely.\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n contrib/completion/git-completion.bash | 60 ++++++++++++++++++----------------\n 1 file changed, 31 insertions(+), 29 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex e003afd54..e7c0b56ea 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -61,6 +61,14 @@ __gitdir ()\n \tfi\n }\n \n+# Runs git with all the options given as argument, respecting any\n+# '--git-dir=<path>' and '-C <path>' options present on the command line\n+__git ()\n+{\n+\tgit ${__git_C_args:+\"${__git_C_args[@]}\"} \\\n+\t\t${__git_dir:+--git-dir=\"$__git_dir\"} \"$@\"\n+}\n+\n # The following function is based on code from:\n #\n #   bash_completion - programmable completion functions for bash 3.2+\n@@ -287,13 +295,11 @@ __gitcomp_file ()\n # argument, and using the options specified in the second argument.\n __git_ls_files_helper ()\n {\n-\tlocal dir=\"$(__gitdir)\"\n-\n \tif [ \"$2\" == \"--committable\" ]; then\n-\t\tgit ${__git_C_args:+\"${__git_C_args[@]}\"} --git-dir=\"$dir\" -C \"$1\" diff-index --name-only --relative HEAD\n+\t\t__git -C \"$1\" diff-index --name-only --relative HEAD\n \telse\n \t\t# NOTE: $2 is not quoted in order to support multiple options\n-\t\tgit ${__git_C_args:+\"${__git_C_args[@]}\"} --git-dir=\"$dir\" -C \"$1\" ls-files --exclude-standard $2\n+\t\t__git -C \"$1\" ls-files --exclude-standard $2\n \tfi 2>/dev/null\n }\n \n@@ -323,8 +329,7 @@ __git_heads ()\n {\n \tlocal dir=\"$(__gitdir)\"\n \tif [ -d \"$dir\" ]; then\n-\t\tgit --git-dir=\"$dir\" for-each-ref --format='%(refname:short)' \\\n-\t\t\trefs/heads\n+\t\t__git for-each-ref --format='%(refname:short)' refs/heads\n \t\treturn\n \tfi\n }\n@@ -333,8 +338,7 @@ __git_tags ()\n {\n \tlocal dir=\"$(__gitdir)\"\n \tif [ -d \"$dir\" ]; then\n-\t\tgit --git-dir=\"$dir\" for-each-ref --format='%(refname:short)' \\\n-\t\t\trefs/tags\n+\t\t__git for-each-ref --format='%(refname:short)' refs/tags\n \t\treturn\n \tfi\n }\n@@ -385,14 +389,14 @@ __git_refs ()\n \t\t\trefs=\"refs/tags refs/heads refs/remotes\"\n \t\t\t;;\n \t\tesac\n-\t\tgit --git-dir=\"$dir\" for-each-ref --format=\"$pfx%($format)\" \\\n+\t\t__git_dir=\"$dir\" __git for-each-ref --format=\"$pfx%($format)\" \\\n \t\t\t$refs\n \t\tif [ -n \"$track\" ]; then\n \t\t\t# employ the heuristic used by git checkout\n \t\t\t# Try to find a remote branch that matches the completion word\n \t\t\t# but only output if the branch name is unique\n \t\t\tlocal ref entry\n-\t\t\tgit --git-dir=\"$dir\" for-each-ref --shell --format=\"ref=%(refname:short)\" \\\n+\t\t\t__git for-each-ref --shell --format=\"ref=%(refname:short)\" \\\n \t\t\t\t\"refs/remotes/\" | \\\n \t\t\twhile read -r entry; do\n \t\t\t\teval \"$entry\"\n@@ -406,7 +410,7 @@ __git_refs ()\n \tfi\n \tcase \"$cur\" in\n \trefs|refs/*)\n-\t\tgit --git-dir=\"$dir\" ls-remote \"$remote\" \"$cur*\" 2>/dev/null | \\\n+\t\t__git ls-remote \"$remote\" \"$cur*\" 2>/dev/null | \\\n \t\twhile read -r hash i; do\n \t\t\tcase \"$i\" in\n \t\t\t*^{}) ;;\n@@ -417,10 +421,10 @@ __git_refs ()\n \t*)\n \t\tif [ \"$list_refs_from\" = remote ]; then\n \t\t\techo \"HEAD\"\n-\t\t\tgit --git-dir=\"$dir\" for-each-ref --format=\"%(refname:short)\" \\\n+\t\t\t__git for-each-ref --format=\"%(refname:short)\" \\\n \t\t\t\t\"refs/remotes/$remote/\" 2>/dev/null | sed -e \"s#^$remote/##\"\n \t\telse\n-\t\t\tgit --git-dir=\"$dir\" ls-remote \"$remote\" HEAD \\\n+\t\t\t__git ls-remote \"$remote\" HEAD \\\n \t\t\t\t\"refs/tags/*\" \"refs/heads/*\" \"refs/remotes/*\" 2>/dev/null |\n \t\t\twhile read -r hash i; do\n \t\t\t\tcase \"$i\" in\n@@ -447,7 +451,7 @@ __git_refs2 ()\n __git_refs_remotes ()\n {\n \tlocal i hash\n-\tgit --git-dir=\"$(__gitdir)\" ls-remote \"$1\" 'refs/heads/*' 2>/dev/null | \\\n+\t__git ls-remote \"$1\" 'refs/heads/*' 2>/dev/null | \\\n \twhile read -r hash i; do\n \t\techo \"$i:refs/remotes/$1/${i#refs/heads/}\"\n \tdone\n@@ -457,7 +461,7 @@ __git_remotes ()\n {\n \tlocal d=\"$(__gitdir)\"\n \ttest -d \"$d/remotes\" && ls -1 \"$d/remotes\"\n-\tgit --git-dir=\"$d\" remote\n+\t__git remote\n }\n \n # Returns true if $1 matches the name of a configured remote, false otherwise.\n@@ -523,7 +527,7 @@ __git_complete_revlist_file ()\n \t\t*)   pfx=\"$ref:$pfx\" ;;\n \t\tesac\n \n-\t\t__gitcomp_nl \"$(git ${__git_C_args:+\"${__git_C_args[@]}\"} --git-dir=\"$(__gitdir)\" ls-tree \"$ls\" 2>/dev/null \\\n+\t\t__gitcomp_nl \"$(__git ls-tree \"$ls\" 2>/dev/null \\\n \t\t\t\t| sed '/^100... blob /{\n \t\t\t\t           s,^.*\t,,\n \t\t\t\t           s,$, ,\n@@ -801,7 +805,7 @@ __git_compute_porcelain_commands ()\n __git_get_config_variables ()\n {\n \tlocal section=\"$1\" i IFS=$'\\n'\n-\tfor i in $(git --git-dir=\"$(__gitdir)\" config --name-only --get-regexp \"^$section\\..*\" 2>/dev/null); do\n+\tfor i in $(__git config --name-only --get-regexp \"^$section\\..*\" 2>/dev/null); do\n \t\techo \"${i#$section.}\"\n \tdone\n }\n@@ -819,8 +823,7 @@ __git_aliases ()\n # __git_aliased_command requires 1 argument\n __git_aliased_command ()\n {\n-\tlocal word cmdline=$(git --git-dir=\"$(__gitdir)\" \\\n-\t\tconfig --get \"alias.$1\" 2>/dev/null)\n+\tlocal word cmdline=$(__git config --get \"alias.$1\" 2>/dev/null)\n \tfor word in $cmdline; do\n \t\tcase \"$word\" in\n \t\t\\!gitk|gitk)\n@@ -896,7 +899,7 @@ __git_get_option_value ()\n \tdone\n \n \tif [ -n \"$config_key\" ] && [ -z \"$result\" ]; then\n-\t\tresult=\"$(git --git-dir=\"$(__gitdir)\" config \"$config_key\")\"\n+\t\tresult=\"$(__git config \"$config_key\")\"\n \tfi\n \n \techo \"$result\"\n@@ -1237,7 +1240,7 @@ _git_commit ()\n \t\treturn\n \tesac\n \n-\tif git --git-dir=\"$(__gitdir)\" rev-parse --verify --quiet HEAD >/dev/null; then\n+\tif __git rev-parse --verify --quiet HEAD >/dev/null; then\n \t\t__git_complete_index_file \"--committable\"\n \telse\n \t\t# This is the first commit\n@@ -1839,7 +1842,7 @@ _git_send_email ()\n \tcase \"$prev\" in\n \t--to|--cc|--bcc|--from)\n \t\t__gitcomp \"\n-\t\t$(git --git-dir=\"$(__gitdir)\" send-email --dump-aliases 2>/dev/null)\n+\t\t$(__git send-email --dump-aliases 2>/dev/null)\n \t\t\"\n \t\treturn\n \t\t;;\n@@ -1871,7 +1874,7 @@ _git_send_email ()\n \t\t;;\n \t--to=*|--cc=*|--bcc=*|--from=*)\n \t\t__gitcomp \"\n-\t\t$(git --git-dir=\"$(__gitdir)\" send-email --dump-aliases 2>/dev/null)\n+\t\t$(__git send-email --dump-aliases 2>/dev/null)\n \t\t\" \"\" \"${cur#--*=}\"\n \t\treturn\n \t\t;;\n@@ -1966,7 +1969,7 @@ __git_config_get_set_variables ()\n \t\tc=$((--c))\n \tdone\n \n-\tgit --git-dir=\"$(__gitdir)\" config $config_file --name-only --list 2>/dev/null\n+\t__git config $config_file --name-only --list 2>/dev/null\n }\n \n _git_config ()\n@@ -2001,9 +2004,8 @@ _git_config ()\n \tremote.*.push)\n \t\tlocal remote=\"${prev#remote.}\"\n \t\tremote=\"${remote%.push}\"\n-\t\t__gitcomp_nl \"$(git --git-dir=\"$(__gitdir)\" \\\n-\t\t\tfor-each-ref --format='%(refname):%(refname)' \\\n-\t\t\trefs/heads)\"\n+\t\t__gitcomp_nl \"$(__git for-each-ref\n+\t\t\t--format='%(refname):%(refname)' refs/heads)\"\n \t\treturn\n \t\t;;\n \tpull.twohead|pull.octopus)\n@@ -2591,12 +2593,12 @@ _git_stash ()\n \t\t\tif [ $cword -eq 3 ]; then\n \t\t\t\t__gitcomp_nl \"$(__git_refs)\";\n \t\t\telse\n-\t\t\t\t__gitcomp_nl \"$(git --git-dir=\"$(__gitdir)\" stash list \\\n+\t\t\t\t__gitcomp_nl \"$(__git stash list \\\n \t\t\t\t\t\t| sed -n -e 's/:.*//p')\"\n \t\t\tfi\n \t\t\t;;\n \t\tshow,*|apply,*|drop,*|pop,*)\n-\t\t\t__gitcomp_nl \"$(git --git-dir=\"$(__gitdir)\" stash list \\\n+\t\t\t__gitcomp_nl \"$(__git stash list \\\n \t\t\t\t\t| sed -n -e 's/:.*//p')\"\n \t\t\t;;\n \t\t*)\n-- \n2.11.0.555.g967c1bcb3\n\n"},{"id":"310763","messageId":"20170203024829.8071-14-szeder.dev@gmail.com","threadId":"45030","inReplyTo":"20170203024829.8071-1-szeder.dev@gmail.com","subject":"[PATCHv2 13/21] completion: don't offer commands when 'git --opt' needs an argument","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-03T02:48:21Z","receivedAt":"2017-02-03T02:50:00Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"The main git options '--git-dir', '-c', '-C', '--worktree' and\n'--namespace' require an argument, but attempting completion right\nafter them lists git commands.\n\nDon't offer anything right after these options, thus let Bash fall\nback to filename completion, because\n\n  - the three options '--git-dir', '-C' and '--worktree' do actually\n    require a path argument, and\n\n  - we don't complete the required argument of '-c' and '--namespace',\n    and in that case the \"standard\" behavior of our completion script\n    is to not offer anything, but fall back to filename completion.\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n contrib/completion/git-completion.bash | 11 +++++++++++\n 1 file changed, 11 insertions(+)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 4ded44977..7d25b33b8 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -2808,6 +2808,17 @@ __git_main ()\n \tdone\n \n \tif [ -z \"$command\" ]; then\n+\t\tcase \"$prev\" in\n+\t\t--git-dir|-C|--work-tree)\n+\t\t\t# these need a path argument, let's fall back to\n+\t\t\t# Bash filename completion\n+\t\t\treturn\n+\t\t\t;;\n+\t\t-c|--namespace)\n+\t\t\t# we don't support completing these options' arguments\n+\t\t\treturn\n+\t\t\t;;\n+\t\tesac\n \t\tcase \"$cur\" in\n \t\t--*)   __gitcomp \"\n \t\t\t--paginate\n-- \n2.11.0.555.g967c1bcb3\n\n"},{"id":"310764","messageId":"20170203024829.8071-21-szeder.dev@gmail.com","threadId":"45030","inReplyTo":"20170203024829.8071-1-szeder.dev@gmail.com","subject":"[PATCHv2 20/21] completion: extract repository discovery from __gitdir()","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-03T02:48:28Z","receivedAt":"2017-02-03T02:50:04Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"To prepare for caching the path to the repository in the following\ncommit, extract the repository discovering part of __gitdir() into the\n__git_find_repo_path() helper function, which stores the found path in\nthe $__git_repo_path variable instead of printing it.  Make __gitdir()\na wrapper around this new function.  Declare $__git_repo_path local in\nthe toplevel completion functions __git_main() and __gitk_main() to\nensure that it never leaks into the environment and influences\nsubsequent completions (though this isn't necessary right now, as\n__gitdir() is still only executed in subshells, but will matter for\nthe following commit).\n\nAdjust tests checking __gitdir() or any other completion function\ncalling __gitdir() to perform those checks in a subshell to prevent\n$__git_repo_path from leaking into the test environment.  Otherwise\nleave the tests unchanged to demonstrate that this change doesn't\nalter __gitdir()'s behavior.\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n contrib/completion/git-completion.bash | 42 +++++++++++++++++++++-------------\n t/t9902-completion.sh                  | 22 +++++++++++++-----\n 2 files changed, 42 insertions(+), 22 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 1c7493ff2..7775411cd 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -34,26 +34,35 @@ case \"$COMP_WORDBREAKS\" in\n *)   COMP_WORDBREAKS=\"$COMP_WORDBREAKS:\"\n esac\n \n+# Discovers the path to the git repository taking any '--git-dir=<path>' and\n+# '-C <path>' options into account and stores it in the $__git_repo_path\n+# variable.\n+__git_find_repo_path ()\n+{\n+\tif [ -n \"${__git_C_args-}\" ]; then\n+\t\t__git_repo_path=\"$(git \"${__git_C_args[@]}\" \\\n+\t\t\t${__git_dir:+--git-dir=\"$__git_dir\"} \\\n+\t\t\trev-parse --absolute-git-dir 2>/dev/null)\"\n+\telif [ -n \"${__git_dir-}\" ]; then\n+\t\ttest -d \"$__git_dir\" &&\n+\t\t__git_repo_path=\"$__git_dir\"\n+\telif [ -n \"${GIT_DIR-}\" ]; then\n+\t\ttest -d \"${GIT_DIR-}\" &&\n+\t\t__git_repo_path=\"$GIT_DIR\"\n+\telif [ -d .git ]; then\n+\t\t__git_repo_path=.git\n+\telse\n+\t\t__git_repo_path=\"$(git rev-parse --git-dir 2>/dev/null)\"\n+\tfi\n+}\n+\n # __gitdir accepts 0 or 1 arguments (i.e., location)\n # returns location of .git repo\n __gitdir ()\n {\n \tif [ -z \"${1-}\" ]; then\n-\t\tif [ -n \"${__git_C_args-}\" ]; then\n-\t\t\tgit \"${__git_C_args[@]}\" \\\n-\t\t\t\t${__git_dir:+--git-dir=\"$__git_dir\"} \\\n-\t\t\t\trev-parse --absolute-git-dir 2>/dev/null\n-\t\telif [ -n \"${__git_dir-}\" ]; then\n-\t\t\ttest -d \"$__git_dir\" || return 1\n-\t\t\techo \"$__git_dir\"\n-\t\telif [ -n \"${GIT_DIR-}\" ]; then\n-\t\t\ttest -d \"${GIT_DIR-}\" || return 1\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+\t\t__git_find_repo_path || return 1\n+\t\techo \"$__git_repo_path\"\n \telif [ -d \"$1/.git\" ]; then\n \t\techo \"$1/.git\"\n \telse\n@@ -2783,7 +2792,7 @@ _git_worktree ()\n \n __git_main ()\n {\n-\tlocal i c=1 command __git_dir\n+\tlocal i c=1 command __git_dir __git_repo_path\n \tlocal __git_C_args C_args_count=0\n \n \twhile [ $c -lt $cword ]; do\n@@ -2855,6 +2864,7 @@ __gitk_main ()\n {\n \t__git_has_doubledash && return\n \n+\tlocal __git_repo_path\n \tlocal g=\"$(__gitdir)\"\n \tlocal merge=\"\"\n \tif [ -f \"$g/MERGE_HEAD\" ]; then\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex 984df05b2..1816ed2e0 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -147,19 +147,25 @@ test_expect_success '__gitdir - from command line (through $__git_dir)' '\n \n test_expect_success '__gitdir - repo as argument' '\n \techo \"otherrepo/.git\" >expected &&\n-\t__gitdir \"otherrepo\" >\"$actual\" &&\n+\t(\n+\t\t__gitdir \"otherrepo\" >\"$actual\"\n+\t) &&\n \ttest_cmp expected \"$actual\"\n '\n \n test_expect_success '__gitdir - remote as argument' '\n \techo \"remote\" >expected &&\n-\t__gitdir \"remote\" >\"$actual\" &&\n+\t(\n+\t\t__gitdir \"remote\" >\"$actual\"\n+\t) &&\n \ttest_cmp expected \"$actual\"\n '\n \n test_expect_success '__gitdir - .git directory in cwd' '\n \techo \".git\" >expected &&\n-\t__gitdir >\"$actual\" &&\n+\t(\n+\t\t__gitdir >\"$actual\"\n+\t) &&\n \ttest_cmp expected \"$actual\"\n '\n \n@@ -450,7 +456,9 @@ test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from\n \tgit remote add remote_in_config_1 git://remote_1 &&\n \ttest_when_finished \"git remote remove remote_in_config_2\" &&\n \tgit remote add remote_in_config_2 git://remote_2 &&\n-\t__git_remotes >actual &&\n+\t(\n+\t\t__git_remotes >actual\n+\t) &&\n \ttest_cmp expect actual\n '\n \n@@ -459,8 +467,10 @@ test_expect_success '__git_is_configured_remote' '\n \tgit remote add remote_1 git://remote_1 &&\n \ttest_when_finished \"git remote remove remote_2\" &&\n \tgit remote add remote_2 git://remote_2 &&\n-\tverbose __git_is_configured_remote remote_2 &&\n-\ttest_must_fail __git_is_configured_remote non-existent\n+\t(\n+\t\tverbose __git_is_configured_remote remote_2 &&\n+\t\ttest_must_fail __git_is_configured_remote non-existent\n+\t)\n '\n \n test_expect_success 'setup for ref completion' '\n-- \n2.11.0.555.g967c1bcb3\n\n"},{"id":"310765","messageId":"20170203024829.8071-22-szeder.dev@gmail.com","threadId":"45030","inReplyTo":"20170203024829.8071-1-szeder.dev@gmail.com","subject":"[PATCHv2 21/21] completion: cache the path to the repository","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-03T02:48:29Z","receivedAt":"2017-02-03T02:50:07Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"After the previous changes in this series there are only a handful of\n$(__gitdir) command substitutions left in the completion script, but\nthere is still a bit of room for improvements:\n\n  1. The command substitution involves the forking of a subshell,\n     which has considerable overhead on some platforms.\n\n  2. There are a few cases, where this command substitution is\n     executed more than once during a single completion, which means\n     multiple subshells and possibly multiple 'git rev-parse'\n     executions.  __gitdir() is invoked twice while completing refs\n     for e.g. 'git log', 'git rebase', 'gitk', or while completing\n     remote refs for 'git fetch' or 'git push'.\n\nBoth of these points can be addressed by using the\n__git_find_repo_path() helper function introduced in the previous\ncommit:\n\n  1. __git_find_repo_path() stores the path to the repository in a\n     variable instead of printing it, so the command substitution\n     around the function can be avoided.  Or rather: the command\n     substitution should be avoided to make the new value of the\n     variable set inside the function visible to the callers.\n     (Yes, there is now a command substitution inside\n     __git_find_repo_path() around each 'git rev-parse', but that's\n     executed only if necessary, and only once per completion, see\n     point 2. below.)\n\n  2. $__git_repo_path, the variable holding the path to the\n     repository, is declared local in the toplevel completion\n     functions __git_main() and __gitk_main().  Thus, once set, the\n     path is visible in all completion functions, including all\n     subsequent calls to __git_find_repo_path(), meaning that they\n     wouldn't have to re-discover the path to the repository.\n\nSo call __git_find_repo_path() and use $__git_repo_path instead of the\n$(__gitdir) command substitution to access paths in the .git\ndirectory.  Turn tests checking __gitdir()'s repository discovery into\ntests of __git_find_repo_path() such that only the tested function\nchanges but the expected results don't, ensuring that repo discovery\nkeeps working as it did before.\n\nAs __gitdir() is not used anymore in the completion script, mark it as\ndeprecated and direct users' attention to __git_find_repo_path() and\n$__git_repo_path.  Yet keep four __gitdir() tests to ensure that it\nhandles success and failure of __git_find_repo_path() and that it\nstill handles its optional remote argument, because users' custom\ncompletion scriptlets might depend on it.\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n contrib/completion/git-completion.bash |  46 ++++++----\n t/t9902-completion.sh                  | 161 +++++++++++++++++++++------------\n 2 files changed, 132 insertions(+), 75 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 7775411cd..ed06cb621 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -39,6 +39,11 @@ esac\n # variable.\n __git_find_repo_path ()\n {\n+\tif [ -n \"$__git_repo_path\" ]; then\n+\t\t# we already know where it is\n+\t\treturn\n+\tfi\n+\n \tif [ -n \"${__git_C_args-}\" ]; then\n \t\t__git_repo_path=\"$(git \"${__git_C_args[@]}\" \\\n \t\t\t${__git_dir:+--git-dir=\"$__git_dir\"} \\\n@@ -56,6 +61,7 @@ __git_find_repo_path ()\n \tfi\n }\n \n+# Deprecated: use __git_find_repo_path() and $__git_repo_path instead\n # __gitdir accepts 0 or 1 arguments (i.e., location)\n # returns location of .git repo\n __gitdir ()\n@@ -350,10 +356,13 @@ __git_tags ()\n #    'git checkout's tracking DWIMery (optional; ignored, if set but empty).\n __git_refs ()\n {\n-\tlocal i hash dir=\"$(__gitdir)\" track=\"${2-}\"\n+\tlocal i hash dir track=\"${2-}\"\n \tlocal list_refs_from=path remote=\"${1-}\"\n \tlocal format refs pfx\n \n+\t__git_find_repo_path\n+\tdir=\"$__git_repo_path\"\n+\n \tif [ -z \"$remote\" ]; then\n \t\tif [ -z \"$dir\" ]; then\n \t\t\treturn\n@@ -458,8 +467,8 @@ __git_refs_remotes ()\n \n __git_remotes ()\n {\n-\tlocal d=\"$(__gitdir)\"\n-\ttest -d \"$d/remotes\" && ls -1 \"$d/remotes\"\n+\t__git_find_repo_path\n+\ttest -d \"$__git_repo_path/remotes\" && ls -1 \"$__git_repo_path/remotes\"\n \t__git remote\n }\n \n@@ -957,8 +966,8 @@ __git_whitespacelist=\"nowarn warn error error-all fix\"\n \n _git_am ()\n {\n-\tlocal dir=\"$(__gitdir)\"\n-\tif [ -d \"$dir\"/rebase-apply ]; then\n+\t__git_find_repo_path\n+\tif [ -d \"$__git_repo_path\"/rebase-apply ]; then\n \t\t__gitcomp \"--skip --continue --resolved --abort\"\n \t\treturn\n \tfi\n@@ -1041,7 +1050,8 @@ _git_bisect ()\n \tlocal subcommands=\"start bad good skip reset visualize replay log run\"\n \tlocal subcommand=\"$(__git_find_on_cmdline \"$subcommands\")\"\n \tif [ -z \"$subcommand\" ]; then\n-\t\tif [ -f \"$(__gitdir)\"/BISECT_START ]; then\n+\t\t__git_find_repo_path\n+\t\tif [ -f \"$__git_repo_path\"/BISECT_START ]; then\n \t\t\t__gitcomp \"$subcommands\"\n \t\telse\n \t\t\t__gitcomp \"replay start\"\n@@ -1146,8 +1156,8 @@ _git_cherry ()\n \n _git_cherry_pick ()\n {\n-\tlocal dir=\"$(__gitdir)\"\n-\tif [ -f \"$dir\"/CHERRY_PICK_HEAD ]; then\n+\t__git_find_repo_path\n+\tif [ -f \"$__git_repo_path\"/CHERRY_PICK_HEAD ]; then\n \t\t__gitcomp \"--continue --quit --abort\"\n \t\treturn\n \tfi\n@@ -1538,10 +1548,10 @@ __git_log_date_formats=\"relative iso8601 rfc2822 short local default raw\"\n _git_log ()\n {\n \t__git_has_doubledash && return\n+\t__git_find_repo_path\n \n-\tlocal g=\"$(__gitdir)\"\n \tlocal merge=\"\"\n-\tif [ -f \"$g/MERGE_HEAD\" ]; then\n+\tif [ -f \"$__git_repo_path/MERGE_HEAD\" ]; then\n \t\tmerge=\"--merge\"\n \tfi\n \tcase \"$cur\" in\n@@ -1788,11 +1798,12 @@ _git_push ()\n \n _git_rebase ()\n {\n-\tlocal dir=\"$(__gitdir)\"\n-\tif [ -f \"$dir\"/rebase-merge/interactive ]; then\n+\t__git_find_repo_path\n+\tif [ -f \"$__git_repo_path\"/rebase-merge/interactive ]; then\n \t\t__gitcomp \"--continue --skip --abort --quit --edit-todo\"\n \t\treturn\n-\telif [ -d \"$dir\"/rebase-apply ] || [ -d \"$dir\"/rebase-merge ]; then\n+\telif [ -d \"$__git_repo_path\"/rebase-apply ] || \\\n+\t     [ -d \"$__git_repo_path\"/rebase-merge ]; then\n \t\t__gitcomp \"--continue --skip --abort --quit\"\n \t\treturn\n \tfi\n@@ -2467,8 +2478,8 @@ _git_reset ()\n \n _git_revert ()\n {\n-\tlocal dir=\"$(__gitdir)\"\n-\tif [ -f \"$dir\"/REVERT_HEAD ]; then\n+\t__git_find_repo_path\n+\tif [ -f \"$__git_repo_path\"/REVERT_HEAD ]; then\n \t\t__gitcomp \"--continue --quit --abort\"\n \t\treturn\n \tfi\n@@ -2865,9 +2876,10 @@ __gitk_main ()\n \t__git_has_doubledash && return\n \n \tlocal __git_repo_path\n-\tlocal g=\"$(__gitdir)\"\n+\t__git_find_repo_path\n+\n \tlocal merge=\"\"\n-\tif [ -f \"$g/MERGE_HEAD\" ]; then\n+\tif [ -f \"$__git_repo_path/MERGE_HEAD\" ]; then\n \t\tmerge=\"--merge\"\n \tfi\n \tcase \"$cur\" in\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex 1816ed2e0..d711bef24 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -131,213 +131,217 @@ else\n \tROOT=\"$(pwd)\"\n fi\n \n-test_expect_success 'setup for __gitdir tests' '\n+test_expect_success 'setup for __git_find_repo_path/__gitdir tests' '\n \tmkdir -p subdir/subsubdir &&\n+\tmkdir -p non-repo &&\n \tgit init otherrepo\n '\n \n-test_expect_success '__gitdir - from command line (through $__git_dir)' '\n+test_expect_success '__git_find_repo_path - from command line (through $__git_dir)' '\n \techo \"$ROOT/otherrepo/.git\" >expected &&\n \t(\n \t\t__git_dir=\"$ROOT/otherrepo/.git\" &&\n-\t\t__gitdir >\"$actual\"\n-\t) &&\n-\ttest_cmp expected \"$actual\"\n-'\n-\n-test_expect_success '__gitdir - repo as argument' '\n-\techo \"otherrepo/.git\" >expected &&\n-\t(\n-\t\t__gitdir \"otherrepo\" >\"$actual\"\n+\t\t__git_find_repo_path &&\n+\t\techo \"$__git_repo_path\" >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n '\n \n-test_expect_success '__gitdir - remote as argument' '\n-\techo \"remote\" >expected &&\n-\t(\n-\t\t__gitdir \"remote\" >\"$actual\"\n-\t) &&\n-\ttest_cmp expected \"$actual\"\n-'\n-\n-test_expect_success '__gitdir - .git directory in cwd' '\n+test_expect_success '__git_find_repo_path - .git directory in cwd' '\n \techo \".git\" >expected &&\n \t(\n-\t\t__gitdir >\"$actual\"\n+\t\t__git_find_repo_path &&\n+\t\techo \"$__git_repo_path\" >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n '\n \n-test_expect_success '__gitdir - .git directory in parent' '\n+test_expect_success '__git_find_repo_path - .git directory in parent' '\n \techo \"$ROOT/.git\" >expected &&\n \t(\n \t\tcd subdir/subsubdir &&\n-\t\t__gitdir >\"$actual\"\n+\t\t__git_find_repo_path &&\n+\t\techo \"$__git_repo_path\" >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n '\n \n-test_expect_success '__gitdir - cwd is a .git directory' '\n+test_expect_success '__git_find_repo_path - cwd is a .git directory' '\n \techo \".\" >expected &&\n \t(\n \t\tcd .git &&\n-\t\t__gitdir >\"$actual\"\n+\t\t__git_find_repo_path &&\n+\t\techo \"$__git_repo_path\" >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n '\n \n-test_expect_success '__gitdir - parent is a .git directory' '\n+test_expect_success '__git_find_repo_path - parent is a .git directory' '\n \techo \"$ROOT/.git\" >expected &&\n \t(\n \t\tcd .git/refs/heads &&\n-\t\t__gitdir >\"$actual\"\n+\t\t__git_find_repo_path &&\n+\t\techo \"$__git_repo_path\" >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n '\n \n-test_expect_success '__gitdir - $GIT_DIR set while .git directory in cwd' '\n+test_expect_success '__git_find_repo_path - $GIT_DIR set while .git directory in cwd' '\n \techo \"$ROOT/otherrepo/.git\" >expected &&\n \t(\n \t\tGIT_DIR=\"$ROOT/otherrepo/.git\" &&\n \t\texport GIT_DIR &&\n-\t\t__gitdir >\"$actual\"\n+\t\t__git_find_repo_path &&\n+\t\techo \"$__git_repo_path\" >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n '\n \n-test_expect_success '__gitdir - $GIT_DIR set while .git directory in parent' '\n+test_expect_success '__git_find_repo_path - $GIT_DIR set while .git directory in parent' '\n \techo \"$ROOT/otherrepo/.git\" >expected &&\n \t(\n \t\tGIT_DIR=\"$ROOT/otherrepo/.git\" &&\n \t\texport GIT_DIR &&\n \t\tcd subdir &&\n-\t\t__gitdir >\"$actual\"\n+\t\t__git_find_repo_path &&\n+\t\techo \"$__git_repo_path\" >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n '\n \n-test_expect_success '__gitdir - from command line while \"git -C\"' '\n+test_expect_success '__git_find_repo_path - from command line while \"git -C\"' '\n \techo \"$ROOT/.git\" >expected &&\n \t(\n \t\t__git_dir=\"$ROOT/.git\" &&\n \t\t__git_C_args=(-C otherrepo) &&\n-\t\t__gitdir >\"$actual\"\n+\t\t__git_find_repo_path &&\n+\t\techo \"$__git_repo_path\" >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n '\n \n-test_expect_success '__gitdir - relative dir from command line and \"git -C\"' '\n+test_expect_success '__git_find_repo_path - relative dir from command line and \"git -C\"' '\n \techo \"$ROOT/otherrepo/.git\" >expected &&\n \t(\n \t\tcd subdir &&\n \t\t__git_dir=\"otherrepo/.git\" &&\n \t\t__git_C_args=(-C ..) &&\n-\t\t__gitdir >\"$actual\"\n+\t\t__git_find_repo_path &&\n+\t\techo \"$__git_repo_path\" >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n '\n \n-test_expect_success '__gitdir - $GIT_DIR set while \"git -C\"' '\n+test_expect_success '__git_find_repo_path - $GIT_DIR set while \"git -C\"' '\n \techo \"$ROOT/.git\" >expected &&\n \t(\n \t\tGIT_DIR=\"$ROOT/.git\" &&\n \t\texport GIT_DIR &&\n \t\t__git_C_args=(-C otherrepo) &&\n-\t\t__gitdir >\"$actual\"\n+\t\t__git_find_repo_path &&\n+\t\techo \"$__git_repo_path\" >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n '\n \n-test_expect_success '__gitdir - relative dir in $GIT_DIR and \"git -C\"' '\n+test_expect_success '__git_find_repo_path - relative dir in $GIT_DIR and \"git -C\"' '\n \techo \"$ROOT/otherrepo/.git\" >expected &&\n \t(\n \t\tcd subdir &&\n \t\tGIT_DIR=\"otherrepo/.git\" &&\n \t\texport GIT_DIR &&\n \t\t__git_C_args=(-C ..) &&\n-\t\t__gitdir >\"$actual\"\n+\t\t__git_find_repo_path &&\n+\t\techo \"$__git_repo_path\" >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n '\n \n-test_expect_success '__gitdir - \"git -C\" while .git directory in cwd' '\n+test_expect_success '__git_find_repo_path - \"git -C\" while .git directory in cwd' '\n \techo \"$ROOT/otherrepo/.git\" >expected &&\n \t(\n \t\t__git_C_args=(-C otherrepo) &&\n-\t\t__gitdir >\"$actual\"\n+\t\t__git_find_repo_path &&\n+\t\techo \"$__git_repo_path\" >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n '\n \n-test_expect_success '__gitdir - \"git -C\" while cwd is a .git directory' '\n+test_expect_success '__git_find_repo_path - \"git -C\" while cwd is a .git directory' '\n \techo \"$ROOT/otherrepo/.git\" >expected &&\n \t(\n \t\tcd .git &&\n \t\t__git_C_args=(-C .. -C otherrepo) &&\n-\t\t__gitdir >\"$actual\"\n+\t\t__git_find_repo_path &&\n+\t\techo \"$__git_repo_path\" >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n '\n \n-test_expect_success '__gitdir - \"git -C\" while .git directory in parent' '\n+test_expect_success '__git_find_repo_path - \"git -C\" while .git directory in parent' '\n \techo \"$ROOT/otherrepo/.git\" >expected &&\n \t(\n \t\tcd subdir &&\n \t\t__git_C_args=(-C .. -C otherrepo) &&\n-\t\t__gitdir >\"$actual\"\n+\t\t__git_find_repo_path &&\n+\t\techo \"$__git_repo_path\" >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n '\n \n-test_expect_success '__gitdir - non-existing path in \"git -C\"' '\n+test_expect_success '__git_find_repo_path - non-existing path in \"git -C\"' '\n \t(\n \t\t__git_C_args=(-C non-existing) &&\n-\t\ttest_must_fail __gitdir >\"$actual\"\n+\t\ttest_must_fail __git_find_repo_path &&\n+\t\tprintf \"$__git_repo_path\" >\"$actual\"\n \t) &&\n \ttest_must_be_empty \"$actual\"\n '\n \n-test_expect_success '__gitdir - non-existing path in $__git_dir' '\n+test_expect_success '__git_find_repo_path - non-existing path in $__git_dir' '\n \t(\n \t\t__git_dir=\"non-existing\" &&\n-\t\ttest_must_fail __gitdir >\"$actual\"\n+\t\ttest_must_fail __git_find_repo_path &&\n+\t\tprintf \"$__git_repo_path\" >\"$actual\"\n \t) &&\n \ttest_must_be_empty \"$actual\"\n '\n \n-test_expect_success '__gitdir - non-existing $GIT_DIR' '\n+test_expect_success '__git_find_repo_path - non-existing $GIT_DIR' '\n \t(\n \t\tGIT_DIR=\"$ROOT/non-existing\" &&\n \t\texport GIT_DIR &&\n-\t\ttest_must_fail __gitdir >\"$actual\"\n+\t\ttest_must_fail __git_find_repo_path &&\n+\t\tprintf \"$__git_repo_path\" >\"$actual\"\n \t) &&\n \ttest_must_be_empty \"$actual\"\n '\n \n-test_expect_success '__gitdir - gitfile in cwd' '\n+test_expect_success '__git_find_repo_path - gitfile in cwd' '\n \techo \"$ROOT/otherrepo/.git\" >expected &&\n \techo \"gitdir: $ROOT/otherrepo/.git\" >subdir/.git &&\n \ttest_when_finished \"rm -f subdir/.git\" &&\n \t(\n \t\tcd subdir &&\n-\t\t__gitdir >\"$actual\"\n+\t\t__git_find_repo_path &&\n+\t\techo \"$__git_repo_path\" >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n '\n \n-test_expect_success '__gitdir - gitfile in parent' '\n+test_expect_success '__git_find_repo_path - gitfile in parent' '\n \techo \"$ROOT/otherrepo/.git\" >expected &&\n \techo \"gitdir: $ROOT/otherrepo/.git\" >subdir/.git &&\n \ttest_when_finished \"rm -f subdir/.git\" &&\n \t(\n \t\tcd subdir/subsubdir &&\n-\t\t__gitdir >\"$actual\"\n+\t\t__git_find_repo_path &&\n+\t\techo \"$__git_repo_path\" >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n '\n \n-test_expect_success SYMLINKS '__gitdir - resulting path avoids symlinks' '\n+test_expect_success SYMLINKS '__git_find_repo_path - resulting path avoids symlinks' '\n \techo \"$ROOT/otherrepo/.git\" >expected &&\n \tmkdir otherrepo/dir &&\n \ttest_when_finished \"rm -rf otherrepo/dir\" &&\n@@ -345,16 +349,57 @@ test_expect_success SYMLINKS '__gitdir - resulting path avoids symlinks' '\n \ttest_when_finished \"rm -f link\" &&\n \t(\n \t\tcd link &&\n+\t\t__git_find_repo_path &&\n+\t\techo \"$__git_repo_path\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success '__git_find_repo_path - not a git repository' '\n+\t(\n+\t\tcd non-repo &&\n+\t\tGIT_CEILING_DIRECTORIES=\"$ROOT\" &&\n+\t\texport GIT_CEILING_DIRECTORIES &&\n+\t\ttest_must_fail __git_find_repo_path &&\n+\t\tprintf \"$__git_repo_path\" >\"$actual\"\n+\t) &&\n+\ttest_must_be_empty \"$actual\"\n+'\n+\n+test_expect_success '__gitdir - finds repo' '\n+\techo \"$ROOT/.git\" >expected &&\n+\t(\n+\t\tcd subdir/subsubdir &&\n \t\t__gitdir >\"$actual\"\n \t) &&\n \ttest_cmp expected \"$actual\"\n '\n \n-test_expect_success '__gitdir - not a git repository' '\n-\tnongit test_must_fail __gitdir >\"$actual\" &&\n+\n+test_expect_success '__gitdir - returns error when cant find repo' '\n+\t(\n+\t\t__git_dir=\"non-existing\" &&\n+\t\ttest_must_fail __gitdir >\"$actual\"\n+\t) &&\n \ttest_must_be_empty \"$actual\"\n '\n \n+test_expect_success '__gitdir - repo as argument' '\n+\techo \"otherrepo/.git\" >expected &&\n+\t(\n+\t\t__gitdir \"otherrepo\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success '__gitdir - remote as argument' '\n+\techo \"remote\" >expected &&\n+\t(\n+\t\t__gitdir \"remote\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n test_expect_success '__gitcomp - trailing space - options' '\n \ttest_gitcomp \"--re\" \"--dry-run --reuse-message= --reedit-message=\n \t\t--reset-author\" <<-EOF\n-- \n2.11.0.555.g967c1bcb3\n\n"},{"id":"310766","messageId":"20170203024829.8071-10-szeder.dev@gmail.com","threadId":"45030","inReplyTo":"20170203024829.8071-1-szeder.dev@gmail.com","subject":"[PATCHv2 09/21] completion: respect 'git --git-dir=<path>' when listing remote refs","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-03T02:48:17Z","receivedAt":"2017-02-03T02:50:08Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"In __git_refs() the git commands listing refs, both short and full,\nfrom a given remote repository are run without giving them the path to\nthe git repository which might have been specified on the command line\nvia 'git --git-dir=<path>'.  This is bad, those git commands should\naccess the 'refs/remotes/<remote>/' hierarchy or the remote and\ncredentials configuration in that specified repository.\n\nUse the __gitdir() helper only to find the path to the .git directory\nand pass the resulting path to the 'git ls-remote' and 'for-each-ref'\nexecutions that list remote refs.  While modifying that 'for-each-ref'\nline, remove the superfluous disambiguating doubledash.\n\nDon't use __gitdir() to check that the given remote is on the file\nsystem: basically it performs only a single if statement for us at the\nconsiderable cost of fork()ing a subshell for a command substitution.\nWe are better off to perform all the necessary checks of the remote in\n__git_refs().\n\nThough __git_refs() was the last remaining callsite that passed a\nremote to __gitdir(), don't delete __gitdir()'s remote-handling part\nyet, just in case some users' custom completion scriptlets depend on\nit.\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n contrib/completion/git-completion.bash | 22 +++++++++++++++++-----\n t/t9902-completion.sh                  |  4 ++--\n 2 files changed, 19 insertions(+), 7 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 295f6de24..7d7e8b9d9 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -342,9 +342,21 @@ __git_tags ()\n #    'git checkout's tracking DWIMery (optional; ignored, if set but empty).\n __git_refs ()\n {\n-\tlocal i hash dir=\"$(__gitdir \"${1-}\")\" track=\"${2-}\"\n+\tlocal i hash dir=\"$(__gitdir)\" track=\"${2-}\"\n+\tlocal list_refs_from=path remote=\"${1-}\"\n \tlocal format refs pfx\n-\tif [ -d \"$dir\" ]; then\n+\n+\tif [ -n \"$remote\" ]; then\n+\t\tif [ -d \"$remote/.git\" ]; then\n+\t\t\tdir=\"$remote/.git\"\n+\t\telif [ -d \"$remote\" ]; then\n+\t\t\tdir=\"$remote\"\n+\t\telse\n+\t\t\tlist_refs_from=remote\n+\t\tfi\n+\tfi\n+\n+\tif [ \"$list_refs_from\" = path ] && [ -d \"$dir\" ]; then\n \t\tcase \"$cur\" in\n \t\trefs|refs/*)\n \t\t\tformat=\"refname\"\n@@ -381,7 +393,7 @@ __git_refs ()\n \tfi\n \tcase \"$cur\" in\n \trefs|refs/*)\n-\t\tgit ls-remote \"$dir\" \"$cur*\" 2>/dev/null | \\\n+\t\tgit --git-dir=\"$dir\" ls-remote \"$remote\" \"$cur*\" 2>/dev/null | \\\n \t\twhile read -r hash i; do\n \t\t\tcase \"$i\" in\n \t\t\t*^{}) ;;\n@@ -391,8 +403,8 @@ __git_refs ()\n \t\t;;\n \t*)\n \t\techo \"HEAD\"\n-\t\tgit for-each-ref --format=\"%(refname:short)\" -- \\\n-\t\t\t\"refs/remotes/$dir/\" 2>/dev/null | sed -e \"s#^$dir/##\"\n+\t\tgit --git-dir=\"$dir\" for-each-ref --format=\"%(refname:short)\" \\\n+\t\t\t\"refs/remotes/$remote/\" 2>/dev/null | sed -e \"s#^$remote/##\"\n \t\t;;\n \tesac\n }\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex 7667baabf..6e64cd6ba 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -486,7 +486,7 @@ test_expect_success '__git_refs - configured remote - full refs' '\n \ttest_cmp expected \"$actual\"\n '\n \n-test_expect_failure '__git_refs - configured remote - repo given on the command line' '\n+test_expect_success '__git_refs - configured remote - repo given on the command line' '\n \tcat >expected <<-EOF &&\n \tHEAD\n \tbranch-in-other\n@@ -501,7 +501,7 @@ test_expect_failure '__git_refs - configured remote - repo given on the command\n \ttest_cmp expected \"$actual\"\n '\n \n-test_expect_failure '__git_refs - configured remote - full refs - repo given on the command line' '\n+test_expect_success '__git_refs - configured remote - full refs - repo given on the command line' '\n \tcat >expected <<-EOF &&\n \trefs/heads/branch-in-other\n \trefs/heads/master-in-other\n-- \n2.11.0.555.g967c1bcb3\n\n"},{"id":"310767","messageId":"20170203024829.8071-15-szeder.dev@gmail.com","threadId":"45030","inReplyTo":"20170203024829.8071-1-szeder.dev@gmail.com","subject":"[PATCHv2 14/21] completion: fix completion after 'git -C <path>'","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-03T02:48:22Z","receivedAt":"2017-02-03T02:50:12Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"The main completion function finds the name of the git command by\niterating through all the words on the command line in search for the\nfirst non-option-looking word.  As it is not aware of 'git -C's\nmandatory path argument, if the '-C <path>' option is present, 'path'\nwill be the first such word and it will be mistaken for a git command.\nThis breaks completion in various ways:\n\n - If 'path' happens to match one of the commands supported by the\n   completion script, then options of that command will be offered.\n\n - If 'path' doesn't match a supported command and doesn't contain any\n   characters not allowed in Bash identifier names, then the\n   completion script does basically nothing and Bash in turn falls\n   back to filename completion for all subsequent words.\n\n - Otherwise, if 'path' does contain such an unallowed character, then\n   it leads to a more or less ugly error message in the middle of the\n   command line.  The standard '/' directory separator is such a\n   character, and it happens to trigger one of the uglier errors:\n\n     $ git -C some/path <TAB>sh.exe\": declare: `_git_some/path': not a valid identifier\n     error: invalid key: alias.some/path\n\nFix this by skipping 'git -C's mandatory path argument while iterating\nover the words on the command line.  Extend the relevant test with\nthis case and, while at it, with cases that needed similar treatment\nin the past ('--git-dir', '-c', '--work-tree' and '--namespace').\n\nAdditionally, silence the standard error of the 'declare' builtins\nlooking for the completion function associated with the git command\nand of the 'git config' query for the aliased command.  So if git ever\nlearns a new option with a mandatory argument in the future, then,\nthough the completion script will again misbehave, at least the\ncommand line will not be utterly disrupted by those error messages.\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n contrib/completion/git-completion.bash | 8 ++++----\n t/t9902-completion.sh                  | 7 ++++++-\n 2 files changed, 10 insertions(+), 5 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 7d25b33b8..ac5eb9ced 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -816,7 +816,7 @@ __git_aliases ()\n __git_aliased_command ()\n {\n \tlocal word cmdline=$(git --git-dir=\"$(__gitdir)\" \\\n-\t\tconfig --get \"alias.$1\")\n+\t\tconfig --get \"alias.$1\" 2>/dev/null)\n \tfor word in $cmdline; do\n \t\tcase \"$word\" in\n \t\t\\!gitk|gitk)\n@@ -2800,7 +2800,7 @@ __git_main ()\n \t\t--git-dir)   ((c++)) ; __git_dir=\"${words[c]}\" ;;\n \t\t--bare)      __git_dir=\".\" ;;\n \t\t--help) command=\"help\"; break ;;\n-\t\t-c|--work-tree|--namespace) ((c++)) ;;\n+\t\t-c|-C|--work-tree|--namespace) ((c++)) ;;\n \t\t-*) ;;\n \t\t*) command=\"$i\"; break ;;\n \t\tesac\n@@ -2844,13 +2844,13 @@ __git_main ()\n \tfi\n \n \tlocal completion_func=\"_git_${command//-/_}\"\n-\tdeclare -f $completion_func >/dev/null && $completion_func && return\n+\tdeclare -f $completion_func >/dev/null 2>/dev/null && $completion_func && return\n \n \tlocal expansion=$(__git_aliased_command \"$command\")\n \tif [ -n \"$expansion\" ]; then\n \t\twords[1]=$expansion\n \t\tcompletion_func=\"_git_${expansion//-/_}\"\n-\t\tdeclare -f $completion_func >/dev/null && $completion_func\n+\t\tdeclare -f $completion_func >/dev/null 2>/dev/null && $completion_func\n \tfi\n }\n \ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex 500505dca..be2ed8704 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -755,7 +755,12 @@ test_expect_success 'general options plus command' '\n \ttest_completion \"git --namespace=foo check\" \"checkout \" &&\n \ttest_completion \"git --paginate check\" \"checkout \" &&\n \ttest_completion \"git --info-path check\" \"checkout \" &&\n-\ttest_completion \"git --no-replace-objects check\" \"checkout \"\n+\ttest_completion \"git --no-replace-objects check\" \"checkout \" &&\n+\ttest_completion \"git --git-dir some/path check\" \"checkout \" &&\n+\ttest_completion \"git -c conf.var=value check\" \"checkout \" &&\n+\ttest_completion \"git -C some/path check\" \"checkout \" &&\n+\ttest_completion \"git --work-tree some/path check\" \"checkout \" &&\n+\ttest_completion \"git --namespace name/space check\" \"checkout \"\n '\n \n test_expect_success 'git --help completion' '\n-- \n2.11.0.555.g967c1bcb3\n\n"},{"id":"310768","messageId":"20170203024829.8071-7-szeder.dev@gmail.com","threadId":"45030","inReplyTo":"20170203024829.8071-1-szeder.dev@gmail.com","subject":"[PATCHv2 06/21] completion tests: add tests for the __git_refs() helper function","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-03T02:48:14Z","receivedAt":"2017-02-03T02:50:19Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"Check how __git_refs() lists refs in different scenarios, i.e.\n\n  - short and full refs,\n  - from a local or from a remote repository,\n  - remote specified via path, name or URL,\n  - with or without a repository specified on the command line,\n  - non-existing remote,\n  - unique remote branches for 'git checkout's tracking DWIMery,\n  - not in a git repository, and\n  - interesting combinations of the above.\n\nSeven of these tests expect failure, mostly demonstrating bugs related\nto listing refs from a remote repository:\n\n  - ignoring the repository specified on the command line (2 tests),\n  - listing refs from the wrong place when the name of a configured\n    remote happens to match a directory,\n  - listing only 'HEAD' but no short refs from a remote given as URL,\n  - listing 'HEAD' even from non-existing remotes (2 tests), and\n  - listing 'HEAD' when not in a repository.\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n t/t9902-completion.sh | 265 +++++++++++++++++++++++++++++++++++++++++++++++++-\n 1 file changed, 264 insertions(+), 1 deletion(-)\n\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex f7f7d49fb..7956cb9b1 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -365,6 +365,269 @@ test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'setup for ref completion' '\n+\tgit commit --allow-empty -m initial &&\n+\tgit branch matching-branch &&\n+\tgit tag matching-tag &&\n+\t(\n+\t\tcd otherrepo &&\n+\t\tgit commit --allow-empty -m initial &&\n+\t\tgit branch -m master master-in-other &&\n+\t\tgit branch branch-in-other &&\n+\t\tgit tag tag-in-other\n+\t) &&\n+\tgit remote add other \"$ROOT/otherrepo/.git\" &&\n+\tgit fetch --no-tags other &&\n+\trm -f .git/FETCH_HEAD &&\n+\tgit init thirdrepo\n+'\n+\n+test_expect_success '__git_refs - simple' '\n+\tcat >expected <<-EOF &&\n+\tHEAD\n+\tmaster\n+\tmatching-branch\n+\tother/branch-in-other\n+\tother/master-in-other\n+\tmatching-tag\n+\tEOF\n+\t(\n+\t\tcur= &&\n+\t\t__git_refs >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success '__git_refs - full refs' '\n+\tcat >expected <<-EOF &&\n+\trefs/heads/master\n+\trefs/heads/matching-branch\n+\tEOF\n+\t(\n+\t\tcur=refs/heads/ &&\n+\t\t__git_refs >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success '__git_refs - repo given on the command line' '\n+\tcat >expected <<-EOF &&\n+\tHEAD\n+\tbranch-in-other\n+\tmaster-in-other\n+\ttag-in-other\n+\tEOF\n+\t(\n+\t\t__git_dir=\"$ROOT/otherrepo/.git\" &&\n+\t\tcur= &&\n+\t\t__git_refs >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success '__git_refs - remote on local file system' '\n+\tcat >expected <<-EOF &&\n+\tHEAD\n+\tbranch-in-other\n+\tmaster-in-other\n+\ttag-in-other\n+\tEOF\n+\t(\n+\t\tcur= &&\n+\t\t__git_refs otherrepo >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success '__git_refs - remote on local file system - full refs' '\n+\tcat >expected <<-EOF &&\n+\trefs/heads/branch-in-other\n+\trefs/heads/master-in-other\n+\trefs/tags/tag-in-other\n+\tEOF\n+\t(\n+\t\tcur=refs/ &&\n+\t\t__git_refs otherrepo >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success '__git_refs - configured remote' '\n+\tcat >expected <<-EOF &&\n+\tHEAD\n+\tbranch-in-other\n+\tmaster-in-other\n+\tEOF\n+\t(\n+\t\tcur= &&\n+\t\t__git_refs other >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success '__git_refs - configured remote - full refs' '\n+\tcat >expected <<-EOF &&\n+\trefs/heads/branch-in-other\n+\trefs/heads/master-in-other\n+\trefs/tags/tag-in-other\n+\tEOF\n+\t(\n+\t\tcur=refs/ &&\n+\t\t__git_refs other >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_failure '__git_refs - configured remote - repo given on the command line' '\n+\tcat >expected <<-EOF &&\n+\tHEAD\n+\tbranch-in-other\n+\tmaster-in-other\n+\tEOF\n+\t(\n+\t\tcd thirdrepo &&\n+\t\t__git_dir=\"$ROOT/.git\" &&\n+\t\tcur= &&\n+\t\t__git_refs other >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_failure '__git_refs - configured remote - full refs - repo given on the command line' '\n+\tcat >expected <<-EOF &&\n+\trefs/heads/branch-in-other\n+\trefs/heads/master-in-other\n+\trefs/tags/tag-in-other\n+\tEOF\n+\t(\n+\t\tcd thirdrepo &&\n+\t\t__git_dir=\"$ROOT/.git\" &&\n+\t\tcur=refs/ &&\n+\t\t__git_refs other >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_failure '__git_refs - configured remote - remote name matches a directory' '\n+\tcat >expected <<-EOF &&\n+\tHEAD\n+\tbranch-in-other\n+\tmaster-in-other\n+\tEOF\n+\tmkdir other &&\n+\ttest_when_finished \"rm -rf other\" &&\n+\t(\n+\t\tcur= &&\n+\t\t__git_refs other >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_failure '__git_refs - URL remote' '\n+\tcat >expected <<-EOF &&\n+\tHEAD\n+\tbranch-in-other\n+\tmaster-in-other\n+\ttag-in-other\n+\tEOF\n+\t(\n+\t\tcur= &&\n+\t\t__git_refs \"file://$ROOT/otherrepo/.git\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success '__git_refs - URL remote - full refs' '\n+\tcat >expected <<-EOF &&\n+\trefs/heads/branch-in-other\n+\trefs/heads/master-in-other\n+\trefs/tags/tag-in-other\n+\tEOF\n+\t(\n+\t\tcur=refs/ &&\n+\t\t__git_refs \"file://$ROOT/otherrepo/.git\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_failure '__git_refs - non-existing remote' '\n+\t(\n+\t\tcur= &&\n+\t\t__git_refs non-existing >\"$actual\"\n+\t) &&\n+\ttest_must_be_empty \"$actual\"\n+'\n+\n+test_expect_success '__git_refs - non-existing remote - full refs' '\n+\t(\n+\t\tcur=refs/ &&\n+\t\t__git_refs non-existing >\"$actual\"\n+\t) &&\n+\ttest_must_be_empty \"$actual\"\n+'\n+\n+test_expect_failure '__git_refs - non-existing URL remote' '\n+\t(\n+\t\tcur= &&\n+\t\t__git_refs \"file://$ROOT/non-existing\" >\"$actual\"\n+\t) &&\n+\ttest_must_be_empty \"$actual\"\n+'\n+\n+test_expect_success '__git_refs - non-existing URL remote - full refs' '\n+\t(\n+\t\tcur=refs/ &&\n+\t\t__git_refs \"file://$ROOT/non-existing\" >\"$actual\"\n+\t) &&\n+\ttest_must_be_empty \"$actual\"\n+'\n+\n+test_expect_failure '__git_refs - not in a git repository' '\n+\t(\n+\t\tGIT_CEILING_DIRECTORIES=\"$ROOT\" &&\n+\t\texport GIT_CEILING_DIRECTORIES &&\n+\t\tcd subdir &&\n+\t\tcur= &&\n+\t\t__git_refs >\"$actual\"\n+\t) &&\n+\ttest_must_be_empty \"$actual\"\n+'\n+\n+test_expect_success '__git_refs - unique remote branches for git checkout DWIMery' '\n+\tcat >expected <<-EOF &&\n+\tHEAD\n+\tmaster\n+\tmatching-branch\n+\tother/ambiguous\n+\tother/branch-in-other\n+\tother/master-in-other\n+\tremote/ambiguous\n+\tremote/branch-in-remote\n+\tmatching-tag\n+\tbranch-in-other\n+\tbranch-in-remote\n+\tmaster-in-other\n+\tEOF\n+\tfor remote_ref in refs/remotes/other/ambiguous \\\n+\t\trefs/remotes/remote/ambiguous \\\n+\t\trefs/remotes/remote/branch-in-remote\n+\tdo\n+\t\tgit update-ref $remote_ref master &&\n+\t\ttest_when_finished \"git update-ref -d $remote_ref\"\n+\tdone &&\n+\t(\n+\t\tcur= &&\n+\t\t__git_refs \"\" 1 >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'teardown after ref completion' '\n+\tgit branch -d matching-branch &&\n+\tgit tag -d matching-tag &&\n+\tgit remote remove other\n+'\n+\n test_expect_success '__git_get_config_variables' '\n \tcat >expect <<-EOF &&\n \tname-1\n@@ -483,7 +746,7 @@ test_expect_success 'git --help completion' '\n \ttest_completion \"git --help core\" \"core-tutorial \"\n '\n \n-test_expect_success 'setup for ref completion' '\n+test_expect_success 'setup for integration tests' '\n \techo content >file1 &&\n \techo more >file2 &&\n \tgit add file1 file2 &&\n-- \n2.11.0.555.g967c1bcb3\n\n"},{"id":"310769","messageId":"20170203024829.8071-2-szeder.dev@gmail.com","threadId":"45030","inReplyTo":"20170203024829.8071-1-szeder.dev@gmail.com","subject":"[PATCHv2 01/21] completion: improve __git_refs()'s in-code documentation","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-03T02:48:09Z","receivedAt":"2017-02-03T02:50:20Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"That \"first argument is passed to __gitdir()\" statement in particular\nis not really helpful, and after this series it won't be the case\nanyway.\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n contrib/completion/git-completion.bash | 8 +++++---\n 1 file changed, 5 insertions(+), 3 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 6721ff80f..ee6fb2259 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -332,9 +332,11 @@ __git_tags ()\n \tfi\n }\n \n-# __git_refs accepts 0, 1 (to pass to __gitdir), or 2 arguments\n-# presence of 2nd argument means use the guess heuristic employed\n-# by checkout for tracking branches\n+# Lists refs from the local (by default) or from a remote repository.\n+# It accepts 0, 1 or 2 arguments:\n+# 1: The remote to list refs from (optional; ignored, if set but empty).\n+# 2: In addition to local refs, list unique branches from refs/remotes/ for\n+#    'git checkout's tracking DWIMery (optional; ignored, if set but empty).\n __git_refs ()\n {\n \tlocal i hash dir=\"$(__gitdir \"${1-}\")\" track=\"${2-}\"\n-- \n2.11.0.555.g967c1bcb3\n\n"},{"id":"310945","messageId":"xmqq8tpj0zzg.fsf@gitster.mtv.corp.google.com","threadId":"45030","inReplyTo":"20170203024829.8071-1-szeder.dev@gmail.com","subject":"Re: [PATCHv2 00/21] completion: various __gitdir()-related improvements","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2017-02-06T21:29:55Z","receivedAt":"2017-02-06T21:30:01Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"SZEDER Gábor <szeder.dev@gmail.com> writes:\n\n> This is a long overdue reroll of a bunch of bugfixes, cleanups and\n> optimizations related to how the completion script finds the path to\n> the repository and how it uses that path.  Most importantly this\n> series adds support for completion following 'git -C path', and it\n> eliminates a few subshells and git processes, for the sake of\n> fork()+exec() challenged OSes.\n\nThanks. Queued.\n"},{"id":"311419","messageId":"20170213192036.10671-1-szeder.dev@gmail.com","threadId":"45030","inReplyTo":"20170203024829.8071-18-szeder.dev@gmail.com","subject":"[PATCH] completion: restore removed line continuating backslash","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-13T19:20:36Z","receivedAt":"2017-02-13T19:21:09Z","isPatch":true,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"Recent commit 1cd23e9e0 (completion: don't use __gitdir() for git\ncommands, 2017-02-03) rewrapped a couple of long lines, and while\ndoing so it inadvertently removed a '\\' from the end of a line, thus\nbreaking completion for 'git config remote.name.push <TAB>'.\n\nSigned-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n---\n\nI wanted this to be a fixup!, but then noticed that this series is\nalready in next, hence the proper commit message.\n\nI see the last What's cooking marks this series as \"will merge to\nmaster\".  That doesn't mean that it will be in v2.12, does it?\n\n contrib/completion/git-completion.bash | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 993085af6..f2b294aac 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -2086,7 +2086,7 @@ _git_config ()\n \tremote.*.push)\n \t\tlocal remote=\"${prev#remote.}\"\n \t\tremote=\"${remote%.push}\"\n-\t\t__gitcomp_nl \"$(__git for-each-ref\n+\t\t__gitcomp_nl \"$(__git for-each-ref \\\n \t\t\t--format='%(refname):%(refname)' refs/heads)\"\n \t\treturn\n \t\t;;\n-- \n2.11.1.499.ge87add2e7\n\n"},{"id":"311437","messageId":"xmqqfujhddl2.fsf@gitster.mtv.corp.google.com","threadId":"45030","inReplyTo":"20170213192036.10671-1-szeder.dev@gmail.com","subject":"Re: [PATCH] completion: restore removed line continuating backslash","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2017-02-13T20:45:45Z","receivedAt":"2017-02-13T20:45:51Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"SZEDER Gábor <szeder.dev@gmail.com> writes:\n\n> Recent commit 1cd23e9e0 (completion: don't use __gitdir() for git\n> commands, 2017-02-03) rewrapped a couple of long lines, and while\n> doing so it inadvertently removed a '\\' from the end of a line, thus\n> breaking completion for 'git config remote.name.push <TAB>'.\n>\n> Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n> ---\n>\n> I wanted this to be a fixup!, but then noticed that this series is\n> already in next, hence the proper commit message.\n\nWe get \"--format=... : command not found\"?\nThanks for a set of sharp eyes.\n\n> I see the last What's cooking marks this series as \"will merge to\n> master\".  That doesn't mean that it will be in v2.12, does it?\n\nI actually was hoping that these can go in.  Others that can (or\nshould) wait are marked as \"Will cook in 'next'\".\n\nIf you feel uncomfortable and want these to cook longer, please tell\nme so.\n\nThanks.\n\n>  contrib/completion/git-completion.bash | 2 +-\n>  1 file changed, 1 insertion(+), 1 deletion(-)\n>\n> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\n> index 993085af6..f2b294aac 100644\n> --- a/contrib/completion/git-completion.bash\n> +++ b/contrib/completion/git-completion.bash\n> @@ -2086,7 +2086,7 @@ _git_config ()\n>  \tremote.*.push)\n>  \t\tlocal remote=\"${prev#remote.}\"\n>  \t\tremote=\"${remote%.push}\"\n> -\t\t__gitcomp_nl \"$(__git for-each-ref\n> +\t\t__gitcomp_nl \"$(__git for-each-ref \\\n>  \t\t\t--format='%(refname):%(refname)' refs/heads)\"\n>  \t\treturn\n>  \t\t;;\n"},{"id":"311629","messageId":"CAM0VKjmU57saSfyRuoWfC+UZFNypH1Wp9X33VgzPq9fatD=qtg@mail.gmail.com","threadId":"45030","inReplyTo":"xmqqfujhddl2.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH] completion: restore removed line continuating backslash","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-15T01:35:28Z","receivedAt":"2017-02-15T01:35:40Z","isPatch":true,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"On Mon, Feb 13, 2017 at 9:45 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> SZEDER Gábor <szeder.dev@gmail.com> writes:\n>\n>> Recent commit 1cd23e9e0 (completion: don't use __gitdir() for git\n>> commands, 2017-02-03) rewrapped a couple of long lines, and while\n>> doing so it inadvertently removed a '\\' from the end of a line, thus\n>> breaking completion for 'git config remote.name.push <TAB>'.\n>>\n>> Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>\n>> ---\n>>\n>> I wanted this to be a fixup!, but then noticed that this series is\n>> already in next, hence the proper commit message.\n>\n> We get \"--format=... : command not found\"?\n\nYeah, that's the one.\n\n> Thanks for a set of sharp eyes.\n\nHeh.  Sharp?!  It took over five minutes to notice after I first got\nthat error...\n\nFurthermore, that '\\' was already missing in v1, almost a year ago :)\n\n>> I see the last What's cooking marks this series as \"will merge to\n>> master\".  That doesn't mean that it will be in v2.12, does it?\n>\n> I actually was hoping that these can go in.  Others that can (or\n> should) wait are marked as \"Will cook in 'next'\".\n>\n> If you feel uncomfortable and want these to cook longer, please tell\n> me so.\n\nWell, it was mainly my surprise that a 20+ patch series arriving so\nlate that it gets queued on top of -rc0 would still make it into the\nrelease.  However, I have been using this series with only minor\nmodifications for the better part of a year now, so it's unlikely that\nthere will be any big issues with it.  Maybe something small, like\nthis missing '\\', but we will deal with it when it arises.\n\nGábor\n"},{"id":"311636","messageId":"xmqqtw7w41m5.fsf@gitster.mtv.corp.google.com","threadId":"45030","inReplyTo":"CAM0VKjmU57saSfyRuoWfC+UZFNypH1Wp9X33VgzPq9fatD=qtg@mail.gmail.com","subject":"Re: [PATCH] completion: restore removed line continuating backslash","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2017-02-15T02:41:22Z","receivedAt":"2017-02-15T02:41:31Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"SZEDER Gábor <szeder.dev@gmail.com> writes:\n\n>> If you feel uncomfortable and want these to cook longer, please tell\n>> me so.\n>\n> Well, it was mainly my surprise that a 20+ patch series arriving so\n> late that it gets queued on top of -rc0 would still make it into the\n> release.\n\nIt all depends on what area the changes are about ;-)\n\nNot meaning to make light of your contribution, but if the upcoming\nversion of Git shipped with a slightly broken completion, and the\nbreakage is severe enough, the only thing we need to do is to do a\nmaintenance release that reverts a single script.  Distro packagers\nmay do that for us.  While waiting, the user can choose not to load\nthe completion and can keep using Git just fine.  A broken\n\"mergetool\" would be similar in that a workaround to inspect \"git\ndiff\" is available.  If we break \"pull\" or \"commit\" on the other\nhand, the necessary workaround would be a lot more involved.\n\nSome changes in low-impact areas can wait without reducing the\nend-user value of the new release, but if the risk of regression is\nsmall, I favour merging them, rather than postponing them for one\ncycle, if only to reduce the number of patches that I need to hold\nonto.  Patches to clarify existing documentation fall into this\ncategory.\n\nMy perception of \"risk of regression\" obviously is affected by the\nsize of the series, and 20+ is certainly on the larger side.  But\nother things also come into the picture.  Patches from an author who\nknows the area the patches touch very well and with track record of\nnot causing embarrassing regressions, would make me feel safer than\npatches from others, for example.\n\n"},{"id":"311646","messageId":"CAM0VKjk4YxX_+QzRw3HmnABV9AbkyzFjKau4Q=z4dPQpLzd--g@mail.gmail.com","threadId":"45030","inReplyTo":"xmqqtw7w41m5.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH] completion: restore removed line continuating backslash","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2017-02-15T13:06:47Z","receivedAt":"2017-02-15T13:07:02Z","isPatch":true,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"On Wed, Feb 15, 2017 at 3:41 AM, Junio C Hamano <gitster@pobox.com> wrote:\n> SZEDER Gábor <szeder.dev@gmail.com> writes:\n>\n>>> If you feel uncomfortable and want these to cook longer, please tell\n>>> me so.\n>>\n>> Well, it was mainly my surprise that a 20+ patch series arriving so\n>> late that it gets queued on top of -rc0 would still make it into the\n>> release.\n>\n> It all depends on what area the changes are about ;-)\n\nSure.  I actually wanted to end that email with something like \"and\nit's in contrib anyway :)\", but by the time I got there I forgot about\nit.\n"}]}