| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | use std::{path::PathBuf, pin::Pin}; |
| 7 | |
| 8 | use tokio::io::AsyncRead; |
| 9 | |
| 10 | use crate::domain::{ |
| 11 | BranchRow, CommitDetail, CommitSummary, DomainError, GitRef, GrepHit, ImportSource, ObjectId, |
| 12 | OrgName, PasswordHash, RefName, RepoName, RepoPath, TagRow, TagSummary, TreeEntry, |
| 13 | }; |
| 14 | |
| 15 | use super::blame::Blame; |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | pub trait PasswordHasher: Send + Sync { |
| 23 | |
| 24 | fn hash(&self, plaintext: &str) -> Result<PasswordHash, PasswordError>; |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | fn verify(&self, plaintext: &str, hash: &PasswordHash) -> Result<bool, PasswordError>; |
| 32 | } |
| 33 | |
| 34 | |
| 35 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 36 | pub struct PasswordError(String); |
| 37 | |
| 38 | impl PasswordError { |
| 39 | pub fn new(message: impl Into<String>) -> Self { |
| 40 | Self(message.into()) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | impl std::fmt::Display for PasswordError { |
| 45 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 46 | write!(f, "password hashing failed: {}", self.0) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | impl std::error::Error for PasswordError {} |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | pub trait GitStorage: Send + Sync { |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | fn init_bare( |
| 72 | &self, |
| 73 | handle: &OrgName, |
| 74 | name: &RepoName, |
| 75 | ) -> impl Future<Output = Result<(), GitStorageError>> + Send; |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | fn remove( |
| 83 | &self, |
| 84 | handle: &OrgName, |
| 85 | name: &RepoName, |
| 86 | ) -> impl Future<Output = Result<(), GitStorageError>> + Send; |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | fn rename( |
| 99 | &self, |
| 100 | handle: &OrgName, |
| 101 | from: &RepoName, |
| 102 | to: &RepoName, |
| 103 | ) -> impl Future<Output = Result<(), GitStorageError>> + Send; |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | fn set_default_branch( |
| 120 | &self, |
| 121 | handle: &OrgName, |
| 122 | name: &RepoName, |
| 123 | branch: &RefName, |
| 124 | ) -> impl Future<Output = Result<(), GitStorageError>> + Send; |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | fn enable_reflogs(&self) -> impl Future<Output = Result<u64, GitStorageError>> + Send; |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | fn repo_path(&self, handle: &OrgName, name: &RepoName) -> PathBuf; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | #[derive(Debug)] |
| 155 | pub enum GitStorageError { |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | AlreadyExists, |
| 162 | |
| 163 | Backend(Box<dyn std::error::Error + Send + Sync>), |
| 164 | } |
| 165 | |
| 166 | impl GitStorageError { |
| 167 | pub fn backend(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self { |
| 168 | Self::Backend(error.into()) |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | impl std::fmt::Display for GitStorageError { |
| 173 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 174 | match self { |
| 175 | Self::AlreadyExists => f.write_str("a repository already exists at that path"), |
| 176 | Self::Backend(error) => write!(f, "git storage failure: {error}"), |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | impl std::error::Error for GitStorageError { |
| 182 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 183 | match self { |
| 184 | Self::AlreadyExists => None, |
| 185 | Self::Backend(error) => Some(&**error), |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | pub type ByteStream = Pin<Box<dyn AsyncRead + Send>>; |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 203 | pub enum GitMethod { |
| 204 | Get, |
| 205 | Post, |
| 206 | } |
| 207 | |
| 208 | impl GitMethod { |
| 209 | pub fn as_str(self) -> &'static str { |
| 210 | match self { |
| 211 | Self::Get => "GET", |
| 212 | Self::Post => "POST", |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | pub struct GitRequest { |
| 226 | pub method: GitMethod, |
| 227 | |
| 228 | pub path_info: String, |
| 229 | |
| 230 | pub query: String, |
| 231 | pub content_type: Option<String>, |
| 232 | |
| 233 | |
| 234 | |
| 235 | pub content_encoding: Option<String>, |
| 236 | pub content_length: Option<String>, |
| 237 | |
| 238 | |
| 239 | pub git_protocol: Option<String>, |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | pub allow_receive_pack: bool, |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | pub remote_user: Option<String>, |
| 257 | pub body: ByteStream, |
| 258 | } |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | pub struct GitResponse { |
| 266 | pub status: u16, |
| 267 | pub headers: Vec<(String, String)>, |
| 268 | pub body: ByteStream, |
| 269 | } |
| 270 | |
| 271 | |
| 272 | |
| 273 | impl std::fmt::Debug for GitRequest { |
| 274 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 275 | f.debug_struct("GitRequest") |
| 276 | .field("method", &self.method) |
| 277 | .field("path_info", &self.path_info) |
| 278 | .field("query", &self.query) |
| 279 | .field("content_type", &self.content_type) |
| 280 | .field("content_encoding", &self.content_encoding) |
| 281 | .field("content_length", &self.content_length) |
| 282 | .field("git_protocol", &self.git_protocol) |
| 283 | .field("allow_receive_pack", &self.allow_receive_pack) |
| 284 | .field("remote_user", &self.remote_user) |
| 285 | .finish_non_exhaustive() |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | impl std::fmt::Debug for GitResponse { |
| 290 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 291 | f.debug_struct("GitResponse") |
| 292 | .field("status", &self.status) |
| 293 | .field("headers", &self.headers) |
| 294 | .finish_non_exhaustive() |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | pub trait GitProtocolServer: Send + Sync { |
| 303 | fn serve( |
| 304 | &self, |
| 305 | request: GitRequest, |
| 306 | ) -> impl Future<Output = Result<GitResponse, GitProtocolError>> + Send; |
| 307 | } |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | #[derive(Debug)] |
| 315 | pub struct GitProtocolError(Box<dyn std::error::Error + Send + Sync>); |
| 316 | |
| 317 | impl GitProtocolError { |
| 318 | pub fn new(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self { |
| 319 | Self(error.into()) |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | impl std::fmt::Display for GitProtocolError { |
| 324 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 325 | write!(f, "git protocol failure: {}", self.0) |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | impl std::error::Error for GitProtocolError { |
| 330 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 331 | Some(&*self.0) |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 342 | pub struct WebhookRequest { |
| 343 | pub url: String, |
| 344 | pub headers: Vec<(String, String)>, |
| 345 | pub body: Vec<u8>, |
| 346 | } |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 354 | pub struct WebhookOutcome { |
| 355 | |
| 356 | |
| 357 | pub status: Option<u16>, |
| 358 | |
| 359 | |
| 360 | pub body: String, |
| 361 | pub duration: std::time::Duration, |
| 362 | } |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | pub trait WebhookSender: Send + Sync { |
| 370 | fn send(&self, request: WebhookRequest) -> impl Future<Output = WebhookOutcome> + Send; |
| 371 | } |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 379 | pub struct Blob { |
| 380 | pub id: ObjectId, |
| 381 | pub size: u64, |
| 382 | pub content: Option<Vec<u8>>, |
| 383 | } |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | pub trait GitQuery: Send + Sync { |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | fn default_branch( |
| 406 | &self, |
| 407 | handle: &OrgName, |
| 408 | name: &RepoName, |
| 409 | ) -> impl Future<Output = Result<Option<RefName>, GitQueryError>> + Send; |
| 410 | |
| 411 | |
| 412 | fn resolve( |
| 413 | &self, |
| 414 | handle: &OrgName, |
| 415 | name: &RepoName, |
| 416 | rev: &RefName, |
| 417 | ) -> impl Future<Output = Result<Option<ObjectId>, GitQueryError>> + Send; |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | fn list_tree( |
| 424 | &self, |
| 425 | handle: &OrgName, |
| 426 | name: &RepoName, |
| 427 | rev: &RefName, |
| 428 | path: &RepoPath, |
| 429 | ) -> impl Future<Output = Result<Option<Vec<TreeEntry>>, GitQueryError>> + Send; |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | fn read_blob( |
| 435 | &self, |
| 436 | handle: &OrgName, |
| 437 | name: &RepoName, |
| 438 | rev: &RefName, |
| 439 | path: &RepoPath, |
| 440 | max_bytes: u64, |
| 441 | ) -> impl Future<Output = Result<Option<Blob>, GitQueryError>> + Send; |
| 442 | |
| 443 | |
| 444 | fn log( |
| 445 | &self, |
| 446 | handle: &OrgName, |
| 447 | name: &RepoName, |
| 448 | rev: &RefName, |
| 449 | limit: usize, |
| 450 | ) -> impl Future<Output = Result<Vec<CommitSummary>, GitQueryError>> + Send; |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | fn list_refs( |
| 467 | &self, |
| 468 | handle: &OrgName, |
| 469 | name: &RepoName, |
| 470 | ) -> impl Future<Output = Result<Vec<GitRef>, GitQueryError>> + Send; |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | fn count_commits( |
| 479 | &self, |
| 480 | handle: &OrgName, |
| 481 | name: &RepoName, |
| 482 | rev: &RefName, |
| 483 | ) -> impl Future<Output = Result<u64, GitQueryError>> + Send; |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | fn latest_tag( |
| 495 | &self, |
| 496 | handle: &OrgName, |
| 497 | name: &RepoName, |
| 498 | ) -> impl Future<Output = Result<Option<TagSummary>, GitQueryError>> + Send; |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | fn commit( |
| 511 | &self, |
| 512 | handle: &OrgName, |
| 513 | name: &RepoName, |
| 514 | rev: &RefName, |
| 515 | ) -> impl Future<Output = Result<Option<CommitDetail>, GitQueryError>> + Send; |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | fn diff( |
| 535 | &self, |
| 536 | handle: &OrgName, |
| 537 | name: &RepoName, |
| 538 | base: Option<&ObjectId>, |
| 539 | head: &ObjectId, |
| 540 | max_bytes: u64, |
| 541 | ) -> impl Future<Output = Result<RawDiff, GitQueryError>> + Send; |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | fn merge_base( |
| 550 | &self, |
| 551 | handle: &OrgName, |
| 552 | name: &RepoName, |
| 553 | base: &ObjectId, |
| 554 | head: &ObjectId, |
| 555 | ) -> impl Future<Output = Result<Option<ObjectId>, GitQueryError>> + Send; |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | fn log_between( |
| 566 | &self, |
| 567 | handle: &OrgName, |
| 568 | name: &RepoName, |
| 569 | base: Option<&ObjectId>, |
| 570 | head: &ObjectId, |
| 571 | limit: usize, |
| 572 | ) -> impl Future<Output = Result<Vec<CommitSummary>, GitQueryError>> + Send; |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | fn branches( |
| 591 | &self, |
| 592 | handle: &OrgName, |
| 593 | name: &RepoName, |
| 594 | ) -> impl Future<Output = Result<Vec<BranchRow>, GitQueryError>> + Send; |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | fn tags( |
| 602 | &self, |
| 603 | handle: &OrgName, |
| 604 | name: &RepoName, |
| 605 | ) -> impl Future<Output = Result<Vec<TagRow>, GitQueryError>> + Send; |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | fn grep( |
| 620 | &self, |
| 621 | handle: &OrgName, |
| 622 | name: &RepoName, |
| 623 | commit: &ObjectId, |
| 624 | query: &str, |
| 625 | limit: usize, |
| 626 | ) -> impl Future<Output = Result<Vec<GrepHit>, GitQueryError>> + Send; |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | fn blame( |
| 643 | &self, |
| 644 | handle: &OrgName, |
| 645 | name: &RepoName, |
| 646 | rev: &RefName, |
| 647 | path: &RepoPath, |
| 648 | ) -> impl Future<Output = Result<Option<Blame>, GitQueryError>> + Send; |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | fn ref_snapshot( |
| 670 | &self, |
| 671 | handle: &OrgName, |
| 672 | name: &RepoName, |
| 673 | ) -> impl Future<Output = Result<Vec<(RefName, ObjectId)>, GitQueryError>> + Send; |
| 674 | } |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | #[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 686 | pub struct RawDiff { |
| 687 | |
| 688 | |
| 689 | pub numstat: Vec<u8>, |
| 690 | |
| 691 | |
| 692 | pub patch: Vec<u8>, |
| 693 | |
| 694 | pub truncated: bool, |
| 695 | } |
| 696 | |
| 697 | #[derive(Debug)] |
| 698 | pub struct GitQueryError { |
| 699 | kind: GitQueryErrorKind, |
| 700 | source: Box<dyn std::error::Error + Send + Sync>, |
| 701 | } |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 709 | pub enum GitQueryErrorKind { |
| 710 | Failed, |
| 711 | TimedOut, |
| 712 | } |
| 713 | |
| 714 | impl GitQueryError { |
| 715 | pub fn new(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self { |
| 716 | Self { |
| 717 | kind: GitQueryErrorKind::Failed, |
| 718 | source: error.into(), |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | |
| 723 | pub fn timed_out(limit: std::time::Duration) -> Self { |
| 724 | Self { |
| 725 | kind: GitQueryErrorKind::TimedOut, |
| 726 | source: format!("git did not finish within {limit:?}").into(), |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | pub fn kind(&self) -> GitQueryErrorKind { |
| 731 | self.kind |
| 732 | } |
| 733 | |
| 734 | pub fn is_timeout(&self) -> bool { |
| 735 | self.kind == GitQueryErrorKind::TimedOut |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | impl std::fmt::Display for GitQueryError { |
| 740 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 741 | match self.kind { |
| 742 | GitQueryErrorKind::Failed => { |
| 743 | write!(f, "could not read the repository: {}", self.source) |
| 744 | } |
| 745 | GitQueryErrorKind::TimedOut => { |
| 746 | write!(f, "reading the repository took too long: {}", self.source) |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | impl std::error::Error for GitQueryError { |
| 753 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 754 | Some(&*self.source) |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | |
| 763 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 764 | pub enum ArchiveFormat { |
| 765 | TarGz, |
| 766 | Zip, |
| 767 | } |
| 768 | |
| 769 | impl ArchiveFormat { |
| 770 | |
| 771 | |
| 772 | pub fn as_str(self) -> &'static str { |
| 773 | match self { |
| 774 | Self::TarGz => "tar.gz", |
| 775 | Self::Zip => "zip", |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | pub fn content_type(self) -> &'static str { |
| 785 | match self { |
| 786 | Self::TarGz => "application/gzip", |
| 787 | Self::Zip => "application/zip", |
| 788 | } |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | |
| 793 | |
| 794 | impl std::str::FromStr for ArchiveFormat { |
| 795 | type Err = DomainError; |
| 796 | |
| 797 | fn from_str(value: &str) -> std::result::Result<Self, Self::Err> { |
| 798 | match value { |
| 799 | "tar.gz" => Ok(Self::TarGz), |
| 800 | "zip" => Ok(Self::Zip), |
| 801 | other => Err(DomainError::validation( |
| 802 | "format", |
| 803 | format!("unknown archive format {other:?}"), |
| 804 | )), |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | |
| 813 | |
| 814 | |
| 815 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 816 | pub struct ArchiveRequest { |
| 817 | pub handle: OrgName, |
| 818 | pub name: RepoName, |
| 819 | pub format: ArchiveFormat, |
| 820 | pub commit: ObjectId, |
| 821 | |
| 822 | |
| 823 | pub prefix: String, |
| 824 | } |
| 825 | |
| 826 | |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | |
| 832 | pub trait GitArchive: Send + Sync { |
| 833 | fn archive( |
| 834 | &self, |
| 835 | request: ArchiveRequest, |
| 836 | ) -> impl Future<Output = Result<ByteStream, GitArchiveError>> + Send; |
| 837 | } |
| 838 | |
| 839 | |
| 840 | |
| 841 | |
| 842 | |
| 843 | #[derive(Debug)] |
| 844 | pub struct GitArchiveError(Box<dyn std::error::Error + Send + Sync>); |
| 845 | |
| 846 | impl GitArchiveError { |
| 847 | pub fn new(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self { |
| 848 | Self(error.into()) |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | impl std::fmt::Display for GitArchiveError { |
| 853 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 854 | write!(f, "could not archive the repository: {}", self.0) |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | impl std::error::Error for GitArchiveError { |
| 859 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 860 | Some(&*self.0) |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | |
| 865 | |
| 866 | |
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | |
| 873 | |
| 874 | |
| 875 | pub trait GitImport: Send + Sync { |
| 876 | |
| 877 | |
| 878 | |
| 879 | |
| 880 | |
| 881 | |
| 882 | fn mirror( |
| 883 | &self, |
| 884 | handle: &OrgName, |
| 885 | name: &RepoName, |
| 886 | source: &ImportSource, |
| 887 | ) -> impl Future<Output = Result<(), GitImportError>> + Send; |
| 888 | } |
| 889 | |
| 890 | |
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | |
| 896 | #[derive(Debug)] |
| 897 | pub struct GitImportError(String); |
| 898 | |
| 899 | impl GitImportError { |
| 900 | pub fn new(message: impl Into<String>) -> Self { |
| 901 | Self(message.into()) |
| 902 | } |
| 903 | |
| 904 | |
| 905 | |
| 906 | pub fn message(&self) -> &str { |
| 907 | &self.0 |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | impl std::fmt::Display for GitImportError { |
| 912 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 913 | write!(f, "could not import the repository: {}", self.0) |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | impl std::error::Error for GitImportError {} |