Skip to content

Commit a86c9e4

Browse files
committed
Merge branch 'main' into add-file-tree-to-file-view-page
2 parents 38de8aa + 6d5aa92 commit a86c9e4

File tree

90 files changed

+920
-559
lines changed

Some content is hidden

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

90 files changed

+920
-559
lines changed

models/actions/runner_token.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"code.gitea.io/gitea/models/db"
1111
repo_model "code.gitea.io/gitea/models/repo"
1212
user_model "code.gitea.io/gitea/models/user"
13+
"code.gitea.io/gitea/modules/base"
1314
"code.gitea.io/gitea/modules/timeutil"
1415
"code.gitea.io/gitea/modules/util"
1516
)
@@ -51,7 +52,7 @@ func GetRunnerToken(ctx context.Context, token string) (*ActionRunnerToken, erro
5152
if err != nil {
5253
return nil, err
5354
} else if !has {
54-
return nil, fmt.Errorf("runner token %q: %w", token, util.ErrNotExist)
55+
return nil, fmt.Errorf(`runner token "%s...": %w`, base.TruncateString(token, 3), util.ErrNotExist)
5556
}
5657
return &runnerToken, nil
5758
}
@@ -68,19 +69,15 @@ func UpdateRunnerToken(ctx context.Context, r *ActionRunnerToken, cols ...string
6869
return err
6970
}
7071

71-
// NewRunnerToken creates a new active runner token and invalidate all old tokens
72+
// NewRunnerTokenWithValue creates a new active runner token and invalidate all old tokens
7273
// ownerID will be ignored and treated as 0 if repoID is non-zero.
73-
func NewRunnerToken(ctx context.Context, ownerID, repoID int64) (*ActionRunnerToken, error) {
74+
func NewRunnerTokenWithValue(ctx context.Context, ownerID, repoID int64, token string) (*ActionRunnerToken, error) {
7475
if ownerID != 0 && repoID != 0 {
7576
// It's trying to create a runner token that belongs to a repository, but OwnerID has been set accidentally.
7677
// Remove OwnerID to avoid confusion; it's not worth returning an error here.
7778
ownerID = 0
7879
}
7980

80-
token, err := util.CryptoRandomString(40)
81-
if err != nil {
82-
return nil, err
83-
}
8481
runnerToken := &ActionRunnerToken{
8582
OwnerID: ownerID,
8683
RepoID: repoID,
@@ -95,11 +92,19 @@ func NewRunnerToken(ctx context.Context, ownerID, repoID int64) (*ActionRunnerTo
9592
return err
9693
}
9794

98-
_, err = db.GetEngine(ctx).Insert(runnerToken)
95+
_, err := db.GetEngine(ctx).Insert(runnerToken)
9996
return err
10097
})
10198
}
10299

100+
func NewRunnerToken(ctx context.Context, ownerID, repoID int64) (*ActionRunnerToken, error) {
101+
token, err := util.CryptoRandomString(40)
102+
if err != nil {
103+
return nil, err
104+
}
105+
return NewRunnerTokenWithValue(ctx, ownerID, repoID, token)
106+
}
107+
103108
// GetLatestRunnerToken returns the latest runner token
104109
func GetLatestRunnerToken(ctx context.Context, ownerID, repoID int64) (*ActionRunnerToken, error) {
105110
if ownerID != 0 && repoID != 0 {

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_commit.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,6 @@ type CommitsByFileAndRangeOptions struct {
216216

217217
// CommitsByFileAndRange return the commits according revision file and the page
218218
func (repo *Repository) CommitsByFileAndRange(opts CommitsByFileAndRangeOptions) ([]*Commit, error) {
219-
skip := (opts.Page - 1) * setting.Git.CommitsRangeSize
220-
221219
stdoutReader, stdoutWriter := io.Pipe()
222220
defer func() {
223221
_ = stdoutReader.Close()
@@ -226,8 +224,8 @@ func (repo *Repository) CommitsByFileAndRange(opts CommitsByFileAndRangeOptions)
226224
go func() {
227225
stderr := strings.Builder{}
228226
gitCmd := NewCommand(repo.Ctx, "rev-list").
229-
AddOptionFormat("--max-count=%d", setting.Git.CommitsRangeSize*opts.Page).
230-
AddOptionFormat("--skip=%d", skip)
227+
AddOptionFormat("--max-count=%d", setting.Git.CommitsRangeSize).
228+
AddOptionFormat("--skip=%d", (opts.Page-1)*setting.Git.CommitsRangeSize)
231229
gitCmd.AddDynamicArguments(opts.Revision)
232230

233231
if opts.Not != "" {

modules/git/repo_commit_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import (
88
"path/filepath"
99
"testing"
1010

11+
"code.gitea.io/gitea/modules/setting"
12+
"code.gitea.io/gitea/modules/test"
13+
1114
"github.com/stretchr/testify/assert"
15+
"github.com/stretchr/testify/require"
1216
)
1317

1418
func TestRepository_GetCommitBranches(t *testing.T) {
@@ -126,3 +130,21 @@ func TestGetRefCommitID(t *testing.T) {
126130
}
127131
}
128132
}
133+
134+
func TestCommitsByFileAndRange(t *testing.T) {
135+
defer test.MockVariableValue(&setting.Git.CommitsRangeSize, 2)()
136+
137+
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
138+
bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
139+
require.NoError(t, err)
140+
defer bareRepo1.Close()
141+
142+
// "foo" has 3 commits in "master" branch
143+
commits, err := bareRepo1.CommitsByFileAndRange(CommitsByFileAndRangeOptions{Revision: "master", File: "foo", Page: 1})
144+
require.NoError(t, err)
145+
assert.Len(t, commits, 2)
146+
147+
commits, err = bareRepo1.CommitsByFileAndRange(CommitsByFileAndRangeOptions{Revision: "master", File: "foo", Page: 2})
148+
require.NoError(t, err)
149+
assert.Len(t, commits, 1)
150+
}

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)