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
244
245
246
247
248
249
250
251
252
253
|
// Copyright 2025 WIT.COM Inc Licensed GPL 3.0
package forgepb
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"go.wit.com/lib/config"
"go.wit.com/lib/env"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log"
)
func (f *Forge) NewGoRepo(gopath string, url string) (*gitpb.Repo, error) {
fullpath := filepath.Join(env.Get("ReposDir"), gopath)
test := f.Repos.FindByFullPath(fullpath)
if test != nil {
return test, nil
}
if gopath == "" {
log.Info("TODO: gopath was blank. I'm not sure what is happening here, but this needs to determine the repo Namespace. all calls to NewGoRepo() need to be deprecated. Someone will have to do this when I have funding for WIT. If you are the one looking at this, track me down and I will help do this correctly.")
return nil, log.Errorf("gopath was blank")
}
repo, err := f.Repos.NewGoRepo(fullpath, gopath)
if err != nil {
log.Info("WARNING. NEW FAILED", fullpath, err)
return nil, err
}
// slices.Reverse(f.Repos.Repos)
repo.URL = url
f.VerifyBranchNames(repo)
if f.Config.IsReadOnly(repo.GetGoPath()) {
repo.ReadOnly = true
}
repo.ReloadCheck()
return repo, nil
}
// used by the forge daemon
func (f *Forge) AddNamespaceDir(ns string, fullpath string) (*gitpb.Repo, error) {
test := f.Repos.FindByNamespace(ns)
if test != nil {
return test, fmt.Errorf("already have namespace")
}
repo, err := f.Repos.NewGoRepo(fullpath, ns)
if err != nil {
log.Info("WARNING. NEW FAILED", fullpath)
return nil, err
}
f.VerifyBranchNames(repo)
repo.ReloadCheck()
// set the readonly flag based on the users' forge config
if f.Config.IsReadOnly(repo.GetGoPath()) {
repo.ReadOnly = true
}
config.SetChanged("repos", true)
return repo, nil
}
func isValidSemVer(version string) bool {
// Regular expression for semantic versioning
regex := `^v(\d+)\.(\d+)\.(\d+)$`
matched, _ := regexp.MatchString(regex, version)
return matched
}
// golang versions MUST be vX.X.X
// it can not be vX.X and it also can not be v0.0.0
// verifies the version is format v3.2.1
// this is retardedly wrong. it needs to be done right
func (f *Forge) ValidGoVersion(ver string) (bool, error) {
return ValidGoVersion(ver)
}
func ValidGoVersion(ver string) (bool, error) {
if ver == "v0.0.0" {
return false, fmt.Errorf("golang does not allow version v0.0.0")
}
if !strings.HasPrefix(ver, "v") {
return false, fmt.Errorf("(%s) invalid. golang versions must start with a 'v'", ver)
}
xver := ver[1:]
parts := strings.Split(xver, ".")
if len(parts) != 3 {
return false, fmt.Errorf("(%s) invalid. golang versions must have exactly 3 numbers (v1.2.3)", ver)
}
if isValidSemVer(ver) {
return true, nil
}
return false, nil
}
// figure out what the name of the git master branch is
// also check the forge config
func (f *Forge) findMasterBranch(repo *gitpb.Repo) {
// check the forge config first
if bname := f.Config.FindMasterBranch(repo.GetGoPath()); bname != "" {
log.Info("Using master branch name from forge config:", bname)
repo.SetMasterBranchName(bname)
return
}
// todo: fix this after .git parsing is better or libgit2 is being used
headfile := filepath.Join(repo.GetFullPath(), ".git/refs/remotes/origin/HEAD")
if data, err := os.ReadFile(headfile); err == nil {
s := string(data)
if strings.HasPrefix(s, "ref: ") {
fields := strings.Fields(s)
_, bname := filepath.Split(fields[1])
// log.Info("Using master branch name from .git/ HEAD:", bname)
repo.SetMasterBranchName(bname)
return
}
}
// try to guess what the 'master' branch is
if repo.IsBranch("master") {
repo.SetMasterBranchName("master")
return
}
if repo.IsBranch("main") {
repo.SetMasterBranchName("main")
return
}
// TODO: figure out the name from git
repo.SetMasterBranchName("master")
}
// figure out what the name of the git devel branch is
// also check the forge config
func (f *Forge) findDevelBranch(repo *gitpb.Repo) {
// check the forge config first
if bname := f.Config.FindDevelBranch(repo.GetGoPath()); bname != "" {
repo.SetDevelBranchName(bname)
return
}
if repo.IsBranch("devel") {
repo.SetDevelBranchName("devel")
return
}
repo.SetDevelBranchName("devel")
}
// this is still in flux
func (f *Forge) VerifyBranchNames(repo *gitpb.Repo) {
// log.Info("init worked for", repo.GoPath)
if repo.GetMasterBranchName() == "" {
f.findMasterBranch(repo)
}
if repo.GetDevelBranchName() == "" {
f.findDevelBranch(repo)
}
if repo.GetUserBranchName() == "" {
uname := f.configUserBranchName(repo)
if uname == "" {
log.Info("configUserBranchName() ERROR: failed with blank")
}
if repo.IsBranch(uname) {
repo.SetUserBranchName(uname)
} else {
// forcing for now. todo: warn users
repo.SetUserBranchName(uname)
}
}
if repo.GetUserBranchName() == "" || repo.GetDevelBranchName() == "" {
log.Infof("VerifyBranchNames() failed m=%s d=%s u=%s\n", repo.GetMasterBranchName(), repo.GetDevelBranchName(), repo.GetUserBranchName())
}
}
// what name should be used for the user branch?
func (f *Forge) configUserBranchName(repo *gitpb.Repo) string {
// look in the repo record for the username
uname := repo.GetUserBranchName()
if uname != "" {
return uname
}
// query the the forge config for a custom User Branch Name
uname = f.Config.FindUserBranch(repo.GetGoPath())
if uname != "" {
return uname
}
if env.Get("username") == "" {
// something is wrong!
}
// use the os.Username
uname = env.Get("username")
return uname
}
// todo: check the forge config
func (f *Forge) configDevelBranchName(repo *gitpb.Repo) string {
if repo.GetDevelBranchName() != "" {
return repo.GetDevelBranchName()
}
if repo.IsBranch("guidevel") {
return "guidevel"
}
if repo.IsBranch("devel") {
return "devel"
}
return "devel"
}
func (f *Forge) AddFullPath(fulldir string) *gitpb.Repo {
repo := f.Repos.FindByFullPath(fulldir)
if repo != nil {
return repo
}
log.Info("don't know how to add full paths yet", fulldir)
return nil
}
// tries any parent
func (f *Forge) FindAnyPath(dir string) *gitpb.Repo {
dir = filepath.Clean(dir)
repo := f.Repos.FindByFullPath(dir)
if repo != nil {
log.Info("FindAnyPath() worked = ", dir)
return repo
}
if dir == "" {
return nil
}
if dir == "/" {
return nil
}
basedir, newdir := filepath.Split(dir)
if newdir == "" {
// is this correct? not sure what stupid windows does
return nil
}
return f.FindAnyPath(basedir)
}
func (f *Forge) DeleteByGoPath(gopath string) bool {
fullpath := filepath.Join(env.Get("ReposDir"), gopath)
return f.Repos.DeleteByFullPath(fullpath)
}
|