summaryrefslogtreecommitdiff
path: root/refs.update.go
blob: 3266fd16dea57eadbb321115e9ef89e5e8cf21e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package gitpb

import (
	"slices"
	"strings"
	"time"

	"go.wit.com/lib/gui/shell"
	"go.wit.com/log"
	timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)

// Update repo.Refs from .git/
func (repo *Repo) UpdateGit() error {
	// delete the old hash
	// r.DeleteByHash(hash)
	repo.Refs = nil

	tags := []string{"%(objectname)", "%(creatordate)", "%(*authordate)", "%(refname)", "%(subject)"}
	format := strings.Join(tags, "_,,,_")
	cmd := []string{"git", "for-each-ref", "--sort=taggerdate", "--format", format}
	// log.Info("RUNNING:", strings.Join(cmd, " "))
	result := shell.PathRunQuiet(repo.FullPath, cmd)
	if result.Error != nil {
		log.Warn("git for-each-ref error:", result.Error)
		return result.Error
	}

	lines := result.Stdout
	// reverse the git order
	slices.Reverse(lines)

	var refName string
	var hash string
	var subject string
	var ctime time.Time

	for i, line := range lines {
		var parts []string
		parts = make([]string, 0)
		parts = strings.Split(line, "_,,,_")
		if len(parts) != 5 {
			log.Info("tag error:", i, parts)
			continue
		}
		refName = parts[3]
		hash = parts[0]

		ctime = getGitDateStamp(parts[1])

		subject = parts[4]
	}

	newr := Ref{
		Hash:    hash,
		Subject: subject,
		RefName: refName,
		Ctime:   timestamppb.New(ctime),
	}

	repo.AppendRef(&newr)
	return nil
}

// converts a git for-each-ref date. "Wed Feb 7 10:13:38 2024 -0600"
func getGitDateStamp(gitdefault string) time.Time {
	// now := time.Now().Format("Wed Feb 7 10:13:38 2024 -0600")
	const gitLayout = "Mon Jan 2 15:04:05 2006 -0700"
	tagTime, err := time.Parse(gitLayout, gitdefault)
	if err != nil {
		log.Warn("GOT THIS IN PARSE AAA." + gitdefault + ".AAA")
		log.Warn(err)
		return time.Now()
	}
	return tagTime
}