Elffuss BraidHub

utopia-ia/elffuss-braid public

356f55034aff · 3.7 KiBRawPermalink
1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 UtopiaIA
3
4//! Changes and conflicts as `braid land --json` prints them, so that a
5//! landing on the hub reports exactly what a local landing does. The CLI's
6//! own printer and these must agree.
7
8use braid_engine::{Conflict, ConflictKind, HunkSide};
9use braid_format::{Change, ChangeOp, EntryKind};
10use serde_json::{Value, json};
11
12fn show(path: &[u8]) -> String {
13 String::from_utf8_lossy(path).into_owned()
14}
15
16/// A change: `{"path", "op", …}` with `op` one of `delete`, `file`,
17/// `executable`, `symlink` (with `blob` and `size`) or `gitlink` (with
18/// `commit`).
19pub fn change(change: &Change) -> Value {
20 let path = show(&change.path);
21 match &change.op {
22 ChangeOp::Delete => json!({ "path": path, "op": "delete" }),
23 ChangeOp::File { blob, size } => {
24 json!({ "path": path, "op": "file", "blob": blob.to_hex(), "size": size })
25 }
26 ChangeOp::Executable { blob, size } => {
27 json!({ "path": path, "op": "executable", "blob": blob.to_hex(), "size": size })
28 }
29 ChangeOp::Symlink { blob, size } => {
30 json!({ "path": path, "op": "symlink", "blob": blob.to_hex(), "size": size })
31 }
32 ChangeOp::Gitlink { commit } => json!({
33 "path": path,
34 "op": "gitlink",
35 "commit": commit.as_bytes().iter().map(|b| format!("{b:02x}")).collect::<String>(),
36 }),
37 }
38}
39
40/// One side of a conflict: `null`, `{"kind": "tree", "tree"}`, or the entry
41/// as a change without its path, `op` renamed `kind`.
42pub fn entry(entry: &Option<EntryKind>) -> Value {
43 match entry {
44 None => Value::Null,
45 Some(EntryKind::Tree { tree }) => json!({ "kind": "tree", "tree": tree.to_hex() }),
46 Some(kind) => match ChangeOp::from_entry(kind) {
47 Some(op) => {
48 let mut value = change(&Change {
49 path: Vec::new(),
50 op,
51 });
52 if let Some(object) = value.as_object_mut() {
53 object.remove("path");
54 if let Some(op) = object.remove("op") {
55 object.insert("kind".to_owned(), op);
56 }
57 }
58 value
59 }
60 None => Value::Null,
61 },
62 }
63}
64
65fn side(side: &HunkSide) -> Value {
66 json!({
67 "start": side.lines.start,
68 "end": side.lines.end,
69 "text": String::from_utf8_lossy(&side.text),
70 })
71}
72
73/// A conflict as data, its sides named from the lander's point of view:
74/// `base` (the common ancestor), `branch` (what the branch has now) and
75/// `workspace` (the change being landed), with the hunks of a content
76/// conflict.
77pub fn conflict(conflict: &Conflict) -> Value {
78 let mut value = json!({
79 "path": show(&conflict.path),
80 "kind": conflict.kind.code(),
81 "base": entry(&conflict.base),
82 "branch": entry(&conflict.ours),
83 "workspace": entry(&conflict.theirs),
84 "hunks": [],
85 });
86 match &conflict.kind {
87 ConflictKind::Content { hunks } => {
88 value["hunks"] = hunks
89 .iter()
90 .map(|hunk| {
91 json!({
92 "base": side(&hunk.base),
93 "branch": side(&hunk.ours),
94 "workspace": side(&hunk.theirs),
95 })
96 })
97 .collect();
98 }
99 ConflictKind::DirectoryRemoved { directory } => {
100 value["directory"] = Value::String(show(directory));
101 }
102 ConflictKind::Case { existing } => {
103 value["existing"] = Value::String(show(existing));
104 }
105 _ => {}
106 }
107 value
108}