From e5bf4281b5519d845dd85a153831ba5cc1117590 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sun, 7 Aug 2016 14:29:16 -0700 Subject: [PATCH] #2825 early response 200 when ?go-get=1 --- README.md | 2 +- gogs.go | 2 +- models/repo.go | 7 ++++++- modules/context/repo.go | 33 ++++++++++++++++++++++++++++++++- templates/.VERSION | 2 +- 5 files changed, 41 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index daa372c240b1..a593274ca3bc 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Gogs - Go Git Service [![Build Status](https://travis-ci.org/gogits/gogs.svg?bra ![](https://github.com/gogits/gogs/blob/master/public/img/gogs-large-resize.png?raw=true) -##### Current tip version: 0.9.66 (see [Releases](https://github.com/gogits/gogs/releases) for binary versions) +##### Current tip version: 0.9.67 (see [Releases](https://github.com/gogits/gogs/releases) for binary versions) | Web | UI | Preview | |:-------------:|:-------:|:-------:| diff --git a/gogs.go b/gogs.go index 6f5f4a434a39..0e850eb2d736 100644 --- a/gogs.go +++ b/gogs.go @@ -17,7 +17,7 @@ import ( "github.com/gogits/gogs/modules/setting" ) -const APP_VER = "0.9.66.0806" +const APP_VER = "0.9.67.0806" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/models/repo.go b/models/repo.go index 02b4e9fbc76f..7de9499ae5d0 100644 --- a/models/repo.go +++ b/models/repo.go @@ -494,6 +494,11 @@ type CloneLink struct { Git string } +// ComposeHTTPSCloneURL returns HTTPS clone URL based on given owner and repository name. +func ComposeHTTPSCloneURL(owner, repo string) string { + return fmt.Sprintf("%s%s/%s.git", setting.AppUrl, owner, repo) +} + func (repo *Repository) cloneLink(isWiki bool) *CloneLink { repoName := repo.Name if isWiki { @@ -507,7 +512,7 @@ func (repo *Repository) cloneLink(isWiki bool) *CloneLink { } else { cl.SSH = fmt.Sprintf("%s@%s:%s/%s.git", setting.RunUser, setting.SSH.Domain, repo.Owner.Name, repoName) } - cl.HTTPS = fmt.Sprintf("%s%s/%s.git", setting.AppUrl, repo.Owner.Name, repoName) + cl.HTTPS = ComposeHTTPSCloneURL(repo.Owner.Name, repo.Name) return cl } diff --git a/modules/context/repo.go b/modules/context/repo.go index 949913273ce7..bc58b490eb9d 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -9,6 +9,7 @@ import ( "path" "strings" + "github.com/Unknwon/com" "gopkg.in/macaron.v1" "github.com/gogits/git-module" @@ -84,6 +85,24 @@ func RetrieveBaseRepo(ctx *Context, repo *models.Repository) { } } +// composeGoGetImport returns go-get-import meta content. +func composeGoGetImport(owner, repo string) string { + return path.Join(setting.Domain, setting.AppSubUrl, owner, repo) +} + +// earlyResponseForGoGetMeta responses appropriate go-get meta with status 200 +// if user does not have actual access to the requested repository, +// or the owner or repository does not exist at all. +// This is particular a workaround for "go get" command which does not respect +// .netrc file. +func earlyResponseForGoGetMeta(ctx *Context) { + ctx.PlainText(200, []byte(com.Expand(``, + map[string]string{ + "GoGetImport": composeGoGetImport(ctx.Params(":username"), ctx.Params(":reponame")), + "CloneLink": models.ComposeHTTPSCloneURL(ctx.Params(":username"), ctx.Params(":reponame")), + }))) +} + func RepoAssignment(args ...bool) macaron.Handler { return func(ctx *Context) { var ( @@ -112,6 +131,10 @@ func RepoAssignment(args ...bool) macaron.Handler { owner, err = models.GetUserByName(userName) if err != nil { if models.IsErrUserNotExist(err) { + if ctx.Query("go-get") == "1" { + earlyResponseForGoGetMeta(ctx) + return + } ctx.Handle(404, "GetUserByName", err) } else { ctx.Handle(500, "GetUserByName", err) @@ -125,6 +148,10 @@ func RepoAssignment(args ...bool) macaron.Handler { repo, err := models.GetRepositoryByName(owner.ID, repoName) if err != nil { if models.IsErrRepoNotExist(err) { + if ctx.Query("go-get") == "1" { + earlyResponseForGoGetMeta(ctx) + return + } ctx.Handle(404, "GetRepositoryByName", err) } else { ctx.Handle(500, "GetRepositoryByName", err) @@ -149,6 +176,10 @@ func RepoAssignment(args ...bool) macaron.Handler { // Check access. if ctx.Repo.AccessMode == models.ACCESS_MODE_NONE { + if ctx.Query("go-get") == "1" { + earlyResponseForGoGetMeta(ctx) + return + } ctx.Handle(404, "no access right", err) return } @@ -269,7 +300,7 @@ func RepoAssignment(args ...bool) macaron.Handler { ctx.Data["PullRequestCtx"] = ctx.Repo.PullRequest if ctx.Query("go-get") == "1" { - ctx.Data["GoGetImport"] = path.Join(setting.Domain, setting.AppSubUrl, owner.Name, repo.Name) + ctx.Data["GoGetImport"] = composeGoGetImport(owner.Name, repo.Name) prefix := setting.AppUrl + path.Join(owner.Name, repo.Name, "src", ctx.Repo.BranchName) ctx.Data["GoDocDirectory"] = prefix + "{/dir}" ctx.Data["GoDocFile"] = prefix + "{/dir}/{file}#L{line}" diff --git a/templates/.VERSION b/templates/.VERSION index f9b30b1cf689..a6c0a2d1e5d0 100644 --- a/templates/.VERSION +++ b/templates/.VERSION @@ -1 +1 @@ -0.9.66.0806 \ No newline at end of file +0.9.67.0806 \ No newline at end of file