gitpb/mtime.go

181 lines
4.5 KiB
Go
Raw Permalink Normal View History

2024-12-17 00:00:49 -06:00
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)
2024-12-17 06:37:14 -06:00
pbtime := repo.Times.MtimeDir
if pbtime == nil { // this can happen?
repo.Times.MtimeDir = mtime
return true
}
2024-12-17 00:00:49 -06:00
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
}
2024-12-17 20:48:23 -06:00
func (repo *Repo) didFileChange(fname string, pbtime *timestamppb.Timestamp) bool {
fileTime := repo.Mtime(fname)
if fileTime == nil {
2024-12-17 21:15:19 -06:00
repo.StateChange = fmt.Sprintf("%s missing", fname)
2024-12-17 20:48:23 -06:00
return true
}
mtime := timestamppb.New(*fileTime)
if pbtime == nil {
2024-12-17 21:15:19 -06:00
repo.StateChange = fmt.Sprintf("%s mtime never recorded", fname)
2024-12-17 20:48:23 -06:00
return true
}
if (pbtime.Seconds == mtime.Seconds) && (pbtime.Nanos == mtime.Nanos) {
// it's the same!
return false
}
2024-12-17 21:15:19 -06:00
dur := mtime.AsTime().Sub(pbtime.AsTime())
repo.StateChange = fmt.Sprintf("%s mtime changed %s", fname, shell.FormatDuration(dur))
2024-12-17 20:48:23 -06:00
// need to reload from the filesystem
2024-12-17 21:15:19 -06:00
return true
2024-12-17 20:48:23 -06:00
}
// 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
}
2024-12-17 00:00:49 -06:00
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)
2024-12-17 06:37:14 -06:00
pbtime := repo.Times.MtimeHead
if pbtime == nil { // this can happen?
repo.Times.MtimeHead = mtime
return true
}
2024-12-17 00:00:49 -06:00
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)
2024-12-17 06:37:14 -06:00
pbtime := repo.Times.MtimeIndex
if pbtime == nil { // this can happen?
repo.Times.MtimeIndex = mtime
return true
}
2024-12-17 00:00:49 -06:00
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
}
2024-12-17 20:48:23 -06:00
func (repo *Repo) updateMtimes() bool {
2024-12-17 00:00:49 -06:00
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() {
2024-12-17 06:37:14 -06:00
// changed = true
2024-12-17 00:00:49 -06:00
}
return changed
}
2024-12-17 20:48:23 -06:00
func (repo *Repo) DidRepoChange() bool {
2024-12-18 23:06:27 -06:00
if repo.Times == nil {
repo.Times = new(GitTimes)
}
2024-12-17 20:48:23 -06:00
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
}
2024-12-17 21:15:19 -06:00
// log.Info("DidRepoChange() is false", repo.FullPath)
2024-12-17 20:48:23 -06:00
return false
}