8
8
"encoding/gob"
9
9
"fmt"
10
10
"net/http"
11
+ "net/url"
11
12
"os"
12
13
"path"
13
14
"strings"
@@ -23,6 +24,7 @@ import (
23
24
"code.gitea.io/gitea/modules/setting"
24
25
"code.gitea.io/gitea/modules/storage"
25
26
"code.gitea.io/gitea/modules/templates"
27
+ "code.gitea.io/gitea/modules/util"
26
28
"code.gitea.io/gitea/modules/validation"
27
29
"code.gitea.io/gitea/modules/web"
28
30
"code.gitea.io/gitea/routers"
@@ -47,6 +49,7 @@ import (
47
49
"github.com/go-chi/chi/middleware"
48
50
"github.com/prometheus/client_golang/prometheus"
49
51
"github.com/tstranex/u2f"
52
+ "github.com/unknwon/com"
50
53
)
51
54
52
55
const (
@@ -216,6 +219,67 @@ func WebRoutes() *web.Route {
216
219
return r
217
220
}
218
221
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
+
219
283
// RegisterRoutes register routes
220
284
func RegisterRoutes (m * web.Route ) {
221
285
reqSignIn := context .Toggle (& context.ToggleOptions {SignInRequired : true })
@@ -1004,7 +1068,7 @@ func RegisterRoutes(m *web.Route) {
1004
1068
m .Group ("/{username}" , func () {
1005
1069
m .Group ("/{reponame}" , func () {
1006
1070
m .Get ("" , repo .SetEditorconfigIfExists , repo .Home )
1007
- }, ignSignIn , context .RepoAssignment (), context .RepoRef (), context .UnitTypes ())
1071
+ }, goGet , ignSignIn , context .RepoAssignment (), context .RepoRef (), context .UnitTypes ())
1008
1072
1009
1073
m .Group ("/{reponame}" , func () {
1010
1074
m .Group ("/info/lfs" , func () {
0 commit comments