Skip to content

Commit 8da5c23

Browse files
committed
Merge branch 'main' into lunny/avoid_create_temporary_cat_file
2 parents b0d0cc4 + c1b9ecc commit 8da5c23

File tree

34 files changed

+509
-203
lines changed

34 files changed

+509
-203
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,12 +410,12 @@ watch-backend: go-check ## watch backend files and continuously rebuild
410410
test: test-frontend test-backend ## test everything
411411

412412
.PHONY: test-backend
413-
test-backend: ## test frontend files
413+
test-backend: ## test backend files
414414
@echo "Running go test with $(GOTESTFLAGS) -tags '$(TEST_TAGS)'..."
415415
@$(GO) test $(GOTESTFLAGS) -tags='$(TEST_TAGS)' $(GO_TEST_PACKAGES)
416416

417417
.PHONY: test-frontend
418-
test-frontend: node_modules ## test backend files
418+
test-frontend: node_modules ## test frontend files
419419
npx vitest
420420

421421
.PHONY: test-check
@@ -737,7 +737,7 @@ generate-go: $(TAGS_PREREQ)
737737

738738
.PHONY: security-check
739739
security-check:
740-
go run $(GOVULNCHECK_PACKAGE) ./...
740+
go run $(GOVULNCHECK_PACKAGE) -show color ./...
741741

742742
$(EXECUTABLE): $(GO_SOURCES) $(TAGS_PREREQ)
743743
CGO_CFLAGS="$(CGO_CFLAGS)" $(GO) build $(GOFLAGS) $(EXTRA_GOFLAGS) -tags '$(TAGS)' -ldflags '-s -w $(LDFLAGS)' -o $@

models/actions/run_job.go

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

1212
"code.gitea.io/gitea/models/db"
13+
repo_model "code.gitea.io/gitea/models/repo"
1314
"code.gitea.io/gitea/modules/timeutil"
1415
"code.gitea.io/gitea/modules/util"
1516

