| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! History: the log, changesets and their diffs, comparisons, branches and tags (section 15). |
| 5 | //! |
| 6 | //! Ids are 64 lowercase hex digits. The times of changesets are RFC 3339, |
| 7 | //! with the offset from UTC the changeset recorded. Text that a changeset |
| 8 | //! holds as bytes (names, messages, paths) comes as UTF-8, with what is not |
| 9 | //! UTF-8 replaced. A list holds at most one page, and `next`, when there is |
| 10 | //! more, is the opaque cursor that asks for the page after it (`?after=`). |
| 11 | |
| 12 | use braid_format::ObjectId; |
| 13 | use serde::{Deserialize, Serialize}; |
| 14 | |
| 15 | use crate::hex; |
| 16 | |
| 17 | /// The longest summary of a changeset, in characters. |
| 18 | pub const SUMMARY_CHARS: usize = 200; |
| 19 | |
| 20 | /// Who wrote or recorded a changeset, and when. |
| 21 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 22 | pub struct Signature { |
| 23 | /// The name. |
| 24 | pub name: String, |
| 25 | /// The e-mail address. |
| 26 | pub email: String, |
| 27 | /// When, in RFC 3339, with the offset from UTC that was recorded. |
| 28 | pub time: String, |
| 29 | } |
| 30 | |
| 31 | /// A changeset in a list: the log, or what a comparison brings. |
| 32 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 33 | pub struct ChangesetSummary { |
| 34 | /// The changeset. |
| 35 | #[serde(with = "hex::id")] |
| 36 | pub id: ObjectId, |
| 37 | /// Its parents, the first one first. |
| 38 | #[serde(with = "hex::ids")] |
| 39 | pub parents: Vec<ObjectId>, |
| 40 | /// Who wrote it. |
| 41 | pub author: Signature, |
| 42 | /// Who recorded it. |
| 43 | pub committer: Signature, |
| 44 | /// The first line of its message that is not blank, trimmed, at most |
| 45 | /// [`SUMMARY_CHARS`] characters. |
| 46 | pub summary: String, |
| 47 | /// The agent that made it, when it names one. |
| 48 | #[serde(default)] |
| 49 | pub agent: Option<String>, |
| 50 | } |
| 51 | |
| 52 | /// `GET …/log`: changesets along first parents, newest first. |
| 53 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 54 | pub struct Log { |
| 55 | /// The revision the log starts from, as asked (the default branch when |
| 56 | /// none was). |
| 57 | #[serde(rename = "ref")] |
| 58 | pub reference: String, |
| 59 | /// The changesets of this page. |
| 60 | pub changesets: Vec<ChangesetSummary>, |
| 61 | /// The cursor of the next page, or `None` at the first changeset. |
| 62 | #[serde(default)] |
| 63 | pub next: Option<String>, |
| 64 | } |
| 65 | |
| 66 | /// A file as one side of a change has it. |
| 67 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 68 | pub struct FileVersion { |
| 69 | /// `file`, `executable`, `symlink` or `gitlink`. |
| 70 | pub kind: String, |
| 71 | /// The blob of its content (for a symlink, its target); `None` for a |
| 72 | /// gitlink. |
| 73 | #[serde(default, with = "hex::opt_id", skip_serializing_if = "Option::is_none")] |
| 74 | pub blob: Option<ObjectId>, |
| 75 | /// The size of its content, in bytes; `None` for a gitlink. |
| 76 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 77 | pub size: Option<u64>, |
| 78 | /// A gitlink's git commit, in hex. |
| 79 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 80 | pub commit: Option<String>, |
| 81 | } |
| 82 | |
| 83 | /// One path that a changeset, or a comparison, changes. |
| 84 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 85 | pub struct FileChange { |
| 86 | /// The path, from the repository's root. |
| 87 | pub path: String, |
| 88 | /// `added`, `deleted`, `modified` (its content or its executable bit) or |
| 89 | /// `type_changed` (between a file, a symlink and a gitlink). |
| 90 | pub status: String, |
| 91 | /// What was there before; `None` when it is added. |
| 92 | pub old: Option<FileVersion>, |
| 93 | /// What is there after; `None` when it is deleted. |
| 94 | pub new: Option<FileVersion>, |
| 95 | } |
| 96 | |
| 97 | /// `GET …/changesets/{rev}`: a changeset, and the files it changes against |
| 98 | /// its first parent. |
| 99 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 100 | pub struct ChangesetInfo { |
| 101 | /// The changeset. |
| 102 | #[serde(with = "hex::id")] |
| 103 | pub id: ObjectId, |
| 104 | /// Its parents, the first one first. |
| 105 | #[serde(with = "hex::ids")] |
| 106 | pub parents: Vec<ObjectId>, |
| 107 | /// Its root tree. |
| 108 | #[serde(with = "hex::id")] |
| 109 | pub root: ObjectId, |
| 110 | /// Who wrote it. |
| 111 | pub author: Signature, |
| 112 | /// Who recorded it. |
| 113 | pub committer: Signature, |
| 114 | /// Its whole message. |
| 115 | pub message: String, |
| 116 | /// The agent that made it, when it names one. |
| 117 | #[serde(default)] |
| 118 | pub agent: Option<String>, |
| 119 | /// The changeset it was rebased from, when a landing rebased it. |
| 120 | #[serde(default, with = "hex::opt_id")] |
| 121 | pub rebased_from: Option<ObjectId>, |
| 122 | /// The git commit it exports to, when the hub knows it already. |
| 123 | #[serde(default)] |
| 124 | pub git_commit: Option<String>, |
| 125 | /// Its extra fields, in order, as `[name, value]`. |
| 126 | #[serde(default)] |
| 127 | pub extras: Vec<(String, String)>, |
| 128 | /// The files it changes, in order of their paths' bytes. |
| 129 | pub files: Vec<FileChange>, |
| 130 | /// Whether it changes more files than `files` lists. |
| 131 | #[serde(default)] |
| 132 | pub files_truncated: bool, |
| 133 | } |
| 134 | |
| 135 | /// One hunk of a file's diff. |
| 136 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 137 | pub struct Hunk { |
| 138 | /// Its first line in the old version, counted from 1; with no old |
| 139 | /// lines, the line it comes after (0 at the start). |
| 140 | pub old_start: u64, |
| 141 | /// How many lines of the old version it holds. |
| 142 | pub old_lines: u64, |
| 143 | /// Its first line in the new version, as `old_start` counts. |
| 144 | pub new_start: u64, |
| 145 | /// How many lines of the new version it holds. |
| 146 | pub new_lines: u64, |
| 147 | /// Its lines, in order, as `[op, text]`: `op` is `" "` for a line both |
| 148 | /// versions have, `"-"` for one of the old version only and `"+"` for |
| 149 | /// one of the new version only; `text` is the line, with its line |
| 150 | /// ending when it has one. |
| 151 | pub lines: Vec<(String, String)>, |
| 152 | } |
| 153 | |
| 154 | /// One file of a diff. |
| 155 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 156 | pub struct FileDiff { |
| 157 | /// The path, from the repository's root. |
| 158 | pub path: String, |
| 159 | /// As [`FileChange::status`] says. |
| 160 | pub status: String, |
| 161 | /// What was there before; `None` when it is added. |
| 162 | pub old: Option<FileVersion>, |
| 163 | /// What is there after; `None` when it is deleted. |
| 164 | pub new: Option<FileVersion>, |
| 165 | /// Whether either version is binary, which is not diffed by lines. A |
| 166 | /// file whose content stays the same (a new executable bit alone) has |
| 167 | /// no lines to diff, and is neither binary nor truncated. |
| 168 | #[serde(default)] |
| 169 | pub binary: bool, |
| 170 | /// The lines the new version adds. |
| 171 | #[serde(default)] |
| 172 | pub additions: u64, |
| 173 | /// The lines it deletes. |
| 174 | #[serde(default)] |
| 175 | pub deletions: u64, |
| 176 | /// The hunks, in order. |
| 177 | #[serde(default)] |
| 178 | pub hunks: Vec<Hunk>, |
| 179 | /// Whether the hunks are cut short, or left out, to stay within the |
| 180 | /// hub's sizes: ask for the file alone with `path` to get it whole when |
| 181 | /// it fits. |
| 182 | #[serde(default)] |
| 183 | pub truncated: bool, |
| 184 | } |
| 185 | |
| 186 | /// `GET …/changesets/{rev}/diff`: a page of a changeset's diff. |
| 187 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 188 | pub struct DiffPage { |
| 189 | /// The files of this page, in order of their paths' bytes. |
| 190 | pub files: Vec<FileDiff>, |
| 191 | /// The page after this one, when there are more files. |
| 192 | #[serde(default)] |
| 193 | pub next_page: Option<u64>, |
| 194 | } |
| 195 | |
| 196 | /// `GET …/compare`: what a head brings to a base. |
| 197 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 198 | pub struct Comparison { |
| 199 | /// The base's changeset. |
| 200 | #[serde(with = "hex::id")] |
| 201 | pub base: ObjectId, |
| 202 | /// The head's changeset. |
| 203 | #[serde(with = "hex::id")] |
| 204 | pub head: ObjectId, |
| 205 | /// Their best common ancestor (the one with the smallest id when there |
| 206 | /// are several), or `None` when they share no history. |
| 207 | #[serde(with = "hex::opt_id")] |
| 208 | pub merge_base: Option<ObjectId>, |
| 209 | /// The head's first-parent chain down to a changeset the base has, |
| 210 | /// oldest first. |
| 211 | pub stack: Vec<ChangesetSummary>, |
| 212 | /// The rest of what the head has and the base lacks, which merges in |
| 213 | /// the stack bring: parents before children. |
| 214 | pub brings: Vec<ChangesetSummary>, |
| 215 | /// The files the head changes since the merge base (since nothing, when |
| 216 | /// there is none). |
| 217 | pub files: Vec<FileChange>, |
| 218 | /// Whether `stack`, `brings` or `files` is cut short to stay within the |
| 219 | /// hub's sizes. |
| 220 | #[serde(default)] |
| 221 | pub truncated: bool, |
| 222 | } |
| 223 | |
| 224 | /// A branch or a tag. |
| 225 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 226 | pub struct RefSummary { |
| 227 | /// The ref, as a revision names it: `main`, `tags/v1`. |
| 228 | pub name: String, |
| 229 | /// The changeset it points to. |
| 230 | #[serde(with = "hex::id")] |
| 231 | pub head: ObjectId, |
| 232 | /// When that changeset was recorded, as [`Signature::time`] says. |
| 233 | pub time: String, |
| 234 | /// Its summary, as [`ChangesetSummary::summary`] says. |
| 235 | pub summary: String, |
| 236 | } |
| 237 | |
| 238 | /// `GET …/branches` and `GET …/tags`, in order of their names. |
| 239 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 240 | pub struct RefList { |
| 241 | /// The refs of this page. |
| 242 | pub refs: Vec<RefSummary>, |
| 243 | /// The cursor of the next page, or `None` on the last one. |
| 244 | #[serde(default)] |
| 245 | pub next: Option<String>, |
| 246 | } |
| 247 | |
| 248 | #[cfg(test)] |
| 249 | mod tests { |
| 250 | use super::*; |
| 251 | use braid_format::Kind; |
| 252 | |
| 253 | fn id(text: &str) -> ObjectId { |
| 254 | ObjectId::hash(Kind::Changeset, text.as_bytes()) |
| 255 | } |
| 256 | |
| 257 | #[test] |
| 258 | fn bodies_on_the_wire() { |
| 259 | let log = Log { |
| 260 | reference: "main".into(), |
| 261 | changesets: vec![ChangesetSummary { |
| 262 | id: id("two"), |
| 263 | parents: vec![id("one")], |
| 264 | author: Signature { |
| 265 | name: "Ada".into(), |
| 266 | email: "ada@example.org".into(), |
| 267 | time: "2023-11-14T22:13:20Z".into(), |
| 268 | }, |
| 269 | committer: Signature::default(), |
| 270 | summary: "Say why".into(), |
| 271 | agent: Some("agent-1".into()), |
| 272 | }], |
| 273 | next: Some(id("one").to_hex()), |
| 274 | }; |
| 275 | let text = serde_json::to_string(&log).unwrap(); |
| 276 | assert!(text.contains("\"ref\":\"main\""), "{text}"); |
| 277 | assert!(text.contains(&id("two").to_hex()), "{text}"); |
| 278 | assert_eq!(serde_json::from_str::<Log>(&text).unwrap(), log); |
| 279 | |
| 280 | // A gitlink has a commit and no blob; a file has a blob and a size. |
| 281 | let gitlink = FileVersion { |
| 282 | kind: "gitlink".into(), |
| 283 | blob: None, |
| 284 | size: None, |
| 285 | commit: Some("ab".repeat(20)), |
| 286 | }; |
| 287 | let text = serde_json::to_string(&gitlink).unwrap(); |
| 288 | assert!(!text.contains("blob") && !text.contains("size"), "{text}"); |
| 289 | assert_eq!(serde_json::from_str::<FileVersion>(&text).unwrap(), gitlink); |
| 290 | |
| 291 | let hunk = Hunk { |
| 292 | old_start: 0, |
| 293 | old_lines: 0, |
| 294 | new_start: 1, |
| 295 | new_lines: 1, |
| 296 | lines: vec![("+".into(), "new\n".into())], |
| 297 | }; |
| 298 | let text = serde_json::to_string(&hunk).unwrap(); |
| 299 | assert!(text.contains(r#""lines":[["+","new\n"]]"#), "{text}"); |
| 300 | assert_eq!(serde_json::from_str::<Hunk>(&text).unwrap(), hunk); |
| 301 | |
| 302 | // What a hub that says less gives still reads. |
| 303 | let page: DiffPage = serde_json::from_str( |
| 304 | r#"{"files":[{"path":"a","status":"added","old":null,"new":null}]}"#, |
| 305 | ) |
| 306 | .unwrap(); |
| 307 | assert_eq!(page.next_page, None); |
| 308 | assert!(!page.files[0].binary && page.files[0].hunks.is_empty()); |
| 309 | let refs: RefList = serde_json::from_str(r#"{"refs":[]}"#).unwrap(); |
| 310 | assert_eq!(refs, RefList::default()); |
| 311 | } |
| 312 | } |