Major post-refactor cleanup
parent: tbd commit: f90d7fb
1 | use Error; |
2 | use async_trait; |
3 | use StreamExt; |
4 | use ObjectType; |
5 | use ; |
6 | use ; |
7 | use Error; |
8 | |
9 | use crate Instance; |
10 | use crate |
11 | Commit, Repository, RepositoryObjectType, RepositorySummary, RepositoryTreeEntry, |
12 | RepositoryVisibility, |
13 | ; |
14 | use crate User; |
15 | use crate:: |
16 | |
17 | CreateRepositoryRequest, CreateRepositoryResponse, RepositoryFileInspectRequest, |
18 | RepositoryFileInspectionResponse, RepositoryInfoRequest, RepositoryIssueLabelsRequest, |
19 | RepositoryIssueLabelsResponse, RepositoryIssuesCountRequest, RepositoryIssuesCountResponse, |
20 | RepositoryIssuesRequest, RepositoryIssuesResponse, |
21 | , |
22 | , | RepositoryView
23 | ; |
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 | |
101 | |
102 | |
103 | |
104 | pg_pool: &PgPool, |
105 | repository_folder: &str, |
106 | instance: impl , |
107 | |
108 | Self |
109 | pg_pool: pg_pool.clone, |
110 | repository_folder: repository_folder.to_string, |
111 | instance: instance.to_owned, |
112 | |
113 | |
114 | |
115 | pub async |
116 | &self, |
117 | user: &User, |
118 | repository_name: &str, |
119 | |
120 | if let Ok = query_as! |
121 | r#"SELECT owner_user, name, description, visibility as "visibility: _", default_branch FROM repositories WHERE owner_user = $1 AND name = $2"#, |
122 | user.to_string, repository_name |
123 | .fetch_one |
124 | .await |
125 | Ok |
126 | else |
127 | Err |
128 | owner_user: user.to_string, |
129 | name: repository_name.to_string, |
130 | |
131 | |
132 | |
133 | |
134 | pub async |
135 | &self, |
136 | user: &User, |
137 | repository_name: &str, |
138 | |
139 | if let Err = remove_dir_all |
140 | "{}/{}/{}/{}" |
141 | self.repository_folder, user.instance.url, user.username, repository_name |
142 | ) |
143 | let err = CouldNotDeleteFromDisk; |
144 | error! |
145 | "Couldn't delete repository from disk, this is bad! {:?}", |
146 | err |
147 | ; |
148 | |
149 | return Err; |
150 | |
151 | |
152 | // Delete the repository from the database |
153 | match query! |
154 | "DELETE FROM repositories WHERE owner_user = $1 AND name = $2", |
155 | user.to_string, |
156 | repository_name |
157 | |
158 | .execute |
159 | .await |
160 | |
161 | Ok => Ok, |
162 | Err => Err, |
163 | |
164 | |
165 | |
166 | // TODO: Find where this fits |
167 | // TODO: Cache this and general repository tree and invalidate select files on push |
168 | // TODO: Find better and faster technique for this |
169 | |
170 | path: &str, |
171 | git: & Repository, |
172 | start_commit: & Commit, |
173 | |
174 | let mut revwalk = git.revwalk?; |
175 | revwalk.set_sorting?; |
176 | revwalk.push?; |
177 | |
178 | for oid in revwalk |
179 | let oid = oid?; |
180 | let commit = git.find_commit?; |
181 | |
182 | // Merge commits have 2 or more parents |
183 | // Commits with 0 parents are handled different because we can't diff against them |
184 | if commit.parent_count == 0 |
185 | return Ok; |
186 | else if commit.parent_count == 1 |
187 | let tree = commit.tree?; |
188 | let last_tree = commit.parent?.tree?; |
189 | |
190 | // Get the diff between the current tree and the last one |
191 | let diff = git.diff_tree_to_tree?; |
192 | |
193 | for dd in diff.deltas |
194 | // Get the path of the current file we're diffing against |
195 | let current_path = dd.new_file .path .unwrap; |
196 | |
197 | // Path or directory |
198 | if current_path.eq || current_path.starts_with |
199 | return Ok; |
200 | |
201 | |
202 | |
203 | |
204 | |
205 | Err? |
206 | |
207 | |
208 | |
209 | |
210 | |
211 | async |
212 | &mut self, |
213 | _user: &User, |
214 | request: &CreateRepositoryRequest, |
215 | |
216 | // Check if repository already exists in the database |
217 | if let Ok = self |
218 | .find_by_owner_user_name |
219 | .await |
220 | |
221 | let err = RepositoryAlreadyExists |
222 | owner_user: repository.owner_user.to_string, |
223 | name: repository.name, |
224 | ; |
225 | error!; |
226 | |
227 | return Ok; |
228 | |
229 | |
230 | // Insert the repository into the database |
231 | let _ = match query_as! |
232 | r#"INSERT INTO repositories VALUES ($1, $2, $3, $4, $5) RETURNING owner_user, name, description, visibility as "visibility: _", default_branch"#, |
233 | request.owner.to_string, request.name, request.description, request.visibility as _, "master" |
234 | .fetch_one |
235 | .await |
236 | Ok => repository, |
237 | Err => |
238 | let err = FailedInsertingIntoDatabase; |
239 | error!; |
240 | |
241 | return Ok; |
242 | |
243 | ; |
244 | |
245 | // Create bare (server side) repository on disk |
246 | match init_bare |
247 | "{}/{}/{}/{}" |
248 | self.repository_folder, |
249 | request.owner.instance.url, |
250 | request.owner.username, |
251 | request.name |
252 | ) |
253 | Ok => |
254 | debug! |
255 | "Created new repository with the name {}/{}/{}", |
256 | request.owner.instance.url, request.owner.username, request.name |
257 | ; |
258 | Ok |
259 | |
260 | Err => |
261 | let err = FailedCreatingRepository; |
262 | error!; |
263 | |
264 | // Delete repository from database |
265 | if let Err = self |
266 | .delete_by_owner_user_name |
267 | .await |
268 | |
269 | return Err; |
270 | |
271 | |
272 | // ??? |
273 | Ok |
274 | //Err(Box::new(err)) |
275 | |
276 | |
277 | |
278 | |
279 | async |
280 | &mut self, |
281 | requester: , |
282 | request: &RepositoryInfoRequest, |
283 | |
284 | let repository = match self |
285 | .find_by_owner_user_name |
286 | // &request.owner.instance.url, |
287 | &request.repository.owner, |
288 | &request.repository.name, |
289 | |
290 | .await |
291 | |
292 | Ok => repository, |
293 | Err => return Err, |
294 | ; |
295 | |
296 | if let Some = requester |
297 | if !repository.can_user_view_repository |
298 | return Err |
299 | owner_user: request.repository.owner.to_string, |
300 | name: request.repository.name.clone, |
301 | |
302 | .into; |
303 | |
304 | else if matches! |
305 | // Unauthenticated users can never view private repositories |
306 | |
307 | return Err |
308 | owner_user: request.repository.owner.to_string, |
309 | name: request.repository.name.clone, |
310 | |
311 | .into; |
312 | |
313 | |
314 | let git = match repository.open_git2_repository |
315 | Ok => git, |
316 | Err => return Err, |
317 | ; |
318 | |
319 | let rev_name = match &request.rev |
320 | None => |
321 | if let Ok = git.head |
322 | head.name .unwrap .to_string |
323 | else |
324 | // Nothing in database, render empty tree. |
325 | return Ok |
326 | name: repository.name, |
327 | owner: request.repository.owner.clone, |
328 | description: repository.description, |
329 | visibility: repository.visibility, |
330 | default_branch: repository.default_branch, |
331 | latest_commit: None, |
332 | tree_rev: None, |
333 | tree: vec!, |
334 | ; |
335 | |
336 | |
337 | Some => |
338 | // Find the reference, otherwise return GitBackendError |
339 | match git |
340 | .find_reference |
341 | .map_err |
342 | |
343 | Ok => reference.name .unwrap .to_string, |
344 | Err => return Err, |
345 | |
346 | |
347 | ; |
348 | |
349 | // Get the git object as a commit |
350 | let rev = match git |
351 | .revparse_single |
352 | .map_err |
353 | |
354 | Ok => rev, |
355 | Err => return Err, |
356 | ; |
357 | let commit = rev.as_commit .unwrap; |
358 | |
359 | // this is stupid |
360 | let mut current_path = rev_name.replace; |
361 | |
362 | // Get the commit tree |
363 | let git_tree = if let Some = &request.path |
364 | // Add it to our full path string |
365 | current_path.push_str; |
366 | // Get the specified path, return an error if it wasn't found. |
367 | let entry = match commit |
368 | .tree |
369 | .unwrap |
370 | .get_path |
371 | .map_err |
372 | |
373 | Ok => entry, |
374 | Err => return Err, |
375 | ; |
376 | // Turn the entry into a git tree |
377 | entry.to_object .unwrap .as_tree .unwrap .clone |
378 | else |
379 | commit.tree .unwrap |
380 | ; |
381 | |
382 | // Iterate over the git tree and collect it into our own tree types |
383 | let mut tree = git_tree |
384 | .iter |
385 | .map |
386 | let object_type = match entry.kind .unwrap |
387 | => Tree, | Tree
388 | => Blob, | Blob
389 | _ => unreachable!, |
390 | ; |
391 | let mut tree_entry = |
392 | ; | new
393 | |
394 | if request.extra_metadata |
395 | // Get the file size if It's a blob |
396 | let object = entry.to_object .unwrap; |
397 | if let Some = object.as_blob |
398 | tree_entry.size = Some; |
399 | |
400 | |
401 | // Could possibly be done better |
402 | let path = if let Some = current_path.split_once |
403 | format! |
404 | else |
405 | entry.name .unwrap .to_string |
406 | ; |
407 | |
408 | // Get the last commit made to the entry |
409 | if let Ok = |
410 | get_last_commit_of_file |
411 | |
412 | tree_entry.last_commit = Some; |
413 | |
414 | |
415 | |
416 | tree_entry |
417 | |
418 | .; |
419 | |
420 | // Sort the tree alphabetically and with tree first |
421 | tree.sort_unstable_by_key; |
422 | tree.sort_unstable_by_key |
423 | Reverse |
424 | ; |
425 | |
426 | Ok |
427 | name: repository.name, |
428 | owner: request.repository.owner.clone, |
429 | description: repository.description, |
430 | visibility: repository.visibility, |
431 | default_branch: repository.default_branch, |
432 | latest_commit: None, |
433 | tree_rev: Some, |
434 | tree, |
435 | |
436 | |
437 | |
438 | async |
439 | &mut self, |
440 | _requester: , |
441 | _request: &RepositoryFileInspectRequest, |
442 | |
443 | todo! |
444 | |
445 | |
446 | async |
447 | &mut self, |
448 | user: &User, |
449 | |
450 | let mut repositories = query_as! |
451 | GitRepository, |
452 | r#"SELECT visibility as "visibility: _", owner_user, name, description, default_branch FROM repositories WHERE owner_user = $1"#, |
453 | user.to_string |
454 | |
455 | .fetch_many; |
456 | |
457 | let mut result = vec!; |
458 | |
459 | while let Some = repositories.next .await |
460 | result.push |
461 | repository: Repository |
462 | owner: repository.owner_user.clone, |
463 | name: repository.name, |
464 | instance: self.instance.clone, |
465 | , |
466 | owner: repository.owner_user.clone, |
467 | visibility: repository.visibility, |
468 | description: repository.description, |
469 | // TODO |
470 | last_commit: None, |
471 | ; |
472 | |
473 | |
474 | Ok |
475 | |
476 | |
477 | |
478 | |
479 | |
480 | &mut self, |
481 | _requester: , |
482 | _request: &RepositoryIssuesCountRequest, |
483 | |
484 | todo! |
485 | |
486 | |
487 | |
488 | &mut self, |
489 | _requester: , |
490 | _request: &RepositoryIssueLabelsRequest, |
491 | |
492 | todo! |
493 | |
494 | |
495 | |
496 | &mut self, |
497 | _requester: , |
498 | _request: &RepositoryIssuesRequest, |
499 | |
500 | todo! |
501 | |
502 | |
503 |