brain: Default diff target to HEAD

f98127d9168a8272ec5c2734a804419666aae4db
Allow a review range to omit its upper revision when comparing a base
revision with the current branch head.

Assisted-by: Codex:gpt-5
Alexis Sellier committed ago 1 parent d8233e9a
crates/brain/src/main.rs +26 -5
1 1
//! Binary entry point for the `brain` code-review TUI.
2 2
//!
3 -
//! `brain diff <from> <to>` opens an interactive review of the changes between
4 -
//! two revisions. The reviewer accepts hunks one by one; accepted hunks are
5 -
//! hidden by default and can be revealed for inspection.
3 +
//! `brain diff <from> [<to>]` opens an interactive review of the changes between
4 +
//! two revisions. The upper revision defaults to `HEAD`. The reviewer accepts
5 +
//! hunks one by one; accepted hunks are hidden by default and can be revealed
6 +
//! for inspection.
6 7
7 8
// `serde` is the local alias for the `microserde` crate; the derive macro
8 9
// emits `microserde::*` paths so the original crate name must remain in scope.
9 10
extern crate serde as microserde;
10 11
37 38
38 39
/// Terminal backend used by the interactive viewer.
39 40
type BrainTerminal = Terminal<tui_termion::TermionBackend<tui_termion::term::TerminalScreen>>;
40 41
41 42
/// Command-line usage text.
42 -
const USAGE: &str = "usage: brain [status]\n       brain [--reset|--rebase] diff <from> <to>";
43 +
const USAGE: &str = "usage: brain [status]\n       brain [--reset|--rebase] diff <from> [<to>]";
43 44
/// Default output width used when terminal width is unknown.
44 45
const DEFAULT_STATUS_WIDTH: usize = 140;
45 46
/// Marker shown for approved review state: โœ”.
46 47
const APPROVED_STATUS_ICON: char = 'โœ”';
47 48
/// Marker shown for in-review state: โ—.
80 81
                Value(command_name) if command_name == "status" => {
81 82
                    command = Some(Command::Status);
82 83
                }
83 84
                Value(command_name) if command_name == "diff" => {
84 85
                    let from = parse_revision("from", parser.value()?)?;
85 -
                    let to = parse_revision("to", parser.value()?)?;
86 +
                    let to = match parser.next()? {
87 +
                        Some(Value(to)) => parse_revision("to", to)?,
88 +
                        Some(_) => return Err(Error::Usage(USAGE.to_owned())),
89 +
                        None => parse_revision("to", OsString::from("HEAD"))?,
90 +
                    };
86 91
87 92
                    command = Some(Command::Diff { from, to });
88 93
                }
89 94
                Value(value) => {
90 95
                    let value = value.to_string_lossy();
471 476
                },
472 477
            }
473 478
        );
474 479
    }
475 480
481 +
    #[test]
482 +
    fn defaults_diff_to_head() {
483 +
        let parsed = Args::parse(args(&["diff", "main"])).unwrap();
484 +
485 +
        assert_eq!(
486 +
            parsed,
487 +
            Args {
488 +
                state: ReviewStateLoad::Existing,
489 +
                command: Command::Diff {
490 +
                    from: Rev::try_from("main").unwrap(),
491 +
                    to: Rev::try_from("HEAD").unwrap(),
492 +
                },
493 +
            }
494 +
        );
495 +
    }
496 +
476 497
    #[test]
477 498
    fn parses_reset_flag_before_diff_command() {
478 499
        let parsed = Args::parse(args(&["--reset", "diff", "main", "HEAD"])).unwrap();
479 500
480 501
        assert_eq!(parsed.state, ReviewStateLoad::Fresh);