git-completion.bash: add support for path completion
PATCH v3 1/1 git-completion.bash: add support for path completion
The git-completion.bash script did not implemented full, git aware, support to complete paths, for git commands that operate on files within the current working directory or the index.
As an example:
git add <TAB>
will suggest all files in the current working directory, including ignored files and files that have not been modified.
Support path completion, for git commands where the non-option arguments always refer to paths within the current working directory or the index, as the follow:
* the path completion for the "git mv", "git rm" and "git ls-tree" commands will suggest all cached files.
* the path completion for the "git add" command will suggest all untracked and modified files. Ignored files are excluded.
* the path completion for the "git clean" command will suggest all untracked files. Ignored files are excluded.
* the path completion for the "git commit" command will suggest all files that have been modified from the HEAD.
For all affected commands, completion will always stop at directory boundary. Only standard ignored files are excluded, using the --exclude-standard option of the ls-files command.
---
Changes from version 2:
* Perl is no more used.
* Fixed some coding style issues.
* Refactorized code, to improve future path completion support for the "git reset" command.
contrib/completion/git-completion.bash | 127 ++++++++++++++++++++++++++++----- 1 file changed, 111 insertions(+), 16 deletions(-)
contrib/completion/git-completion.bash +111 -16
@@ -13,6 +13,7 @@ # *) .git/remotes file names # *) git 'subcommands' # *) tree paths within 'ref:path/to/file' expressions +# *) file paths within current working directory and index # *) common --long-options # # To use these routines:
@@ -233,6 +234,58 @@ __gitcomp_nl () COMPREPLY=($(compgen -P "${2-}" -S "${4- }" -W "$1" -- "${3-$cur}")) } +# Process path list returned by "ls-files" and "diff-index --name-only" +# commands, in order to list only file names relative to a specified +# directory, and append a slash to directory names. +# It accepts 1 optional argument: a directory path. The path must have +# a trailing slash. +__git_index_file_list_filter () +{ + local path + + while read -r path; do + path=${path#$1} + + case "$path" in + ?*/*) echo "${path%%/*}/" ;; + *) echo $path ;; + esac + done +} + +# __git_index_files accepts 1 or 2 arguments: +# 1: A string for file index status mode ("c", "m", "d", "o"), as +# supported by git ls-files command. +# 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_index_files () +{ + local dir="$(__gitdir)" + + if [ -d "$dir" ]; then + git --git-dir="$dir" ls-files --exclude-standard "-${1}" "${2}" | + __git_index_file_list_filter ${2} | uniq + 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)" + + if [ -d "$dir" ]; then + git --git-dir="$dir" diff-index --name-only "${1}" | + __git_index_file_list_filter "${2}" | uniq + fi +} + __git_heads () { local dir="$(__gitdir)"
@@ -430,6 +483,46 @@ __git_complete_revlist_file () } +# __git_complete_index_file requires 1 argument: the file index +# status mode +__git_complete_index_file () +{ + local pfx cur_="$cur" + + case "$cur_" in + ?*/*) + pfx="${cur_%/*}" + cur_="${cur_##*/}" + pfx="${pfx}/" + + __gitcomp_nl "$(__git_index_files ${1} ${pfx})" "$pfx" "$cur_" "" + ;; + *) + __gitcomp_nl "$(__git_index_files ${1})" "" "$cur_" "" + ;; + 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_nl "$(__git_diff_index_files ${1} ${pfx})" "$pfx" "$cur_" "" + ;; + *) + __gitcomp_nl "$(__git_diff_index_files ${1})" "" "$cur_" "" + ;; + esac +} + __git_complete_file () { __git_complete_revlist_file
@@ -770,8 +863,6 @@ _git_apply () _git_add () { - __git_has_doubledash && return - case "$cur" in --*) __gitcomp "
@@ -780,7 +871,9 @@ _git_add () " return esac - COMPREPLY=() + + # XXX should we check for --update and --all options ? + __git_complete_index_file "om" } _git_archive ()
@@ -930,15 +1023,15 @@ _git_cherry_pick () _git_clean () { - __git_has_doubledash && return - case "$cur" in --*) __gitcomp "--dry-run --quiet" return ;; esac - COMPREPLY=() + + # XXX should we check for -x option ? + __git_complete_index_file "o" } _git_clone ()
@@ -969,8 +1062,6 @@ _git_clone () _git_commit () { - __git_has_doubledash && return - case "$cur" in --cleanup=*) __gitcomp "default strip verbatim whitespace
@@ -997,8 +1088,10 @@ _git_commit () --verbose --quiet --fixup= --squash= " return + ;; esac - COMPREPLY=() + + __git_complete_diff_index_file "HEAD" } _git_describe ()
@@ -1216,8 +1309,6 @@ _git_init () _git_ls_files () { - __git_has_doubledash && return - case "$cur" in --*) __gitcomp "--cached --deleted --modified --others --ignored
@@ -1230,7 +1321,10 @@ _git_ls_files () return ;; esac - COMPREPLY=() + + # XXX ignore options like --modified and always suggest all cached + # files. + __git_complete_index_file "c" } _git_ls_remote ()
@@ -1362,7 +1456,9 @@ _git_mv () return ;; esac - COMPREPLY=() + + # XXX needs more work + __git_complete_index_file "c" } _git_name_rev ()
@@ -2068,15 +2164,14 @@ _git_revert () _git_rm () { - __git_has_doubledash && return - case "$cur" in --*) __gitcomp "--cached --dry-run --ignore-unmatch --quiet" return ;; esac - COMPREPLY=() + + __git_complete_index_file "c" } _git_shortlog ()
Signature · 1 line
1.8.1.rc1.18.g9db0d25
[jch: again, adding area experts to Cc]
Quoted 6 lines · Manlio Perillo
Changes from version 2:
* Perl is no more used.
* Fixed some coding style issues.
* Refactorized code, to improve future path completion support for the "git reset" command.
Thanks. Will replace what was queued on 'pu'.
contrib/completion/git-completion.bash __gitcomp_nl () · line 241
Quoted 5 lines
contrib/completion/git-completion.bash +5
-0+# Process path list returned by "ls-files" and "diff-index --name-only" +# commands, in order to list only file names relative to a specified +# directory, and append a slash to directory names. +# It accepts 1 optional argument: a directory path. The path must have +# a trailing slash.
The callsites that call this function, and the way the argument is used, do not make it look like it is an optional argument to me.
After reading later parts of the patch, I think the callers are wrong (see below) and you did intend to make "$1" optional.
contrib/completion/git-completion.bash __gitcomp_nl () · line 247
Quoted 6 lines
contrib/completion/git-completion.bash +6
-0+__git_index_file_list_filter () +{ + local path + + while read -r path; do + path=${path#$1}
This will work correctly only if $1 does not have any shell metacharacter that removes prefix of $path that matches $1 as a pathname expansion pattern. As this file is for bash completion, using string-oriented Bash-isms like ${#1} (to get the length of the prefix) and ${path:$offset} (to get substring) are perfectly fine way to correct it.
Also, as $1 is optional, won't this barf if it is run under "set -u"?
Quoted 4 lines
contrib/completion/git-completion.bash +4
-0+# __git_index_files accepts 1 or 2 arguments: +# 1: A string for file index status mode ("c", "m", "d", "o"), as +# supported by git ls-files command. +# 2: A directory path (optional).
contrib/completion/git-completion.bash __gitcomp_nl () · line 269
Quoted 10 lines
contrib/completion/git-completion.bash +10
-0+# If provided, only files within the specified directory are listed. +# Sub directories are never recursed. Path must have a trailing +# slash. +__git_index_files () +{ + local dir="$(__gitdir)" + + if [ -d "$dir" ]; then + git --git-dir="$dir" ls-files --exclude-standard "-${1}" "${2}" | + __git_index_file_list_filter ${2} | uniq
Even though everywhere else you seem to quote the variables with dq, but this ${2} is not written as "${2}", which looks odd. Deliberate?
If you wanted to call __git_index_file_list_filter without parameter when the caller did not give you the optional $2, then the above is not the way to write it. It would be ${2+"$2"}. The upstream of the pipe (ls-files) also is getting an empty string as the pathspec when $2 is omitted, and the call will break if this is run under "set -u".
contrib/completion/git-completion.bash __gitcomp_nl () · line 285
Quoted 13 lines
contrib/completion/git-completion.bash +13
-0+# __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)" + + if [ -d "$dir" ]; then + git --git-dir="$dir" diff-index --name-only "${1}" | + __git_index_file_list_filter "${2}" | uniq
This one, when the optional $2 is absent, will call __git_index_file_list_filter with an empty string in its "$1". Intended, or is it also ${2+"$2"}?
Quoted 8 lines
contrib/completion/git-completion.bash +8
-0+# __git_complete_index_file requires 1 argument: the file index +# status mode +__git_complete_index_file () +{ + local pfx cur_="$cur" + + case "$cur_" in + ?*/*)
contrib/completion/git-completion.bash __git_complete_revlist_file () · line 503
Quoted 10 lines
contrib/completion/git-completion.bash +10
-0+ pfx="${cur_%/*}" + cur_="${cur_##*/}" + pfx="${pfx}/" + + __gitcomp_nl "$(__git_index_files ${1} ${pfx})" "$pfx" "$cur_" "" + ;; + *) + __gitcomp_nl "$(__git_index_files ${1})" "" "$cur_" "" + ;; + esac
Please dedent the case arms by one level, i.e.
case $value in
$pattern1)
action1
;;
...
;;
esacQuoted 7 lines
contrib/completion/git-completion.bash +7
-0+# __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
contrib/completion/git-completion.bash __git_complete_revlist_file () · line 522
Quoted 10 lines
contrib/completion/git-completion.bash +10
-0+ ?*/*) + pfx="${cur_%/*}" + cur_="${cur_##*/}" + pfx="${pfx}/" + + __gitcomp_nl "$(__git_diff_index_files ${1} ${pfx})" "$pfx" "$cur_" "" + ;; + *) + __gitcomp_nl "$(__git_diff_index_files ${1})" "" "$cur_" "" + ;;
Unquoted $1 looks fishy, even if the only caller of this function always gives "HEAD" to it (in which case you can do without making it a parameter in the first place).
Unquoted ${pfx} given to __git_diff_index_files also looks fishy.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Il 19/12/2012 20:57, Junio C Hamano ha scritto:
Quoted 22 lines
[jch: again, adding area experts to Cc]
Quoted 6 lines · Manlio Perillo
Changes from version 2:
* Perl is no more used.
* Fixed some coding style issues.
* Refactorized code, to improve future path completion support for the "git reset" command.Thanks. Will replace what was queued on 'pu'.
contrib/completion/git-completion.bash __gitcomp_nl () · line 241
Quoted 5 lines
contrib/completion/git-completion.bash +5
-0+# Process path list returned by "ls-files" and "diff-index --name-only" +# commands, in order to list only file names relative to a specified +# directory, and append a slash to directory names. +# It accepts 1 optional argument: a directory path. The path must have +# a trailing slash.The callsites that call this function, and the way the argument is used, do not make it look like it is an optional argument to me.
Quoted 3 lines
After reading later parts of the patch, I think the callers are wrong (see below) and you did intend to make "$1" optional.
Thanks for the corrections.
As you can see, I'm not very expert in bash programming. I will review the code to use proper escaping and correct optional parameters handling, based on your review.
In this case, I incorrectly assumed that bash expands ${1} to an empty string, in case no arguments are passed to the function.
Quoted 5 lines
Quoted 5 lines
contrib/completion/git-completion.bash +5
-0+__git_index_file_list_filter () +{ + local path + + while read -r path; do
Quoted 9 lines
contrib/completion/git-completion.bash __gitcomp_nl () · line 247
Quoted 1 line
contrib/completion/git-completion.bash +1
-0+ path=${path#$1}This will work correctly only if $1 does not have any shell metacharacter that removes prefix of $path that matches $1 as a pathname expansion pattern. As this file is for bash completion, using string-oriented Bash-isms like ${#1} (to get the length of the prefix) and ${path:$offset} (to get substring) are perfectly fine way to correct it.
Ok.
Quoted 2 lines
Also, as $1 is optional, won't this barf if it is run under "set -u"?
Ok.
Here I should use ${1-}.
Quoted 9 lines
Quoted 9 lines
contrib/completion/git-completion.bash +9
-0+# __git_index_files accepts 1 or 2 arguments: +# 1: A string for file index status mode ("c", "m", "d", "o"), as +# supported by git ls-files command. +# 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_index_files () +{
Quoted 9 lines
contrib/completion/git-completion.bash __gitcomp_nl () · line 269
Quoted 5 lines
contrib/completion/git-completion.bash +5
-0+ local dir="$(__gitdir)" + + if [ -d "$dir" ]; then + git --git-dir="$dir" ls-files --exclude-standard "-${1}" "${2}" | + __git_index_file_list_filter ${2} | uniqEven though everywhere else you seem to quote the variables with dq, but this ${2} is not written as "${2}", which looks odd. Deliberate?
No, I simply missed it.
Quoted 3 lines
If you wanted to call
__git_index_file_list_filterwithout parameter when the caller did not give you the optional $2, then the above is not the way to write it. It would be ${2+"$2"}.
Yes, this seems the better solution.
Quoted 18 lines
[...]
contrib/completion/git-completion.bash __gitcomp_nl () · line 285
Quoted 13 lines
contrib/completion/git-completion.bash +13
-0+# __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)" + + if [ -d "$dir" ]; then + git --git-dir="$dir" diff-index --name-only "${1}" | + __git_index_file_list_filter "${2}" | uniqThis one, when the optional $2 is absent, will call
__git_index_file_list_filterwith an empty string in its "$1". Intended, or is it also ${2+"$2"}?
No, it was not intended. But here it should probably be ${2-}.
One possible rule is:
* ${n+"$n"} should be used by the _git_complete_xxx_file function;
* ${n-} should be used by the _git_xxx_file functions
The alternative is for each function to use ${n-}, or {n+"$n"}.
But I'm not sure. What is the best practice in bash for optional parameters "propagation"?
Quoted 13 lines
Quoted 13 lines
contrib/completion/git-completion.bash +12
-0+# __git_complete_index_file requires 1 argument: the file index +# status mode +__git_complete_index_file () +{ + local pfx cur_="$cur" + + case "$cur_" in + ?*/*) + pfx="${cur_%/*}" + cur_="${cur_##*/}" + pfx="${pfx}/" +
Quoted 9 lines
contrib/completion/git-completion.bash __git_complete_revlist_file () · line 503
Quoted 6 lines
contrib/completion/git-completion.bash +6
-0+ __gitcomp_nl "$(__git_index_files ${1} ${pfx})" "$pfx" "$cur_" "" + ;; + *) + __gitcomp_nl "$(__git_index_files ${1})" "" "$cur_" "" + ;; + esacPlease dedent the case arms by one level, i.e.
I missed it.
Vim do not intent correctly this, and I forgot to dedent.
Quoted 14 lines
[...]
Quoted 13 lines
contrib/completion/git-completion.bash +13
-0+# __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_nl "$(__git_diff_index_files ${1} ${pfx})" "$pfx" "$cur_" ""
Quoted 9 lines
contrib/completion/git-completion.bash __git_complete_revlist_file () · line 522
Quoted 4 lines
contrib/completion/git-completion.bash +4
-0+ ;; + *) + __gitcomp_nl "$(__git_diff_index_files ${1})" "" "$cur_" "" + ;;Unquoted $1 looks fishy, even if the only caller of this function always gives "HEAD" to it (in which case you can do without making it a parameter in the first place).
Currently it is always "HEAD", but in future it may contain an arbitrary tree-ish (for my planned "git reset" path completion support). This is the reason why in version 3 of the patch I added this new __git_complete_diff_index_file function.
Better to quote it.
Quoted 1 line
Unquoted ${pfx} given to
__git_diff_index_filesalso looks fishy.
I missed it.
Thanks Manlio
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAlDSN30ACgkQscQJ24LbaUQPRQCgkQaDyBeXjk5gMJsufGoe9FCe yDkAn2M4d1xRYSkF6P0lQQmENlbYiCb8
=iml7
-----END PGP SIGNATURE-----
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Il 19/12/2012 20:57, Junio C Hamano ha scritto:
Quoted 1 line
[...]
I just found a serious bug with "git commit" path completion.
When doing the first commit on an empty repository, completion will cause an error:
$git commit -m init <TAB>fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
The problem is that for a newly created repository there is no HEAD.
If HEAD does not exists, code must use ls-files instead of diff-index to get the completion list.
Another change is to always call git commands with stderr redirected to /dev/null.
By the way, this is also a bug of current bash completion code:
$ git show does-not-exists:<TAB>fatal: Not a valid object name
does-not-existsI will write a patch (based on master) to fix this.
Quoted 1 line
[...]
Regards Manlio
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAlDSUR0ACgkQscQJ24LbaUSkRwCfVKk9JhSD4sKDFm4heAkVbN0o KAAAn3paTXyUiFlfY/UBpnkwiARegLsE
=7Q5s
-----END PGP SIGNATURE-----