[PATCH] git-completion.bash: add support for path completion

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The git-completion.bash script did not implemented full support for
completion, for git commands that operate on files from the current
working directory or the index.

For these commands, only options completion was available.

Full support for completion is now implemented, for git commands where
the non-option arguments always refer to paths on the current working
directory or the index, as the follow:

* the path completion for the "git mv" and "git rm" commands is provided
  using "git ls-files --exclude-standard"

* the path completion for the "git add" command is provided using
  "git ls-files --exclude-standard -o -m"

* the path completion for the "git clean" command is provided using
  "git ls-files --exclude-standard -o"

* the path completion for the "git commit" command is provides using
  "git diff-index --name-only HEAD"

Signed-off-by: Manlio Perillo <manlio.perillo@xxxxxxxxx>
---
 contrib/completion/git-completion.bash | 39 ++++++++++++++++++++++------------
 1 file changed, 26 insertions(+), 13 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 0b77eb1..8b348c3 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -233,6 +233,25 @@ __gitcomp_nl ()
 	COMPREPLY=($(compgen -P "${2-}" -S "${4- }" -W "$1" -- "${3-$cur}"))
 }
 
+__git_files ()
+{
+	local dir="$(__gitdir)"
+	if [ -d "$dir" ]; then
+		git --git-dir="$dir" ls-files --exclude-standard $*
+		return
+	fi
+}
+
+# Return all staged files with modification from current HEAD
+__git_commit_files ()
+{
+	local dir="$(__gitdir)"
+	if [ -d "$dir" ]; then
+		git --git-dir="$dir" diff-index --name-only HEAD
+		return
+	fi
+}
+
 __git_heads ()
 {
 	local dir="$(__gitdir)"
@@ -770,8 +789,6 @@ _git_apply ()
 
 _git_add ()
 {
-	__git_has_doubledash && return
-
 	case "$cur" in
 	--*)
 		__gitcomp "
@@ -780,7 +797,8 @@ _git_add ()
 			"
 		return
 	esac
-	COMPREPLY=()
+	# XXX should we care for --update and --all options ?
+	__gitcomp_nl "$(__git_files -o -m)" "" "$cur" ""
 }
 
 _git_archive ()
@@ -930,15 +948,14 @@ _git_cherry_pick ()
 
 _git_clean ()
 {
-	__git_has_doubledash && return
-
 	case "$cur" in
 	--*)
 		__gitcomp "--dry-run --quiet"
 		return
 		;;
 	esac
-	COMPREPLY=()
+	# TODO: check for -x option
+	__gitcomp_nl "$(__git_files -o)" "" "$cur" ""
 }
 
 _git_clone ()
@@ -969,8 +986,6 @@ _git_clone ()
 
 _git_commit ()
 {
-	__git_has_doubledash && return
-
 	case "$cur" in
 	--cleanup=*)
 		__gitcomp "default strip verbatim whitespace
@@ -998,7 +1013,7 @@ _git_commit ()
 			"
 		return
 	esac
-	COMPREPLY=()
+	__gitcomp_nl "$(__git_commit_files)" "" "$cur" ""
 }
 
 _git_describe ()
@@ -1362,7 +1377,7 @@ _git_mv ()
 		return
 		;;
 	esac
-	COMPREPLY=()
+	__gitcomp_nl "$(__git_files)" "" "$cur" ""
 }
 
 _git_name_rev ()
@@ -2068,15 +2083,13 @@ _git_revert ()
 
 _git_rm ()
 {
-	__git_has_doubledash && return
-
 	case "$cur" in
 	--*)
 		__gitcomp "--cached --dry-run --ignore-unmatch --quiet"
 		return
 		;;
 	esac
-	COMPREPLY=()
+	__gitcomp_nl "$(__git_files)" "" "$cur" ""
 }
 
 _git_shortlog ()
-- 
1.8.1.rc1.18.g9db0d25

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]