MCPcopy Create free account
hub / github.com/gogs/gogs / ServeGoGet

Function ServeGoGet

internal/context/go_get.go:20–63  ·  view source on GitHub ↗

ServeGoGet does quick responses for appropriate go-get meta with status OK regardless of whether the user has access to the repository, or the repository does exist at all. This is particular a workaround for "go get" command which does not respect .netrc file.

()

Source from the content-addressed store, hash-verified

18// does exist at all. This is particular a workaround for "go get" command which
19// does not respect .netrc file.
20func ServeGoGet() macaron.Handler {
21 return func(c *macaron.Context) {
22 if c.Query("go-get") != "1" {
23 return
24 }
25
26 ownerName := c.Params(":username")
27 repoName := c.Params(":reponame")
28 branchName := "master"
29
30 owner, err := database.Handle.Users().GetByUsername(c.Req.Context(), ownerName)
31 if err == nil {
32 repo, err := database.Handle.Repositories().GetByName(c.Req.Context(), owner.ID, repoName)
33 if err == nil && repo.DefaultBranch != "" {
34 branchName = repo.DefaultBranch
35 }
36 }
37
38 prefix := conf.Server.ExternalURL + path.Join(ownerName, repoName, "src", branchName)
39 insecureFlag := ""
40 if !strings.HasPrefix(conf.Server.ExternalURL, "https://") {
41 insecureFlag = "--insecure "
42 }
43 c.PlainText(http.StatusOK, []byte(com.Expand(`<!doctype html>
44<html>
45 <head>
46 <meta name="go-import" content="{GoGetImport} git {CloneLink}">
47 <meta name="go-source" content="{GoGetImport} _ {GoDocDirectory} {GoDocFile}">
48 </head>
49 <body>
50 go get {InsecureFlag}{GoGetImport}
51 </body>
52</html>
53`,
54 map[string]string{
55 "GoGetImport": path.Join(conf.Server.URL.Host, conf.Server.Subpath, ownerName, repoName),
56 "CloneLink": repoutil.HTTPSCloneURL(ownerName, repoName),
57 "GoDocDirectory": prefix + "{/dir}",
58 "GoDocFile": prefix + "{/dir}/{file}#L{line}",
59 "InsecureFlag": insecureFlag,
60 },
61 )))
62 }
63}

Callers 1

runWebFunction · 0.92

Calls 6

HTTPSCloneURLFunction · 0.92
GetByUsernameMethod · 0.80
UsersMethod · 0.80
GetByNameMethod · 0.80
RepositoriesMethod · 0.80
PlainTextMethod · 0.80

Tested by

no test coverage detected