26d9828feat: password hashing behind a port1mo | 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
47db238feat: serve the git protocol through http-backend, behind a port8d | 6 | use std::{path::PathBuf, pin::Pin}; |
| 7 | |
| 8 | use tokio::io::AsyncRead; |
02eb2e4feat: GitStorage port and DiskGitStorage24d | 9 | |
dce0bf3feat: browse a repository's files and history8d | 10 | use crate::domain::{ |
3bbcd9ffeat: a branch and a tag are rows, not just names19h | 11 | BranchRow, CommitSummary, GitRef, ObjectId, OrgName, PasswordHash, RefName, RepoName, RepoPath, |
| 12 | TagRow, TagSummary, TreeEntry, |
dce0bf3feat: browse a repository's files and history8d | 13 | }; |
26d9828feat: password hashing behind a port1mo | 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | pub trait PasswordHasher: Send + Sync { |
| 21 | |
| 22 | fn hash(&self, plaintext: &str) -> Result<PasswordHash, PasswordError>; |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | fn verify(&self, plaintext: &str, hash: &PasswordHash) -> Result<bool, PasswordError>; |
| 30 | } |
| 31 | |
| 32 | |
| 33 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 34 | pub struct PasswordError(String); |
| 35 | |
| 36 | impl PasswordError { |
| 37 | pub fn new(message: impl Into<String>) -> Self { |
| 38 | Self(message.into()) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | impl std::fmt::Display for PasswordError { |
| 43 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 44 | write!(f, "password hashing failed: {}", self.0) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | impl std::error::Error for PasswordError {} |
02eb2e4feat: GitStorage port and DiskGitStorage24d | 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | pub trait GitStorage: Send + Sync { |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | fn init_bare( |
| 70 | &self, |
| 71 | handle: &OrgName, |
| 72 | name: &RepoName, |
| 73 | ) -> impl Future<Output = Result<(), GitStorageError>> + Send; |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | fn remove( |
| 81 | &self, |
| 82 | handle: &OrgName, |
| 83 | name: &RepoName, |
| 84 | ) -> impl Future<Output = Result<(), GitStorageError>> + Send; |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | fn repo_path(&self, handle: &OrgName, name: &RepoName) -> PathBuf; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | #[derive(Debug)] |
| 95 | pub enum GitStorageError { |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | AlreadyExists, |
| 102 | |
| 103 | Backend(Box<dyn std::error::Error + Send + Sync>), |
| 104 | } |
| 105 | |
| 106 | impl GitStorageError { |
| 107 | pub fn backend(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self { |
| 108 | Self::Backend(error.into()) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | impl std::fmt::Display for GitStorageError { |
| 113 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 114 | match self { |
| 115 | Self::AlreadyExists => f.write_str("a repository already exists at that path"), |
| 116 | Self::Backend(error) => write!(f, "git storage failure: {error}"), |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | impl std::error::Error for GitStorageError { |
| 122 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 123 | match self { |
| 124 | Self::AlreadyExists => None, |
| 125 | Self::Backend(error) => Some(&**error), |
| 126 | } |
| 127 | } |
| 128 | } |
47db238feat: serve the git protocol through http-backend, behind a port8d | 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | pub type ByteStream = Pin<Box<dyn AsyncRead + Send>>; |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 143 | pub enum GitMethod { |
| 144 | Get, |
| 145 | Post, |
| 146 | } |
| 147 | |
| 148 | impl GitMethod { |
| 149 | pub fn as_str(self) -> &'static str { |
| 150 | match self { |
| 151 | Self::Get => "GET", |
| 152 | Self::Post => "POST", |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | pub struct GitRequest { |
| 166 | pub method: GitMethod, |
| 167 | |
| 168 | pub path_info: String, |
| 169 | |
| 170 | pub query: String, |
| 171 | pub content_type: Option<String>, |
| 172 | |
| 173 | |
| 174 | |
| 175 | pub content_encoding: Option<String>, |
| 176 | pub content_length: Option<String>, |
| 177 | |
| 178 | |
| 179 | pub git_protocol: Option<String>, |
a814db5feat: push and clone private repositories with a token8d | 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | pub allow_receive_pack: bool, |
47db238feat: serve the git protocol through http-backend, behind a port8d | 188 | pub body: ByteStream, |
| 189 | } |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | pub struct GitResponse { |
| 197 | pub status: u16, |
| 198 | pub headers: Vec<(String, String)>, |
| 199 | pub body: ByteStream, |
| 200 | } |
| 201 | |
8ac00bdfeat: decide who may reach the git protocol8d | 202 | |
| 203 | |
| 204 | impl std::fmt::Debug for GitRequest { |
| 205 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 206 | f.debug_struct("GitRequest") |
| 207 | .field("method", &self.method) |
| 208 | .field("path_info", &self.path_info) |
| 209 | .field("query", &self.query) |
| 210 | .field("content_type", &self.content_type) |
| 211 | .field("content_encoding", &self.content_encoding) |
| 212 | .field("content_length", &self.content_length) |
| 213 | .field("git_protocol", &self.git_protocol) |
a814db5feat: push and clone private repositories with a token8d | 214 | .field("allow_receive_pack", &self.allow_receive_pack) |
8ac00bdfeat: decide who may reach the git protocol8d | 215 | .finish_non_exhaustive() |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | impl std::fmt::Debug for GitResponse { |
| 220 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 221 | f.debug_struct("GitResponse") |
| 222 | .field("status", &self.status) |
| 223 | .field("headers", &self.headers) |
| 224 | .finish_non_exhaustive() |
| 225 | } |
| 226 | } |
| 227 | |
47db238feat: serve the git protocol through http-backend, behind a port8d | 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | pub trait GitProtocolServer: Send + Sync { |
| 233 | fn serve( |
| 234 | &self, |
| 235 | request: GitRequest, |
| 236 | ) -> impl Future<Output = Result<GitResponse, GitProtocolError>> + Send; |
| 237 | } |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | #[derive(Debug)] |
| 245 | pub struct GitProtocolError(Box<dyn std::error::Error + Send + Sync>); |
| 246 | |
| 247 | impl GitProtocolError { |
| 248 | pub fn new(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self { |
| 249 | Self(error.into()) |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | impl std::fmt::Display for GitProtocolError { |
| 254 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 255 | write!(f, "git protocol failure: {}", self.0) |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | impl std::error::Error for GitProtocolError { |
| 260 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 261 | Some(&*self.0) |
| 262 | } |
| 263 | } |
dce0bf3feat: browse a repository's files and history8d | 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 271 | pub struct Blob { |
| 272 | pub id: ObjectId, |
| 273 | pub size: u64, |
| 274 | pub content: Option<Vec<u8>>, |
| 275 | } |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | pub trait GitQuery: Send + Sync { |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | fn default_branch( |
| 298 | &self, |
| 299 | handle: &OrgName, |
| 300 | name: &RepoName, |
| 301 | ) -> impl Future<Output = Result<Option<RefName>, GitQueryError>> + Send; |
| 302 | |
| 303 | |
| 304 | fn resolve( |
| 305 | &self, |
| 306 | handle: &OrgName, |
| 307 | name: &RepoName, |
| 308 | rev: &RefName, |
| 309 | ) -> impl Future<Output = Result<Option<ObjectId>, GitQueryError>> + Send; |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | fn list_tree( |
| 316 | &self, |
| 317 | handle: &OrgName, |
| 318 | name: &RepoName, |
| 319 | rev: &RefName, |
| 320 | path: &RepoPath, |
| 321 | ) -> impl Future<Output = Result<Option<Vec<TreeEntry>>, GitQueryError>> + Send; |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | fn read_blob( |
| 327 | &self, |
| 328 | handle: &OrgName, |
| 329 | name: &RepoName, |
| 330 | rev: &RefName, |
| 331 | path: &RepoPath, |
| 332 | max_bytes: u64, |
| 333 | ) -> impl Future<Output = Result<Option<Blob>, GitQueryError>> + Send; |
| 334 | |
| 335 | |
| 336 | fn log( |
| 337 | &self, |
| 338 | handle: &OrgName, |
| 339 | name: &RepoName, |
| 340 | rev: &RefName, |
| 341 | limit: usize, |
| 342 | ) -> impl Future<Output = Result<Vec<CommitSummary>, GitQueryError>> + Send; |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | fn list_refs( |
| 359 | &self, |
| 360 | handle: &OrgName, |
| 361 | name: &RepoName, |
| 362 | ) -> impl Future<Output = Result<Vec<GitRef>, GitQueryError>> + Send; |
dd5b600feat: the facts a repository page states about itself20h | 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | fn count_commits( |
| 371 | &self, |
| 372 | handle: &OrgName, |
| 373 | name: &RepoName, |
| 374 | rev: &RefName, |
| 375 | ) -> impl Future<Output = Result<u64, GitQueryError>> + Send; |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | fn latest_tag( |
| 387 | &self, |
| 388 | handle: &OrgName, |
| 389 | name: &RepoName, |
| 390 | ) -> impl Future<Output = Result<Option<TagSummary>, GitQueryError>> + Send; |
3bbcd9ffeat: a branch and a tag are rows, not just names19h | 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | fn branches( |
| 409 | &self, |
| 410 | handle: &OrgName, |
| 411 | name: &RepoName, |
| 412 | ) -> impl Future<Output = Result<Vec<BranchRow>, GitQueryError>> + Send; |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | fn tags( |
| 420 | &self, |
| 421 | handle: &OrgName, |
| 422 | name: &RepoName, |
| 423 | ) -> impl Future<Output = Result<Vec<TagRow>, GitQueryError>> + Send; |
dce0bf3feat: browse a repository's files and history8d | 424 | } |
| 425 | |
| 426 | |
| 427 | #[derive(Debug)] |
c974d80feat: every read-side git process is bounded, and a timeout is not a fault20h | 428 | pub struct GitQueryError { |
| 429 | kind: GitQueryErrorKind, |
| 430 | source: Box<dyn std::error::Error + Send + Sync>, |
| 431 | } |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 439 | pub enum GitQueryErrorKind { |
| 440 | Failed, |
| 441 | TimedOut, |
| 442 | } |
dce0bf3feat: browse a repository's files and history8d | 443 | |
| 444 | impl GitQueryError { |
| 445 | pub fn new(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self { |
c974d80feat: every read-side git process is bounded, and a timeout is not a fault20h | 446 | Self { |
| 447 | kind: GitQueryErrorKind::Failed, |
| 448 | source: error.into(), |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | |
| 453 | pub fn timed_out(limit: std::time::Duration) -> Self { |
| 454 | Self { |
| 455 | kind: GitQueryErrorKind::TimedOut, |
| 456 | source: format!("git did not finish within {limit:?}").into(), |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | pub fn kind(&self) -> GitQueryErrorKind { |
| 461 | self.kind |
| 462 | } |
| 463 | |
| 464 | pub fn is_timeout(&self) -> bool { |
| 465 | self.kind == GitQueryErrorKind::TimedOut |
dce0bf3feat: browse a repository's files and history8d | 466 | } |
| 467 | } |
| 468 | |
| 469 | impl std::fmt::Display for GitQueryError { |
| 470 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
c974d80feat: every read-side git process is bounded, and a timeout is not a fault20h | 471 | match self.kind { |
| 472 | GitQueryErrorKind::Failed => { |
| 473 | write!(f, "could not read the repository: {}", self.source) |
| 474 | } |
| 475 | GitQueryErrorKind::TimedOut => { |
| 476 | write!(f, "reading the repository took too long: {}", self.source) |
| 477 | } |
| 478 | } |
dce0bf3feat: browse a repository's files and history8d | 479 | } |
| 480 | } |
| 481 | |
| 482 | impl std::error::Error for GitQueryError { |
| 483 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
c974d80feat: every read-side git process is bounded, and a timeout is not a fault20h | 484 | Some(&*self.source) |
dce0bf3feat: browse a repository's files and history8d | 485 | } |
| 486 | } |