Skip to content

Commit af9c626

Browse files
unknwonmpsonntag
authored andcommitted
web: correctly serving go-get pages for subdirs (#6318)
* web: correctly serving go-get page for subdirs * Update CHANGELOG * Fix golint error
1 parent f8d0b84 commit af9c626

File tree

4 files changed

+106
-51
lines changed

4 files changed

+106
-51
lines changed

CHANGELOG.md

+42-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,48 @@
22

33
All notable changes to Gogs are documented in this file.
44

5-
## 0.13.0+dev (`master`)
5+
## 0.13.0+dev (`main`)
6+
7+
### Added
8+
9+
- An unlisted option is added when create or migrate a repository. Unlisted repositories are public but not being listed for users without direct access in the UI. [#5733](https://github.com/gogs/gogs/issues/5733)
10+
11+
### Changed
12+
13+
- The default branch has been changed to `main`. [#6285](https://github.com/gogs/gogs/pull/6285)
14+
- MSSQL as database backend is deprecated, installation page no longer shows it as an option. Existing installations and manually craft configuration file continue to work. [#6295](https://github.com/gogs/gogs/pull/6295)
15+
- Use [Task](https://github.com/go-task/task) as the default build tool for development. [#6297](https://github.com/gogs/gogs/pull/6297)
16+
17+
### Fixed
18+
19+
- _Regression:_ Pages are correctly rendered when requesting `?go-get=1` for subdirectories. [#6314](https://github.com/gogs/gogs/issues/6314)
20+
21+
### Removed
22+
23+
- ⚠️ Migrations before 0.12 are removed, installations not on 0.12 should upgrade to it to run the migrations and then upgrade to 0.13.
24+
- Configuration section `[mailer]` is no longer used.
25+
- Configuration section `[service]` is no longer used.
26+
- Configuration option `APP_NAME` is no longer used.
27+
- Configuration option `[security] REVERSE_PROXY_AUTHENTICATION_USER` is no longer used.
28+
- Configuration option `[database] PASSWD` is no longer used.
29+
- Configuration option `[auth] ACTIVE_CODE_LIVE_MINUTES` is no longer used.
30+
- Configuration option `[auth] RESET_PASSWD_CODE_LIVE_MINUTES` is no longer used.
31+
- Configuration option `[auth] ENABLE_CAPTCHA` is no longer used.
32+
- Configuration option `[auth] ENABLE_NOTIFY_MAIL` is no longer used.
33+
- Configuration option `[auth] REGISTER_EMAIL_CONFIRM` is no longer used.
34+
- Configuration option `[session] GC_INTERVAL_TIME` is no longer used.
35+
- Configuration option `[session] SESSION_LIFE_TIME` is no longer used.
36+
- Configuration option `[server] ROOT_URL` is no longer used.
37+
- Configuration option `[server] LANDING_PAGE` is no longer used.
38+
- Configuration option `[database] DB_TYPE` is no longer used.
39+
- Configuration option `[database] PASSWD` is no longer used.
40+
41+
## 0.12.1
42+
43+
### Fixed
44+
45+
- The `updated_at` field is now correctly updated when updates an issue. [#6209](https://github.com/gogs/gogs/issues/6209)
46+
- Fixed a regression which created `login_source.cfg` column to have `VARCHAR(255)` instead of `TEXT` in MySQL. [#6280](https://github.com/gogs/gogs/issues/6280)
647

748
## 0.12.0
849

internal/cmd/web.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ func runWeb(c *cli.Context) error {
632632
m.Get("/compare/:before([a-z0-9]{40})\\.\\.\\.:after([a-z0-9]{40})", repo.MustBeNotBare, context.RepoRef(), repo.CompareDiff)
633633
}, ignSignIn, context.RepoAssignment())
634634
m.Group("/:username/:reponame", func() {
635-
m.Get("", repo.Home)
635+
m.Get("", context.ServeGoGet(), repo.Home)
636636
m.Get("/stars", repo.Stars)
637637
m.Get("/watchers", repo.Watchers)
638638
}, ignSignIn, context.RepoAssignment(), context.RepoRef())
@@ -689,7 +689,7 @@ func runWeb(c *cli.Context) error {
689689
lfs.RegisterRoutes(m.Router)
690690
})
691691

692-
m.Route("/*", "GET,POST,OPTIONS", repo.HTTPContexter(), repo.HTTP)
692+
m.Route("/*", "GET,POST,OPTIONS", context.ServeGoGet(), repo.HTTPContexter(), repo.HTTP)
693693
})
694694

695695
// ***************************

internal/context/context.go

-48
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ import (
88
"fmt"
99
"io"
1010
"net/http"
11-
"path"
1211
"strings"
1312
"time"
1413

1514
"github.com/go-macaron/cache"
1615
"github.com/go-macaron/csrf"
1716
"github.com/go-macaron/i18n"
1817
"github.com/go-macaron/session"
19-
"github.com/unknwon/com"
2018
"gopkg.in/macaron.v1"
2119
log "unknwon.dev/clog/v2"
2220

@@ -249,52 +247,6 @@ func Contexter() macaron.Handler {
249247
c.Data["Link"] = template.EscapePound(c.Link)
250248
c.Data["PageStartTime"] = time.Now()
251249

252-
// Quick responses appropriate go-get meta with status 200
253-
// regardless of if user have access to the repository,
254-
// or the repository does not exist at all.
255-
// This is particular a workaround for "go get" command which does not respect
256-
// .netrc file.
257-
if c.Query("go-get") == "1" {
258-
ownerName := c.Params(":username")
259-
repoName := c.Params(":reponame")
260-
branchName := "master"
261-
262-
owner, err := db.GetUserByName(ownerName)
263-
if err != nil {
264-
c.NotFoundOrError(err, "get user by name")
265-
return
266-
}
267-
268-
repo, err := db.GetRepositoryByName(owner.ID, repoName)
269-
if err == nil && len(repo.DefaultBranch) > 0 {
270-
branchName = repo.DefaultBranch
271-
}
272-
273-
prefix := conf.Server.ExternalURL + path.Join(ownerName, repoName, "src", branchName)
274-
insecureFlag := ""
275-
if !strings.HasPrefix(conf.Server.ExternalURL, "https://") {
276-
insecureFlag = "--insecure "
277-
}
278-
c.PlainText(http.StatusOK, com.Expand(`<!doctype html>
279-
<html>
280-
<head>
281-
<meta name="go-import" content="{GoGetImport} git {CloneLink}">
282-
<meta name="go-source" content="{GoGetImport} _ {GoDocDirectory} {GoDocFile}">
283-
</head>
284-
<body>
285-
go get {InsecureFlag}{GoGetImport}
286-
</body>
287-
</html>
288-
`, map[string]string{
289-
"GoGetImport": path.Join(conf.Server.URL.Host, conf.Server.Subpath, ownerName, repoName),
290-
"CloneLink": db.ComposeHTTPSCloneURL(ownerName, repoName),
291-
"GoDocDirectory": prefix + "{/dir}",
292-
"GoDocFile": prefix + "{/dir}/{file}#L{line}",
293-
"InsecureFlag": insecureFlag,
294-
}))
295-
return
296-
}
297-
298250
if len(conf.HTTP.AccessControlAllowOrigin) > 0 {
299251
c.Header().Set("Access-Control-Allow-Origin", conf.HTTP.AccessControlAllowOrigin)
300252
c.Header().Set("'Access-Control-Allow-Credentials' ", "true")

internal/context/go_get.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package context
2+
3+
import (
4+
"net/http"
5+
"path"
6+
"strings"
7+
8+
"github.com/unknwon/com"
9+
"gopkg.in/macaron.v1"
10+
11+
"github.com/G-Node/gogs/internal/conf"
12+
"github.com/G-Node/gogs/internal/db"
13+
)
14+
15+
// ServeGoGet does quick responses for appropriate go-get meta with status OK
16+
// regardless of whether the user has access to the repository, or the repository
17+
// does exist at all. This is particular a workaround for "go get" command which
18+
// does not respect .netrc file.
19+
func ServeGoGet() macaron.Handler {
20+
return func(c *macaron.Context) {
21+
if c.Query("go-get") != "1" {
22+
return
23+
}
24+
25+
ownerName := c.Params(":username")
26+
repoName := c.Params(":reponame")
27+
branchName := "master"
28+
29+
owner, err := db.Users.GetByUsername(ownerName)
30+
if err == nil {
31+
repo, err := db.Repos.GetByName(owner.ID, repoName)
32+
if err == nil && repo.DefaultBranch != "" {
33+
branchName = repo.DefaultBranch
34+
}
35+
}
36+
37+
prefix := conf.Server.ExternalURL + path.Join(ownerName, repoName, "src", branchName)
38+
insecureFlag := ""
39+
if !strings.HasPrefix(conf.Server.ExternalURL, "https://") {
40+
insecureFlag = "--insecure "
41+
}
42+
c.PlainText(http.StatusOK, []byte(com.Expand(`<!doctype html>
43+
<html>
44+
<head>
45+
<meta name="go-import" content="{GoGetImport} git {CloneLink}">
46+
<meta name="go-source" content="{GoGetImport} _ {GoDocDirectory} {GoDocFile}">
47+
</head>
48+
<body>
49+
go get {InsecureFlag}{GoGetImport}
50+
</body>
51+
</html>
52+
`,
53+
map[string]string{
54+
"GoGetImport": path.Join(conf.Server.URL.Host, conf.Server.Subpath, ownerName, repoName),
55+
"CloneLink": db.ComposeHTTPSCloneURL(ownerName, repoName),
56+
"GoDocDirectory": prefix + "{/dir}",
57+
"GoDocFile": prefix + "{/dir}/{file}#L{line}",
58+
"InsecureFlag": insecureFlag,
59+
},
60+
)))
61+
}
62+
}

0 commit comments

Comments
 (0)