merge changes
parent: tbd commit: 5213bdc
1 | use async_trait; |
2 | use ObjectType; |
3 | use PgPool; |
4 | use Error; |
5 | use ; |
6 | use Error; |
7 | |
8 | use crate ValidatedUserAuthenticated; |
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 | use crate User; |
23 | |
24 | use ; |
25 | |
26 | // TODO: Handle this |
27 | //region database structures |
28 | |
29 | /// Repository in the database |
30 | |
31 | |
32 | pub owner_user: User, |
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.owner_user.instance.url != user.map_or |
45 | && self.owner_user.username != user.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.owner_user.instance.url, self.owner_user.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 | owner_user: String, |
79 | name: String, |
80 | , |
81 | |
82 | RepositoryAlreadyExists |
83 | owner_user: String, |
84 | name: String, |
85 | , |
86 | |
87 | CouldNotDeleteFromDisk, |
88 | |
89 | FailedDeletingFromDatabase, |
90 | |
91 | FailedOpeningFromDisk, |
92 | |
93 | RefNotFound, |
94 | |
95 | PathNotFound, |
96 | |
97 | LastCommitNotFound, |
98 | |
99 | |
100 | |
101 | pub pg_pool: PgPool, |
102 | pub repository_folder: String, |
103 | |
104 | |
105 | |
106 | |
107 | Self |
108 | pg_pool: pg_pool.clone, |
109 | repository_folder: repository_folder.to_string, |
110 | |
111 | |
112 | |
113 | pub async |
114 | &self, |
115 | user: &User, |
116 | repository_name: &str, |
117 | |
118 | if let Ok = query_as! |
119 | r#"SELECT owner_user, name, description, visibility as "visibility: _", default_branch FROM repositories WHERE owner_user = $1 AND name = $2"#, |
120 | user.to_string, repository_name |
121 | .fetch_one |
122 | .await |
123 | Ok |
124 | else |
125 | Err |
126 | owner_user: user.to_string, |
127 | name: repository_name.to_string, |
128 | |
129 | |
130 | |
131 | |
132 | pub async |
133 | &self, |
134 | user: &User, |
135 | repository_name: &str, |
136 | |
137 | if let Err = remove_dir_all |
138 | "{}/{}/{}/{}" |
139 | self.repository_folder, user.instance.url, user.username, repository_name |
140 | ) |
141 | let err = CouldNotDeleteFromDisk; |
142 | error! |
143 | "Couldn't delete repository from disk, this is bad! {:?}", |
144 | err |
145 | ; |
146 | |
147 | return Err; |
148 | |
149 | |
150 | // Delete the repository from the database |
151 | match query! |
152 | "DELETE FROM repositories WHERE owner_user = $1 AND name = $2", |
153 | user.to_string, |
154 | repository_name |
155 | |
156 | .execute |
157 | .await |
158 | |
159 | Ok => Ok, |
160 | Err => Err, |
161 | |
162 | |
163 | |
164 | // TODO: Find where this fits |
165 | // TODO: Cache this and general repository tree and invalidate select files on push |
166 | // TODO: Find better and faster technique for this |
167 | |
168 | path: &str, |
169 | git: & Repository, |
170 | start_commit: & Commit, |
171 | |
172 | let mut revwalk = git.revwalk?; |
173 | revwalk.set_sorting?; |
174 | revwalk.push?; |
175 | |
176 | for oid in revwalk |
177 | let oid = oid?; |
178 | let commit = git.find_commit?; |
179 | |
180 | // Merge commits have 2 or more parents |
181 | // Commits with 0 parents are handled different because we can't diff against them |
182 | if commit.parent_count == 0 |
183 | return Ok; |
184 | else if commit.parent_count == 1 |
185 | let tree = commit.tree?; |
186 | let last_tree = commit.parent?.tree?; |
187 | |
188 | // Get the diff between the current tree and the last one |
189 | let diff = git.diff_tree_to_tree?; |
190 | |
191 | for dd in diff.deltas |
192 | // Get the path of the current file we're diffing against |
193 | let current_path = dd.new_file .path .unwrap; |
194 | |
195 | // Path or directory |
196 | if current_path.eq || current_path.starts_with |
197 | return Ok; |
198 | |
199 | |
200 | |
201 | |
202 | |
203 | Err? |
204 | |
205 | |
206 | |
207 | |
208 | |
209 | async |
210 | &mut self, |
211 | raw_request: & , |
212 | |
213 | let request = raw_request.inner .await; |
214 | |
215 | // let public_key = public_key(&Instance { |
216 | // url: String::from("giterated.dev"), |
217 | // }) |
218 | // .await |
219 | // .unwrap(); |
220 | // |
221 | // match raw_request.validate(public_key).await { |
222 | // Ok(_) => info!("Request was validated"), |
223 | // Err(err) => { |
224 | // error!("Failed to validate request: {:?}", err); |
225 | // panic!(); |
226 | // } |
227 | // } |
228 | // |
229 | // info!("Request was valid!"); |
230 | |
231 | // Check if repository already exists in the database |
232 | if let Ok = self |
233 | .find_by_owner_user_name |
234 | &request.owner, |
235 | &request.name, |
236 | |
237 | .await |
238 | |
239 | let err = RepositoryAlreadyExists |
240 | owner_user: repository.owner_user.to_string, |
241 | name: repository.name, |
242 | ; |
243 | error!; |
244 | |
245 | return Ok; |
246 | |
247 | |
248 | // Insert the repository into the database |
249 | let _ = match query_as! |
250 | r#"INSERT INTO repositories VALUES ($1, $2, $3, $4, $5) RETURNING owner_user, name, description, visibility as "visibility: _", default_branch"#, |
251 | request.owner.to_string, request.name, request.description, request.visibility as _, "master" |
252 | .fetch_one |
253 | .await |
254 | Ok => repository, |
255 | Err => |
256 | let err = FailedInsertingIntoDatabase; |
257 | error!; |
258 | |
259 | return Ok; |
260 | |
261 | ; |
262 | |
263 | // Create bare (server side) repository on disk |
264 | match init_bare |
265 | "{}/{}/{}/{}" |
266 | self.repository_folder, |
267 | request.owner.instance.url, |
268 | request.owner.username, |
269 | request.name |
270 | ) |
271 | Ok => |
272 | debug! |
273 | "Created new repository with the name {}/{}/{}", |
274 | request.owner.instance.url, request.owner.username, request.name |
275 | ; |
276 | Ok |
277 | |
278 | Err => |
279 | let err = FailedCreatingRepository; |
280 | error!; |
281 | |
282 | // Delete repository from database |
283 | if let Err = self |
284 | .delete_by_owner_user_name |
285 | &request.owner, |
286 | request.name.as_str, |
287 | |
288 | .await |
289 | |
290 | return Err; |
291 | |
292 | |
293 | // ??? |
294 | Ok |
295 | //Err(Box::new(err)) |
296 | |
297 | |
298 | |
299 | |
300 | async |
301 | &mut self, |
302 | // TODO: Allow non-authenticated??? |
303 | raw_request: & , |
304 | |
305 | let request = raw_request.inner .await; |
306 | |
307 | let repository = match self |
308 | .find_by_owner_user_name |
309 | // &request.owner.instance.url, |
310 | &request.repository.owner, |
311 | &request.repository.name, |
312 | |
313 | .await |
314 | |
315 | Ok => repository, |
316 | Err => return Err, |
317 | ; |
318 | |
319 | if !repository.can_user_view_repository |
320 | Some, |
321 | |
322 | return Err |
323 | owner_user: request.repository.owner.to_string, |
324 | name: request.repository.name.clone, |
325 | ; |
326 | |
327 | |
328 | let git = match repository.open_git2_repository |
329 | Ok => git, |
330 | Err => return Err, |
331 | ; |
332 | |
333 | let rev_name = match &request.rev |
334 | None => |
335 | if let Ok = git.head |
336 | head.name .unwrap .to_string |
337 | else |
338 | // Nothing in database, render empty tree. |
339 | return Ok |
340 | name: repository.name, |
341 | owner: request.repository.owner.clone, |
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 | owner: request.repository.owner.clone, |
443 | description: repository.description, |
444 | visibility: repository.visibility, |
445 | default_branch: repository.default_branch, |
446 | latest_commit: None, |
447 | tree_rev: Some, |
448 | tree, |
449 | |
450 | |
451 | |
452 | |
453 | &mut self, |
454 | _request: & , |
455 | |
456 | todo! |
457 | |
458 | |
459 | |
460 | |
461 | |
462 | |
463 | &mut self, |
464 | request: & , |
465 | |
466 | todo! |
467 | |
468 | |
469 | |
470 | &mut self, |
471 | request: & , |
472 | |
473 | todo! |
474 | |
475 | |
476 | |
477 | &mut self, |
478 | request: & , |
479 | |
480 | todo! |
481 | |
482 |