From bea54091d2901d00c03541be09f33ae29e72cde1 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Tue, 17 Dec 2024 20:48:23 -0600 Subject: try to fix Current branch and version --- changed.go | 110 ------------------------------------ common.go | 14 +++++ isPrimitive.go | 4 +- mtime.go | 174 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ reload.go | 8 ++- 5 files changed, 197 insertions(+), 113 deletions(-) delete mode 100644 changed.go create mode 100644 mtime.go diff --git a/changed.go b/changed.go deleted file mode 100644 index 4d6fed4..0000000 --- a/changed.go +++ /dev/null @@ -1,110 +0,0 @@ -package gitpb - -// An app to submit patches for the 30 GO GUI repos - -import ( - "fmt" - "time" - - "go.wit.com/lib/gui/shell" - "go.wit.com/log" - "google.golang.org/protobuf/types/known/timestamppb" -) - -func (repo *Repo) Mtime(fname string) *time.Time { - var fileTime *time.Time - tmp, err := repo.oldMtime(fname) - fileTime = &tmp - if err != nil { - log.Info("MTime got err", err) - return nil - } - return fileTime -} - -func (repo *Repo) changedDir() bool { - fname := ".git" - fileTime := repo.Mtime(fname) - if fileTime == nil { - // .git doesn't exist. something is wrong. rescan this repo - return true - } - mtime := timestamppb.New(*fileTime) - pbtime := repo.Times.MtimeDir - if pbtime == nil { // this can happen? - repo.Times.MtimeDir = mtime - return true - } - 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.MtimeDir = mtime - return true -} - -func (repo *Repo) changedHead() bool { - fname := ".git/HEAD" - 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 - } - - 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) changedIndex() bool { - fname := ".git/index" - fileTime := repo.Mtime(fname) - if fileTime == nil { - // .git/index doesn't exist. something is wrong. rescan this repo - return true - } - mtime := timestamppb.New(*fileTime) - pbtime := repo.Times.MtimeIndex - if pbtime == nil { // this can happen? - repo.Times.MtimeIndex = mtime - return true - } - 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.MtimeIndex = mtime - return true -} - -func (repo *Repo) RepoChanged() bool { - var changed bool - if repo.Times == nil { - repo.Times = new(GitTimes) - log.Info(repo.FullPath, "repo.Times were nil") - } - - if repo.changedHead() { - changed = true - } - if repo.changedIndex() { - changed = true - } - if repo.changedDir() { - // changed = true - } - - return changed -} diff --git a/common.go b/common.go index cd88d45..6065b8c 100644 --- a/common.go +++ b/common.go @@ -21,3 +21,17 @@ func (repo *Repo) GetGoPath() string { } 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 +} diff --git a/isPrimitive.go b/isPrimitive.go index a632e1a..a3ecbba 100644 --- a/isPrimitive.go +++ b/isPrimitive.go @@ -18,7 +18,7 @@ import ( // deprecate use of IsPrimitive() to this function // this assumes go.mod and go.sum are in a releasable state func (repo *Repo) SetPrimitive() error { - _, err := repo.IsPrimitive() + _, err := repo.computePrimitive() return err } @@ -26,7 +26,7 @@ func (repo *Repo) SetPrimitive() error { // will return true if the repo is truly not dependent on _anything_ else // like spew or lib/widget // 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 log.Log(GITPB, "isPrimativeGoMod()", repo.FullPath) tmp := filepath.Join(repo.FullPath, "go.mod") diff --git a/mtime.go b/mtime.go new file mode 100644 index 0000000..17cec9c --- /dev/null +++ b/mtime.go @@ -0,0 +1,174 @@ +package gitpb + +// An app to submit patches for the 30 GO GUI repos + +import ( + "fmt" + "time" + + "go.wit.com/lib/gui/shell" + "go.wit.com/log" + "google.golang.org/protobuf/types/known/timestamppb" +) + +func (repo *Repo) Mtime(fname string) *time.Time { + var fileTime *time.Time + tmp, err := repo.oldMtime(fname) + fileTime = &tmp + if err != nil { + log.Info("MTime got err", err) + return nil + } + return fileTime +} + +func (repo *Repo) changedDir() bool { + fname := ".git" + fileTime := repo.Mtime(fname) + if fileTime == nil { + // .git doesn't exist. something is wrong. rescan this repo + return true + } + mtime := timestamppb.New(*fileTime) + pbtime := repo.Times.MtimeDir + if pbtime == nil { // this can happen? + repo.Times.MtimeDir = mtime + return true + } + 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.MtimeDir = mtime + 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 { + fname := ".git/HEAD" + 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 + } + + 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) changedIndex() bool { + fname := ".git/index" + fileTime := repo.Mtime(fname) + if fileTime == nil { + // .git/index doesn't exist. something is wrong. rescan this repo + return true + } + mtime := timestamppb.New(*fileTime) + pbtime := repo.Times.MtimeIndex + if pbtime == nil { // this can happen? + repo.Times.MtimeIndex = mtime + return true + } + 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.MtimeIndex = mtime + return true +} + +func (repo *Repo) updateMtimes() bool { + var changed bool + if repo.Times == nil { + repo.Times = new(GitTimes) + log.Info(repo.FullPath, "repo.Times were nil") + } + + if repo.changedHead() { + changed = true + } + if repo.changedIndex() { + changed = true + } + if repo.changedDir() { + // changed = true + } + + 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 +} diff --git a/reload.go b/reload.go index 0168ea8..f86001c 100644 --- a/reload.go +++ b/reload.go @@ -6,19 +6,25 @@ import ( "go.wit.com/log" ) +// TODO: fix and clean this up. this is a work in progress func (repo *Repo) Reload() error { repo.Tags = new(GitTags) repo.reloadGitTags() repo.GoDeps = new(GoDeps) + if repo.GoInfo == nil { + repo.GoInfo = new(GoInfo) + } repo.ParseGoSum() repo.setLastTag() repo.setCurrentBranchName() + repo.setCurrentBranchVersion() + repo.setRepoType() // everything has been checked, now save the mtime's - repo.RepoChanged() + repo.updateMtimes() return nil } -- cgit v1.2.3