Skip to content

Commit 7da99b0

Browse files
committed
Merge remote-tracking branch 'giteaofficial/main'
* giteaofficial/main: demilestone should not include milestone (go-gitea#32923) fix textarea newline handle (go-gitea#32966) Fix Azure blob object `Seek` (go-gitea#32974) Fix maven pom inheritance (go-gitea#32943) Refactor arch route handlers (go-gitea#32972) [skip ci] Updated translations via Crowdin Refactor tmpl and blob_excerpt (go-gitea#32967) Clarify path param naming (go-gitea#32969) Refactor getpatch/getdiff functions and remove unnecessary fallback (go-gitea#32817) Refactor request context (go-gitea#32956)
2 parents c837f69 + f44712f commit 7da99b0

File tree

159 files changed

+1326
-1206
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

159 files changed

+1326
-1206
lines changed

modules/git/repo_compare.go

Lines changed: 9 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -233,72 +233,34 @@ func parseDiffStat(stdout string) (numFiles, totalAdditions, totalDeletions int,
233233
return numFiles, totalAdditions, totalDeletions, err
234234
}
235235

236-
// GetDiffOrPatch generates either diff or formatted patch data between given revisions
237-
func (repo *Repository) GetDiffOrPatch(base, head string, w io.Writer, patch, binary bool) error {
238-
if patch {
239-
return repo.GetPatch(base, head, w)
240-
}
241-
if binary {
242-
return repo.GetDiffBinary(base, head, w)
243-
}
244-
return repo.GetDiff(base, head, w)
245-
}
246-
247236
// GetDiff generates and returns patch data between given revisions, optimized for human readability
248-
func (repo *Repository) GetDiff(base, head string, w io.Writer) error {
237+
func (repo *Repository) GetDiff(compareArg string, w io.Writer) error {
249238
stderr := new(bytes.Buffer)
250-
err := NewCommand(repo.Ctx, "diff", "-p").AddDynamicArguments(base + "..." + head).
239+
return NewCommand(repo.Ctx, "diff", "-p").AddDynamicArguments(compareArg).
251240
Run(&RunOpts{
252241
Dir: repo.Path,
253242
Stdout: w,
254243
Stderr: stderr,
255244
})
256-
if err != nil && bytes.Contains(stderr.Bytes(), []byte("no merge base")) {
257-
return NewCommand(repo.Ctx, "diff", "-p").AddDynamicArguments(base, head).
258-
Run(&RunOpts{
259-
Dir: repo.Path,
260-
Stdout: w,
261-
})
262-
}
263-
return err
264245
}
265246

266247
// GetDiffBinary generates and returns patch data between given revisions, including binary diffs.
267-
func (repo *Repository) GetDiffBinary(base, head string, w io.Writer) error {
268-
stderr := new(bytes.Buffer)
269-
err := NewCommand(repo.Ctx, "diff", "-p", "--binary", "--histogram").AddDynamicArguments(base + "..." + head).
270-
Run(&RunOpts{
271-
Dir: repo.Path,
272-
Stdout: w,
273-
Stderr: stderr,
274-
})
275-
if err != nil && bytes.Contains(stderr.Bytes(), []byte("no merge base")) {
276-
return NewCommand(repo.Ctx, "diff", "-p", "--binary", "--histogram").AddDynamicArguments(base, head).
277-
Run(&RunOpts{
278-
Dir: repo.Path,
279-
Stdout: w,
280-
})
281-
}
282-
return err
248+
func (repo *Repository) GetDiffBinary(compareArg string, w io.Writer) error {
249+
return NewCommand(repo.Ctx, "diff", "-p", "--binary", "--histogram").AddDynamicArguments(compareArg).Run(&RunOpts{
250+
Dir: repo.Path,
251+
Stdout: w,
252+
})
283253
}
284254

285255
// GetPatch generates and returns format-patch data between given revisions, able to be used with `git apply`
286-
func (repo *Repository) GetPatch(base, head string, w io.Writer) error {
256+
func (repo *Repository) GetPatch(compareArg string, w io.Writer) error {
287257
stderr := new(bytes.Buffer)
288-
err := NewCommand(repo.Ctx, "format-patch", "--binary", "--stdout").AddDynamicArguments(base + "..." + head).
258+
return NewCommand(repo.Ctx, "format-patch", "--binary", "--stdout").AddDynamicArguments(compareArg).
289259
Run(&RunOpts{
290260
Dir: repo.Path,
291261
Stdout: w,
292262
Stderr: stderr,
293263
})
294-
if err != nil && bytes.Contains(stderr.Bytes(), []byte("no merge base")) {
295-
return NewCommand(repo.Ctx, "format-patch", "--binary", "--stdout").AddDynamicArguments(base, head).
296-
Run(&RunOpts{
297-
Dir: repo.Path,
298-
Stdout: w,
299-
})
300-
}
301-
return err
302264
}
303265

304266
// GetFilesChangedBetween returns a list of all files that have been changed between the given commits
@@ -329,21 +291,6 @@ func (repo *Repository) GetFilesChangedBetween(base, head string) ([]string, err
329291
return split, err
330292
}
331293

332-
// GetDiffFromMergeBase generates and return patch data from merge base to head
333-
func (repo *Repository) GetDiffFromMergeBase(base, head string, w io.Writer) error {
334-
stderr := new(bytes.Buffer)
335-
err := NewCommand(repo.Ctx, "diff", "-p", "--binary").AddDynamicArguments(base + "..." + head).
336-
Run(&RunOpts{
337-
Dir: repo.Path,
338-
Stdout: w,
339-
Stderr: stderr,
340-
})
341-
if err != nil && bytes.Contains(stderr.Bytes(), []byte("no merge base")) {
342-
return repo.GetDiffBinary(base, head, w)
343-
}
344-
return err
345-
}
346-
347294
// ReadPatchCommit will check if a diff patch exists and return stats
348295
func (repo *Repository) ReadPatchCommit(prID int64) (commitSHA string, err error) {
349296
// Migrated repositories download patches to "pulls" location

modules/git/repo_compare_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestGetFormatPatch(t *testing.T) {
2828
defer repo.Close()
2929

3030
rd := &bytes.Buffer{}
31-
err = repo.GetPatch("8d92fc95^", "8d92fc95", rd)
31+
err = repo.GetPatch("8d92fc95^...8d92fc95", rd)
3232
if err != nil {
3333
assert.NoError(t, err)
3434
return

modules/gitrepo/gitrepo.go

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111

1212
"code.gitea.io/gitea/modules/git"
13+
"code.gitea.io/gitea/modules/reqctx"
1314
"code.gitea.io/gitea/modules/setting"
1415
"code.gitea.io/gitea/modules/util"
1516
)
@@ -38,63 +39,32 @@ func OpenWikiRepository(ctx context.Context, repo Repository) (*git.Repository,
3839

3940
// contextKey is a value for use with context.WithValue.
4041
type contextKey struct {
41-
name string
42-
}
43-
44-
// RepositoryContextKey is a context key. It is used with context.Value() to get the current Repository for the context
45-
var RepositoryContextKey = &contextKey{"repository"}
46-
47-
// RepositoryFromContext attempts to get the repository from the context
48-
func repositoryFromContext(ctx context.Context, repo Repository) *git.Repository {
49-
value := ctx.Value(RepositoryContextKey)
50-
if value == nil {
51-
return nil
52-
}
53-
54-
if gitRepo, ok := value.(*git.Repository); ok && gitRepo != nil {
55-
if gitRepo.Path == repoPath(repo) {
56-
return gitRepo
57-
}
58-
}
59-
60-
return nil
42+
repoPath string
6143
}
6244

6345
// RepositoryFromContextOrOpen attempts to get the repository from the context or just opens it
6446
func RepositoryFromContextOrOpen(ctx context.Context, repo Repository) (*git.Repository, io.Closer, error) {
65-
gitRepo := repositoryFromContext(ctx, repo)
66-
if gitRepo != nil {
67-
return gitRepo, util.NopCloser{}, nil
47+
ds := reqctx.GetRequestDataStore(ctx)
48+
if ds != nil {
49+
gitRepo, err := RepositoryFromRequestContextOrOpen(ctx, ds, repo)
50+
return gitRepo, util.NopCloser{}, err
6851
}
69-
7052
gitRepo, err := OpenRepository(ctx, repo)
7153
return gitRepo, gitRepo, err
7254
}
7355

74-
// repositoryFromContextPath attempts to get the repository from the context
75-
func repositoryFromContextPath(ctx context.Context, path string) *git.Repository {
76-
value := ctx.Value(RepositoryContextKey)
77-
if value == nil {
78-
return nil
56+
// RepositoryFromRequestContextOrOpen opens the repository at the given relative path in the provided request context
57+
// The repo will be automatically closed when the request context is done
58+
func RepositoryFromRequestContextOrOpen(ctx context.Context, ds reqctx.RequestDataStore, repo Repository) (*git.Repository, error) {
59+
ck := contextKey{repoPath: repoPath(repo)}
60+
if gitRepo, ok := ctx.Value(ck).(*git.Repository); ok {
61+
return gitRepo, nil
7962
}
80-
81-
if repo, ok := value.(*git.Repository); ok && repo != nil {
82-
if repo.Path == path {
83-
return repo
84-
}
63+
gitRepo, err := git.OpenRepository(ctx, ck.repoPath)
64+
if err != nil {
65+
return nil, err
8566
}
86-
87-
return nil
88-
}
89-
90-
// RepositoryFromContextOrOpenPath attempts to get the repository from the context or just opens it
91-
// Deprecated: Use RepositoryFromContextOrOpen instead
92-
func RepositoryFromContextOrOpenPath(ctx context.Context, path string) (*git.Repository, io.Closer, error) {
93-
gitRepo := repositoryFromContextPath(ctx, path)
94-
if gitRepo != nil {
95-
return gitRepo, util.NopCloser{}, nil
96-
}
97-
98-
gitRepo, err := git.OpenRepository(ctx, path)
99-
return gitRepo, gitRepo, err
67+
ds.AddCloser(gitRepo)
68+
ds.SetContextValue(ck, gitRepo)
69+
return gitRepo, nil
10070
}

modules/gitrepo/walk_gogit.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@ import (
1414
// WalkReferences walks all the references from the repository
1515
// refname is empty, ObjectTag or ObjectBranch. All other values should be treated as equivalent to empty.
1616
func WalkReferences(ctx context.Context, repo Repository, walkfn func(sha1, refname string) error) (int, error) {
17-
gitRepo := repositoryFromContext(ctx, repo)
18-
if gitRepo == nil {
19-
var err error
20-
gitRepo, err = OpenRepository(ctx, repo)
21-
if err != nil {
22-
return 0, err
23-
}
24-
defer gitRepo.Close()
17+
gitRepo, closer, err := RepositoryFromContextOrOpen(ctx, repo)
18+
if err != nil {
19+
return 0, err
2520
}
21+
defer closer.Close()
2622

2723
i := 0
2824
iter, err := gitRepo.GoGitRepo().References()

modules/packages/maven/metadata.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/xml"
88
"io"
99

10+
"code.gitea.io/gitea/modules/util"
1011
"code.gitea.io/gitea/modules/validation"
1112

1213
"golang.org/x/net/html/charset"
@@ -31,18 +32,27 @@ type Dependency struct {
3132
}
3233

3334
type pomStruct struct {
34-
XMLName xml.Name `xml:"project"`
35-
GroupID string `xml:"groupId"`
36-
ArtifactID string `xml:"artifactId"`
37-
Version string `xml:"version"`
38-
Name string `xml:"name"`
39-
Description string `xml:"description"`
40-
URL string `xml:"url"`
41-
Licenses []struct {
35+
XMLName xml.Name `xml:"project"`
36+
37+
Parent struct {
38+
GroupID string `xml:"groupId"`
39+
ArtifactID string `xml:"artifactId"`
40+
Version string `xml:"version"`
41+
} `xml:"parent"`
42+
43+
GroupID string `xml:"groupId"`
44+
ArtifactID string `xml:"artifactId"`
45+
Version string `xml:"version"`
46+
Name string `xml:"name"`
47+
Description string `xml:"description"`
48+
URL string `xml:"url"`
49+
50+
Licenses []struct {
4251
Name string `xml:"name"`
4352
URL string `xml:"url"`
4453
Distribution string `xml:"distribution"`
4554
} `xml:"licenses>license"`
55+
4656
Dependencies []struct {
4757
GroupID string `xml:"groupId"`
4858
ArtifactID string `xml:"artifactId"`
@@ -81,8 +91,16 @@ func ParsePackageMetaData(r io.Reader) (*Metadata, error) {
8191
})
8292
}
8393

94+
pomGroupID := pom.GroupID
95+
if pomGroupID == "" {
96+
// the current module could inherit parent: https://maven.apache.org/pom.html#Inheritance
97+
pomGroupID = pom.Parent.GroupID
98+
}
99+
if pomGroupID == "" {
100+
return nil, util.ErrInvalidArgument
101+
}
84102
return &Metadata{
85-
GroupID: pom.GroupID,
103+
GroupID: pomGroupID,
86104
ArtifactID: pom.ArtifactID,
87105
Name: pom.Name,
88106
Description: pom.Description,

modules/packages/maven/metadata_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import (
77
"strings"
88
"testing"
99

10+
"code.gitea.io/gitea/modules/util"
11+
1012
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
1114
"golang.org/x/text/encoding/charmap"
1215
)
1316

@@ -86,4 +89,35 @@ func TestParsePackageMetaData(t *testing.T) {
8689
assert.NoError(t, err)
8790
assert.NotNil(t, m)
8891
})
92+
93+
t.Run("ParentInherit", func(t *testing.T) {
94+
pom := `<?xml version="1.0"?>
95+
<project>
96+
<modelVersion>4.0.0</modelVersion>
97+
<parent>
98+
<groupId>com.mycompany.app</groupId>
99+
<artifactId>my-app</artifactId>
100+
<version>1.0-SNAPSHOT</version>
101+
</parent>
102+
<artifactId>submodule1</artifactId>
103+
</project>
104+
`
105+
m, err := ParsePackageMetaData(strings.NewReader(pom))
106+
require.NoError(t, err)
107+
require.NotNil(t, m)
108+
109+
assert.Equal(t, "com.mycompany.app", m.GroupID)
110+
assert.Equal(t, "submodule1", m.ArtifactID)
111+
})
112+
113+
t.Run("ParentInherit", func(t *testing.T) {
114+
pom := `<?xml version="1.0"?>
115+
<project>
116+
<modelVersion>4.0.0</modelVersion>
117+
<artifactId></artifactId>
118+
</project>
119+
`
120+
_, err := ParsePackageMetaData(strings.NewReader(pom))
121+
require.ErrorIs(t, err, util.ErrInvalidArgument)
122+
})
89123
}

0 commit comments

Comments
 (0)