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 259c161b9c0a3bf486aa6029113100d8338ea846 3542 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
    /// Builds a `Tag` from a `refs/tags/*` Git reference.
32
    ///
33
    /// Git has two types of tags:
34
    /// 1. **Annotated tags** (`git tag -a`): store a dedicated Tag object with a tagger, date, and message.
35
    /// 2. **Lightweight tags** (`git tag`): just a reference pointing directly at a Commit (no Tag object).
36
    pub fn from_gix(mut gix: gix::Reference<'_>) -> anyhow::Result<Tag> {
37
        let name = gix.name().shorten().to_string();
38
39
        if let Ok(tag_obj) = gix.peel_to_tag() {
40
            // Case 1: Annotated tag
41
            let tag_ref = tag_obj.decode()?;
42
            let message = tag_ref.message.to_string();
43
            let tagged_object =
44
                TaggedObject::from_kind_and_id(tag_ref.target_kind, tag_ref.target.to_string());
45
            let tagger = tag_ref.tagger()?.map(GitSignature::from_gix);
46
47
            let time = match &tagger {
48
                Some(t) => t.time.clone(),
49
                None => Self::commit_time(&mut gix)?,
50
            };
51
52
            Ok(Tag {
53
                name,
54
                id: gix.id().to_string(),
55
                message,
56
                tagger,
57
                tagged_object,
58
                time,
59
            })
60
        } else {
61
            // Case 2: Lightweight tag (points directly to a Commit or Tree)
62
            let target = gix
63
                .follow_to_object()
64
                .context("Failed to resolve lightweight tag target")?;
65
            let target_kind = target.header()?.kind();
66
            let tagged_object = TaggedObject::from_kind_and_id(target_kind, target.to_string());
67
            let time = Self::commit_time(&mut gix)?;
68
69
            Ok(Tag {
70
                name,
71
                id: gix.id().to_string(),
72
                message: String::new(),
73
                tagger: None,
74
                tagged_object,
75
                time,
76
            })
77
        }
78
    }
79
80
    fn commit_time(gix: &mut gix::Reference<'_>) -> anyhow::Result<Time> {
81
        let commit = gix
82
            .peel_to_commit()
83
            .context("Couldn't get to a commit from the given reference!")?;
84
        Ok(Time::new(commit.time()?))
85
    }
86
}
87
88
impl Repository {
89
    pub fn find_tag(&self, name: &str) -> anyhow::Result<Tag> {
90
        let gix_repo = self.gix();
91
        let reference = gix_repo.find_reference(&format!("tags/{name}"))?;
92
        Tag::from_gix(reference)
93
    }
94
95
    pub fn tags(&self, offset: usize, limit: usize) -> anyhow::Result<(Vec<Tag>, bool)> {
96
        let gix_repo = self.gix();
97
        let references = gix_repo.references()?;
98
        let gix_tags = references.tags()?;
99
100
        let mut tags: Vec<Tag> = Vec::new();
101
        let mut has_next = false;
102
103
        for (i, rev) in gix_tags.skip(offset).enumerate() {
104
            if i >= limit {
105
                has_next = true;
106
                break;
107
            }
108
109
            let rev = rev.map_err(|e| anyhow::anyhow!(e))?;
110
            tags.push(Tag::from_gix(rev)?);
111
        }
112
        Ok((tags, has_next))
113
    }
114
}