index : gitrs
A lightweight web interface for browsing Git repositories, implemented in Rust
about summary refs log tree commit diff

Diffstat (limited to 'src/git/tag.rs')

-rw-r--r-- src/git/tag.rs 49
 1 file changed, 17 insertions(+), 32 deletions(-)

diff --git a/src/git/tag.rs b/src/git/tag.rs
index 259c161..54f4b78 100644
--- a/src/git/tag.rs
+++ b/src/git/tag.rs
@@ -28,53 +28,39 @@
}

impl Tag {
    /// Builds a `Tag` from a `refs/tags/*` Git reference.
    ///
    /// Git has two types of tags:
    /// 1. **Annotated tags** (`git tag -a`): store a dedicated Tag object with a tagger, date, and message.
    /// 2. **Lightweight tags** (`git tag`): just a reference pointing directly at a Commit (no Tag object).
    pub fn from_gix(mut gix: gix::Reference<'_>) -> anyhow::Result<Tag> {
        let name = gix.name().shorten().to_string();
        let id = gix.id().to_string();

        if let Ok(tag_obj) = gix.peel_to_tag() {
            // Case 1: Annotated tag
        let (message, tagger, tagged_object, time) = if let Ok(tag_obj) = gix.peel_to_tag() {
            let tag_ref = tag_obj.decode()?;
            let message = tag_ref.message.to_string();
            let tagged_object =
                TaggedObject::from_kind_and_id(tag_ref.target_kind, tag_ref.target.to_string());
            let tagger = tag_ref.tagger()?.map(GitSignature::from_gix);

            let time = match &tagger {
                Some(t) => t.time.clone(),
                None => Self::commit_time(&mut gix)?,
            };

            Ok(Tag {
                name,
                id: gix.id().to_string(),
                message,
                tagger,
                tagged_object,
                time,
            })
            (message, tagger, tagged_object, time)
        } else {
            // Case 2: Lightweight tag (points directly to a Commit or Tree)
            let target = gix
                .follow_to_object()
                .context("Failed to resolve lightweight tag target")?;
            let target_kind = target.header()?.kind();
            let tagged_object = TaggedObject::from_kind_and_id(target_kind, target.to_string());
            let tagged_object =
                TaggedObject::from_kind_and_id(target.header()?.kind(), target.to_string());
            let time = Self::commit_time(&mut gix)?;

            Ok(Tag {
                name,
                id: gix.id().to_string(),
                message: String::new(),
                tagger: None,
                tagged_object,
                time,
            })
        }
            (String::new(), None, tagged_object, time)
        };

        Ok(Tag {
            name,
            id,
            message,
            tagger,
            tagged_object,
            time,
        })
    }

    fn commit_time(gix: &mut gix::Reference<'_>) -> anyhow::Result<Time> {
@@ -97,7 +83,7 @@
        let references = gix_repo.references()?;
        let gix_tags = references.tags()?;

        let mut tags: Vec<Tag> = Vec::new();
        let mut tags = Vec::new();
        let mut has_next = false;

        for (i, rev) in gix_tags.skip(offset).enumerate() {
@@ -105,7 +91,6 @@
                has_next = true;
                break;
            }

            let rev = rev.map_err(|e| anyhow::anyhow!(e))?;
            tags.push(Tag::from_gix(rev)?);
        }