| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | use std::{path::PathBuf, pin::Pin}; |
| 7 | |
| 8 | use tokio::io::AsyncRead; |
| 9 | |
| 10 | use crate::domain::{ |
| 11 | CommitSummary, GitRef, ObjectId, OrgName, PasswordHash, RefName, RepoName, RepoPath, TreeEntry, |
| 12 | }; |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | pub trait PasswordHasher: Send + Sync { |
| 20 | |
| 21 | fn hash(&self, plaintext: &str) -> Result<PasswordHash, PasswordError>; |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | fn verify(&self, plaintext: &str, hash: &PasswordHash) -> Result<bool, PasswordError>; |
| 29 | } |
| 30 | |
| 31 | |
| 32 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 33 | pub struct PasswordError(String); |
| 34 | |
| 35 | impl PasswordError { |
| 36 | pub fn new(message: impl Into<String>) -> Self { |
| 37 | Self(message.into()) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | impl std::fmt::Display for PasswordError { |
| 42 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 43 | write!(f, "password hashing failed: {}", self.0) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | impl std::error::Error for PasswordError {} |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | pub trait GitStorage: Send + Sync { |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | fn init_bare( |
| 69 | &self, |
| 70 | handle: &OrgName, |
| 71 | name: &RepoName, |
| 72 | ) -> impl Future<Output = Result<(), GitStorageError>> + Send; |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | fn remove( |
| 80 | &self, |
| 81 | handle: &OrgName, |
| 82 | name: &RepoName, |
| 83 | ) -> impl Future<Output = Result<(), GitStorageError>> + Send; |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | fn repo_path(&self, handle: &OrgName, name: &RepoName) -> PathBuf; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | #[derive(Debug)] |
| 94 | pub enum GitStorageError { |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | AlreadyExists, |
| 101 | |
| 102 | Backend(Box<dyn std::error::Error + Send + Sync>), |
| 103 | } |
| 104 | |
| 105 | impl GitStorageError { |
| 106 | pub fn backend(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self { |
| 107 | Self::Backend(error.into()) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | impl std::fmt::Display for GitStorageError { |
| 112 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 113 | match self { |
| 114 | Self::AlreadyExists => f.write_str("a repository already exists at that path"), |
| 115 | Self::Backend(error) => write!(f, "git storage failure: {error}"), |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | impl std::error::Error for GitStorageError { |
| 121 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 122 | match self { |
| 123 | Self::AlreadyExists => None, |
| 124 | Self::Backend(error) => Some(&**error), |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | pub type ByteStream = Pin<Box<dyn AsyncRead + Send>>; |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 142 | pub enum GitMethod { |
| 143 | Get, |
| 144 | Post, |
| 145 | } |
| 146 | |
| 147 | impl GitMethod { |
| 148 | pub fn as_str(self) -> &'static str { |
| 149 | match self { |
| 150 | Self::Get => "GET", |
| 151 | Self::Post => "POST", |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | pub struct GitRequest { |
| 165 | pub method: GitMethod, |
| 166 | |
| 167 | pub path_info: String, |
| 168 | |
| 169 | pub query: String, |
| 170 | pub content_type: Option<String>, |
| 171 | |
| 172 | |
| 173 | |
| 174 | pub content_encoding: Option<String>, |
| 175 | pub content_length: Option<String>, |
| 176 | |
| 177 | |
| 178 | pub git_protocol: Option<String>, |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | pub allow_receive_pack: bool, |
| 187 | pub body: ByteStream, |
| 188 | } |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | pub struct GitResponse { |
| 196 | pub status: u16, |
| 197 | pub headers: Vec<(String, String)>, |
| 198 | pub body: ByteStream, |
| 199 | } |
| 200 | |
| 201 | |
| 202 | |
| 203 | impl std::fmt::Debug for GitRequest { |
| 204 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 205 | f.debug_struct("GitRequest") |
| 206 | .field("method", &self.method) |
| 207 | .field("path_info", &self.path_info) |
| 208 | .field("query", &self.query) |
| 209 | .field("content_type", &self.content_type) |
| 210 | .field("content_encoding", &self.content_encoding) |
| 211 | .field("content_length", &self.content_length) |
| 212 | .field("git_protocol", &self.git_protocol) |
| 213 | .field("allow_receive_pack", &self.allow_receive_pack) |
| 214 | .finish_non_exhaustive() |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | impl std::fmt::Debug for GitResponse { |
| 219 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 220 | f.debug_struct("GitResponse") |
| 221 | .field("status", &self.status) |
| 222 | .field("headers", &self.headers) |
| 223 | .finish_non_exhaustive() |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | pub trait GitProtocolServer: Send + Sync { |
| 232 | fn serve( |
| 233 | &self, |
| 234 | request: GitRequest, |
| 235 | ) -> impl Future<Output = Result<GitResponse, GitProtocolError>> + Send; |
| 236 | } |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | #[derive(Debug)] |
| 244 | pub struct GitProtocolError(Box<dyn std::error::Error + Send + Sync>); |
| 245 | |
| 246 | impl GitProtocolError { |
| 247 | pub fn new(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self { |
| 248 | Self(error.into()) |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | impl std::fmt::Display for GitProtocolError { |
| 253 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 254 | write!(f, "git protocol failure: {}", self.0) |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | impl std::error::Error for GitProtocolError { |
| 259 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 260 | Some(&*self.0) |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 270 | pub struct Blob { |
| 271 | pub id: ObjectId, |
| 272 | pub size: u64, |
| 273 | pub content: Option<Vec<u8>>, |
| 274 | } |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | pub trait GitQuery: Send + Sync { |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | fn default_branch( |
| 297 | &self, |
| 298 | handle: &OrgName, |
| 299 | name: &RepoName, |
| 300 | ) -> impl Future<Output = Result<Option<RefName>, GitQueryError>> + Send; |
| 301 | |
| 302 | |
| 303 | fn resolve( |
| 304 | &self, |
| 305 | handle: &OrgName, |
| 306 | name: &RepoName, |
| 307 | rev: &RefName, |
| 308 | ) -> impl Future<Output = Result<Option<ObjectId>, GitQueryError>> + Send; |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | fn list_tree( |
| 315 | &self, |
| 316 | handle: &OrgName, |
| 317 | name: &RepoName, |
| 318 | rev: &RefName, |
| 319 | path: &RepoPath, |
| 320 | ) -> impl Future<Output = Result<Option<Vec<TreeEntry>>, GitQueryError>> + Send; |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | fn read_blob( |
| 326 | &self, |
| 327 | handle: &OrgName, |
| 328 | name: &RepoName, |
| 329 | rev: &RefName, |
| 330 | path: &RepoPath, |
| 331 | max_bytes: u64, |
| 332 | ) -> impl Future<Output = Result<Option<Blob>, GitQueryError>> + Send; |
| 333 | |
| 334 | |
| 335 | fn log( |
| 336 | &self, |
| 337 | handle: &OrgName, |
| 338 | name: &RepoName, |
| 339 | rev: &RefName, |
| 340 | limit: usize, |
| 341 | ) -> impl Future<Output = Result<Vec<CommitSummary>, GitQueryError>> + Send; |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | fn list_refs( |
| 358 | &self, |
| 359 | handle: &OrgName, |
| 360 | name: &RepoName, |
| 361 | ) -> impl Future<Output = Result<Vec<GitRef>, GitQueryError>> + Send; |
| 362 | } |
| 363 | |
| 364 | |
| 365 | #[derive(Debug)] |
| 366 | pub struct GitQueryError(Box<dyn std::error::Error + Send + Sync>); |
| 367 | |
| 368 | impl GitQueryError { |
| 369 | pub fn new(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self { |
| 370 | Self(error.into()) |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | impl std::fmt::Display for GitQueryError { |
| 375 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 376 | write!(f, "could not read the repository: {}", self.0) |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | impl std::error::Error for GitQueryError { |
| 381 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 382 | Some(&*self.0) |
| 383 | } |
| 384 | } |