nushell/crates/nu-engine/src/glob_from.rs
Horasal e5145358eb
treat path contains '?' as pattern (#10142)
Fix https://github.com/nushell/nushell/issues/10136

# Description
Current nushell only handle path containing '*' as match pattern and
treat '?' as just normal path.
This pr makes path containing '?' is also processed as pattern.

🔴 **Concerns: Need to design/comfirm a consistent rule to handle
dirs/files with '?' in their names.**

Currently:

- if no dir has exactly same name with pattern, it will print the list
of matched directories
- if pattern exactly matches an empty dir's name, it will just print the
empty dir's content ( i.e. `[]`)
- if pattern exactly matches an dir's name, it will perform pattern
match and print all the dir contains

e.g.
```bash
mkdir src
ls s?c 
```

| name | type | size   | modified                                      |
| ---- | ---- | ------ | --------------------------------------------- |
| src  | dir  | 1.1 KB | Tue, 29 Aug 2023 07:39:41 +0900 (9 hours ago) |

-----------

```bash
mkdir src
mkdir scc
mkdir scs
ls s?c
```

| name | type | size | modified |
| ---- | ---- | ------ |
------------------------------------------------ |
| scc | dir | 64 B | Tue, 29 Aug 2023 16:55:31 +0900 (14 seconds ago) |
| src | dir | 1.1 KB | Tue, 29 Aug 2023 07:39:41 +0900 (9 hours ago) |

-----------

```bash
mkdir  s?c
ls s?c
```

print empty (i.e. ls of dir `s?c`)

-----------

```bash
mkdir -p  s?c/test
ls s?c
```
|name|type|size|modified|
|-|-|-|-|
|s?c/test|dir|64 B|Tue, 29 Aug 2023 16:47:53 +0900 (2 minutes ago)|
|src/bytes|dir|480 B|Fri, 25 Aug 2023 17:43:52 +0900 (3 days ago)|
|src/charting|dir|160 B|Fri, 25 Aug 2023 17:43:52 +0900 (3 days ago)|
|src/conversions|dir|160 B|Fri, 25 Aug 2023 17:43:52 +0900 (3 days ago)|

-----------

# User-Facing Changes

User will be able to use '?' to match directory/file.

# Tests + Formatting

- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

# After Submitting

None

---------

Co-authored-by: Horasal <horsal@horsal.dev>
2023-09-03 19:25:00 -05:00

108 lines
3.3 KiB
Rust

use std::{
fs,
path::{Component, Path, PathBuf},
};
use nu_glob::MatchOptions;
use nu_path::{canonicalize_with, expand_path_with};
use nu_protocol::{ShellError, Span, Spanned};
const GLOB_CHARS: &[char] = &['*', '?', '['];
/// This function is like `nu_glob::glob` from the `glob` crate, except it is relative to a given cwd.
///
/// It returns a tuple of two values: the first is an optional prefix that the expanded filenames share.
/// This prefix can be removed from the front of each value to give an approximation of the relative path
/// to the user
///
/// The second of the two values is an iterator over the matching filepaths.
#[allow(clippy::type_complexity)]
pub fn glob_from(
pattern: &Spanned<String>,
cwd: &Path,
span: Span,
options: Option<MatchOptions>,
) -> Result<
(
Option<PathBuf>,
Box<dyn Iterator<Item = Result<PathBuf, ShellError>> + Send>,
),
ShellError,
> {
let (prefix, pattern) = if pattern.item.contains(GLOB_CHARS) {
// Pattern contains glob, split it
let mut p = PathBuf::new();
let path = PathBuf::from(&pattern.item);
let components = path.components();
let mut counter = 0;
for c in components {
if let Component::Normal(os) = c {
if os.to_string_lossy().contains(GLOB_CHARS) {
break;
}
}
p.push(c);
counter += 1;
}
let mut just_pattern = PathBuf::new();
for c in counter..path.components().count() {
if let Some(comp) = path.components().nth(c) {
just_pattern.push(comp);
}
}
// Now expand `p` to get full prefix
let path = expand_path_with(p, cwd);
let escaped_prefix = PathBuf::from(nu_glob::Pattern::escape(&path.to_string_lossy()));
(Some(path), escaped_prefix.join(just_pattern))
} else {
let path = PathBuf::from(&pattern.item);
let path = expand_path_with(path, cwd);
let is_symlink = match fs::symlink_metadata(&path) {
Ok(attr) => attr.file_type().is_symlink(),
Err(_) => false,
};
if is_symlink {
(path.parent().map(|parent| parent.to_path_buf()), path)
} else {
let path = if let Ok(p) = canonicalize_with(path, cwd) {
p
} else {
return Err(ShellError::DirectoryNotFound(pattern.span, None));
};
(path.parent().map(|parent| parent.to_path_buf()), path)
}
};
let pattern = pattern.to_string_lossy().to_string();
let glob_options = options.unwrap_or_else(MatchOptions::new);
let glob = nu_glob::glob_with(&pattern, glob_options).map_err(|err| {
nu_protocol::ShellError::GenericError(
"Error extracting glob pattern".into(),
err.to_string(),
Some(span),
None,
Vec::new(),
)
})?;
Ok((
prefix,
Box::new(glob.map(move |x| match x {
Ok(v) => Ok(v),
Err(err) => Err(nu_protocol::ShellError::GenericError(
"Error extracting glob pattern".into(),
err.to_string(),
Some(span),
None,
Vec::new(),
)),
})),
))
}