Skip to main content

lux_lib/git/
mod.rs

1use std::fmt::Display;
2
3use miette::Diagnostic;
4use thiserror::Error;
5
6use crate::{
7    fs,
8    git::url::RemoteGitUrl,
9    lua_rockspec::{DisplayAsLuaKV, DisplayAsLuaValue, DisplayLuaKV, DisplayLuaValue},
10};
11
12pub mod shorthand;
13pub mod url;
14pub mod utils;
15
16impl DisplayAsLuaValue for RemoteGitUrl {
17    fn display_lua_value(&self) -> DisplayLuaValue {
18        DisplayLuaValue::String(self.to_string())
19    }
20}
21
22#[derive(Debug, Error, Diagnostic)]
23pub enum Error {
24    #[error(transparent)]
25    #[diagnostic(transparent)]
26    Fs(#[from] fs::Error),
27    #[error("error initializing temporary bare git repository to fetch metadata")]
28    BareRepoInit(#[source] git2::Error),
29    #[error("error initializing remote repository '{0}' to fetch metadata")]
30    RemoteInit(String, #[source] git2::Error),
31    #[error("error fetching from remote repository '{0}'")]
32    RemoteFetch(String, #[source] git2::Error),
33    #[error("error listing remote refs for '{0}'")]
34    RemoteList(String, #[source] git2::Error),
35    #[error("could not determine latest tag or commit sha for '{0}'")]
36    NoTagOrCommitSha(String),
37}
38/// Specifies a git reference (tag or branch)
39#[derive(Debug, PartialEq, Eq, Hash, Clone)]
40pub enum GitRef {
41    Tag(String),
42    Branch(String),
43}
44
45/// Specifies a source to be fetched from a git forge
46#[derive(Debug, PartialEq, Eq, Hash, Clone)]
47pub struct GitSource {
48    pub url: RemoteGitUrl,
49    pub git_ref: Option<GitRef>,
50}
51
52impl DisplayAsLuaKV for GitSource {
53    fn display_lua(&self) -> DisplayLuaKV {
54        let mut fields = vec![DisplayLuaKV {
55            key: "url".to_string(),
56            value: DisplayLuaValue::String(self.url.to_string()),
57        }];
58
59        if let Some(git_ref) = &self.git_ref {
60            match git_ref {
61                GitRef::Tag(tag) => fields.push(DisplayLuaKV {
62                    key: "tag".to_string(),
63                    value: DisplayLuaValue::String(tag.clone()),
64                }),
65                GitRef::Branch(branch) => fields.push(DisplayLuaKV {
66                    key: "branch".to_string(),
67                    value: DisplayLuaValue::String(branch.clone()),
68                }),
69            }
70        }
71
72        DisplayLuaKV {
73            key: "source".to_string(),
74            value: DisplayLuaValue::Table(fields),
75        }
76    }
77}
78
79impl Display for GitSource {
80    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81        match &self.git_ref {
82            Some(GitRef::Tag(tag)) => format!("{}@{}", self.url, tag).fmt(f),
83            Some(GitRef::Branch(branch)) => format!("{}@{}", self.url, branch).fmt(f),
84            None => self.url.fmt(f),
85        }
86    }
87}