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::{ |
2268309feat: a commit is a page, and two revisions can be compared17h | 11 | BranchRow, CommitDetail, CommitSummary, DomainError, GitRef, GrepHit, ObjectId, OrgName, |
| 12 | PasswordHash, RefName, RepoName, RepoPath, 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 itself17h | 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 names17h | 391 | |
2268309feat: a commit is a page, and two revisions can be compared17h | 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | fn commit( |
| 403 | &self, |
| 404 | handle: &OrgName, |
| 405 | name: &RepoName, |
| 406 | rev: &RefName, |
| 407 | ) -> impl Future<Output = Result<Option<CommitDetail>, GitQueryError>> + Send; |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | fn diff( |
| 427 | &self, |
| 428 | handle: &OrgName, |
| 429 | name: &RepoName, |
| 430 | base: Option<&ObjectId>, |
| 431 | head: &ObjectId, |
| 432 | max_bytes: u64, |
| 433 | ) -> impl Future<Output = Result<RawDiff, GitQueryError>> + Send; |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | fn merge_base( |
| 442 | &self, |
| 443 | handle: &OrgName, |
| 444 | name: &RepoName, |
| 445 | base: &ObjectId, |
| 446 | head: &ObjectId, |
| 447 | ) -> impl Future<Output = Result<Option<ObjectId>, GitQueryError>> + Send; |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | fn log_between( |
| 458 | &self, |
| 459 | handle: &OrgName, |
| 460 | name: &RepoName, |
| 461 | base: Option<&ObjectId>, |
| 462 | head: &ObjectId, |
| 463 | limit: usize, |
| 464 | ) -> impl Future<Output = Result<Vec<CommitSummary>, GitQueryError>> + Send; |
| 465 | |
3bbcd9ffeat: a branch and a tag are rows, not just names17h | 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | fn branches( |
| 483 | &self, |
| 484 | handle: &OrgName, |
| 485 | name: &RepoName, |
| 486 | ) -> impl Future<Output = Result<Vec<BranchRow>, GitQueryError>> + Send; |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | fn tags( |
| 494 | &self, |
| 495 | handle: &OrgName, |
| 496 | name: &RepoName, |
| 497 | ) -> impl Future<Output = Result<Vec<TagRow>, GitQueryError>> + Send; |
1b7587dfeat: finding a string in a repository, and landing on the line17h | 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | fn grep( |
| 512 | &self, |
| 513 | handle: &OrgName, |
| 514 | name: &RepoName, |
| 515 | commit: &ObjectId, |
| 516 | query: &str, |
| 517 | limit: usize, |
| 518 | ) -> impl Future<Output = Result<Vec<GrepHit>, GitQueryError>> + Send; |
dce0bf3feat: browse a repository's files and history8d | 519 | } |
2268309feat: a commit is a page, and two revisions can be compared17h | 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | #[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 531 | pub struct RawDiff { |
| 532 | |
| 533 | |
| 534 | pub numstat: Vec<u8>, |
| 535 | |
| 536 | |
| 537 | pub patch: Vec<u8>, |
| 538 | |
| 539 | pub truncated: bool, |
| 540 | } |
dce0bf3feat: browse a repository's files and history8d | 541 | |
| 542 | #[derive(Debug)] |
c974d80feat: every read-side git process is bounded, and a timeout is not a fault17h | 543 | pub struct GitQueryError { |
| 544 | kind: GitQueryErrorKind, |
| 545 | source: Box<dyn std::error::Error + Send + Sync>, |
| 546 | } |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 554 | pub enum GitQueryErrorKind { |
| 555 | Failed, |
| 556 | TimedOut, |
| 557 | } |
dce0bf3feat: browse a repository's files and history8d | 558 | |
| 559 | impl GitQueryError { |
| 560 | 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 fault17h | 561 | Self { |
| 562 | kind: GitQueryErrorKind::Failed, |
| 563 | source: error.into(), |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | |
| 568 | pub fn timed_out(limit: std::time::Duration) -> Self { |
| 569 | Self { |
| 570 | kind: GitQueryErrorKind::TimedOut, |
| 571 | source: format!("git did not finish within {limit:?}").into(), |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | pub fn kind(&self) -> GitQueryErrorKind { |
| 576 | self.kind |
| 577 | } |
| 578 | |
| 579 | pub fn is_timeout(&self) -> bool { |
| 580 | self.kind == GitQueryErrorKind::TimedOut |
dce0bf3feat: browse a repository's files and history8d | 581 | } |
| 582 | } |
| 583 | |
| 584 | impl std::fmt::Display for GitQueryError { |
| 585 | 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 fault17h | 586 | match self.kind { |
| 587 | GitQueryErrorKind::Failed => { |
| 588 | write!(f, "could not read the repository: {}", self.source) |
| 589 | } |
| 590 | GitQueryErrorKind::TimedOut => { |
| 591 | write!(f, "reading the repository took too long: {}", self.source) |
| 592 | } |
| 593 | } |
dce0bf3feat: browse a repository's files and history8d | 594 | } |
| 595 | } |
| 596 | |
| 597 | impl std::error::Error for GitQueryError { |
| 598 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
c974d80feat: every read-side git process is bounded, and a timeout is not a fault17h | 599 | Some(&*self.source) |
dce0bf3feat: browse a repository's files and history8d | 600 | } |
| 601 | } |
b7a57dbfeat: a repository can be taken away as a file17h | 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 609 | pub enum ArchiveFormat { |
| 610 | TarGz, |
| 611 | Zip, |
| 612 | } |
| 613 | |
| 614 | impl ArchiveFormat { |
| 615 | |
| 616 | |
| 617 | pub fn as_str(self) -> &'static str { |
| 618 | match self { |
| 619 | Self::TarGz => "tar.gz", |
| 620 | Self::Zip => "zip", |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | pub fn content_type(self) -> &'static str { |
| 630 | match self { |
| 631 | Self::TarGz => "application/gzip", |
| 632 | Self::Zip => "application/zip", |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | |
| 638 | |
| 639 | impl std::str::FromStr for ArchiveFormat { |
| 640 | type Err = DomainError; |
| 641 | |
| 642 | fn from_str(value: &str) -> std::result::Result<Self, Self::Err> { |
| 643 | match value { |
| 644 | "tar.gz" => Ok(Self::TarGz), |
| 645 | "zip" => Ok(Self::Zip), |
| 646 | other => Err(DomainError::validation( |
| 647 | "format", |
| 648 | format!("unknown archive format {other:?}"), |
| 649 | )), |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 661 | pub struct ArchiveRequest { |
| 662 | pub handle: OrgName, |
| 663 | pub name: RepoName, |
| 664 | pub format: ArchiveFormat, |
| 665 | pub commit: ObjectId, |
| 666 | |
| 667 | |
| 668 | pub prefix: String, |
| 669 | } |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | pub trait GitArchive: Send + Sync { |
| 678 | fn archive( |
| 679 | &self, |
| 680 | request: ArchiveRequest, |
| 681 | ) -> impl Future<Output = Result<ByteStream, GitArchiveError>> + Send; |
| 682 | } |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | #[derive(Debug)] |
| 689 | pub struct GitArchiveError(Box<dyn std::error::Error + Send + Sync>); |
| 690 | |
| 691 | impl GitArchiveError { |
| 692 | pub fn new(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self { |
| 693 | Self(error.into()) |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | impl std::fmt::Display for GitArchiveError { |
| 698 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 699 | write!(f, "could not archive the repository: {}", self.0) |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | impl std::error::Error for GitArchiveError { |
| 704 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 705 | Some(&*self.0) |
| 706 | } |
| 707 | } |