bash-completion
bash-completion copied to clipboard
fix(scp): keep tilde in local paths by using _filedir instead of _expand
Do not replace ~
by /home/user/
for local files to get behavior of scp
consistent with other tools like ls
. I hope, dropping _expand
and adding _filedir
to have file name completion will not cause issues like Ville Skyttä [Bash-completion-devel] _expand
, scp and quoting when a patch intended to fix Debian #521406. bash-completion: leading tilde always expanded
was reverted.
I have a little experience with bash completion, so I can easily miss important use cases. You can consider this pull request as an invitation to discuss how to properly fix the issue with expanding of tilde during completion of scp
arguments.
I do not like when ~
is expanded by /home/user
since it makes commands longer and more noisy. For a decade I had a hack from [1] redefining _expand
in my .bashrc
file. Recently I decided to try if the problem has been fixed and the snippet may be removed. For most of commands completion works accordingly to my preferences, but for some tools tilde is expanded. I checked bug reports another time and realized that a fix was committed and reverted. Due to other changes, _longopt
widely used for various tools preserves tilde however. Maybe it is better to fix particular tools including scp
than try to patch _expand
(that is not used by _filedir
called from _longopt
and helpers specific to various tools).
The cases that this change should address:
scp ~/Do
scp localhost:/tmp/file ~us
[1] lp:622403 bash-completion: readline expand-tilde not acknowledged
The pathname completions are originally processed by _comp_xfunc_ssh_scp_local_files
. With this PR, filenames are doubly generated.
$ scp ./COPYING[TAB twice]
COPYING COPYING
$ scp ./COPYING[TAB twice] COPYING COPYING
Mea culpa. I have added a bit more code to avoid _comp_xfunc_ssh_scp_local_files when possible, however I am not sure that I have not broken something else. Counting COMPREPLY
options may be fragile, but I have not managed to pass variants through some function to determine if file exists and whether it is a directory, but keeping tildes. I do not consider code with a subprocess per each completion variant.
While I dislike the tilde expansion myself as well and do support efforts to get rid of it as much as possible, I don't think we should make behavioral changes here without a set of unit tests having good coverage over the possibilities. This has been a tricky and bug prone thing in the past.
The same probably applies to all other uses of _expand
we have as well (rsync and sshfs at least -- I suspect the instance in povray
is a false one that could be just removed, but IIRC I had no way to verify it when I tried back in the day).
While I dislike the tilde expansion myself as well and do support efforts to get rid of it as much as possible, I don't think we should make behavioral changes here without a set of unit tests having good coverage over the possibilities. This has been a tricky and bug prone thing in the past.
It would be great to have unit tests and it can be done by an independent pull request since it would be improvement even in the case of declining of this change.
In the meanwhile I have updated the patch to avoid changing of nospace
option. I still do not like scp
related code. Frankly speaking, creating the pull request I was expecting reaction like "it is a legacy code, look at the command X that can do the same in a robust way". Are there some hints which files may be considered as a kind of style guide?
I am not motivated enough to create unit tests for so fragile code. I have missed an obvious issue with initial variant of my patch partially because I noticed enough glitches with original code:
- File names with newline characters (
M-/
works) #704. -
\*2
for*2
file unescaped in response to[Tab]
. - File names with trailing
@
are completed without it due tols
-based approach. -
-F
as a prefix is supported, but it seems there are-iidentity
and-Sprogram
variants. - IPv6 addressed are displayed as completion variants but not actually completed.
-
scp -F-F [Tab]
causessed
help message due to unknown-F
option (I was intended to check./-F
file name). - Looking at the file list to find a source for inspiration, I discovered that
perl -x~fil[Tab]
treats tab as at the beginning of word, so completes user names, not files started with literal tilde.