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 54f4b7851f1bad5a0cb3bcc414801a4a34f5a3e2 2982 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 gix_tags = references.tags()?;
85
86
        let mut tags = Vec::new();
87
        let mut has_next = false;
88
89
        for (i, rev) in gix_tags.skip(offset).enumerate() {
90
            if i >= limit {
91
                has_next = true;
92
                break;
93
            }
94
            let rev = rev.map_err(|e| anyhow::anyhow!(e))?;
95
            tags.push(Tag::from_gix(rev)?);
96
        }
97
        Ok((tags, has_next))
98
    }
99
}