aboutsummaryrefslogtreecommitdiff
path: root/internal/context/go_get.go
blob: 06417ebfb3697139cb199003842c4eb31a144aa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package context

import (
	"net/http"
	"path"
	"strings"

	"github.com/unknwon/com"
	"gopkg.in/macaron.v1"

	"gogs.io/gogs/internal/conf"
	"gogs.io/gogs/internal/db"
)

// 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.
func ServeGoGet() macaron.Handler {
	return func(c *macaron.Context) {
		if c.Query("go-get") != "1" {
			return
		}

		ownerName := c.Params(":username")
		repoName := c.Params(":reponame")
		branchName := "master"

		owner, err := db.Users.GetByUsername(c.Req.Context(), ownerName)
		if err == nil {
			repo, err := db.Repos.GetByName(c.Req.Context(), owner.ID, repoName)
			if err == nil && repo.DefaultBranch != "" {
				branchName = repo.DefaultBranch
			}
		}

		prefix := conf.Server.ExternalURL + path.Join(ownerName, repoName, "src", branchName)
		insecureFlag := ""
		if !strings.HasPrefix(conf.Server.ExternalURL, "https://") {
			insecureFlag = "--insecure "
		}
		c.PlainText(http.StatusOK, []byte(com.Expand(`<!doctype html>
<html>
	<head>
		<meta name="go-import" content="{GoGetImport} git {CloneLink}">
		<meta name="go-source" content="{GoGetImport} _ {GoDocDirectory} {GoDocFile}">
	</head>
	<body>
		go get {InsecureFlag}{GoGetImport}
	</body>
</html>
`,
			map[string]string{
				"GoGetImport":    path.Join(conf.Server.URL.Host, conf.Server.Subpath, ownerName, repoName),
				"CloneLink":      db.ComposeHTTPSCloneURL(ownerName, repoName),
				"GoDocDirectory": prefix + "{/dir}",
				"GoDocFile":      prefix + "{/dir}/{file}#L{line}",
				"InsecureFlag":   insecureFlag,
			},
		)))
	}
}