utopia-ia/elffuss-braid public
Changesets · Branches · Tags · Compare
| Changeset | 2a5b407d07df187a02d530035110f983130db2d69a3c98bf28b73364f9e21e24 |
|---|---|
| Author | KikoCis <KikoCis@users.noreply.github.com> · 2026-10-15 23:58 +02:00 |
| Committer | KikoCis <KikoCis@users.noreply.github.com> · 2026-10-15 23:58 +02:00 |
| Parents | 10d96d61af6a |
2 files changed
modified crates/cli/src/main.rs +128 −12
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | mod sync; |
| 7 | 7 | mod work; |
| 8 | 8 | |
| 9 | +use std::collections::HashMap; | |
| 9 | 10 | use std::fs::File; |
| 10 | 11 | use std::io::{self, Read, Write}; |
| 11 | 12 | use std::path::{Path, PathBuf}; |
| @@ -96,12 +97,23 @@ | ||
| 96 | 97 | #[arg(short = 'n', long, default_value_t = 20)] |
| 97 | 98 | limit: usize, |
| 98 | 99 | }, |
| 99 | - /// Show one changeset: parents, root, signatures, message, changed paths. | |
| 100 | + /// Show one changeset: parents, root, signatures, agent, message, and | |
| 101 | + /// the files it changes against its first parent, as A, M, D or T. | |
| 100 | 102 | Show { |
| 101 | 103 | /// A ref, a changeset id, or a short id (6 hex digits or more). |
| 102 | 104 | #[arg(default_value = "main")] |
| 103 | 105 | revision: String, |
| 104 | 106 | }, |
| 107 | + /// Show the files that differ between two changesets: A (added), M | |
| 108 | + /// (modified), D (deleted) or T (type changed), as `git diff | |
| 109 | + /// --name-status` does. | |
| 110 | + #[command(long_about = DIFF_HELP)] | |
| 111 | + Diff { | |
| 112 | + /// The changeset to compare from: a ref, an id, or a short id. | |
| 113 | + from: String, | |
| 114 | + /// The changeset to compare to: a ref, an id, or a short id. | |
| 115 | + to: String, | |
| 116 | + }, | |
| 105 | 117 | /// Import from and export to git repositories. |
| 106 | 118 | Git { |
| 107 | 119 | #[command(subcommand)] |
| @@ -238,6 +250,7 @@ | ||
| 238 | 250 | Command::Undo => undo(cli), |
| 239 | 251 | Command::Log { revision, limit } => log(cli, revision, *limit), |
| 240 | 252 | Command::Show { revision } => show(cli, revision), |
| 253 | + Command::Diff { from, to } => diff(cli, from, to), | |
| 241 | 254 | Command::Git { |
| 242 | 255 | action: GitAction::Import { path }, |
| 243 | 256 | } => git_import(cli, path), |
| @@ -712,14 +725,126 @@ | ||
| 712 | 725 | } |
| 713 | 726 | } |
| 714 | 727 | |
| 728 | +/// How a path changed, as `git diff --name-status` says it: `A` added, `M` | |
| 729 | +/// modified (content or executable bit), `D` deleted, or `T` for a change | |
| 730 | +/// between a regular file, a symlink and a gitlink. `old` is what was there | |
| 731 | +/// before, if a file was. | |
| 732 | +fn change_status(old: Option<&ChangeOp>, new: &ChangeOp) -> &'static str { | |
| 733 | + let class = |op: &ChangeOp| match op { | |
| 734 | + ChangeOp::File { .. } | ChangeOp::Executable { .. } => 0, | |
| 735 | + ChangeOp::Symlink { .. } => 1, | |
| 736 | + ChangeOp::Gitlink { .. } => 2, | |
| 737 | + ChangeOp::Delete => 3, | |
| 738 | + }; | |
| 739 | + match (old, new) { | |
| 740 | + (_, ChangeOp::Delete) => "D", | |
| 741 | + (None, _) => "A", | |
| 742 | + (Some(old), new) if class(old) == class(new) => "M", | |
| 743 | + (Some(_), _) => "T", | |
| 744 | + } | |
| 745 | +} | |
| 746 | + | |
| 747 | +/// What the tree `old` has at each path where the tree `new` differs from | |
| 748 | +/// it: the change list of the diff back from `new` to `old`, one walk over | |
| 749 | +/// the difference. | |
| 750 | +fn old_entries(repo: &Repo, old: ObjectId, new: ObjectId) -> Result<HashMap<Vec<u8>, ChangeOp>> { | |
| 751 | + Ok(braid_engine::diff(repo.store(), new, old)? | |
| 752 | + .into_iter() | |
| 753 | + .filter(|change| !matches!(change.op, ChangeOp::Delete)) | |
| 754 | + .map(|change| (change.path, change.op)) | |
| 755 | + .collect()) | |
| 756 | +} | |
| 757 | + | |
| 758 | +/// A change as JSON, with its status and the file it replaced. | |
| 759 | +fn change_with_status_json(change: &Change, old: Option<&ChangeOp>) -> Value { | |
| 760 | + let mut value = change_json(change); | |
| 761 | + value["status"] = Value::String(change_status(old, &change.op).to_owned()); | |
| 762 | + value["old"] = match old { | |
| 763 | + Some(op) => { | |
| 764 | + let mut old = change_json(&Change { | |
| 765 | + path: change.path.clone(), | |
| 766 | + op: op.clone(), | |
| 767 | + }); | |
| 768 | + if let Some(fields) = old.as_object_mut() { | |
| 769 | + fields.remove("path"); | |
| 770 | + } | |
| 771 | + old | |
| 772 | + } | |
| 773 | + None => Value::Null, | |
| 774 | + }; | |
| 775 | + value | |
| 776 | +} | |
| 777 | + | |
| 778 | +/// Prints changes as `git diff --name-status` does: the status, a tab and | |
| 779 | +/// the path. | |
| 780 | +fn write_name_status( | |
| 781 | + out: &mut impl Write, | |
| 782 | + changes: &[Change], | |
| 783 | + olds: &HashMap<Vec<u8>, ChangeOp>, | |
| 784 | +) -> io::Result<()> { | |
| 785 | + for change in changes { | |
| 786 | + let status = change_status(olds.get(&change.path), &change.op); | |
| 787 | + writeln!(out, "{status}\t{}", String::from_utf8_lossy(&change.path))?; | |
| 788 | + } | |
| 789 | + Ok(()) | |
| 790 | +} | |
| 791 | + | |
| 792 | +/// The long help of `diff`. | |
| 793 | +const DIFF_HELP: &str = "Show the files that differ between two changesets, one line each: a | |
| 794 | +status, a tab and the path, as `git diff --name-status` prints them. The | |
| 795 | +status is A (added), M (modified: content or executable bit), D (deleted) | |
| 796 | +or T (type changed: between a regular file, a symlink and a gitlink). A | |
| 797 | +directory that became a file, or the other way round, is a D for every | |
| 798 | +file it had and an A for every file it has. | |
| 799 | + | |
| 800 | +With --json, diff prints from and to, the two changesets' full ids, and | |
| 801 | +changes: one per path, sorted by path bytes, with path, status, op (file, | |
| 802 | +executable, symlink, gitlink or delete) with blob and size, and old, what | |
| 803 | +the path held before, or null."; | |
| 804 | + | |
| 805 | +fn diff(cli: &Cli, from: &str, to: &str) -> Result<ExitCode> { | |
| 806 | + let repo = open_repo(cli)?; | |
| 807 | + let from = resolve_changeset(&repo, from)?; | |
| 808 | + let to = resolve_changeset(&repo, to)?; | |
| 809 | + let (from_root, to_root) = ( | |
| 810 | + read_changeset(&repo, &from)?.root, | |
| 811 | + read_changeset(&repo, &to)?.root, | |
| 812 | + ); | |
| 813 | + let changes = braid_engine::diff(repo.store(), from_root, to_root)?; | |
| 814 | + let olds = old_entries(&repo, from_root, to_root)?; | |
| 815 | + if cli.json { | |
| 816 | + print_json(&json!({ | |
| 817 | + "from": from.to_hex(), | |
| 818 | + "to": to.to_hex(), | |
| 819 | + "changes": changes | |
| 820 | + .iter() | |
| 821 | + .map(|change| change_with_status_json(change, olds.get(&change.path))) | |
| 822 | + .collect::<Vec<_>>(), | |
| 823 | + }))?; | |
| 824 | + } else { | |
| 825 | + write_name_status(&mut io::stdout().lock(), &changes, &olds)?; | |
| 826 | + } | |
| 827 | + Ok(ExitCode::SUCCESS) | |
| 828 | +} | |
| 829 | + | |
| 715 | 830 | fn show(cli: &Cli, revision: &str) -> Result<ExitCode> { |
| 716 | 831 | let repo = open_repo(cli)?; |
| 717 | 832 | let id = resolve_changeset(&repo, revision)?; |
| 718 | 833 | let changeset = read_changeset(&repo, &id)?; |
| 719 | 834 | let changes = read_changes(&repo, &changeset)?; |
| 835 | + // The change list is against the first parent: what that has at each | |
| 836 | + // path tells added from modified. | |
| 837 | + let parent_root = match changeset.parents.first() { | |
| 838 | + Some(parent) => read_changeset(&repo, parent)?.root, | |
| 839 | + None => braid_engine::EMPTY_TREE, | |
| 840 | + }; | |
| 841 | + let olds = old_entries(&repo, parent_root, changeset.root)?; | |
| 720 | 842 | if cli.json { |
| 721 | 843 | let mut value = changeset_json(&id, &changeset); |
| 722 | - value["changes"] = Value::Array(changes.iter().map(change_json).collect()); | |
| 844 | + value["changes"] = changes | |
| 845 | + .iter() | |
| 846 | + .map(|change| change_with_status_json(change, olds.get(&change.path))) | |
| 847 | + .collect(); | |
| 723 | 848 | print_json(&value)?; |
| 724 | 849 | return Ok(ExitCode::SUCCESS); |
| 725 | 850 | } |
| @@ -749,16 +874,7 @@ | ||
| 749 | 874 | writeln!(out, " {line}")?; |
| 750 | 875 | } |
| 751 | 876 | writeln!(out)?; |
| 752 | - for change in &changes { | |
| 753 | - let op = match change.op { | |
| 754 | - ChangeOp::Delete => "delete", | |
| 755 | - ChangeOp::File { .. } => "file", | |
| 756 | - ChangeOp::Executable { .. } => "exec", | |
| 757 | - ChangeOp::Symlink { .. } => "link", | |
| 758 | - ChangeOp::Gitlink { .. } => "gitlink", | |
| 759 | - }; | |
| 760 | - writeln!(out, "{op:<8} {}", String::from_utf8_lossy(&change.path))?; | |
| 761 | - } | |
| 877 | + write_name_status(&mut out, &changes, &olds)?; | |
| 762 | 878 | Ok(ExitCode::SUCCESS) |
| 763 | 879 | } |
| 764 | 880 |
modified crates/cli/tests/workspaces.rs +115 −0
| @@ -672,6 +672,121 @@ | ||
| 672 | 672 | assert!(text.contains(" Ada (agent-b) b's two"), "{text}"); |
| 673 | 673 | } |
| 674 | 674 | |
| 675 | +/// Statuses by path, from the changes of `show --json` or `diff --json`. | |
| 676 | +fn statuses(changes: &Value) -> Vec<(String, String)> { | |
| 677 | + changes | |
| 678 | + .as_array() | |
| 679 | + .unwrap() | |
| 680 | + .iter() | |
| 681 | + .map(|c| { | |
| 682 | + ( | |
| 683 | + c["path"].as_str().unwrap().to_owned(), | |
| 684 | + c["status"].as_str().unwrap().to_owned(), | |
| 685 | + ) | |
| 686 | + }) | |
| 687 | + .collect() | |
| 688 | +} | |
| 689 | + | |
| 690 | +fn pairs(expected: &[(&str, &str)]) -> Vec<(String, String)> { | |
| 691 | + expected | |
| 692 | + .iter() | |
| 693 | + .map(|(path, status)| (path.to_string(), status.to_string())) | |
| 694 | + .collect() | |
| 695 | +} | |
| 696 | + | |
| 697 | +#[cfg(unix)] | |
| 698 | +#[test] | |
| 699 | +fn show_and_diff_tell_added_modified_deleted_and_type_changes() { | |
| 700 | + use std::os::unix::fs::PermissionsExt; | |
| 701 | + | |
| 702 | + let repo = Repo::new(); | |
| 703 | + let seeded = seed( | |
| 704 | + &repo, | |
| 705 | + &[ | |
| 706 | + ("dir/x", "x\n"), | |
| 707 | + ("edit.txt", "1\n"), | |
| 708 | + ("flip", "a file\n"), | |
| 709 | + ("gone.txt", "g\n"), | |
| 710 | + ("run.sh", "#!/bin/sh\n"), | |
| 711 | + ], | |
| 712 | + ); | |
| 713 | + let ws = repo.workspace("ws"); | |
| 714 | + write(&ws, "edit.txt", "2\n"); | |
| 715 | + std::fs::remove_file(ws.join("gone.txt")).unwrap(); | |
| 716 | + write(&ws, "new.txt", "n\n"); | |
| 717 | + std::fs::set_permissions(ws.join("run.sh"), std::fs::Permissions::from_mode(0o755)).unwrap(); | |
| 718 | + std::fs::remove_file(ws.join("flip")).unwrap(); | |
| 719 | + std::os::unix::fs::symlink("edit.txt", ws.join("flip")).unwrap(); | |
| 720 | + std::fs::remove_dir_all(ws.join("dir")).unwrap(); | |
| 721 | + write(&ws, "dir", "now a file\n"); | |
| 722 | + let committed = json(&ws, &["commit", "-m", "every kind"]); | |
| 723 | + let id = committed["changeset"].as_str().unwrap(); | |
| 724 | + | |
| 725 | + let expected = pairs(&[ | |
| 726 | + ("dir", "A"), | |
| 727 | + ("dir/x", "D"), | |
| 728 | + ("edit.txt", "M"), | |
| 729 | + ("flip", "T"), | |
| 730 | + ("gone.txt", "D"), | |
| 731 | + ("new.txt", "A"), | |
| 732 | + ("run.sh", "M"), | |
| 733 | + ]); | |
| 734 | + let shown = json(&repo.root, &["show", id]); | |
| 735 | + assert_eq!(statuses(&shown["changes"]), expected); | |
| 736 | + // What each path held before is there too. | |
| 737 | + let edit = &shown["changes"][2]; | |
| 738 | + assert_eq!(edit["op"], "file"); | |
| 739 | + assert_eq!(edit["old"]["op"], "file"); | |
| 740 | + assert_eq!(edit["old"]["size"], 2); | |
| 741 | + assert_ne!(edit["old"]["blob"], edit["blob"]); | |
| 742 | + assert_eq!(shown["changes"][0]["old"], Value::Null); | |
| 743 | + assert_eq!(shown["changes"][6]["op"], "executable"); | |
| 744 | + // The text form is git's --name-status. | |
| 745 | + let text = String::from_utf8(braid(&repo.root, &["show", id]).stdout).unwrap(); | |
| 746 | + let listed: Vec<&str> = text.lines().filter(|line| line.contains('\t')).collect(); | |
| 747 | + assert_eq!( | |
| 748 | + listed, | |
| 749 | + [ | |
| 750 | + "A\tdir", | |
| 751 | + "D\tdir/x", | |
| 752 | + "M\tedit.txt", | |
| 753 | + "T\tflip", | |
| 754 | + "D\tgone.txt", | |
| 755 | + "A\tnew.txt", | |
| 756 | + "M\trun.sh" | |
| 757 | + ] | |
| 758 | + ); | |
| 759 | + | |
| 760 | + // diff says the same between the two changesets, and the opposite the | |
| 761 | + // other way round. | |
| 762 | + let diffed = json(&repo.root, &["diff", &seeded[..10], id]); | |
| 763 | + assert_eq!(diffed["from"], seeded.as_str()); | |
| 764 | + assert_eq!(diffed["to"], id); | |
| 765 | + assert_eq!(statuses(&diffed["changes"]), expected); | |
| 766 | + let back = json(&repo.root, &["diff", id, "main"]); | |
| 767 | + assert_eq!( | |
| 768 | + statuses(&back["changes"]), | |
| 769 | + pairs(&[ | |
| 770 | + ("dir", "D"), | |
| 771 | + ("dir/x", "A"), | |
| 772 | + ("edit.txt", "M"), | |
| 773 | + ("flip", "T"), | |
| 774 | + ("gone.txt", "A"), | |
| 775 | + ("new.txt", "D"), | |
| 776 | + ("run.sh", "M"), | |
| 777 | + ]) | |
| 778 | + ); | |
| 779 | + let text = String::from_utf8(braid(&repo.root, &["diff", "main", id]).stdout).unwrap(); | |
| 780 | + assert_eq!(text.lines().count(), 7); | |
| 781 | + assert!(text.starts_with("A\tdir\nD\tdir/x\n"), "{text}"); | |
| 782 | + assert!( | |
| 783 | + json(&repo.root, &["diff", id, id])["changes"] | |
| 784 | + .as_array() | |
| 785 | + .unwrap() | |
| 786 | + .is_empty() | |
| 787 | + ); | |
| 788 | +} | |
| 789 | + | |
| 675 | 790 | #[test] |
| 676 | 791 | fn removing_a_workspace() { |
| 677 | 792 | let repo = Repo::new(); |