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

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

From
AZAndreas Zeidler <az@zitc.de>
Date
Nov 9, 2012, 13:22 UTC
Message-ID
<5E69B894-C392-4DD5-A4F1-723DA1A3D059@zitc.de>
hi,
i've always been annoyed by having to type — or copy & paste — paths when i wanted to simply add some files to the index.  i know about the `add -i` interface, and that improves things a little, but having bash completion for so many other things it still seemed to much hassle.
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
best regards,
andi
From cbac6caee7e788569562cb7138eb698cc46a1b8f Mon Sep 17 00:00:00 2001
From: Andreas Zeidler <az@zitc.de>
Date: Fri, 9 Nov 2012 13:05:43 +0100
Subject: [PATCH] add bash completion for "addable" files

this adds support for completing only modified, updated and untracked files on `git add`, since almost always these are what you want to add to the index.

the list of possible completions is determined using `git status` and filtered using `egrep` and `sed`.

Signed-off-by: Andreas Zeidler <az@zitc.de>
---
 contrib/completion/git-completion.bash | 15 +++++++++++++++
 t/t9902-completion.sh                  | 11 +++++++++++
 2 files changed, 26 insertions(+)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index be800e0..4aa0981 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -799,6 +799,16 @@ _git_apply ()
 	COMPREPLY=()
 }
 
+__git_addable ()
+{
+	local dir="$(__gitdir)"
+	if [ -d "$dir" ]; then
+		git --git-dir="$dir" status --short --untracked=all |\
+			egrep '^.[UM?] ' | sed 's/^.. //'
+		return
+	fi
+}
+
 _git_add ()
 {
 	__git_has_doubledash && return
@@ -810,6 +820,11 @@ _git_add ()
 			--ignore-errors --intent-to-add
 			"
 		return
+		;;
+	*)
+		__gitcomp "$(__git_addable)"
+		return
+		;;
 	esac
 	COMPREPLY=()
 }
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index cbd0fb6..a7c81d3 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -288,4 +288,15 @@ test_expect_failure 'complete tree filename with metacharacters' '
 	EOF
 '
 
+test_expect_success 'add completes untracked and modified names' '
+	echo other content >file1 &&
+	echo content >file2 &&
+	echo more >file3 &&
+	git add file1 &&
+	test_completion_long "git add f" <<-\EOF
+	file2_
+	file3_
+	EOF
+'
+
 test_done
-- 
1.8.0.1.g43af610


-- 
pgp key at http://zitc.de/pgp - http://wwwkeys.de.pgp.net/
upgrade to plone 4! -- http://plone.org/4
Next: Jeff King
Message 1 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.