| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! How each branch of a linked repository compares with the hub's, for |
| 5 | //! `braid hub status`, and how the hub verifies it: its green and its red. |
| 6 | |
| 7 | use std::collections::{BTreeMap, BTreeSet}; |
| 8 | |
| 9 | use anyhow::Result; |
| 10 | use braid_format::{Kind, ObjectId, Objects}; |
| 11 | use braid_hubproto::verify::{green_ref, red_ref}; |
| 12 | use braid_repo::Repo; |
| 13 | |
| 14 | use super::fetch::remote_ref; |
| 15 | |
| 16 | /// How a branch compares with the hub's. |
| 17 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 18 | pub(crate) enum State { |
| 19 | /// Both at the same changeset. |
| 20 | Same, |
| 21 | /// The hub's has changesets this one lacks, on top of it; or only the |
| 22 | /// hub has the branch. `braid hub fetch` brings them in. |
| 23 | Behind, |
| 24 | /// This one has changesets the hub lacks, on top of the hub's; or only |
| 25 | /// this repository has the branch. |
| 26 | Ahead, |
| 27 | /// Each has changesets the other lacks. |
| 28 | Diverged, |
| 29 | } |
| 30 | |
| 31 | impl State { |
| 32 | /// How output names it. |
| 33 | pub(crate) fn code(self) -> &'static str { |
| 34 | match self { |
| 35 | State::Same => "same", |
| 36 | State::Behind => "behind", |
| 37 | State::Ahead => "ahead", |
| 38 | State::Diverged => "diverged", |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /// One branch, here and on the hub. |
| 44 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 45 | pub(crate) struct BranchState { |
| 46 | /// The branch. |
| 47 | pub(crate) name: String, |
| 48 | /// Its head here. |
| 49 | pub(crate) local: Option<ObjectId>, |
| 50 | /// Its head on the hub. |
| 51 | pub(crate) hub: Option<ObjectId>, |
| 52 | /// How they compare. |
| 53 | pub(crate) state: State, |
| 54 | /// The hub's green of the branch, when it verifies the branch: the |
| 55 | /// newest changeset with a pass it accepts. |
| 56 | pub(crate) green: Option<ObjectId>, |
| 57 | /// The hub's red of the branch, while it holds the branch red. |
| 58 | pub(crate) red: Option<ObjectId>, |
| 59 | } |
| 60 | |
| 61 | /// Compares every branch of `repo` and of the hub, whose refs are `hub`. |
| 62 | /// |
| 63 | /// When the hub's head is not in the store yet, the hub moved since the |
| 64 | /// last fetch, and a hub's branch only ever moves forward: a branch at, or |
| 65 | /// below, where the hub's was last seen (`remote/hub/<branch>`) is behind, |
| 66 | /// and any other is diverged. |
| 67 | pub(crate) fn compare(repo: &Repo, hub: &BTreeMap<String, ObjectId>) -> Result<Vec<BranchState>> { |
| 68 | let store = repo.store(); |
| 69 | let local = store.refs()?; |
| 70 | let names: BTreeSet<&str> = local |
| 71 | .iter() |
| 72 | .map(|(name, _)| name) |
| 73 | .chain(hub.keys().map(String::as_str)) |
| 74 | .filter(|name| braid_repo::is_branch(name)) |
| 75 | .collect(); |
| 76 | let holds = |id: &ObjectId| -> Result<bool> { |
| 77 | Ok(matches!(store.stat(id)?, Some((Kind::Changeset, _)))) |
| 78 | }; |
| 79 | let mut history = repo.history(); |
| 80 | let mut on_chain = |head: ObjectId, base: ObjectId| -> Result<bool> { |
| 81 | Ok(history.first_parent_chain_contains(head, base, usize::MAX)?) |
| 82 | }; |
| 83 | let mut states = Vec::new(); |
| 84 | for name in names { |
| 85 | let (here, there) = (local.get(name), hub.get(name).copied()); |
| 86 | let state = match (here, there) { |
| 87 | (Some(here), Some(there)) if here == there => State::Same, |
| 88 | (Some(here), Some(there)) if holds(&here)? && holds(&there)? => { |
| 89 | if on_chain(there, here)? { |
| 90 | State::Behind |
| 91 | } else if on_chain(here, there)? { |
| 92 | State::Ahead |
| 93 | } else { |
| 94 | State::Diverged |
| 95 | } |
| 96 | } |
| 97 | (Some(here), Some(there)) if holds(&here)? && !holds(&there)? => { |
| 98 | match local.get(&remote_ref(name)) { |
| 99 | Some(seen) if holds(&seen)? && (seen == here || on_chain(seen, here)?) => { |
| 100 | State::Behind |
| 101 | } |
| 102 | _ => State::Diverged, |
| 103 | } |
| 104 | } |
| 105 | (Some(_), Some(_)) => State::Diverged, |
| 106 | (None, _) => State::Behind, |
| 107 | (Some(_), None) => State::Ahead, |
| 108 | }; |
| 109 | states.push(BranchState { |
| 110 | name: name.to_owned(), |
| 111 | local: here, |
| 112 | hub: there, |
| 113 | state, |
| 114 | green: hub.get(&green_ref(name)).copied(), |
| 115 | red: hub.get(&red_ref(name)).copied(), |
| 116 | }); |
| 117 | } |
| 118 | history.flush()?; |
| 119 | Ok(states) |
| 120 | } |
| 121 | |
| 122 | #[cfg(test)] |
| 123 | mod tests { |
| 124 | use super::*; |
| 125 | use crate::hub::testing::{Fixture, ada_change, set_ref}; |
| 126 | |
| 127 | #[test] |
| 128 | fn each_branch_against_the_hub() { |
| 129 | let f = Fixture::new(); |
| 130 | let root = ada_change(&f.repo, None, &[("a.txt", "a\n")]); |
| 131 | let two = ada_change(&f.repo, Some(root), &[("b.txt", "b\n")]); |
| 132 | let other = ada_change(&f.repo, Some(root), &[("c.txt", "c\n")]); |
| 133 | for name in ["same", "behind", "ahead", "diverged", "local-only"] { |
| 134 | set_ref(&f.repo, name, root); |
| 135 | } |
| 136 | set_ref(&f.repo, "ahead", two); |
| 137 | set_ref(&f.repo, "diverged", other); |
| 138 | set_ref(&f.repo, "tags/v1", root); |
| 139 | // The hub moved past what this repository holds, from where it was |
| 140 | // last seen. |
| 141 | let unknown = ObjectId::hash(Kind::Changeset, b"not fetched"); |
| 142 | set_ref(&f.repo, "moved", root); |
| 143 | set_ref(&f.repo, "remote/hub/moved", root); |
| 144 | set_ref(&f.repo, "lost", two); |
| 145 | set_ref(&f.repo, "remote/hub/lost", other); |
| 146 | |
| 147 | let hub: BTreeMap<String, ObjectId> = [ |
| 148 | ("same", root), |
| 149 | ("behind", two), |
| 150 | ("ahead", root), |
| 151 | ("diverged", two), |
| 152 | ("hub-only", two), |
| 153 | ("moved", unknown), |
| 154 | ("lost", unknown), |
| 155 | ("tags/v1", two), |
| 156 | // The hub verifies `behind`: green at root, red at two. |
| 157 | ("braid/green/behind", root), |
| 158 | ("braid/red/behind", two), |
| 159 | ] |
| 160 | .into_iter() |
| 161 | .map(|(name, id)| (name.to_owned(), id)) |
| 162 | .collect(); |
| 163 | let states = compare(&f.repo, &hub).unwrap(); |
| 164 | let found: Vec<(&str, State)> = states.iter().map(|s| (s.name.as_str(), s.state)).collect(); |
| 165 | assert_eq!( |
| 166 | found, |
| 167 | [ |
| 168 | ("ahead", State::Ahead), |
| 169 | ("behind", State::Behind), |
| 170 | ("diverged", State::Diverged), |
| 171 | ("hub-only", State::Behind), |
| 172 | ("local-only", State::Ahead), |
| 173 | ("lost", State::Diverged), |
| 174 | ("moved", State::Behind), |
| 175 | ("same", State::Same), |
| 176 | ] |
| 177 | ); |
| 178 | let verified: Vec<(&str, Option<ObjectId>, Option<ObjectId>)> = states |
| 179 | .iter() |
| 180 | .filter(|s| s.green.is_some() || s.red.is_some()) |
| 181 | .map(|s| (s.name.as_str(), s.green, s.red)) |
| 182 | .collect(); |
| 183 | assert_eq!(verified, [("behind", Some(root), Some(two))]); |
| 184 | } |
| 185 | } |