nushell/crates/nu-command/src
Bob Hyman 09b3dab35d
Allow filesystem commands to access files with glob metachars in name (#10694)
(squashed version of #10557, clean commit history and review thread)

Fixes #10571, also potentially: #10364, #10211, #9558, #9310,


# Description
Changes processing of arguments to filesystem commands that are source
paths or globs.
Applies to `cp, cp-old, mv, rm, du` but not `ls` (because it uses a
different globbing interface) or `glob` (because it uses a different
globbing library).

The core of the change is to lookup the argument first as a file and
only glob if it is not. That way,
a path containing glob metacharacters can be referenced without glob
quoting, though it will have to be single quoted to avoid nushell
parsing.

Before: A file path that looks like a glob is not matched by the glob
specified as a (source) argument and takes some thinking about to
access. You might say the glob pattern shadows a file with the same
spelling.
```
> ls a*
╭───┬────────┬──────┬──────┬────────────────╮
│ # │  name  │ type │ size │    modified    │
├───┼────────┼──────┼──────┼────────────────┤
│ 0 │ a[bc]d │ file │  0 B │ 34 seconds ago │
│ 1 │ abd    │ file │  0 B │ now            │
│ 2 │ acd    │ file │  0 B │ now            │
╰───┴────────┴──────┴──────┴────────────────╯

> cp --verbose 'a[bc]d' dest
copied /home/bobhy/src/rust/work/r4/abd to /home/bobhy/src/rust/work/r4/dest/abd
copied /home/bobhy/src/rust/work/r4/acd to /home/bobhy/src/rust/work/r4/dest/acd

> ## Note -- a[bc]d *not* copied, and seemingly hard to access.
> cp --verbose 'a\[bc\]d' dest
Error:   × No matches found
   ╭─[entry #33:1:1]
 1 │ cp --verbose 'a\[bc\]d' dest
   ·              ─────┬────
   ·                   ╰── no matches found
   ╰────

> #.. but is accessible with enough glob quoting.
> cp --verbose 'a[[]bc[]]d' dest
copied /home/bobhy/src/rust/work/r4/a[bc]d to /home/bobhy/src/rust/work/r4/dest/a[bc]d
```
Before_2: if file has glob metachars but isn't a valid pattern, user
gets a confusing error:

```
> touch 'a[b'
> cp 'a[b' dest
Error:   × Pattern syntax error near position 30: invalid range pattern
   ╭─[entry #13:1:1]
 1 │ cp 'a[b' dest
   ·    ──┬──
   ·      ╰── invalid pattern
   ╰────
```

After: Args to cp, mv, etc. are tried first as literal files, and only
as globs if not found to be files.

```
> cp --verbose 'a[bc]d' dest
copied /home/bobhy/src/rust/work/r4/a[bc]d to /home/bobhy/src/rust/work/r4/dest/a[bc]d
> cp --verbose '[a][bc]d' dest
copied /home/bobhy/src/rust/work/r4/abd to /home/bobhy/src/rust/work/r4/dest/abd
copied /home/bobhy/src/rust/work/r4/acd to /home/bobhy/src/rust/work/r4/dest/acd
```
After_2: file with glob metachars but invalid pattern just works.
(though Windows does not allow file name to contain `*`.).

```
> cp --verbose 'a[b' dest
copied /home/bobhy/src/rust/work/r4/a[b to /home/bobhy/src/rust/work/r4/dest/a[b
```

So, with this fix, a file shadows a glob pattern with the same spelling.
If you have such a file and really want to use the glob pattern, you
will have to glob quote some of the characters in the pattern. I think
that's less confusing to the user: if ls shows a file with a weird name,
s/he'll still be able to copy, rename or delete it.

# User-Facing Changes
Could break some existing scripts. If user happened to have a file with
a globbish name but was using a glob pattern with the same spelling, the
new version will process the file and not expand the glob.

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->

---------

Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-10-18 13:31:15 -05:00
..
bytes Add long options for bits and bytes (#10601) 2023-10-05 18:45:28 +02:00
charting Move Value to helpers, separate span call (#10121) 2023-09-03 07:27:29 -07:00
conversions remove into decimal (#10341) 2023-10-10 20:05:44 +02:00
database Move Value to helpers, separate span call (#10121) 2023-09-03 07:27:29 -07:00
date fix clippy (#10659) 2023-10-10 03:31:15 +13:00
debug add a debug info command to show memory info (#10711) 2023-10-14 12:28:48 -05:00
env Fix editor config for reedline and config nu/env (#10535) 2023-09-29 16:36:03 +02:00
experimental Add functions for each Value case (#9736) 2023-07-21 08:20:33 -05:00
filesystem Allow filesystem commands to access files with glob metachars in name (#10694) 2023-10-18 13:31:15 -05:00
filters Add long options for filters (#10641) 2023-10-08 13:12:46 +02:00
formats Deprecate to xml --pretty {int} in favor of --indent {int} (#10660) 2023-10-09 19:05:33 +02:00
generators Use int type name consistently (#10579) 2023-10-03 18:24:32 +02:00
hash Move Value to helpers, separate span call (#10121) 2023-09-03 07:27:29 -07:00
help Add 'help escapes' command for quick reference of nushell string escapes (#10522) 2023-09-30 09:04:27 -05:00
math Fix output types of math commands to be narrower (#9740) 2023-10-11 21:26:35 +02:00
misc REFACTOR: move source out of deprecated commands (#9060) 2023-05-04 00:02:03 +02:00
network give better error if required field of url join is invalid (#10589) 2023-10-10 19:24:23 +02:00
path add examples with .. and / to path join (#10620) 2023-10-09 18:58:32 +02:00
platform Allow filesystem commands to access files with glob metachars in name (#10694) 2023-10-18 13:31:15 -05:00
random remove random decimal (#10342) 2023-10-10 18:57:53 +02:00
removed Rename misused "deprecation" to removal (#10000) 2023-08-15 07:17:31 +12:00
shells Fix usage for the exit command. (#9450) 2023-06-16 10:09:02 +02:00
strings fix clippy (#10659) 2023-10-10 03:31:15 +13:00
system Fix clippy in registry_query.rs (#10652) 2023-10-09 16:19:20 +08:00
viewers Revert "Port command examples to long option" (#10597) 2023-10-04 09:41:13 +13:00
default_context.rs add a debug info command to show memory info (#10711) 2023-10-14 12:28:48 -05:00
example_test.rs feat: Add unfold command (#10489) 2023-09-30 09:08:06 -05:00
lib.rs Move eval_hook to nu-cmd-base (#10146) 2023-08-29 23:46:50 +02:00
progress_bar.rs cp progress bar implementation (#8012) 2023-02-22 11:57:38 -08:00
sort_utils.rs Use slices directly instead of &Vec (#10328) 2023-09-12 11:38:20 +08:00