git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCHv2 07/21] completion: ensure that the repository path given on the command line exists

From
SZEDER Gábor <szeder.dev@gmail.com>
Date
Feb 3, 2017, 02:48 UTC
Message-ID
<20170203024829.8071-8-szeder.dev@gmail.com>
In-Reply-To
<20170203024829.8071-1-szeder.dev@gmail.com>

The __gitdir() helper function prints the path to the git repository to its stdout or stays silent and returns with error when it can't find a repository or when the repository given via $GIT_DIR doesn't exist.

This is not the case, however, when the path in $__git_dir, i.e. the path to the repository specified on the command line via 'git --git-dir=<path>', doesn't exist: __gitdir() still outputs it as if it were a real existing repository, making some completion functions believe that they operate on an existing repository.

Check that the path in $__git_dir exists and return with error without printing anything to stdout if it doesn't.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
 contrib/completion/git-completion.bash | 1 +
 t/t9902-completion.sh                  | 8 ++++++++
 2 files changed, 9 insertions(+)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index ee6fb2259..5b2bd6721 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -40,6 +40,7 @@ __gitdir ()
 {
 	if [ -z "${1-}" ]; then
 		if [ -n "${__git_dir-}" ]; then
+			test -d "$__git_dir" || return 1
 			echo "$__git_dir"
 		elif [ -n "${GIT_DIR-}" ]; then
 			test -d "${GIT_DIR-}" || return 1
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index 7956cb9b1..7667baabf 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -211,6 +211,14 @@ test_expect_success '__gitdir - $GIT_DIR set while .git directory in parent' '
 	test_cmp expected "$actual"
 '
 
+test_expect_success '__gitdir - non-existing path in $__git_dir' '
+	(
+		__git_dir="non-existing" &&
+		test_must_fail __gitdir >"$actual"
+	) &&
+	test_must_be_empty "$actual"
+'
+
 test_expect_success '__gitdir - non-existing $GIT_DIR' '
 	(
 		GIT_DIR="$ROOT/non-existing" &&
-- 
2.11.0.555.g967c1bcb3
Previous: SZEDER GáborNext: SZEDER Gábor
Message 8 of 28 in “[PATCHv2 00/21] completion: various __gitdir()-related improvements”
  1. SZEDER GáborFeb 3, 2017
  2. 02/21 completion tests: don't add test cruft to the test repositorySZEDER Gábor, Feb 3, 2017
  3. 04/21 completion tests: consolidate getting path of current working directorySZEDER Gábor, Feb 3, 2017
  4. 05/21 completion tests: check __gitdir()'s output in the error casesSZEDER Gábor, Feb 3, 2017
  5. 03/21 completion tests: make the $cur variable local to the test helper functionsSZEDER Gábor, Feb 3, 2017
  6. 11/21 completion: don't list 'HEAD' when trying refs completion outside of a repoSZEDER Gábor, Feb 3, 2017
  7. 12/21 completion: list short refs from a remote given as a URLSZEDER Gábor, Feb 3, 2017
  8. 07/21 completion: ensure that the repository path given on the command line existsSZEDER Gábor, Feb 3, 2017
  9. 16/21 completion: respect 'git -C <path>'SZEDER Gábor, Feb 3, 2017
  10. 08/21 completion: fix most spots not respecting 'git --git-dir=<path>'SZEDER Gábor, Feb 3, 2017
  11. 15/21 rev-parse: add '--absolute-git-dir' optionSZEDER Gábor, Feb 3, 2017
  12. 18/21 completion: consolidate silencing errors from git commandsSZEDER Gábor, Feb 3, 2017
  13. 19/21 completion: don't guard git executions with __gitdir()SZEDER Gábor, Feb 3, 2017
  14. 10/21 completion: list refs from remote when remote's name matches a directorySZEDER Gábor, Feb 3, 2017
  15. 17/21 completion: don't use __gitdir() for git commandsSZEDER Gábor, Feb 3, 2017
  16. completion: restore removed line continuating backslashSZEDER Gábor, Feb 13, 2017
  17. Junio C HamanoFeb 13, 2017
  18. SZEDER GáborFeb 15, 2017
  19. Junio C HamanoFeb 15, 2017
  20. SZEDER GáborFeb 15, 2017
  21. 13/21 completion: don't offer commands when 'git --opt' needs an argumentSZEDER Gábor, Feb 3, 2017
  22. 20/21 completion: extract repository discovery from __gitdir()SZEDER Gábor, Feb 3, 2017
  23. 21/21 completion: cache the path to the repositorySZEDER Gábor, Feb 3, 2017
  24. 09/21 completion: respect 'git --git-dir=<path>' when listing remote refsSZEDER Gábor, Feb 3, 2017
  25. 14/21 completion: fix completion after 'git -C <path>'SZEDER Gábor, Feb 3, 2017
  26. 06/21 completion tests: add tests for the __git_refs() helper functionSZEDER Gábor, Feb 3, 2017
  27. 01/21 completion: improve __git_refs()'s in-code documentationSZEDER Gábor, Feb 3, 2017
  28. Junio C HamanoFeb 6, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.