add ExamineBranches()

This commit is contained in:
Jeff Carr 2025-01-17 11:00:06 -06:00
parent 0773f20d00
commit 660255c8a3
2 changed files with 83 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import (
"go.wit.com/lib/gui/shell"
"go.wit.com/log"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)
// TODO: make this report the error somewhere
@ -116,3 +117,57 @@ func ListFiles(directory string) []string {
return files
}
func (repo *Repo) ExamineBranches() *GitTag {
var hashCheck string
all := repo.GetBranches()
path := filepath.Join(repo.FullPath, ".git/refs/")
for _, b := range all {
parts := strings.Split(b, "/")
rdir := "heads"
if len(parts) == 2 {
rdir = "remotes"
}
fullfile := filepath.Join(path, rdir, b)
// check if the ref name is "HEAD". if so, skip
runeCount := utf8.RuneCountInString(fullfile)
// Convert the string to a slice of runes
runes := []rune(fullfile)
// Slice the last 4 runes
lastFour := runes[runeCount-4:]
if string(lastFour) == "HEAD" {
// assume HEAD is always a valid branch
// log.Info("skip HEAD. always valid branch name", fullfile)
continue
}
content, _ := ioutil.ReadFile(fullfile)
hash := strings.TrimSpace(string(content))
if hashCheck == "" {
hashCheck = hash
}
var cmd []string
cmd = append(cmd, "git", "show", "-s", "--format=%cI", hash)
r := shell.PathRunLog(repo.GetFullPath(), cmd, INFO)
if r.Error != nil {
log.Log(WARN, "CheckBranches() git show error:", r.Error)
}
// git show -s --format=%ci <hash> will give you the time
// log.Log(REPO, fullfile)
if hash == hashCheck {
// log.Info("notsure why this git show is here", hash)
} else {
// log.Printf("UNKNOWN BRANCH %-50s %s %s %s\n", repo.GetFullPath(), r.Stdout, cmd, b)
tag := new(GitTag)
tag.Refname = b
tag.Hash = hash
if len(r.Stdout) > 0 {
tagtime := parseDateRFC3339(r.Stdout[0])
tag.Creatordate = timestamppb.New(tagtime)
}
return tag
}
}
return nil
}

View File

@ -81,6 +81,34 @@ func (repo *Repo) reloadGitTags() error {
return nil
}
// attempt to parse "2024-12-13 15:39:57 -0600"
func parseGitDate(dateString string) time.Time {
// now := time.Now().Format("Wed Feb 7 10:13:38 2024 -0600")
const gitLayout = "2006-01-02 15:04:05 -0600"
tagTime, err := time.Parse(gitLayout, dateString)
if err != nil {
const gitLayout2 = "2006-01-02 15:04:05 +0600"
tagTime, err = time.Parse(gitLayout2, dateString)
}
if err != nil {
log.Warn("GOT THIS IN PARSE AAA." + dateString + ".AAA")
log.Warn(err)
return time.Now()
}
return tagTime
}
// attempt to parse strict ISO 8601 format // 2025-01-07T21:22:16-06:00
func parseDateRFC3339(dateString string) time.Time {
tagTime, err := time.Parse(time.RFC3339, dateString)
if err != nil {
log.Warn("GOT THIS IN PARSE AAA." + dateString + ".AAA")
log.Warn(err)
return time.Now()
}
return tagTime
}
// 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")