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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
package main
import (
"errors"
"strings"
"go.wit.com/lib/cobol"
"go.wit.com/lib/env"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log"
"google.golang.org/protobuf/types/known/timestamppb"
)
func doStats() (string, error) {
var allerr error
if argv.All {
for r := range me.forge.Repos.IterByFullPath() {
if r.Stats().Len() == 0 {
_, err := doStatsRepo(r)
allerr = errors.Join(allerr, err)
}
}
return "verify ran everywhere", nil
}
repo := workingDirToRepo()
if repo == nil {
return "no repo", errors.New("working dir isn't a repo I know about")
}
if argv.Verify.Stats.List {
s := repo.Stats()
s.SortPatchId()
// s.SortCtime()
footer := s.PrintTableLimit(-1)
return "stats table: " + footer, nil
}
s, err := doStatsRepo(repo)
allerr = errors.Join(allerr, err)
user, err := refHash(repo, "heads/"+repo.GetUserBranchName())
allerr = errors.Join(allerr, err)
master, err := refHash(repo, "remotes/origin/master")
allerr = errors.Join(allerr, err)
HEAD, err := refHash(repo, "remotes/origin/HEAD")
allerr = errors.Join(allerr, err)
log.Printf("user=%10.10s master=%10.10s HEAD=%10.10s\n", user, master, HEAD)
safeDelete(repo, "refs/heads/"+repo.GetUserBranchName(), "refs/remotes/origin/HEAD") // delete user if safely contained in HEAD
safeDelete(repo, user, HEAD) // checkes by hash ID // delete user if safely contained in HEAD
return s, allerr
}
func doStatsRepo(r *gitpb.Repo) (string, error) {
var allerr error
pb, err := r.LoadStats()
if err == nil {
log.Info("LoadStats() ok", pb.Filename)
} else {
log.Info("LoadStats() err", err)
}
allerr = errors.Join(allerr, err)
if hasOrigin(r) {
log.Info("todo: detect origin")
}
// collect the stats
counter, err := last100(r, pb)
allerr = errors.Join(allerr, err)
s := log.Sprintf("found %d new hashes", counter)
for stat := range pb.IterAll() {
if stat.PatchId == "" {
stat.PatchId, err = r.FindPatchIdByHash(stat.Hash)
allerr = errors.Join(allerr, err)
log.Info("patchid for hash", stat.Hash, "is", stat.PatchId)
counter += 1
}
}
if counter > 0 {
pb.SaveVerbose()
}
return s, nil
}
func collectStats(r *gitpb.Repo, pb *gitpb.Stats) error {
return nil
}
// git show-ref refs/heads/devel
func refHash(r *gitpb.Repo, name string) (string, error) {
var hash string
refname := "refs/" + name
cmd := []string{"git", "show-ref", refname}
cmdout := r.Run(cmd)
for i, line := range cmdout.Stdout {
parts := strings.Fields(line)
if env.If("stats") {
log.Info(parts[0], "LINE:", i, line)
}
hash = parts[0]
}
if len(cmdout.Stdout) != 1 {
return "", errors.New("no refname:" + name)
}
return hash, nil
}
// git show-ref --verify refs/heads/devel
func hasOrigin(r *gitpb.Repo) bool {
// git show-ref refs/heads/devel
return true
}
var standardFmts []string = []string{"H", "T", "at", "ct", "f"}
var standardSeperator string = "___FORGE___"
func makeFmts() string {
// fmts := strings.Fields(config.GetPanic("standardFmts"))
// fmts := strings.Fields(config.GetPanic("standardSeperator"))
var all []string
for _, fmtvar := range standardFmts {
all = append(all, "%"+fmtvar)
}
return "--format=" + strings.Join(all, standardSeperator)
}
// the correct syntax for
// git log -n 8 --format=%H%00%ae%00%as%00%s origin/HEAD
func last100(r *gitpb.Repo, pb *gitpb.Stats) (int, error) {
var allerr error
var counter int
cmd := []string{"git", "log", "-n", "100", makeFmts(), "origin/" + r.GetMasterBranchName()} // must use 'master' as queried from the git server
// cmd := []string{"git", "log", "-n", "100", makeFmts(), "origin/HEAD"} // HEAD is _NOT_ always set
if env.If("stats") {
log.Info("Run:", cmd)
}
cmdout := r.Run(cmd)
for i, line := range cmdout.Stdout {
parts := strings.Split(line, standardSeperator)
hash := parts[0]
if env.If("stats") {
log.Printf("LINE:%8.8s %2d %v\n", hash, i, parts[1:])
}
found := pb.FindByHash(hash)
if found != nil {
// already have this hash
continue
}
counter += 1
astat := new(gitpb.Stat)
astat.Hash = hash
ctime, err := cobol.GetTime(parts[2])
allerr = errors.Join(allerr, err)
astat.Ctime = timestamppb.New(*ctime)
astat.Subject = parts[4]
astat.Type = gitpb.Stat_REMOTE
pb.Append(astat)
}
return counter, allerr
}
func findPatchIdInStats(pb *gitpb.Stats, patchId string) *gitpb.Stat {
for stat := range pb.IterAll() {
if stat.PatchId == patchId {
return stat
}
}
// log.Info("findPatchId searched in", pb.Len(), "stats")
return nil
}
// delete localRef if it's completely contained in the masterRef
func safeDelete(r *gitpb.Repo, deleteHash string, keepHash string) error {
// compare the branches
hashok, hashbad, cmd1, cmd2, err := r.CompareHashes(keepHash, deleteHash)
if err != nil {
// things are really really messed up. might be 'branchless' at this point (?)
log.Printf("%-13.13s %-55.55s err='%v' %s %v\n", "CMD ERR", r.FullPath, err, "NOT SAFE TO DELETE. Reload()?", cmd1)
log.Printf("%-13.13s %-55.55s err='%v' %s %v\n", "CMD ERR", r.FullPath, err, "NOT SAFE TO DELETE. Reload()?", cmd2)
return err
}
// things only in the master branch (safe to ignore)
for _, line := range hashok {
parts := strings.Split(line, "%00") // git log doesn't actually convert %00 to NULL
patchId, err := r.FindPatchIdByHash(parts[0])
_ = err
log.Printf("%-13.13s %-55.55s hId %10.10s pId %10.10s %v\n", "OK delete", r.FullPath, parts[0], patchId, parts[2:])
}
if len(hashbad) > 0 {
log.Printf("%-13.13s %v\n", "BAD cmd", cmd1)
}
var ACTUALLYOK bool = true
// things still only in the local branch (bad to delete)
for _, line := range hashbad {
parts := strings.Split(line, "%00") // git log doesn't actually convert %00 to NULL
patchId, err := r.FindPatchIdByHash(parts[0])
_ = err
searchResult := log.Sprintf("NOPE(%d)", r.Stats().Len())
stat := findPatchIdInStats(r.Stats(), patchId)
if stat != nil {
searchResult = log.Sprintf("FOUND %10.10s %s", stat.PatchId, stat.Subject)
} else {
ACTUALLYOK = false
}
log.Printf("%-13.13s %-55.55s hId %10.10s pId %10.10s %s %v\n", "BAD keep", r.FullPath, parts[0], patchId, searchResult, parts[2:])
}
if ACTUALLYOK {
// todo: force checkout to local master branch
// before doing this
cmd := []string{"git", "update-ref", "-d", deleteHash}
// log.Printf("%-13.13s %-55.55s %v %s\n", "CMD OK", r.FullPath, cmd1, "")
// log.Printf("%-13.13s %-55.55s %v %s\n", "CMD OK", r.FullPath, cmd2, "")
if r.GetCurrentBranchName() == r.GetMasterBranchName() {
log.Printf("%-55.55s %v %s\n", r.FullPath, cmd, "SAFE TO DELETE add --fix")
} else {
log.Printf("%-55.55s %v %s\n", r.FullPath, cmd, "SAFE TO DELETE BUT NOT ON MASTER BRANCH add --fix")
}
if argv.Fix {
err := r.RunVerbose(cmd)
if err != nil {
log.Info(deleteHash, r.FullPath)
s := "local user branch could not be deleted"
me.argv.BadExit(s, err)
}
}
return ErrorNeedArgvFix
}
return log.Errorf("NOT SAFE")
}
|