Skip to content

Commit

Permalink
status: fix "--no-root-relative" handling
Browse files Browse the repository at this point in the history
Summary: Previously with HGPLAIN enabled, it was impossible to enable CWD relative output using the "no-" version of the "--root-relative" flag. Fix by representing the flag as Option<bool>, and only use HGPLAIN to default if the flag was not specified.

Reviewed By: quark-zju

Differential Revision: D45717808

fbshipit-source-id: e1830fd42fa50a5a6188fc5d1d6fc32f0ebf9518
  • Loading branch information
muirdm authored and facebook-github-bot committed May 10, 2023
1 parent dfc201b commit bb37a6e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
37 changes: 34 additions & 3 deletions eden/scm/lib/cliparser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ impl From<Value> for bool {
}
}

impl From<Value> for Option<bool> {
fn from(v: Value) -> Self {
match v {
Value::Bool(b) => b,
_ => panic!("programming error: {:?} was converted to bool", v),
}
}
}

impl From<Value> for Vec<String> {
fn from(v: Value) -> Self {
match v {
Expand All @@ -203,6 +212,12 @@ impl From<bool> for Value {
}
}

impl From<Option<bool>> for Value {
fn from(v: Option<bool>) -> Self {
Value::Bool(v)
}
}

impl From<&str> for Value {
fn from(v: &str) -> Self {
Value::Str(v.to_string())
Expand Down Expand Up @@ -1384,7 +1399,7 @@ mod tests {
let flags = vec![(None, "foo", "foo desc", "", "").into()];
let parsed = ParseOptions::new()
.flags(flags)
.parse_args(&vec!["--no-foo", "bar"])
.parse_args(&["--no-foo", "bar"])
.unwrap();
let foo: String = parsed.pick("foo");
assert_eq!(foo, "bar");
Expand All @@ -1396,7 +1411,7 @@ mod tests {
let flags = vec![(None, "no-foo", "foo desc", "", "").into()];
let parsed = ParseOptions::new()
.flags(flags)
.parse_args(&vec!["--foo", "bar"])
.parse_args(&["--foo", "bar"])
.unwrap();
let foo: String = parsed.pick("no-foo");
assert_eq!(foo, "bar");
Expand All @@ -1420,10 +1435,26 @@ mod tests {
let parsed = parser.parse_args(&args).unwrap();
assert_eq!(parsed.pick::<Option<String>>("opt_str"), None);

let parsed = parser.parse_args(&vec!["--opt_str", "foo"]).unwrap();
let parsed = parser.parse_args(&["--opt_str", "foo"]).unwrap();
assert_eq!(
parsed.pick::<Option<String>>("opt_str"),
Some("foo".to_string())
);
}

#[test]
fn test_parse_option_bool_value() {
let flags = vec![(None, "opt-bool", "a bool", Value::Bool(None), "").into()];
let parser = ParseOptions::new().flags(flags).into_parser();

let args: Vec<&str> = Default::default();
let parsed = parser.parse_args(&args).unwrap();
assert_eq!(parsed.pick::<Option<bool>>("opt-bool"), None);

let parsed = parser.parse_args(&["--opt-bool"]).unwrap();
assert_eq!(parsed.pick::<Option<bool>>("opt-bool"), Some(true),);

let parsed = parser.parse_args(&["--no-opt-bool"]).unwrap();
assert_eq!(parsed.pick::<Option<bool>>("opt-bool"), Some(false),);
}
}
7 changes: 5 additions & 2 deletions eden/scm/lib/hgcommands/src/commands/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ define_flags! {
change: String,

/// show status relative to root
root_relative: bool,
root_relative: Option<bool>,

walk_opts: WalkOpts,
formatter_opts: FormatterOpts,
Expand Down Expand Up @@ -164,7 +164,10 @@ pub fn run(ctx: ReqCtx<StatusOpts>, repo: &mut Repo, wc: &mut WorkingCopy) -> Re
.config()
.get_or::<bool>("ui", "statuscopies", || false)?,
endl: if ctx.opts.print0 { '\0' } else { '\n' },
root_relative: ctx.opts.root_relative || hgplain::is_plain(None),
root_relative: ctx
.opts
.root_relative
.unwrap_or_else(|| hgplain::is_plain(None)),
};

let (status, copymap) = match repo.config().get_or_default("status", "use-rust")? {
Expand Down
7 changes: 7 additions & 0 deletions eden/scm/tests/test-fb-ext-tweakdefaults.t
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,13 @@ Don't break automation
A dir1/file with space
A dir1/subdir1/subf1

But can still manually disable root-relative:
$ HGPLAIN=1 hg status --no-root-relative
A ../-v
A ../f1
A ../file with space
A ../subdir1/subf1

This tag is kept to keep the rest of the test consistent:
$ echo >> ../.hgtags
$ hg commit -Aqm "add foo tag"
Expand Down

0 comments on commit bb37a6e

Please sign in to comment.