From a0bb94886846bedac8a2e70466c10b51b787efc8 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 06:47:41 -0600 Subject: [PATCH] fix(id): reject conflicting first and last limits --- pkgs/id/src/cli.rs | 83 +++++++++++++++++++++++++++++--- pkgs/id/src/repl/runner.rs | 99 ++++++++++++++++++++++++++------------ 2 files changed, 143 insertions(+), 39 deletions(-) diff --git a/pkgs/id/src/cli.rs b/pkgs/id/src/cli.rs index f8e103b9eb..d3f9b2fdb6 100644 --- a/pkgs/id/src/cli.rs +++ b/pkgs/id/src/cli.rs @@ -723,10 +723,20 @@ pub enum Command { #[arg(short = 'o', long)] output: Option, /// Return only the first N matches (default 1 if no number given). - #[arg(long, num_args = 0..=1, default_missing_value = "1")] + #[arg( + long, + conflicts_with = "last", + num_args = 0..=1, + default_missing_value = "1" + )] first: Option, /// Return only the last N matches (default 1 if no number given). - #[arg(long, num_args = 0..=1, default_missing_value = "1")] + #[arg( + long, + conflicts_with = "first", + num_args = 0..=1, + default_missing_value = "1" + )] last: Option, /// Exclude matches where name or hash contains this pattern (repeatable). #[arg(long, action = clap::ArgAction::Append)] @@ -804,10 +814,20 @@ pub enum Command { #[arg(long)] all: bool, /// Return only the first N matches (default 1 if no number given). - #[arg(long, num_args = 0..=1, default_missing_value = "1")] + #[arg( + long, + conflicts_with = "last", + num_args = 0..=1, + default_missing_value = "1" + )] first: Option, /// Return only the last N matches (default 1 if no number given). - #[arg(long, num_args = 0..=1, default_missing_value = "1")] + #[arg( + long, + conflicts_with = "first", + num_args = 0..=1, + default_missing_value = "1" + )] last: Option, /// Exclude matches where name or hash contains this pattern (repeatable). #[arg(long, action = clap::ArgAction::Append)] @@ -890,10 +910,20 @@ pub enum Command { #[arg(long, default_value = "tag")] format: String, /// Return only the first N matches (default 1 if no number given). - #[arg(long, num_args = 0..=1, default_missing_value = "1")] + #[arg( + long, + conflicts_with = "last", + num_args = 0..=1, + default_missing_value = "1" + )] first: Option, /// Return only the last N matches (default 1 if no number given). - #[arg(long, num_args = 0..=1, default_missing_value = "1")] + #[arg( + long, + conflicts_with = "first", + num_args = 0..=1, + default_missing_value = "1" + )] last: Option, /// Print count of matches instead of the matches themselves. #[arg(long)] @@ -957,10 +987,20 @@ pub enum Command { #[arg(long, default_value = "tag")] format: String, /// Return only the first N matches (default 1 if no number given). - #[arg(long, num_args = 0..=1, default_missing_value = "1")] + #[arg( + long, + conflicts_with = "last", + num_args = 0..=1, + default_missing_value = "1" + )] first: Option, /// Return only the last N matches (default 1 if no number given). - #[arg(long, num_args = 0..=1, default_missing_value = "1")] + #[arg( + long, + conflicts_with = "first", + num_args = 0..=1, + default_missing_value = "1" + )] last: Option, /// Print count of matches instead of the matches themselves. #[arg(long)] @@ -2850,6 +2890,33 @@ mod tests { Cli::command().debug_assert(); } + #[test] + fn test_cli_rejects_first_and_last_together() { + std::thread::Builder::new() + .stack_size(8 * 1024 * 1024) + .spawn(|| { + assert!( + Cli::try_parse_from(["id", "show", "--first", "2", "--last", "3", "query"]) + .is_err() + ); + assert!( + Cli::try_parse_from(["id", "peek", "--first", "2", "--last", "3", "query"]) + .is_err() + ); + assert!( + Cli::try_parse_from(["id", "find", "--first", "2", "--last", "3", "query"]) + .is_err() + ); + assert!( + Cli::try_parse_from(["id", "search", "--first", "2", "--last", "3", "query"]) + .is_err() + ); + }) + .unwrap() + .join() + .unwrap(); + } + // Tests for Show command #[test] fn test_cli_parse_show() { diff --git a/pkgs/id/src/repl/runner.rs b/pkgs/id/src/repl/runner.rs index c63fd80500..87c4ebb0ef 100644 --- a/pkgs/id/src/repl/runner.rs +++ b/pkgs/id/src/repl/runner.rs @@ -598,6 +598,14 @@ struct FindArgs<'a> { } impl FindArgs<'_> { + /// Reject mutually exclusive result limits before any search work starts. + fn validate_result_limits(&self) -> Result<()> { + if self.first.is_some() && self.last.is_some() { + anyhow::bail!("--first and --last cannot be used together"); + } + Ok(()) + } + /// Convert to `SearchOptions` for filtering. fn to_search_options(&self) -> SearchOptions { SearchOptions::new( @@ -624,7 +632,7 @@ impl FindArgs<'_> { /// - `--last [N]`: Return only last N matches (default 1) /// - `--count`: Print count instead of matches /// - `--exclude PATTERN`: Exclude matches containing pattern (repeatable) -fn parse_find_args<'a>(rest: &[&'a str], default_format: &'a str) -> FindArgs<'a> { +fn parse_find_args<'a>(rest: &[&'a str], default_format: &'a str) -> Result> { let mut args = FindArgs { queries: Vec::new(), prefer_name: false, @@ -708,7 +716,8 @@ fn parse_find_args<'a>(rest: &[&'a str], default_format: &'a str) -> FindArgs<'a } i += 1; } - args + args.validate_result_limits()?; + Ok(args) } /// Handle the `find` command in the REPL. @@ -730,7 +739,7 @@ async fn handle_find_command( rl: &mut DefaultEditor, rest: &[&str], ) -> Result<()> { - let args = parse_find_args(rest, "union"); + let args = parse_find_args(rest, "union")?; if args.queries.is_empty() { println!( @@ -791,7 +800,7 @@ async fn handle_find_command( /// Lists blobs matching the queries without outputting content by default. /// With `--all` or `--file`, also retrieves the matching files. async fn handle_search_command(ctx: &mut ReplContext, rest: &[&str]) -> Result<()> { - let args = parse_find_args(rest, "union"); + let args = parse_find_args(rest, "union")?; if args.queries.is_empty() { println!( @@ -843,7 +852,7 @@ async fn handle_search_command(ctx: &mut ReplContext, rest: &[&str]) -> Result<( /// Searches for blobs matching the queries and outputs their content /// to stdout (or file with -o). async fn handle_show_command(ctx: &mut ReplContext, rest: &[&str]) -> Result<()> { - let args = parse_find_args(rest, "union"); + let args = parse_find_args(rest, "union")?; if args.queries.is_empty() { println!( @@ -891,7 +900,7 @@ async fn handle_show_command(ctx: &mut ReplContext, rest: &[&str]) -> Result<()> /// /// Searches for blobs and shows a preview with configurable head/tail lines. async fn handle_peek_command(ctx: &mut ReplContext, rest: &[&str]) -> Result<()> { - let (args, peek_opts) = parse_peek_args(rest); + let (args, peek_opts) = parse_peek_args(rest)?; if args.queries.is_empty() { println!( @@ -1182,7 +1191,7 @@ async fn select_and_output_matches_filtered( } /// Parse peek command arguments. -fn parse_peek_args<'a>(rest: &[&'a str]) -> (FindArgs<'a>, PeekOptions) { +fn parse_peek_args<'a>(rest: &[&'a str]) -> Result<(FindArgs<'a>, PeekOptions)> { let mut find_args = FindArgs { queries: Vec::new(), prefer_name: false, @@ -1260,7 +1269,8 @@ fn parse_peek_args<'a>(rest: &[&'a str]) -> (FindArgs<'a>, PeekOptions) { i += 1; } - (find_args, peek_opts) + find_args.validate_result_limits()?; + Ok((find_args, peek_opts)) } /// Fetch content for peek preview. @@ -1515,7 +1525,7 @@ mod tests { #[test] fn test_parse_find_args_basic() { let rest = vec!["query1", "query2"]; - let args = parse_find_args(&rest, "union"); + let args = parse_find_args(&rest, "union").unwrap(); assert_eq!(args.queries, vec!["query1", "query2"]); assert!(!args.prefer_name); assert!(!args.all); @@ -1525,7 +1535,7 @@ mod tests { #[test] fn test_parse_find_args_with_flags() { let rest = vec!["query", "--name", "--all", "--format", "tag"]; - let args = parse_find_args(&rest, "union"); + let args = parse_find_args(&rest, "union").unwrap(); assert_eq!(args.queries, vec!["query"]); assert!(args.prefer_name); assert!(args.all); @@ -1535,7 +1545,7 @@ mod tests { #[test] fn test_parse_find_args_with_output_file() { let rest = vec!["query", ">output.txt"]; - let args = parse_find_args(&rest, "union"); + let args = parse_find_args(&rest, "union").unwrap(); assert_eq!(args.queries, vec!["query"]); assert!(args.to_file); assert_eq!(args.output_file, Some("output.txt")); @@ -1544,7 +1554,7 @@ mod tests { #[test] fn test_parse_find_args_with_dir() { let rest = vec!["query", "--dir", "/tmp/out"]; - let args = parse_find_args(&rest, "union"); + let args = parse_find_args(&rest, "union").unwrap(); assert_eq!(args.queries, vec!["query"]); assert_eq!(args.dir, Some("/tmp/out")); } @@ -1552,22 +1562,22 @@ mod tests { #[test] fn test_parse_find_args_shorthand_formats() { let rest = vec!["query", "--tag"]; - let args = parse_find_args(&rest, "union"); + let args = parse_find_args(&rest, "union").unwrap(); assert_eq!(args.format, "tag"); let rest = vec!["query", "--group"]; - let args = parse_find_args(&rest, "union"); + let args = parse_find_args(&rest, "union").unwrap(); assert_eq!(args.format, "group"); let rest = vec!["query", "--union"]; - let args = parse_find_args(&rest, "tag"); + let args = parse_find_args(&rest, "tag").unwrap(); assert_eq!(args.format, "union"); } #[test] fn test_parse_find_args_first_with_number() { let rest = vec!["query", "--first", "5"]; - let args = parse_find_args(&rest, "union"); + let args = parse_find_args(&rest, "union").unwrap(); assert_eq!(args.first, Some(5)); assert_eq!(args.last, None); } @@ -1575,42 +1585,55 @@ mod tests { #[test] fn test_parse_find_args_first_without_number() { let rest = vec!["query", "--first"]; - let args = parse_find_args(&rest, "union"); + let args = parse_find_args(&rest, "union").unwrap(); assert_eq!(args.first, Some(1)); // defaults to 1 } #[test] fn test_parse_find_args_last_with_number() { let rest = vec!["query", "--last", "3"]; - let args = parse_find_args(&rest, "union"); + let args = parse_find_args(&rest, "union").unwrap(); assert_eq!(args.last, Some(3)); assert_eq!(args.first, None); } + #[test] + fn test_parse_find_args_rejects_first_and_last_together() { + let err = parse_find_args(&["query", "--first", "2", "--last", "3"], "union") + .err() + .expect("both limits should be rejected"); + assert_eq!( + err.to_string(), + "--first and --last cannot be used together" + ); + + assert!(parse_find_args(&["query", "--first", "2"], "union").is_ok()); + assert!(parse_find_args(&["query", "--last", "3"], "union").is_ok()); + } + #[test] fn test_parse_find_args_count() { let rest = vec!["query", "--count"]; - let args = parse_find_args(&rest, "union"); + let args = parse_find_args(&rest, "union").unwrap(); assert!(args.count); } #[test] fn test_parse_find_args_exclude() { let rest = vec!["query", "--exclude", ".bak", "--exclude", ".tmp"]; - let args = parse_find_args(&rest, "union"); + let args = parse_find_args(&rest, "union").unwrap(); assert_eq!(args.exclude, vec![".bak", ".tmp"]); } #[test] fn test_parse_find_args_combined_filters() { let rest = vec!["query", "--first", "10", "--exclude", ".bak", "--name"]; - let args = parse_find_args(&rest, "union"); + let args = parse_find_args(&rest, "union").unwrap(); assert_eq!(args.first, Some(10)); assert_eq!(args.exclude, vec![".bak"]); assert!(args.prefer_name); } - // Regression (#1787): lines = usize::MAX/2 + 1 must not overflow the // printers' doubling — saturating arithmetic prints short content once, // with no omitted-count line and no panic. @@ -1673,7 +1696,7 @@ mod tests { #[test] fn test_parse_peek_args_basic() { let rest = vec!["readme"]; - let (find_args, peek_opts) = parse_peek_args(&rest); + let (find_args, peek_opts) = parse_peek_args(&rest).unwrap(); assert_eq!(find_args.queries, vec!["readme"]); assert_eq!(peek_opts.lines, 5); // default assert!(!peek_opts.head_only); @@ -1683,14 +1706,14 @@ mod tests { #[test] fn test_parse_peek_args_with_lines() { let rest = vec!["readme", "--lines", "10"]; - let (_, peek_opts) = parse_peek_args(&rest); + let (_, peek_opts) = parse_peek_args(&rest).unwrap(); assert_eq!(peek_opts.lines, 10); } #[test] fn test_parse_peek_args_head_only() { let rest = vec!["readme", "--head-only"]; - let (_, peek_opts) = parse_peek_args(&rest); + let (_, peek_opts) = parse_peek_args(&rest).unwrap(); assert!(peek_opts.head_only); assert!(!peek_opts.tail_only); } @@ -1698,7 +1721,7 @@ mod tests { #[test] fn test_parse_peek_args_tail_only() { let rest = vec!["readme", "--tail-only"]; - let (_, peek_opts) = parse_peek_args(&rest); + let (_, peek_opts) = parse_peek_args(&rest).unwrap(); assert!(!peek_opts.head_only); assert!(peek_opts.tail_only); } @@ -1706,7 +1729,7 @@ mod tests { #[test] fn test_parse_peek_args_chars() { let rest = vec!["readme", "--chars"]; - let (_, peek_opts) = parse_peek_args(&rest); + let (_, peek_opts) = parse_peek_args(&rest).unwrap(); assert!(peek_opts.chars); assert!(!peek_opts.words); } @@ -1714,7 +1737,7 @@ mod tests { #[test] fn test_parse_peek_args_words() { let rest = vec!["readme", "--words"]; - let (_, peek_opts) = parse_peek_args(&rest); + let (_, peek_opts) = parse_peek_args(&rest).unwrap(); assert!(!peek_opts.chars); assert!(peek_opts.words); } @@ -1722,21 +1745,35 @@ mod tests { #[test] fn test_parse_peek_args_quiet() { let rest = vec!["readme", "-q"]; - let (_, peek_opts) = parse_peek_args(&rest); + let (_, peek_opts) = parse_peek_args(&rest).unwrap(); assert!(peek_opts.quiet); } #[test] fn test_parse_peek_args_output_file() { let rest = vec!["readme", "-o", "out.txt"]; - let (find_args, _) = parse_peek_args(&rest); + let (find_args, _) = parse_peek_args(&rest).unwrap(); assert_eq!(find_args.output_file, Some("out.txt")); } + #[test] + fn test_parse_peek_args_rejects_first_and_last_together() { + let err = parse_peek_args(&["readme", "--first", "2", "--last", "3"]) + .err() + .expect("both limits should be rejected"); + assert_eq!( + err.to_string(), + "--first and --last cannot be used together" + ); + + assert!(parse_peek_args(&["readme", "--first", "2"]).is_ok()); + assert!(parse_peek_args(&["readme", "--last", "3"]).is_ok()); + } + #[test] fn test_find_args_to_search_options() { let rest = vec!["query", "--first", "5", "--exclude", ".bak"]; - let args = parse_find_args(&rest, "union"); + let args = parse_find_args(&rest, "union").unwrap(); let opts = args.to_search_options(); assert_eq!(opts.first, Some(5)); assert_eq!(opts.exclude, vec![".bak".to_owned()]);