Skip to content

Commit 03ff768

Browse files
lunnylafriks
authored andcommitted
fix go get subpackage bug (#2584) (#2589)
* fix go get subpackage bug * merge the duplicated funtions
1 parent f646154 commit 03ff768

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
@@ -124,21 +124,23 @@ func RetrieveBaseRepo(ctx *Context, repo *models.Repository) {
124124
}
125125
}
126126

127-
// composeGoGetImport returns go-get-import meta content.
128-
func composeGoGetImport(owner, repo string) string {
127+
// ComposeGoGetImport returns go-get-import meta content.
128+
func ComposeGoGetImport(owner, repo string) string {
129129
return path.Join(setting.Domain, setting.AppSubURL, owner, repo)
130130
}
131131

132-
// earlyResponseForGoGetMeta responses appropriate go-get meta with status 200
132+
// EarlyResponseForGoGetMeta responses appropriate go-get meta with status 200
133133
// if user does not have actual access to the requested repository,
134134
// or the owner or repository does not exist at all.
135135
// This is particular a workaround for "go get" command which does not respect
136136
// .netrc file.
137-
func earlyResponseForGoGetMeta(ctx *Context) {
137+
func EarlyResponseForGoGetMeta(ctx *Context) {
138+
username := ctx.Params(":username")
139+
reponame := ctx.Params(":reponame")
138140
ctx.PlainText(200, []byte(com.Expand(`<meta name="go-import" content="{GoGetImport} git {CloneLink}">`,
139141
map[string]string{
140-
"GoGetImport": composeGoGetImport(ctx.Params(":username"), strings.TrimSuffix(ctx.Params(":reponame"), ".git")),
141-
"CloneLink": models.ComposeHTTPSCloneURL(ctx.Params(":username"), ctx.Params(":reponame")),
142+
"GoGetImport": ComposeGoGetImport(username, strings.TrimSuffix(reponame, ".git")),
143+
"CloneLink": models.ComposeHTTPSCloneURL(username, reponame),
142144
})))
143145
}
144146

@@ -206,7 +208,7 @@ func RepoIDAssignment() macaron.Handler {
206208
// Check access.
207209
if ctx.Repo.AccessMode == models.AccessModeNone {
208210
if ctx.Query("go-get") == "1" {
209-
earlyResponseForGoGetMeta(ctx)
211+
EarlyResponseForGoGetMeta(ctx)
210212
return
211213
}
212214
ctx.Handle(404, "no access right", err)
@@ -250,7 +252,7 @@ func RepoAssignment() macaron.Handler {
250252
if err != nil {
251253
if models.IsErrUserNotExist(err) {
252254
if ctx.Query("go-get") == "1" {
253-
earlyResponseForGoGetMeta(ctx)
255+
EarlyResponseForGoGetMeta(ctx)
254256
return
255257
}
256258
ctx.Handle(404, "GetUserByName", nil)
@@ -272,7 +274,7 @@ func RepoAssignment() macaron.Handler {
272274
RedirectToRepo(ctx, redirectRepoID)
273275
} else if models.IsErrRepoRedirectNotExist(err) {
274276
if ctx.Query("go-get") == "1" {
275-
earlyResponseForGoGetMeta(ctx)
277+
EarlyResponseForGoGetMeta(ctx)
276278
return
277279
}
278280
ctx.Handle(404, "GetRepositoryByName", nil)
@@ -305,7 +307,7 @@ func RepoAssignment() macaron.Handler {
305307
// Check access.
306308
if ctx.Repo.AccessMode == models.AccessModeNone {
307309
if ctx.Query("go-get") == "1" {
308-
earlyResponseForGoGetMeta(ctx)
310+
EarlyResponseForGoGetMeta(ctx)
309311
return
310312
}
311313
ctx.Handle(404, "no access right", err)
@@ -433,7 +435,7 @@ func RepoAssignment() macaron.Handler {
433435
ctx.Data["PullRequestCtx"] = ctx.Repo.PullRequest
434436

435437
if ctx.Query("go-get") == "1" {
436-
ctx.Data["GoGetImport"] = composeGoGetImport(owner.Name, repo.Name)
438+
ctx.Data["GoGetImport"] = ComposeGoGetImport(owner.Name, repo.Name)
437439
prefix := setting.AppURL + path.Join(owner.Name, repo.Name, "src", ctx.Repo.BranchName)
438440
ctx.Data["GoDocDirectory"] = prefix + "{/dir}"
439441
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)