mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 10:36:00 +02:00
Unify glob behavior on open
, rm
, cp-old
, mv
, umv
, cp
and du
commands (#11621)
# Description This pr is a follow up to [#11569](https://github.com/nushell/nushell/pull/11569#issuecomment-1902279587) > Revert the logic in https://github.com/nushell/nushell/pull/10694 and apply the logic in this pr to mv, cp, rv will require a larger change, I need to think how to achieve the bahavior And sorry @bobhy for reverting some of your changes. This pr is going to unify glob behavior on the given commands: * open * rm * cp-old * mv * umv * cp * du So they have the same behavior to `ls`, which is: If given parameter is quoted by single quote(`'`) or double quote(`"`), don't auto-expand the glob pattern. If not quoted, auto-expand the glob pattern. Fixes: #9558 Fixes: #10211 Fixes: #9310 Fixes: #10364 # TODO But there is one thing remains: if we give a variable to the command, it will always auto-expand the glob pattern, e.g: ```nushell let path = "a[123]b" rm $path ``` I don't think it's expected. But I also think user might want to auto-expand the glob pattern in variables. So I'll introduce a new command called `glob escape`, then if user doesn't want to auto-expand the glob pattern, he can just do this: `rm ($path | glob escape)` # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting Done # 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. --> ## NOTE This pr changes the semantic of `GlobPattern`, before this pr, it will `expand path` after evaluated, this makes `nu_engine::glob_from` have no chance to glob things right if a path contains glob pattern. e.g: [#9310 ](https://github.com/nushell/nushell/issues/9310#issuecomment-1886824030) #10211 I think changing the semantic is fine, because it makes glob works if path contains something like '*'. It maybe a breaking change if a custom command's argument are annotated by `: glob`.
This commit is contained in:
@ -1151,23 +1151,6 @@ impl Eval for EvalRuntime {
|
||||
Ok(Value::string(name, span))
|
||||
}
|
||||
|
||||
fn eval_glob_pattern(
|
||||
engine_state: Self::State<'_>,
|
||||
stack: &mut Self::MutState,
|
||||
pattern: String,
|
||||
quoted: bool,
|
||||
span: Span,
|
||||
) -> Result<Value, ShellError> {
|
||||
if quoted {
|
||||
Ok(Value::string(pattern, span))
|
||||
} else {
|
||||
let cwd = current_dir_str(engine_state, stack)?;
|
||||
let path = expand_path_with(pattern, cwd);
|
||||
|
||||
Ok(Value::string(path.to_string_lossy(), span))
|
||||
}
|
||||
}
|
||||
|
||||
fn unreachable(expr: &Expression) -> Result<Value, ShellError> {
|
||||
Ok(Value::nothing(expr.span))
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ use std::{
|
||||
|
||||
use nu_glob::MatchOptions;
|
||||
use nu_path::{canonicalize_with, expand_path_with};
|
||||
use nu_protocol::{ShellError, Span, Spanned};
|
||||
use nu_protocol::{NuPath, ShellError, Span, Spanned};
|
||||
|
||||
const GLOB_CHARS: &[char] = &['*', '?', '['];
|
||||
|
||||
@ -18,7 +18,7 @@ const GLOB_CHARS: &[char] = &['*', '?', '['];
|
||||
/// The second of the two values is an iterator over the matching filepaths.
|
||||
#[allow(clippy::type_complexity)]
|
||||
pub fn glob_from(
|
||||
pattern: &Spanned<String>,
|
||||
pattern: &Spanned<NuPath>,
|
||||
cwd: &Path,
|
||||
span: Span,
|
||||
options: Option<MatchOptions>,
|
||||
@ -29,10 +29,11 @@ pub fn glob_from(
|
||||
),
|
||||
ShellError,
|
||||
> {
|
||||
let (prefix, pattern) = if pattern.item.contains(GLOB_CHARS) {
|
||||
let no_glob_for_pattern = matches!(pattern.item, NuPath::Quoted(_));
|
||||
let (prefix, pattern) = if pattern.item.as_ref().contains(GLOB_CHARS) {
|
||||
// Pattern contains glob, split it
|
||||
let mut p = PathBuf::new();
|
||||
let path = PathBuf::from(&pattern.item);
|
||||
let path = PathBuf::from(&pattern.item.as_ref());
|
||||
let components = path.components();
|
||||
let mut counter = 0;
|
||||
|
||||
@ -52,6 +53,9 @@ pub fn glob_from(
|
||||
just_pattern.push(comp);
|
||||
}
|
||||
}
|
||||
if no_glob_for_pattern {
|
||||
just_pattern = PathBuf::from(nu_glob::Pattern::escape(&just_pattern.to_string_lossy()));
|
||||
}
|
||||
|
||||
// Now expand `p` to get full prefix
|
||||
let path = expand_path_with(p, cwd);
|
||||
@ -59,7 +63,7 @@ pub fn glob_from(
|
||||
|
||||
(Some(path), escaped_prefix.join(just_pattern))
|
||||
} else {
|
||||
let path = PathBuf::from(&pattern.item);
|
||||
let path = PathBuf::from(&pattern.item.as_ref());
|
||||
let path = expand_path_with(path, cwd);
|
||||
let is_symlink = match fs::symlink_metadata(&path) {
|
||||
Ok(attr) => attr.file_type().is_symlink(),
|
||||
@ -70,7 +74,14 @@ pub fn glob_from(
|
||||
(path.parent().map(|parent| parent.to_path_buf()), path)
|
||||
} else {
|
||||
let path = if let Ok(p) = canonicalize_with(path.clone(), cwd) {
|
||||
p
|
||||
if p.to_string_lossy().contains(GLOB_CHARS) {
|
||||
// our path might contains GLOB_CHARS too
|
||||
// in such case, we need to escape our path to make
|
||||
// glob work successfully
|
||||
PathBuf::from(nu_glob::Pattern::escape(&p.to_string_lossy()))
|
||||
} else {
|
||||
p
|
||||
}
|
||||
} else {
|
||||
return Err(ShellError::DirectoryNotFound {
|
||||
dir: path.to_string_lossy().to_string(),
|
||||
|
Reference in New Issue
Block a user