index : gitrs
A lightweight web interface for browsing Git repositories, implemented in Rust
about summary refs log tree commit diff
gitrs / src / git / tag.rs
-rw-r--r-- blob f31302e12b055a6b182a78c0919e0cac768243ee 3284 bytes (plain)
1
use anyhow::Context;
2
use gix::objs::Kind;
3
4
use crate::prelude::*;
5
6
pub enum TaggedObject {
7
    Commit(String),
8
    Tree(String),
9
}
10
11
impl TaggedObject {
12
    fn from_kind_and_id(kind: Kind, id: String) -> Option<Self> {
13
        match kind {
14
            Kind::Commit => Some(Self::Commit(id)),
15
            Kind::Tree => Some(Self::Tree(id)),
16
            _ => None,
17
        }
18
    }
19
}
20
21
pub struct Tag {
22
    pub name: String,
23
    pub id: String,
24
    pub message: String,
25
    pub tagger: Option<GitSignature>,
26
    pub tagged_object: Option<TaggedObject>,
27
    pub time: Time,
28
}
29
30
impl Tag {
31
    pub fn from_gix(mut gix: gix::Reference<'_>) -> anyhow::Result<Tag> {
32
        let name = gix.name().shorten().to_string();
33
        let id = gix.id().to_string();
34
35
        let (message, tagger, tagged_object, time) = if let Ok(tag_obj) = gix.peel_to_tag() {
36
            let tag_ref = tag_obj.decode()?;
37
            let message = tag_ref.message.to_string();
38
            let tagged_object =
39
                TaggedObject::from_kind_and_id(tag_ref.target_kind, tag_ref.target.to_string());
40
            let tagger = tag_ref.tagger()?.map(GitSignature::from_gix);
41
            let time = match &tagger {
42
                Some(t) => t.time.clone(),
43
                None => Self::commit_time(&mut gix)?,
44
            };
45
            (message, tagger, tagged_object, time)
46
        } else {
47
            let target = gix
48
                .follow_to_object()
49
                .context("Failed to resolve lightweight tag target")?;
50
            let tagged_object =
51
                TaggedObject::from_kind_and_id(target.header()?.kind(), target.to_string());
52
            let time = Self::commit_time(&mut gix)?;
53
            (String::new(), None, tagged_object, time)
54
        };
55
56
        Ok(Tag {
57
            name,
58
            id,
59
            message,
60
            tagger,
61
            tagged_object,
62
            time,
63
        })
64
    }
65
66
    fn commit_time(gix: &mut gix::Reference<'_>) -> anyhow::Result<Time> {
67
        let commit = gix
68
            .peel_to_commit()
69
            .context("Couldn't get to a commit from the given reference!")?;
70
        Ok(Time::new(commit.time()?))
71
    }
72
}
73
74
impl Repository {
75
    pub fn find_tag(&self, name: &str) -> anyhow::Result<Tag> {
76
        let gix_repo = self.gix();
77
        let reference = gix_repo.find_reference(&format!("tags/{name}"))?;
78
        Tag::from_gix(reference)
79
    }
80
81
    pub fn tags(&self, offset: usize, limit: usize) -> anyhow::Result<(Vec<Tag>, bool)> {
82
        let gix_repo = self.gix();
83
        let references = gix_repo.references()?;
84
        let mut all_tags: Vec<Tag> = references
85
            .tags()?
86
            .map(|rev| {
87
                let rev = rev.map_err(|e| anyhow::anyhow!(e))?;
88
                Tag::from_gix(rev)
89
            })
90
            .collect::<anyhow::Result<Vec<_>>>()?;
91
92
        // Sort tags descending (newest / highest version first, e.g. v0.2.0 -> v0.1.1 -> v0.1.0)
93
        all_tags.sort_by(|a, b| {
94
            b.time
95
                .seconds()
96
                .cmp(&a.time.seconds())
97
                .then_with(|| b.name.cmp(&a.name))
98
        });
99
100
        let has_next = all_tags.len() > offset.saturating_add(limit);
101
        let tags = all_tags.into_iter().skip(offset).take(limit).collect();
102
        Ok((tags, has_next))
103
    }
104
}