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