Author:
Hash:
Timestamp:
+45 -54 +/-3 browse
Kevin Schoon [me@kevinschoon.com]
7d5c0de1c465320e3ce28764195a422aa5bb08a8
Tue, 05 Aug 2025 15:41:07 +0000 (3 months ago)
| 1 | diff --git a/crates/git/src/error.rs b/crates/git/src/error.rs |
| 2 | index d7393c0..98872f2 100644 |
| 3 | --- a/crates/git/src/error.rs |
| 4 | +++ b/crates/git/src/error.rs |
| 5 | @@ -1,12 +1,16 @@ |
| 6 | use std::error::Error as StdError; |
| 7 | use std::fmt::Display; |
| 8 | use std::io::{Error as IoError, ErrorKind as IoErrorKind}; |
| 9 | + use std::path::PathBuf; |
| 10 | |
| 11 | use git2::{Error as GitError, ErrorCode as GitErrorCode}; |
| 12 | |
| 13 | + /// git related errors |
| 14 | #[derive(Debug)] |
| 15 | - pub enum ErrorKind { |
| 16 | + pub enum Error { |
| 17 | Git(GitError), |
| 18 | + CannotOpenRepository { path: PathBuf, git_error: GitError }, |
| 19 | + CannotCreateRepository { path: PathBuf, git_error: GitError }, |
| 20 | Io(IoError), |
| 21 | BlobTooLarge, |
| 22 | BlobIsBinary, |
| 23 | @@ -15,71 +19,51 @@ pub enum ErrorKind { |
| 24 | ConfigError(String), |
| 25 | } |
| 26 | |
| 27 | - #[derive(Debug)] |
| 28 | - /// git related errors |
| 29 | - pub struct Error { |
| 30 | - kind: ErrorKind, |
| 31 | - } |
| 32 | - |
| 33 | impl Error { |
| 34 | - pub fn kind(kind: ErrorKind) -> Self { |
| 35 | - Error { kind } |
| 36 | - } |
| 37 | - |
| 38 | /// determines if the error looks like a missing resource |
| 39 | pub fn not_found(&self) -> bool { |
| 40 | - match self.kind { |
| 41 | - ErrorKind::Git(ref err) => err.code() == GitErrorCode::NotFound, |
| 42 | - ErrorKind::Io(ref err) => err.kind() == IoErrorKind::NotFound, |
| 43 | - ErrorKind::BlobTooLarge => false, |
| 44 | - ErrorKind::BlobIsBinary => false, |
| 45 | - ErrorKind::ObjectNotATree => false, |
| 46 | - ErrorKind::NotAReference => false, |
| 47 | - ErrorKind::ConfigError(_) => false, |
| 48 | + match self { |
| 49 | + Error::Git(ref err) => err.code() == GitErrorCode::NotFound, |
| 50 | + Error::Io(ref err) => err.kind() == IoErrorKind::NotFound, |
| 51 | + Error::CannotOpenRepository { path: _, git_error } => { |
| 52 | + git_error.code() == GitErrorCode::NotFound |
| 53 | + } |
| 54 | + _ => false, |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | impl From<GitError> for Error { |
| 60 | fn from(value: GitError) -> Self { |
| 61 | - Error { |
| 62 | - kind: ErrorKind::Git(value), |
| 63 | - } |
| 64 | + Error::Git(value) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | impl From<IoError> for Error { |
| 69 | fn from(value: IoError) -> Self { |
| 70 | - Error { |
| 71 | - kind: ErrorKind::Io(value), |
| 72 | - } |
| 73 | + Error::Io(value) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | impl Display for Error { |
| 78 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 79 | - match self.kind { |
| 80 | - ErrorKind::Git(ref err) => err.fmt(f), |
| 81 | - ErrorKind::Io(ref err) => err.fmt(f), |
| 82 | - ErrorKind::BlobTooLarge => write!(f, "blob is too large"), |
| 83 | - ErrorKind::BlobIsBinary => write!(f, "blob is a binary file"), |
| 84 | - ErrorKind::ObjectNotATree => write!(f, "object is not a tree"), |
| 85 | - ErrorKind::NotAReference => write!(f, "reference is invalid"), |
| 86 | - ErrorKind::ConfigError(ref msg) => write!(f, "config is invalid: {msg}"), |
| 87 | + match self { |
| 88 | + Error::Git(ref err) => write!(f, "libgit operation failed: {err}"), |
| 89 | + Error::Io(ref err) => err.fmt(f), |
| 90 | + Error::BlobTooLarge => write!(f, "blob is too large"), |
| 91 | + Error::BlobIsBinary => write!(f, "blob is a binary file"), |
| 92 | + Error::ObjectNotATree => write!(f, "object is not a tree"), |
| 93 | + Error::NotAReference => write!(f, "reference is invalid"), |
| 94 | + Error::ConfigError(ref msg) => write!(f, "config is invalid: {msg}"), |
| 95 | + Error::CannotOpenRepository { path, git_error } => { |
| 96 | + write!(f, "could not open repository {path:?}: {git_error:?}") |
| 97 | + } |
| 98 | + Error::CannotCreateRepository { path, git_error } => write!( |
| 99 | + f, |
| 100 | + "could not create a new repository {path:?}: {git_error:?}" |
| 101 | + ), |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | - impl StdError for Error { |
| 107 | - fn source(&self) -> Option<&(dyn StdError + 'static)> { |
| 108 | - match self.kind { |
| 109 | - ErrorKind::Git(ref err) => Some(err), |
| 110 | - ErrorKind::Io(ref err) => Some(err), |
| 111 | - ErrorKind::BlobTooLarge => None, |
| 112 | - ErrorKind::BlobIsBinary => None, |
| 113 | - ErrorKind::ObjectNotATree => None, |
| 114 | - ErrorKind::NotAReference => None, |
| 115 | - ErrorKind::ConfigError(_) => None, |
| 116 | - } |
| 117 | - } |
| 118 | - } |
| 119 | + impl StdError for Error {} |
| 120 | diff --git a/crates/git/src/lib.rs b/crates/git/src/lib.rs |
| 121 | index fced42f..58b8240 100644 |
| 122 | --- a/crates/git/src/lib.rs |
| 123 | +++ b/crates/git/src/lib.rs |
| 124 | @@ -1,5 +1,5 @@ |
| 125 | pub use config::{ChatKind, ChatLink, Config, Sites}; |
| 126 | - pub use error::{Error, ErrorKind}; |
| 127 | + pub use error::Error; |
| 128 | pub use lite::{Blob, Branch, Commit, Kind, Note, Stats, Tag, TreeEntry}; |
| 129 | pub use scanner::{collection, collection_and_name, contains, git_dir, name, Scanner}; |
| 130 | pub use wrapper::{Selector, Wrapper}; |
| 131 | diff --git a/crates/git/src/wrapper.rs b/crates/git/src/wrapper.rs |
| 132 | index 184f647..d3d29e2 100644 |
| 133 | --- a/crates/git/src/wrapper.rs |
| 134 | +++ b/crates/git/src/wrapper.rs |
| 135 | @@ -14,7 +14,7 @@ use rand::{distr::Alphanumeric, Rng}; |
| 136 | use tracing::log; |
| 137 | |
| 138 | use crate::config; |
| 139 | - use crate::error::{Error, ErrorKind}; |
| 140 | + use crate::error::Error; |
| 141 | use crate::lite; |
| 142 | |
| 143 | // maximum blob size we are willing to load into memory |
| 144 | @@ -35,7 +35,10 @@ pub struct Wrapper { |
| 145 | |
| 146 | impl Wrapper { |
| 147 | pub fn new(path: &Path) -> Result<Self, Error> { |
| 148 | - let repository = Repository::open(path)?; |
| 149 | + let repository = Repository::open(path).map_err(|e| Error::CannotOpenRepository { |
| 150 | + path: path.to_path_buf(), |
| 151 | + git_error: e, |
| 152 | + })?; |
| 153 | Ok(Wrapper { |
| 154 | path: path.to_path_buf(), |
| 155 | repository: Box::new(repository), |
| 156 | @@ -45,7 +48,11 @@ impl Wrapper { |
| 157 | /// Create a new git repository at the given path |
| 158 | pub fn create(path: &Path, bare: bool) -> Result<Self, Error> { |
| 159 | let repository = |
| 160 | - Repository::init_opts(path, RepositoryInitOptions::new().bare(bare).mkdir(true))?; |
| 161 | + Repository::init_opts(path, RepositoryInitOptions::new().bare(bare).mkdir(true)) |
| 162 | + .map_err(|e| Error::CannotCreateRepository { |
| 163 | + path: path.to_path_buf(), |
| 164 | + git_error: e, |
| 165 | + })?; |
| 166 | Ok(Wrapper { |
| 167 | path: path.to_path_buf(), |
| 168 | repository: Box::new(repository), |
| 169 | @@ -522,7 +529,7 @@ impl Wrapper { |
| 170 | let object = result.to_object(&self.repository)?; |
| 171 | let blob = object.as_blob().unwrap(); |
| 172 | if blob.is_binary() { |
| 173 | - return Err(Error::kind(ErrorKind::BlobIsBinary)); |
| 174 | + return Err(Error::BlobIsBinary); |
| 175 | }; |
| 176 | let content = String::from_utf8(blob.content().to_vec()).unwrap(); |
| 177 | Ok(Some(content)) |
| 178 | @@ -557,7 +564,7 @@ impl Wrapper { |
| 179 | Some(ObjectType::Blob) => { |
| 180 | let blob = obj.as_blob().unwrap(); |
| 181 | if blob.size() >= MAX_BLOB_SIZE as usize { |
| 182 | - return Err(Error::kind(ErrorKind::BlobTooLarge)); |
| 183 | + return Err(Error::BlobTooLarge); |
| 184 | } |
| 185 | let mut blob = lite::Blob::from(blob); |
| 186 | blob.mode = r.filemode(); |
| 187 | @@ -865,7 +872,7 @@ impl Wrapper { |
| 188 | entries.push(entry) |
| 189 | } |
| 190 | } |
| 191 | - _ => return Err(Error::kind(ErrorKind::ObjectNotATree)), |
| 192 | + _ => return Err(Error::ObjectNotATree), |
| 193 | } |
| 194 | } |
| 195 | None => { |
| 196 | @@ -955,7 +962,7 @@ impl Wrapper { |
| 197 | Some(ReferenceType::Direct) => reference.is_tag() || reference.is_branch(), |
| 198 | _ => false, |
| 199 | }) { |
| 200 | - return Err(Error::kind(ErrorKind::NotAReference)); |
| 201 | + return Err(Error::NotAReference); |
| 202 | } |
| 203 | } |
| 204 |