| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | use std::{collections::HashMap, path::PathBuf, pin::Pin}; |
| 7 | |
| 8 | use tokio::io::AsyncRead; |
| 9 | |
| 10 | use crate::domain::{ |
| 11 | BranchRow, CommitDetail, CommitEntry, CommitSummary, DomainError, EntryCommit, GitRef, GrepHit, |
| 12 | ImportSource, ObjectId, OrgName, PasswordHash, RefName, RepoName, RepoPath, TagRow, TagSummary, |
| 13 | TreeEntry, |
| 14 | }; |
| 15 | |
| 16 | use super::blame::Blame; |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | pub trait PasswordHasher: Send + Sync { |
| 24 | |
| 25 | fn hash(&self, plaintext: &str) -> Result<PasswordHash, PasswordError>; |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | fn verify(&self, plaintext: &str, hash: &PasswordHash) -> Result<bool, PasswordError>; |
| 33 | } |
| 34 | |
| 35 | |
| 36 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 37 | pub struct PasswordError(String); |
| 38 | |
| 39 | impl PasswordError { |
| 40 | pub fn new(message: impl Into<String>) -> Self { |
| 41 | Self(message.into()) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | impl std::fmt::Display for PasswordError { |
| 46 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 47 | write!(f, "password hashing failed: {}", self.0) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | impl std::error::Error for PasswordError {} |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | pub trait GitStorage: Send + Sync { |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | fn init_bare( |
| 73 | &self, |
| 74 | handle: &OrgName, |
| 75 | name: &RepoName, |
| 76 | ) -> impl Future<Output = Result<(), GitStorageError>> + Send; |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | fn remove( |
| 84 | &self, |
| 85 | handle: &OrgName, |
| 86 | name: &RepoName, |
| 87 | ) -> impl Future<Output = Result<(), GitStorageError>> + Send; |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | fn rename( |
| 100 | &self, |
| 101 | handle: &OrgName, |
| 102 | from: &RepoName, |
| 103 | to: &RepoName, |
| 104 | ) -> impl Future<Output = Result<(), GitStorageError>> + Send; |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | fn set_default_branch( |
| 121 | &self, |
| 122 | handle: &OrgName, |
| 123 | name: &RepoName, |
| 124 | branch: &RefName, |
| 125 | ) -> impl Future<Output = Result<(), GitStorageError>> + Send; |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | fn enable_reflogs(&self) -> impl Future<Output = Result<u64, GitStorageError>> + Send; |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | fn list_repo_dirs(&self) -> impl Future<Output = Result<Vec<RepoDir>, GitStorageError>> + Send; |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | fn quarantine( |
| 169 | &self, |
| 170 | dir: &RepoDir, |
| 171 | label: &str, |
| 172 | ) -> impl Future<Output = Result<PathBuf, GitStorageError>> + Send; |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | fn repo_path(&self, handle: &OrgName, name: &RepoName) -> PathBuf; |
| 179 | } |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 187 | pub struct RepoDir { |
| 188 | |
| 189 | pub handle: String, |
| 190 | |
| 191 | pub name: String, |
| 192 | |
| 193 | pub path: PathBuf, |
| 194 | } |
| 195 | |
| 196 | |
| 197 | #[derive(Debug)] |
| 198 | pub enum GitStorageError { |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | AlreadyExists, |
| 205 | |
| 206 | Backend(Box<dyn std::error::Error + Send + Sync>), |
| 207 | } |
| 208 | |
| 209 | impl GitStorageError { |
| 210 | pub fn backend(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self { |
| 211 | Self::Backend(error.into()) |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | impl std::fmt::Display for GitStorageError { |
| 216 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 217 | match self { |
| 218 | Self::AlreadyExists => f.write_str("a repository already exists at that path"), |
| 219 | Self::Backend(error) => write!(f, "git storage failure: {error}"), |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | impl std::error::Error for GitStorageError { |
| 225 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 226 | match self { |
| 227 | Self::AlreadyExists => None, |
| 228 | Self::Backend(error) => Some(&**error), |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | pub type ByteStream = Pin<Box<dyn AsyncRead + Send>>; |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 246 | pub enum GitMethod { |
| 247 | Get, |
| 248 | Post, |
| 249 | } |
| 250 | |
| 251 | impl GitMethod { |
| 252 | pub fn as_str(self) -> &'static str { |
| 253 | match self { |
| 254 | Self::Get => "GET", |
| 255 | Self::Post => "POST", |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | pub struct GitRequest { |
| 269 | pub method: GitMethod, |
| 270 | |
| 271 | pub path_info: String, |
| 272 | |
| 273 | pub query: String, |
| 274 | pub content_type: Option<String>, |
| 275 | |
| 276 | |
| 277 | |
| 278 | pub content_encoding: Option<String>, |
| 279 | pub content_length: Option<String>, |
| 280 | |
| 281 | |
| 282 | pub git_protocol: Option<String>, |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | pub allow_receive_pack: bool, |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | pub remote_user: Option<String>, |
| 300 | pub body: ByteStream, |
| 301 | } |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | pub struct GitResponse { |
| 309 | pub status: u16, |
| 310 | pub headers: Vec<(String, String)>, |
| 311 | pub body: ByteStream, |
| 312 | } |
| 313 | |
| 314 | |
| 315 | |
| 316 | impl std::fmt::Debug for GitRequest { |
| 317 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 318 | f.debug_struct("GitRequest") |
| 319 | .field("method", &self.method) |
| 320 | .field("path_info", &self.path_info) |
| 321 | .field("query", &self.query) |
| 322 | .field("content_type", &self.content_type) |
| 323 | .field("content_encoding", &self.content_encoding) |
| 324 | .field("content_length", &self.content_length) |
| 325 | .field("git_protocol", &self.git_protocol) |
| 326 | .field("allow_receive_pack", &self.allow_receive_pack) |
| 327 | .field("remote_user", &self.remote_user) |
| 328 | .finish_non_exhaustive() |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | impl std::fmt::Debug for GitResponse { |
| 333 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 334 | f.debug_struct("GitResponse") |
| 335 | .field("status", &self.status) |
| 336 | .field("headers", &self.headers) |
| 337 | .finish_non_exhaustive() |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | pub trait GitProtocolServer: Send + Sync { |
| 346 | fn serve( |
| 347 | &self, |
| 348 | request: GitRequest, |
| 349 | ) -> impl Future<Output = Result<GitResponse, GitProtocolError>> + Send; |
| 350 | } |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | #[derive(Debug)] |
| 358 | pub struct GitProtocolError(Box<dyn std::error::Error + Send + Sync>); |
| 359 | |
| 360 | impl GitProtocolError { |
| 361 | pub fn new(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self { |
| 362 | Self(error.into()) |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | impl std::fmt::Display for GitProtocolError { |
| 367 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 368 | write!(f, "git protocol failure: {}", self.0) |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | impl std::error::Error for GitProtocolError { |
| 373 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 374 | Some(&*self.0) |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 385 | pub struct WebhookRequest { |
| 386 | pub url: String, |
| 387 | pub headers: Vec<(String, String)>, |
| 388 | pub body: Vec<u8>, |
| 389 | } |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 397 | pub struct WebhookOutcome { |
| 398 | |
| 399 | |
| 400 | pub status: Option<u16>, |
| 401 | |
| 402 | |
| 403 | pub body: String, |
| 404 | pub duration: std::time::Duration, |
| 405 | } |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | pub trait WebhookSender: Send + Sync { |
| 413 | fn send(&self, request: WebhookRequest) -> impl Future<Output = WebhookOutcome> + Send; |
| 414 | } |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | pub trait Clock: Send + Sync { |
| 428 | fn now(&self) -> std::time::SystemTime; |
| 429 | } |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 437 | pub struct Blob { |
| 438 | pub id: ObjectId, |
| 439 | pub size: u64, |
| 440 | pub content: Option<Vec<u8>>, |
| 441 | } |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | pub trait GitQuery: Send + Sync { |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | fn default_branch( |
| 464 | &self, |
| 465 | handle: &OrgName, |
| 466 | name: &RepoName, |
| 467 | ) -> impl Future<Output = Result<Option<RefName>, GitQueryError>> + Send; |
| 468 | |
| 469 | |
| 470 | fn resolve( |
| 471 | &self, |
| 472 | handle: &OrgName, |
| 473 | name: &RepoName, |
| 474 | rev: &RefName, |
| 475 | ) -> impl Future<Output = Result<Option<ObjectId>, GitQueryError>> + Send; |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | fn list_tree( |
| 482 | &self, |
| 483 | handle: &OrgName, |
| 484 | name: &RepoName, |
| 485 | rev: &RefName, |
| 486 | path: &RepoPath, |
| 487 | ) -> impl Future<Output = Result<Option<Vec<TreeEntry>>, GitQueryError>> + Send; |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | fn last_commits( |
| 507 | &self, |
| 508 | handle: &OrgName, |
| 509 | name: &RepoName, |
| 510 | rev: &RefName, |
| 511 | path: &RepoPath, |
| 512 | entries: &[String], |
| 513 | ) -> impl Future<Output = Result<HashMap<String, EntryCommit>, GitQueryError>> + Send; |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | fn read_blob( |
| 519 | &self, |
| 520 | handle: &OrgName, |
| 521 | name: &RepoName, |
| 522 | rev: &RefName, |
| 523 | path: &RepoPath, |
| 524 | max_bytes: u64, |
| 525 | ) -> impl Future<Output = Result<Option<Blob>, GitQueryError>> + Send; |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | fn log( |
| 539 | &self, |
| 540 | handle: &OrgName, |
| 541 | name: &RepoName, |
| 542 | rev: &RefName, |
| 543 | offset: usize, |
| 544 | limit: usize, |
| 545 | ) -> impl Future<Output = Result<Vec<CommitSummary>, GitQueryError>> + Send; |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | fn log_entries( |
| 554 | &self, |
| 555 | handle: &OrgName, |
| 556 | name: &RepoName, |
| 557 | rev: &RefName, |
| 558 | limit: usize, |
| 559 | ) -> impl Future<Output = Result<Vec<CommitEntry>, GitQueryError>> + Send; |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | fn list_refs( |
| 576 | &self, |
| 577 | handle: &OrgName, |
| 578 | name: &RepoName, |
| 579 | ) -> impl Future<Output = Result<Vec<GitRef>, GitQueryError>> + Send; |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | fn count_commits( |
| 588 | &self, |
| 589 | handle: &OrgName, |
| 590 | name: &RepoName, |
| 591 | rev: &RefName, |
| 592 | ) -> impl Future<Output = Result<u64, GitQueryError>> + Send; |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | fn latest_tag( |
| 604 | &self, |
| 605 | handle: &OrgName, |
| 606 | name: &RepoName, |
| 607 | ) -> impl Future<Output = Result<Option<TagSummary>, GitQueryError>> + Send; |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | fn commit( |
| 620 | &self, |
| 621 | handle: &OrgName, |
| 622 | name: &RepoName, |
| 623 | rev: &RefName, |
| 624 | ) -> impl Future<Output = Result<Option<CommitDetail>, GitQueryError>> + Send; |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | fn diff( |
| 644 | &self, |
| 645 | handle: &OrgName, |
| 646 | name: &RepoName, |
| 647 | base: Option<&ObjectId>, |
| 648 | head: &ObjectId, |
| 649 | max_bytes: u64, |
| 650 | ) -> impl Future<Output = Result<RawDiff, GitQueryError>> + Send; |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | fn merge_base( |
| 659 | &self, |
| 660 | handle: &OrgName, |
| 661 | name: &RepoName, |
| 662 | base: &ObjectId, |
| 663 | head: &ObjectId, |
| 664 | ) -> impl Future<Output = Result<Option<ObjectId>, GitQueryError>> + Send; |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | fn log_between( |
| 675 | &self, |
| 676 | handle: &OrgName, |
| 677 | name: &RepoName, |
| 678 | base: Option<&ObjectId>, |
| 679 | head: &ObjectId, |
| 680 | offset: usize, |
| 681 | limit: usize, |
| 682 | ) -> impl Future<Output = Result<Vec<CommitSummary>, GitQueryError>> + Send; |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | fn count_between( |
| 690 | &self, |
| 691 | handle: &OrgName, |
| 692 | name: &RepoName, |
| 693 | base: Option<&ObjectId>, |
| 694 | head: &ObjectId, |
| 695 | ) -> impl Future<Output = Result<usize, GitQueryError>> + Send; |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | fn branches( |
| 714 | &self, |
| 715 | handle: &OrgName, |
| 716 | name: &RepoName, |
| 717 | ) -> impl Future<Output = Result<Vec<BranchRow>, GitQueryError>> + Send; |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | fn tags( |
| 725 | &self, |
| 726 | handle: &OrgName, |
| 727 | name: &RepoName, |
| 728 | ) -> impl Future<Output = Result<Vec<TagRow>, GitQueryError>> + Send; |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | fn grep( |
| 743 | &self, |
| 744 | handle: &OrgName, |
| 745 | name: &RepoName, |
| 746 | commit: &ObjectId, |
| 747 | query: &str, |
| 748 | limit: usize, |
| 749 | ) -> impl Future<Output = Result<Vec<GrepHit>, GitQueryError>> + Send; |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | |
| 763 | |
| 764 | |
| 765 | fn blame( |
| 766 | &self, |
| 767 | handle: &OrgName, |
| 768 | name: &RepoName, |
| 769 | rev: &RefName, |
| 770 | path: &RepoPath, |
| 771 | ) -> impl Future<Output = Result<Option<Blame>, GitQueryError>> + Send; |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | |
| 791 | |
| 792 | fn ref_snapshot( |
| 793 | &self, |
| 794 | handle: &OrgName, |
| 795 | name: &RepoName, |
| 796 | ) -> impl Future<Output = Result<Vec<(RefName, ObjectId)>, GitQueryError>> + Send; |
| 797 | |
| 798 | |
| 799 | |
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | |
| 813 | |
| 814 | |
| 815 | |
| 816 | #[allow(clippy::too_many_arguments)] |
| 817 | fn log_at_path( |
| 818 | &self, |
| 819 | handle: &OrgName, |
| 820 | name: &RepoName, |
| 821 | rev: &RefName, |
| 822 | path: &RepoPath, |
| 823 | follow: bool, |
| 824 | offset: usize, |
| 825 | limit: usize, |
| 826 | ) -> impl Future<Output = Result<Vec<CommitSummary>, GitQueryError>> + Send; |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | |
| 832 | |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | |
| 838 | |
| 839 | |
| 840 | |
| 841 | |
| 842 | fn count_commits_at_path( |
| 843 | &self, |
| 844 | handle: &OrgName, |
| 845 | name: &RepoName, |
| 846 | rev: &RefName, |
| 847 | path: &RepoPath, |
| 848 | follow: bool, |
| 849 | ) -> impl Future<Output = Result<CommitCount, GitQueryError>> + Send; |
| 850 | |
| 851 | |
| 852 | |
| 853 | |
| 854 | |
| 855 | |
| 856 | |
| 857 | |
| 858 | |
| 859 | |
| 860 | |
| 861 | fn symlink_targets( |
| 862 | &self, |
| 863 | handle: &OrgName, |
| 864 | name: &RepoName, |
| 865 | ids: &[ObjectId], |
| 866 | ) -> impl Future<Output = Result<HashMap<ObjectId, String>, GitQueryError>> + Send; |
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | |
| 873 | |
| 874 | |
| 875 | |
| 876 | |
| 877 | |
| 878 | |
| 879 | |
| 880 | |
| 881 | |
| 882 | |
| 883 | |
| 884 | |
| 885 | |
| 886 | |
| 887 | |
| 888 | |
| 889 | |
| 890 | fn merge_preview( |
| 891 | &self, |
| 892 | handle: &OrgName, |
| 893 | name: &RepoName, |
| 894 | target: &ObjectId, |
| 895 | source: &ObjectId, |
| 896 | ) -> impl Future<Output = Result<MergePreview, GitQueryError>> + Send; |
| 897 | } |
| 898 | |
| 899 | |
| 900 | |
| 901 | |
| 902 | |
| 903 | |
| 904 | |
| 905 | |
| 906 | #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] |
| 907 | pub struct CommitCount { |
| 908 | |
| 909 | pub total: u64, |
| 910 | |
| 911 | pub capped: bool, |
| 912 | } |
| 913 | |
| 914 | |
| 915 | |
| 916 | |
| 917 | |
| 918 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 919 | pub enum MergePreview { |
| 920 | |
| 921 | Clean { tree: ObjectId }, |
| 922 | |
| 923 | |
| 924 | |
| 925 | Conflicted { paths: Vec<String> }, |
| 926 | } |
| 927 | |
| 928 | impl MergePreview { |
| 929 | pub fn is_clean(&self) -> bool { |
| 930 | matches!(self, Self::Clean { .. }) |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | |
| 935 | |
| 936 | |
| 937 | |
| 938 | |
| 939 | |
| 940 | |
| 941 | |
| 942 | |
| 943 | #[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 944 | pub struct RawDiff { |
| 945 | |
| 946 | |
| 947 | pub numstat: Vec<u8>, |
| 948 | |
| 949 | |
| 950 | pub patch: Vec<u8>, |
| 951 | |
| 952 | pub truncated: bool, |
| 953 | } |
| 954 | |
| 955 | #[derive(Debug)] |
| 956 | pub struct GitQueryError { |
| 957 | kind: GitQueryErrorKind, |
| 958 | source: Box<dyn std::error::Error + Send + Sync>, |
| 959 | } |
| 960 | |
| 961 | |
| 962 | |
| 963 | |
| 964 | |
| 965 | |
| 966 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 967 | pub enum GitQueryErrorKind { |
| 968 | Failed, |
| 969 | TimedOut, |
| 970 | } |
| 971 | |
| 972 | impl GitQueryError { |
| 973 | pub fn new(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self { |
| 974 | Self { |
| 975 | kind: GitQueryErrorKind::Failed, |
| 976 | source: error.into(), |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | |
| 981 | pub fn timed_out(limit: std::time::Duration) -> Self { |
| 982 | Self { |
| 983 | kind: GitQueryErrorKind::TimedOut, |
| 984 | source: format!("git did not finish within {limit:?}").into(), |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | pub fn kind(&self) -> GitQueryErrorKind { |
| 989 | self.kind |
| 990 | } |
| 991 | |
| 992 | pub fn is_timeout(&self) -> bool { |
| 993 | self.kind == GitQueryErrorKind::TimedOut |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | impl std::fmt::Display for GitQueryError { |
| 998 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 999 | match self.kind { |
| 1000 | GitQueryErrorKind::Failed => { |
| 1001 | write!(f, "could not read the repository: {}", self.source) |
| 1002 | } |
| 1003 | GitQueryErrorKind::TimedOut => { |
| 1004 | write!(f, "reading the repository took too long: {}", self.source) |
| 1005 | } |
| 1006 | } |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | impl std::error::Error for GitQueryError { |
| 1011 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 1012 | Some(&*self.source) |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | |
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | |
| 1021 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 1022 | pub enum ArchiveFormat { |
| 1023 | TarGz, |
| 1024 | Zip, |
| 1025 | } |
| 1026 | |
| 1027 | impl ArchiveFormat { |
| 1028 | |
| 1029 | |
| 1030 | pub fn as_str(self) -> &'static str { |
| 1031 | match self { |
| 1032 | Self::TarGz => "tar.gz", |
| 1033 | Self::Zip => "zip", |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | |
| 1038 | |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | pub fn content_type(self) -> &'static str { |
| 1043 | match self { |
| 1044 | Self::TarGz => "application/gzip", |
| 1045 | Self::Zip => "application/zip", |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | |
| 1051 | |
| 1052 | impl std::str::FromStr for ArchiveFormat { |
| 1053 | type Err = DomainError; |
| 1054 | |
| 1055 | fn from_str(value: &str) -> std::result::Result<Self, Self::Err> { |
| 1056 | match value { |
| 1057 | "tar.gz" => Ok(Self::TarGz), |
| 1058 | "zip" => Ok(Self::Zip), |
| 1059 | other => Err(DomainError::validation( |
| 1060 | "format", |
| 1061 | format!("unknown archive format {other:?}"), |
| 1062 | )), |
| 1063 | } |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | |
| 1068 | |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | |
| 1073 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 1074 | pub struct ArchiveRequest { |
| 1075 | pub handle: OrgName, |
| 1076 | pub name: RepoName, |
| 1077 | pub format: ArchiveFormat, |
| 1078 | pub commit: ObjectId, |
| 1079 | |
| 1080 | |
| 1081 | pub prefix: String, |
| 1082 | } |
| 1083 | |
| 1084 | |
| 1085 | |
| 1086 | |
| 1087 | |
| 1088 | |
| 1089 | |
| 1090 | pub trait GitArchive: Send + Sync { |
| 1091 | fn archive( |
| 1092 | &self, |
| 1093 | request: ArchiveRequest, |
| 1094 | ) -> impl Future<Output = Result<ByteStream, GitArchiveError>> + Send; |
| 1095 | } |
| 1096 | |
| 1097 | |
| 1098 | |
| 1099 | |
| 1100 | |
| 1101 | #[derive(Debug)] |
| 1102 | pub struct GitArchiveError(Box<dyn std::error::Error + Send + Sync>); |
| 1103 | |
| 1104 | impl GitArchiveError { |
| 1105 | pub fn new(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self { |
| 1106 | Self(error.into()) |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | impl std::fmt::Display for GitArchiveError { |
| 1111 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 1112 | write!(f, "could not archive the repository: {}", self.0) |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | impl std::error::Error for GitArchiveError { |
| 1117 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 1118 | Some(&*self.0) |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | |
| 1123 | |
| 1124 | |
| 1125 | |
| 1126 | |
| 1127 | |
| 1128 | |
| 1129 | |
| 1130 | |
| 1131 | |
| 1132 | |
| 1133 | pub trait GitImport: Send + Sync { |
| 1134 | |
| 1135 | |
| 1136 | |
| 1137 | |
| 1138 | |
| 1139 | |
| 1140 | fn mirror( |
| 1141 | &self, |
| 1142 | handle: &OrgName, |
| 1143 | name: &RepoName, |
| 1144 | source: &ImportSource, |
| 1145 | ) -> impl Future<Output = Result<(), GitImportError>> + Send; |
| 1146 | } |
| 1147 | |
| 1148 | |
| 1149 | |
| 1150 | |
| 1151 | |
| 1152 | |
| 1153 | |
| 1154 | #[derive(Debug)] |
| 1155 | pub struct GitImportError(String); |
| 1156 | |
| 1157 | impl GitImportError { |
| 1158 | pub fn new(message: impl Into<String>) -> Self { |
| 1159 | Self(message.into()) |
| 1160 | } |
| 1161 | |
| 1162 | |
| 1163 | |
| 1164 | pub fn message(&self) -> &str { |
| 1165 | &self.0 |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | impl std::fmt::Display for GitImportError { |
| 1170 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 1171 | write!(f, "could not import the repository: {}", self.0) |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | impl std::error::Error for GitImportError {} |
| 1176 | |
| 1177 | |
| 1178 | |
| 1179 | |
| 1180 | |
| 1181 | |
| 1182 | |
| 1183 | |
| 1184 | |
| 1185 | |
| 1186 | |
| 1187 | |
| 1188 | |
| 1189 | |
| 1190 | |
| 1191 | |
| 1192 | pub trait GitMerge: Send + Sync { |
| 1193 | |
| 1194 | |
| 1195 | |
| 1196 | |
| 1197 | |
| 1198 | fn commit_tree( |
| 1199 | &self, |
| 1200 | handle: &OrgName, |
| 1201 | name: &RepoName, |
| 1202 | request: &CommitTreeRequest, |
| 1203 | ) -> impl Future<Output = Result<ObjectId, GitMergeError>> + Send; |
| 1204 | |
| 1205 | |
| 1206 | |
| 1207 | |
| 1208 | |
| 1209 | |
| 1210 | |
| 1211 | |
| 1212 | fn update_ref( |
| 1213 | &self, |
| 1214 | handle: &OrgName, |
| 1215 | name: &RepoName, |
| 1216 | branch: &RefName, |
| 1217 | new: &ObjectId, |
| 1218 | old: &ObjectId, |
| 1219 | ) -> impl Future<Output = Result<(), GitMergeError>> + Send; |
| 1220 | |
| 1221 | |
| 1222 | |
| 1223 | |
| 1224 | |
| 1225 | |
| 1226 | fn delete_ref( |
| 1227 | &self, |
| 1228 | handle: &OrgName, |
| 1229 | name: &RepoName, |
| 1230 | branch: &RefName, |
| 1231 | old: &ObjectId, |
| 1232 | ) -> impl Future<Output = Result<(), GitMergeError>> + Send; |
| 1233 | } |
| 1234 | |
| 1235 | |
| 1236 | |
| 1237 | |
| 1238 | |
| 1239 | |
| 1240 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 1241 | pub struct CommitTreeRequest { |
| 1242 | |
| 1243 | |
| 1244 | pub tree: ObjectId, |
| 1245 | |
| 1246 | |
| 1247 | pub parents: Vec<ObjectId>, |
| 1248 | |
| 1249 | pub message: String, |
| 1250 | |
| 1251 | |
| 1252 | |
| 1253 | pub author_name: String, |
| 1254 | pub author_email: String, |
| 1255 | |
| 1256 | |
| 1257 | pub now: std::time::SystemTime, |
| 1258 | } |
| 1259 | |
| 1260 | |
| 1261 | |
| 1262 | |
| 1263 | |
| 1264 | |
| 1265 | #[derive(Debug)] |
| 1266 | pub enum GitMergeError { |
| 1267 | |
| 1268 | Stale, |
| 1269 | Backend(Box<dyn std::error::Error + Send + Sync>), |
| 1270 | } |
| 1271 | |
| 1272 | impl GitMergeError { |
| 1273 | pub fn backend(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self { |
| 1274 | Self::Backend(error.into()) |
| 1275 | } |
| 1276 | |
| 1277 | pub fn is_stale(&self) -> bool { |
| 1278 | matches!(self, Self::Stale) |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | impl std::fmt::Display for GitMergeError { |
| 1283 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 1284 | match self { |
| 1285 | Self::Stale => f.write_str( |
| 1286 | "the branch moved while the merge was being prepared, so nothing was changed", |
| 1287 | ), |
| 1288 | Self::Backend(error) => write!(f, "the merge could not be written: {error}"), |
| 1289 | } |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | impl std::error::Error for GitMergeError { |
| 1294 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 1295 | match self { |
| 1296 | Self::Stale => None, |
| 1297 | Self::Backend(error) => Some(&**error), |
| 1298 | } |
| 1299 | } |
| 1300 | } |