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

[PATCH 10/21] completion: list refs from remote when remote's name matches a directory

From
SZEDER Gábor <szeder@ira.uka.de>
Date
Feb 25, 2016, 22:50 UTC
Message-ID
<1456440650-32623-11-git-send-email-szeder@ira.uka.de>
In-Reply-To
<1456440650-32623-1-git-send-email-szeder@ira.uka.de>

If the remote given to __git_refs() happens to match both the name of a configured remote and the name of a directory in the current working directory, then that directory is assumed to be a git repository, and listing refs from that directory will be attempted. This is wrong, because in such a situation git commands (e.g. 'git fetch|pull|push <remote>' whom these refs will eventually be passed to) give precedence to the configured remote. Therefore, __git_refs() should list refs from the configured remote as well.

Add the helper function __git_is_configured_remote() that checks whether its argument matches the name of a configured remote. Use this helper to decide how to handle the remote passed to __git_refs().

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
 contrib/completion/git-completion.bash | 28 +++++++++++++++++++++++-----
 t/t9902-completion.sh                  | 11 ++++++++++-
 2 files changed, 33 insertions(+), 6 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index adc968acea9d..b85ab27fb18a 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -347,12 +347,18 @@ __git_refs ()
 	local format refs
 
 	if [ -n "$remote" ]; then
-		if [ -d "$remote/.git" ]; then
-			dir="$remote/.git"
-		elif [ -d "$remote" ]; then
-			dir="$remote"
-		else
+		if __git_is_configured_remote "$remote"; then
+			# configured remote takes precedence over a
+			# local directory with the same name
 			from_local=n
+		else
+			if [ -d "$remote/.git" ]; then
+				dir="$remote/.git"
+			elif [ -d "$remote" ]; then
+				dir="$remote"
+			else
+				from_local=n
+			fi
 		fi
 	fi
 
@@ -434,6 +440,18 @@ __git_remotes ()
 	git --git-dir="$d" remote
 }
 
+# Returns true if $1 matches the name of a configured remote, false otherwise.
+__git_is_configured_remote ()
+{
+	local remote
+	for remote in $(__git_remotes); do
+		if [ "$remote" = "$1" ]; then
+			return 0
+		fi
+	done
+	return 1
+}
+
 __git_list_merge_strategies ()
 {
 	git merge -s help 2>&1 |
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index f9993d2c005a..9f8be9ab1f3b 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -378,6 +378,15 @@ test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from
 	test_cmp expect actual
 '
 
+test_expect_success '__git_is_configured_remote' '
+	test_when_finished "git remote remove remote_1" &&
+	git remote add remote_1 git://remote_1 &&
+	test_when_finished "git remote remove remote_2" &&
+	git remote add remote_2 git://remote_2 &&
+	verbose __git_is_configured_remote remote_2 &&
+	test_must_fail __git_is_configured_remote non-existent
+'
+
 test_expect_success 'setup for ref completion' '
 	echo content >file1 &&
 	echo more >file2 &&
@@ -519,7 +528,7 @@ test_expect_success '__git_refs - configured remote - full refs - repo given on
 	test_cmp expected "$actual"
 '
 
-test_expect_failure '__git_refs - configured remote - remote name matches a directory' '
+test_expect_success '__git_refs - configured remote - remote name matches a directory' '
 	cat >expected <<-EOF &&
 	HEAD
 	branch
-- 
2.7.2.410.g92cb358
Previous: SZEDER GáborNext: SZEDER Gábor
Message 11 of 25 in “completion: __gitdir()-related improvements”
  1. 00/21 completion: __gitdir()-related improvementsSZEDER Gábor, Feb 25, 2016
  2. 01/21 completion: improve __git_refs()'s in-code documentationSZEDER Gábor, Feb 25, 2016
  3. 02/21 completion tests: don't add test cruft to the test repositorySZEDER Gábor, Feb 25, 2016
  4. 03/21 completion tests: make the $cur variable local to the test helper functionsSZEDER Gábor, Feb 25, 2016
  5. 04/21 completion tests: consolidate getting path of current working directorySZEDER Gábor, Feb 25, 2016
  6. 05/21 completion tests: check __gitdir()'s output in the error casesSZEDER Gábor, Feb 25, 2016
  7. 06/21 completion tests: add tests for the __git_refs() helper functionSZEDER Gábor, Feb 25, 2016
  8. 07/21 completion: ensure that the repository path given on the command line existsSZEDER Gábor, Feb 25, 2016
  9. 08/21 completion: fix most spots not respecting 'git --git-dir=<path>'SZEDER Gábor, Feb 25, 2016
  10. 09/21 completion: respect 'git --git-dir=<path>' when listing remote refsSZEDER Gábor, Feb 25, 2016
  11. 10/21 completion: list refs from remote when remote's name matches a directorySZEDER Gábor, Feb 25, 2016
  12. 11/21 completion: don't list 'HEAD' when trying refs completion outside of a repoSZEDER Gábor, Feb 25, 2016
  13. 12/21 completion: list short refs from a remote given as a URLSZEDER Gábor, Feb 25, 2016
  14. 13/21 rev-parse: add '--absolute-git-dir' optionSZEDER Gábor, Feb 25, 2016
  15. Mike RappazzoApr 26, 2016
  16. Jeff KingApr 26, 2016
  17. SZEDER GáborMay 18, 2016
  18. 14/21 completion: don't offer commands when 'git --opt' needs an argumentSZEDER Gábor, Feb 25, 2016
  19. 15/21 completion: fix completion after 'git -C <path>'SZEDER Gábor, Feb 25, 2016
  20. 16/21 completion: respect 'git -C <path>'SZEDER Gábor, Feb 25, 2016
  21. 17/21 completion: don't use __gitdir() for git commandsSZEDER Gábor, Feb 25, 2016
  22. 18/21 completion: consolidate silencing errors from git commandsSZEDER Gábor, Feb 25, 2016
  23. 19/21 completion: don't guard git executions with __gitdir()SZEDER Gábor, Feb 25, 2016
  24. 20/21 completion: extract repository discovery from __gitdir()SZEDER Gábor, Feb 25, 2016
  25. 21/21 completion: cache the path to the repositorySZEDER Gábor, Feb 25, 2016

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.