summaryrefslogtreecommitdiff
path: root/mtime.go
blob: 17cec9c141484ea9fc847c670ed0b324f2dc70ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
}