brain: Include unreviewed branches in all status
0fbd41afd8d72893016193e302d3266f15f5384d
1 parent
50b122f1
crates/brain/src/backend.rs
+56 -3
| 15 | 15 | use rustik_diff::{ |
|
| 16 | 16 | CommitHeader, Decode as _, DiffDocument, DiffLineKind, Encode as _, FileDiff, FileKind, Hunk, |
|
| 17 | 17 | HunkHeader, HunkId, ReviewProgress, |
|
| 18 | 18 | }; |
|
| 19 | 19 | use rustik_repo::{ |
|
| 20 | - | AheadBehind, CommitTime, DiffStat, MergeStatus, Oid, PatchId, ReadRepository, Rev, |
|
| 20 | + | AheadBehind, Branch, CommitTime, DiffStat, MergeStatus, Oid, PatchId, ReadRepository, Rev, |
|
| 21 | 21 | WriteRepository, |
|
| 22 | 22 | git::{ |
|
| 23 | 23 | self, |
|
| 24 | 24 | refs::{Component, Qualified, RefString}, |
|
| 25 | 25 | }, |
| 638 | 638 | Ok(Rev::from(base)) |
|
| 639 | 639 | } |
|
| 640 | 640 | ||
| 641 | 641 | /// Returns stored review statuses for the repository containing `cwd`. |
|
| 642 | 642 | pub fn statuses(cwd: &Path) -> Result<Vec<BrainStatus>, Error> { |
|
| 643 | - | let repository = open_repository(cwd)?; |
|
| 643 | + | if is_jj_workspace(cwd) { |
|
| 644 | + | let jj = jj::Repository::new(cwd); |
|
| 645 | + | let repository = jj.git_repository()?; |
|
| 646 | + | let branches = jj |
|
| 647 | + | .bookmarks()? |
|
| 648 | + | .into_iter() |
|
| 649 | + | .map(|(head, name)| { |
|
| 650 | + | let time = repository.commit_time(head)?; |
|
| 651 | + | Ok(Branch { name, head, time }) |
|
| 652 | + | }) |
|
| 653 | + | .collect::<Result<Vec<_>, Error>>()?; |
|
| 654 | + | return Brain::statuses_for_branches(&repository, branches); |
|
| 655 | + | } |
|
| 656 | + | let repository = git::Repository::discover(cwd.to_path_buf())?; |
|
| 644 | 657 | Brain::statuses(&repository) |
|
| 645 | 658 | } |
|
| 646 | 659 | ||
| 647 | 660 | /// Opens the Git repository backing the workspace containing `cwd`. |
|
| 648 | 661 | fn open_repository(cwd: &Path) -> Result<git::Repository, Error> { |
| 722 | 735 | let target = component(crate::comments::encode_component(identity.target.as_str()))?; |
|
| 723 | 736 | ||
| 724 | 737 | Ok(prefix.join(range).join(base).join(target)) |
|
| 725 | 738 | } |
|
| 726 | 739 | ||
| 727 | - | /// Returns stored review statuses for all brain state refs. |
|
| 740 | + | /// Returns stored review statuses for the repository's local branches. |
|
| 728 | 741 | fn statuses(repository: &git::Repository) -> Result<Vec<BrainStatus>, Error> { |
|
| 729 | 742 | let branches = repository.branches()?; |
|
| 743 | + | Self::statuses_for_branches(repository, branches) |
|
| 744 | + | } |
|
| 745 | + | ||
| 746 | + | /// Returns stored review statuses joined to `branches`. |
|
| 747 | + | fn statuses_for_branches( |
|
| 748 | + | repository: &git::Repository, |
|
| 749 | + | branches: Vec<Branch>, |
|
| 750 | + | ) -> Result<Vec<BrainStatus>, Error> { |
|
| 730 | 751 | if branches.is_empty() { |
|
| 731 | 752 | return Ok(Vec::new()); |
|
| 732 | 753 | } |
|
| 733 | 754 | ||
| 734 | 755 | let trunk = repository |
| 1567 | 1588 | fn new() -> Self { |
|
| 1568 | 1589 | let nonce = SystemTime::now() |
|
| 1569 | 1590 | .duration_since(UNIX_EPOCH) |
|
| 1570 | 1591 | .unwrap() |
|
| 1571 | 1592 | .as_nanos(); |
|
| 1593 | + | ||
| 1572 | 1594 | let path = std::env::temp_dir().join(format!( |
|
| 1573 | 1595 | "rustik-brain-rebase-{}-{nonce}", |
|
| 1574 | 1596 | std::process::id() |
|
| 1575 | 1597 | )); |
|
| 1576 | 1598 | fs::create_dir(&path).unwrap(); |
| 1588 | 1610 | .current_dir(&self.path) |
|
| 1589 | 1611 | .status() |
|
| 1590 | 1612 | .unwrap(); |
|
| 1591 | 1613 | assert!(status.success()); |
|
| 1592 | 1614 | } |
|
| 1615 | + | /// Runs a Jujutsu command and requires success. |
|
| 1616 | + | fn jj(&self, args: &[&str]) { |
|
| 1617 | + | let status = Command::new("jj") |
|
| 1618 | + | .args(args) |
|
| 1619 | + | .current_dir(&self.path) |
|
| 1620 | + | .status() |
|
| 1621 | + | .unwrap(); |
|
| 1622 | + | assert!(status.success()); |
|
| 1623 | + | } |
|
| 1593 | 1624 | ||
| 1594 | 1625 | /// Returns the fixture working tree. |
|
| 1595 | 1626 | fn path(&self) -> &std::path::Path { |
|
| 1596 | 1627 | &self.path |
|
| 1597 | 1628 | } |
| 1716 | 1747 | let statuses = statuses(fixture.path()).unwrap(); |
|
| 1717 | 1748 | ||
| 1718 | 1749 | assert!(statuses.is_empty()); |
|
| 1719 | 1750 | } |
|
| 1720 | 1751 | ||
| 1752 | + | #[test] |
|
| 1753 | + | fn statuses_include_native_jujutsu_bookmarks() { |
|
| 1754 | + | let fixture = TestRepository::new(); |
|
| 1755 | + | fs::remove_dir_all(fixture.path().join(".git")).unwrap(); |
|
| 1756 | + | fixture.jj(&["git", "init", "--no-colocate", "."]); |
|
| 1757 | + | fixture.jj(&["describe", "-m", "base"]); |
|
| 1758 | + | fixture.jj(&["bookmark", "create", "main", "-r", "@"]); |
|
| 1759 | + | fixture.jj(&["new", "main"]); |
|
| 1760 | + | fixture.jj(&["describe", "-m", "target"]); |
|
| 1761 | + | fixture.jj(&["bookmark", "create", "topic", "-r", "@"]); |
|
| 1762 | + | ||
| 1763 | + | let statuses = statuses(fixture.path()).unwrap(); |
|
| 1764 | + | ||
| 1765 | + | assert_eq!( |
|
| 1766 | + | statuses |
|
| 1767 | + | .into_iter() |
|
| 1768 | + | .map(|status| status.head) |
|
| 1769 | + | .collect::<BTreeSet<_>>(), |
|
| 1770 | + | BTreeSet::from(["main".to_owned(), "topic".to_owned()]) |
|
| 1771 | + | ); |
|
| 1772 | + | } |
|
| 1773 | + | ||
| 1721 | 1774 | #[test] |
|
| 1722 | 1775 | fn statuses_require_trunk_in_nonempty_repository() { |
|
| 1723 | 1776 | let fixture = TestRepository::new(); |
|
| 1724 | 1777 | fixture.git(&["branch", "-m", "topic"]); |
|
| 1725 | 1778 | fixture.git(&["commit", "-q", "--allow-empty", "-m", "topic"]); |
f
+0 -0
new.rs
+0 -0
image.png
+0 -0
new.txt
+0 -0
crates/brain/src/main.rs
+76 -16
| 37 | 37 | ||
| 38 | 38 | /// Terminal backend used by the interactive viewer. |
|
| 39 | 39 | type BrainTerminal = Terminal<tui_termion::TermionBackend<tui_termion::term::TerminalScreen>>; |
|
| 40 | 40 | ||
| 41 | 41 | /// Command-line usage text. |
|
| 42 | - | const USAGE: &str = "usage: brain [status]\n brain [--reset|--rebase] diff [<base>] <target>"; |
|
| 42 | + | const USAGE: &str = |
|
| 43 | + | "usage: brain [status [--all]]\n brain [--reset|--rebase] diff [<base>] <target>"; |
|
| 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: โ. |
| 67 | 68 | lexopt::Parser::from_iter([OsString::from("brain")].into_iter().chain(args)); |
|
| 68 | 69 | let mut state = ReviewStateLoad::Existing; |
|
| 69 | 70 | let mut command = None; |
|
| 70 | 71 | ||
| 71 | 72 | while let Some(arg) = parser.next()? { |
|
| 73 | + | if let Long("all") = arg |
|
| 74 | + | && let Some(Command::Status { scope }) = &mut command |
|
| 75 | + | { |
|
| 76 | + | *scope = StatusScope::All; |
|
| 77 | + | continue; |
|
| 78 | + | } |
|
| 72 | 79 | if command.is_some() { |
|
| 73 | 80 | return Err(Error::Usage(format!( |
|
| 74 | 81 | "unexpected argument: {arg:?}\n{USAGE}" |
|
| 75 | 82 | ))); |
|
| 76 | 83 | } |
|
| 77 | 84 | match arg { |
|
| 78 | 85 | Long("reset") => state = ReviewStateLoad::Fresh, |
|
| 79 | 86 | Long("rebase") => state = ReviewStateLoad::Rebase, |
|
| 80 | 87 | Value(command_name) if command_name == "status" => { |
|
| 81 | - | command = Some(Command::Status); |
|
| 88 | + | command = Some(Command::Status { |
|
| 89 | + | scope: StatusScope::Reviews, |
|
| 90 | + | }); |
|
| 82 | 91 | } |
|
| 83 | 92 | Value(command_name) if command_name == "diff" => { |
|
| 84 | 93 | let first = parse_revision("target", parser.value()?)?; |
|
| 85 | 94 | let (base, target) = match parser.next()? { |
|
| 86 | 95 | Some(Value(target)) => { |
| 104 | 113 | return Err(Error::Usage(USAGE.to_owned())); |
|
| 105 | 114 | } |
|
| 106 | 115 | ||
| 107 | 116 | Ok(Self { |
|
| 108 | 117 | state, |
|
| 109 | - | command: command.unwrap_or(Command::Status), |
|
| 118 | + | command: command.unwrap_or(Command::Status { |
|
| 119 | + | scope: StatusScope::Reviews, |
|
| 120 | + | }), |
|
| 110 | 121 | }) |
|
| 111 | 122 | } |
|
| 112 | 123 | } |
|
| 113 | 124 | ||
| 114 | 125 | /// Review command requested by the user. |
|
| 115 | 126 | #[derive(Debug, Eq, PartialEq)] |
|
| 116 | 127 | enum Command { |
|
| 117 | - | /// Show stored review state. |
|
| 118 | - | Status, |
|
| 128 | + | /// Show review status for the selected branch scope. |
|
| 129 | + | Status { |
|
| 130 | + | /// Branch scope included in status output. |
|
| 131 | + | scope: StatusScope, |
|
| 132 | + | }, |
|
| 119 | 133 | /// Review the commits in `base..target`. |
|
| 120 | 134 | Diff { |
|
| 121 | 135 | /// Exclusive lower bound of the reviewed range, when supplied. |
|
| 122 | 136 | base: Option<Rev>, |
|
| 123 | 137 | /// Inclusive upper bound of the reviewed range. |
|
| 124 | 138 | target: Rev, |
|
| 125 | 139 | }, |
|
| 126 | 140 | } |
|
| 127 | 141 | ||
| 142 | + | /// Branch scope included in status output. |
|
| 143 | + | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
|
| 144 | + | enum StatusScope { |
|
| 145 | + | /// Branches with stored review state. |
|
| 146 | + | Reviews, |
|
| 147 | + | /// Every branch, including branches without stored review state. |
|
| 148 | + | All, |
|
| 149 | + | } |
|
| 150 | + | ||
| 128 | 151 | /// How the persisted review state should be loaded. |
|
| 129 | 152 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
|
| 130 | 153 | enum ReviewStateLoad { |
|
| 131 | 154 | /// Load existing state, prompting if it is stale. |
|
| 132 | 155 | Existing, |
| 193 | 216 | ||
| 194 | 217 | /// Parses arguments and dispatches the requested command. |
|
| 195 | 218 | fn run(args: Vec<OsString>) -> Result<(), Error> { |
|
| 196 | 219 | let args = Args::parse(args)?; |
|
| 197 | 220 | match args.command { |
|
| 198 | - | Command::Status => run_status(), |
|
| 221 | + | Command::Status { scope } => run_status(scope), |
|
| 199 | 222 | Command::Diff { base, target } => run_diff(args.state, base, target), |
|
| 200 | 223 | } |
|
| 201 | 224 | } |
|
| 202 | 225 | ||
| 203 | 226 | /// Loads the review range and runs the interactive diff session. |
| 280 | 303 | result?; |
|
| 281 | 304 | cursor.map_err(Error::Io) |
|
| 282 | 305 | } |
|
| 283 | 306 | ||
| 284 | 307 | /// Prints the stored review status table. |
|
| 285 | - | fn run_status() -> Result<(), Error> { |
|
| 308 | + | fn run_status(scope: StatusScope) -> Result<(), Error> { |
|
| 286 | 309 | let cwd = env::current_dir()?; |
|
| 287 | 310 | let mut statuses = backend::statuses(&cwd)?; |
|
| 288 | - | statuses.retain(|status| status.state != BrainStatusState::NoState); |
|
| 311 | + | if scope == StatusScope::Reviews { |
|
| 312 | + | statuses.retain(|status| status.state != BrainStatusState::NoState); |
|
| 313 | + | } |
|
| 289 | 314 | let mut stdout = io::stdout().lock(); |
|
| 290 | 315 | ||
| 291 | - | for line in status_lines(&statuses, status_width()) { |
|
| 316 | + | for line in status_lines(&statuses, scope, status_width()) { |
|
| 292 | 317 | writeln!(stdout, "{}", line.styled())?; |
|
| 293 | 318 | } |
|
| 294 | 319 | ||
| 295 | 320 | Ok(()) |
|
| 296 | 321 | } |
|
| 297 | 322 | ||
| 298 | 323 | /// Returns styled status output lines. |
|
| 299 | - | fn status_lines(statuses: &[BrainStatus], width: usize) -> Vec<Line> { |
|
| 324 | + | fn status_lines(statuses: &[BrainStatus], scope: StatusScope, width: usize) -> Vec<Line> { |
|
| 300 | 325 | if statuses.is_empty() { |
|
| 301 | - | return vec![Line::new(Label::new("No reviews.").fg(colors::fixed::DIM))]; |
|
| 326 | + | let empty = match scope { |
|
| 327 | + | StatusScope::Reviews => "No reviews.", |
|
| 328 | + | StatusScope::All => "No branches.", |
|
| 329 | + | }; |
|
| 330 | + | return vec![Line::new(Label::new(empty).fg(colors::fixed::DIM))]; |
|
| 302 | 331 | } |
|
| 303 | 332 | ||
| 304 | 333 | let palette = DiffPalette::default(); |
|
| 305 | 334 | let mut table = Table::<6, Line>::new(TableOptions { |
|
| 306 | 335 | border: Some(Border::new(colors::fixed::DIM)), |
| 321 | 350 | brain_state(status.state), |
|
| 322 | 351 | status_head(status), |
|
| 323 | 352 | review_status(status), |
|
| 324 | 353 | ahead_behind(status.relation), |
|
| 325 | 354 | comment_status(status.comments), |
|
| 326 | - | progress_bar(status.progress, palette, colors::fixed::DIM), |
|
| 355 | + | if status.state == BrainStatusState::NoState { |
|
| 356 | + | Line::blank() |
|
| 357 | + | } else { |
|
| 358 | + | progress_bar(status.progress, palette, colors::fixed::DIM) |
|
| 359 | + | }, |
|
| 327 | 360 | ]); |
|
| 328 | 361 | } |
|
| 329 | 362 | table.lines(Constraint::max(Size::new(width, usize::MAX))) |
|
| 330 | 363 | } |
|
| 331 | 364 |
| 514 | 547 | } |
|
| 515 | 548 | ||
| 516 | 549 | #[test] |
|
| 517 | 550 | fn parses_status_command() { |
|
| 518 | 551 | let parsed = Args::parse(args(&["status"])).unwrap(); |
|
| 519 | - | ||
| 520 | - | assert_eq!(parsed.command, Command::Status); |
|
| 552 | + | assert_eq!( |
|
| 553 | + | parsed.command, |
|
| 554 | + | Command::Status { |
|
| 555 | + | scope: StatusScope::Reviews |
|
| 556 | + | } |
|
| 557 | + | ); |
|
| 521 | 558 | } |
|
| 522 | 559 | ||
| 523 | 560 | #[test] |
|
| 524 | 561 | fn defaults_to_status_command() { |
|
| 525 | 562 | let parsed = Args::parse(args(&[])).unwrap(); |
|
| 563 | + | assert_eq!( |
|
| 564 | + | parsed.command, |
|
| 565 | + | Command::Status { |
|
| 566 | + | scope: StatusScope::Reviews |
|
| 567 | + | } |
|
| 568 | + | ); |
|
| 569 | + | } |
|
| 570 | + | ||
| 571 | + | #[test] |
|
| 572 | + | fn parses_all_status_command() { |
|
| 573 | + | let parsed = Args::parse(args(&["status", "--all"])).unwrap(); |
|
| 526 | 574 | ||
| 527 | - | assert_eq!(parsed.command, Command::Status); |
|
| 575 | + | assert_eq!( |
|
| 576 | + | parsed.command, |
|
| 577 | + | Command::Status { |
|
| 578 | + | scope: StatusScope::All |
|
| 579 | + | } |
|
| 580 | + | ); |
|
| 528 | 581 | } |
|
| 529 | 582 | ||
| 530 | 583 | #[test] |
|
| 531 | 584 | fn rejects_reset_without_diff_command() { |
|
| 532 | 585 | let error = Args::parse(args(&["--reset"])).unwrap_err(); |
| 541 | 594 | assert!(error.to_string().contains("usage: brain")); |
|
| 542 | 595 | } |
|
| 543 | 596 | ||
| 544 | 597 | #[test] |
|
| 545 | 598 | fn empty_status_reports_no_reviews() { |
|
| 546 | - | assert_eq!(status_lines(&[], 80)[0].to_string(), "No reviews."); |
|
| 599 | + | assert_eq!( |
|
| 600 | + | status_lines(&[], StatusScope::Reviews, 80)[0].to_string(), |
|
| 601 | + | "No reviews." |
|
| 602 | + | ); |
|
| 603 | + | assert_eq!( |
|
| 604 | + | status_lines(&[], StatusScope::All, 80)[0].to_string(), |
|
| 605 | + | "No branches." |
|
| 606 | + | ); |
|
| 547 | 607 | } |
|
| 548 | 608 | } |