Fixes and cleanup
parent: tbd commit: 555bd26
1 | use Error; |
2 | use async_trait; |
3 | |
4 | use BranchType; |
5 | use ; |
6 | |
7 | use |
8 | Commit, DefaultBranch, Description, IssueLabel, LatestCommit, Repository, |
9 | RepositoryCommitBeforeRequest, RepositoryDiff, RepositoryDiffRequest, RepositoryFile, |
10 | RepositoryFileFromIdRequest, RepositoryFileInspectRequest, RepositoryIssue, |
11 | RepositoryIssueLabelsRequest, RepositoryIssuesCountRequest, RepositoryIssuesRequest, |
12 | RepositoryObjectType, RepositoryTreeEntry, RepositoryVisibility, Visibility, |
13 | ; |
14 | use ; |
15 | use ; |
16 | use ; |
17 | use Value; |
18 | use PgPool; |
19 | use |
20 | , |
21 | , | Arc
22 | ; |
23 | use Error; |
24 | use Mutex; |
25 | |
26 | use ; |
27 | |
28 | // TODO: Handle this |
29 | //region database structures |
30 | |
31 | /// Repository in the database |
32 | |
33 | |
34 | #[sqlx(try_from = "String")] |
35 | pub owner_user: User, |
36 | pub name: String, |
37 | pub description: , |
38 | pub visibility: RepositoryVisibility, |
39 | pub default_branch: String, |
40 | |
41 | |
42 | |
43 | // Separate function because "Private" will be expanded later |
44 | /// Checks if the user is allowed to view this repository |
45 | |
46 | !matches! |
47 | || |
48 | && Some == user |
49 | |
50 | |
51 | // This is in it's own function because I assume I'll have to add logic to this later |
52 | |
53 | &self, |
54 | repository_directory: &str, |
55 | |
56 | match open |
57 | "{}/{}/{}/{}" |
58 | repository_directory, self.owner_user.instance.url, self.owner_user.username, self.name |
59 | ) |
60 | Ok => Ok, |
61 | Err => |
62 | let err = FailedOpeningFromDisk; |
63 | error!; |
64 | |
65 | Err |
66 | |
67 | |
68 | |
69 | |
70 | |
71 | //endregion |
72 | |
73 | |
74 | |
75 | |
76 | FailedCreatingRepository, |
77 | |
78 | FailedInsertingIntoDatabase, |
79 | |
80 | RepositoryNotFound , |
81 | |
82 | RepositoryAlreadyExists , |
83 | |
84 | CouldNotDeleteFromDisk, |
85 | |
86 | FailedDeletingFromDatabase, |
87 | |
88 | FailedOpeningFromDisk, |
89 | |
90 | RefNotFound, |
91 | |
92 | PathNotFound, |
93 | |
94 | LastCommitNotFound, |
95 | |
96 | InvalidObjectId, |
97 | |
98 | BlobNotFound, |
99 | |
100 | TreeNotFound, |
101 | |
102 | CommitNotFound, |
103 | |
104 | CommitParentNotFound, |
105 | |
106 | FailedDiffing, |
107 | |
108 | |
109 | |
110 | pub pg_pool: PgPool, |
111 | pub repository_folder: String, |
112 | pub instance: Instance, |
113 | pub settings_provider: , |
114 | |
115 | |
116 | |
117 | |
118 | pg_pool: &PgPool, |
119 | repository_folder: &str, |
120 | instance: impl , |
121 | settings_provider: , |
122 | |
123 | Self |
124 | pg_pool: pg_pool.clone, |
125 | repository_folder: repository_folder.to_string, |
126 | instance: instance.to_owned, |
127 | settings_provider, |
128 | |
129 | |
130 | |
131 | pub async |
132 | &self, |
133 | user: &User, |
134 | repository_name: &str, |
135 | |
136 | if let Ok = query_as! |
137 | r#"SELECT owner_user, name, description, visibility as "visibility: _", default_branch FROM repositories WHERE owner_user = $1 AND name = $2"#, |
138 | user.to_string, repository_name |
139 | .fetch_one |
140 | .await |
141 | Ok |
142 | else |
143 | Err |
144 | owner_user: user.to_string, |
145 | name: repository_name.to_string, |
146 | |
147 | |
148 | |
149 | |
150 | pub async |
151 | &self, |
152 | user: &User, |
153 | repository_name: &str, |
154 | |
155 | if let Err = remove_dir_all |
156 | "{}/{}/{}/{}" |
157 | self.repository_folder, user.instance.url, user.username, repository_name |
158 | ) |
159 | let err = CouldNotDeleteFromDisk; |
160 | error! |
161 | "Couldn't delete repository from disk, this is bad! {:?}", |
162 | err |
163 | ; |
164 | |
165 | return Err; |
166 | |
167 | |
168 | // Delete the repository from the database |
169 | match query! |
170 | "DELETE FROM repositories WHERE owner_user = $1 AND name = $2", |
171 | user.to_string, |
172 | repository_name |
173 | |
174 | .execute |
175 | .await |
176 | |
177 | Ok => Ok, |
178 | Err => Err, |
179 | |
180 | |
181 | |
182 | pub async |
183 | &self, |
184 | owner: &User, |
185 | name: &str, |
186 | requester: , |
187 | |
188 | let repository = match self |
189 | .find_by_owner_user_name |
190 | // &request.owner.instance.url, |
191 | owner, name, |
192 | |
193 | .await |
194 | |
195 | Ok => repository, |
196 | Err => return Err, |
197 | ; |
198 | |
199 | if let Some = requester |
200 | if !repository.can_user_view_repository |
201 | return Err |
202 | owner_user: repository.owner_user.to_string, |
203 | name: repository.name.clone, |
204 | ; |
205 | |
206 | else if matches! |
207 | info!; |
208 | // Unauthenticated users can never view private repositories |
209 | |
210 | return Err |
211 | owner_user: repository.owner_user.to_string, |
212 | name: repository.name.clone, |
213 | ; |
214 | |
215 | |
216 | match repository.open_git2_repository |
217 | Ok => Ok, |
218 | Err => return Err, |
219 | |
220 | |
221 | |
222 | // TODO: Find where this fits |
223 | // TODO: Cache this and general repository tree and invalidate select files on push |
224 | // TODO: Find better and faster technique for this |
225 | |
226 | path: &str, |
227 | git: & Repository, |
228 | start_commit: & Commit, |
229 | |
230 | let mut revwalk = git.revwalk?; |
231 | revwalk.set_sorting?; |
232 | revwalk.push?; |
233 | |
234 | for oid in revwalk |
235 | let oid = oid?; |
236 | let commit = git.find_commit?; |
237 | |
238 | // Merge commits have 2 or more parents |
239 | // Commits with 0 parents are handled different because we can't diff against them |
240 | if commit.parent_count == 0 |
241 | return Ok; |
242 | else if commit.parent_count == 1 |
243 | let tree = commit.tree?; |
244 | let last_tree = commit.parent?.tree?; |
245 | |
246 | // Get the diff between the current tree and the last one |
247 | let diff = git.diff_tree_to_tree?; |
248 | |
249 | for dd in diff.deltas |
250 | // Get the path of the current file we're diffing against |
251 | let current_path = dd.new_file .path .unwrap; |
252 | |
253 | // Path or directory |
254 | if current_path.eq || current_path.starts_with |
255 | return Ok; |
256 | |
257 | |
258 | |
259 | |
260 | |
261 | Err? |
262 | |
263 | |
264 | |
265 | |
266 | |
267 | async |
268 | if let Ok = self |
269 | .find_by_owner_user_name |
270 | .await |
271 | |
272 | Ok |
273 | else |
274 | Ok |
275 | |
276 | |
277 | |
278 | async |
279 | &mut self, |
280 | _user: &User, |
281 | request: &RepositoryCreateRequest, |
282 | |
283 | // Check if repository already exists in the database |
284 | if let Ok = self |
285 | .find_by_owner_user_name |
286 | .await |
287 | |
288 | let err = RepositoryAlreadyExists |
289 | owner_user: repository.owner_user.to_string, |
290 | name: repository.name, |
291 | ; |
292 | error!; |
293 | |
294 | return Err; |
295 | |
296 | |
297 | // Insert the repository into the database |
298 | let _ = match query_as! |
299 | r#"INSERT INTO repositories VALUES ($1, $2, $3, $4, $5) RETURNING owner_user, name, description, visibility as "visibility: _", default_branch"#, |
300 | request.owner.to_string, request.name, request.description, request.visibility as _, "master" |
301 | .fetch_one |
302 | .await |
303 | Ok => repository, |
304 | Err => |
305 | let err = FailedInsertingIntoDatabase; |
306 | error!; |
307 | |
308 | return Err; |
309 | |
310 | ; |
311 | |
312 | // Create bare (server side) repository on disk |
313 | match init_bare |
314 | "{}/{}/{}/{}" |
315 | self.repository_folder, |
316 | request.owner.instance.url, |
317 | request.owner.username, |
318 | request.name |
319 | ) |
320 | Ok => |
321 | debug! |
322 | "Created new repository with the name {}/{}/{}", |
323 | request.owner.instance.url, request.owner.username, request.name |
324 | ; |
325 | Ok |
326 | owner: request.owner.clone, |
327 | name: request.name.clone, |
328 | instance: request.instance.as_ref .unwrap_or .clone, |
329 | |
330 | |
331 | Err => |
332 | let err = FailedCreatingRepository; |
333 | error!; |
334 | |
335 | // Delete repository from database |
336 | self.delete_by_owner_user_name |
337 | .await?; |
338 | |
339 | // ??? |
340 | Err |
341 | |
342 | |
343 | |
344 | |
345 | async |
346 | &mut self, |
347 | repository: &Repository, |
348 | name: &str, |
349 | |
350 | Ok |
351 | if name == value_name |
352 | from_raw |
353 | else if name == value_name |
354 | from_raw |
355 | else if name == value_name |
356 | from_raw |
357 | else if name == value_name |
358 | from_raw |
359 | else |
360 | return Err; |
361 | |
362 | |
363 | |
364 | |
365 | async |
366 | &mut self, |
367 | repository: &Repository, |
368 | name: &str, |
369 | |
370 | let mut provider = self.settings_provider.lock .await; |
371 | |
372 | Ok |
373 | |
374 | |
375 | async |
376 | &mut self, |
377 | repository: &Repository, |
378 | name: &str, |
379 | setting: &Value, |
380 | |
381 | let mut provider = self.settings_provider.lock .await; |
382 | |
383 | provider |
384 | .repository_write |
385 | .await |
386 | |
387 | |
388 | // async fn repository_info( |
389 | // &mut self, |
390 | // requester: Option<&User>, |
391 | // request: &RepositoryInfoRequest, |
392 | // ) -> Result<RepositoryView, Error> { |
393 | // let repository = match self |
394 | // .find_by_owner_user_name( |
395 | // // &request.owner.instance.url, |
396 | // &request.repository.owner, |
397 | // &request.repository.name, |
398 | // ) |
399 | // .await |
400 | // { |
401 | // Ok(repository) => repository, |
402 | // Err(err) => return Err(Box::new(err).into()), |
403 | // }; |
404 | |
405 | // if let Some(requester) = requester { |
406 | // if !repository.can_user_view_repository(Some(&requester)) { |
407 | // return Err(Box::new(GitBackendError::RepositoryNotFound { |
408 | // owner_user: request.repository.owner.to_string(), |
409 | // name: request.repository.name.clone(), |
410 | // }) |
411 | // .into()); |
412 | // } |
413 | // } else if matches!(repository.visibility, RepositoryVisibility::Private) { |
414 | // info!("Unauthenticated"); |
415 | // // Unauthenticated users can never view private repositories |
416 | |
417 | // return Err(Box::new(GitBackendError::RepositoryNotFound { |
418 | // owner_user: request.repository.owner.to_string(), |
419 | // name: request.repository.name.clone(), |
420 | // }) |
421 | // .into()); |
422 | // } |
423 | |
424 | // let git = match repository.open_git2_repository(&self.repository_folder) { |
425 | // Ok(git) => git, |
426 | // Err(err) => return Err(Box::new(err).into()), |
427 | // }; |
428 | |
429 | // let rev_name = match &request.rev { |
430 | // None => { |
431 | // if let Ok(head) = git.head() { |
432 | // head.name().unwrap().to_string() |
433 | // } else { |
434 | // // Nothing in database, render empty tree. |
435 | // return Ok(RepositoryView { |
436 | // name: repository.name, |
437 | // owner: request.repository.owner.clone(), |
438 | // description: repository.description, |
439 | // visibility: repository.visibility, |
440 | // default_branch: repository.default_branch, |
441 | // latest_commit: None, |
442 | // tree_rev: None, |
443 | // tree: vec![], |
444 | // }); |
445 | // } |
446 | // } |
447 | // Some(rev_name) => { |
448 | // // Find the reference, otherwise return GitBackendError |
449 | // match git |
450 | // .find_reference(format!("refs/heads/{}", rev_name).as_str()) |
451 | // .map_err(|_| GitBackendError::RefNotFound(rev_name.to_string())) |
452 | // { |
453 | // Ok(reference) => reference.name().unwrap().to_string(), |
454 | // Err(err) => return Err(Box::new(err).into()), |
455 | // } |
456 | // } |
457 | // }; |
458 | |
459 | // // Get the git object as a commit |
460 | // let rev = match git |
461 | // .revparse_single(rev_name.as_str()) |
462 | // .map_err(|_| GitBackendError::RefNotFound(rev_name.to_string())) |
463 | // { |
464 | // Ok(rev) => rev, |
465 | // Err(err) => return Err(Box::new(err).into()), |
466 | // }; |
467 | // let commit = rev.as_commit().unwrap(); |
468 | |
469 | // // this is stupid |
470 | // let mut current_path = rev_name.replace("refs/heads/", ""); |
471 | |
472 | // // Get the commit tree |
473 | // let git_tree = if let Some(path) = &request.path { |
474 | // // Add it to our full path string |
475 | // current_path.push_str(format!("/{}", path).as_str()); |
476 | // // Get the specified path, return an error if it wasn't found. |
477 | // let entry = match commit |
478 | // .tree() |
479 | // .unwrap() |
480 | // .get_path(&PathBuf::from(path)) |
481 | // .map_err(|_| GitBackendError::PathNotFound(path.to_string())) |
482 | // { |
483 | // Ok(entry) => entry, |
484 | // Err(err) => return Err(Box::new(err).into()), |
485 | // }; |
486 | // // Turn the entry into a git tree |
487 | // entry.to_object(&git).unwrap().as_tree().unwrap().clone() |
488 | // } else { |
489 | // commit.tree().unwrap() |
490 | // }; |
491 | |
492 | // // Iterate over the git tree and collect it into our own tree types |
493 | // let mut tree = git_tree |
494 | // .iter() |
495 | // .map(|entry| { |
496 | // let object_type = match entry.kind().unwrap() { |
497 | // ObjectType::Tree => RepositoryObjectType::Tree, |
498 | // ObjectType::Blob => RepositoryObjectType::Blob, |
499 | // _ => unreachable!(), |
500 | // }; |
501 | // let mut tree_entry = |
502 | // RepositoryTreeEntry::new(entry.name().unwrap(), object_type, entry.filemode()); |
503 | |
504 | // if request.extra_metadata { |
505 | // // Get the file size if It's a blob |
506 | // let object = entry.to_object(&git).unwrap(); |
507 | // if let Some(blob) = object.as_blob() { |
508 | // tree_entry.size = Some(blob.size()); |
509 | // } |
510 | |
511 | // // Could possibly be done better |
512 | // let path = if let Some(path) = current_path.split_once('/') { |
513 | // format!("{}/{}", path.1, entry.name().unwrap()) |
514 | // } else { |
515 | // entry.name().unwrap().to_string() |
516 | // }; |
517 | |
518 | // // Get the last commit made to the entry |
519 | // if let Ok(last_commit) = |
520 | // GitBackend::get_last_commit_of_file(&path, &git, commit) |
521 | // { |
522 | // tree_entry.last_commit = Some(last_commit); |
523 | // } |
524 | // } |
525 | |
526 | // tree_entry |
527 | // }) |
528 | // .collect::<Vec<RepositoryTreeEntry>>(); |
529 | |
530 | // // Sort the tree alphabetically and with tree first |
531 | // tree.sort_unstable_by_key(|entry| entry.name.to_lowercase()); |
532 | // tree.sort_unstable_by_key(|entry| { |
533 | // std::cmp::Reverse(format!("{:?}", entry.object_type).to_lowercase()) |
534 | // }); |
535 | |
536 | // Ok(RepositoryView { |
537 | // name: repository.name, |
538 | // owner: request.repository.owner.clone(), |
539 | // description: repository.description, |
540 | // visibility: repository.visibility, |
541 | // default_branch: repository.default_branch, |
542 | // latest_commit: Some(Commit::from(commit.clone())), |
543 | // tree_rev: Some(rev_name), |
544 | // tree, |
545 | // }) |
546 | // } |
547 | |
548 | async |
549 | &mut self, |
550 | requester: , |
551 | repository: &Repository, |
552 | request: &RepositoryFileInspectRequest, |
553 | |
554 | let git = self |
555 | .open_repository_and_check_permissions |
556 | .await?; |
557 | |
558 | // Try and parse the input as a reference and get the object ID |
559 | let mut tree_id = match &request.rev |
560 | None => |
561 | if let Ok = git.head |
562 | // TODO: Fix for symbolic references |
563 | head.target |
564 | else |
565 | // Nothing in database, render empty tree. |
566 | return Ok; |
567 | |
568 | |
569 | Some => |
570 | // Find the reference, otherwise return GitBackendError |
571 | git.refname_to_id .ok |
572 | |
573 | ; |
574 | |
575 | // If the reference wasn't found, try parsing it as a commit ID |
576 | if tree_id.is_none |
577 | if let Ok = from_str |
578 | tree_id = Some |
579 | |
580 | |
581 | |
582 | // If the commit ID wasn't found, try parsing it as a branch and otherwise return error |
583 | if tree_id.is_none |
584 | match git.find_branch |
585 | Ok => tree_id = branch.get .target, |
586 | Err => |
587 | return Err |
588 | request.rev.clone .unwrap, |
589 | |
590 | .into |
591 | |
592 | |
593 | |
594 | |
595 | // unwrap might be dangerous? |
596 | // Get the commit from the oid |
597 | let commit = git.find_commit .unwrap; |
598 | |
599 | // this is stupid |
600 | let mut current_path = request.rev.clone .unwrap_or_else; |
601 | |
602 | // Get the commit tree |
603 | let git_tree = if let Some = &request.path |
604 | // Add it to our full path string |
605 | current_path.push_str; |
606 | // Get the specified path, return an error if it wasn't found. |
607 | let entry = match commit |
608 | .tree |
609 | .unwrap |
610 | .get_path |
611 | .map_err |
612 | |
613 | Ok => entry, |
614 | Err => return Err, |
615 | ; |
616 | // Turn the entry into a git tree |
617 | entry.to_object .unwrap .as_tree .unwrap .clone |
618 | else |
619 | commit.tree .unwrap |
620 | ; |
621 | |
622 | // Iterate over the git tree and collect it into our own tree types |
623 | let mut tree = git_tree |
624 | .iter |
625 | .map |
626 | let object_type = match entry.kind .unwrap |
627 | => Tree, | Tree
628 | => Blob, | Blob
629 | _ => unreachable!, |
630 | ; |
631 | let mut tree_entry = new |
632 | entry.id .to_string .as_str, |
633 | entry.name .unwrap, |
634 | object_type, |
635 | entry.filemode, |
636 | ; |
637 | |
638 | if request.extra_metadata |
639 | // Get the file size if It's a blob |
640 | let object = entry.to_object .unwrap; |
641 | if let Some = object.as_blob |
642 | tree_entry.size = Some; |
643 | |
644 | |
645 | // Could possibly be done better |
646 | let path = if let Some = current_path.split_once |
647 | format! |
648 | else |
649 | entry.name .unwrap .to_string |
650 | ; |
651 | |
652 | // Get the last commit made to the entry |
653 | if let Ok = |
654 | get_last_commit_of_file |
655 | |
656 | tree_entry.last_commit = Some; |
657 | |
658 | |
659 | |
660 | tree_entry |
661 | |
662 | .; |
663 | |
664 | // Sort the tree alphabetically and with tree first |
665 | tree.sort_unstable_by_key; |
666 | tree.sort_unstable_by_key |
667 | Reverse |
668 | ; |
669 | |
670 | Ok |
671 | |
672 | |
673 | async |
674 | &mut self, |
675 | requester: , |
676 | repository: &Repository, |
677 | request: &RepositoryFileFromIdRequest, |
678 | |
679 | let git = self |
680 | .open_repository_and_check_permissions |
681 | .await?; |
682 | |
683 | // Parse the passed object id |
684 | let oid = match from_str |
685 | Ok => oid, |
686 | Err => |
687 | return Err |
688 | |
689 | ; |
690 | |
691 | // Find the file and turn it into our own struct |
692 | let file = match git.find_blob |
693 | Ok => RepositoryFile |
694 | content: blob.content .to_vec, |
695 | binary: blob.is_binary, |
696 | size: blob.size, |
697 | , |
698 | Err => return Err, |
699 | ; |
700 | |
701 | Ok |
702 | |
703 | |
704 | async |
705 | &mut self, |
706 | requester: , |
707 | repository: &Repository, |
708 | request: &RepositoryDiffRequest, |
709 | |
710 | let git = self |
711 | .open_repository_and_check_permissions |
712 | .await?; |
713 | |
714 | // Parse the passed object ids |
715 | let oid_old = match from_str |
716 | Ok => oid, |
717 | Err => |
718 | return Err |
719 | Box new .into, |
720 | |
721 | |
722 | ; |
723 | let oid_new = match from_str |
724 | Ok => oid, |
725 | Err => |
726 | return Err |
727 | Box new .into, |
728 | |
729 | |
730 | ; |
731 | |
732 | // Get the trees of those object ids |
733 | let tree_old = match git.find_tree |
734 | Ok => tree, |
735 | Err => |
736 | return Err |
737 | |
738 | ; |
739 | let tree_new = match git.find_tree |
740 | Ok => tree, |
741 | Err => |
742 | return Err |
743 | |
744 | ; |
745 | |
746 | // Diff the two trees against each other |
747 | let diff = match git.diff_tree_to_tree |
748 | Ok => diff, |
749 | Err => |
750 | return Err |
751 | oid_old.to_string, |
752 | oid_new.to_string, |
753 | |
754 | .into |
755 | |
756 | ; |
757 | |
758 | // Should be safe to unwrap? |
759 | let stats = diff.stats .unwrap; |
760 | |
761 | // Honestly not quite sure what is going on here, could not find documentation. |
762 | // Print the entire patch |
763 | let mut patch = String new; |
764 | |
765 | diff.print |
766 | patch.push_str; |
767 | true |
768 | |
769 | .unwrap; |
770 | |
771 | Ok |
772 | files_changed: stats.files_changed, |
773 | insertions: stats.insertions, |
774 | deletions: stats.deletions, |
775 | patch, |
776 | |
777 | |
778 | |
779 | async |
780 | &mut self, |
781 | requester: , |
782 | repository: &Repository, |
783 | request: &RepositoryCommitBeforeRequest, |
784 | |
785 | let git = self |
786 | .open_repository_and_check_permissions |
787 | .await?; |
788 | |
789 | // Parse the passed object id |
790 | let oid = match from_str |
791 | Ok => oid, |
792 | Err => |
793 | return Err |
794 | |
795 | ; |
796 | |
797 | // Find the commit using the parsed oid |
798 | let commit = match git.find_commit |
799 | Ok => commit, |
800 | Err => return Err, |
801 | ; |
802 | |
803 | // Get the first parent it has |
804 | let parent = match commit.parent |
805 | Ok => from, |
806 | Err => |
807 | return Err |
808 | |
809 | ; |
810 | |
811 | Ok |
812 | |
813 | |
814 | |
815 | |
816 | |
817 | &mut self, |
818 | _requester: , |
819 | _request: &RepositoryIssuesCountRequest, |
820 | |
821 | todo! |
822 | |
823 | |
824 | |
825 | &mut self, |
826 | _requester: , |
827 | _request: &RepositoryIssueLabelsRequest, |
828 | |
829 | todo! |
830 | |
831 | |
832 | |
833 | &mut self, |
834 | _requester: , |
835 | _request: &RepositoryIssuesRequest, |
836 | |
837 | todo! |
838 | |
839 | |
840 | |
841 | |
842 | |
843 | |
844 | pub repository: String, |
845 | pub name: String, |
846 | pub value: String, |
847 | |
848 |