~kornellapacz/gmnigit

b266bf6f6d32162d83df91c35ab4f43e3da445eb — Omar Polo 4 months ago 636c885 master
sort the tags for the refs page
1 files changed, 29 insertions(+), 12 deletions(-)

M commits.go
M commits.go => commits.go +29 -12
@@ 6,7 6,9 @@ import (
	"log"
	"os"
	"path/filepath"
	"sort"
	"text/template"
	"time"

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


@@ 93,10 95,17 @@ func createCommitsFiles(repository *git.Repository) {
	wg.Done()
}

type commit struct {
	hash    string
	refname string
	when    time.Time
}

func createRefsFile(r *git.Repository) {
	tags := []string{}
	iter, err := r.Tags()
	check(err)

	var commits []commit
	err = iter.ForEach(func(ref *plumbing.Reference) error {
		var c *object.Commit



@@ 118,19 127,21 @@ func createRefsFile(r *git.Repository) {
			return err
		}

		tag := fmt.Sprintf(
			"=> ../%s/%s.patch %s - %s",
			commitsSubPath,
			hash,
			c.Author.When.Format("2006-01-02"),
			filepath.Base(ref.Name().String()),
		)
		tags = append(tags, tag)
		commits = append(commits, commit{
			hash:    hash.String(),
			refname: ref.Name().String(),
			when:    c.Author.When,
		})

		return nil
	})
	check(err)

	sort.Slice(commits, func(i, j int) bool {
		/* reverse sort so newest is first */
		return commits[j].when.Before(commits[i].when)
	})

	refsPath := filepath.Join(distPath, refsSubPath)
	check(os.Mkdir(refsPath, os.ModePerm))



@@ 140,12 151,18 @@ func createRefsFile(r *git.Repository) {

	fmt.Fprintf(refsFile, "# %s tags\n\n", projectName)

	if len(tags) == 0 {
	if len(commits) == 0 {
		fmt.Fprintln(refsFile, "No tags yet.")
		return
	}

	for i := len(tags) - 1; i >= 0; i-- {
		fmt.Fprintln(refsFile, tags[i])
	for _, c := range commits {
		fmt.Fprintf(refsFile,
			"=> ../%s/%s.patch %s - %s\n",
			commitsSubPath,
			c.hash,
			c.when.Format("2006-01-02"),
			filepath.Base(c.refname),
		)
	}
}