Skip to content

Commit 2ef8b8b

Browse files
lunnylafriks
authored andcommitted
fix go get subpackage bug (#2584)
* fix go get subpackage bug * merge the duplicated funtions
1 parent 2db424c commit 2ef8b8b

File tree

2 files changed

+14
-32
lines changed

2 files changed

+14
-32
lines changed

modules/context/repo.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,21 +134,23 @@ func RetrieveBaseRepo(ctx *Context, repo *models.Repository) {
134134
}
135135
}
136136

137-
// composeGoGetImport returns go-get-import meta content.
138-
func composeGoGetImport(owner, repo string) string {
137+
// ComposeGoGetImport returns go-get-import meta content.
138+
func ComposeGoGetImport(owner, repo string) string {
139139
return path.Join(setting.Domain, setting.AppSubURL, owner, repo)
140140
}
141141

142-
// earlyResponseForGoGetMeta responses appropriate go-get meta with status 200
142+
// EarlyResponseForGoGetMeta responses appropriate go-get meta with status 200
143143
// if user does not have actual access to the requested repository,
144144
// or the owner or repository does not exist at all.
145145
// This is particular a workaround for "go get" command which does not respect
146146
// .netrc file.
147-
func earlyResponseForGoGetMeta(ctx *Context) {
147+
func EarlyResponseForGoGetMeta(ctx *Context) {
148+
username := ctx.Params(":username")
149+
reponame := ctx.Params(":reponame")
148150
ctx.PlainText(200, []byte(com.Expand(`<meta name="go-import" content="{GoGetImport} git {CloneLink}">`,
149151
map[string]string{
150-
"GoGetImport": composeGoGetImport(ctx.Params(":username"), strings.TrimSuffix(ctx.Params(":reponame"), ".git")),
151-
"CloneLink": models.ComposeHTTPSCloneURL(ctx.Params(":username"), ctx.Params(":reponame")),
152+
"GoGetImport": ComposeGoGetImport(username, strings.TrimSuffix(reponame, ".git")),
153+
"CloneLink": models.ComposeHTTPSCloneURL(username, reponame),
152154
})))
153155
}
154156

@@ -216,7 +218,7 @@ func RepoIDAssignment() macaron.Handler {
216218
// Check access.
217219
if ctx.Repo.AccessMode == models.AccessModeNone {
218220
if ctx.Query("go-get") == "1" {
219-
earlyResponseForGoGetMeta(ctx)
221+
EarlyResponseForGoGetMeta(ctx)
220222
return
221223
}
222224
ctx.Handle(404, "no access right", err)
@@ -260,7 +262,7 @@ func RepoAssignment() macaron.Handler {
260262
if err != nil {
261263
if models.IsErrUserNotExist(err) {
262264
if ctx.Query("go-get") == "1" {
263-
earlyResponseForGoGetMeta(ctx)
265+
EarlyResponseForGoGetMeta(ctx)
264266
return
265267
}
266268
ctx.Handle(404, "GetUserByName", nil)
@@ -282,7 +284,7 @@ func RepoAssignment() macaron.Handler {
282284
RedirectToRepo(ctx, redirectRepoID)
283285
} else if models.IsErrRepoRedirectNotExist(err) {
284286
if ctx.Query("go-get") == "1" {
285-
earlyResponseForGoGetMeta(ctx)
287+
EarlyResponseForGoGetMeta(ctx)
286288
return
287289
}
288290
ctx.Handle(404, "GetRepositoryByName", nil)
@@ -315,7 +317,7 @@ func RepoAssignment() macaron.Handler {
315317
// Check access.
316318
if ctx.Repo.AccessMode == models.AccessModeNone {
317319
if ctx.Query("go-get") == "1" {
318-
earlyResponseForGoGetMeta(ctx)
320+
EarlyResponseForGoGetMeta(ctx)
319321
return
320322
}
321323
ctx.Handle(404, "no access right", err)
@@ -443,7 +445,7 @@ func RepoAssignment() macaron.Handler {
443445
ctx.Data["PullRequestCtx"] = ctx.Repo.PullRequest
444446

445447
if ctx.Query("go-get") == "1" {
446-
ctx.Data["GoGetImport"] = composeGoGetImport(owner.Name, repo.Name)
448+
ctx.Data["GoGetImport"] = ComposeGoGetImport(owner.Name, repo.Name)
447449
prefix := setting.AppURL + path.Join(owner.Name, repo.Name, "src", ctx.Repo.BranchName)
448450
ctx.Data["GoDocDirectory"] = prefix + "{/dir}"
449451
ctx.Data["GoDocFile"] = prefix + "{/dir}/{file}#L{line}"

routers/repo/http.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,15 @@ import (
2222
"code.gitea.io/gitea/modules/context"
2323
"code.gitea.io/gitea/modules/log"
2424
"code.gitea.io/gitea/modules/setting"
25-
26-
"github.com/Unknwon/com"
2725
)
2826

29-
func composeGoGetImport(owner, repo, sub string) string {
30-
return path.Join(setting.Domain, setting.AppSubURL, owner, repo, sub)
31-
}
32-
33-
// earlyResponseForGoGetMeta responses appropriate go-get meta with status 200
34-
// if user does not have actual access to the requested repository,
35-
// or the owner or repository does not exist at all.
36-
// This is particular a workaround for "go get" command which does not respect
37-
// .netrc file.
38-
func earlyResponseForGoGetMeta(ctx *context.Context, username, reponame, subpath string) {
39-
ctx.PlainText(200, []byte(com.Expand(`<meta name="go-import" content="{GoGetImport} git {CloneLink}">`,
40-
map[string]string{
41-
"GoGetImport": composeGoGetImport(username, reponame, subpath),
42-
"CloneLink": models.ComposeHTTPSCloneURL(username, reponame),
43-
})))
44-
}
45-
4627
// HTTP implmentation git smart HTTP protocol
4728
func HTTP(ctx *context.Context) {
4829
username := ctx.Params(":username")
4930
reponame := strings.TrimSuffix(ctx.Params(":reponame"), ".git")
50-
subpath := ctx.Params("*")
5131

5232
if ctx.Query("go-get") == "1" {
53-
earlyResponseForGoGetMeta(ctx, username, reponame, subpath)
33+
context.EarlyResponseForGoGetMeta(ctx)
5434
return
5535
}
5636

0 commit comments

Comments
 (0)