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 <felipe.contreras@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Felipe Contreras committed Apr 27, 2013 at 15:10 UTC f825972c38ba5a8e0309a552b401ad55aeaaaf36
1 file changed +16 -55
contrib/completion/git-completion.bash
+16 -55
@@ -297,30 +297,25 @@ __git_index_file_list_filter ()
297 __git_index_file_list_filter_bash
298 }
299
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.
304 __git_ls_files_helper ()
305 {
306 (
307 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
315 ) 2>/dev/null
316 }
317
318
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 -
319 # __git_index_files accepts 1 or 2 arguments:
320 # 1: Options to pass to ls-files (required).
321 # 2: A directory path (optional).
@@ -337,22 +332,6 @@ __git_index_files ()
332 fi
333 }
334
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 -
335 __git_heads ()
336 {
337 local dir="$(__gitdir)"
@@ -550,8 +529,10 @@ __git_complete_revlist_file ()
529 }
530
531
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.
536 __git_complete_index_file ()
537 {
538 local pfx cur_="$cur"
@@ -570,26 +551,6 @@ __git_complete_index_file ()
551 esac
552 }
553
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 -
554 __git_complete_file ()
555 {
556 __git_complete_revlist_file
@@ -1211,7 +1172,7 @@ _git_commit ()
1172 esac
1173
1174 if git rev-parse --verify --quiet HEAD >/dev/null; then
1214 - __git_complete_diff_index_file "HEAD"
1175 + __git_complete_index_file "--committable"
1176 else
1177 # This is the first commit
1178 __git_complete_index_file "--cached"