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

Re: bash completion of "addable" files for `git add`

From
Jeff King <peff@peff.net>
Date
Nov 9, 2012, 16:27 UTC
Message-ID
<20121109162750.GC19725@sigill.intra.peff.net>
In-Reply-To
<5E69B894-C392-4DD5-A4F1-723DA1A3D059@zitc.de>
On Fri, Nov 09, 2012 at 02:22:27PM +0100, Andreas Zeidler wrote:
> so here's a patch adding bash completion on `git add` for modified,
> updated and untracked files.  i've also set up a pull request — before
> i found `Documentation/SubmittingPatches`.  fwiw, it's at
> https://github.com/git/git/pull/29

Please put cover letter material like this after the "---" divider, or include a "-- >8 --" scissors line before your commit. Either helps "git am" realize which content should go into the commit message.

> From cbac6caee7e788569562cb7138eb698cc46a1b8f Mon Sep 17 00:00:00 2001
> From: Andreas Zeidler <az@zitc.de>
> Date: Fri, 9 Nov 2012 13:05:43 +0100
Please omit these lines, as they are redundant with your email header.
> Subject: [PATCH] add bash completion for "addable" files

This one is useful, but it would be even better if it were the subject of your email. :)

Show 9 quoted lines
> +__git_addable ()
> +{
> +	local dir="$(__gitdir)"
> +	if [ -d "$dir" ]; then
> +		git --git-dir="$dir" status --short --untracked=all |\
> +			egrep '^.[UM?] ' | sed 's/^.. //'
> +		return
> +	fi
> +}

You would want to use the stable, scriptable "--porcelain" output, so the completion is not broken by future changes to the "--short" format. However, I do not think using "git status" is the best option. It computes three things:

  1. The diff between HEAD and index.
  2. The diff between index and working tree.
  3. The list of untracked files.

But you only care about (2) and (3), so you are wasting time computing (1). I think the list you want could be generated with:

  git diff-files --name-only
  git ls-files --exclude-standard -o
and then you do not need to worry about grep or sed at all.
Show 9 quoted lines
> @@ -810,6 +820,11 @@ _git_add ()
>  			--ignore-errors --intent-to-add
>  			"
>  		return
> +		;;
> +	*)
> +		__gitcomp "$(__git_addable)"
> +		return
> +		;;
I think you'd want to use __gitcomp_nl to handle filenames with spaces.

Speaking of which, the output of status (or of the commands I mentioned) may have quoting applied to pathnames. I think that is not something we handle very well right now in the completion, so it may not be worth worrying about for this particular patch.

Other than those comments, I think the intent of your patch makes a lot of sense.

-Peff
Previous: Andreas Zeidler
Message 2 of 2 in “bash completion of "addable" files for `git add`”
  1. Andreas ZeidlerNov 9, 2012
  2. Jeff KingNov 9, 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.