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

ambee/giterated

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

L + Ratio

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨1f5f028

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