Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion crates/mergify-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,20 @@ fn looks_native(argv: &[String]) -> bool {
})
}

/// Did clap exit on `--help` / `-h` / `--version`? Those return a
/// special `Err` whose `kind()` is `DisplayHelp` /
/// `DisplayHelpOnMissingArgumentOrSubcommand` / `DisplayVersion`;
/// callers should always honor them and exit (printing the help /
/// version) instead of falling through to the Python shim or
/// surfacing them as argument errors.
fn is_help_or_version(err: &clap::Error) -> bool {
matches!(
err.kind(),
clap::error::ErrorKind::DisplayHelp
| clap::error::ErrorKind::DisplayHelpOnMissingArgumentOrSubcommand
)
}

/// Try to recognize the invocation as a native command.
///
/// Returns ``None`` when the argv doesn't look like a native
Expand All @@ -163,13 +177,23 @@ fn looks_native(argv: &[String]) -> bool {
/// — the corresponding exit code is the one chosen by the command
/// implementation (typically [`mergify_core::ExitCode::Configuration`]
/// = 8), not 2.
#[allow(clippy::too_many_lines)] // mostly mechanical match arms
fn detect_native(argv: &[String]) -> Option<NativeCommand> {
let looks_native = looks_native(argv);

let parsed = match CliRoot::try_parse_from(
std::iter::once("mergify".to_string()).chain(argv.iter().cloned()),
) {
Ok(parsed) => parsed,
Err(err) if is_help_or_version(&err) => {
// ``--help`` (or implicit help on a subcommand group)
// is always handled natively by clap — even when
// ``looks_native`` is false. Otherwise we'd fall
// through to the Python shim's help, which no longer
// lists Rust-native subcommands. ``err.exit()`` prints
// to stdout and calls ``process::exit(0)``.
err.exit()
}
Err(err) if looks_native => {
// Native intent + clap rejection = surface clap's error
// and exit. ``err.exit()`` prints to stderr and calls
Expand Down Expand Up @@ -370,7 +394,7 @@ fn run_native(cmd: NativeCommand) -> ExitCode {

#[derive(Parser)]
#[command(name = "mergify", disable_help_subcommand = true)]
#[command(disable_version_flag = true, disable_help_flag = true)]
#[command(disable_version_flag = true)]
struct CliRoot {
#[command(subcommand)]
command: Subcommands,
Expand Down
2 changes: 1 addition & 1 deletion crates/mergify-queue/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ fn print_waiting_prs(
if let Some(queued_at) = &pr.queued_at {
let rel = relative_time(queued_at, now, false);
if !rel.is_empty() {
write!(w, " {D}queued {rel}{R}", D = theme.dim, R = theme.reset,)?;
write!(w, " {D}queued {rel}{R}", D = theme.dim, R = theme.reset)?;
}
}
if let Some(eta) = &pr.estimated_merge_at {
Expand Down
2 changes: 1 addition & 1 deletion crates/mergify-tui/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ mod tests {

#[test]
fn fg_respects_enabled_flag() {
assert_eq!(format!("{}", Theme::new(false).fg(AnsiColor::Red)), "",);
assert_eq!(format!("{}", Theme::new(false).fg(AnsiColor::Red)), "");
assert!(!format!("{}", Theme::new(true).fg(AnsiColor::Red)).is_empty());
}
}
Loading