Skip to content

Commit bd93613

Browse files
lunny6543
andauthored
Fix go get (#14758)
* Fix go get * Fix default branch Co-authored-by: 6543 <[email protected]>
1 parent b56c19d commit bd93613

File tree

2 files changed

+65
-59
lines changed

2 files changed

+65
-59
lines changed

modules/context/context.go

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"code.gitea.io/gitea/modules/setting"
3030
"code.gitea.io/gitea/modules/templates"
3131
"code.gitea.io/gitea/modules/translation"
32-
"code.gitea.io/gitea/modules/util"
3332
"code.gitea.io/gitea/modules/web/middleware"
3433

3534
"gitea.com/go-chi/cache"
@@ -613,63 +612,6 @@ func Contexter() func(next http.Handler) http.Handler {
613612

614613
ctx.Flash = f
615614

616-
// Quick responses appropriate go-get meta with status 200
617-
// regardless of if user have access to the repository,
618-
// or the repository does not exist at all.
619-
// This is particular a workaround for "go get" command which does not respect
620-
// .netrc file.
621-
if ctx.Query("go-get") == "1" {
622-
ownerName := ctx.Params(":username")
623-
repoName := ctx.Params(":reponame")
624-
trimmedRepoName := strings.TrimSuffix(repoName, ".git")
625-
626-
if ownerName == "" || trimmedRepoName == "" {
627-
_, _ = ctx.Write([]byte(`<!doctype html>
628-
<html>
629-
<body>
630-
invalid import path
631-
</body>
632-
</html>
633-
`))
634-
ctx.Status(400)
635-
return
636-
}
637-
branchName := "master"
638-
639-
repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName)
640-
if err == nil && len(repo.DefaultBranch) > 0 {
641-
branchName = repo.DefaultBranch
642-
}
643-
prefix := setting.AppURL + path.Join(url.PathEscape(ownerName), url.PathEscape(repoName), "src", "branch", util.PathEscapeSegments(branchName))
644-
645-
appURL, _ := url.Parse(setting.AppURL)
646-
647-
insecure := ""
648-
if appURL.Scheme == string(setting.HTTP) {
649-
insecure = "--insecure "
650-
}
651-
ctx.Header().Set("Content-Type", "text/html")
652-
ctx.Status(http.StatusOK)
653-
_, _ = ctx.Write([]byte(com.Expand(`<!doctype html>
654-
<html>
655-
<head>
656-
<meta name="go-import" content="{GoGetImport} git {CloneLink}">
657-
<meta name="go-source" content="{GoGetImport} _ {GoDocDirectory} {GoDocFile}">
658-
</head>
659-
<body>
660-
go get {Insecure}{GoGetImport}
661-
</body>
662-
</html>
663-
`, map[string]string{
664-
"GoGetImport": ComposeGoGetImport(ownerName, trimmedRepoName),
665-
"CloneLink": models.ComposeHTTPSCloneURL(ownerName, repoName),
666-
"GoDocDirectory": prefix + "{/dir}",
667-
"GoDocFile": prefix + "{/dir}/{file}#L{line}",
668-
"Insecure": insecure,
669-
})))
670-
return
671-
}
672-
673615
// If request sends files, parse them here otherwise the Query() can't be parsed and the CsrfToken will be invalid.
674616
if ctx.Req.Method == "POST" && strings.Contains(ctx.Req.Header.Get("Content-Type"), "multipart/form-data") {
675617
if err := ctx.Req.ParseMultipartForm(setting.Attachment.MaxSize << 20); err != nil && !strings.Contains(err.Error(), "EOF") { // 32MB max size

routers/routes/web.go

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/gob"
99
"fmt"
1010
"net/http"
11+
"net/url"
1112
"os"
1213
"path"
1314
"strings"
@@ -23,6 +24,7 @@ import (
2324
"code.gitea.io/gitea/modules/setting"
2425
"code.gitea.io/gitea/modules/storage"
2526
"code.gitea.io/gitea/modules/templates"
27+
"code.gitea.io/gitea/modules/util"
2628
"code.gitea.io/gitea/modules/validation"
2729
"code.gitea.io/gitea/modules/web"
2830
"code.gitea.io/gitea/routers"
@@ -47,6 +49,7 @@ import (
4749
"github.com/go-chi/chi/middleware"
4850
"github.com/prometheus/client_golang/prometheus"
4951
"github.com/tstranex/u2f"
52+
"github.com/unknwon/com"
5053
)
5154

5255
const (
@@ -216,6 +219,67 @@ func WebRoutes() *web.Route {
216219
return r
217220
}
218221

222+
func goGet(ctx *context.Context) {
223+
if ctx.Query("go-get") != "1" {
224+
return
225+
}
226+
227+
// Quick responses appropriate go-get meta with status 200
228+
// regardless of if user have access to the repository,
229+
// or the repository does not exist at all.
230+
// This is particular a workaround for "go get" command which does not respect
231+
// .netrc file.
232+
233+
ownerName := ctx.Params(":username")
234+
repoName := ctx.Params(":reponame")
235+
trimmedRepoName := strings.TrimSuffix(repoName, ".git")
236+
237+
if ownerName == "" || trimmedRepoName == "" {
238+
_, _ = ctx.Write([]byte(`<!doctype html>
239+
<html>
240+
<body>
241+
invalid import path
242+
</body>
243+
</html>
244+
`))
245+
ctx.Status(400)
246+
return
247+
}
248+
branchName := setting.Repository.DefaultBranch
249+
250+
repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName)
251+
if err == nil && len(repo.DefaultBranch) > 0 {
252+
branchName = repo.DefaultBranch
253+
}
254+
prefix := setting.AppURL + path.Join(url.PathEscape(ownerName), url.PathEscape(repoName), "src", "branch", util.PathEscapeSegments(branchName))
255+
256+
appURL, _ := url.Parse(setting.AppURL)
257+
258+
insecure := ""
259+
if appURL.Scheme == string(setting.HTTP) {
260+
insecure = "--insecure "
261+
}
262+
ctx.Header().Set("Content-Type", "text/html")
263+
ctx.Status(http.StatusOK)
264+
_, _ = ctx.Write([]byte(com.Expand(`<!doctype html>
265+
<html>
266+
<head>
267+
<meta name="go-import" content="{GoGetImport} git {CloneLink}">
268+
<meta name="go-source" content="{GoGetImport} _ {GoDocDirectory} {GoDocFile}">
269+
</head>
270+
<body>
271+
go get {Insecure}{GoGetImport}
272+
</body>
273+
</html>
274+
`, map[string]string{
275+
"GoGetImport": context.ComposeGoGetImport(ownerName, trimmedRepoName),
276+
"CloneLink": models.ComposeHTTPSCloneURL(ownerName, repoName),
277+
"GoDocDirectory": prefix + "{/dir}",
278+
"GoDocFile": prefix + "{/dir}/{file}#L{line}",
279+
"Insecure": insecure,
280+
})))
281+
}
282+
219283
// RegisterRoutes register routes
220284
func RegisterRoutes(m *web.Route) {
221285
reqSignIn := context.Toggle(&context.ToggleOptions{SignInRequired: true})
@@ -1004,7 +1068,7 @@ func RegisterRoutes(m *web.Route) {
10041068
m.Group("/{username}", func() {
10051069
m.Group("/{reponame}", func() {
10061070
m.Get("", repo.SetEditorconfigIfExists, repo.Home)
1007-
}, ignSignIn, context.RepoAssignment(), context.RepoRef(), context.UnitTypes())
1071+
}, goGet, ignSignIn, context.RepoAssignment(), context.RepoRef(), context.UnitTypes())
10081072

10091073
m.Group("/{reponame}", func() {
10101074
m.Group("/info/lfs", func() {

0 commit comments

Comments
 (0)