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
|
// 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/protobuf/forgepb"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log"
)
var ErrorNeedArgvFix error = errors.New("add --fix")
// FORGE USES THESE TO RECOVER FROM WHEN TOOLKITS FAIL TO LOAD
// so don't delete them
func doDev() (string, error) {
if argv.Dev.DeleteUser {
found := gitpb.NewRepos()
setForgeMode(forgepb.ForgeMode_MASTER)
for repo := range me.forge.Repos.IterByNamespace() {
uremoteref := repo.GetRemoteTag(repo.GetUserBranchName())
if uremoteref == nil {
continue
}
found.Repos = append(found.Repos, repo)
if !repo.IsMasterBranch() {
if argv.Fix {
log.Info("you just be in the master branch to run --fix")
repo.State = "needs master branch"
continue
}
}
doFixDeleteUserBranches(repo, uremoteref)
}
me.forge.PrintHumanTable(found)
}
return "", nil
}
func doFixDeleteUserBranches(repo *gitpb.Repo, remoteRef *gitpb.GitTag) *notesList {
// log.Info("delete remote user", repo.FullPath)
localTag := repo.IsBranchLocal(repo.GetUserBranchName())
if localTag != nil {
// log.Info("THERE IS local user", repo.FullPath, localTag.Refname)
log.Printf("%-13.13s %-55.55s %v\n", "LOCAL USER", repo.FullPath, localTag.Refname+" local branch exists")
err, notes := doFixDeleteUserLocalBranch(repo, remoteRef, localTag)
if err != nil {
log.Printf("%-13.13s %-55.55s %v noteslen(%d)\n", "need --fix", repo.FullPath, err, len(notes.all))
}
}
err, notes := doFixDeleteRemoteUserBranch(repo, remoteRef)
if err != nil {
log.Printf("%-13.13s %-55.55s %v noteslen(%d)\n", "need --fix", repo.FullPath, err, len(notes.all))
}
return notes
}
// notes.addNote("SAFE", repo, remoteUser, line)
func (notes *notesList) addNote(note string, repo *gitpb.Repo, ref *gitpb.GitTag, cmd []string, line string) {
newnote := new(DeleteBranchNotes)
newnote.Note = note
newnote.Refname = ref.Refname
newnote.Fullpath = repo.FullPath
newnote.Cmd = cmd
parts := strings.Split(line, "%00") // git log doesn't actually convert %00 to NULL
if len(parts) != 4 {
log.Info("len", len(parts))
panic("nope")
}
newnote.H = parts[0]
newnote.Ae = parts[1]
newnote.As = parts[2]
newnote.S = parts[3]
// newnote.Hash = parts[0]
log.Printf("%-5.5s %7.7s %-55.55s %v\n", note, newnote.H, newnote.Fullpath, cmd)
notes.all = append(notes.all, newnote)
}
type notesList struct {
all []*DeleteBranchNotes
}
type DeleteBranchNotes struct {
Note string
Refname string
Fullpath string
Cmd []string
// git log variable names
H string
S string
Ae string
As string
}
// git branch -D refs/heads/jcarr
// git update-ref -d refs/heads/jcarr
func doFixDeleteUserLocalBranch(repo *gitpb.Repo, remoteRef *gitpb.GitTag, localRef *gitpb.GitTag) (error, *notesList) {
notes := new(notesList)
// get local user, remote user & local devel branches
dref := repo.GetLocalDevelRef()
localUser := repo.NewCompareRef(localRef)
remoteUser := repo.NewCompareRef(remoteRef)
localDevel := repo.NewCompareRef(dref)
// Is the localUser completely inside the remoteUser branch?
hashok, hashbad, cmd1, cmd2, err := remoteUser.CompareBranch(localUser)
for _, line := range hashok {
notes.addNote("OK", repo, remoteUser.GetRef(), cmd1, line)
}
for _, line := range hashbad {
notes.addNote("BAD", repo, localUser.GetRef(), cmd2, line)
}
if err != nil {
log.Printf("%-13.13s %-55.55s err='%v' %s %v\n", "CMD ERR", repo.FullPath, err, "NOT SAFE TO DELETE. Reload()?", cmd1)
log.Printf("%-13.13s %-55.55s err='%v' %s %v\n", "CMD ERR", repo.FullPath, err, "NOT SAFE TO DELETE. Reload()?", cmd2)
return err, notes
}
if len(hashbad) == 0 {
// git update-ref -d refs/heads/jcarr
cmd := []string{"git", "update-ref", "-d", localUser.GetRefname()}
log.Printf("%-13.13s %-55.55s %v %s\n", "CMD OK", repo.FullPath, cmd1, "")
log.Printf("%-13.13s %-55.55s %v %s\n", "CMD OK", repo.FullPath, cmd2, "")
log.Printf("%-13.13s %-55.55s %v %s\n", "SAFE TO DELETE", repo.FullPath, cmd, "add --fix")
if argv.Fix {
err := repo.RunVerbose(cmd)
if err != nil {
log.Info(localUser.GetRefname(), repo.FullPath)
s := "local user branch could not be deleted"
me.sh.BadExit(s, err)
}
return nil, notes
}
return ErrorNeedArgvFix, notes
}
// Is the localUser completely inside the localDevel branch?
hashok, hashbad, cmd1, cmd2, err = localDevel.CompareBranch(localUser)
for _, line := range hashok {
notes.addNote("OK", repo, localDevel.GetRef(), cmd1, line)
}
for _, line := range hashbad {
notes.addNote("BAD", repo, localUser.GetRef(), cmd2, line)
}
// git update-ref -d refs/heads/jcarr
if err != nil {
log.Printf("%-13.13s %-55.55s err='%v' %s %v\n", "CMD ERR", repo.FullPath, err, "NOT SAFE TO DELETE. Reload()?", cmd1)
log.Printf("%-13.13s %-55.55s err='%v' %s %v\n", "CMD ERR", repo.FullPath, err, "NOT SAFE TO DELETE. Reload()?", cmd2)
return err, notes
}
if len(hashbad) == 0 {
cmd := []string{"git", "update-ref", "-d", localUser.GetRefname()}
log.Printf("%-13.13s %-55.55s %v %s\n", "CMD OK", repo.FullPath, cmd1, "")
log.Printf("%-13.13s %-55.55s %v %s\n", "CMD OK", repo.FullPath, cmd2, "")
log.Printf("%-13.13s %-55.55s %v %s\n", "SAFE TO DELETE", repo.FullPath, cmd, "add --fix")
if argv.Fix {
err := repo.RunVerbose(cmd)
if err != nil {
log.Info(localUser.GetRefname(), repo.FullPath)
s := "local user branch could not be deleted"
me.sh.BadExit(s, err)
}
}
return ErrorNeedArgvFix, notes
}
return log.Errorf("NOT SAFE"), notes
}
// git push --delete origin jcarr
// git push origin :refs/remotes/origin/jcarr
func doFixDeleteRemoteUserBranch(repo *gitpb.Repo, remoteRef *gitpb.GitTag) (error, *notesList) {
notes := new(notesList)
// get remote user & local devel branches
remoteUser := repo.NewCompareRef(remoteRef)
dref := repo.GetLocalDevelRef()
localDevel := repo.NewCompareRef(dref)
// Is the remoteUser completely inside the localDevel branch?
hashok, hashbad, cmd1, cmd2, err := localDevel.CompareBranch(remoteUser)
for _, line := range hashok {
notes.addNote("OK", repo, localDevel.GetRef(), cmd1, line)
}
for _, line := range hashbad {
notes.addNote("BAD", repo, remoteUser.GetRef(), cmd2, line)
}
// git update-ref -d refs/heads/jcarr
if err != nil {
log.Printf("%-13.13s %-55.55s err='%v' %s %v\n", "CMD ERR", repo.FullPath, err, "NOT SAFE TO DELETE. Reload()?", cmd1)
log.Printf("%-13.13s %-55.55s err='%v' %s %v\n", "CMD ERR", repo.FullPath, err, "NOT SAFE TO DELETE. Reload()?", cmd2)
return err, notes
}
if len(hashbad) == 0 {
cmd := []string{"git", "push", "origin", ":" + remoteUser.GetRefname()}
log.Printf("%-13.13s %-55.55s %v %s\n", "CMD OK", repo.FullPath, cmd1, "")
log.Printf("%-13.13s %-55.55s %v %s\n", "CMD OK", repo.FullPath, cmd2, "")
log.Printf("%-13.13s %-55.55s %v %s\n", "SAFE TO DELETE", repo.FullPath, cmd, "add --fix")
if argv.Fix {
err := repo.RunVerbose(cmd)
if err != nil {
log.Info(remoteUser.GetRefname(), repo.FullPath)
s := "remote user branch could not be deleted"
me.sh.BadExit(s, err)
}
}
return ErrorNeedArgvFix, notes
}
return log.Errorf("NOT SAFE"), notes
}
|