Skip to main content

lux_lib/git/
mod.rs

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