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

ambee/giterated

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

Initial git handling

Type: Feature

emilia - ⁨2⁩ years ago

parent: tbd commit: ⁨523decf

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