Fix handling stack
parent: tbd commit: c53b026
1 | use Error; |
2 | use async_trait; |
3 | use StreamExt; |
4 | |
5 | use ; |
6 | |
7 | use |
8 | Commit, DefaultBranch, Description, IssueLabel, LatestCommit, Repository, |
9 | RepositoryFileInspectRequest, RepositoryIssue, RepositoryIssueLabelsRequest, |
10 | RepositoryIssuesCountRequest, RepositoryIssuesRequest, RepositoryObjectType, |
11 | RepositoryTreeEntry, RepositoryVisibility, Visibility, |
12 | ; |
13 | use ; |
14 | use ; |
15 | use ; |
16 | use Value; |
17 | use PgPool; |
18 | use |
19 | , |
20 | , | Arc
21 | ; |
22 | use Error; |
23 | use Mutex; |
24 | |
25 | use ; |
26 | |
27 | // TODO: Handle this |
28 | //region database structures |
29 | |
30 | /// Repository in the database |
31 | |
32 | |
33 | #[sqlx(try_from = "String")] |
34 | pub owner_user: User, |
35 | pub name: String, |
36 | pub description: , |
37 | pub visibility: RepositoryVisibility, |
38 | pub default_branch: String, |
39 | |
40 | |
41 | |
42 | // Separate function because "Private" will be expanded later |
43 | /// Checks if the user is allowed to view this repository |
44 | |
45 | !matches! |
46 | || |
47 | && Some == user |
48 | |
49 | |
50 | // This is in it's own function because I assume I'll have to add logic to this later |
51 | |
52 | &self, |
53 | repository_directory: &str, |
54 | |
55 | match open |
56 | "{}/{}/{}/{}" |
57 | repository_directory, self.owner_user.instance.url, self.owner_user.username, self.name |
58 | ) |
59 | Ok => Ok, |
60 | Err => |
61 | let err = FailedOpeningFromDisk; |
62 | error!; |
63 | |
64 | Err |
65 | |
66 | |
67 | |
68 | |
69 | |
70 | //endregion |
71 | |
72 | |
73 | |
74 | |
75 | FailedCreatingRepository, |
76 | |
77 | FailedInsertingIntoDatabase, |
78 | |
79 | RepositoryNotFound , |
80 | |
81 | RepositoryAlreadyExists , |
82 | |
83 | CouldNotDeleteFromDisk, |
84 | |
85 | FailedDeletingFromDatabase, |
86 | |
87 | FailedOpeningFromDisk, |
88 | |
89 | RefNotFound, |
90 | |
91 | PathNotFound, |
92 | |
93 | LastCommitNotFound, |
94 | |
95 | |
96 | |
97 | pub pg_pool: PgPool, |
98 | pub repository_folder: String, |
99 | pub instance: Instance, |
100 | pub settings_provider: , |
101 | |
102 | |
103 | |
104 | |
105 | pg_pool: &PgPool, |
106 | repository_folder: &str, |
107 | instance: impl , |
108 | settings_provider: , |
109 | |
110 | Self |
111 | pg_pool: pg_pool.clone, |
112 | repository_folder: repository_folder.to_string, |
113 | instance: instance.to_owned, |
114 | settings_provider, |
115 | |
116 | |
117 | |
118 | pub async |
119 | &self, |
120 | user: &User, |
121 | repository_name: &str, |
122 | |
123 | if let Ok = query_as! |
124 | r#"SELECT owner_user, name, description, visibility as "visibility: _", default_branch FROM repositories WHERE owner_user = $1 AND name = $2"#, |
125 | user.to_string, repository_name |
126 | .fetch_one |
127 | .await |
128 | Ok |
129 | else |
130 | Err |
131 | owner_user: user.to_string, |
132 | name: repository_name.to_string, |
133 | |
134 | |
135 | |
136 | |
137 | pub async |
138 | &self, |
139 | user: &User, |
140 | repository_name: &str, |
141 | |
142 | if let Err = remove_dir_all |
143 | "{}/{}/{}/{}" |
144 | self.repository_folder, user.instance.url, user.username, repository_name |
145 | ) |
146 | let err = CouldNotDeleteFromDisk; |
147 | error! |
148 | "Couldn't delete repository from disk, this is bad! {:?}", |
149 | err |
150 | ; |
151 | |
152 | return Err; |
153 | |
154 | |
155 | // Delete the repository from the database |
156 | match query! |
157 | "DELETE FROM repositories WHERE owner_user = $1 AND name = $2", |
158 | user.to_string, |
159 | repository_name |
160 | |
161 | .execute |
162 | .await |
163 | |
164 | Ok => Ok, |
165 | Err => Err, |
166 | |
167 | |
168 | |
169 | // TODO: Find where this fits |
170 | // TODO: Cache this and general repository tree and invalidate select files on push |
171 | // TODO: Find better and faster technique for this |
172 | |
173 | path: &str, |
174 | git: & Repository, |
175 | start_commit: & Commit, |
176 | |
177 | let mut revwalk = git.revwalk?; |
178 | revwalk.set_sorting?; |
179 | revwalk.push?; |
180 | |
181 | for oid in revwalk |
182 | let oid = oid?; |
183 | let commit = git.find_commit?; |
184 | |
185 | // Merge commits have 2 or more parents |
186 | // Commits with 0 parents are handled different because we can't diff against them |
187 | if commit.parent_count == 0 |
188 | return Ok; |
189 | else if commit.parent_count == 1 |
190 | let tree = commit.tree?; |
191 | let last_tree = commit.parent?.tree?; |
192 | |
193 | // Get the diff between the current tree and the last one |
194 | let diff = git.diff_tree_to_tree?; |
195 | |
196 | for dd in diff.deltas |
197 | // Get the path of the current file we're diffing against |
198 | let current_path = dd.new_file .path .unwrap; |
199 | |
200 | // Path or directory |
201 | if current_path.eq || current_path.starts_with |
202 | return Ok; |
203 | |
204 | |
205 | |
206 | |
207 | |
208 | Err? |
209 | |
210 | |
211 | |
212 | |
213 | |
214 | async |
215 | if let Ok = self |
216 | .find_by_owner_user_name |
217 | .await |
218 | |
219 | Ok |
220 | else |
221 | Ok |
222 | |
223 | |
224 | |
225 | async |
226 | &mut self, |
227 | _user: &User, |
228 | request: &RepositoryCreateRequest, |
229 | |
230 | // Check if repository already exists in the database |
231 | if let Ok = self |
232 | .find_by_owner_user_name |
233 | .await |
234 | |
235 | let err = RepositoryAlreadyExists |
236 | owner_user: repository.owner_user.to_string, |
237 | name: repository.name, |
238 | ; |
239 | error!; |
240 | |
241 | return Err; |
242 | |
243 | |
244 | // Insert the repository into the database |
245 | let _ = match query_as! |
246 | r#"INSERT INTO repositories VALUES ($1, $2, $3, $4, $5) RETURNING owner_user, name, description, visibility as "visibility: _", default_branch"#, |
247 | request.owner.to_string, request.name, request.description, request.visibility as _, "master" |
248 | .fetch_one |
249 | .await |
250 | Ok => repository, |
251 | Err => |
252 | let err = FailedInsertingIntoDatabase; |
253 | error!; |
254 | |
255 | return Err; |
256 | |
257 | ; |
258 | |
259 | // Create bare (server side) repository on disk |
260 | match init_bare |
261 | "{}/{}/{}/{}" |
262 | self.repository_folder, |
263 | request.owner.instance.url, |
264 | request.owner.username, |
265 | request.name |
266 | ) |
267 | Ok => |
268 | debug! |
269 | "Created new repository with the name {}/{}/{}", |
270 | request.owner.instance.url, request.owner.username, request.name |
271 | ; |
272 | Ok |
273 | owner: request.owner.clone, |
274 | name: request.name.clone, |
275 | instance: request.instance.as_ref .unwrap_or .clone, |
276 | |
277 | |
278 | Err => |
279 | let err = FailedCreatingRepository; |
280 | error!; |
281 | |
282 | // Delete repository from database |
283 | self.delete_by_owner_user_name |
284 | .await?; |
285 | |
286 | // ??? |
287 | Err |
288 | |
289 | |
290 | |
291 | |
292 | async |
293 | &mut self, |
294 | repository: &Repository, |
295 | name: &str, |
296 | |
297 | Ok |
298 | if name == value_name |
299 | from_raw |
300 | else if name == value_name |
301 | from_raw |
302 | else if name == value_name |
303 | from_raw |
304 | else if name == value_name |
305 | from_raw |
306 | else |
307 | return Err; |
308 | |
309 | |
310 | |
311 | |
312 | async |
313 | &mut self, |
314 | repository: &Repository, |
315 | name: &str, |
316 | |
317 | let mut provider = self.settings_provider.lock .await; |
318 | |
319 | Ok |
320 | |
321 | |
322 | async |
323 | &mut self, |
324 | repository: &Repository, |
325 | name: &str, |
326 | setting: &Value, |
327 | |
328 | let mut provider = self.settings_provider.lock .await; |
329 | |
330 | provider |
331 | .repository_write |
332 | .await |
333 | |
334 | |
335 | // async fn repository_info( |
336 | // &mut self, |
337 | // requester: Option<&User>, |
338 | // request: &RepositoryInfoRequest, |
339 | // ) -> Result<RepositoryView, Error> { |
340 | // let repository = match self |
341 | // .find_by_owner_user_name( |
342 | // // &request.owner.instance.url, |
343 | // &request.repository.owner, |
344 | // &request.repository.name, |
345 | // ) |
346 | // .await |
347 | // { |
348 | // Ok(repository) => repository, |
349 | // Err(err) => return Err(Box::new(err).into()), |
350 | // }; |
351 | |
352 | // if let Some(requester) = requester { |
353 | // if !repository.can_user_view_repository(Some(&requester)) { |
354 | // return Err(Box::new(GitBackendError::RepositoryNotFound { |
355 | // owner_user: request.repository.owner.to_string(), |
356 | // name: request.repository.name.clone(), |
357 | // }) |
358 | // .into()); |
359 | // } |
360 | // } else if matches!(repository.visibility, RepositoryVisibility::Private) { |
361 | // info!("Unauthenticated"); |
362 | // // Unauthenticated users can never view private repositories |
363 | |
364 | // return Err(Box::new(GitBackendError::RepositoryNotFound { |
365 | // owner_user: request.repository.owner.to_string(), |
366 | // name: request.repository.name.clone(), |
367 | // }) |
368 | // .into()); |
369 | // } |
370 | |
371 | // let git = match repository.open_git2_repository(&self.repository_folder) { |
372 | // Ok(git) => git, |
373 | // Err(err) => return Err(Box::new(err).into()), |
374 | // }; |
375 | |
376 | // let rev_name = match &request.rev { |
377 | // None => { |
378 | // if let Ok(head) = git.head() { |
379 | // head.name().unwrap().to_string() |
380 | // } else { |
381 | // // Nothing in database, render empty tree. |
382 | // return Ok(RepositoryView { |
383 | // name: repository.name, |
384 | // owner: request.repository.owner.clone(), |
385 | // description: repository.description, |
386 | // visibility: repository.visibility, |
387 | // default_branch: repository.default_branch, |
388 | // latest_commit: None, |
389 | // tree_rev: None, |
390 | // tree: vec![], |
391 | // }); |
392 | // } |
393 | // } |
394 | // Some(rev_name) => { |
395 | // // Find the reference, otherwise return GitBackendError |
396 | // match git |
397 | // .find_reference(format!("refs/heads/{}", rev_name).as_str()) |
398 | // .map_err(|_| GitBackendError::RefNotFound(rev_name.to_string())) |
399 | // { |
400 | // Ok(reference) => reference.name().unwrap().to_string(), |
401 | // Err(err) => return Err(Box::new(err).into()), |
402 | // } |
403 | // } |
404 | // }; |
405 | |
406 | // // Get the git object as a commit |
407 | // let rev = match git |
408 | // .revparse_single(rev_name.as_str()) |
409 | // .map_err(|_| GitBackendError::RefNotFound(rev_name.to_string())) |
410 | // { |
411 | // Ok(rev) => rev, |
412 | // Err(err) => return Err(Box::new(err).into()), |
413 | // }; |
414 | // let commit = rev.as_commit().unwrap(); |
415 | |
416 | // // this is stupid |
417 | // let mut current_path = rev_name.replace("refs/heads/", ""); |
418 | |
419 | // // Get the commit tree |
420 | // let git_tree = if let Some(path) = &request.path { |
421 | // // Add it to our full path string |
422 | // current_path.push_str(format!("/{}", path).as_str()); |
423 | // // Get the specified path, return an error if it wasn't found. |
424 | // let entry = match commit |
425 | // .tree() |
426 | // .unwrap() |
427 | // .get_path(&PathBuf::from(path)) |
428 | // .map_err(|_| GitBackendError::PathNotFound(path.to_string())) |
429 | // { |
430 | // Ok(entry) => entry, |
431 | // Err(err) => return Err(Box::new(err).into()), |
432 | // }; |
433 | // // Turn the entry into a git tree |
434 | // entry.to_object(&git).unwrap().as_tree().unwrap().clone() |
435 | // } else { |
436 | // commit.tree().unwrap() |
437 | // }; |
438 | |
439 | // // Iterate over the git tree and collect it into our own tree types |
440 | // let mut tree = git_tree |
441 | // .iter() |
442 | // .map(|entry| { |
443 | // let object_type = match entry.kind().unwrap() { |
444 | // ObjectType::Tree => RepositoryObjectType::Tree, |
445 | // ObjectType::Blob => RepositoryObjectType::Blob, |
446 | // _ => unreachable!(), |
447 | // }; |
448 | // let mut tree_entry = |
449 | // RepositoryTreeEntry::new(entry.name().unwrap(), object_type, entry.filemode()); |
450 | |
451 | // if request.extra_metadata { |
452 | // // Get the file size if It's a blob |
453 | // let object = entry.to_object(&git).unwrap(); |
454 | // if let Some(blob) = object.as_blob() { |
455 | // tree_entry.size = Some(blob.size()); |
456 | // } |
457 | |
458 | // // Could possibly be done better |
459 | // let path = if let Some(path) = current_path.split_once('/') { |
460 | // format!("{}/{}", path.1, entry.name().unwrap()) |
461 | // } else { |
462 | // entry.name().unwrap().to_string() |
463 | // }; |
464 | |
465 | // // Get the last commit made to the entry |
466 | // if let Ok(last_commit) = |
467 | // GitBackend::get_last_commit_of_file(&path, &git, commit) |
468 | // { |
469 | // tree_entry.last_commit = Some(last_commit); |
470 | // } |
471 | // } |
472 | |
473 | // tree_entry |
474 | // }) |
475 | // .collect::<Vec<RepositoryTreeEntry>>(); |
476 | |
477 | // // Sort the tree alphabetically and with tree first |
478 | // tree.sort_unstable_by_key(|entry| entry.name.to_lowercase()); |
479 | // tree.sort_unstable_by_key(|entry| { |
480 | // std::cmp::Reverse(format!("{:?}", entry.object_type).to_lowercase()) |
481 | // }); |
482 | |
483 | // Ok(RepositoryView { |
484 | // name: repository.name, |
485 | // owner: request.repository.owner.clone(), |
486 | // description: repository.description, |
487 | // visibility: repository.visibility, |
488 | // default_branch: repository.default_branch, |
489 | // latest_commit: Some(Commit::from(commit.clone())), |
490 | // tree_rev: Some(rev_name), |
491 | // tree, |
492 | // }) |
493 | // } |
494 | |
495 | async |
496 | &mut self, |
497 | requester: , |
498 | repository: &Repository, |
499 | request: &RepositoryFileInspectRequest, |
500 | |
501 | let repository = match self |
502 | .find_by_owner_user_name |
503 | // &request.owner.instance.url, |
504 | &repository.owner, |
505 | &repository.name, |
506 | |
507 | .await |
508 | |
509 | Ok => repository, |
510 | Err => return Err, |
511 | ; |
512 | |
513 | if let Some = requester |
514 | if !repository.can_user_view_repository |
515 | return Err |
516 | owner_user: repository.owner_user.to_string, |
517 | name: repository.name.clone, |
518 | |
519 | .into; |
520 | |
521 | else if matches! |
522 | info!; |
523 | // Unauthenticated users can never view private repositories |
524 | |
525 | return Err |
526 | owner_user: repository.owner_user.to_string, |
527 | name: repository.name.clone, |
528 | |
529 | .into; |
530 | |
531 | |
532 | let git = match repository.open_git2_repository |
533 | Ok => git, |
534 | Err => return Err, |
535 | ; |
536 | |
537 | let rev_name = match &request.rev |
538 | None => |
539 | if let Ok = git.head |
540 | head.name .unwrap .to_string |
541 | else |
542 | // Nothing in database, render empty tree. |
543 | return Ok; |
544 | |
545 | |
546 | Some => |
547 | // Find the reference, otherwise return GitBackendError |
548 | match git |
549 | .find_reference |
550 | .map_err |
551 | |
552 | Ok => reference.name .unwrap .to_string, |
553 | Err => return Err, |
554 | |
555 | |
556 | ; |
557 | |
558 | // Get the git object as a commit |
559 | let rev = match git |
560 | .revparse_single |
561 | .map_err |
562 | |
563 | Ok => rev, |
564 | Err => return Err, |
565 | ; |
566 | let commit = rev.as_commit .unwrap; |
567 | |
568 | // this is stupid |
569 | let mut current_path = rev_name.replace; |
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 = |
601 | ; | new
602 | |
603 | if request.extra_metadata |
604 | // Get the file size if It's a blob |
605 | let object = entry.to_object .unwrap; |
606 | if let Some = object.as_blob |
607 | tree_entry.size = Some; |
608 | |
609 | |
610 | // Could possibly be done better |
611 | let path = if let Some = current_path.split_once |
612 | format! |
613 | else |
614 | entry.name .unwrap .to_string |
615 | ; |
616 | |
617 | // Get the last commit made to the entry |
618 | if let Ok = |
619 | get_last_commit_of_file |
620 | |
621 | tree_entry.last_commit = Some; |
622 | |
623 | |
624 | |
625 | tree_entry |
626 | |
627 | .; |
628 | |
629 | // Sort the tree alphabetically and with tree first |
630 | tree.sort_unstable_by_key; |
631 | tree.sort_unstable_by_key |
632 | Reverse |
633 | ; |
634 | |
635 | Ok |
636 | |
637 | |
638 | |
639 | |
640 | |
641 | &mut self, |
642 | _requester: , |
643 | _request: &RepositoryIssuesCountRequest, |
644 | |
645 | todo! |
646 | |
647 | |
648 | |
649 | &mut self, |
650 | _requester: , |
651 | _request: &RepositoryIssueLabelsRequest, |
652 | |
653 | todo! |
654 | |
655 | |
656 | |
657 | &mut self, |
658 | _requester: , |
659 | _request: &RepositoryIssuesRequest, |
660 | |
661 | todo! |
662 | |
663 | |
664 | |
665 | |
666 | |
667 | pub repository: String, |
668 | pub name: String, |
669 | pub value: String, |
670 | |
671 |