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

[PATCH 05/11] completion: refactor diff_index wrappers

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
Apr 27, 2013, 10:19 UTC
Message-ID
<1367057994-19887-6-git-send-email-felipe.contreras@gmail.com>
In-Reply-To
<1367057994-19887-1-git-send-email-felipe.contreras@gmail.com>

At the end of the day what we really need is to find out the files that have been staged, or modified, because those files are the ones that make sense to pass as arguments to 'git commit'.

We need diff-index to find those out, since 'git ls-files' doesn't do that.

But we don't need wrappers and wrappers basically identical to the ones used for 'git ls-files', when we can pretend it receives a --committable option that would return what we need.

That way, we can remove a bunch of code without any functional changes.
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 contrib/completion/git-completion.bash | 71 ++++++++--------------------------
 1 file changed, 16 insertions(+), 55 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 7f1ebe4..2561265 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -297,30 +297,25 @@ __git_index_file_list_filter ()
 	__git_index_file_list_filter_bash
 }
 
-# Execute git ls-files, returning paths relative to the directory
-# specified in the first argument, and using the options specified in
-# the second argument.
+# Execute 'git ls-files', unless the --committable option is specified, in
+# which case it runs 'git diff-index' to find out the files that can be
+# committed.  It return paths relative to the directory specified in the first
+# argument, and using the options specified in the second argument.
 __git_ls_files_helper ()
 {
 	(
 		test -n "${CDPATH+set}" && unset CDPATH
-		# NOTE: $2 is not quoted in order to support multiple options
-		cd "$1" && git ls-files --exclude-standard $2
+		cd "$1"
+		if [ "$2" == "--committable" ]; then
+			git diff-index --name-only --relative HEAD
+		else
+			# NOTE: $2 is not quoted in order to support multiple options
+			git ls-files --exclude-standard $2
+		fi
 	) 2>/dev/null
 }
 
 
-# Execute git diff-index, returning paths relative to the directory
-# specified in the first argument, and using the tree object id
-# specified in the second argument.
-__git_diff_index_helper ()
-{
-	(
-		test -n "${CDPATH+set}" && unset CDPATH
-		cd "$1" && git diff-index --name-only --relative "$2"
-	) 2>/dev/null
-}
-
 # __git_index_files accepts 1 or 2 arguments:
 # 1: Options to pass to ls-files (required).
 # 2: A directory path (optional).
@@ -337,22 +332,6 @@ __git_index_files ()
 	fi
 }
 
-# __git_diff_index_files accepts 1 or 2 arguments:
-# 1) The id of a tree object.
-# 2) A directory path (optional).
-#    If provided, only files within the specified directory are listed.
-#    Sub directories are never recursed.  Path must have a trailing
-#    slash.
-__git_diff_index_files ()
-{
-	local dir="$(__gitdir)" root="${2-.}"
-
-	if [ -d "$dir" ]; then
-		__git_diff_index_helper "$root" "$1" | __git_index_file_list_filter |
-			sort | uniq
-	fi
-}
-
 __git_heads ()
 {
 	local dir="$(__gitdir)"
@@ -550,8 +529,10 @@ __git_complete_revlist_file ()
 }
 
 
-# __git_complete_index_file requires 1 argument: the options to pass to
-# ls-file
+# __git_complete_index_file requires 1 argument:
+# 1: the options to pass to ls-file
+#
+# The exception is --committable, which finds the files appropriate commit.
 __git_complete_index_file ()
 {
 	local pfx cur_="$cur"
@@ -570,26 +551,6 @@ __git_complete_index_file ()
 	esac
 }
 
-# __git_complete_diff_index_file requires 1 argument: the id of a tree
-# object
-__git_complete_diff_index_file ()
-{
-	local pfx cur_="$cur"
-
-	case "$cur_" in
-	?*/*)
-		pfx="${cur_%/*}"
-		cur_="${cur_##*/}"
-		pfx="${pfx}/"
-
-		__gitcomp_file "$(__git_diff_index_files "$1" "$pfx")" "$pfx" "$cur_"
-		;;
-	*)
-		__gitcomp_file "$(__git_diff_index_files "$1")" "" "$cur_"
-		;;
-	esac
-}
-
 __git_complete_file ()
 {
 	__git_complete_revlist_file
@@ -1211,7 +1172,7 @@ _git_commit ()
 	esac
 
 	if git rev-parse --verify --quiet HEAD >/dev/null; then
-		__git_complete_diff_index_file "HEAD"
+		__git_complete_index_file "--committable"
 	else
 		# This is the first commit
 		__git_complete_index_file "--cached"
-- 
1.8.2.1.1031.g2ee5873
Previous: Felipe ContrerasNext: Felipe Contreras
Message 6 of 19 in “completion: general cleanups”
  1. 00/11 completion: general cleanupsFelipe Contreras, Apr 27, 2013
  2. 01/11 completion: add file completion testsFelipe Contreras, Apr 27, 2013
  3. 02/11 completion: document tilde expansion failure in testsFelipe Contreras, Apr 27, 2013
  4. 03/11 completion; remove unuseful commentsFelipe Contreras, Apr 27, 2013
  5. 04/11 completion: use __gitcompadd for __gitcomp_fileFelipe Contreras, Apr 27, 2013
  6. 05/11 completion: refactor diff_index wrappersFelipe Contreras, Apr 27, 2013
  7. 06/11 completion: refactor __git_complete_index_file()Felipe Contreras, Apr 27, 2013
  8. 07/11 completion: avoid compopt -o filenamesFelipe Contreras, Apr 27, 2013
  9. 08/11 completion: get rid of __gitcomp_file()Felipe Contreras, Apr 27, 2013
  10. 09/11 completion: add space after completed filenameFelipe Contreras, Apr 27, 2013
  11. 10/11 completion: remove __git_index_file_list_filter()Felipe Contreras, Apr 27, 2013
  12. 11/11 completion: zsh: add proper file supportFelipe Contreras, Apr 27, 2013
  13. Manlio PerilloApr 27, 2013
  14. Felipe ContrerasApr 27, 2013
  15. Felipe ContrerasApr 27, 2013
  16. Manlio PerilloApr 27, 2013
  17. Felipe ContrerasApr 27, 2013
  18. Felipe ContrerasApr 27, 2013
  19. Manlio PerilloApr 27, 2013

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.