gitpb/refs.update.go

77 lines
1.7 KiB
Go
Raw Normal View History

2024-11-26 04:34:01 -06:00
package gitpb
2024-11-26 05:12:28 -06:00
import (
"slices"
"strings"
"time"
"go.wit.com/lib/gui/shell"
"go.wit.com/log"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)
2024-11-27 14:41:57 -06:00
// Update repo.Refs from .git/
func (repo *Repo) UpdateGit() error {
2024-11-26 05:12:28 -06:00
// delete the old hash
// r.DeleteByHash(hash)
2024-11-27 14:41:57 -06:00
repo.Refs = nil
2024-11-26 05:12:28 -06:00
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, " "))
2024-11-27 14:41:57 -06:00
result := shell.PathRunQuiet(repo.FullPath, cmd)
2024-11-26 05:12:28 -06:00
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
2024-11-26 04:34:01 -06:00
}
2024-11-26 05:12:28 -06:00
refName = parts[3]
hash = parts[0]
ctime = getGitDateStamp(parts[1])
subject = parts[4]
2024-11-26 04:34:01 -06:00
}
2024-11-26 05:12:28 -06:00
newr := Ref{
Hash: hash,
Subject: subject,
RefName: refName,
Ctime: timestamppb.New(ctime),
2024-11-26 04:34:01 -06:00
}
2024-11-27 14:41:57 -06:00
repo.AppendRef(&newr)
2024-11-26 05:12:28 -06:00
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
2024-11-26 04:34:01 -06:00
}