Skip to content

Commit 987f0f1

Browse files
authored
Merge branch 'main' into open-repository-open-cat-file-batch
2 parents 9e37bb8 + 038e1db commit 987f0f1

File tree

7 files changed

+2146
-1356
lines changed

7 files changed

+2146
-1356
lines changed

custom/conf/app.example.ini

+2,013-1,288
Large diffs are not rendered by default.

integrations/goget_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integrations
6+
7+
import (
8+
"fmt"
9+
"net/http"
10+
"testing"
11+
12+
"code.gitea.io/gitea/modules/setting"
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestGoGet(t *testing.T) {
17+
defer prepareTestEnv(t)()
18+
19+
req := NewRequest(t, "GET", "/blah/glah/plah?go-get=1")
20+
resp := MakeRequest(t, req, http.StatusOK)
21+
22+
expected := fmt.Sprintf(`<!doctype html>
23+
<html>
24+
<head>
25+
<meta name="go-import" content="%[1]s:%[2]s/blah/glah git %[3]sblah/glah.git">
26+
<meta name="go-source" content="%[1]s:%[2]s/blah/glah _ %[3]sblah/glah/src/branch/master{/dir} %[3]sblah/glah/src/branch/master{/dir}/{file}#L{line}">
27+
</head>
28+
<body>
29+
go get --insecure %[1]s:%[2]s/blah/glah
30+
</body>
31+
</html>
32+
`, setting.Domain, setting.HTTPPort, setting.AppURL)
33+
34+
assert.Equal(t, expected, resp.Body.String())
35+
}

modules/references/references.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var (
2929
// mentionPattern matches all mentions in the form of "@user" or "@org/team"
3030
mentionPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(@[0-9a-zA-Z-_]+|@[0-9a-zA-Z-_]+\/?[0-9a-zA-Z-_]+|@[0-9a-zA-Z-_][0-9a-zA-Z-_.]+\/?[0-9a-zA-Z-_.]+[0-9a-zA-Z-_])(?:\s|[:,;.?!]\s|[:,;.?!]?$|\)|\])`)
3131
// issueNumericPattern matches string that references to a numeric issue, e.g. #1287
32-
issueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([#!][0-9]+)(?:\s|$|\)|\]|[:;,.?!]\s|[:;,.?!]$)`)
32+
issueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[|\')([#!][0-9]+)(?:\s|$|\)|\]|[:;,.?!]\s|[:;,.?!]$)`)
3333
// issueAlphanumericPattern matches string that references to an alphanumeric issue, e.g. ABC-1234
3434
issueAlphanumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([A-Z]{1,10}-[1-9][0-9]*)(?:\s|$|\)|\]|:|\.(\s|$))`)
3535
// crossReferenceIssueNumericPattern matches string that references a numeric issue in a different repository

modules/references/references_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ func TestFindAllIssueReferences(t *testing.T) {
197197
{200, "user3", "repo4", "200", false, XRefActionNone, &RefSpan{Start: 5, End: 20}, nil, ""},
198198
},
199199
},
200+
{
201+
"Merge pull request '#12345 My fix for a bug' (!1337) from feature-branch into main",
202+
[]testResult{
203+
{12345, "", "", "12345", false, XRefActionNone, &RefSpan{Start: 20, End: 26}, nil, ""},
204+
{1337, "", "", "1337", true, XRefActionNone, &RefSpan{Start: 46, End: 51}, nil, ""},
205+
},
206+
},
200207
{
201208
"Which abc. #9434 same as above",
202209
[]testResult{

modules/templates/helper.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ func ReactionToEmoji(reaction string) template.HTML {
766766
if val != nil {
767767
return template.HTML(val.Emoji)
768768
}
769-
return template.HTML(fmt.Sprintf(`<img alt=":%s:" src="%s/img/emoji/%s.png"></img>`, reaction, setting.StaticURLPrefix, reaction))
769+
return template.HTML(fmt.Sprintf(`<img alt=":%s:" src="%s/assets/img/emoji/%s.png"></img>`, reaction, setting.StaticURLPrefix, reaction))
770770
}
771771

772772
// RenderNote renders the contents of a git-notes file as a commit message.

routers/routes/goget.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package routes
6+
7+
import (
8+
"net/http"
9+
"net/url"
10+
"path"
11+
"strings"
12+
13+
"code.gitea.io/gitea/models"
14+
"code.gitea.io/gitea/modules/context"
15+
"code.gitea.io/gitea/modules/setting"
16+
"code.gitea.io/gitea/modules/util"
17+
"github.com/unknwon/com"
18+
)
19+
20+
func goGet(ctx *context.Context) {
21+
if ctx.Req.Method != "GET" || ctx.Query("go-get") != "1" || len(ctx.Req.URL.Query()) > 1 {
22+
return
23+
}
24+
25+
parts := strings.SplitN(ctx.Req.URL.EscapedPath(), "/", 4)
26+
27+
if len(parts) < 3 {
28+
return
29+
}
30+
31+
ownerName := parts[1]
32+
repoName := parts[2]
33+
34+
// Quick responses appropriate go-get meta with status 200
35+
// regardless of if user have access to the repository,
36+
// or the repository does not exist at all.
37+
// This is particular a workaround for "go get" command which does not respect
38+
// .netrc file.
39+
40+
trimmedRepoName := strings.TrimSuffix(repoName, ".git")
41+
42+
if ownerName == "" || trimmedRepoName == "" {
43+
_, _ = ctx.Write([]byte(`<!doctype html>
44+
<html>
45+
<body>
46+
invalid import path
47+
</body>
48+
</html>
49+
`))
50+
ctx.Status(400)
51+
return
52+
}
53+
branchName := setting.Repository.DefaultBranch
54+
55+
repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName)
56+
if err == nil && len(repo.DefaultBranch) > 0 {
57+
branchName = repo.DefaultBranch
58+
}
59+
prefix := setting.AppURL + path.Join(url.PathEscape(ownerName), url.PathEscape(repoName), "src", "branch", util.PathEscapeSegments(branchName))
60+
61+
appURL, _ := url.Parse(setting.AppURL)
62+
63+
insecure := ""
64+
if appURL.Scheme == string(setting.HTTP) {
65+
insecure = "--insecure "
66+
}
67+
ctx.Header().Set("Content-Type", "text/html")
68+
ctx.Status(http.StatusOK)
69+
_, _ = ctx.Write([]byte(com.Expand(`<!doctype html>
70+
<html>
71+
<head>
72+
<meta name="go-import" content="{GoGetImport} git {CloneLink}">
73+
<meta name="go-source" content="{GoGetImport} _ {GoDocDirectory} {GoDocFile}">
74+
</head>
75+
<body>
76+
go get {Insecure}{GoGetImport}
77+
</body>
78+
</html>
79+
`, map[string]string{
80+
"GoGetImport": context.ComposeGoGetImport(ownerName, trimmedRepoName),
81+
"CloneLink": models.ComposeHTTPSCloneURL(ownerName, repoName),
82+
"GoDocDirectory": prefix + "{/dir}",
83+
"GoDocFile": prefix + "{/dir}/{file}#L{line}",
84+
"Insecure": insecure,
85+
})))
86+
}

routers/routes/web.go

+3-66
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"encoding/gob"
99
"fmt"
1010
"net/http"
11-
"net/url"
1211
"os"
1312
"path"
1413
"strings"
@@ -22,7 +21,6 @@ import (
2221
"code.gitea.io/gitea/modules/setting"
2322
"code.gitea.io/gitea/modules/storage"
2423
"code.gitea.io/gitea/modules/templates"
25-
"code.gitea.io/gitea/modules/util"
2624
"code.gitea.io/gitea/modules/validation"
2725
"code.gitea.io/gitea/modules/web"
2826
"code.gitea.io/gitea/routers"
@@ -51,7 +49,6 @@ import (
5149
"github.com/go-chi/cors"
5250
"github.com/prometheus/client_golang/prometheus"
5351
"github.com/tstranex/u2f"
54-
"github.com/unknwon/com"
5552
)
5653

5754
const (
@@ -173,7 +170,7 @@ func WebRoutes() *web.Route {
173170

174171
// this png is very likely to always be below the limit for gzip so it doesn't need to pass through gzip
175172
routes.Get("/apple-touch-icon.png", func(w http.ResponseWriter, req *http.Request) {
176-
http.Redirect(w, req, path.Join(setting.StaticURLPrefix, "img/apple-touch-icon.png"), 301)
173+
http.Redirect(w, req, path.Join(setting.StaticURLPrefix, "/assets/img/apple-touch-icon.png"), 301)
177174
})
178175

179176
gob.Register(&u2f.Challenge{})
@@ -228,6 +225,7 @@ func WebRoutes() *web.Route {
228225
// TODO: These really seem like things that could be folded into Contexter or as helper functions
229226
common = append(common, user.GetNotificationCount)
230227
common = append(common, repo.GetActiveStopwatch)
228+
common = append(common, goGet)
231229

232230
others := web.NewRoute()
233231
for _, middle := range common {
@@ -239,67 +237,6 @@ func WebRoutes() *web.Route {
239237
return routes
240238
}
241239

242-
func goGet(ctx *context.Context) {
243-
if ctx.Query("go-get") != "1" {
244-
return
245-
}
246-
247-
// Quick responses appropriate go-get meta with status 200
248-
// regardless of if user have access to the repository,
249-
// or the repository does not exist at all.
250-
// This is particular a workaround for "go get" command which does not respect
251-
// .netrc file.
252-
253-
ownerName := ctx.Params(":username")
254-
repoName := ctx.Params(":reponame")
255-
trimmedRepoName := strings.TrimSuffix(repoName, ".git")
256-
257-
if ownerName == "" || trimmedRepoName == "" {
258-
_, _ = ctx.Write([]byte(`<!doctype html>
259-
<html>
260-
<body>
261-
invalid import path
262-
</body>
263-
</html>
264-
`))
265-
ctx.Status(400)
266-
return
267-
}
268-
branchName := setting.Repository.DefaultBranch
269-
270-
repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName)
271-
if err == nil && len(repo.DefaultBranch) > 0 {
272-
branchName = repo.DefaultBranch
273-
}
274-
prefix := setting.AppURL + path.Join(url.PathEscape(ownerName), url.PathEscape(repoName), "src", "branch", util.PathEscapeSegments(branchName))
275-
276-
appURL, _ := url.Parse(setting.AppURL)
277-
278-
insecure := ""
279-
if appURL.Scheme == string(setting.HTTP) {
280-
insecure = "--insecure "
281-
}
282-
ctx.Header().Set("Content-Type", "text/html")
283-
ctx.Status(http.StatusOK)
284-
_, _ = ctx.Write([]byte(com.Expand(`<!doctype html>
285-
<html>
286-
<head>
287-
<meta name="go-import" content="{GoGetImport} git {CloneLink}">
288-
<meta name="go-source" content="{GoGetImport} _ {GoDocDirectory} {GoDocFile}">
289-
</head>
290-
<body>
291-
go get {Insecure}{GoGetImport}
292-
</body>
293-
</html>
294-
`, map[string]string{
295-
"GoGetImport": context.ComposeGoGetImport(ownerName, trimmedRepoName),
296-
"CloneLink": models.ComposeHTTPSCloneURL(ownerName, repoName),
297-
"GoDocDirectory": prefix + "{/dir}",
298-
"GoDocFile": prefix + "{/dir}/{file}#L{line}",
299-
"Insecure": insecure,
300-
})))
301-
}
302-
303240
// RegisterRoutes register routes
304241
func RegisterRoutes(m *web.Route) {
305242
reqSignIn := context.Toggle(&context.ToggleOptions{SignInRequired: true})
@@ -1104,7 +1041,7 @@ func RegisterRoutes(m *web.Route) {
11041041
m.Group("/{username}", func() {
11051042
m.Group("/{reponame}", func() {
11061043
m.Get("", repo.SetEditorconfigIfExists, repo.Home)
1107-
}, goGet, ignSignIn, context.RepoAssignment, context.RepoRef(), context.UnitTypes())
1044+
}, ignSignIn, context.RepoAssignment, context.RepoRef(), context.UnitTypes())
11081045

11091046
m.Group("/{reponame}", func() {
11101047
m.Group("/info/lfs", func() {

0 commit comments

Comments
 (0)