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

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

From
Manlio Perillo <manlio.perillo@gmail.com>
Date
Apr 23, 2013, 15:25 UTC
Message-ID
<5176A7E4.2070608@gmail.com>
In-Reply-To
<CAMP44s1TjTiZ9HsXn9YiJ8E6+jC=s+g_tps6AY2ixrrgX=0jnw@mail.gmail.com>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Il 21/04/2013 12:14, Felipe Contreras ha scritto:
Show 34 quoted lines
> On Fri, Jan 11, 2013 at 12:48 PM, Manlio Perillo
> <manlio.perillo@gmail.com> wrote:
>> 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.
> 
>> +__git_index_file_list_filter_compat ()
>> +{
>> +       local path
>> +
>> +       while read -r path; do
>> +               case "$path" in
>> +               ?*/*) echo "${path%%/*}/" ;;
>> +               *) echo "$path" ;;
>> +               esac
>> +       done
>> +}
>> +
>> +__git_index_file_list_filter_bash ()
>> +{
>> +       local path
>> +
>> +       while read -r path; do
>> +               case "$path" in
>> +               ?*/*)
>> +                       # XXX if we append a slash to directory names when using
>> +                       # `compopt -o filenames`, Bash will append another slash.
>> +                       # This is pretty stupid, and this the reason why we have to
>> +                       # define a compatible version for this function.
>> +                       echo "${path%%/*}" ;;
> 
> Which version of bash is that? It works perfectly fine here with or
> without the /.
> 

GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu) on a GNU Linux Debian 6

Show 39 quoted lines
>> +# __git_index_files accepts 1 or 2 arguments:
>> +# 1: Options to pass to ls-files (required).
>> +#    Supported options are --cached, --modified, --deleted, --others,
>> +#    and --directory.
>> +# 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)" root="${2-.}"
>> +
>> +       if [ -d "$dir" ]; then
>> +               __git_ls_files_helper "$root" "$1" | __git_index_file_list_filter |
>> +                       sort | 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)" root="${2-.}"
>> +
>> +       if [ -d "$dir" ]; then
>> +               __git_diff_index_helper "$root" "$1" | __git_index_file_list_filter |
>> +                       sort | uniq
>> +       fi
>> +}
> 
> These two are exactly the same, except one calls
> __git_ls_files_helper, and the other one __git_diff_index_helper,
> can't we make another argument that is and select one or the other
> based on that?
> 
They are not exactly the same.

The first function requires, as first parameter, (space separed) options to pass to ls-files command; the second function, instead, requires the id of a tree object.

IMHO, using only one function may be confusing.
Show 50 quoted lines
>>  __git_heads ()
>>  {
>>         local dir="$(__gitdir)"
>> @@ -430,6 +543,46 @@ __git_complete_revlist_file ()
>>  }
>>
>>
>> +# __git_complete_index_file requires 1 argument: the options to pass to
>> +# ls-file
>> +__git_complete_index_file ()
>> +{
>> +       local pfx cur_="$cur"
>> +
>> +       case "$cur_" in
>> +       ?*/*)
>> +               pfx="${cur_%/*}"
>> +               cur_="${cur_##*/}"
>> +               pfx="${pfx}/"
>> +
>> +               __gitcomp_file "$(__git_index_files "$1" "$pfx")" "$pfx" "$cur_"
>> +               ;;
>> +       *)
>> +               __gitcomp_file "$(__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_file "$(__git_diff_index_files "$1" "$pfx")" "$pfx" "$cur_"
>> +               ;;
>> +       *)
>> +               __gitcomp_file "$(__git_diff_index_files "$1")" "" "$cur_"
>> +               ;;
>> +       esac
>> +}
> 
> These are also exactly the same, we could pass the argument to the
> function above.
> 
See previous note.
Regards  Manlio Perillo
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAlF2p+QACgkQscQJ24LbaUQVcACfeYFO8umJDbgTrXWChqqbk69E CE4AniZFP7PQkOZCbBY+6hZ2gMpNIJTn =HqAf -----END PGP SIGNATURE-----

Previous: Felipe ContrerasNext: Felipe Contreras
Message 8 of 9 in “git-completion.bash: add support for path completion”
  1. git-completion.bash: add support for path completionManlio Perillo, Jan 11, 2013
  2. Junio C HamanoJan 11, 2013
  3. Manlio PerilloJan 12, 2013
  4. Junio C HamanoJan 13, 2013
  5. Manlio PerilloJan 12, 2013
  6. Junio C HamanoJan 13, 2013
  7. Felipe ContrerasApr 21, 2013
  8. Manlio PerilloApr 23, 2013
  9. Felipe ContrerasApr 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.