try to fix Current branch and version

This commit is contained in:
Jeff Carr 2024-12-17 20:48:23 -06:00
parent 10cc012a12
commit bea54091d2
4 changed files with 88 additions and 4 deletions

View File

@ -21,3 +21,17 @@ func (repo *Repo) GetGoPath() string {
} }
return repo.GoInfo.GoPath return repo.GoInfo.GoPath
} }
func (repo *Repo) GetGoPrimitive() bool {
if repo.GoInfo == nil {
return false
}
return repo.GoInfo.GoPrimitive
}
func (repo *Repo) SetGoPrimitive(b bool) {
if repo.GoInfo == nil {
repo.GoInfo = new(GoInfo)
}
repo.GoInfo.GoPrimitive = b
}

View File

@ -18,7 +18,7 @@ import (
// deprecate use of IsPrimitive() to this function // deprecate use of IsPrimitive() to this function
// this assumes go.mod and go.sum are in a releasable state // this assumes go.mod and go.sum are in a releasable state
func (repo *Repo) SetPrimitive() error { func (repo *Repo) SetPrimitive() error {
_, err := repo.IsPrimitive() _, err := repo.computePrimitive()
return err return err
} }
@ -26,7 +26,7 @@ func (repo *Repo) SetPrimitive() error {
// will return true if the repo is truly not dependent on _anything_ else // will return true if the repo is truly not dependent on _anything_ else
// like spew or lib/widget // like spew or lib/widget
// it assumes go mod ran init and tidy ran without error // it assumes go mod ran init and tidy ran without error
func (repo *Repo) IsPrimitive() (bool, error) { func (repo *Repo) computePrimitive() (bool, error) {
// go mod init & go mod tidy ran without errors // go mod init & go mod tidy ran without errors
log.Log(GITPB, "isPrimativeGoMod()", repo.FullPath) log.Log(GITPB, "isPrimativeGoMod()", repo.FullPath)
tmp := filepath.Join(repo.FullPath, "go.mod") tmp := filepath.Join(repo.FullPath, "go.mod")

View File

@ -44,6 +44,56 @@ func (repo *Repo) changedDir() bool {
return true return true
} }
func (repo *Repo) didFileChange(fname string, pbtime *timestamppb.Timestamp) bool {
fileTime := repo.Mtime(fname)
if fileTime == nil {
// file missing, assume changed
return true
}
mtime := timestamppb.New(*fileTime)
if pbtime == nil {
// mtime has not been stored yet
return true
}
if (pbtime.Seconds == mtime.Seconds) && (pbtime.Nanos == mtime.Nanos) {
// it's the same!
return false
}
// need to reload from the filesystem
return false
}
// boo. I'm not good at golang. this should use reflect. I'm bad. my code is bad. boo this man. you're cool, I'm outta here
// make this work right someday
func (repo *Repo) updateMtime(fname string, pbname string) bool {
fileTime := repo.Mtime(fname)
if fileTime == nil {
// .git/HEAD doesn't exist. something is wrong. rescan this repo
return true
}
mtime := timestamppb.New(*fileTime)
pbtime := repo.Times.MtimeHead
if pbtime == nil { // this can happen?
repo.Times.MtimeHead = mtime
return true
}
switch pbname {
case "MtimeHead":
if pbtime == nil { // this can happen?
repo.Times.MtimeHead = mtime
return true
}
default:
}
if (pbtime.Seconds == mtime.Seconds) && (pbtime.Nanos == mtime.Nanos) {
return false
}
dur := mtime.AsTime().Sub(pbtime.AsTime())
repo.StateChange = fmt.Sprintf("%s changed %s", fname, shell.FormatDuration(dur))
repo.Times.MtimeHead = mtime
return true
}
func (repo *Repo) changedHead() bool { func (repo *Repo) changedHead() bool {
fname := ".git/HEAD" fname := ".git/HEAD"
fileTime := repo.Mtime(fname) fileTime := repo.Mtime(fname)
@ -89,7 +139,7 @@ func (repo *Repo) changedIndex() bool {
return true return true
} }
func (repo *Repo) RepoChanged() bool { func (repo *Repo) updateMtimes() bool {
var changed bool var changed bool
if repo.Times == nil { if repo.Times == nil {
repo.Times = new(GitTimes) repo.Times = new(GitTimes)
@ -108,3 +158,17 @@ func (repo *Repo) RepoChanged() bool {
return changed return changed
} }
func (repo *Repo) DidRepoChange() bool {
if repo.didFileChange(".git/HEAD", repo.Times.MtimeHead) {
return true
}
if repo.didFileChange(".git/index", repo.Times.MtimeIndex) {
return true
}
if repo.didFileChange(".git", repo.Times.MtimeDir) {
// todo: do something with CheckDirty()
// return true
}
return false
}

View File

@ -6,19 +6,25 @@ import (
"go.wit.com/log" "go.wit.com/log"
) )
// TODO: fix and clean this up. this is a work in progress
func (repo *Repo) Reload() error { func (repo *Repo) Reload() error {
repo.Tags = new(GitTags) repo.Tags = new(GitTags)
repo.reloadGitTags() repo.reloadGitTags()
repo.GoDeps = new(GoDeps) repo.GoDeps = new(GoDeps)
if repo.GoInfo == nil {
repo.GoInfo = new(GoInfo)
}
repo.ParseGoSum() repo.ParseGoSum()
repo.setLastTag() repo.setLastTag()
repo.setCurrentBranchName() repo.setCurrentBranchName()
repo.setCurrentBranchVersion()
repo.setRepoType() repo.setRepoType()
// everything has been checked, now save the mtime's // everything has been checked, now save the mtime's
repo.RepoChanged() repo.updateMtimes()
return nil return nil
} }