{"thread":{"id":"41526","subject":"[PATCH 00/21] completion: __gitdir()-related improvements","startedAt":"2016-02-25T22:50:29Z","lastAt":"2016-05-18T16:58:25Z","messageCount":25,"participants":["SZEDER Gábor","Mike Rappazzo","Jeff King"],"isPatch":true,"patchVersion":1,"patchTotal":21},"messages":[{"id":"279396","messageId":"1456440650-32623-1-git-send-email-szeder@ira.uka.de","threadId":"41526","inReplyTo":null,"subject":"[PATCH 00/21] completion: __gitdir()-related improvements","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2016-02-25T22:50:29Z","receivedAt":"2016-02-25T22:50:29Z","isPatch":true,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"Hi,\n\nhere is a big mixed bag of bugfixes, cleanups and optimizations mostly\nrelated to how we find out the path to the repository and how we use\nthat path.\n\nNoteworthier changes are the following:\n\n  - Patch 18 shuts up error messages from git commands.  This is the\n    one I mentioned some days ago[1].\n\n  - Patches 13-16 make 'git -C <path> <cmd> <TAB>' work as it should,\n    with the help of a new 'git rev-parse' option.\n\n  - Patches 17, 19-21 get rid of a few fork()s and exec()s.\n    Funfact: I submitted initial versions of patches 20-21 already in\n    2012 ;) [2]\n\nTests pass with all 3.x and 4.x Bash versions[3].\n\nThis series is also available from:\n\n  https://github.com/szeder/git completion-gitdir-improvements\n\n\nBest,\nGábor\n\n\n[1] - http://thread.gmane.org/gmane.comp.version-control.git/286074/focus=286078\n[2] - http://thread.gmane.org/gmane.comp.version-control.git/197432/focus=197438\n[3] - In case somebody feels interested, a little proof of concept\n      (with Travis CI integration!):\n\n        https://github.com/szeder/git completion-test-multiple-bash-versions\n\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  rev-parse: add '--absolute-git-dir' option\n  completion: don't offer commands when 'git --opt' needs an argument\n  completion: fix completion after 'git -C <path>'\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                    |  29 +-\n contrib/completion/git-completion.bash | 248 ++++++++++-----\n t/t1500-rev-parse.sh                   |  17 +-\n t/t9902-completion.sh                  | 560 ++++++++++++++++++++++++++++-----\n 5 files changed, 682 insertions(+), 176 deletions(-)\n\n-- \n2.7.2.410.g92cb358\n"},{"id":"279397","messageId":"1456440650-32623-2-git-send-email-szeder@ira.uka.de","threadId":"41526","inReplyTo":"1456440650-32623-1-git-send-email-szeder@ira.uka.de","subject":"[PATCH 01/21] completion: improve __git_refs()'s in-code documentation","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2016-02-25T22:50:30Z","receivedAt":"2016-02-25T22:50:30Z","isPatch":true,"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@ira.uka.de>\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 e3918c87e3ad..0e1fd778bfe8 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 lists 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.7.2.410.g92cb358\n"},{"id":"279398","messageId":"1456440650-32623-3-git-send-email-szeder@ira.uka.de","threadId":"41526","inReplyTo":"1456440650-32623-1-git-send-email-szeder@ira.uka.de","subject":"[PATCH 02/21] completion tests: don't add test cruft to the test repository","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2016-02-25T22:50:31Z","receivedAt":"2016-02-25T22:50:31Z","isPatch":true,"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@ira.uka.de>\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 2ba62fbc178e..cec98a7e814c 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -491,7 +491,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@@ -522,7 +522,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@@ -531,7 +531,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.7.2.410.g92cb358\n"},{"id":"279399","messageId":"1456440650-32623-4-git-send-email-szeder@ira.uka.de","threadId":"41526","inReplyTo":"1456440650-32623-1-git-send-email-szeder@ira.uka.de","subject":"[PATCH 03/21] completion tests: make the $cur variable local to the test helper functions","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2016-02-25T22:50:32Z","receivedAt":"2016-02-25T22:50:32Z","isPatch":true,"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@ira.uka.de>\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 cec98a7e814c..9f591c361fab 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.7.2.410.g92cb358\n"},{"id":"279400","messageId":"1456440650-32623-5-git-send-email-szeder@ira.uka.de","threadId":"41526","inReplyTo":"1456440650-32623-1-git-send-email-szeder@ira.uka.de","subject":"[PATCH 04/21] completion tests: consolidate getting path of current working directory","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2016-02-25T22:50:33Z","receivedAt":"2016-02-25T22:50:33Z","isPatch":true,"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@ira.uka.de>\n---\n t/t9902-completion.sh | 46 ++++++++++++++++++++++------------------------\n 1 file changed, 22 insertions(+), 24 deletions(-)\n\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex 9f591c361fab..447f57b89291 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@@ -259,7 +257,7 @@ test_expect_success SYMLINKS '__gitdir - resulting path avoids symlinks' '\n test_expect_success '__gitdir - not a git repository' '\n \t(\n \t\tcd subdir/subsubdir &&\n-\t\tGIT_CEILING_DIRECTORIES=\"$TRASH_DIRECTORY\" &&\n+\t\tGIT_CEILING_DIRECTORIES=\"$ROOT\" &&\n \t\texport GIT_CEILING_DIRECTORIES &&\n \t\ttest_must_fail __gitdir\n \t)\n-- \n2.7.2.410.g92cb358\n"},{"id":"279401","messageId":"1456440650-32623-6-git-send-email-szeder@ira.uka.de","threadId":"41526","inReplyTo":"1456440650-32623-1-git-send-email-szeder@ira.uka.de","subject":"[PATCH 05/21] completion tests: check __gitdir()'s output in the error cases","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2016-02-25T22:50:34Z","receivedAt":"2016-02-25T22:50:34Z","isPatch":true,"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@ira.uka.de>\n---\n t/t9902-completion.sh | 10 ++++++----\n 1 file changed, 6 insertions(+), 4 deletions(-)\n\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex 447f57b89291..1e8794747efd 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@@ -259,8 +260,9 @@ test_expect_success '__gitdir - not a git repository' '\n \t\tcd subdir/subsubdir &&\n \t\tGIT_CEILING_DIRECTORIES=\"$ROOT\" &&\n \t\texport GIT_CEILING_DIRECTORIES &&\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 '__gitcomp - trailing space - options' '\n-- \n2.7.2.410.g92cb358\n"},{"id":"279402","messageId":"1456440650-32623-7-git-send-email-szeder@ira.uka.de","threadId":"41526","inReplyTo":"1456440650-32623-1-git-send-email-szeder@ira.uka.de","subject":"[PATCH 06/21] completion tests: add tests for the __git_refs() helper function","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2016-02-25T22:50:35Z","receivedAt":"2016-02-25T22:50:35Z","isPatch":true,"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@ira.uka.de>\n---\n t/t9902-completion.sh | 266 ++++++++++++++++++++++++++++++++++++++++++++++++--\n 1 file changed, 257 insertions(+), 9 deletions(-)\n\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex 1e8794747efd..7ab398568594 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -370,6 +370,263 @@ 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+\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+\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) &&\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 init thirdrepo\n+'\n+\n+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+\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/mybranch\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\n+\tmaster\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\n+\tmaster\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\n+\trefs/heads/master\n+\tEOF\n+\t(\n+\t\tcur=refs/heads/ &&\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\n+\tmaster\n+\tEOF\n+\t(\n+\t\tcur= &&\n+\t\t__git_refs remote >\"$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+\tEOF\n+\t(\n+\t\tcur=refs/heads/ &&\n+\t\t__git_refs remote >\"$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\n+\tmaster\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) &&\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\n+\trefs/heads/master\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) &&\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\n+\tmaster\n+\tEOF\n+\tmkdir remote &&\n+\ttest_when_finished \"rm -rf remote\" &&\n+\t(\n+\t\tcur= &&\n+\t\t__git_refs remote >\"$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\n+\tmaster\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\n+\trefs/heads/master\n+\tEOF\n+\t(\n+\t\tcur=refs/heads/ &&\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/heads/ &&\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/heads/ &&\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 - unique remote branches for git checkout DWIMery' '\n+\tcat >expected <<-EOF &&\n+\tHEAD\n+\tmaster\n+\tmybranch\n+\totherremote/ambiguous\n+\totherremote/otherbranch\n+\tremote/ambiguous\n+\tremote/branch\n+\tremote/master\n+\tmytag\n+\tbranch\n+\tmaster\n+\totherbranch\n+\tEOF\n+\tfor remote_ref in refs/remotes/remote/ambiguous \\\n+\t\trefs/remotes/otherremote/ambiguous \\\n+\t\trefs/remotes/otherremote/otherbranch\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_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 'remove configured remote used for refs completion' '\n+\tgit remote remove remote\n+'\n+\n test_expect_success '__git_get_config_variables' '\n \tcat >expect <<-EOF &&\n \tname-1\n@@ -488,15 +745,6 @@ 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-\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-- \n2.7.2.410.g92cb358\n"},{"id":"279403","messageId":"1456440650-32623-8-git-send-email-szeder@ira.uka.de","threadId":"41526","inReplyTo":"1456440650-32623-1-git-send-email-szeder@ira.uka.de","subject":"[PATCH 07/21] completion: ensure that the repository path given on the command line exists","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2016-02-25T22:50:36Z","receivedAt":"2016-02-25T22:50:36Z","isPatch":true,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"The __gitdir() helper function stays silent and returns with error\nwhen it can't find a repository or when the repository given via\n$GIT_DIR doesn't exist.\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 as 'git\n--git-dir=<path>', doesn't exist: __gitdir() still outputs it and\nreturns with success, making completion functions believe that they\noperate on an existing repository.\n\nCheck that the path in $__git_dir exists and return with error if it\ndoesn't.\n\nSigned-off-by: SZEDER Gábor <szeder@ira.uka.de>\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 0e1fd778bfe8..0ec988c0ee26 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 7ab398568594..809856110235 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.7.2.410.g92cb358\n"},{"id":"279404","messageId":"1456440650-32623-9-git-send-email-szeder@ira.uka.de","threadId":"41526","inReplyTo":"1456440650-32623-1-git-send-email-szeder@ira.uka.de","subject":"[PATCH 08/21] completion: fix most spots not respecting 'git --git-dir=<path>'","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2016-02-25T22:50:37Z","receivedAt":"2016-02-25T22:50:37Z","isPatch":true,"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@ira.uka.de>\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 0ec988c0ee26..59dffe7f39c2 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@@ -407,7 +409,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@@ -1138,7 +1140,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@@ -1431,7 +1433,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.7.2.410.g92cb358\n"},{"id":"279405","messageId":"1456440650-32623-10-git-send-email-szeder@ira.uka.de","threadId":"41526","inReplyTo":"1456440650-32623-1-git-send-email-szeder@ira.uka.de","subject":"[PATCH 09/21] completion: respect 'git --git-dir=<path>' when listing remote refs","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2016-02-25T22:50:38Z","receivedAt":"2016-02-25T22:50:38Z","isPatch":true,"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.\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@ira.uka.de>\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 59dffe7f39c2..adc968acea9d 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 from_local=y remote=\"${1-}\"\n \tlocal format refs\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\tfrom_local=n\n+\t\tfi\n+\tfi\n+\n+\tif [ \"$from_local\" = y ] && [ -d \"$dir\" ]; then\n \t\tcase \"$cur\" in\n \t\trefs|refs/*)\n \t\t\tformat=\"refname\"\n@@ -380,7 +392,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@@ -390,8 +402,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 809856110235..f9993d2c005a 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -490,7 +490,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\n@@ -505,7 +505,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\n \trefs/heads/master\n-- \n2.7.2.410.g92cb358\n"},{"id":"279406","messageId":"1456440650-32623-11-git-send-email-szeder@ira.uka.de","threadId":"41526","inReplyTo":"1456440650-32623-1-git-send-email-szeder@ira.uka.de","subject":"[PATCH 10/21] completion: list refs from remote when remote's name matches a directory","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2016-02-25T22:50:39Z","receivedAt":"2016-02-25T22:50:39Z","isPatch":true,"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@ira.uka.de>\n---\n contrib/completion/git-completion.bash | 28 +++++++++++++++++++++++-----\n t/t9902-completion.sh                  | 11 ++++++++++-\n 2 files changed, 33 insertions(+), 6 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex adc968acea9d..b85ab27fb18a 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -347,12 +347,18 @@ __git_refs ()\n \tlocal format refs\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\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\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\tfi\n \tfi\n \n@@ -434,6 +440,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 f9993d2c005a..9f8be9ab1f3b 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -378,6 +378,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 \techo content >file1 &&\n \techo more >file2 &&\n@@ -519,7 +528,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\n-- \n2.7.2.410.g92cb358\n"},{"id":"279408","messageId":"1456440650-32623-12-git-send-email-szeder@ira.uka.de","threadId":"41526","inReplyTo":"1456440650-32623-1-git-send-email-szeder@ira.uka.de","subject":"[PATCH 11/21] completion: don't list 'HEAD' when trying refs completion outside of a repo","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2016-02-25T22:50:40Z","receivedAt":"2016-02-25T22:50:40Z","isPatch":true,"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 exit early if neither is given.\n\nSigned-off-by: SZEDER Gábor <szeder@ira.uka.de>\n---\n contrib/completion/git-completion.bash | 4 ++++\n t/t9902-completion.sh                  | 2 +-\n 2 files changed, 5 insertions(+), 1 deletion(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex b85ab27fb18a..f6ccfb708451 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -346,6 +346,10 @@ __git_refs ()\n \tlocal from_local=y remote=\"${1-}\"\n \tlocal format refs\n \n+\tif [ -z \"$dir\" ] && [ -z \"$remote\" ]; then\n+\t\treturn\n+\tfi\n+\n \tif [ -n \"$remote\" ]; then\n \t\tif __git_is_configured_remote \"$remote\"; then\n \t\t\t# configured remote takes precedence over a\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex 9f8be9ab1f3b..f42a9ba9058f 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -629,7 +629,7 @@ test_expect_success '__git_refs - unique remote branches for git checkout DWIMer\n \ttest_cmp expected \"$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.7.2.410.g92cb358\n"},{"id":"279407","messageId":"1456440650-32623-13-git-send-email-szeder@ira.uka.de","threadId":"41526","inReplyTo":"1456440650-32623-1-git-send-email-szeder@ira.uka.de","subject":"[PATCH 12/21] completion: list short refs from a remote given as a URL","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2016-02-25T22:50:41Z","receivedAt":"2016-02-25T22:50:41Z","isPatch":true,"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@ira.uka.de>\n---\n contrib/completion/git-completion.bash | 22 ++++++++++++++++++----\n t/t9902-completion.sh                  |  6 +++---\n 2 files changed, 21 insertions(+), 7 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex f6ccfb708451..6932d2a276eb 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -338,12 +338,13 @@ __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 lists 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=\"$(__gitdir)\" track=\"${2-}\"\n-\tlocal from_local=y remote=\"${1-}\"\n+\tlocal from_local=y remote=\"${1-}\" named_remote=n\n \tlocal format refs\n \n \tif [ -z \"$dir\" ] && [ -z \"$remote\" ]; then\n@@ -355,6 +356,7 @@ __git_refs ()\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\telse\n \t\t\tif [ -d \"$remote/.git\" ]; then\n \t\t\t\tdir=\"$remote/.git\"\n@@ -411,9 +413,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 [ \"$named_remote\" = y ]; 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\" ;;\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 f42a9ba9058f..a1f69682e5ec 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -543,7 +543,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\n@@ -568,7 +568,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@@ -584,7 +584,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.7.2.410.g92cb358\n"},{"id":"279409","messageId":"1456440650-32623-14-git-send-email-szeder@ira.uka.de","threadId":"41526","inReplyTo":"1456440650-32623-1-git-send-email-szeder@ira.uka.de","subject":"[PATCH 13/21] rev-parse: add '--absolute-git-dir' option","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2016-02-25T22:50:42Z","receivedAt":"2016-02-25T22:50:42Z","isPatch":true,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"Some scripts can benefit from not having to deal with the possibility\nof relative paths to the repository, but the output of 'git rev-parse\n--git-dir' can be a relative path.  Case in point: supporting 'git -C\n<path>' in our Bash completion script turned out to be considerably\nmore difficult, error prone and required more subshells and git\nprocesses when we had to cope with a relative path to the .git\ndirectory.\n\nHelp these use cases 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@ira.uka.de>\n---\n Documentation/git-rev-parse.txt |  4 ++++\n builtin/rev-parse.c             | 29 +++++++++++++++++++++--------\n t/t1500-rev-parse.sh            | 17 ++++++++++-------\n 3 files changed, 35 insertions(+), 15 deletions(-)\n\ndiff --git a/Documentation/git-rev-parse.txt b/Documentation/git-rev-parse.txt\nindex b6c6326cdc7b..fb06e3118570 100644\n--- a/Documentation/git-rev-parse.txt\n+++ b/Documentation/git-rev-parse.txt\n@@ -216,6 +216,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 cf8487b3b95f..90a4dd6032c0 100644\n--- a/builtin/rev-parse.c\n+++ b/builtin/rev-parse.c\n@@ -744,17 +744,30 @@ 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\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\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 48ee07779d64..617fcd821309 100755\n--- a/t/t1500-rev-parse.sh\n+++ b/t/t1500-rev-parse.sh\n@@ -31,23 +31,26 @@ test_rev_parse() {\n \t\"test '$1' = \\\"\\$(git rev-parse --git-dir)\\\"\"\n \tshift\n \t[ $# -eq 0 ] && return\n+\n+\ttest_expect_success \"$name: absolute-git-dir\" \\\n+\t\"verbose test '$1' = \\\"\\$(git rev-parse --absolute-git-dir)\\\"\"\n }\n \n-# label is-bare is-inside-git is-inside-work prefix git-dir\n+# label is-bare is-inside-git is-inside-work prefix git-dir absolute-git-dir\n \n ROOT=$(pwd)\n \n-test_rev_parse toplevel false false true '' .git\n+test_rev_parse toplevel false false true '' .git \"$ROOT/.git\"\n \n cd .git || exit 1\n-test_rev_parse .git/ false true false '' .\n+test_rev_parse .git/ false true false '' . \"$ROOT/.git\"\n cd objects || exit 1\n-test_rev_parse .git/objects/ false true false '' \"$ROOT/.git\"\n+test_rev_parse .git/objects/ false true false '' \"$ROOT/.git\" \"$ROOT/.git\"\n cd ../.. || exit 1\n \n mkdir -p sub/dir || exit 1\n cd sub/dir || exit 1\n-test_rev_parse subdirectory false false true sub/dir/ \"$ROOT/.git\"\n+test_rev_parse subdirectory false false true sub/dir/ \"$ROOT/.git\" \"$ROOT/.git\"\n cd ../.. || exit 1\n \n git config core.bare true\n@@ -63,7 +66,7 @@ GIT_CONFIG=\"$(pwd)\"/../.git/config\n export GIT_DIR GIT_CONFIG\n \n git config core.bare false\n-test_rev_parse 'GIT_DIR=../.git, core.bare = false' false false true ''\n+test_rev_parse 'GIT_DIR=../.git, core.bare = false' false false true '' \"../.git\" \"$ROOT/.git\"\n \n git config core.bare true\n test_rev_parse 'GIT_DIR=../.git, core.bare = true' true false false ''\n@@ -76,7 +79,7 @@ GIT_DIR=../repo.git\n GIT_CONFIG=\"$(pwd)\"/../repo.git/config\n \n git config core.bare false\n-test_rev_parse 'GIT_DIR=../repo.git, core.bare = false' false false true ''\n+test_rev_parse 'GIT_DIR=../repo.git, core.bare = false' false false true '' \"../repo.git\" \"$ROOT/repo.git\"\n \n git config core.bare true\n test_rev_parse 'GIT_DIR=../repo.git, core.bare = true' true false false ''\n-- \n2.7.2.410.g92cb358\n"},{"id":"279410","messageId":"1456440650-32623-15-git-send-email-szeder@ira.uka.de","threadId":"41526","inReplyTo":"1456440650-32623-1-git-send-email-szeder@ira.uka.de","subject":"[PATCH 14/21] completion: don't offer commands when 'git --opt' needs an argument","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2016-02-25T22:50:43Z","receivedAt":"2016-02-25T22:50:43Z","isPatch":true,"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 completion scripts in\n    general is to not offer anything, but fall back to filename\n    completion.\n\nSigned-off-by: SZEDER Gábor <szeder@ira.uka.de>\n---\n contrib/completion/git-completion.bash | 8 ++++++++\n 1 file changed, 8 insertions(+)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 6932d2a276eb..6027733a4b46 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -2666,6 +2666,14 @@ __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\treturn\n+\t\t\t;;\n+\t\t-c|--namespace)\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.7.2.410.g92cb358\n"},{"id":"279411","messageId":"1456440650-32623-16-git-send-email-szeder@ira.uka.de","threadId":"41526","inReplyTo":"1456440650-32623-1-git-send-email-szeder@ira.uka.de","subject":"[PATCH 15/21] completion: fix completion after 'git -C <path>'","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2016-02-25T22:50:44Z","receivedAt":"2016-02-25T22:50:44Z","isPatch":true,"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@ira.uka.de>\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 6027733a4b46..5e5437d7a5c2 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -818,7 +818,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@@ -2658,7 +2658,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@@ -2699,13 +2699,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 a1f69682e5ec..d172199ed256 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -754,7 +754,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.7.2.410.g92cb358\n"},{"id":"279412","messageId":"1456440650-32623-17-git-send-email-szeder@ira.uka.de","threadId":"41526","inReplyTo":"1456440650-32623-1-git-send-email-szeder@ira.uka.de","subject":"[PATCH 16/21] completion: respect 'git -C <path>'","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2016-02-25T22:50:45Z","receivedAt":"2016-02-25T22:50:45Z","isPatch":true,"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    and\n\n  - a few git commands executed in the completion script do actually\n    care about from which directory they are executed from.\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@ira.uka.de>\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 5e5437d7a5c2..9ffea9580ff7 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@@ -521,7 +525,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@@ -2650,6 +2654,7 @@ _git_whatchanged ()\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@@ -2658,7 +2663,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 d172199ed256..f68b3383caa3 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@@ -775,6 +856,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 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.7.2.410.g92cb358\n"},{"id":"279413","messageId":"1456440650-32623-18-git-send-email-szeder@ira.uka.de","threadId":"41526","inReplyTo":"1456440650-32623-1-git-send-email-szeder@ira.uka.de","subject":"[PATCH 17/21] completion: don't use __gitdir() for git commands","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2016-02-25T22:50:46Z","receivedAt":"2016-02-25T22:50:46Z","isPatch":true,"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.\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@ira.uka.de>\n---\n contrib/completion/git-completion.bash | 58 ++++++++++++++++++----------------\n 1 file changed, 30 insertions(+), 28 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 9ffea9580ff7..c496f4026fc8 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@@ -387,14 +391,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=\"%($format)\" \\\n+\t\t__git_dir=\"$dir\" __git for-each-ref --format=\"%($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@@ -408,7 +412,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@@ -419,10 +423,10 @@ __git_refs ()\n \t*)\n \t\tif [ \"$named_remote\" = y ]; 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@@ -449,7 +453,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@@ -459,7 +463,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@@ -525,7 +529,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@@ -803,7 +807,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@@ -821,8 +825,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@@ -1192,7 +1195,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@@ -1778,7 +1781,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@@ -1810,7 +1813,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@@ -1855,7 +1858,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@@ -1890,9 +1893,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@@ -2475,12 +2477,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.7.2.410.g92cb358\n"},{"id":"279414","messageId":"1456440650-32623-19-git-send-email-szeder@ira.uka.de","threadId":"41526","inReplyTo":"1456440650-32623-1-git-send-email-szeder@ira.uka.de","subject":"[PATCH 18/21] completion: consolidate silencing errors from git commands","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2016-02-25T22:50:47Z","receivedAt":"2016-02-25T22:50:47Z","isPatch":true,"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@ira.uka.de>\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 c496f4026fc8..2d0bd018701e 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@@ -412,7 +412,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@@ -424,10 +424,10 @@ __git_refs ()\n \t\tif [ \"$named_remote\" = y ]; 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@@ -453,7 +453,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@@ -529,7 +529,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@@ -807,7 +807,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@@ -825,7 +825,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@@ -1780,9 +1780,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@@ -1812,9 +1810,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@@ -1858,7 +1854,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.7.2.410.g92cb358\n"},{"id":"279416","messageId":"1456440650-32623-20-git-send-email-szeder@ira.uka.de","threadId":"41526","inReplyTo":"1456440650-32623-1-git-send-email-szeder@ira.uka.de","subject":"[PATCH 19/21] completion: don't guard git executions with __gitdir()","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2016-02-25T22:50:48Z","receivedAt":"2016-02-25T22:50:48Z","isPatch":true,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"Three completion functions first run __gitdir() and check that the\npath it outputs exists, i.e. that there is a git repository, and run a\ngit command 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@ira.uka.de>\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 2d0bd018701e..605ab84296a2 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.7.2.410.g92cb358\n"},{"id":"279415","messageId":"1456440650-32623-21-git-send-email-szeder@ira.uka.de","threadId":"41526","inReplyTo":"1456440650-32623-1-git-send-email-szeder@ira.uka.de","subject":"[PATCH 20/21] completion: extract repository discovery from __gitdir()","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2016-02-25T22:50:49Z","receivedAt":"2016-02-25T22:50:49Z","isPatch":true,"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\nprevent it from leaking into the environment and influencing\nsubsequent completions.\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@ira.uka.de>\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 605ab84296a2..69000a33bcfa 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@@ -2641,7 +2650,7 @@ _git_whatchanged ()\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@@ -2710,6 +2719,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 f68b3383caa3..d44e72066328 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@@ -455,7 +461,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@@ -464,8 +472,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.7.2.410.g92cb358\n"},{"id":"279417","messageId":"1456440650-32623-22-git-send-email-szeder@ira.uka.de","threadId":"41526","inReplyTo":"1456440650-32623-1-git-send-email-szeder@ira.uka.de","subject":"[PATCH 21/21] completion: cache the path to the repository","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2016-02-25T22:50:50Z","receivedAt":"2016-02-25T22:50:50Z","isPatch":true,"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.\n\nSigned-off-by: SZEDER Gábor <szeder@ira.uka.de>\n---\n contrib/completion/git-completion.bash |  46 ++++++----\n t/t9902-completion.sh                  | 155 +++++++++++++++++++++------------\n 2 files changed, 126 insertions(+), 75 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 69000a33bcfa..7300a2b64bab 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 from_local=y remote=\"${1-}\" named_remote=n\n \tlocal format refs\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@@ -460,8 +469,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@@ -915,8 +924,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@@ -999,7 +1008,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@@ -1104,8 +1114,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@@ -1486,10 +1496,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@@ -1727,11 +1737,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 --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\"\n \t\treturn\n \tfi\n@@ -2355,8 +2366,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@@ -2720,9 +2731,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 d44e72066328..458850641635 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -131,213 +131,216 @@ 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 \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) &&\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\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 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,21 +348,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__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 - not a git repository' '\n+test_expect_success '__git_find_repo_path - not a git repository' '\n \t(\n \t\tcd subdir/subsubdir &&\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+\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.7.2.410.g92cb358\n"},{"id":"284508","messageId":"CANoM8SXO_Rz_CVOz9ptsaVCzcQ2D1FQrSuFFW4vZ4SdRYMzD=w@mail.gmail.com","threadId":"41526","inReplyTo":"1456440650-32623-14-git-send-email-szeder@ira.uka.de","subject":"Re: [PATCH 13/21] rev-parse: add '--absolute-git-dir' option","fromName":"Mike Rappazzo","fromEmail":"rappazzo@gmail.com","sentAt":"2016-04-26T02:33:13Z","receivedAt":"2016-04-26T02:33:13Z","isPatch":true,"sender":{"key":"rappazzo@gmail.com","avatar":"https://avatars.githubusercontent.com/u/525287?v=4"},"body":"On Thu, Feb 25, 2016 at 5:54 PM SZEDER Gábor <szeder@ira.uka.de> wrote:\n>\n> Some scripts can benefit from not having to deal with the possibility\n> of relative paths to the repository, but the output of 'git rev-parse\n> --git-dir' can be a relative path.  Case in point: supporting 'git -C\n> <path>' in our Bash completion script turned out to be considerably\n> more difficult, error prone and required more subshells and git\n> processes when we had to cope with a relative path to the .git\n> directory.\n>\n> Help these use cases and teach 'git rev-parse' a new\n> '--absolute-git-dir' option which always outputs a canonicalized\n> absolute path to the .git directory, regardless of whether the path is\n> discovered automatically or is specified via $GIT_DIR or 'git\n> --git-dir=<path>'.\n>\n> Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>\n> ---\n>  Documentation/git-rev-parse.txt |  4 ++++\n>  builtin/rev-parse.c             | 29 +++++++++++++++++++++--------\n>  t/t1500-rev-parse.sh            | 17 ++++++++++-------\n>  3 files changed, 35 insertions(+), 15 deletions(-)\n>\n> diff --git a/Documentation/git-rev-parse.txt b/Documentation/git-rev-parse.txt\n> index b6c6326cdc7b..fb06e3118570 100644\n> --- a/Documentation/git-rev-parse.txt\n> +++ b/Documentation/git-rev-parse.txt\n> @@ -216,6 +216,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> +       Like `--git-dir`, but its output is always the canonicalized\n> +       absolute path.\n> +\n>  --git-common-dir::\n>         Show `$GIT_COMMON_DIR` if defined, else `$GIT_DIR`.\n>\n\nAfter working a little bit with rev-parse [1], I feel that this might\nbe better served as a\nstand-alone option.  Consider that in addition to --git-dir, the\noptions --git-common-dir,\n--git-path, and --git-shared-index produce relative paths.\n\nI propose that it might make more sense to use something like\n`--abs-path` to indicate\nthat the result should include an absolute path (or we could also just spell out\n`--absolute-path`).  That way we don't have to add additional options\nfor any other type\nthat might want an absolute path.\n\n    git rev-parse --git-dir --abs-path\n    git rev-parse --git-common-dir --absolute-path\n\nI do understand that this might be more work than is necessary for the\ncompletion series\nhere.  Would it be unreasonable to suggest a partial implementation\nthat, for now, only\nworks with `--git-dir`?\n\n> diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c\n> index cf8487b3b95f..90a4dd6032c0 100644\n> --- a/builtin/rev-parse.c\n> +++ b/builtin/rev-parse.c\n> @@ -744,17 +744,30 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)\n>                                 putchar('\\n');\n>                                 continue;\n>                         }\n> -                       if (!strcmp(arg, \"--git-dir\")) {\n> +                       if (!strcmp(arg, \"--git-dir\") ||\n> +                           !strcmp(arg, \"--absolute-git-dir\")) {\n>                                 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);\n>                                 char *cwd;\n>                                 int len;\n> -                               if (gitdir) {\n> -                                       puts(gitdir);\n> -                                       continue;\n> -                               }\n> -                               if (!prefix) {\n> -                                       puts(\".git\");\n> -                                       continue;\n> +                               if (arg[2] == 'g') {    /* --git-dir */\n> +                                       if (gitdir) {\n> +                                               puts(gitdir);\n> +                                               continue;\n> +                                       }\n> +                                       if (!prefix) {\n> +                                               puts(\".git\");\n> +                                               continue;\n> +                                       }\n> +                               } else {                /* --absolute-git-dir */\n> +                                       if (!gitdir && !prefix)\n> +                                               gitdir = \".git\";\n> +                                       if (gitdir) {\n> +                                               char absolute_path[PATH_MAX];\n> +                                               if (!realpath(gitdir, absolute_path))\n> +                                                       die_errno(_(\"unable to get absolute path\"));\n> +                                               puts(absolute_path);\n> +                                               continue;\n> +                                       }\n>                                 }\n>                                 cwd = xgetcwd();\n>                                 len = strlen(cwd);\n> diff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh\n> index 48ee07779d64..617fcd821309 100755\n> --- a/t/t1500-rev-parse.sh\n> +++ b/t/t1500-rev-parse.sh\n> @@ -31,23 +31,26 @@ test_rev_parse() {\n>         \"test '$1' = \\\"\\$(git rev-parse --git-dir)\\\"\"\n>         shift\n>         [ $# -eq 0 ] && return\n> +\n> +       test_expect_success \"$name: absolute-git-dir\" \\\n> +       \"verbose test '$1' = \\\"\\$(git rev-parse --absolute-git-dir)\\\"\"\n>  }\n>\n> -# label is-bare is-inside-git is-inside-work prefix git-dir\n> +# label is-bare is-inside-git is-inside-work prefix git-dir absolute-git-dir\n>\n>  ROOT=$(pwd)\n>\n> -test_rev_parse toplevel false false true '' .git\n> +test_rev_parse toplevel false false true '' .git \"$ROOT/.git\"\n>\n>  cd .git || exit 1\n> -test_rev_parse .git/ false true false '' .\n> +test_rev_parse .git/ false true false '' . \"$ROOT/.git\"\n>  cd objects || exit 1\n> -test_rev_parse .git/objects/ false true false '' \"$ROOT/.git\"\n> +test_rev_parse .git/objects/ false true false '' \"$ROOT/.git\" \"$ROOT/.git\"\n>  cd ../.. || exit 1\n>\n>  mkdir -p sub/dir || exit 1\n>  cd sub/dir || exit 1\n> -test_rev_parse subdirectory false false true sub/dir/ \"$ROOT/.git\"\n> +test_rev_parse subdirectory false false true sub/dir/ \"$ROOT/.git\" \"$ROOT/.git\"\n>  cd ../.. || exit 1\n>\n>  git config core.bare true\n> @@ -63,7 +66,7 @@ GIT_CONFIG=\"$(pwd)\"/../.git/config\n>  export GIT_DIR GIT_CONFIG\n>\n>  git config core.bare false\n> -test_rev_parse 'GIT_DIR=../.git, core.bare = false' false false true ''\n> +test_rev_parse 'GIT_DIR=../.git, core.bare = false' false false true '' \"../.git\" \"$ROOT/.git\"\n>\n>  git config core.bare true\n>  test_rev_parse 'GIT_DIR=../.git, core.bare = true' true false false ''\n> @@ -76,7 +79,7 @@ GIT_DIR=../repo.git\n>  GIT_CONFIG=\"$(pwd)\"/../repo.git/config\n>\n>  git config core.bare false\n> -test_rev_parse 'GIT_DIR=../repo.git, core.bare = false' false false true ''\n> +test_rev_parse 'GIT_DIR=../repo.git, core.bare = false' false false true '' \"../repo.git\" \"$ROOT/repo.git\"\n>\n>  git config core.bare true\n>  test_rev_parse 'GIT_DIR=../repo.git, core.bare = true' true false false ''\n> --\n> 2.7.2.410.g92cb358\n>\n> --\n\n[1] http://thread.gmane.org/gmane.comp.version-control.git/292272\n"},{"id":"284509","messageId":"20160426053522.GA23949@sigill.intra.peff.net","threadId":"41526","inReplyTo":"CANoM8SXO_Rz_CVOz9ptsaVCzcQ2D1FQrSuFFW4vZ4SdRYMzD=w@mail.gmail.com","subject":"Re: [PATCH 13/21] rev-parse: add '--absolute-git-dir' option","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2016-04-26T05:35:22Z","receivedAt":"2016-04-26T05:35:22Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Mon, Apr 25, 2016 at 10:33:13PM -0400, Mike Rappazzo wrote:\n\n> I propose that it might make more sense to use something like\n> `--abs-path` to indicate\n> that the result should include an absolute path (or we could also just spell out\n> `--absolute-path`).  That way we don't have to add additional options\n> for any other type\n> that might want an absolute path.\n> \n>     git rev-parse --git-dir --abs-path\n>     git rev-parse --git-common-dir --absolute-path\n> \n> I do understand that this might be more work than is necessary for the\n> completion series\n> here.  Would it be unreasonable to suggest a partial implementation\n> that, for now, only\n> works with `--git-dir`?\n\nI do like the concept of keeping \"--absolute-path\" orthogonal. The only\ntrick is that we need to either support it for all appropriate options,\nor document which options it _does_ work with. Otherwise, we're going to\nget bug reports when somebody tries \"--absolute-path --git-common-dir\".\n\nIt would be cleaner to provide a separate option to let people compose\nthe options, like:\n\n  git rev-parse --git-dir | git rev-parse --realpath\n\nbut that's a lot less efficient.\n\n> > +                                       if (gitdir) {\n> > +                                               char absolute_path[PATH_MAX];\n> > +                                               if (!realpath(gitdir, absolute_path))\n> > +                                                       die_errno(_(\"unable to get absolute path\"));\n> > +                                               puts(absolute_path);\n> > +                                               continue;\n> > +                                       }\n\nI don't recall if this came up in earlier review, but I happened to\nnotice the use of realpath() here. We should be using our custom\nreal_path() instead. There are some platforms without realpath(), I\nthink, and our real_path() is not limited to the static PATH_MAX (which\nis too small on some platforms).\n\n-Peff\n"},{"id":"286901","messageId":"20160518185825.Horde.EPd2nJNvqEW_VX4b01yWdIr@webmail.informatik.kit.edu","threadId":"41526","inReplyTo":"CANoM8SXO_Rz_CVOz9ptsaVCzcQ2D1FQrSuFFW4vZ4SdRYMzD=w@mail.gmail.com","subject":"Re: [PATCH 13/21] rev-parse: add '--absolute-git-dir' option","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2016-05-18T16:58:25Z","receivedAt":"2016-05-18T16:58:25Z","isPatch":true,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"\nQuoting Mike Rappazzo <rappazzo@gmail.com>:\n\n> On Thu, Feb 25, 2016 at 5:54 PM SZEDER Gábor <szeder@ira.uka.de> wrote:\n>>\n>> Some scripts can benefit from not having to deal with the possibility\n>> of relative paths to the repository, but the output of 'git rev-parse\n>> --git-dir' can be a relative path.  Case in point: supporting 'git -C\n>> <path>' in our Bash completion script turned out to be considerably\n>> more difficult, error prone and required more subshells and git\n>> processes when we had to cope with a relative path to the .git\n>> directory.\n>>\n>> Help these use cases and teach 'git rev-parse' a new\n>> '--absolute-git-dir' option which always outputs a canonicalized\n>> absolute path to the .git directory, regardless of whether the path is\n>> discovered automatically or is specified via $GIT_DIR or 'git\n>> --git-dir=<path>'.\n>>\n>> Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>\n>> ---\n>> Documentation/git-rev-parse.txt |  4 ++++\n>> builtin/rev-parse.c             | 29 +++++++++++++++++++++--------\n>> t/t1500-rev-parse.sh            | 17 ++++++++++-------\n>> 3 files changed, 35 insertions(+), 15 deletions(-)\n>>\n>> diff --git a/Documentation/git-rev-parse.txt\n>> b/Documentation/git-rev-parse.txt\n>> index b6c6326cdc7b..fb06e3118570 100644\n>> --- a/Documentation/git-rev-parse.txt\n>> +++ b/Documentation/git-rev-parse.txt\n>> @@ -216,6 +216,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>> +       Like `--git-dir`, but its output is always the canonicalized\n>> +       absolute path.\n>> +\n>> --git-common-dir::\n>>    Show `$GIT_COMMON_DIR` if defined, else `$GIT_DIR`.\n>>\n>\n> After working a little bit with rev-parse [1], I feel that this might\n> be better served as a\n> stand-alone option.  Consider that in addition to --git-dir, the\n> options --git-common-dir,\n> --git-path, and --git-shared-index produce relative paths.\n>\n> I propose that it might make more sense to use something like\n> `--abs-path` to indicate\n> that the result should include an absolute path (or we could also\n> just spell out\n> `--absolute-path`).  That way we don't have to add additional options\n> for any other type\n> that might want an absolute path.\n\nDo you have a specific use case in mind, where scripts struggle because\nin some cases '--git-dir', '--git-common-dir' or '--git-path' return a\nrelative path?  (Assuming that these options produce the right relative\npaths, of course.  At the moment some don't in some cases, but your\npatches elsewhere will eventually take care of that.)\n\nAs far as git is concerned, relative paths are relative to the current\nworking directory of the git process or script.  If a script is invoked\nas 'git -C subdir myscript', then the CWD is changed accordingly by the\nmain git process first going into the given directory before starting\nthe script.\n\nNow, though I wrote that generalizing \"some scripts\" and \"these use\ncases\" in the commit message, I really only meant the completion\nscript, because it is rather special in that it's executed in the\nuser's interactive shell environment.  This means that it can't just\nsimply 'cd' around and it has to cope with 'git -C subdir' options on\nit's own, because in that case the CWD of the git processes invoked\nfrom the completion script will be different from the CWD of the\nuser's shell.  That's the situation where a relative path returned\nfrom 'git rev-parse --git-dir' causes troubles.\n\nI can't really see other scripts in this situation, and I really don't\nexpect that the completion script would need any path other than the\npath to the git repository, i.e. it only needs '--absolute-git-dir'.\nTherefore I think that a separate '--absolute-path' option would offer\nlittle benefit, but at the same time would pose considerable\ncomplications, more on that below.\n\n> git rev-parse --git-dir --abs-path\n> git rev-parse --git-common-dir --absolute-path\n\n'git rev-parse's command line option parsing loop not just parses an\noption but in a lot of cases performs the necessary action right away,\nbefore examining any subsequent options.  In the above example\nit would print the path to the .git directory as soon as it encounters\n'--git-dir' while iterating through the command line options, before\neven noticing that there is an '--absolute-path' option as well.  If\nanything, then the other way around:\n\n     git rev-parse --absolute-path --git-dir\n\nAnd if we were to go this route, then there are a bunch of questions\nabout the gory details that would have to be cleared up first:\n\nShould the presence of an '--absolute-path' option influence all\nsubsequent path-returning options or just the one immediately\nfollowing it?  I.e. should all these options always return an\nabsolute path?\n\n     git rev-parse --absolute-path --git-dir --git-common-dir \\\n                   --git-path objects\n\nWhat about this, i.e. when there are other option(s) between\n'--absolute-path' and a path-returning option?\n\n     git rev-parse --absolute-path --is-bare-repository --git-dir\n\nI'd think they should all return an absolute path then.\n\nNow, should '--absolute-path' have an effect on '--show-prefix'\nand '--prefix' as well?\n\n     git rev-parse --absolute-path --show-prefix\n     git rev-parse --absolute-path --prefix t/ README\n\nLuckily, options after a filename are not allowed, so we don't even\nhave to think about cases like\n\n     git rev-parse --prefix t/ README --absolute-path Makefile\n\nas it would error out anyway.\n\nWould we need a '--no-absolute-path' (or '--relative-path', perhaps?)\noption to turn off the effect of a previous '--absolute-path', e.g.\n\n     git rev-parse --absolute-path --git-dir \\\n                   --no-absolute-path --git-common-dir\n\nIf yes, should a '--no-abolute-path' force subsequent path-returning\noptions to return a relative path even when they by default would\nreturn an absolute path, e.g.:\n\n     git -C t/ rev-parse --no-absolute-path --git-dir\n\nOr should it return a relative path even when paths are explicitly\nspecified as absolute paths?\n\n     git --git-dir=/absolute/path/to/repo.git rev-parse  \n--no-absolute-path --git-dir\n     git rev-parse --prefix /home/szeder/src/git/t/ --no-absolute-path README\n"}]}