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

Re: Problem completing remotes when .git/remotes exits

From
SZEDER Gábor <szeder@ira.uka.de>
Date
Sep 25, 2012, 23:00 UTC
Message-ID
<20120925230045.GA13266@goldbirke>
In-Reply-To
<505A2330.9040800@kdbg.org>
Hi,
On Wed, Sep 19, 2012 at 09:55:28PM +0200, Johannes Sixt wrote:
Show 33 quoted lines
> I have an empty .git/remotes directory. Trying to complete the name of
> a remote always reports an error:
> 
> git@master:1023> git fetch <TAB>ls: invalid option -- ' '
> Try `ls --help' for more information.
> 
> I have these:
> 
> 	alias ls='ls $LS_OPTIONS'
> and
> 	LS_OPTIONS='-N --color=tty -T 0'
> 
> I instrumented __git_remotes with set -x, which shows:
> 
> git@master:1006> git fetch <TAB>+++ __gitdir
> +++ '[' -z '' ']'
> +++ '[' -n '' ']'
> +++ '[' -n '' ']'
> +++ '[' -d .git ']'
> +++ echo .git
> ++ local i 'IFS=
> ' d=.git
> ++ test -d .git/remotes
> ++ ls '-N --color=tty -T 0' -1 .git/remotes
> ls: invalid option -- ' '
> Try `ls --help' for more information.
> ...
> 
> Notice that the expansion of $LS_OPTIONS is not split at the blanks,
> obviously, because $IFS does not contain a blank at that moment.
> 
> The patch below helps, but it looks like a work-around rather than a
> solution. Ideas?
I've got two alternative solutions for this issue.

The first one is less intrusive: use the 'command' builtin to tell the shell to ignore shell functions and aliases and just run the ls command.

diff --git a/contrib/completion/git-completion.bash
b/contrib/completion/git-completion.bash
index be800e09..bcde9472 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -370,7 +370,7 @@ __git_refs_remotes ()
 __git_remotes ()
 {
 	local i IFS=$'\n' d="$(__gitdir)"
-	test -d "$d/remotes" && ls -1 "$d/remotes"
+	test -d "$d/remotes" && command ls -1 "$d/remotes"
	for i in $(git --git-dir="$d" config --get-regexp 'remote\..*\.url' 2>/dev/null); do
 		i="${i#remote.}"
 		echo "${i/.url*/}"


But then it got me thinking...  Notice how much effort we spend just
to get the list of remotes?  We could just run 'git remote' directly
instead...

diff --git a/contrib/completion/git-completion.bash
b/contrib/completion/git-completion.bash
index be800e09..1daeaccf 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -369,12 +369,8 @@ __git_refs_remotes ()
 
 __git_remotes ()
 {
-	local i IFS=$'\n' d="$(__gitdir)"
-	test -d "$d/remotes" && ls -1 "$d/remotes"
-	for i in $(git --git-dir="$d" config --get-regexp 'remote\..*\.url' 2>/dev/null); do
-		i="${i#remote.}"
-		echo "${i/.url*/}"
-	done
+	local d="$(__gitdir)"
+	git --git-dir="$d" remote
 }
 
 __git_list_merge_strategies ()


I prefer the second one ;)

Best,
Gábor
Previous: Johannes SixtNext: Junio C Hamano
Message 2 of 6 in “Problem completing remotes when .git/remotes exits”
  1. Johannes SixtSep 19, 2012
  2. SZEDER GáborSep 25, 2012
  3. Junio C HamanoSep 25, 2012
  4. SZEDER GáborSep 26, 2012
  5. SZEDER GáborSep 26, 2012
  6. SZEDER GáborSep 26, 2012

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.