glob with ../ prefix now works; (#10504)

Fixes #10503 
Also improves link to metacharacter help;

# Description
`glob` code was using pattern as provided by user. If that had leading
`..\`, `wax::Glob` is documented to treat them as literal chars to be
matched.
Fix is to use `wax::Glob.partition()` to split such invariant prefixes
off the pattern and tack them onto the working directory computed
separately.

Before
```
> ls ..
╭───┬───────┬──────┬──────┬───────────────╮
│ # │ name  │ type │ size │   modified    │
├───┼───────┼──────┼──────┼───────────────┤
│ 0 │ ../r1 │ dir  │  7 B │ 3 hours ago   │
│ 1 │ ../r2 │ dir  │  3 B │ a day ago     │
│ 2 │ ../r3 │ dir  │ 13 B │ 4 minutes ago │
╰───┴───────┴──────┴──────┴───────────────╯
> glob ../r*
╭────────────╮
│ empty list │
╰────────────╯
```
After 
```
> glob ../r*
╭───┬──────────────────────────────╮
│ 0 │ /home/bobhy/src/rust/work/r2 │
│ 1 │ /home/bobhy/src/rust/work/r1 │
│ 2 │ /home/bobhy/src/rust/work/r3 │
╰───┴──────────────────────────────╯
```

# User-Facing Changes
None

# Tests + Formatting
- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

# 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>
This commit is contained in:
Bob Hyman
2023-09-29 04:48:55 -07:00
committed by GitHub
parent 78d0e1d0b8
commit 9a0c6f2e02
2 changed files with 77 additions and 4 deletions

View File

@ -123,7 +123,7 @@ impl Command for Glob {
}
fn extra_usage(&self) -> &str {
r#"For more glob pattern help, please refer to https://github.com/olson-sean-k/wax"#
r#"For more glob pattern help, please refer to https://docs.rs/crate/wax/latest"#
}
fn run(
@ -135,7 +135,6 @@ impl Command for Glob {
) -> Result<PipelineData, ShellError> {
let ctrlc = engine_state.ctrlc.clone();
let span = call.head;
let path = current_dir(engine_state, stack)?;
let glob_pattern: Spanned<String> = call.req(engine_state, stack, 0)?;
let depth = call.get_flag(engine_state, stack, "depth")?;
let no_dirs = call.has_flag("no-dir");
@ -173,8 +172,8 @@ impl Command for Glob {
usize::MAX
};
let glob = match WaxGlob::new(&glob_pattern.item) {
Ok(p) => p,
let (prefix, glob) = match WaxGlob::new(&glob_pattern.item) {
Ok(p) => p.partition(),
Err(e) => {
return Err(ShellError::GenericError(
"error with glob pattern".to_string(),
@ -186,6 +185,25 @@ impl Command for Glob {
}
};
let path = current_dir(engine_state, stack)?;
let path = match nu_path::canonicalize_with(prefix, path) {
Ok(path) => path,
Err(e) if e.to_string().contains("os error 2") =>
// path we're trying to glob doesn't exist,
{
std::path::PathBuf::new() // user should get empty list not an error
}
Err(e) => {
return Err(ShellError::GenericError(
"error in canonicalize".to_string(),
format!("{e}"),
Some(glob_pattern.span),
None,
Vec::new(),
))
}
};
Ok(if !not_patterns.is_empty() {
let np: Vec<&str> = not_patterns.iter().map(|s| s as &str).collect();
let glob_results = glob