~kornellapacz/public-inbox

generate refs page with tags v1 APPLIED

Omar Polo
Omar Polo: 1
 generate refs page with tags

 3 files changed, 36 insertions(+), 0 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~kornellapacz/public-inbox/patches/22254/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH] generate refs page with tags Export this patch

Omar Polo
The following patch creates an additional file in "/refs/index.gmi" with
the list of the tags and links to the relevant commit.  It follows the
companion specification for subscriptions, so users can easily subscribe
to that page to know when a release is tagged.

Please note that if a tag points to a commit that isn't reachable from
any branch, the link won't work.  This can happen if one created a
branch off a tag, done some work, tagged a new version and deleted the
branch.
---
 commits.go | 33 +++++++++++++++++++++++++++++++++
 files.go   |  1 +
 main.go    |  2 ++
 3 files changed, 36 insertions(+)

diff --git a/commits.go b/commits.go
index c2e735c..cdf6e35 100644
--- a/commits.go
+++ b/commits.go
@@ -1,11 +1,13 @@
package main

import (
	"fmt"
	"os"
	"path/filepath"
	"text/template"

	"github.com/go-git/go-git/v5"
	"github.com/go-git/go-git/v5/plumbing"
	"github.com/go-git/go-git/v5/plumbing/object"
)

@@ -78,3 +80,34 @@ func createCommitsFiles(repository *git.Repository) {
	check(commitsIndex.Close())
	wg.Done()
}

func createRefsFile(r *git.Repository) {
	refsPath := filepath.Join(distPath, refsSubPath)
	check(os.Mkdir(refsPath, os.ModePerm))

	refsFile, err := os.Create(filepath.Join(refsPath, "index.gmi"))
	check(err)
	defer refsFile.Close()

	fmt.Fprintf(refsFile, "# %s tags\n\n", filepath.Base(repositoryPath))

	iter, err := r.Tags()
	check(err)
	err = iter.ForEach(func(ref *plumbing.Reference) error {
		c, err := r.CommitObject(ref.Hash())
		if err != nil {
			return err
		}

		fmt.Fprintf(
			refsFile,
			"=> ../%s/%s.patch %s - %s\n",
			commitsSubPath,
			ref.Hash(),
			c.Author.When.Format("2006-01-02"),
			filepath.Base(ref.Name().String()),
		)
		return nil
	})
	check(err)
}
diff --git a/files.go b/files.go
index 0ba60e6..4da3869 100644
--- a/files.go
+++ b/files.go
@@ -121,6 +121,7 @@ func createIndexFile() {
	}

	indexFile.WriteString("=> " + commitsSubPath + "/\n")
	indexFile.WriteString("=> " + refsSubPath + "/\n")
	indexFile.WriteString("=> " + treeSubPath + "/\n\n")

	// convert README.md to gemini and write it to index.gmi in repository root
diff --git a/main.go b/main.go
index cd18b18..0ea4047 100644
--- a/main.go
+++ b/main.go
@@ -14,6 +14,7 @@ var wg sync.WaitGroup
const (
	treeSubPath    = "tree"
	commitsSubPath = "commits"
	refsSubPath    = "refs"
)

var (
@@ -54,6 +55,7 @@ func main() {
	wg.Add(1)

	go createCommitsFiles(repository)
	createRefsFile(repository)
	createBrowsableTree()
	createIndexFile()

-- 
2.31.1