Skip to content

Commit f825972

Browse files
felipecgitster
authored andcommitted
completion: refactor diff_index wrappers
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0afe8e9 commit f825972

File tree

1 file changed

+16
-55
lines changed

1 file changed

+16
-55
lines changed

contrib/completion/git-completion.bash

Lines changed: 16 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -297,30 +297,25 @@ __git_index_file_list_filter ()
297297
__git_index_file_list_filter_bash
298298
}
299299

300-
# Execute git ls-files, returning paths relative to the directory
301-
# specified in the first argument, and using the options specified in
302-
# the second argument.
300+
# Execute 'git ls-files', unless the --committable option is specified, in
301+
# which case it runs 'git diff-index' to find out the files that can be
302+
# committed. It return paths relative to the directory specified in the first
303+
# argument, and using the options specified in the second argument.
303304
__git_ls_files_helper ()
304305
{
305306
(
306307
test -n "${CDPATH+set}" && unset CDPATH
307-
# NOTE: $2 is not quoted in order to support multiple options
308-
cd "$1" && git ls-files --exclude-standard $2
308+
cd "$1"
309+
if [ "$2" == "--committable" ]; then
310+
git diff-index --name-only --relative HEAD
311+
else
312+
# NOTE: $2 is not quoted in order to support multiple options
313+
git ls-files --exclude-standard $2
314+
fi
309315
) 2>/dev/null
310316
}
311317

312318

313-
# Execute git diff-index, returning paths relative to the directory
314-
# specified in the first argument, and using the tree object id
315-
# specified in the second argument.
316-
__git_diff_index_helper ()
317-
{
318-
(
319-
test -n "${CDPATH+set}" && unset CDPATH
320-
cd "$1" && git diff-index --name-only --relative "$2"
321-
) 2>/dev/null
322-
}
323-
324319
# __git_index_files accepts 1 or 2 arguments:
325320
# 1: Options to pass to ls-files (required).
326321
# 2: A directory path (optional).
@@ -337,22 +332,6 @@ __git_index_files ()
337332
fi
338333
}
339334

340-
# __git_diff_index_files accepts 1 or 2 arguments:
341-
# 1) The id of a tree object.
342-
# 2) A directory path (optional).
343-
# If provided, only files within the specified directory are listed.
344-
# Sub directories are never recursed. Path must have a trailing
345-
# slash.
346-
__git_diff_index_files ()
347-
{
348-
local dir="$(__gitdir)" root="${2-.}"
349-
350-
if [ -d "$dir" ]; then
351-
__git_diff_index_helper "$root" "$1" | __git_index_file_list_filter |
352-
sort | uniq
353-
fi
354-
}
355-
356335
__git_heads ()
357336
{
358337
local dir="$(__gitdir)"
@@ -550,8 +529,10 @@ __git_complete_revlist_file ()
550529
}
551530

552531

553-
# __git_complete_index_file requires 1 argument: the options to pass to
554-
# ls-file
532+
# __git_complete_index_file requires 1 argument:
533+
# 1: the options to pass to ls-file
534+
#
535+
# The exception is --committable, which finds the files appropriate commit.
555536
__git_complete_index_file ()
556537
{
557538
local pfx cur_="$cur"
@@ -570,26 +551,6 @@ __git_complete_index_file ()
570551
esac
571552
}
572553

573-
# __git_complete_diff_index_file requires 1 argument: the id of a tree
574-
# object
575-
__git_complete_diff_index_file ()
576-
{
577-
local pfx cur_="$cur"
578-
579-
case "$cur_" in
580-
?*/*)
581-
pfx="${cur_%/*}"
582-
cur_="${cur_##*/}"
583-
pfx="${pfx}/"
584-
585-
__gitcomp_file "$(__git_diff_index_files "$1" "$pfx")" "$pfx" "$cur_"
586-
;;
587-
*)
588-
__gitcomp_file "$(__git_diff_index_files "$1")" "" "$cur_"
589-
;;
590-
esac
591-
}
592-
593554
__git_complete_file ()
594555
{
595556
__git_complete_revlist_file
@@ -1211,7 +1172,7 @@ _git_commit ()
12111172
esac
12121173

12131174
if git rev-parse --verify --quiet HEAD >/dev/null; then
1214-
__git_complete_diff_index_file "HEAD"
1175+
__git_complete_index_file "--committable"
12151176
else
12161177
# This is the first commit
12171178
__git_complete_index_file "--cached"

0 commit comments

Comments
 (0)