@@ -19,11 +20,12 @@ import (
1920
// ActionRunJob represents a job of a run
2021
type ActionRunJob struct {
2122
ID int64
22-
RunID int64 `xorm:"index"`
23-
Run *ActionRun `xorm:"-"`
24-
RepoID int64 `xorm:"index"`
25-
OwnerID int64 `xorm:"index"`
26-
CommitSHA string `xorm:"index"`
23+
RunID int64 `xorm:"index"`
24+
Run *ActionRun `xorm:"-"`
25+
RepoID int64 `xorm:"index"`
26+
Repo *repo_model.Repository `xorm:"-"`
27+
OwnerID int64 `xorm:"index"`
28+
CommitSHA string `xorm:"index"`
2729
IsForkPullRequest bool
2830
Name string `xorm:"VARCHAR(255)"`
2931
Attempt int64
@@ -58,6 +60,17 @@ func (job *ActionRunJob) LoadRun(ctx context.Context) error {
5860
return nil
5961
}
6062

63+
func (job *ActionRunJob) LoadRepo(ctx context.Context) error {
64+
if job.Repo == nil {
65+
repo, err := repo_model.GetRepositoryByID(ctx, job.RepoID)
66+
if err != nil {
67+
return err
68+
}
69+
job.Repo = repo
70+
}
71+
return nil
72+
}
73+
6174
// LoadAttributes load Run if not loaded
6275
func (job *ActionRunJob) LoadAttributes(ctx context.Context) error {
6376
if job == nil {
@@ -83,7 +96,7 @@ func GetRunJobByID(ctx context.Context, id int64) (*ActionRunJob, error) {
8396
return &job, nil
8497
}
8598

86-
func GetRunJobsByRunID(ctx context.Context, runID int64) ([]*ActionRunJob, error) {
99+
func GetRunJobsByRunID(ctx context.Context, runID int64) (ActionJobList, error) {
87100
var jobs []*ActionRunJob
88101
if err := db.GetEngine(ctx).Where("run_id=?", runID).OrderBy("id").Find(&jobs); err != nil {
89102
return nil, err

models/actions/run_job_list.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88

99
"code.gitea.io/gitea/models/db"
10+
repo_model "code.gitea.io/gitea/models/repo"
1011
"code.gitea.io/gitea/modules/container"
1112
"code.gitea.io/gitea/modules/timeutil"
1213

@@ -21,7 +22,33 @@ func (jobs ActionJobList) GetRunIDs() []int64 {
2122
})
2223
}
2324

25+
func (jobs ActionJobList) LoadRepos(ctx context.Context) error {
26+
repoIDs := container.FilterSlice(jobs, func(j *ActionRunJob) (int64, bool) {
27+
return j.RepoID, j.RepoID != 0 && j.Repo == nil
28+
})
29+
if len(repoIDs) == 0 {
30+
return nil
31+
}
32+
33+
repos := make(map[int64]*repo_model.Repository, len(repoIDs))
34+
if err := db.GetEngine(ctx).In("id", repoIDs).Find(&repos); err != nil {
35+
return err
36+
}
37+
for _, j := range jobs {
38+
if j.RepoID > 0 && j.Repo == nil {
39+
j.Repo = repos[j.RepoID]
40+
}
41+
}
42+
return nil
43+
}
44+
2445
func (jobs ActionJobList) LoadRuns(ctx context.Context, withRepo bool) error {
46+
if withRepo {
47+
if err := jobs.LoadRepos(ctx); err != nil {
48+
return err
49+
}
50+
}
51+
2552
runIDs := jobs.GetRunIDs()
2653
runs := make(map[int64]*ActionRun, len(runIDs))
2754
if err := db.GetEngine(ctx).In("id", runIDs).Find(&runs); err != nil {
@@ -30,15 +57,9 @@ func (jobs ActionJobList) LoadRuns(ctx context.Context, withRepo bool) error {
3057
for _, j := range jobs {
3158
if j.RunID > 0 && j.Run == nil {
3259
j.Run = runs[j.RunID]
60+
j.Run.Repo = j.Repo
3361
}
3462
}
35-
if withRepo {
36-
var runsList RunList = make([]*ActionRun, 0, len(runs))
37-
for _, r := range runs {
38-
runsList = append(runsList, r)
39-
}
40-
return runsList.LoadRepos(ctx)
41-
}
4263
return nil
4364
}
4465

modules/git/batch.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,26 @@ type Batch struct {
1414
Writer WriteCloserError
1515
}
1616

17-
func (repo *Repository) NewBatch(ctx context.Context) (*Batch, error) {
17+
// NewBatch creates a new batch for the given repository, the Close must be invoked before release the batch
18+
func NewBatch(ctx context.Context, repoPath string) (*Batch, error) {
1819
// Now because of some insanity with git cat-file not immediately failing if not run in a valid git directory we need to run git rev-parse first!
19-
if err := ensureValidGitRepository(ctx, repo.Path); err != nil {
20+
if err := ensureValidGitRepository(ctx, repoPath); err != nil {
2021
return nil, err
2122
}
2223

2324
var batch Batch
24-
batch.Writer, batch.Reader, batch.cancel = catFileBatch(ctx, repo.Path)
25+
batch.Writer, batch.Reader, batch.cancel = catFileBatch(ctx, repoPath)
2526
return &batch, nil
2627
}
2728

28-
func (repo *Repository) NewBatchCheck(ctx context.Context) (*Batch, error) {
29+
func NewBatchCheck(ctx context.Context, repoPath string) (*Batch, error) {
2930
// Now because of some insanity with git cat-file not immediately failing if not run in a valid git directory we need to run git rev-parse first!
30-
if err := ensureValidGitRepository(ctx, repo.Path); err != nil {
31+
if err := ensureValidGitRepository(ctx, repoPath); err != nil {
3132
return nil, err
3233
}
3334

3435
var check Batch
35-
check.Writer, check.Reader, check.cancel = catFileBatchCheck(ctx, repo.Path)
36+
check.Writer, check.Reader, check.cancel = catFileBatchCheck(ctx, repoPath)
3637
return &check, nil
3738
}
3839

modules/git/command.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,10 @@ func (c *Command) run(ctx context.Context, skip int, opts *RunOpts) error {
350350
// We need to check if the context is canceled by the program on Windows.
351351
// This is because Windows does not have signal checking when terminating the process.
352352
// It always returns exit code 1, unlike Linux, which has many exit codes for signals.
353+
// `err.Error()` returns "exit status 1" when using the `git check-attr` command after the context is canceled.
353354
if runtime.GOOS == "windows" &&
354355
err != nil &&
355-
err.Error() == "" &&
356+
(err.Error() == "" || err.Error() == "exit status 1") &&
356357
cmd.ProcessState.ExitCode() == 1 &&
357358
ctx.Err() == context.Canceled {
358359
return ctx.Err()

modules/git/hook.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,26 @@ func GetHook(repoPath, name string) (*Hook, error) {
5151
name: name,
5252
path: filepath.Join(repoPath, "hooks", name+".d", name),
5353
}
54-
samplePath := filepath.Join(repoPath, "hooks", name+".sample")
55-
if isFile(h.path) {
54+
isFile, err := util.IsFile(h.path)
55+
if err != nil {
56+
return nil, err
57+
}
58+
if isFile {
5659
data, err := os.ReadFile(h.path)
5760
if err != nil {
5861
return nil, err
5962
}
6063
h.IsActive = true
6164
h.Content = string(data)
62-
} else if isFile(samplePath) {
65+
return h, nil
66+
}
67+
68+
samplePath := filepath.Join(repoPath, "hooks", name+".sample")
69+
isFile, err = util.IsFile(samplePath)
70+
if err != nil {
71+
return nil, err
72+
}
73+
if isFile {
6374
data, err := os.ReadFile(samplePath)
6475
if err != nil {
6576
return nil, err
@@ -77,7 +88,11 @@ func (h *Hook) Name() string {
7788
// Update updates hook settings.
7889
func (h *Hook) Update() error {
7990
if len(strings.TrimSpace(h.Content)) == 0 {
80-
if isExist(h.path) {
91+
exist, err := util.IsExist(h.path)
92+
if err != nil {
93+
return err
94+
}
95+
if exist {
8196
err := util.Remove(h.path)
8297
if err != nil {
8398
return err
@@ -101,7 +116,10 @@ func (h *Hook) Update() error {
101116

102117
// ListHooks returns a list of Git hooks of given repository.
103118
func ListHooks(repoPath string) (_ []*Hook, err error) {
104-
if !isDir(filepath.Join(repoPath, "hooks")) {
119+
exist, err := util.IsDir(filepath.Join(repoPath, "hooks"))
120+
if err != nil {
121+
return nil, err
122+
} else if !exist {
105123
return nil, errors.New("hooks path does not exist")
106124
}
107125

modules/git/repo_attribute.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ func (wr *nulSeparatedAttributeWriter) Write(p []byte) (n int, err error) {
280280
}
281281
}
282282
wr.tmp = append(wr.tmp, p...)
283-
return len(p), nil
283+
return l, nil
284284
}
285285

286286
func (wr *nulSeparatedAttributeWriter) ReadAttribute() <-chan attributeTriple {

modules/git/repo_base_gogit.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
4949
repoPath, err := filepath.Abs(repoPath)
5050
if err != nil {
5151
return nil, err
52-
} else if !isDir(repoPath) {
52+
}
53+
exist, err := util.IsDir(repoPath)
54+
if err != nil {
55+
return nil, err
56+
}
57+
if !exist {
5358
return nil, util.NewNotExistErrorf("no such file or directory")
5459
}
5560

modules/git/repo_base_nogogit.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
4747
repoPath, err := filepath.Abs(repoPath)
4848
if err != nil {
4949
return nil, err
50-
} else if !isDir(repoPath) {
50+
}
51+
exist, err := util.IsDir(repoPath)
52+
if err != nil {
53+
return nil, err
54+
}
55+
if !exist {
5156
return nil, util.NewNotExistErrorf("no such file or directory")
5257
}
5358

@@ -62,7 +67,7 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
6267
func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bufio.Reader, func(), error) {
6368
if repo.batch == nil {
6469
var err error
65-
repo.batch, err = repo.NewBatch(ctx)
70+
repo.batch, err = NewBatch(ctx, repo.Path)
6671
if err != nil {
6772
return nil, nil, nil, err
6873
}
@@ -76,7 +81,7 @@ func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bu
7681
}
7782

7883
log.Debug("Opening temporary cat file batch for: %s", repo.Path)
79-
tempBatch, err := repo.NewBatch(ctx)
84+
tempBatch, err := NewBatch(ctx, repo.Path)
8085
if err != nil {
8186
return nil, nil, nil, err
8287
}
@@ -87,7 +92,7 @@ func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bu
8792
func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError, *bufio.Reader, func(), error) {
8893
if repo.check == nil {
8994
var err error
90-
repo.check, err = repo.NewBatchCheck(ctx)
95+
repo.check, err = NewBatchCheck(ctx, repo.Path)
9196
if err != nil {
9297
return nil, nil, nil, err
9398
}
@@ -101,7 +106,7 @@ func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError
101106
}
102107

103108
log.Debug("Opening temporary cat file batch-check for: %s", repo.Path)
104-
tempBatchCheck, err := repo.NewBatchCheck(ctx)
109+
tempBatchCheck, err := NewBatchCheck(ctx, repo.Path)
105110
if err != nil {
106111
return nil, nil, nil, err
107112
}

modules/git/utils.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"encoding/hex"
99
"fmt"
1010
"io"
11-
"os"
1211
"strconv"
1312
"strings"
1413
"sync"
@@ -41,33 +40,6 @@ func (oc *ObjectCache[T]) Get(id string) (T, bool) {
4140
return obj, has
4241
}
4342

44-
// isDir returns true if given path is a directory,
45-
// or returns false when it's a file or does not exist.
46-
func isDir(dir string) bool {
47-
f, e := os.Stat(dir)
48-
if e != nil {
49-
return false
50-
}
51-
return f.IsDir()
52-
}
53-
54-
// isFile returns true if given path is a file,
55-
// or returns false when it's a directory or does not exist.
56-
func isFile(filePath string) bool {
57-
f, e := os.Stat(filePath)
58-
if e != nil {
59-
return false
60-
}
61-
return !f.IsDir()
62-
}
63-
64-
// isExist checks whether a file or directory exists.
65-
// It returns false when the file or directory does not exist.
66-
func isExist(path string) bool {
67-
_, err := os.Stat(path)
68-
return err == nil || os.IsExist(err)
69-
}
70-
7143
// ConcatenateError concatenats an error with stderr string
7244
func ConcatenateError(err error, stderr string) error {
7345
if len(stderr) == 0 {

0 commit comments

Comments
 (0)