Fix rev
parent: tbd commit: 088af7f
Showing 4 changed files with 99 insertions and 48 deletions
giterated-daemon/src/backend/git.rs
@@ -6,9 +6,10 @@ use giterated_models::instance::{Instance, RepositoryCreateRequest}; | ||
6 | 6 | |
7 | 7 | use giterated_models::repository::{ |
8 | 8 | Commit, DefaultBranch, Description, IssueLabel, LatestCommit, Repository, |
9 | RepositoryFileInspectRequest, RepositoryIssue, RepositoryIssueLabelsRequest, | |
10 | RepositoryIssuesCountRequest, RepositoryIssuesRequest, RepositoryObjectType, | |
11 | RepositoryTreeEntry, RepositoryVisibility, Visibility, RepositoryFile, RepositoryFileFromIdRequest, RepositoryDiffRequest, RepositoryDiff, RepositoryCommitBeforeRequest, | |
9 | RepositoryCommitBeforeRequest, RepositoryDiff, RepositoryDiffRequest, RepositoryFile, | |
10 | RepositoryFileFromIdRequest, RepositoryFileInspectRequest, RepositoryIssue, | |
11 | RepositoryIssueLabelsRequest, RepositoryIssuesCountRequest, RepositoryIssuesRequest, | |
12 | RepositoryObjectType, RepositoryTreeEntry, RepositoryVisibility, Visibility, | |
12 | 13 | }; |
13 | 14 | use giterated_models::settings::{AnySetting, Setting}; |
14 | 15 | use giterated_models::user::{User, UserParseError}; |
@@ -178,12 +179,16 @@ impl GitBackend { | ||
178 | 179 | } |
179 | 180 | } |
180 | 181 | |
181 | pub async fn open_repository_and_check_permissions(&self, owner: &User, name: &str, requester: Option<&User>) -> Result<git2::Repository, GitBackendError> { | |
182 | pub async fn open_repository_and_check_permissions( | |
183 | &self, | |
184 | owner: &User, | |
185 | name: &str, | |
186 | requester: Option<&User>, | |
187 | ) -> Result<git2::Repository, GitBackendError> { | |
182 | 188 | let repository = match self |
183 | 189 | .find_by_owner_user_name( |
184 | 190 | // &request.owner.instance.url, |
185 | owner, | |
186 | name, | |
191 | owner, name, | |
187 | 192 | ) |
188 | 193 | .await |
189 | 194 | { |
@@ -546,7 +551,9 @@ impl RepositoryBackend for GitBackend { | ||
546 | 551 | repository: &Repository, |
547 | 552 | request: &RepositoryFileInspectRequest, |
548 | 553 | ) -> Result<Vec<RepositoryTreeEntry>, Error> { |
549 | let git = self.open_repository_and_check_permissions(&repository.owner, &repository.name, requester).await?; | |
554 | let git = self | |
555 | .open_repository_and_check_permissions(&repository.owner, &repository.name, requester) | |
556 | .await?; | |
550 | 557 | |
551 | 558 | // Try and parse the input as a reference and get the object ID |
552 | 559 | let mut tree_id = match &request.rev { |
@@ -569,7 +576,12 @@ impl RepositoryBackend for GitBackend { | ||
569 | 576 | if tree_id.is_none() { |
570 | 577 | match git2::Oid::from_str(request.rev.as_ref().unwrap()) { |
571 | 578 | Ok(oid) => tree_id = Some(oid), |
572 | Err(_) => return Err(Box::new(GitBackendError::RefNotFound(request.rev.clone().unwrap())).into()), | |
579 | Err(_) => { | |
580 | return Err(Box::new(GitBackendError::RefNotFound( | |
581 | request.rev.clone().unwrap(), | |
582 | )) | |
583 | .into()) | |
584 | } | |
573 | 585 | } |
574 | 586 | } |
575 | 587 | |
@@ -578,7 +590,7 @@ impl RepositoryBackend for GitBackend { | ||
578 | 590 | let commit = git.find_commit(tree_id.unwrap()).unwrap(); |
579 | 591 | |
580 | 592 | // this is stupid |
581 | let mut current_path = request.rev.clone().unwrap(); | |
593 | let mut current_path = request.rev.clone().unwrap_or_else(|| "master".to_string()); | |
582 | 594 | |
583 | 595 | // Get the commit tree |
584 | 596 | let git_tree = if let Some(path) = &request.path { |
@@ -609,8 +621,12 @@ impl RepositoryBackend for GitBackend { | ||
609 | 621 | git2::ObjectType::Blob => RepositoryObjectType::Blob, |
610 | 622 | _ => unreachable!(), |
611 | 623 | }; |
612 | let mut tree_entry = | |
613 | RepositoryTreeEntry::new(entry.id().to_string().as_str(), entry.name().unwrap(), object_type, entry.filemode()); | |
624 | let mut tree_entry = RepositoryTreeEntry::new( | |
625 | entry.id().to_string().as_str(), | |
626 | entry.name().unwrap(), | |
627 | object_type, | |
628 | entry.filemode(), | |
629 | ); | |
614 | 630 | |
615 | 631 | if request.extra_metadata { |
616 | 632 | // Get the file size if It's a blob |
@@ -653,17 +669,25 @@ impl RepositoryBackend for GitBackend { | ||
653 | 669 | repository: &Repository, |
654 | 670 | request: &RepositoryFileFromIdRequest, |
655 | 671 | ) -> Result<RepositoryFile, Error> { |
656 | let git = self.open_repository_and_check_permissions(&repository.owner, &repository.name, requester).await?; | |
672 | let git = self | |
673 | .open_repository_and_check_permissions(&repository.owner, &repository.name, requester) | |
674 | .await?; | |
657 | 675 | |
658 | 676 | // Parse the passed object id |
659 | 677 | let oid = match git2::Oid::from_str(request.0.as_str()) { |
660 | 678 | Ok(oid) => oid, |
661 | Err(_) => return Err(Box::new(GitBackendError::InvalidObjectId(request.0.clone())).into()), | |
679 | Err(_) => { | |
680 | return Err(Box::new(GitBackendError::InvalidObjectId(request.0.clone())).into()) | |
681 | } | |
662 | 682 | }; |
663 | 683 | |
664 | 684 | // Find the file and turn it into our own struct |
665 | 685 | let file = match git.find_blob(oid) { |
666 | Ok(blob) => RepositoryFile { content: blob.content().to_vec(), binary: blob.is_binary(), size: blob.size() }, | |
686 | Ok(blob) => RepositoryFile { | |
687 | content: blob.content().to_vec(), | |
688 | binary: blob.is_binary(), | |
689 | size: blob.size(), | |
690 | }, | |
667 | 691 | Err(_) => return Err(Box::new(GitBackendError::BlobNotFound(oid.to_string())).into()), |
668 | 692 | }; |
669 | 693 | |
@@ -676,32 +700,52 @@ impl RepositoryBackend for GitBackend { | ||
676 | 700 | repository: &Repository, |
677 | 701 | request: &RepositoryDiffRequest, |
678 | 702 | ) -> Result<RepositoryDiff, Error> { |
679 | let git = self.open_repository_and_check_permissions(&repository.owner, &repository.name, requester).await?; | |
703 | let git = self | |
704 | .open_repository_and_check_permissions(&repository.owner, &repository.name, requester) | |
705 | .await?; | |
680 | 706 | |
681 | 707 | // Parse the passed object ids |
682 | 708 | let oid_old = match git2::Oid::from_str(request.old_id.as_str()) { |
683 | 709 | Ok(oid) => oid, |
684 | Err(_) => return Err(Box::new(GitBackendError::InvalidObjectId(request.old_id.clone())).into()), | |
710 | Err(_) => { | |
711 | return Err( | |
712 | Box::new(GitBackendError::InvalidObjectId(request.old_id.clone())).into(), | |
713 | ) | |
714 | } | |
685 | 715 | }; |
686 | 716 | let oid_new = match git2::Oid::from_str(request.new_id.as_str()) { |
687 | 717 | Ok(oid) => oid, |
688 | Err(_) => return Err(Box::new(GitBackendError::InvalidObjectId(request.new_id.clone())).into()), | |
718 | Err(_) => { | |
719 | return Err( | |
720 | Box::new(GitBackendError::InvalidObjectId(request.new_id.clone())).into(), | |
721 | ) | |
722 | } | |
689 | 723 | }; |
690 | 724 | |
691 | 725 | // Get the trees of those object ids |
692 | 726 | let tree_old = match git.find_tree(oid_old) { |
693 | 727 | Ok(tree) => tree, |
694 | Err(_) => return Err(Box::new(GitBackendError::TreeNotFound(oid_old.to_string())).into()), | |
728 | Err(_) => { | |
729 | return Err(Box::new(GitBackendError::TreeNotFound(oid_old.to_string())).into()) | |
730 | } | |
695 | 731 | }; |
696 | 732 | let tree_new = match git.find_tree(oid_new) { |
697 | 733 | Ok(tree) => tree, |
698 | Err(_) => return Err(Box::new(GitBackendError::TreeNotFound(oid_new.to_string())).into()), | |
734 | Err(_) => { | |
735 | return Err(Box::new(GitBackendError::TreeNotFound(oid_new.to_string())).into()) | |
736 | } | |
699 | 737 | }; |
700 | 738 | |
701 | 739 | // Diff the two trees against each other |
702 | 740 | let diff = match git.diff_tree_to_tree(Some(&tree_old), Some(&tree_new), None) { |
703 | 741 | Ok(diff) => diff, |
704 | Err(_) => return Err(Box::new(GitBackendError::FailedDiffing(oid_old.to_string(), oid_new.to_string())).into()), | |
742 | Err(_) => { | |
743 | return Err(Box::new(GitBackendError::FailedDiffing( | |
744 | oid_old.to_string(), | |
745 | oid_new.to_string(), | |
746 | )) | |
747 | .into()) | |
748 | } | |
705 | 749 | }; |
706 | 750 | |
707 | 751 | // Should be safe to unwrap? |
@@ -709,13 +753,14 @@ impl RepositoryBackend for GitBackend { | ||
709 | 753 | |
710 | 754 | // Honestly not quite sure what is going on here, could not find documentation. |
711 | 755 | // Print the entire patch |
712 | let mut patch = String::new(); | |
756 | let mut patch = String::new(); | |
713 | 757 | |
714 | 758 | diff.print(git2::DiffFormat::Patch, |_, _, line| { |
715 | 759 | patch.push_str(std::str::from_utf8(line.content()).unwrap()); |
716 | 760 | true |
717 | }).unwrap(); | |
718 | ||
761 | }) | |
762 | .unwrap(); | |
763 | ||
719 | 764 | Ok(RepositoryDiff { |
720 | 765 | files_changed: stats.files_changed(), |
721 | 766 | insertions: stats.insertions(), |
@@ -730,12 +775,16 @@ impl RepositoryBackend for GitBackend { | ||
730 | 775 | repository: &Repository, |
731 | 776 | request: &RepositoryCommitBeforeRequest, |
732 | 777 | ) -> Result<Commit, Error> { |
733 | let git = self.open_repository_and_check_permissions(&repository.owner, &repository.name, requester).await?; | |
778 | let git = self | |
779 | .open_repository_and_check_permissions(&repository.owner, &repository.name, requester) | |
780 | .await?; | |
734 | 781 | |
735 | 782 | // Parse the passed object id |
736 | 783 | let oid = match git2::Oid::from_str(request.0.as_str()) { |
737 | 784 | Ok(oid) => oid, |
738 | Err(_) => return Err(Box::new(GitBackendError::InvalidObjectId(request.0.clone())).into()), | |
785 | Err(_) => { | |
786 | return Err(Box::new(GitBackendError::InvalidObjectId(request.0.clone())).into()) | |
787 | } | |
739 | 788 | }; |
740 | 789 | |
741 | 790 | // Find the commit using the parsed oid |
@@ -747,7 +796,9 @@ impl RepositoryBackend for GitBackend { | ||
747 | 796 | // Get the first parent it has |
748 | 797 | let parent = match commit.parent(0) { |
749 | 798 | Ok(parent) => Commit::from(parent), |
750 | Err(_) => return Err(Box::new(GitBackendError::CommitParentNotFound(oid.to_string())).into()), | |
799 | Err(_) => { | |
800 | return Err(Box::new(GitBackendError::CommitParentNotFound(oid.to_string())).into()) | |
801 | } | |
751 | 802 | }; |
752 | 803 | |
753 | 804 | Ok(parent) |
giterated-daemon/src/backend/mod.rs
@@ -16,9 +16,10 @@ use giterated_models::instance::{ | ||
16 | 16 | }; |
17 | 17 | |
18 | 18 | use giterated_models::repository::{ |
19 | IssueLabel, Repository, RepositoryFileInspectRequest, RepositoryIssue, | |
20 | RepositoryIssueLabelsRequest, RepositoryIssuesCountRequest, RepositoryIssuesRequest, | |
21 | RepositorySummary, RepositoryTreeEntry, RepositoryFileFromIdRequest, RepositoryFile, RepositoryDiffRequest, RepositoryDiff, Commit, RepositoryCommitBeforeRequest, | |
19 | Commit, IssueLabel, Repository, RepositoryCommitBeforeRequest, RepositoryDiff, | |
20 | RepositoryDiffRequest, RepositoryFile, RepositoryFileFromIdRequest, | |
21 | RepositoryFileInspectRequest, RepositoryIssue, RepositoryIssueLabelsRequest, | |
22 | RepositoryIssuesCountRequest, RepositoryIssuesRequest, RepositorySummary, RepositoryTreeEntry, | |
22 | 23 | }; |
23 | 24 | use giterated_models::settings::AnySetting; |
24 | 25 | use giterated_models::user::User; |
giterated-daemon/src/database_backend/handler.rs
@@ -7,8 +7,10 @@ use giterated_models::{ | ||
7 | 7 | object_backend::ObjectBackend, |
8 | 8 | operation::{AnyOperation, GiteratedOperation}, |
9 | 9 | repository::{ |
10 | DefaultBranch, Description, LatestCommit, Repository, RepositoryFileInspectRequest, | |
11 | RepositoryInfoRequest, RepositorySummary, RepositoryView, Visibility, RepositoryFile, RepositoryFileFromIdRequest, RepositoryDiff, RepositoryDiffRequest, RepositoryCommitBeforeRequest, Commit, | |
10 | Commit, DefaultBranch, Description, LatestCommit, Repository, | |
11 | RepositoryCommitBeforeRequest, RepositoryDiff, RepositoryDiffRequest, RepositoryFile, | |
12 | RepositoryFileFromIdRequest, RepositoryFileInspectRequest, RepositoryInfoRequest, | |
13 | RepositorySummary, RepositoryView, Visibility, | |
12 | 14 | }, |
13 | 15 | settings::{AnySetting, GetSetting, GetSettingError, SetSetting, SetSettingError}, |
14 | 16 | user::{User, UserRepositoriesRequest}, |
@@ -276,7 +278,8 @@ pub fn repository_file_from_id( | ||
276 | 278 | drop(repository_backend); |
277 | 279 | |
278 | 280 | Ok(file) |
279 | }.boxed() | |
281 | } | |
282 | .boxed() | |
280 | 283 | } |
281 | 284 | |
282 | 285 | pub fn repository_diff( |
@@ -294,17 +297,14 @@ pub fn repository_diff( | ||
294 | 297 | |
295 | 298 | let mut repository_backend = state.repository_backend.lock().await; |
296 | 299 | let file = repository_backend |
297 | .repository_diff( | |
298 | None, | |
299 | object.object(), | |
300 | &operation, | |
301 | ) | |
300 | .repository_diff(None, object.object(), &operation) | |
302 | 301 | .await |
303 | 302 | .map_err(|err| OperationError::Internal(format!("{:?}", err)))?; |
304 | 303 | drop(repository_backend); |
305 | 304 | |
306 | 305 | Ok(file) |
307 | }.boxed() | |
306 | } | |
307 | .boxed() | |
308 | 308 | } |
309 | 309 | |
310 | 310 | pub fn repository_commit_before( |
@@ -322,17 +322,14 @@ pub fn repository_commit_before( | ||
322 | 322 | |
323 | 323 | let mut repository_backend = state.repository_backend.lock().await; |
324 | 324 | let file = repository_backend |
325 | .repository_commit_before( | |
326 | None, | |
327 | object.object(), | |
328 | &operation, | |
329 | ) | |
325 | .repository_commit_before(None, object.object(), &operation) | |
330 | 326 | .await |
331 | 327 | .map_err(|err| OperationError::Internal(format!("{:?}", err)))?; |
332 | 328 | drop(repository_backend); |
333 | 329 | |
334 | 330 | Ok(file) |
335 | }.boxed() | |
331 | } | |
332 | .boxed() | |
336 | 333 | } |
337 | 334 | |
338 | 335 | pub fn repository_get_value( |
giterated-daemon/src/database_backend/mod.rs
@@ -17,8 +17,9 @@ use tokio::sync::Mutex; | ||
17 | 17 | use crate::backend::{RepositoryBackend, UserBackend}; |
18 | 18 | |
19 | 19 | use self::handler::{ |
20 | repository_get_setting, repository_get_value, repository_info, repository_set_setting, | |
21 | user_get_repositories, user_get_setting, user_get_value, user_set_setting, OperationHandlers, repository_file_from_id, repository_diff, repository_commit_before, | |
20 | repository_commit_before, repository_diff, repository_file_from_id, repository_get_setting, | |
21 | repository_get_value, repository_info, repository_set_setting, user_get_repositories, | |
22 | user_get_setting, user_get_value, user_set_setting, OperationHandlers, | |
22 | 23 | }; |
23 | 24 | |
24 | 25 | #[derive(Clone, Debug)] |
@@ -257,8 +258,9 @@ mod test { | ||
257 | 258 | use giterated_models::object_backend::ObjectBackend; |
258 | 259 | |
259 | 260 | use giterated_models::repository::{ |
260 | Description, Repository, RepositoryFileInspectRequest, RepositorySummary, | |
261 | RepositoryTreeEntry, RepositoryFileFromIdRequest, RepositoryFile, RepositoryDiff, RepositoryDiffRequest, RepositoryCommitBeforeRequest, Commit | |
261 | Commit, Description, Repository, RepositoryCommitBeforeRequest, RepositoryDiff, | |
262 | RepositoryDiffRequest, RepositoryFile, RepositoryFileFromIdRequest, | |
263 | RepositoryFileInspectRequest, RepositorySummary, RepositoryTreeEntry, | |
262 | 264 | }; |
263 | 265 | use giterated_models::settings::AnySetting; |
264 | 266 | use giterated_models::user::{DisplayName, User}; |