Skip to content

Commit 6bc0173

Browse files
authored
Merge branch 'main' into fix-textarea
2 parents 8b16a5e + ca31d47 commit 6bc0173

File tree

196 files changed

+1551
-1307
lines changed

Some content is hidden

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

196 files changed

+1551
-1307
lines changed

models/unittest/testdb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func CreateTestEngine(opts FixturesOptions) error {
206206
x, err := xorm.NewEngine("sqlite3", "file::memory:?cache=shared&_txlock=immediate")
207207
if err != nil {
208208
if strings.Contains(err.Error(), "unknown driver") {
209-
return fmt.Errorf(`sqlite3 requires: import _ "github.com/mattn/go-sqlite3" or -tags sqlite,sqlite_unlock_notify%s%w`, "\n", err)
209+
return fmt.Errorf(`sqlite3 requires: -tags sqlite,sqlite_unlock_notify%s%w`, "\n", err)
210210
}
211211
return err
212212
}

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/markup/html_commit.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"code.gitea.io/gitea/modules/base"
11+
"code.gitea.io/gitea/modules/references"
1112
"code.gitea.io/gitea/modules/util"
1213

1314
"golang.org/x/net/html"
@@ -194,3 +195,21 @@ func hashCurrentPatternProcessor(ctx *RenderContext, node *html.Node) {
194195
node = node.NextSibling.NextSibling
195196
}
196197
}
198+
199+
func commitCrossReferencePatternProcessor(ctx *RenderContext, node *html.Node) {
200+
next := node.NextSibling
201+
202+
for node != nil && node != next {
203+
found, ref := references.FindRenderizableCommitCrossReference(node.Data)
204+
if !found {
205+
return
206+
}
207+
208+
reftext := ref.Owner + "/" + ref.Name + "@" + base.ShortSha(ref.CommitSha)
209+
linkHref := ctx.RenderHelper.ResolveLink(util.URLJoin(ref.Owner, ref.Name, "commit", ref.CommitSha), LinkTypeApp)
210+
link := createLink(ctx, linkHref, reftext, "commit")
211+
212+
replaceContent(node, ref.RefLocation.Start, ref.RefLocation.End, link)
213+
node = node.NextSibling.NextSibling
214+
}
215+
}

0 commit comments

Comments
 (0)