@@ -10,6 +10,7 @@ import (
1010 "html/template"
1111 "io"
1212 "net/http"
13+ "path"
1314 "strings"
1415 "time"
1516
@@ -18,6 +19,7 @@ import (
1819 "code.gitea.io/gitea/modules/base"
1920 "code.gitea.io/gitea/modules/log"
2021 "code.gitea.io/gitea/modules/setting"
22+ "github.com/Unknwon/com"
2123 "github.com/go-macaron/cache"
2224 "github.com/go-macaron/csrf"
2325 "github.com/go-macaron/i18n"
@@ -33,6 +35,7 @@ type Context struct {
3335 Flash * session.Flash
3436 Session session.Store
3537
38+ Link string // current request URL
3639 User * models.User
3740 IsSigned bool
3841 IsBasicAuth bool
@@ -154,15 +157,50 @@ func Contexter() macaron.Handler {
154157 csrf : x ,
155158 Flash : f ,
156159 Session : sess ,
160+ Link : setting .AppSubURL + strings .TrimSuffix (c .Req .URL .Path , "/" ),
157161 Repo : & Repository {
158162 PullRequest : & PullRequest {},
159163 },
160164 Org : & Organization {},
161165 }
162- // Compute current URL for real-time change language.
163- ctx .Data ["Link" ] = setting .AppSubURL + strings .TrimSuffix (ctx .Req .URL .Path , "/" )
164-
166+ c .Data ["Link" ] = ctx .Link
165167 ctx .Data ["PageStartTime" ] = time .Now ()
168+ // Quick responses appropriate go-get meta with status 200
169+ // regardless of if user have access to the repository,
170+ // or the repository does not exist at all.
171+ // This is particular a workaround for "go get" command which does not respect
172+ // .netrc file.
173+ if ctx .Query ("go-get" ) == "1" {
174+ ownerName := c .Params (":username" )
175+ repoName := c .Params (":reponame" )
176+ branchName := "master"
177+
178+ owner , err := models .GetUserByName (ownerName )
179+ if err == nil {
180+ repo , err := models .GetRepositoryByName (owner .ID , repoName )
181+ if err == nil && len (repo .DefaultBranch ) > 0 {
182+ branchName = repo .DefaultBranch
183+ }
184+ }
185+ prefix := setting .AppURL + path .Join (ownerName , repoName , "src" , branchName )
186+ c .PlainText (http .StatusOK , []byte (com .Expand (`
187+ <html>
188+ <head>
189+ <meta name="go-import" content="{GoGetImport} git {CloneLink}">
190+ <meta name="go-source" content="{GoGetImport} _ {GoDocDirectory} {GoDocFile}">
191+ </head>
192+ <body>
193+ go get {GoGetImport}
194+ </body>
195+ </html>
196+ ` , map [string ]string {
197+ "GoGetImport" : path .Join (setting .Domain , setting .AppSubURL , ctx .Link ),
198+ "CloneLink" : models .ComposeHTTPSCloneURL (ownerName , repoName ),
199+ "GoDocDirectory" : prefix + "{/dir}" ,
200+ "GoDocFile" : prefix + "{/dir}/{file}#L{line}" ,
201+ })))
202+ return
203+ }
166204
167205 // Get user from session if logged in.
168206 ctx .User , ctx .IsBasicAuth = auth .SignedInUser (ctx .Context , ctx .Session )
0 commit comments