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
|
package forgepb
import (
"errors"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"go.wit.com/lib/gui/shell"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log"
)
// TODO: make some config file for things like this
// can be used to work around temporary problems
func clonePathHack(dirname string, basedir string, gopath string) (string, error) {
// newdir = helloworld
// basedir = /home/jcarr/go/src/go.wit.com/apps
// giturl = https://gitea.wit.com/gui/helloworld
// func cloneActual(newdir, basedir, giturl string) error {
switch gopath {
case "golang.org/x/crypto":
return cloneActual(dirname, basedir, "https://"+"go.googlesource.com/crypto")
case "golang.org/x/mod":
return cloneActual(dirname, basedir, "https://"+"go.googlesource.com/mod")
case "golang.org/x/net":
return cloneActual(dirname, basedir, "https://"+"go.googlesource.com/net")
case "golang.org/x/sys":
return cloneActual(dirname, basedir, "https://"+"go.googlesource.com/sys")
case "golang.org/x/sync":
return cloneActual(dirname, basedir, "https://"+"go.googlesource.com/sync")
case "golang.org/x/term":
return cloneActual(dirname, basedir, "https://"+"go.googlesource.com/term")
case "golang.org/x/text":
return cloneActual(dirname, basedir, "https://"+"go.googlesource.com/text")
case "golang.org/x/tools":
return cloneActual(dirname, basedir, "https://"+"go.googlesource.com/tools")
case "golang.org/x/xerrors":
return cloneActual(dirname, basedir, "https://"+"go.googlesource.com/xerrors")
case "google.golang.org/protobuf":
return cloneActual(dirname, basedir, "https://"+"go.googlesource.com/protobuf")
case "google.golang.org/genproto":
return cloneActual(dirname, basedir, "https://"+"go.googlesource.com/genproto")
case "google.golang.org/api":
return cloneActual(dirname, basedir, "https://"+"go.googlesource.com/api")
case "google.golang.org/grpc":
return cloneActual(dirname, basedir, "https://"+"go.googlesource.com/grpc")
case "google.golang.org/appengine":
return cloneActual(dirname, basedir, "https://"+"go.googlesource.com/appengine")
}
if strings.HasPrefix(gopath, "github.com/go-gl/glfw") {
return cloneActual(dirname, basedir, "https://github.com/go-gl/glfw")
}
return "", errors.New("no gopath override here")
}
// attempt to git clone if the go path doesn't exist
// does a git clone, if it works, returns true
// gopath = go.wit.com/apps/helloworld
func (f *Forge) Clone(gopath string) (*gitpb.Repo, error) {
var err error
fullpath := filepath.Join(f.goSrc, gopath)
if pb := f.Repos.FindByFullPath(fullpath); pb != nil {
// repo already exists
return pb, nil
}
dirname := filepath.Base(fullpath)
basedir := strings.TrimSuffix(fullpath, dirname)
url := "https://" + gopath
log.Info("trying git clone")
log.Info("gopath =", gopath)
// try a direct git clone against the gopath
// cloneActual("helloworld", "/home/jcarr/go/src/go.wit.com/apps", "https://go.wit.com/apps/helloworld")
if finalurl, err := cloneActual(dirname, basedir, url); err == nil {
return f.finishClone(gopath, finalurl)
}
log.Info("direct attempt at git clone failed", url)
// if direct git clone doesn't work, look for a redirect
// go directly to the URL as that is autoritive. If that failes
// try the go package system as maybe the git site no longer exists
if url, err = findGoImport(url); err != nil {
log.Info("findGoImport() DID NOT WORK", url)
log.Info("findGoImport() DID NOT WORK", err)
} else {
if finalurl, err := cloneActual(dirname, basedir, url); err == nil {
return f.finishClone(gopath, finalurl)
}
}
log.Info("git clone from 'go-import' info failed", url)
// query the golang package system for the last known location
// NOTE: take time to thank the go developers and google for designing this wonderful system
if url, err = runGoList(gopath); err != nil {
log.Info("go list failed", err)
} else {
if finalurl, err := cloneActual(dirname, basedir, url); err == nil {
return f.finishClone(gopath, finalurl)
}
}
log.Info("git clone from 'git list' info failed", url)
// try to parse a redirect
if finalurl, err := clonePathHack(dirname, basedir, gopath); err == nil {
return f.finishClone(gopath, finalurl)
}
return nil, errors.New("can not find git sources for gopath " + gopath)
}
// actually does something smart
func (f *Forge) finishClone(gopath string, giturl string) (*gitpb.Repo, error) {
var err error
newr := f.FindByGoPath(gopath)
if newr == nil {
newr, err = f.NewGoRepo(gopath, giturl)
}
if newr == nil {
log.Warn("forge.Clone() new repo can not be found or created for gopath", gopath)
return nil, err
}
if newr.URL != giturl {
log.Warn("forge.Clone() url changed", newr.URL, "to", giturl)
newr.URL = giturl
}
if err := newr.RepoIgnoresGoMod(); err != nil {
log.Info("never modify go.mod or go.sum for this repo", newr.GetGoPath())
log.Info("We recommend you add 'go.*' to your .gitignore file and store those files as git tag metadata")
newr.ParseGoSum()
return newr, nil
}
if newr.Exists("go.mod") {
return newr, nil
}
log.Info("todo: something went wrong probably. didn't finish. run go-mod-clean? (can't here. loop of circles)")
log.Info("todo: do go mod init here directly")
log.Info("todo: try to run go mod init here", newr.GetGoPath())
return newr, nil
}
// newdir = helloworld
// basedir = /home/jcarr/go/src/go.wit.com/apps
// giturl = https://gitea.wit.com/gui/helloworld
func cloneActual(newdir, basedir, giturl string) (string, error) {
log.Info("cloneActual() newdir =", newdir)
log.Info("cloneActual() basedir =", basedir)
log.Info("cloneActual() giturl =", giturl)
if !IsDirectory(basedir) {
os.MkdirAll(basedir, 0750)
}
err := os.Chdir(basedir)
if err != nil {
log.Warn("chdir failed", basedir, err)
return giturl, err
}
cmd := []string{"git", "clone", "--verbose", "--progress", giturl, newdir}
log.Info("Running:", basedir, cmd)
r := shell.PathRunRealtime(basedir, cmd)
if r.Error != nil {
log.Warn("git clone error", r.Error)
return giturl, r.Error
}
fullpath := filepath.Join(basedir, newdir)
if !IsDirectory(fullpath) {
log.Info("git clone failed", giturl)
return giturl, errors.New("git clone failed " + giturl)
}
gitdir := filepath.Join(fullpath, ".git")
if IsDirectory(gitdir) {
log.Info("git cloned worked to", fullpath)
// also clone notes -- this can store the go.mod and go.sum files
cmd := []string{"git", "fetch", "origin", "refs/notes/*:refs/notes/*"}
shell.PathRunRealtime(fullpath, cmd)
return giturl, nil
}
// git clone didn't really work but did make a directory
log.Info("fullpath is probably empty", fullpath)
return giturl, errors.New("crapnuts. rmdir fullpath here? " + fullpath)
}
// check the server for the current go path to git url mapping
// for example:
// This will check go.wit.com for "go.wit.com/apps/go-clone"
// and return where the URL to do git clone should be
func findGoImport(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
tmp := string(bodyBytes)
parts := strings.Split(tmp, "go-import")
if len(parts) < 2 {
return "", errors.New("missing go-import")
}
// this is terrible, it doesn't even look for 'content='
// but again, this is just a hack for my own code to be
// usuable after the removal in go v1.22 of the go get clone behavior that was in v1.21
parts = strings.Split(parts[1], "\"")
var newurl string
for {
if len(parts) == 0 {
break
}
tmp := strings.TrimSpace(parts[0])
fields := strings.Split(tmp, " ")
// log.Info("FIELDS:", fields)
if len(fields) == 3 {
newurl = fields[2]
break
}
parts = parts[1:]
}
if newurl == "" {
return "", errors.New("missing git content string")
}
return newurl, nil
}
func IsDirectory(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
return info.IsDir()
}
|