Giterated stack changeover, refactor still incomplete
parent: tbd commit: 8d40dfe
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 | // Unauthenticated users can never view private repositories |
208 | |
209 | return Err |
210 | owner_user: repository.owner_user.to_string, |
211 | name: repository.name.clone, |
212 | ; |
213 | |
214 | |
215 | match repository.open_git2_repository |
216 | Ok => Ok, |
217 | Err => return Err, |
218 | |
219 | |
220 | |
221 | // TODO: Find where this fits |
222 | // TODO: Cache this and general repository tree and invalidate select files on push |
223 | // TODO: Find better and faster technique for this |
224 | |
225 | path: &str, |
226 | git: & Repository, |
227 | start_commit: & Commit, |
228 | |
229 | let mut revwalk = git.revwalk?; |
230 | revwalk.set_sorting?; |
231 | revwalk.push?; |
232 | |
233 | for oid in revwalk |
234 | let oid = oid?; |
235 | let commit = git.find_commit?; |
236 | |
237 | // Merge commits have 2 or more parents |
238 | // Commits with 0 parents are handled different because we can't diff against them |
239 | if commit.parent_count == 0 |
240 | return Ok; |
241 | else if commit.parent_count == 1 |
242 | let tree = commit.tree?; |
243 | let last_tree = commit.parent?.tree?; |
244 | |
245 | // Get the diff between the current tree and the last one |
246 | let diff = git.diff_tree_to_tree?; |
247 | |
248 | for dd in diff.deltas |
249 | // Get the path of the current file we're diffing against |
250 | let current_path = dd.new_file .path .unwrap; |
251 | |
252 | // Path or directory |
253 | if current_path.eq || current_path.starts_with |
254 | return Ok; |
255 | |
256 | |
257 | |
258 | |
259 | |
260 | Err? |
261 | |
262 | |
263 | |
264 | |
265 | |
266 | async |
267 | if let Ok = self |
268 | .find_by_owner_user_name |
269 | .await |
270 | |
271 | Ok |
272 | else |
273 | Ok |
274 | |
275 | |
276 | |
277 | async |
278 | &mut self, |
279 | _user: &User, |
280 | request: &RepositoryCreateRequest, |
281 | |
282 | // Check if repository already exists in the database |
283 | if let Ok = self |
284 | .find_by_owner_user_name |
285 | .await |
286 | |
287 | let err = RepositoryAlreadyExists |
288 | owner_user: repository.owner_user.to_string, |
289 | name: repository.name, |
290 | ; |
291 | error!; |
292 | |
293 | return Err; |
294 | |
295 | |
296 | // Insert the repository into the database |
297 | let _ = match query_as! |
298 | r#"INSERT INTO repositories VALUES ($1, $2, $3, $4, $5) RETURNING owner_user, name, description, visibility as "visibility: _", default_branch"#, |
299 | request.owner.to_string, request.name, request.description, request.visibility as _, "master" |
300 | .fetch_one |
301 | .await |
302 | Ok => repository, |
303 | Err => |
304 | let err = FailedInsertingIntoDatabase; |
305 | error!; |
306 | |
307 | return Err; |
308 | |
309 | ; |
310 | |
311 | // Create bare (server side) repository on disk |
312 | match init_bare |
313 | "{}/{}/{}/{}" |
314 | self.repository_folder, |
315 | request.owner.instance.url, |
316 | request.owner.username, |
317 | request.name |
318 | ) |
319 | Ok => |
320 | debug! |
321 | "Created new repository with the name {}/{}/{}", |
322 | request.owner.instance.url, request.owner.username, request.name |
323 | ; |
324 | Ok |
325 | owner: request.owner.clone, |
326 | name: request.name.clone, |
327 | instance: request.instance.as_ref .unwrap_or .clone, |
328 | |
329 | |
330 | Err => |
331 | let err = FailedCreatingRepository; |
332 | error!; |
333 | |
334 | // Delete repository from database |
335 | self.delete_by_owner_user_name |
336 | .await?; |
337 | |
338 | // ??? |
339 | Err |
340 | |
341 | |
342 | |
343 | |
344 | async |
345 | &mut self, |
346 | repository: &Repository, |
347 | name: &str, |
348 | |
349 | Ok |
350 | if name == value_name |
351 | from_raw |
352 | else if name == value_name |
353 | from_raw |
354 | else if name == value_name |
355 | from_raw |
356 | else if name == value_name |
357 | from_raw |
358 | else |
359 | return Err; |
360 | |
361 | |
362 | |
363 | |
364 | async |
365 | &mut self, |
366 | repository: &Repository, |
367 | name: &str, |
368 | |
369 | let mut provider = self.settings_provider.lock .await; |
370 | |
371 | Ok |
372 | |
373 | |
374 | async |
375 | &mut self, |
376 | repository: &Repository, |
377 | name: &str, |
378 | setting: &Value, |
379 | |
380 | let mut provider = self.settings_provider.lock .await; |
381 | |
382 | provider |
383 | .repository_write |
384 | .await |
385 | |
386 | |
387 | // async fn repository_info( |
388 | // &mut self, |
389 | // requester: Option<&User>, |
390 | // request: &RepositoryInfoRequest, |
391 | // ) -> Result<RepositoryView, Error> { |
392 | // let repository = match self |
393 | // .find_by_owner_user_name( |
394 | // // &request.owner.instance.url, |
395 | // &request.repository.owner, |
396 | // &request.repository.name, |
397 | // ) |
398 | // .await |
399 | // { |
400 | // Ok(repository) => repository, |
401 | // Err(err) => return Err(Box::new(err).into()), |
402 | // }; |
403 | |
404 | // if let Some(requester) = requester { |
405 | // if !repository.can_user_view_repository(Some(&requester)) { |
406 | // return Err(Box::new(GitBackendError::RepositoryNotFound { |
407 | // owner_user: request.repository.owner.to_string(), |
408 | // name: request.repository.name.clone(), |
409 | // }) |
410 | // .into()); |
411 | // } |
412 | // } else if matches!(repository.visibility, RepositoryVisibility::Private) { |
413 | // // Unauthenticated users can never view private repositories |
414 | |
415 | // return Err(Box::new(GitBackendError::RepositoryNotFound { |
416 | // owner_user: request.repository.owner.to_string(), |
417 | // name: request.repository.name.clone(), |
418 | // }) |
419 | // .into()); |
420 | // } |
421 | |
422 | // let git = match repository.open_git2_repository(&self.repository_folder) { |
423 | // Ok(git) => git, |
424 | // Err(err) => return Err(Box::new(err).into()), |
425 | // }; |
426 | |
427 | // let rev_name = match &request.rev { |
428 | // None => { |
429 | // if let Ok(head) = git.head() { |
430 | // head.name().unwrap().to_string() |
431 | // } else { |
432 | // // Nothing in database, render empty tree. |
433 | // return Ok(RepositoryView { |
434 | // name: repository.name, |
435 | // owner: request.repository.owner.clone(), |
436 | // description: repository.description, |
437 | // visibility: repository.visibility, |
438 | // default_branch: repository.default_branch, |
439 | // latest_commit: None, |
440 | // tree_rev: None, |
441 | // tree: vec![], |
442 | // }); |
443 | // } |
444 | // } |
445 | // Some(rev_name) => { |
446 | // // Find the reference, otherwise return GitBackendError |
447 | // match git |
448 | // .find_reference(format!("refs/heads/{}", rev_name).as_str()) |
449 | // .map_err(|_| GitBackendError::RefNotFound(rev_name.to_string())) |
450 | // { |
451 | // Ok(reference) => reference.name().unwrap().to_string(), |
452 | // Err(err) => return Err(Box::new(err).into()), |
453 | // } |
454 | // } |
455 | // }; |
456 | |
457 | // // Get the git object as a commit |
458 | // let rev = match git |
459 | // .revparse_single(rev_name.as_str()) |
460 | // .map_err(|_| GitBackendError::RefNotFound(rev_name.to_string())) |
461 | // { |
462 | // Ok(rev) => rev, |
463 | // Err(err) => return Err(Box::new(err).into()), |
464 | // }; |
465 | // let commit = rev.as_commit().unwrap(); |
466 | |
467 | // // this is stupid |
468 | // let mut current_path = rev_name.replace("refs/heads/", ""); |
469 | |
470 | // // Get the commit tree |
471 | // let git_tree = if let Some(path) = &request.path { |
472 | // // Add it to our full path string |
473 | // current_path.push_str(format!("/{}", path).as_str()); |
474 | // // Get the specified path, return an error if it wasn't found. |
475 | // let entry = match commit |
476 | // .tree() |
477 | // .unwrap() |
478 | // .get_path(&PathBuf::from(path)) |
479 | // .map_err(|_| GitBackendError::PathNotFound(path.to_string())) |
480 | // { |
481 | // Ok(entry) => entry, |
482 | // Err(err) => return Err(Box::new(err).into()), |
483 | // }; |
484 | // // Turn the entry into a git tree |
485 | // entry.to_object(&git).unwrap().as_tree().unwrap().clone() |
486 | // } else { |
487 | // commit.tree().unwrap() |
488 | // }; |
489 | |
490 | // // Iterate over the git tree and collect it into our own tree types |
491 | // let mut tree = git_tree |
492 | // .iter() |
493 | // .map(|entry| { |
494 | // let object_type = match entry.kind().unwrap() { |
495 | // ObjectType::Tree => RepositoryObjectType::Tree, |
496 | // ObjectType::Blob => RepositoryObjectType::Blob, |
497 | // _ => unreachable!(), |
498 | // }; |
499 | // let mut tree_entry = |
500 | // RepositoryTreeEntry::new(entry.name().unwrap(), object_type, entry.filemode()); |
501 | |
502 | // if request.extra_metadata { |
503 | // // Get the file size if It's a blob |
504 | // let object = entry.to_object(&git).unwrap(); |
505 | // if let Some(blob) = object.as_blob() { |
506 | // tree_entry.size = Some(blob.size()); |
507 | // } |
508 | |
509 | // // Could possibly be done better |
510 | // let path = if let Some(path) = current_path.split_once('/') { |
511 | // format!("{}/{}", path.1, entry.name().unwrap()) |
512 | // } else { |
513 | // entry.name().unwrap().to_string() |
514 | // }; |
515 | |
516 | // // Get the last commit made to the entry |
517 | // if let Ok(last_commit) = |
518 | // GitBackend::get_last_commit_of_file(&path, &git, commit) |
519 | // { |
520 | // tree_entry.last_commit = Some(last_commit); |
521 | // } |
522 | // } |
523 | |
524 | // tree_entry |
525 | // }) |
526 | // .collect::<Vec<RepositoryTreeEntry>>(); |
527 | |
528 | // // Sort the tree alphabetically and with tree first |
529 | // tree.sort_unstable_by_key(|entry| entry.name.to_lowercase()); |
530 | // tree.sort_unstable_by_key(|entry| { |
531 | // std::cmp::Reverse(format!("{:?}", entry.object_type).to_lowercase()) |
532 | // }); |
533 | |
534 | // Ok(RepositoryView { |
535 | // name: repository.name, |
536 | // owner: request.repository.owner.clone(), |
537 | // description: repository.description, |
538 | // visibility: repository.visibility, |
539 | // default_branch: repository.default_branch, |
540 | // latest_commit: Some(Commit::from(commit.clone())), |
541 | // tree_rev: Some(rev_name), |
542 | // tree, |
543 | // }) |
544 | // } |
545 | |
546 | async |
547 | &mut self, |
548 | requester: , |
549 | repository: &Repository, |
550 | request: &RepositoryFileInspectRequest, |
551 | |
552 | let git = self |
553 | .open_repository_and_check_permissions |
554 | .await?; |
555 | |
556 | // Try and parse the input as a reference and get the object ID |
557 | let mut tree_id = match &request.rev |
558 | None => |
559 | if let Ok = git.head |
560 | // TODO: Fix for symbolic references |
561 | head.target |
562 | else |
563 | // Nothing in database, render empty tree. |
564 | return Ok; |
565 | |
566 | |
567 | Some => |
568 | // Find the reference, otherwise return GitBackendError |
569 | git.refname_to_id .ok |
570 | |
571 | ; |
572 | |
573 | // If the reference wasn't found, try parsing it as a commit ID |
574 | if tree_id.is_none |
575 | if let Ok = from_str |
576 | tree_id = Some |
577 | |
578 | |
579 | |
580 | // If the commit ID wasn't found, try parsing it as a branch and otherwise return error |
581 | if tree_id.is_none |
582 | match git.find_branch |
583 | Ok => tree_id = branch.get .target, |
584 | Err => |
585 | return Err |
586 | request.rev.clone .unwrap, |
587 | |
588 | .into |
589 | |
590 | |
591 | |
592 | |
593 | // unwrap might be dangerous? |
594 | // Get the commit from the oid |
595 | let commit = git.find_commit .unwrap; |
596 | |
597 | // this is stupid |
598 | let mut current_path = request.rev.clone .unwrap_or_else; |
599 | |
600 | // Get the commit tree |
601 | let git_tree = if let Some = &request.path |
602 | // Add it to our full path string |
603 | current_path.push_str; |
604 | // Get the specified path, return an error if it wasn't found. |
605 | let entry = match commit |
606 | .tree |
607 | .unwrap |
608 | .get_path |
609 | .map_err |
610 | |
611 | Ok => entry, |
612 | Err => return Err, |
613 | ; |
614 | // Turn the entry into a git tree |
615 | entry.to_object .unwrap .as_tree .unwrap .clone |
616 | else |
617 | commit.tree .unwrap |
618 | ; |
619 | |
620 | // Iterate over the git tree and collect it into our own tree types |
621 | let mut tree = git_tree |
622 | .iter |
623 | .map |
624 | let object_type = match entry.kind .unwrap |
625 | => Tree, | Tree
626 | => Blob, | Blob
627 | _ => unreachable!, |
628 | ; |
629 | let mut tree_entry = new |
630 | entry.id .to_string .as_str, |
631 | entry.name .unwrap, |
632 | object_type, |
633 | entry.filemode, |
634 | ; |
635 | |
636 | if request.extra_metadata |
637 | // Get the file size if It's a blob |
638 | let object = entry.to_object .unwrap; |
639 | if let Some = object.as_blob |
640 | tree_entry.size = Some; |
641 | |
642 | |
643 | // Could possibly be done better |
644 | let path = if let Some = current_path.split_once |
645 | format! |
646 | else |
647 | entry.name .unwrap .to_string |
648 | ; |
649 | |
650 | // Get the last commit made to the entry |
651 | if let Ok = |
652 | get_last_commit_of_file |
653 | |
654 | tree_entry.last_commit = Some; |
655 | |
656 | |
657 | |
658 | tree_entry |
659 | |
660 | .; |
661 | |
662 | // Sort the tree alphabetically and with tree first |
663 | tree.sort_unstable_by_key; |
664 | tree.sort_unstable_by_key |
665 | Reverse |
666 | ; |
667 | |
668 | Ok |
669 | |
670 | |
671 | async |
672 | &mut self, |
673 | requester: , |
674 | repository: &Repository, |
675 | request: &RepositoryFileFromIdRequest, |
676 | |
677 | let git = self |
678 | .open_repository_and_check_permissions |
679 | .await?; |
680 | |
681 | // Parse the passed object id |
682 | let oid = match from_str |
683 | Ok => oid, |
684 | Err => |
685 | return Err |
686 | |
687 | ; |
688 | |
689 | // Find the file and turn it into our own struct |
690 | let file = match git.find_blob |
691 | Ok => RepositoryFile |
692 | content: blob.content .to_vec, |
693 | binary: blob.is_binary, |
694 | size: blob.size, |
695 | , |
696 | Err => return Err, |
697 | ; |
698 | |
699 | Ok |
700 | |
701 | |
702 | async |
703 | &mut self, |
704 | requester: , |
705 | repository: &Repository, |
706 | request: &RepositoryDiffRequest, |
707 | |
708 | let git = self |
709 | .open_repository_and_check_permissions |
710 | .await?; |
711 | |
712 | // Parse the passed object ids |
713 | let oid_old = from_str |
714 | .map_err?; |
715 | let oid_new = from_str |
716 | .map_err?; |
717 | |
718 | // Get the ids associates commits |
719 | let commit_old = git |
720 | .find_commit |
721 | .map_err?; |
722 | let commit_new = git |
723 | .find_commit |
724 | .map_err?; |
725 | |
726 | // Get the commit trees |
727 | let tree_old = commit_old |
728 | .tree |
729 | .map_err?; |
730 | let tree_new = commit_new |
731 | .tree |
732 | .map_err?; |
733 | |
734 | // Diff the two trees against each other |
735 | let diff = git |
736 | .diff_tree_to_tree |
737 | .map_err |
738 | FailedDiffing |
739 | ?; |
740 | |
741 | // Should be safe to unwrap? |
742 | let stats = diff.stats .unwrap; |
743 | |
744 | // Honestly not quite sure what is going on here, could not find documentation. |
745 | // Print the entire patch |
746 | let mut patch = String new; |
747 | |
748 | diff.print |
749 | match line.origin |
750 | '+' | '-' | ' ' => patch.push, |
751 | _ => |
752 | |
753 | patch.push_str; |
754 | true |
755 | |
756 | .unwrap; |
757 | |
758 | Ok |
759 | files_changed: stats.files_changed, |
760 | insertions: stats.insertions, |
761 | deletions: stats.deletions, |
762 | patch, |
763 | |
764 | |
765 | |
766 | async |
767 | &mut self, |
768 | requester: , |
769 | repository: &Repository, |
770 | request: &RepositoryCommitBeforeRequest, |
771 | |
772 | let git = self |
773 | .open_repository_and_check_permissions |
774 | .await?; |
775 | |
776 | // Parse the passed object id |
777 | let oid = match from_str |
778 | Ok => oid, |
779 | Err => |
780 | return Err |
781 | |
782 | ; |
783 | |
784 | // Find the commit using the parsed oid |
785 | let commit = match git.find_commit |
786 | Ok => commit, |
787 | Err => return Err, |
788 | ; |
789 | |
790 | // Get the first parent it has |
791 | let parent = match commit.parent |
792 | Ok => from, |
793 | Err => |
794 | return Err |
795 | |
796 | ; |
797 | |
798 | Ok |
799 | |
800 | |
801 | |
802 | |
803 | |
804 | &mut self, |
805 | _requester: , |
806 | _request: &RepositoryIssuesCountRequest, |
807 | |
808 | todo! |
809 | |
810 | |
811 | |
812 | &mut self, |
813 | _requester: , |
814 | _request: &RepositoryIssueLabelsRequest, |
815 | |
816 | todo! |
817 | |
818 | |
819 | |
820 | &mut self, |
821 | _requester: , |
822 | _request: &RepositoryIssuesRequest, |
823 | |
824 | todo! |
825 | |
826 | |
827 | |
828 | |
829 | |
830 | |
831 | pub repository: String, |
832 | pub name: String, |
833 | pub value: String, |
834 | |
835 |