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

ambee/giterated

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

fixes

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨bb30c68

⁨src/model/repository.rs⁩ - ⁨3947⁩ 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.message().map(|message| message.to_string()),
105 author: commit.author().into(),
106 committer: commit.committer().into(),
107 time: chrono::NaiveDateTime::from_timestamp_opt(commit.time().seconds(), 0).unwrap(),
108 }
109 }
110 }
111
112 /// Git commit signature
113 #[derive(Debug, Clone, Serialize, Deserialize)]
114 pub struct CommitSignature {
115 pub name: Option<String>,
116 pub email: Option<String>,
117 pub time: chrono::NaiveDateTime,
118 }
119
120 /// Converts the signature from git2 into something usable without explicit lifetimes.
121 impl From<git2::Signature<'_>> for CommitSignature {
122 fn from(signature: git2::Signature<'_>) -> Self {
123 Self {
124 name: signature.name().map(|name| name.to_string()),
125 email: signature.email().map(|email| email.to_string()),
126 time: chrono::NaiveDateTime::from_timestamp_opt(signature.when().seconds(), 0).unwrap(),
127 }
128 }
129 }
130