Skip to content

Commit

Permalink
pathmatcher: provide a hint when users use -R with a cwd-relative path
Browse files Browse the repository at this point in the history
Summary:
A common mistake is to use -R, but specify a file relative to the repo
instead of cwd.  Detect that case, and provide a hint to the user.

Reviewed By: muirdm

Differential Revision: D69250238

fbshipit-source-id: 84195a55a4e5986d809d3c3055aca64e1d18ab0c
  • Loading branch information
zzl0 authored and facebook-github-bot committed Feb 6, 2025
1 parent 8c26dcf commit 1b4bf29
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
31 changes: 23 additions & 8 deletions eden/scm/lib/pathmatcher/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,30 @@

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("unsuppported pattern kind {0}")]
UnsupportedPatternKind(String),

#[error("{0} not under root '{1}'")]
PathOutsideRoot(String, String),

#[error("non-utf8 path '{0}' when building pattern")]
PathOutsideRoot(String, String, bool),
NonUtf8(String),

#[error("listfile:- may only be used once as a direct CLI argument")]
StdinUnavailable,
}

impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::UnsupportedPatternKind(s) => write!(f, "unsupported pattern kind {}", s),
Error::PathOutsideRoot(path, root, show_hint) => {
let message = format!("cwd relative path '{}' is not under root '{}'", path, root);
if *show_hint {
let hint_message = "consider using --cwd to change working directory";
write!(f, "{}\n(hint: {})", message, hint_message)
} else {
write!(f, "{}", message)
}
}
Error::NonUtf8(s) => write!(f, "non-utf8 path '{}' when building pattern", s),
Error::StdinUnavailable => write!(
f,
"listfile:- may only be used once as a direct CLI argument"
),
}
}
}
2 changes: 2 additions & 0 deletions eden/scm/lib/pathmatcher/src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,11 @@ where
.into_string()
.map_err(|s| Error::NonUtf8(s.to_string_lossy().to_string()))?,
None => {
let show_hint = !cwd.starts_with(root);
return Err(Error::PathOutsideRoot(
pat.to_string(),
root.to_string_lossy().to_string(),
show_hint,
)
.into());
}
Expand Down
3 changes: 2 additions & 1 deletion eden/scm/tests/test-alias.t
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ no closing quotation
"--" in alias definition should be preserved
$ hg --config alias.dash='cat --' -R alias dash -r0
abort: -r0 not under root '$TESTTMP/alias'
abort: cwd relative path '-r0' is not under root '$TESTTMP/alias'
(hint: consider using --cwd to change working directory)
[255]
invalid options
Expand Down
3 changes: 2 additions & 1 deletion eden/scm/tests/test-parents.t
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ hg parents c, single revision


$ hg parents -r 'max(desc(b))' ../a
abort: ../a not under root '$TESTTMP/repo'
abort: cwd relative path '../a' is not under root '$TESTTMP/repo'
(hint: consider using --cwd to change working directory)
[255]


Expand Down
9 changes: 6 additions & 3 deletions eden/scm/tests/test-walk.t
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,12 @@
f mammals/Procyonidae/raccoon mammals/Procyonidae/raccoon
f mammals/skunk mammals/skunk
$ hg debugwalk ..
abort: .. not under root '$TESTTMP/t'
abort: cwd relative path '..' is not under root '$TESTTMP/t'
(hint: consider using --cwd to change working directory)
[255]
$ hg debugwalk beans/../..
abort: beans/../.. not under root '$TESTTMP/t'
abort: cwd relative path 'beans/../..' is not under root '$TESTTMP/t'
(hint: consider using --cwd to change working directory)
[255]
$ hg debugwalk .hg
abort: path contains illegal component: .hg
Expand Down Expand Up @@ -306,7 +308,8 @@ Test absolute paths:
f beans/pinto beans/pinto
f beans/turtle beans/turtle
$ hg debugwalk `pwd`/..
abort: $TESTTMP/t/.. not under root '$TESTTMP/t'
abort: cwd relative path '$TESTTMP/t/..' is not under root '$TESTTMP/t'
(hint: consider using --cwd to change working directory)
[255]

Test patterns:
Expand Down

0 comments on commit 1b4bf29

Please sign in to comment.