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

ambee/giterated

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

User Auth Early

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨8069fba

⁨src/model/repository.rs⁩ - ⁨4089⁩ bytes
Raw
1 use serde::{Deserialize, Serialize};
2
3 use super::{instance::Instance, user::User};
4
5 #[derive(Hash, Clone, Serialize, Deserialize)]
6 pub struct Repository {
7 /// Name of the repository.
8 ///
9 /// This must be treated as an opaque path to a repository. Do not
10 /// modify or parse the path.
11 pub name: String,
12 /// Instance the repository is on
13 pub instance: Instance,
14 }
15
16 /// Visibility of the repository to the general eye
17 #[derive(Debug, Hash, Serialize, Deserialize, Clone, sqlx::Type)]
18 #[sqlx(type_name = "visibility", rename_all = "lowercase")]
19 pub enum RepositoryVisibility {
20 Public,
21 Unlisted,
22 Private,
23 }
24
25 #[derive(Clone, Debug, Serialize, Deserialize)]
26 pub struct RepositoryView {
27 /// Name of the repository
28 ///
29 /// This is different than the [`Repository`] name,
30 /// which may be a path.
31 pub name: String,
32 /// Owner of the Repository
33 pub owner: User,
34 /// Repository description
35 pub description: Option<String>,
36 /// Repository visibility
37 pub visibility: RepositoryVisibility,
38 /// Default branch of the repository
39 pub default_branch: String,
40 /// Last commit made to the repository
41 pub latest_commit: Option<Commit>,
42 /// Revision of the displayed tree
43 pub tree_rev: Option<String>,
44 /// Repository tree
45 pub tree: Vec<RepositoryTreeEntry>,
46 }
47
48 #[derive(Debug, Clone, Serialize, Deserialize)]
49 pub enum RepositoryObjectType {
50 Tree,
51 Blob,
52 }
53
54 /// Stored info for our tree entries
55 #[derive(Debug, Clone, Serialize, Deserialize)]
56 pub struct RepositoryTreeEntry {
57 /// Name of the tree/blob
58 pub name: String,
59 /// Type of the tree entry
60 pub object_type: RepositoryObjectType,
61 /// Git supplies us with the mode at all times, and people like it displayed.
62 pub mode: i32,
63 /// File size
64 pub size: Option<usize>,
65 /// Last commit made to the tree/blob
66 pub last_commit: Option<Commit>,
67 }
68
69 impl RepositoryTreeEntry {
70 // I love you Emilia <3
71 pub fn new(name: &str, object_type: RepositoryObjectType, mode: i32) -> Self {
72 Self {
73 name: name.to_string(),
74 object_type,
75 mode,
76 size: None,
77 last_commit: None,
78 }
79 }
80 }
81
82 #[derive(Debug, Clone, Serialize, Deserialize)]
83 pub struct RepositoryTreeEntryWithCommit {
84 pub tree_entry: RepositoryTreeEntry,
85 pub commit: Commit,
86 }
87
88 /// Info about a git commit
89 #[derive(Debug, Clone, Serialize, Deserialize)]
90 pub struct Commit {
91 /// Unique commit ID
92 pub oid: String,
93 /// Full commit message
94 pub message: Option<String>,
95 /// Who created the commit
96 pub author: CommitSignature,
97 /// Who committed the commit
98 pub committer: CommitSignature,
99 /// Time when the commit happened
100 pub time: chrono::NaiveDateTime,
101 }
102
103 /// Gets all info from [`git2::Commit`] for easy use
104 impl From<git2::Commit<'_>> for Commit {
105 fn from(commit: git2::Commit<'_>) -> Self {
106 Self {
107 oid: commit.id().to_string(),
108 message: commit.message().map(|message| message.to_string()),
109 author: commit.author().into(),
110 committer: commit.committer().into(),
111 time: chrono::NaiveDateTime::from_timestamp_opt(commit.time().seconds(), 0).unwrap(),
112 }
113 }
114 }
115
116 /// Git commit signature
117 #[derive(Debug, Clone, Serialize, Deserialize)]
118 pub struct CommitSignature {
119 pub name: Option<String>,
120 pub email: Option<String>,
121 pub time: chrono::NaiveDateTime,
122 }
123
124 /// Converts the signature from git2 into something usable without explicit lifetimes.
125 impl From<git2::Signature<'_>> for CommitSignature {
126 fn from(signature: git2::Signature<'_>) -> Self {
127 Self {
128 name: signature.name().map(|name| name.to_string()),
129 email: signature.email().map(|email| email.to_string()),
130 time: chrono::NaiveDateTime::from_timestamp_opt(signature.when().seconds(), 0).unwrap(),
131 }
132 }
133 }
134
135 impl ToString for Repository {
136 fn to_string(&self) -> String {
137 format!("{}@{}", self.name, self.instance.url)
138 }
139 }
140