Goit

Simple and lightweight Git web server
git clone http://git.omkov.net/Goit
Log | Tree | Refs | README | Download

AuthorJakob Wakeling <[email protected]>
Date2023-12-23 04:43:05
Commit2af4be7f45a151bffe8c6c1b1bcb9d99c1cde57e
Parentb4b291ece361bb22761f24eac29936f15f83a2c5

Handle annotated tags in repository refs page

Diffstat

M src/repo/refs.go | 31 ++++++++++++++++++++++++-------

1 files changed, 24 insertions, 7 deletions

diff --git a/src/repo/refs.go b/src/repo/refs.go
index af7b88e..152df60 100644
--- a/src/repo/refs.go
+++ b/src/repo/refs.go
@@ -8,6 +8,7 @@ import (
 	"log"
 	"net/http"
 	"path/filepath"
+	"slices"
 	"strings"
 	"time"
 
@@ -16,6 +17,7 @@ import (
 	"github.com/go-chi/chi/v5"
 	"github.com/go-git/go-git/v5"
 	"github.com/go-git/go-git/v5/plumbing"
+	"github.com/go-git/go-git/v5/plumbing/object"
 )
 
 func HandleRefs(w http.ResponseWriter, r *http.Request) {
@@ -93,27 +95,42 @@ func HandleRefs(w http.ResponseWriter, r *http.Request) {
 	}
 
 	if iter, err := gr.Tags(); err != nil {
-		log.Println("[Repo:Refs]", err.Error())
+		log.Println("[/repo/refs]", err.Error())
 		goit.HttpError(w, http.StatusInternalServerError)
 		return
 	} else if err := iter.ForEach(func(r *plumbing.Reference) error {
-		commit, err := gr.CommitObject(r.Hash())
-		if err != nil {
-			return err
+		var c *object.Commit
+
+		if tag, err := gr.TagObject(r.Hash()); err != nil {
+			if !errors.Is(err, plumbing.ErrObjectNotFound) {
+				return err
+			}
+		} else {
+			if c, err = gr.CommitObject(tag.Target); err != nil {
+				return err
+			}
+		}
+
+		if c == nil {
+			if c, err = gr.CommitObject(r.Hash()); err != nil {
+				return err
+			}
 		}
 
 		data.Tags = append(data.Tags, row{
-			Name: r.Name().Short(), Message: strings.SplitN(commit.Message, "\n", 2)[0], Author: commit.Author.Name,
-			LastCommit: commit.Author.When.UTC().Format(time.DateTime), Hash: r.Hash().String(),
+			Name: r.Name().Short(), Message: strings.SplitN(c.Message, "\n", 2)[0], Author: c.Author.Name,
+			LastCommit: c.Author.When.UTC().Format(time.DateTime), Hash: r.Hash().String(),
 		})
 
 		return nil
 	}); err != nil {
-		log.Println("[Repo:Refs]", err.Error())
+		log.Println("[/repo/refs]", err.Error())
 		goit.HttpError(w, http.StatusInternalServerError)
 		return
 	}
 
+	slices.Reverse(data.Tags)
+
 	if err := goit.Tmpl.ExecuteTemplate(w, "repo/refs", data); err != nil {
 		log.Println("[/repo/refs]", err.Error())
 	}