| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! Errors of the git bridge. |
| 5 | |
| 6 | use core::fmt; |
| 7 | |
| 8 | use braid_format::{Kind, ObjectId}; |
| 9 | |
| 10 | use crate::oid::{GitKind, GitOid}; |
| 11 | |
| 12 | /// An error from a lower layer: gitoxide or an object store. |
| 13 | pub type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>; |
| 14 | |
| 15 | /// Why an import, an export or a round trip failed. |
| 16 | #[derive(Debug)] |
| 17 | #[non_exhaustive] |
| 18 | pub enum Error { |
| 19 | /// gitoxide could not open, read or write a repository. |
| 20 | Git { |
| 21 | /// What was being done. |
| 22 | context: String, |
| 23 | /// The underlying error. |
| 24 | source: BoxError, |
| 25 | }, |
| 26 | /// The braid object store failed. |
| 27 | Objects(BoxError), |
| 28 | /// The repository is a shallow clone, so part of its history is missing. |
| 29 | Shallow, |
| 30 | /// The repository uses SHA-256 object ids, which the bridge does not |
| 31 | /// support yet. |
| 32 | Sha256, |
| 33 | /// A ref that was asked for does not exist. |
| 34 | MissingRef(Vec<u8>), |
| 35 | /// A git object is not in the repository. |
| 36 | MissingObject(GitOid), |
| 37 | /// A git object is of another kind than the object naming it says. |
| 38 | WrongKind { |
| 39 | /// The object. |
| 40 | oid: GitOid, |
| 41 | /// The kind it should have. |
| 42 | expected: GitKind, |
| 43 | /// The kind it has. |
| 44 | found: GitKind, |
| 45 | }, |
| 46 | /// A git object's bytes do not hash to its id. |
| 47 | CorruptObject(GitOid), |
| 48 | /// SHA-1 collision detection found a known attack in an object. |
| 49 | Collision { |
| 50 | /// The kind of the object. |
| 51 | kind: GitKind, |
| 52 | }, |
| 53 | /// A git object cannot be parsed. |
| 54 | Malformed { |
| 55 | /// The object. |
| 56 | oid: GitOid, |
| 57 | /// Its kind. |
| 58 | kind: GitKind, |
| 59 | /// What is wrong with it. |
| 60 | reason: &'static str, |
| 61 | }, |
| 62 | /// A tree holds a name that braid refuses, such as an alias of `.git`. |
| 63 | /// These rules protect every checkout made from the repository. |
| 64 | RefusedPath { |
| 65 | /// The commit whose tree holds the path, or the object a ref points |
| 66 | /// to when no commit is involved. |
| 67 | commit: GitOid, |
| 68 | /// The full path from the root of that tree. |
| 69 | path: Vec<u8>, |
| 70 | /// Why the name is refused. |
| 71 | reason: String, |
| 72 | }, |
| 73 | /// A tree entry has a mode that is not a file, an executable, a symlink, |
| 74 | /// a directory or a submodule. |
| 75 | UnsupportedMode { |
| 76 | /// The commit whose tree holds the entry, or the object a ref points |
| 77 | /// to when no commit is involved. |
| 78 | commit: GitOid, |
| 79 | /// The full path of the entry. |
| 80 | path: Vec<u8>, |
| 81 | /// The mode as written in the tree. |
| 82 | mode: Vec<u8>, |
| 83 | }, |
| 84 | /// An imported object, exported again, did not give back its id. |
| 85 | Verification { |
| 86 | /// The object. |
| 87 | oid: GitOid, |
| 88 | /// Its kind. |
| 89 | kind: GitKind, |
| 90 | /// What differed. |
| 91 | detail: String, |
| 92 | }, |
| 93 | /// A braid object is missing from the store. |
| 94 | MissingBraidObject { |
| 95 | /// The object. |
| 96 | id: ObjectId, |
| 97 | /// The kind it should have. |
| 98 | expected: Kind, |
| 99 | }, |
| 100 | /// A braid object is not what the object naming it expects, or cannot be |
| 101 | /// decoded. |
| 102 | BadBraidObject { |
| 103 | /// The object. |
| 104 | id: ObjectId, |
| 105 | /// What is wrong with it. |
| 106 | detail: String, |
| 107 | }, |
| 108 | /// The map does not know an object that an export needs. |
| 109 | NotMapped { |
| 110 | /// What kind of object. |
| 111 | what: &'static str, |
| 112 | /// Its id, as text. |
| 113 | id: String, |
| 114 | }, |
| 115 | /// The git commit a changeset was imported from disagrees with the |
| 116 | /// changeset or with the map. |
| 117 | OriginMismatch { |
| 118 | /// The changeset. |
| 119 | changeset: ObjectId, |
| 120 | /// What disagrees. |
| 121 | detail: String, |
| 122 | }, |
| 123 | /// A changeset cannot be written as a git commit. |
| 124 | Unrepresentable { |
| 125 | /// The changeset. |
| 126 | changeset: ObjectId, |
| 127 | /// Why. |
| 128 | detail: String, |
| 129 | }, |
| 130 | /// A round trip found a difference between the source and the copy. |
| 131 | RoundTrip(String), |
| 132 | } |
| 133 | |
| 134 | /// Shows a path or a name, which are bytes, as text. |
| 135 | pub(crate) struct ShowBytes<'a>(pub &'a [u8]); |
| 136 | |
| 137 | impl fmt::Display for ShowBytes<'_> { |
| 138 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 139 | write!(f, "{:?}", String::from_utf8_lossy(self.0)) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | impl fmt::Display for Error { |
| 144 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 145 | match self { |
| 146 | Error::Git { context, source } => write!(f, "{context}: {source}"), |
| 147 | Error::Objects(source) => write!(f, "object store: {source}"), |
| 148 | Error::Shallow => { |
| 149 | f.write_str("the repository is a shallow clone; importing needs its whole history") |
| 150 | } |
| 151 | Error::Sha256 => f.write_str( |
| 152 | "the repository uses SHA-256 object ids, which the git bridge does not support yet", |
| 153 | ), |
| 154 | Error::MissingRef(name) => write!(f, "ref {} does not exist", ShowBytes(name)), |
| 155 | Error::MissingObject(oid) => write!(f, "git object {oid} is missing"), |
| 156 | Error::WrongKind { |
| 157 | oid, |
| 158 | expected, |
| 159 | found, |
| 160 | } => write!(f, "git object {oid} is a {found}, expected a {expected}"), |
| 161 | Error::CorruptObject(oid) => write!(f, "git object {oid} does not hash to its id"), |
| 162 | Error::Collision { kind } => { |
| 163 | write!(f, "SHA-1 collision attack detected in a git {kind}") |
| 164 | } |
| 165 | Error::Malformed { oid, kind, reason } => { |
| 166 | write!(f, "malformed git {kind} {oid}: {reason}") |
| 167 | } |
| 168 | Error::RefusedPath { |
| 169 | commit, |
| 170 | path, |
| 171 | reason, |
| 172 | } => write!( |
| 173 | f, |
| 174 | "commit {commit}: path {} is refused: {reason}", |
| 175 | ShowBytes(path) |
| 176 | ), |
| 177 | Error::UnsupportedMode { commit, path, mode } => write!( |
| 178 | f, |
| 179 | "commit {commit}: path {} has the unsupported mode {}", |
| 180 | ShowBytes(path), |
| 181 | ShowBytes(mode) |
| 182 | ), |
| 183 | Error::Verification { oid, kind, detail } => { |
| 184 | write!(f, "{kind} {oid} does not survive export: {detail}") |
| 185 | } |
| 186 | Error::MissingBraidObject { id, expected } => { |
| 187 | write!(f, "braid {expected} {id} is missing") |
| 188 | } |
| 189 | Error::BadBraidObject { id, detail } => write!(f, "braid object {id}: {detail}"), |
| 190 | Error::NotMapped { what, id } => write!(f, "the git map does not know {what} {id}"), |
| 191 | Error::OriginMismatch { changeset, detail } => { |
| 192 | write!(f, "changeset {changeset}: {detail}") |
| 193 | } |
| 194 | Error::Unrepresentable { changeset, detail } => write!( |
| 195 | f, |
| 196 | "changeset {changeset} cannot be written as a git commit: {detail}" |
| 197 | ), |
| 198 | Error::RoundTrip(detail) => write!(f, "round trip: {detail}"), |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | impl std::error::Error for Error { |
| 204 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 205 | match self { |
| 206 | Error::Git { source, .. } | Error::Objects(source) => Some(source.as_ref()), |
| 207 | _ => None, |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | impl Error { |
| 213 | /// Wraps a gitoxide error with what was being done. |
| 214 | pub(crate) fn git(context: impl Into<String>, source: impl Into<BoxError>) -> Self { |
| 215 | Error::Git { |
| 216 | context: context.into(), |
| 217 | source: source.into(), |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /// Wraps an object store error. |
| 222 | pub(crate) fn objects(source: impl std::error::Error + Send + Sync + 'static) -> Self { |
| 223 | Error::Objects(Box::new(source)) |
| 224 | } |
| 225 | } |