summaryrefslogtreecommitdiff
path: root/reloadMtime.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-01-08 02:38:50 -0600
committerJeff Carr <[email protected]>2025-01-08 02:38:50 -0600
commitebda2ea222ed9f1d348d5d3e0cffb9c9f9c0acec (patch)
tree09b457c7cd588f3f47e65d53d26b531e1db5f795 /reloadMtime.go
parentd4a31b8e0bd7cf96b9e02bd3f3943d62059645d4 (diff)
keep working the problem
Diffstat (limited to 'reloadMtime.go')
-rw-r--r--reloadMtime.go189
1 files changed, 189 insertions, 0 deletions
diff --git a/reloadMtime.go b/reloadMtime.go
new file mode 100644
index 0000000..7c18e9f
--- /dev/null
+++ b/reloadMtime.go
@@ -0,0 +1,189 @@
+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 {
+ repo.StateChange = fmt.Sprintf("%s missing", fname)
+ return true
+ }
+ mtime := timestamppb.New(*fileTime)
+ if pbtime == nil {
+ repo.StateChange = fmt.Sprintf("%s mtime never recorded", fname)
+ return true
+ }
+ if (pbtime.Seconds == mtime.Seconds) && (pbtime.Nanos == mtime.Nanos) {
+ // it's the same!
+ return false
+ }
+ dur := mtime.AsTime().Sub(pbtime.AsTime())
+ repo.StateChange = fmt.Sprintf("%s mtime changed %s", fname, shell.FormatDuration(dur))
+ // need to reload from the filesystem
+ return true
+}
+
+// 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) reloadMtimes() 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.Times == nil {
+ repo.Times = new(GitTimes)
+ }
+ 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
+ }
+ if repo.Times.LastUpdate == nil {
+ log.Info("repo.Reload() was never run")
+ return true
+ } else {
+ if repo.Times.LastUpdate.Seconds < repo.Times.MtimeHead.Seconds {
+ log.Info("SHOULD RUN Reload() here", repo.Times.MtimeHead.Seconds-repo.Times.LastUpdate.Seconds, "secs diff")
+ return true
+ }
+ }
+ // log.Info("DidRepoChange() is false", repo.FullPath)
+ return false
+}