JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

Major connection refactor base

Type: Refactor

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨8dcc111

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