Fixes
parent: tbd commit: 74f247e
1 | use async_trait; |
2 | use ObjectType; |
3 | use PgPool; |
4 | use Error; |
5 | use ; |
6 | use Error; |
7 | |
8 | use crate Authenticated; |
9 | use crate Instance; |
10 | use crate |
11 | Commit, RepositoryObjectType, RepositoryTreeEntry, RepositoryVisibility, |
12 | ; |
13 | use crate:: |
14 | |
15 | CreateRepositoryRequest, CreateRepositoryResponse, RepositoryFileInspectRequest, |
16 | RepositoryFileInspectionResponse, RepositoryInfoRequest, RepositoryIssueLabelsRequest, |
17 | RepositoryIssueLabelsResponse, RepositoryIssuesCountRequest, RepositoryIssuesCountResponse, |
18 | RepositoryIssuesRequest, RepositoryIssuesResponse, |
19 | , |
20 | , | RepositoryView
21 | ; |
22 | |
23 | use ; |
24 | |
25 | // TODO: Handle this |
26 | //region database structures |
27 | |
28 | /// Repository in the database |
29 | |
30 | |
31 | pub username: String, |
32 | pub instance_url: String, |
33 | pub name: String, |
34 | pub description: , |
35 | pub visibility: RepositoryVisibility, |
36 | pub default_branch: String, |
37 | |
38 | |
39 | |
40 | // Separate function because "Private" will be expanded later |
41 | /// Checks if the user is allowed to view this repository |
42 | |
43 | ! |
44 | && self.instance_url != instance_url |
45 | && self.username != username.map_or |
46 | |
47 | |
48 | // This is in it's own function because I assume I'll have to add logic to this later |
49 | |
50 | &self, |
51 | repository_directory: &str, |
52 | |
53 | match open |
54 | "{}/{}/{}/{}" |
55 | repository_directory, self.instance_url, self.username, self.name |
56 | ) |
57 | Ok => Ok, |
58 | Err => |
59 | let err = FailedOpeningFromDisk; |
60 | error!; |
61 | |
62 | Err |
63 | |
64 | |
65 | |
66 | |
67 | |
68 | //endregion |
69 | |
70 | |
71 | |
72 | |
73 | FailedCreatingRepository, |
74 | |
75 | FailedInsertingIntoDatabase, |
76 | |
77 | RepositoryNotFound |
78 | instance_url: String, |
79 | username: String, |
80 | name: String, |
81 | , |
82 | |
83 | RepositoryAlreadyExists |
84 | instance_url: String, |
85 | username: String, |
86 | name: String, |
87 | , |
88 | |
89 | CouldNotDeleteFromDisk, |
90 | |
91 | FailedDeletingFromDatabase, |
92 | |
93 | FailedOpeningFromDisk, |
94 | |
95 | RefNotFound, |
96 | |
97 | PathNotFound, |
98 | |
99 | LastCommitNotFound, |
100 | |
101 | |
102 | |
103 | pub pg_pool: PgPool, |
104 | pub repository_folder: String, |
105 | |
106 | |
107 | |
108 | |
109 | Self |
110 | pg_pool: pg_pool.clone, |
111 | repository_folder: repository_folder.to_string, |
112 | |
113 | |
114 | |
115 | pub async |
116 | &self, |
117 | instance_url: &str, |
118 | username: &str, |
119 | repository_name: &str, |
120 | |
121 | if let Ok = query_as! |
122 | r#"SELECT username, instance_url, name, description, visibility as "visibility: _", default_branch FROM repositories WHERE instance_url = $1 AND username = $2 AND name = $3"#, |
123 | instance_url, username, repository_name |
124 | .fetch_one |
125 | .await |
126 | Ok |
127 | else |
128 | Err |
129 | instance_url: instance_url.to_string, |
130 | username: username.to_string, |
131 | name: repository_name.to_string, |
132 | |
133 | |
134 | |
135 | |
136 | pub async |
137 | &self, |
138 | instance_url: &str, |
139 | username: &str, |
140 | repository_name: &str, |
141 | |
142 | if let Err = remove_dir_all |
143 | "{}/{}/{}/{}" |
144 | self.repository_folder, instance_url, 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 instance_url = $1 AND username = $2 AND name = $3", |
158 | instance_url, |
159 | username, |
160 | repository_name |
161 | |
162 | .execute |
163 | .await |
164 | |
165 | Ok => Ok, |
166 | Err => Err, |
167 | |
168 | |
169 | |
170 | // TODO: Find where this fits |
171 | // TODO: Cache this and general repository tree and invalidate select files on push |
172 | // TODO: Find better and faster technique for this |
173 | |
174 | path: &str, |
175 | git: & Repository, |
176 | start_commit: & Commit, |
177 | |
178 | let mut revwalk = git.revwalk?; |
179 | revwalk.set_sorting?; |
180 | revwalk.push?; |
181 | |
182 | for oid in revwalk |
183 | let oid = oid?; |
184 | let commit = git.find_commit?; |
185 | |
186 | // Merge commits have 2 or more parents |
187 | // Commits with 0 parents are handled different because we can't diff against them |
188 | if commit.parent_count == 0 |
189 | return Ok; |
190 | else if commit.parent_count == 1 |
191 | let tree = commit.tree?; |
192 | let last_tree = commit.parent?.tree?; |
193 | |
194 | // Get the diff between the current tree and the last one |
195 | let diff = git.diff_tree_to_tree?; |
196 | |
197 | for dd in diff.deltas |
198 | // Get the path of the current file we're diffing against |
199 | let current_path = dd.new_file .path .unwrap; |
200 | |
201 | // Path or directory |
202 | if current_path.eq || current_path.starts_with |
203 | return Ok; |
204 | |
205 | |
206 | |
207 | |
208 | |
209 | Err? |
210 | |
211 | |
212 | |
213 | |
214 | |
215 | async |
216 | &mut self, |
217 | raw_request: & , |
218 | |
219 | let request = raw_request.inner .await; |
220 | |
221 | let public_key = public_key |
222 | url: String from, |
223 | |
224 | .await |
225 | .unwrap; |
226 | |
227 | assert!; |
228 | info!; |
229 | |
230 | // Check if repository already exists in the database |
231 | if let Ok = self |
232 | .find_by_instance_username_name |
233 | request.owner.instance.url.as_str, |
234 | request.owner.username.as_str, |
235 | request.name.as_str, |
236 | |
237 | .await |
238 | |
239 | let err = RepositoryAlreadyExists |
240 | instance_url: request.owner.instance.url.clone, |
241 | username: request.owner.instance.url.clone, |
242 | name: request.name.clone, |
243 | ; |
244 | error!; |
245 | |
246 | return Ok; |
247 | |
248 | |
249 | // Insert the repository into the database |
250 | let _ = match query_as! |
251 | r#"INSERT INTO repositories VALUES ($1, $2, $3, $4, $5, $6) RETURNING username, instance_url, name, description, visibility as "visibility: _", default_branch"#, |
252 | request.owner.username, request.owner.instance.url, request.name, request.description, request.visibility as _, "master" |
253 | .fetch_one |
254 | .await |
255 | Ok => repository, |
256 | Err => |
257 | let err = FailedInsertingIntoDatabase; |
258 | error!; |
259 | |
260 | return Ok; |
261 | |
262 | ; |
263 | |
264 | // Create bare (server side) repository on disk |
265 | match init_bare |
266 | "{}/{}/{}/{}" |
267 | self.repository_folder, |
268 | request.owner.instance.url, |
269 | request.owner.username, |
270 | request.name |
271 | ) |
272 | Ok => |
273 | debug! |
274 | "Created new repository with the name {}/{}/{}", |
275 | request.owner.instance.url, request.owner.username, request.name |
276 | ; |
277 | Ok |
278 | |
279 | Err => |
280 | let err = FailedCreatingRepository; |
281 | error!; |
282 | |
283 | // Delete repository from database |
284 | if let Err = self |
285 | .delete_by_instance_username_name |
286 | request.owner.instance.url.as_str, |
287 | request.owner.username.as_str, |
288 | request.name.as_str, |
289 | |
290 | .await |
291 | |
292 | return Err; |
293 | |
294 | |
295 | // ??? |
296 | Ok |
297 | //Err(Box::new(err)) |
298 | |
299 | |
300 | |
301 | |
302 | async |
303 | &mut self, |
304 | request: &RepositoryInfoRequest, |
305 | |
306 | let repository = match self |
307 | .find_by_instance_username_name |
308 | &request.owner.instance.url, |
309 | &request.owner.username, |
310 | &request.name, |
311 | |
312 | .await |
313 | |
314 | Ok => repository, |
315 | Err => return Err, |
316 | ; |
317 | |
318 | if !repository.can_user_view_repository |
319 | request.owner.instance.url.as_str, |
320 | Some, |
321 | |
322 | return Err |
323 | instance_url: request.owner.instance.url.clone, |
324 | username: request.owner.username.clone, |
325 | name: request.name.clone, |
326 | ; |
327 | |
328 | |
329 | let git = match repository.open_git2_repository |
330 | Ok => git, |
331 | Err => return Err, |
332 | ; |
333 | |
334 | let rev_name = match &request.rev |
335 | None => |
336 | if let Ok = git.head |
337 | head.name .unwrap .to_string |
338 | else |
339 | // Nothing in database, render empty tree. |
340 | return Ok |
341 | name: repository.name, |
342 | description: repository.description, |
343 | visibility: repository.visibility, |
344 | default_branch: repository.default_branch, |
345 | latest_commit: None, |
346 | tree_rev: None, |
347 | tree: vec!, |
348 | ; |
349 | |
350 | |
351 | Some => |
352 | // Find the reference, otherwise return GitBackendError |
353 | match git |
354 | .find_reference |
355 | .map_err |
356 | |
357 | Ok => reference.name .unwrap .to_string, |
358 | Err => return Err, |
359 | |
360 | |
361 | ; |
362 | |
363 | // Get the git object as a commit |
364 | let rev = match git |
365 | .revparse_single |
366 | .map_err |
367 | |
368 | Ok => rev, |
369 | Err => return Err, |
370 | ; |
371 | let commit = rev.as_commit .unwrap; |
372 | |
373 | // this is stupid |
374 | let mut current_path = rev_name.replace; |
375 | |
376 | // Get the commit tree |
377 | let git_tree = if let Some = &request.path |
378 | // Add it to our full path string |
379 | current_path.push_str; |
380 | // Get the specified path, return an error if it wasn't found. |
381 | let entry = match commit |
382 | .tree |
383 | .unwrap |
384 | .get_path |
385 | .map_err |
386 | |
387 | Ok => entry, |
388 | Err => return Err, |
389 | ; |
390 | // Turn the entry into a git tree |
391 | entry.to_object .unwrap .as_tree .unwrap .clone |
392 | else |
393 | commit.tree .unwrap |
394 | ; |
395 | |
396 | // Iterate over the git tree and collect it into our own tree types |
397 | let mut tree = git_tree |
398 | .iter |
399 | .map |
400 | let object_type = match entry.kind .unwrap |
401 | => Tree, | Tree
402 | => Blob, | Blob
403 | _ => unreachable!, |
404 | ; |
405 | let mut tree_entry = |
406 | ; | new
407 | |
408 | if request.extra_metadata |
409 | // Get the file size if It's a blob |
410 | let object = entry.to_object .unwrap; |
411 | if let Some = object.as_blob |
412 | tree_entry.size = Some; |
413 | |
414 | |
415 | // Could possibly be done better |
416 | let path = if let Some = current_path.split_once |
417 | format! |
418 | else |
419 | entry.name .unwrap .to_string |
420 | ; |
421 | |
422 | // Get the last commit made to the entry |
423 | if let Ok = |
424 | get_last_commit_of_file |
425 | |
426 | tree_entry.last_commit = Some; |
427 | |
428 | |
429 | |
430 | tree_entry |
431 | |
432 | .; |
433 | |
434 | // Sort the tree alphabetically and with tree first |
435 | tree.sort_unstable_by_key; |
436 | tree.sort_unstable_by_key |
437 | Reverse |
438 | ; |
439 | |
440 | Ok |
441 | name: repository.name, |
442 | description: repository.description, |
443 | visibility: repository.visibility, |
444 | default_branch: repository.default_branch, |
445 | latest_commit: None, |
446 | tree_rev: Some, |
447 | tree, |
448 | |
449 | |
450 | |
451 | |
452 | &mut self, |
453 | _request: &RepositoryFileInspectRequest, |
454 | |
455 | todo! |
456 | |
457 | |
458 | |
459 | |
460 | |
461 | &mut self, |
462 | _request: &RepositoryIssuesCountRequest, |
463 | |
464 | todo! |
465 | |
466 | |
467 | |
468 | &mut self, |
469 | _request: &RepositoryIssueLabelsRequest, |
470 | |
471 | todo! |
472 | |
473 | |
474 | |
475 | &mut self, |
476 | _request: &RepositoryIssuesRequest, |
477 | |
478 | todo! |
479 | |
480 | |
481 | |
482 | async |
483 | let key = get |
484 | .await? |
485 | .text |
486 | .await?; |
487 | |
488 | Ok |
489 | |
490 |