use serde::{Deserialize, Serialize}; use super::instance::Instance; #[derive(Hash, Clone, Serialize, Deserialize)] pub struct Repository { /// username of the person who owns the repository (who knows how organizations will work) pub owner_username: String, /// Visibility of the repository to the general eye pub visibility: RepositoryVisibility, /// Name of the repository pub name: String, /// Instance the repository is on pub instance: Instance, } /// Visibility of the repository to the general eye #[derive(Debug, Hash, Serialize, Deserialize, Clone, sqlx::Type)] #[sqlx(type_name = "visibility", rename_all = "lowercase")] pub enum RepositoryVisibility { Public, Unlisted, Private, } #[derive(Clone, Debug, Serialize, Deserialize)] pub struct RepositoryView { /// Name of the repository pub name: String, /// Repository description pub description: Option, /// Visibility of the repository to the general eye pub visibility: RepositoryVisibility, /// Default branch of the repository pub default_branch: String, /// Last commit made to the repository pub latest_commit: Option, /// Revision of the displayed tree pub tree_rev: Option, /// Repository tree pub tree: Vec, } #[derive(Debug, Clone, Serialize, Deserialize)] pub enum RepositoryObjectType { Tree, Blob, } /// Stored info for our tree entries #[derive(Debug, Clone, Serialize, Deserialize)] pub struct RepositoryTreeEntry { /// Name of the tree/blob pub name: String, /// Type of the tree entry pub object_type: RepositoryObjectType, /// Git supplies us with the mode at all times, and people like it displayed. pub mode: i32, /// File size pub size: Option, /// Last commit made to the tree/blob pub last_commit: Option, } impl RepositoryTreeEntry { // I love you Emilia <3 pub fn new(name: &str, object_type: RepositoryObjectType, mode: i32) -> Self { Self { name: name.to_string(), object_type, mode, size: None, last_commit: None, } } } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct RepositoryTreeEntryWithCommit { pub tree_entry: RepositoryTreeEntry, pub commit: Commit, } /// Info about a git commit #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Commit { /// Unique commit ID pub oid: String, /// Full commit message pub message: Option, /// Who created the commit pub author: CommitSignature, /// Who committed the commit pub committer: CommitSignature, /// Time when the commit happened pub time: chrono::NaiveDateTime, } /// Gets all info from [`git2::Commit`] for easy use impl From> for Commit { fn from(commit: git2::Commit<'_>) -> Self { Self { oid: commit.id().to_string(), message: commit .message() .map_or(None, |message| Some(message.to_string())), author: commit.author().into(), committer: commit.committer().into(), time: chrono::NaiveDateTime::from_timestamp_opt(commit.time().seconds(), 0).unwrap(), } } } /// Git commit signature #[derive(Debug, Clone, Serialize, Deserialize)] pub struct CommitSignature { pub name: Option, pub email: Option, pub time: chrono::NaiveDateTime, } /// Converts the signature from git2 into something usable without explicit lifetimes. impl From> for CommitSignature { fn from(signature: git2::Signature<'_>) -> Self { Self { name: signature.name().map_or(None, |name| Some(name.to_string())), email: signature .email() .map_or(None, |email| Some(email.to_string())), time: chrono::NaiveDateTime::from_timestamp_opt(signature.when().seconds(), 0).unwrap(), } } }