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

Re: [PATCH] git-completion.bash: always swallow error output of for-each-ref

From
Jeff King <peff@peff.net>
Date
Feb 4, 2016, 11:13 UTC
Message-ID
<20160204111307.GA30495@sigill.intra.peff.net>
In-Reply-To
<56B32953.2010908@gmail.com>
On Thu, Feb 04, 2016 at 11:34:59AM +0100, Sebastian Schuberth wrote:
Show 5 quoted lines
> This avoids output like
> 
>     warning: ignoring broken ref refs/remotes/origin/HEAD
> 
> while completing branch names.

Hmm. I feel like this case (HEAD points to a branch, then `fetch --prune` deletes it) came up recently and we discussed quieting that warning. But now I cannot seem to find it.

Anyway, I this is a reasonable workaround. Errors from bash completion scripts are almost always going to be useless and get in the way of reading your own prompt.

Show 11 quoted lines
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index 15ebba5..7c0549d 100644
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -317,7 +317,7 @@ __git_heads ()
>  	local dir="$(__gitdir)"
>  	if [ -d "$dir" ]; then
>  		git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
> -			refs/heads
> +			refs/heads 2>/dev/null
>  		return

Not really related to your topic, but digging into it caused me to read b7dd2d2 (for-each-ref: Do not lookup objects when they will not be used, 2009-05-27), which is about making sure for-each-ref is very fast in completion.

It looks like %(refname:short) is actually kind of expensive:
$ time git for-each-ref --format='%(refname)' refs/tags  >/dev/null

real 0m0.004s user 0m0.000s sys 0m0.004s

$ time git for-each-ref --format='%(refname:short)' refs/tags >/dev/null

real 0m0.009s user 0m0.004s sys 0m0.004s

The upcoming refname:strip does much better:
$ time git for-each-ref --format='%(refname:strip=2)' refs/tags >/dev/null

real 0m0.004s user 0m0.000s sys 0m0.004s

Obviously these are pretty small timings from my git.git with ~600 tags, but you can see that refname:short really does cost more. On a more ridiculous example repository I have with about 10 million refs, the timings are more like 5s, 66s, 5.5s.

Just thought I'd throw that our there in case any completion people feel like poking around with it.

-Peff
Previous: Sebastian SchuberthNext: Johannes Schindelin
Message 2 of 24 in “git-completion.bash: always swallow error output of for-each-ref”
  1. git-completion.bash: always swallow error output of for-each-refSebastian Schuberth, Feb 4, 2016
  2. Jeff KingFeb 4, 2016
  3. Johannes SchindelinFeb 4, 2016
  4. Jeff KingFeb 4, 2016
  5. Junio C HamanoFeb 4, 2016
  6. SZEDER GáborFeb 12, 2016
  7. Jeff KingFeb 12, 2016
  8. SZEDER GáborFeb 13, 2016
  9. Johannes SchindelinFeb 13, 2016
  10. SZEDER GáborFeb 13, 2016
  11. Johannes SchindelinFeb 13, 2016
  12. Jeff KingFeb 13, 2016
  13. SZEDER GáborFeb 12, 2016
  14. Jeff KingFeb 12, 2016
  15. Duy NguyenFeb 13, 2016
  16. Junio C HamanoFeb 12, 2016
  17. Jeff KingFeb 12, 2016
  18. Junio C HamanoFeb 12, 2016
  19. SZEDER GáborFeb 12, 2016
  20. Jeff KingFeb 12, 2016
  21. Junio C HamanoFeb 12, 2016
  22. Junio C HamanoFeb 23, 2016
  23. Sebastian SchuberthFeb 24, 2016
  24. Sebastian SchuberthFeb 12, 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.