Incomplete repository branches request
parent: tbd commit: 6872fbf
1 | use Error; |
2 | use async_trait; |
3 | |
4 | use BranchType; |
5 | use ; |
6 | |
7 | use |
8 | AccessList, Commit, DefaultBranch, Description, IssueLabel, LatestCommit, Repository, |
9 | RepositoryChunkLine, RepositoryCommitBeforeRequest, RepositoryDiff, RepositoryDiffFile, |
10 | RepositoryDiffFileChunk, RepositoryDiffFileInfo, RepositoryDiffFileStatus, |
11 | RepositoryDiffPatchRequest, RepositoryDiffRequest, RepositoryFile, RepositoryFileFromIdRequest, |
12 | RepositoryFileFromPathRequest, RepositoryFileInspectRequest, RepositoryIssue, |
13 | RepositoryIssueLabelsRequest, RepositoryIssuesCountRequest, RepositoryIssuesRequest, |
14 | RepositoryObjectType, RepositoryTreeEntry, RepositoryVisibility, Visibility, RepositoryCommitFromIdRequest, RepositoryLastCommitOfFileRequest, RepositoryStatisticsRequest, RepositoryStatistics, RepositoryBranchesRequest, RepositoryBranch, |
15 | ; |
16 | use ; |
17 | use ; |
18 | use ; |
19 | use AuthenticatedUser; |
20 | use Value; |
21 | use PgPool; |
22 | use Deref; |
23 | use |
24 | , |
25 | , | Arc
26 | ; |
27 | use Error; |
28 | use Mutex; |
29 | |
30 | use ; |
31 | |
32 | // TODO: Handle this |
33 | //region database structures |
34 | |
35 | /// Repository in the database |
36 | |
37 | |
38 | #[sqlx(try_from = "String")] |
39 | pub owner_user: User, |
40 | pub name: String, |
41 | pub description: , |
42 | pub visibility: RepositoryVisibility, |
43 | pub default_branch: String, |
44 | |
45 | |
46 | |
47 | // Separate function because "Private" will be expanded later |
48 | /// Checks if the user is allowed to view this repository |
49 | pub async |
50 | &self, |
51 | our_instance: &Instance, |
52 | user: & , |
53 | settings: & , |
54 | |
55 | info! |
56 | "Can user {:?} view repository {}/{}?", |
57 | user, self.owner_user, self.name |
58 | ; |
59 | if matches! |
60 | return true; |
61 | |
62 | |
63 | // User must exist for any further checks to pass |
64 | let user = match user |
65 | Some => user, |
66 | None => return false, |
67 | ; |
68 | |
69 | if *user.deref == self.owner_user |
70 | // owner can always view |
71 | return true; |
72 | |
73 | |
74 | if matches! |
75 | // Check if the user can view |
76 | info!; |
77 | let mut settings = settings.lock .await; |
78 | |
79 | let access_list = settings |
80 | .repository_get |
81 | &Repository |
82 | owner: self.owner_user.clone, |
83 | name: self.name.clone, |
84 | instance: our_instance.clone, |
85 | , |
86 | , | name
87 | |
88 | .await; |
89 | |
90 | info!; |
91 | |
92 | let access_list: AccessList = match access_list |
93 | Ok => from_value .unwrap, |
94 | Err => |
95 | return false; |
96 | |
97 | ; |
98 | |
99 | info!; |
100 | |
101 | access_list |
102 | .0 |
103 | .iter |
104 | .find |
105 | .is_some |
106 | else |
107 | false |
108 | |
109 | |
110 | |
111 | // This is in it's own function because I assume I'll have to add logic to this later |
112 | |
113 | &self, |
114 | repository_directory: &str, |
115 | |
116 | match open |
117 | "{}/{}/{}/{}" |
118 | repository_directory, self.owner_user.instance.url, self.owner_user.username, self.name |
119 | ) |
120 | Ok => Ok, |
121 | Err => |
122 | let err = FailedOpeningFromDisk; |
123 | error!; |
124 | |
125 | Err |
126 | |
127 | |
128 | |
129 | |
130 | |
131 | //endregion |
132 | |
133 | |
134 | |
135 | |
136 | FailedCreatingRepository, |
137 | |
138 | FailedInsertingIntoDatabase, |
139 | |
140 | RepositoryNotFound , |
141 | |
142 | RepositoryAlreadyExists , |
143 | |
144 | CouldNotDeleteFromDisk, |
145 | |
146 | FailedDeletingFromDatabase, |
147 | |
148 | FailedOpeningFromDisk, |
149 | |
150 | RefNotFound, |
151 | |
152 | PathNotFound, |
153 | |
154 | LastCommitNotFound, |
155 | |
156 | InvalidObjectId, |
157 | |
158 | BlobNotFound, |
159 | |
160 | TreeNotFound, |
161 | |
162 | CommitNotFound, |
163 | |
164 | CommitParentNotFound, |
165 | |
166 | FailedDiffing, |
167 | |
168 | |
169 | |
170 | pub pg_pool: PgPool, |
171 | pub repository_folder: String, |
172 | pub instance: Instance, |
173 | pub settings_provider: , |
174 | |
175 | |
176 | |
177 | |
178 | pg_pool: &PgPool, |
179 | repository_folder: &str, |
180 | instance: impl , |
181 | settings_provider: , |
182 | |
183 | Self |
184 | pg_pool: pg_pool.clone, |
185 | repository_folder: repository_folder.to_string, |
186 | instance: instance.to_owned, |
187 | settings_provider, |
188 | |
189 | |
190 | |
191 | pub async |
192 | &self, |
193 | user: &User, |
194 | repository_name: &str, |
195 | |
196 | if let Ok = query_as! |
197 | r#"SELECT owner_user, name, description, visibility as "visibility: _", default_branch FROM repositories WHERE owner_user = $1 AND name = $2"#, |
198 | user.to_string, repository_name |
199 | .fetch_one |
200 | .await |
201 | Ok |
202 | else |
203 | Err |
204 | owner_user: user.to_string, |
205 | name: repository_name.to_string, |
206 | |
207 | |
208 | |
209 | |
210 | pub async |
211 | &self, |
212 | user: &User, |
213 | repository_name: &str, |
214 | |
215 | if let Err = remove_dir_all |
216 | "{}/{}/{}/{}" |
217 | self.repository_folder, user.instance.url, user.username, repository_name |
218 | ) |
219 | let err = CouldNotDeleteFromDisk; |
220 | error! |
221 | "Couldn't delete repository from disk, this is bad! {:?}", |
222 | err |
223 | ; |
224 | |
225 | return Err; |
226 | |
227 | |
228 | // Delete the repository from the database |
229 | match query! |
230 | "DELETE FROM repositories WHERE owner_user = $1 AND name = $2", |
231 | user.to_string, |
232 | repository_name |
233 | |
234 | .execute |
235 | .await |
236 | |
237 | Ok => Ok, |
238 | Err => Err, |
239 | |
240 | |
241 | |
242 | pub async |
243 | &self, |
244 | owner: &User, |
245 | name: &str, |
246 | requester: & , |
247 | |
248 | info! |
249 | "Checking permissions for user {:?} on {}/{}", |
250 | requester, owner, name |
251 | ; |
252 | |
253 | let repository = match self |
254 | .find_by_owner_user_name |
255 | // &request.owner.instance.url, |
256 | owner, name, |
257 | |
258 | .await |
259 | |
260 | Ok => repository, |
261 | Err => return Err, |
262 | ; |
263 | |
264 | if let Some = requester |
265 | if !repository |
266 | .can_user_view_repository |
267 | &self.instance, |
268 | &Some, |
269 | &self.settings_provider, |
270 | |
271 | .await |
272 | |
273 | return Err |
274 | owner_user: repository.owner_user.to_string, |
275 | name: repository.name.clone, |
276 | ; |
277 | |
278 | else if matches! |
279 | // Unauthenticated users can never view private repositories |
280 | |
281 | return Err |
282 | owner_user: repository.owner_user.to_string, |
283 | name: repository.name.clone, |
284 | ; |
285 | |
286 | |
287 | match repository.open_git2_repository |
288 | Ok => Ok, |
289 | Err => return Err, |
290 | |
291 | |
292 | |
293 | // TODO: Find where this fits |
294 | // TODO: Cache this and general repository tree and invalidate select files on push |
295 | // TODO: Find better and faster technique for this |
296 | |
297 | path: &str, |
298 | git: & Repository, |
299 | start_commit: & Commit, |
300 | |
301 | let mut revwalk = git.revwalk?; |
302 | revwalk.set_sorting?; |
303 | revwalk.push?; |
304 | |
305 | for oid in revwalk |
306 | let oid = oid?; |
307 | let commit = git.find_commit?; |
308 | |
309 | // Merge commits have 2 or more parents |
310 | // Commits with 0 parents are handled different because we can't diff against them |
311 | if commit.parent_count == 0 |
312 | return Ok; |
313 | else if commit.parent_count == 1 |
314 | let tree = commit.tree?; |
315 | let last_tree = commit.parent?.tree?; |
316 | |
317 | // Get the diff between the current tree and the last one |
318 | let diff = git.diff_tree_to_tree?; |
319 | |
320 | for dd in diff.deltas |
321 | // Get the path of the current file we're diffing against |
322 | let current_path = dd.new_file .path .unwrap; |
323 | |
324 | // Path or directory |
325 | if current_path.eq || current_path.starts_with |
326 | return Ok; |
327 | |
328 | |
329 | |
330 | |
331 | |
332 | Err? |
333 | |
334 | |
335 | /// Gets the total amount of commits using revwalk |
336 | |
337 | git: & Repository, |
338 | start_commit: & Commit, |
339 | |
340 | // TODO: There must be a better way |
341 | let mut revwalk = git.revwalk?; |
342 | revwalk.set_sorting?; |
343 | revwalk.push?; |
344 | |
345 | Ok |
346 | |
347 | |
348 | |
349 | |
350 | |
351 | async |
352 | &mut self, |
353 | requester: & , |
354 | repository: &Repository, |
355 | |
356 | if let Ok = self |
357 | .find_by_owner_user_name |
358 | .await |
359 | |
360 | Ok |
361 | .can_user_view_repository |
362 | .await |
363 | else |
364 | Ok |
365 | |
366 | |
367 | |
368 | async |
369 | &mut self, |
370 | _user: &AuthenticatedUser, |
371 | request: &RepositoryCreateRequest, |
372 | |
373 | // Check if repository already exists in the database |
374 | if let Ok = self |
375 | .find_by_owner_user_name |
376 | .await |
377 | |
378 | let err = RepositoryAlreadyExists |
379 | owner_user: repository.owner_user.to_string, |
380 | name: repository.name, |
381 | ; |
382 | error!; |
383 | |
384 | return Err; |
385 | |
386 | |
387 | // Insert the repository into the database |
388 | let _ = match query_as! |
389 | r#"INSERT INTO repositories VALUES ($1, $2, $3, $4, $5) RETURNING owner_user, name, description, visibility as "visibility: _", default_branch"#, |
390 | request.owner.to_string, request.name, request.description, request.visibility as _, "master" |
391 | .fetch_one |
392 | .await |
393 | Ok => repository, |
394 | Err => |
395 | let err = FailedInsertingIntoDatabase; |
396 | error!; |
397 | |
398 | return Err; |
399 | |
400 | ; |
401 | |
402 | // Create bare (server side) repository on disk |
403 | match init_bare |
404 | "{}/{}/{}/{}" |
405 | self.repository_folder, |
406 | request.owner.instance.url, |
407 | request.owner.username, |
408 | request.name |
409 | ) |
410 | Ok => |
411 | debug! |
412 | "Created new repository with the name {}/{}/{}", |
413 | request.owner.instance.url, request.owner.username, request.name |
414 | ; |
415 | |
416 | let repository = Repository |
417 | owner: request.owner.clone, |
418 | name: request.name.clone, |
419 | instance: request.instance.as_ref .unwrap_or .clone, |
420 | ; |
421 | |
422 | let mut settings_backend = self.settings_provider.lock .await; |
423 | settings_backend |
424 | .repository_write |
425 | &repository, |
426 | , | name
427 | AnySetting |
428 | to_value |
429 | request.description.clone .unwrap_or_default, |
430 | |
431 | .unwrap, |
432 | , |
433 | |
434 | .await |
435 | .unwrap; |
436 | settings_backend |
437 | .repository_write |
438 | &repository, |
439 | , | name
440 | AnySetting |
441 | unwrap, | to_value .
442 | , |
443 | |
444 | .await |
445 | .unwrap; |
446 | settings_backend |
447 | .repository_write |
448 | &repository, |
449 | , | name
450 | AnySetting |
451 | to_value |
452 | .unwrap, |
453 | , |
454 | |
455 | .await |
456 | .unwrap; |
457 | |
458 | Ok |
459 | |
460 | Err => |
461 | let err = FailedCreatingRepository; |
462 | error!; |
463 | |
464 | // Delete repository from database |
465 | self.delete_by_owner_user_name |
466 | .await?; |
467 | |
468 | // ??? |
469 | Err |
470 | |
471 | |
472 | |
473 | |
474 | async |
475 | &mut self, |
476 | repository: &Repository, |
477 | name: &str, |
478 | |
479 | Ok |
480 | if name == value_name |
481 | from_raw |
482 | else if name == value_name |
483 | from_raw |
484 | else if name == value_name |
485 | from_raw |
486 | else if name == value_name |
487 | from_raw |
488 | else |
489 | return Err; |
490 | |
491 | |
492 | |
493 | |
494 | async |
495 | &mut self, |
496 | repository: &Repository, |
497 | name: &str, |
498 | |
499 | let mut provider = self.settings_provider.lock .await; |
500 | |
501 | Ok |
502 | |
503 | |
504 | async |
505 | &mut self, |
506 | repository: &Repository, |
507 | name: &str, |
508 | setting: &Value, |
509 | |
510 | let mut provider = self.settings_provider.lock .await; |
511 | |
512 | provider |
513 | .repository_write |
514 | .await |
515 | |
516 | |
517 | async |
518 | &mut self, |
519 | requester: & , |
520 | repository: &Repository, |
521 | request: &RepositoryFileInspectRequest, |
522 | |
523 | let git = self |
524 | .open_repository_and_check_permissions |
525 | .await?; |
526 | |
527 | // Try and parse the input as a reference and get the object ID |
528 | let mut tree_id = match &request.rev |
529 | None => |
530 | if let Ok = git.head |
531 | // TODO: Fix for symbolic references |
532 | head.target |
533 | else |
534 | // Nothing in database, render empty tree. |
535 | return Ok; |
536 | |
537 | |
538 | Some => |
539 | // Find the reference, otherwise return GitBackendError |
540 | git.refname_to_id .ok |
541 | |
542 | ; |
543 | |
544 | // If the reference wasn't found, try parsing it as a commit ID |
545 | if tree_id.is_none |
546 | if let Ok = from_str |
547 | tree_id = Some |
548 | |
549 | |
550 | |
551 | // If the commit ID wasn't found, try parsing it as a branch and otherwise return error |
552 | if tree_id.is_none |
553 | match git.find_branch |
554 | Ok => tree_id = branch.get .target, |
555 | Err => |
556 | return Err |
557 | request.rev.clone .unwrap, |
558 | |
559 | .into |
560 | |
561 | |
562 | |
563 | |
564 | // unwrap might be dangerous? |
565 | // Get the commit from the oid |
566 | let commit = git.find_commit .unwrap; |
567 | |
568 | // this is stupid |
569 | let mut current_path = request.rev.clone .unwrap_or_else; |
570 | |
571 | // Get the commit tree |
572 | let git_tree = if let Some = &request.path |
573 | // Add it to our full path string |
574 | current_path.push_str; |
575 | // Get the specified path, return an error if it wasn't found. |
576 | let entry = match commit |
577 | .tree |
578 | .unwrap |
579 | .get_path |
580 | .map_err |
581 | |
582 | Ok => entry, |
583 | Err => return Err, |
584 | ; |
585 | // Turn the entry into a git tree |
586 | entry.to_object .unwrap .as_tree .unwrap .clone |
587 | else |
588 | commit.tree .unwrap |
589 | ; |
590 | |
591 | // Iterate over the git tree and collect it into our own tree types |
592 | let mut tree = git_tree |
593 | .iter |
594 | .map |
595 | let object_type = match entry.kind .unwrap |
596 | => Tree, | Tree
597 | => Blob, | Blob
598 | _ => unreachable!, |
599 | ; |
600 | let mut tree_entry = new |
601 | entry.id .to_string .as_str, |
602 | entry.name .unwrap, |
603 | object_type, |
604 | entry.filemode, |
605 | ; |
606 | |
607 | if request.extra_metadata |
608 | // Get the file size if It's a blob |
609 | let object = entry.to_object .unwrap; |
610 | if let Some = object.as_blob |
611 | tree_entry.size = Some; |
612 | |
613 | |
614 | // Could possibly be done better |
615 | let path = if let Some = current_path.split_once |
616 | format! |
617 | else |
618 | entry.name .unwrap .to_string |
619 | ; |
620 | |
621 | // Get the last commit made to the entry |
622 | if let Ok = |
623 | get_last_commit_of_file |
624 | |
625 | tree_entry.last_commit = Some; |
626 | |
627 | |
628 | |
629 | tree_entry |
630 | |
631 | .; |
632 | |
633 | // Sort the tree alphabetically and with tree first |
634 | tree.sort_unstable_by_key; |
635 | tree.sort_unstable_by_key |
636 | Reverse |
637 | ; |
638 | |
639 | Ok |
640 | |
641 | |
642 | async |
643 | &mut self, |
644 | requester: & , |
645 | repository: &Repository, |
646 | request: &RepositoryFileFromIdRequest, |
647 | |
648 | let git = self |
649 | .open_repository_and_check_permissions |
650 | .await?; |
651 | |
652 | // Parse the passed object id |
653 | let oid = match from_str |
654 | Ok => oid, |
655 | Err => |
656 | return Err |
657 | |
658 | ; |
659 | |
660 | // Find the file and turn it into our own struct |
661 | let file = match git.find_blob |
662 | Ok => RepositoryFile |
663 | id: blob.id .to_string, |
664 | content: blob.content .to_vec, |
665 | binary: blob.is_binary, |
666 | size: blob.size, |
667 | , |
668 | Err => return Err, |
669 | ; |
670 | |
671 | Ok |
672 | |
673 | |
674 | async |
675 | &mut self, |
676 | requester: & , |
677 | repository: &Repository, |
678 | request: &RepositoryFileFromPathRequest, |
679 | |
680 | let git = self |
681 | .open_repository_and_check_permissions |
682 | .await?; |
683 | |
684 | // TODO: Remove duplicate code with repository_file_inspect, most of it is duplicate. |
685 | // Try and parse the input as a reference and get the object ID |
686 | let mut tree_id = match &request.rev |
687 | None => |
688 | if let Ok = git.head |
689 | // TODO: Fix for symbolic references |
690 | head.target |
691 | else |
692 | // Nothing in database, render empty tree. |
693 | return Err |
694 | request.rev.clone .unwrap, |
695 | |
696 | .into; |
697 | |
698 | |
699 | Some => |
700 | // Find the reference, otherwise return GitBackendError |
701 | git.refname_to_id .ok |
702 | |
703 | ; |
704 | |
705 | // If the reference wasn't found, try parsing it as a commit ID |
706 | if tree_id.is_none |
707 | if let Ok = from_str |
708 | tree_id = Some |
709 | |
710 | |
711 | |
712 | // If the commit ID wasn't found, try parsing it as a branch and otherwise return error |
713 | if tree_id.is_none |
714 | match git.find_branch |
715 | Ok => tree_id = branch.get .target, |
716 | Err => |
717 | return Err |
718 | request.rev.clone .unwrap, |
719 | |
720 | .into |
721 | |
722 | |
723 | |
724 | |
725 | // unwrap might be dangerous? |
726 | // Get the commit from the oid |
727 | let commit = git.find_commit .unwrap; |
728 | |
729 | // this is stupid |
730 | let mut current_path = request.rev.clone .unwrap_or_else; |
731 | |
732 | // Add it to our full path string |
733 | current_path.push_str; |
734 | // Get the specified path, return an error if it wasn't found. |
735 | let entry = match commit |
736 | .tree |
737 | .unwrap |
738 | .get_path |
739 | .map_err |
740 | |
741 | Ok => entry, |
742 | Err => return Err, |
743 | ; |
744 | |
745 | // Find the file and turn it into our own struct |
746 | let file = match git.find_blob |
747 | Ok => RepositoryFile |
748 | id: blob.id .to_string, |
749 | content: blob.content .to_vec, |
750 | binary: blob.is_binary, |
751 | size: blob.size, |
752 | , |
753 | Err => |
754 | return Err |
755 | |
756 | ; |
757 | |
758 | Ok |
759 | |
760 | |
761 | async |
762 | &mut self, |
763 | requester: & , |
764 | repository: &Repository, |
765 | request: &RepositoryCommitFromIdRequest, |
766 | |
767 | let git = self |
768 | .open_repository_and_check_permissions |
769 | .await?; |
770 | |
771 | // Parse the passed object ids |
772 | let oid = from_str |
773 | .map_err?; |
774 | |
775 | // Get the commit from the oid |
776 | let commit = git.find_commit .map_err?; |
777 | |
778 | Ok |
779 | |
780 | |
781 | async |
782 | &mut self, |
783 | requester: & , |
784 | repository: &Repository, |
785 | request: &RepositoryLastCommitOfFileRequest, |
786 | |
787 | let git = self |
788 | .open_repository_and_check_permissions |
789 | .await?; |
790 | |
791 | // Parse the passed object ids |
792 | let oid = from_str |
793 | .map_err?; |
794 | |
795 | // Get the commit from the oid |
796 | let commit = git.find_commit .map_err?; |
797 | |
798 | // Find the last commit of the file |
799 | let commit = get_last_commit_of_file?; |
800 | |
801 | Ok |
802 | |
803 | |
804 | async |
805 | &mut self, |
806 | requester: & , |
807 | repository: &Repository, |
808 | request: &RepositoryStatisticsRequest, |
809 | |
810 | let git = self |
811 | .open_repository_and_check_permissions |
812 | .await?; |
813 | |
814 | // TODO: Remove duplicate code with repository_file_inspect, most of it is duplicate. |
815 | // Try and parse the input as a reference and get the object ID |
816 | let mut tree_id = match &request.rev |
817 | None => |
818 | if let Ok = git.head |
819 | // TODO: Fix for symbolic references |
820 | head.target |
821 | else |
822 | // Nothing in database, render empty tree. |
823 | return Err |
824 | request.rev.clone .unwrap, |
825 | |
826 | .into; |
827 | |
828 | |
829 | Some => |
830 | // Find the reference, otherwise return GitBackendError |
831 | git.refname_to_id .ok |
832 | |
833 | ; |
834 | |
835 | // If the reference wasn't found, try parsing it as a commit ID |
836 | if tree_id.is_none |
837 | if let Ok = from_str |
838 | tree_id = Some |
839 | |
840 | |
841 | |
842 | // If the commit ID wasn't found, try parsing it as a branch and otherwise return error |
843 | if tree_id.is_none |
844 | match git.find_branch |
845 | Ok => tree_id = branch.get .target, |
846 | Err => |
847 | return Err |
848 | request.rev.clone .unwrap, |
849 | |
850 | .into |
851 | |
852 | |
853 | |
854 | |
855 | // unwrap might be dangerous? |
856 | // Get the commit from the oid |
857 | let commit = git.find_commit .unwrap; |
858 | |
859 | // Count the amount of branches and tags |
860 | let mut branches = 0; |
861 | let mut tags = 0; |
862 | if let Ok = git.references |
863 | for reference in references.flatten |
864 | if reference.is_branch |
865 | branches += 1; |
866 | else if reference.is_tag |
867 | tags += 1; |
868 | |
869 | |
870 | |
871 | |
872 | Ok |
873 | commits: get_total_commit_count?, |
874 | branches, |
875 | tags, |
876 | |
877 | |
878 | |
879 | async |
880 | &mut self, |
881 | requester: & , |
882 | repository: &Repository, |
883 | _request: &RepositoryBranchesRequest, |
884 | |
885 | let git = self |
886 | .open_repository_and_check_permissions |
887 | .await?; |
888 | |
889 | let mut branches = vec!; |
890 | |
891 | // TODO: Add details like stale and such |
892 | for branch in git.branches? |
893 | let branch = branch?; |
894 | |
895 | let Some = branch.0.name .ok .flatten else |
896 | continue; |
897 | ; |
898 | |
899 | branches.push |
900 | |
901 | |
902 | Ok |
903 | |
904 | |
905 | async |
906 | &mut self, |
907 | requester: & , |
908 | repository: &Repository, |
909 | request: &RepositoryDiffRequest, |
910 | |
911 | let git = self |
912 | .open_repository_and_check_permissions |
913 | .await?; |
914 | |
915 | // Parse the passed object ids |
916 | let oid_old = from_str |
917 | .map_err?; |
918 | let oid_new = from_str |
919 | .map_err?; |
920 | |
921 | // Get the ids associates commits |
922 | let commit_old = git |
923 | .find_commit |
924 | .map_err?; |
925 | let commit_new = git |
926 | .find_commit |
927 | .map_err?; |
928 | |
929 | // Get the commit trees |
930 | let tree_old = commit_old |
931 | .tree |
932 | .map_err?; |
933 | let tree_new = commit_new |
934 | .tree |
935 | .map_err?; |
936 | |
937 | // Diff the two trees against each other |
938 | let diff = git |
939 | .diff_tree_to_tree |
940 | .map_err |
941 | FailedDiffing |
942 | ?; |
943 | |
944 | // Should be safe to unwrap? |
945 | let stats = diff.stats .unwrap; |
946 | let mut files: = vec!; |
947 | |
948 | diff.deltas .enumerate .for_each |
949 | // Parse the old file info from the delta |
950 | let old_file_info = match delta.old_file .exists |
951 | true => Some |
952 | id: delta.old_file .id .to_string, |
953 | path: delta |
954 | .old_file |
955 | .path |
956 | .unwrap |
957 | .to_str |
958 | .unwrap |
959 | .to_string, |
960 | size: delta.old_file .size, |
961 | binary: delta.old_file .is_binary, |
962 | , |
963 | false => None, |
964 | ; |
965 | // Parse the new file info from the delta |
966 | let new_file_info = match delta.new_file .exists |
967 | true => Some |
968 | id: delta.new_file .id .to_string, |
969 | path: delta |
970 | .new_file |
971 | .path |
972 | .unwrap |
973 | .to_str |
974 | .unwrap |
975 | .to_string, |
976 | size: delta.new_file .size, |
977 | binary: delta.new_file .is_binary, |
978 | , |
979 | false => None, |
980 | ; |
981 | |
982 | let mut chunks: = vec!; |
983 | if let Some = from_diff .ok .flatten |
984 | for chunk_num in 0..patch.num_hunks |
985 | if let Ok = patch.hunk |
986 | let mut lines: = vec!; |
987 | |
988 | for line_num in 0..chunk_num_lines |
989 | if let Ok = patch.line_in_hunk |
990 | if let Ok = String from_utf8 |
991 | lines.push |
992 | change_type: line.origin_value .into, |
993 | content: line_utf8, |
994 | old_line_num: line.old_lineno, |
995 | new_line_num: line.new_lineno, |
996 | ; |
997 | |
998 | |
999 | continue; |
1000 | |
1001 | |
1002 | |
1003 | chunks.push |
1004 | header: String from_utf8 .ok, |
1005 | old_start: chunk.old_start, |
1006 | old_lines: chunk.old_lines, |
1007 | new_start: chunk.new_start, |
1008 | new_lines: chunk.new_lines, |
1009 | lines, |
1010 | ; |
1011 | |
1012 | |
1013 | ; |
1014 | |
1015 | let file = RepositoryDiffFile |
1016 | status: from, |
1017 | old_file_info, |
1018 | new_file_info, |
1019 | chunks, |
1020 | ; |
1021 | |
1022 | files.push; |
1023 | ; |
1024 | |
1025 | Ok |
1026 | new_commit: from, |
1027 | files_changed: stats.files_changed, |
1028 | insertions: stats.insertions, |
1029 | deletions: stats.deletions, |
1030 | files, |
1031 | |
1032 | |
1033 | |
1034 | async |
1035 | &mut self, |
1036 | requester: & , |
1037 | repository: &Repository, |
1038 | request: &RepositoryDiffPatchRequest, |
1039 | |
1040 | let git = self |
1041 | .open_repository_and_check_permissions |
1042 | .await?; |
1043 | |
1044 | // Parse the passed object ids |
1045 | let oid_old = from_str |
1046 | .map_err?; |
1047 | let oid_new = from_str |
1048 | .map_err?; |
1049 | |
1050 | // Get the ids associates commits |
1051 | let commit_old = git |
1052 | .find_commit |
1053 | .map_err?; |
1054 | let commit_new = git |
1055 | .find_commit |
1056 | .map_err?; |
1057 | |
1058 | // Get the commit trees |
1059 | let tree_old = commit_old |
1060 | .tree |
1061 | .map_err?; |
1062 | let tree_new = commit_new |
1063 | .tree |
1064 | .map_err?; |
1065 | |
1066 | // Diff the two trees against each other |
1067 | let diff = git |
1068 | .diff_tree_to_tree |
1069 | .map_err |
1070 | FailedDiffing |
1071 | ?; |
1072 | |
1073 | // Print the entire patch |
1074 | let mut patch = String new; |
1075 | |
1076 | diff.print |
1077 | match line.origin |
1078 | '+' | '-' | ' ' => patch.push, |
1079 | _ => |
1080 | |
1081 | patch.push_str; |
1082 | true |
1083 | |
1084 | .unwrap; |
1085 | |
1086 | Ok |
1087 | |
1088 | |
1089 | async |
1090 | &mut self, |
1091 | requester: & , |
1092 | repository: &Repository, |
1093 | request: &RepositoryCommitBeforeRequest, |
1094 | |
1095 | let git = self |
1096 | .open_repository_and_check_permissions |
1097 | .await?; |
1098 | |
1099 | // Parse the passed object id |
1100 | let oid = match from_str |
1101 | Ok => oid, |
1102 | Err => |
1103 | return Err |
1104 | |
1105 | ; |
1106 | |
1107 | // Find the commit using the parsed oid |
1108 | let commit = match git.find_commit |
1109 | Ok => commit, |
1110 | Err => return Err, |
1111 | ; |
1112 | |
1113 | // Get the first parent it has |
1114 | let parent = commit.parent; |
1115 | if let Ok = parent |
1116 | return Ok; |
1117 | else |
1118 | // TODO: See if can be done better |
1119 | // Walk through the repository commit graph starting at our current commit |
1120 | let mut revwalk = git.revwalk?; |
1121 | revwalk.set_sorting?; |
1122 | revwalk.push?; |
1123 | |
1124 | if let Some = revwalk.next |
1125 | if let Ok = next |
1126 | // Find the commit using the parsed oid |
1127 | if let Ok = git.find_commit |
1128 | return Ok; |
1129 | |
1130 | |
1131 | |
1132 | |
1133 | Err |
1134 | |
1135 | |
1136 | |
1137 | |
1138 | |
1139 | |
1140 | &mut self, |
1141 | _requester: & , |
1142 | _request: &RepositoryIssuesCountRequest, |
1143 | |
1144 | todo! |
1145 | |
1146 | |
1147 | |
1148 | &mut self, |
1149 | _requester: & , |
1150 | _request: &RepositoryIssueLabelsRequest, |
1151 | |
1152 | todo! |
1153 | |
1154 | |
1155 | |
1156 | &mut self, |
1157 | _requester: & , |
1158 | _request: &RepositoryIssuesRequest, |
1159 | |
1160 | todo! |
1161 | |
1162 | |
1163 | |
1164 | |
1165 | |
1166 | |
1167 | pub repository: String, |
1168 | pub name: String, |
1169 | pub value: String, |
1170 | |
1171 |