loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request '[PORT] Refactor the usage of batch catfile (gitea#31754)' (#5122) from gusted/forgejo-port-gt-31754 into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/5122
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>

Gusted d6231261 190b5a38

+447 -279
+46
modules/git/batch.go
··· 1 + // Copyright 2024 The Gitea Authors. All rights reserved. 2 + // SPDX-License-Identifier: MIT 3 + 4 + package git 5 + 6 + import ( 7 + "bufio" 8 + "context" 9 + ) 10 + 11 + type Batch struct { 12 + cancel context.CancelFunc 13 + Reader *bufio.Reader 14 + Writer WriteCloserError 15 + } 16 + 17 + func (repo *Repository) NewBatch(ctx context.Context) (*Batch, error) { 18 + // 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 + return nil, err 21 + } 22 + 23 + var batch Batch 24 + batch.Writer, batch.Reader, batch.cancel = catFileBatch(ctx, repo.Path) 25 + return &batch, nil 26 + } 27 + 28 + func (repo *Repository) NewBatchCheck(ctx context.Context) (*Batch, error) { 29 + // 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 + return nil, err 32 + } 33 + 34 + var check Batch 35 + check.Writer, check.Reader, check.cancel = catFileBatchCheck(ctx, repo.Path) 36 + return &check, nil 37 + } 38 + 39 + func (b *Batch) Close() { 40 + if b.cancel != nil { 41 + b.cancel() 42 + b.Reader = nil 43 + b.Writer = nil 44 + b.cancel = nil 45 + } 46 + }
+6 -6
modules/git/batch_reader.go
··· 26 26 CloseWithError(err error) error 27 27 } 28 28 29 - // EnsureValidGitRepository runs git rev-parse in the repository path - thus ensuring that the repository is a valid repository. 29 + // ensureValidGitRepository runs git rev-parse in the repository path - thus ensuring that the repository is a valid repository. 30 30 // Run before opening git cat-file. 31 31 // This is needed otherwise the git cat-file will hang for invalid repositories. 32 - func EnsureValidGitRepository(ctx context.Context, repoPath string) error { 32 + func ensureValidGitRepository(ctx context.Context, repoPath string) error { 33 33 stderr := strings.Builder{} 34 34 err := NewCommand(ctx, "rev-parse"). 35 35 SetDescription(fmt.Sprintf("%s rev-parse [repo_path: %s]", GitExecutable, repoPath)). ··· 43 43 return nil 44 44 } 45 45 46 - // CatFileBatchCheck opens git cat-file --batch-check in the provided repo and returns a stdin pipe, a stdout reader and cancel function 47 - func CatFileBatchCheck(ctx context.Context, repoPath string) (WriteCloserError, *bufio.Reader, func()) { 46 + // catFileBatchCheck opens git cat-file --batch-check in the provided repo and returns a stdin pipe, a stdout reader and cancel function 47 + func catFileBatchCheck(ctx context.Context, repoPath string) (WriteCloserError, *bufio.Reader, func()) { 48 48 batchStdinReader, batchStdinWriter := io.Pipe() 49 49 batchStdoutReader, batchStdoutWriter := io.Pipe() 50 50 ctx, ctxCancel := context.WithCancel(ctx) ··· 93 93 return batchStdinWriter, batchReader, cancel 94 94 } 95 95 96 - // CatFileBatch opens git cat-file --batch in the provided repo and returns a stdin pipe, a stdout reader and cancel function 97 - func CatFileBatch(ctx context.Context, repoPath string) (WriteCloserError, *bufio.Reader, func()) { 96 + // catFileBatch opens git cat-file --batch in the provided repo and returns a stdin pipe, a stdout reader and cancel function 97 + func catFileBatch(ctx context.Context, repoPath string) (WriteCloserError, *bufio.Reader, func()) { 98 98 // We often want to feed the commits in order into cat-file --batch, followed by their trees and sub trees as necessary. 99 99 // so let's create a batch stdin and stdout 100 100 batchStdinReader, batchStdinWriter := io.Pipe()
+11 -4
modules/git/blob.go
··· 28 28 // DataAsync gets a ReadCloser for the contents of a blob without reading it all. 29 29 // Calling the Close function on the result will discard all unread output. 30 30 func (b *Blob) DataAsync() (io.ReadCloser, error) { 31 - wr, rd, cancel := b.repo.CatFileBatch(b.repo.Ctx) 31 + wr, rd, cancel, err := b.repo.CatFileBatch(b.repo.Ctx) 32 + if err != nil { 33 + return nil, err 34 + } 32 35 33 - _, err := wr.Write([]byte(b.ID.String() + "\n")) 36 + _, err = wr.Write([]byte(b.ID.String() + "\n")) 34 37 if err != nil { 35 38 cancel() 36 39 return nil, err ··· 66 69 return b.size 67 70 } 68 71 69 - wr, rd, cancel := b.repo.CatFileBatchCheck(b.repo.Ctx) 72 + wr, rd, cancel, err := b.repo.CatFileBatchCheck(b.repo.Ctx) 73 + if err != nil { 74 + log.Debug("error whilst reading size for %s in %s. Error: %v", b.ID.String(), b.repo.Path, err) 75 + return 0 76 + } 70 77 defer cancel() 71 - _, err := wr.Write([]byte(b.ID.String() + "\n")) 78 + _, err = wr.Write([]byte(b.ID.String() + "\n")) 72 79 if err != nil { 73 80 log.Debug("error whilst reading size for %s in %s. Error: %v", b.ID.String(), b.repo.Path, err) 74 81 return 0
+4 -1
modules/git/commit_info.go
··· 129 129 return nil, err 130 130 } 131 131 132 - batchStdinWriter, batchReader, cancel := commit.repo.CatFileBatch(ctx) 132 + batchStdinWriter, batchReader, cancel, err := commit.repo.CatFileBatch(ctx) 133 + if err != nil { 134 + return nil, err 135 + } 133 136 defer cancel() 134 137 135 138 commitsMap := map[string]*Commit{}
+4 -1
modules/git/pipeline/lfs.go
··· 67 67 68 68 // Next feed the commits in order into cat-file --batch, followed by their trees and sub trees as necessary. 69 69 // so let's create a batch stdin and stdout 70 - batchStdinWriter, batchReader, cancel := repo.CatFileBatch(repo.Ctx) 70 + batchStdinWriter, batchReader, cancel, err := repo.CatFileBatch(repo.Ctx) 71 + if err != nil { 72 + return nil, err 73 + } 71 74 defer cancel() 72 75 73 76 // We'll use a scanner for the revList because it's simpler than a bufio.Reader
+52 -44
modules/git/repo_base.go
··· 21 21 22 22 gpgSettings *GPGSettings 23 23 24 - batchInUse bool 25 - batchCancel context.CancelFunc 26 - batchReader *bufio.Reader 27 - batchWriter WriteCloserError 24 + batchInUse bool 25 + batch *Batch 28 26 29 - checkInUse bool 30 - checkCancel context.CancelFunc 31 - checkReader *bufio.Reader 32 - checkWriter WriteCloserError 27 + checkInUse bool 28 + check *Batch 33 29 34 30 Ctx context.Context 35 31 LastCommitCache *LastCommitCache ··· 51 47 return nil, errors.New("no such file or directory") 52 48 } 53 49 54 - // 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! 55 - if err := EnsureValidGitRepository(ctx, repoPath); err != nil { 56 - return nil, err 57 - } 58 - 59 - repo := &Repository{ 50 + return &Repository{ 60 51 Path: repoPath, 61 52 tagCache: newObjectCache(), 62 53 Ctx: ctx, 63 - } 64 - 65 - repo.batchWriter, repo.batchReader, repo.batchCancel = CatFileBatch(ctx, repoPath) 66 - repo.checkWriter, repo.checkReader, repo.checkCancel = CatFileBatchCheck(ctx, repoPath) 67 - 68 - return repo, nil 54 + }, nil 69 55 } 70 56 71 57 // CatFileBatch obtains a CatFileBatch for this repository 72 - func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bufio.Reader, func()) { 73 - if repo.batchCancel == nil || repo.batchInUse { 74 - log.Debug("Opening temporary cat file batch for: %s", repo.Path) 75 - return CatFileBatch(ctx, repo.Path) 58 + func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bufio.Reader, func(), error) { 59 + if repo.batch == nil { 60 + var err error 61 + repo.batch, err = repo.NewBatch(ctx) 62 + if err != nil { 63 + return nil, nil, nil, err 64 + } 76 65 } 77 - repo.batchInUse = true 78 - return repo.batchWriter, repo.batchReader, func() { 79 - repo.batchInUse = false 66 + 67 + if !repo.batchInUse { 68 + repo.batchInUse = true 69 + return repo.batch.Writer, repo.batch.Reader, func() { 70 + repo.batchInUse = false 71 + }, nil 80 72 } 73 + 74 + log.Debug("Opening temporary cat file batch for: %s", repo.Path) 75 + tempBatch, err := repo.NewBatch(ctx) 76 + if err != nil { 77 + return nil, nil, nil, err 78 + } 79 + return tempBatch.Writer, tempBatch.Reader, tempBatch.Close, nil 81 80 } 82 81 83 82 // CatFileBatchCheck obtains a CatFileBatchCheck for this repository 84 - func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError, *bufio.Reader, func()) { 85 - if repo.checkCancel == nil || repo.checkInUse { 86 - log.Debug("Opening temporary cat file batch-check for: %s", repo.Path) 87 - return CatFileBatchCheck(ctx, repo.Path) 83 + func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError, *bufio.Reader, func(), error) { 84 + if repo.check == nil { 85 + var err error 86 + repo.check, err = repo.NewBatchCheck(ctx) 87 + if err != nil { 88 + return nil, nil, nil, err 89 + } 88 90 } 89 - repo.checkInUse = true 90 - return repo.checkWriter, repo.checkReader, func() { 91 - repo.checkInUse = false 91 + 92 + if !repo.checkInUse { 93 + repo.checkInUse = true 94 + return repo.check.Writer, repo.check.Reader, func() { 95 + repo.checkInUse = false 96 + }, nil 92 97 } 98 + 99 + log.Debug("Opening temporary cat file batch-check for: %s", repo.Path) 100 + tempBatchCheck, err := repo.NewBatchCheck(ctx) 101 + if err != nil { 102 + return nil, nil, nil, err 103 + } 104 + return tempBatchCheck.Writer, tempBatchCheck.Reader, tempBatchCheck.Close, nil 93 105 } 94 106 95 107 func (repo *Repository) Close() error { 96 108 if repo == nil { 97 109 return nil 98 110 } 99 - if repo.batchCancel != nil { 100 - repo.batchCancel() 101 - repo.batchReader = nil 102 - repo.batchWriter = nil 103 - repo.batchCancel = nil 111 + if repo.batch != nil { 112 + repo.batch.Close() 113 + repo.batch = nil 104 114 repo.batchInUse = false 105 115 } 106 - if repo.checkCancel != nil { 107 - repo.checkCancel() 108 - repo.checkCancel = nil 109 - repo.checkReader = nil 110 - repo.checkWriter = nil 116 + if repo.check != nil { 117 + repo.check.Close() 118 + repo.check = nil 111 119 repo.checkInUse = false 112 120 } 113 121 repo.LastCommitCache = nil
+163
modules/git/repo_base_test.go
··· 1 + // Copyright 2024 The Forgejo Authors. All rights reserved. 2 + // SPDX-License-Identifier: GPL-3.0-or-later 3 + 4 + package git 5 + 6 + import ( 7 + "bufio" 8 + "context" 9 + "testing" 10 + 11 + "github.com/stretchr/testify/assert" 12 + "github.com/stretchr/testify/require" 13 + ) 14 + 15 + // This unit test relies on the implementation detail of CatFileBatch. 16 + func TestCatFileBatch(t *testing.T) { 17 + ctx, cancel := context.WithCancel(context.Background()) 18 + defer cancel() 19 + 20 + repo, err := OpenRepository(ctx, "./tests/repos/repo1_bare") 21 + require.NoError(t, err) 22 + defer repo.Close() 23 + 24 + var wr WriteCloserError 25 + var r *bufio.Reader 26 + var cancel1 func() 27 + t.Run("Request cat file batch", func(t *testing.T) { 28 + assert.Nil(t, repo.batch) 29 + wr, r, cancel1, err = repo.CatFileBatch(ctx) 30 + require.NoError(t, err) 31 + assert.NotNil(t, repo.batch) 32 + assert.Equal(t, repo.batch.Writer, wr) 33 + assert.True(t, repo.batchInUse) 34 + }) 35 + 36 + t.Run("Request temporary cat file batch", func(t *testing.T) { 37 + wr, r, cancel, err := repo.CatFileBatch(ctx) 38 + require.NoError(t, err) 39 + assert.NotEqual(t, repo.batch.Writer, wr) 40 + 41 + t.Run("Check temporary cat file batch", func(t *testing.T) { 42 + _, err = wr.Write([]byte("95bb4d39648ee7e325106df01a621c530863a653" + "\n")) 43 + require.NoError(t, err) 44 + 45 + sha, typ, size, err := ReadBatchLine(r) 46 + require.NoError(t, err) 47 + assert.Equal(t, "commit", typ) 48 + assert.EqualValues(t, []byte("95bb4d39648ee7e325106df01a621c530863a653"), sha) 49 + assert.EqualValues(t, 144, size) 50 + }) 51 + 52 + cancel() 53 + assert.True(t, repo.batchInUse) 54 + }) 55 + 56 + t.Run("Check cached cat file batch", func(t *testing.T) { 57 + _, err = wr.Write([]byte("95bb4d39648ee7e325106df01a621c530863a653" + "\n")) 58 + require.NoError(t, err) 59 + 60 + sha, typ, size, err := ReadBatchLine(r) 61 + require.NoError(t, err) 62 + assert.Equal(t, "commit", typ) 63 + assert.EqualValues(t, []byte("95bb4d39648ee7e325106df01a621c530863a653"), sha) 64 + assert.EqualValues(t, 144, size) 65 + }) 66 + 67 + t.Run("Cancel cached cat file batch", func(t *testing.T) { 68 + cancel1() 69 + assert.False(t, repo.batchInUse) 70 + assert.NotNil(t, repo.batch) 71 + }) 72 + 73 + t.Run("Request cached cat file batch", func(t *testing.T) { 74 + wr, _, _, err := repo.CatFileBatch(ctx) 75 + require.NoError(t, err) 76 + assert.NotNil(t, repo.batch) 77 + assert.Equal(t, repo.batch.Writer, wr) 78 + assert.True(t, repo.batchInUse) 79 + 80 + t.Run("Close git repo", func(t *testing.T) { 81 + require.NoError(t, repo.Close()) 82 + assert.Nil(t, repo.batch) 83 + }) 84 + 85 + _, err = wr.Write([]byte("95bb4d39648ee7e325106df01a621c530863a653" + "\n")) 86 + require.Error(t, err) 87 + }) 88 + } 89 + 90 + // This unit test relies on the implementation detail of CatFileBatchCheck. 91 + func TestCatFileBatchCheck(t *testing.T) { 92 + ctx, cancel := context.WithCancel(context.Background()) 93 + defer cancel() 94 + 95 + repo, err := OpenRepository(ctx, "./tests/repos/repo1_bare") 96 + require.NoError(t, err) 97 + defer repo.Close() 98 + 99 + var wr WriteCloserError 100 + var r *bufio.Reader 101 + var cancel1 func() 102 + t.Run("Request cat file batch check", func(t *testing.T) { 103 + assert.Nil(t, repo.check) 104 + wr, r, cancel1, err = repo.CatFileBatchCheck(ctx) 105 + require.NoError(t, err) 106 + assert.NotNil(t, repo.check) 107 + assert.Equal(t, repo.check.Writer, wr) 108 + assert.True(t, repo.checkInUse) 109 + }) 110 + 111 + t.Run("Request temporary cat file batch check", func(t *testing.T) { 112 + wr, r, cancel, err := repo.CatFileBatchCheck(ctx) 113 + require.NoError(t, err) 114 + assert.NotEqual(t, repo.check.Writer, wr) 115 + 116 + t.Run("Check temporary cat file batch check", func(t *testing.T) { 117 + _, err = wr.Write([]byte("test" + "\n")) 118 + require.NoError(t, err) 119 + 120 + sha, typ, size, err := ReadBatchLine(r) 121 + require.NoError(t, err) 122 + assert.Equal(t, "tag", typ) 123 + assert.EqualValues(t, []byte("3ad28a9149a2864384548f3d17ed7f38014c9e8a"), sha) 124 + assert.EqualValues(t, 807, size) 125 + }) 126 + 127 + cancel() 128 + assert.True(t, repo.checkInUse) 129 + }) 130 + 131 + t.Run("Check cached cat file batch check", func(t *testing.T) { 132 + _, err = wr.Write([]byte("test" + "\n")) 133 + require.NoError(t, err) 134 + 135 + sha, typ, size, err := ReadBatchLine(r) 136 + require.NoError(t, err) 137 + assert.Equal(t, "tag", typ) 138 + assert.EqualValues(t, []byte("3ad28a9149a2864384548f3d17ed7f38014c9e8a"), sha) 139 + assert.EqualValues(t, 807, size) 140 + }) 141 + 142 + t.Run("Cancel cached cat file batch check", func(t *testing.T) { 143 + cancel1() 144 + assert.False(t, repo.checkInUse) 145 + assert.NotNil(t, repo.check) 146 + }) 147 + 148 + t.Run("Request cached cat file batch check", func(t *testing.T) { 149 + wr, _, _, err := repo.CatFileBatchCheck(ctx) 150 + require.NoError(t, err) 151 + assert.NotNil(t, repo.check) 152 + assert.Equal(t, repo.check.Writer, wr) 153 + assert.True(t, repo.checkInUse) 154 + 155 + t.Run("Close git repo", func(t *testing.T) { 156 + require.NoError(t, repo.Close()) 157 + assert.Nil(t, repo.check) 158 + }) 159 + 160 + _, err = wr.Write([]byte("test" + "\n")) 161 + require.Error(t, err) 162 + }) 163 + }
+12 -4
modules/git/repo_branch.go
··· 169 169 return false 170 170 } 171 171 172 - wr, rd, cancel := repo.CatFileBatchCheck(repo.Ctx) 172 + wr, rd, cancel, err := repo.CatFileBatchCheck(repo.Ctx) 173 + if err != nil { 174 + log.Debug("Error writing to CatFileBatchCheck %v", err) 175 + return false 176 + } 173 177 defer cancel() 174 - _, err := wr.Write([]byte(name + "\n")) 178 + _, err = wr.Write([]byte(name + "\n")) 175 179 if err != nil { 176 180 log.Debug("Error writing to CatFileBatchCheck %v", err) 177 181 return false ··· 186 190 return false 187 191 } 188 192 189 - wr, rd, cancel := repo.CatFileBatchCheck(repo.Ctx) 193 + wr, rd, cancel, err := repo.CatFileBatchCheck(repo.Ctx) 194 + if err != nil { 195 + log.Debug("Error writing to CatFileBatchCheck %v", err) 196 + return false 197 + } 190 198 defer cancel() 191 - _, err := wr.Write([]byte(name + "\n")) 199 + _, err = wr.Write([]byte(name + "\n")) 192 200 if err != nil { 193 201 log.Debug("Error writing to CatFileBatchCheck %v", err) 194 202 return false
+17 -4
modules/git/repo_commit.go
··· 536 536 537 537 // GetRefCommitID returns the last commit ID string of given reference (branch or tag). 538 538 func (repo *Repository) GetRefCommitID(name string) (string, error) { 539 - wr, rd, cancel := repo.CatFileBatchCheck(repo.Ctx) 539 + wr, rd, cancel, err := repo.CatFileBatchCheck(repo.Ctx) 540 + if err != nil { 541 + return "", err 542 + } 540 543 defer cancel() 541 - _, err := wr.Write([]byte(name + "\n")) 544 + _, err = wr.Write([]byte(name + "\n")) 542 545 if err != nil { 543 546 return "", err 544 547 } ··· 564 567 565 568 // IsCommitExist returns true if given commit exists in current repository. 566 569 func (repo *Repository) IsCommitExist(name string) bool { 570 + if err := ensureValidGitRepository(repo.Ctx, repo.Path); err != nil { 571 + log.Error("IsCommitExist: %v", err) 572 + return false 573 + } 567 574 _, _, err := NewCommand(repo.Ctx, "cat-file", "-e").AddDynamicArguments(name).RunStdString(&RunOpts{Dir: repo.Path}) 568 575 return err == nil 569 576 } 570 577 571 578 func (repo *Repository) getCommit(id ObjectID) (*Commit, error) { 572 - wr, rd, cancel := repo.CatFileBatch(repo.Ctx) 579 + wr, rd, cancel, err := repo.CatFileBatch(repo.Ctx) 580 + if err != nil { 581 + return nil, err 582 + } 573 583 defer cancel() 574 584 575 585 _, _ = wr.Write([]byte(id.String() + "\n")) ··· 646 656 } 647 657 } 648 658 649 - wr, rd, cancel := repo.CatFileBatchCheck(repo.Ctx) 659 + wr, rd, cancel, err := repo.CatFileBatchCheck(repo.Ctx) 660 + if err != nil { 661 + return nil, err 662 + } 650 663 defer cancel() 651 664 _, err = wr.Write([]byte(commitID + "\n")) 652 665 if err != nil {
+4 -1
modules/git/repo_language_stats.go
··· 60 60 func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, error) { 61 61 // We will feed the commit IDs in order into cat-file --batch, followed by blobs as necessary. 62 62 // so let's create a batch stdin and stdout 63 - batchStdinWriter, batchReader, cancel := repo.CatFileBatch(repo.Ctx) 63 + batchStdinWriter, batchReader, cancel, err := repo.CatFileBatch(repo.Ctx) 64 + if err != nil { 65 + return nil, err 66 + } 64 67 defer cancel() 65 68 66 69 writeID := func(id string) error {
+9 -3
modules/git/repo_tag.go
··· 257 257 258 258 // GetTagType gets the type of the tag, either commit (simple) or tag (annotated) 259 259 func (repo *Repository) GetTagType(id ObjectID) (string, error) { 260 - wr, rd, cancel := repo.CatFileBatchCheck(repo.Ctx) 260 + wr, rd, cancel, err := repo.CatFileBatchCheck(repo.Ctx) 261 + if err != nil { 262 + return "", err 263 + } 261 264 defer cancel() 262 - _, err := wr.Write([]byte(id.String() + "\n")) 265 + _, err = wr.Write([]byte(id.String() + "\n")) 263 266 if err != nil { 264 267 return "", err 265 268 } ··· 315 318 } 316 319 317 320 // The tag is an annotated tag with a message. 318 - wr, rd, cancel := repo.CatFileBatch(repo.Ctx) 321 + wr, rd, cancel, err := repo.CatFileBatch(repo.Ctx) 322 + if err != nil { 323 + return nil, err 324 + } 319 325 defer cancel() 320 326 321 327 if _, err := wr.Write([]byte(tagID.String() + "\n")); err != nil {
+4 -1
modules/git/repo_tree.go
··· 68 68 } 69 69 70 70 func (repo *Repository) getTree(id ObjectID) (*Tree, error) { 71 - wr, rd, cancel := repo.CatFileBatch(repo.Ctx) 71 + wr, rd, cancel, err := repo.CatFileBatch(repo.Ctx) 72 + if err != nil { 73 + return nil, err 74 + } 72 75 defer cancel() 73 76 74 77 _, _ = wr.Write([]byte(id.String() + "\n"))
+4 -1
modules/git/tree.go
··· 41 41 } 42 42 43 43 if t.repo != nil { 44 - wr, rd, cancel := t.repo.CatFileBatch(t.repo.Ctx) 44 + wr, rd, cancel, err := t.repo.CatFileBatch(t.repo.Ctx) 45 + if err != nil { 46 + return nil, err 47 + } 45 48 defer cancel() 46 49 47 50 _, _ = wr.Write([]byte(t.ID.String() + "\n"))
+6 -2
modules/git/tree_entry.go
··· 47 47 return te.size 48 48 } 49 49 50 - wr, rd, cancel := te.ptree.repo.CatFileBatchCheck(te.ptree.repo.Ctx) 50 + wr, rd, cancel, err := te.ptree.repo.CatFileBatchCheck(te.ptree.repo.Ctx) 51 + if err != nil { 52 + log.Debug("error whilst reading size for %s in %s. Error: %v", te.ID.String(), te.ptree.repo.Path, err) 53 + return 0 54 + } 51 55 defer cancel() 52 - _, err := wr.Write([]byte(te.ID.String() + "\n")) 56 + _, err = wr.Write([]byte(te.ID.String() + "\n")) 53 57 if err != nil { 54 58 log.Debug("error whilst reading size for %s in %s. Error: %v", te.ID.String(), te.ptree.repo.Path, err) 55 59 return 0
+11 -9
modules/indexer/code/bleve/bleve.go
··· 16 16 "code.gitea.io/gitea/modules/analyze" 17 17 "code.gitea.io/gitea/modules/charset" 18 18 "code.gitea.io/gitea/modules/git" 19 + "code.gitea.io/gitea/modules/gitrepo" 19 20 "code.gitea.io/gitea/modules/indexer/code/internal" 20 21 indexer_internal "code.gitea.io/gitea/modules/indexer/internal" 21 22 inner_bleve "code.gitea.io/gitea/modules/indexer/internal/bleve" 22 - "code.gitea.io/gitea/modules/log" 23 23 "code.gitea.io/gitea/modules/setting" 24 24 "code.gitea.io/gitea/modules/timeutil" 25 25 "code.gitea.io/gitea/modules/typesniffer" ··· 193 193 func (b *Indexer) Index(ctx context.Context, repo *repo_model.Repository, sha string, changes *internal.RepoChanges) error { 194 194 batch := inner_bleve.NewFlushingBatch(b.inner.Indexer, maxBatchSize) 195 195 if len(changes.Updates) > 0 { 196 - // 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! 197 - if err := git.EnsureValidGitRepository(ctx, repo.RepoPath()); err != nil { 198 - log.Error("Unable to open git repo: %s for %-v: %v", repo.RepoPath(), repo, err) 196 + r, err := gitrepo.OpenRepository(ctx, repo) 197 + if err != nil { 199 198 return err 200 199 } 201 - 202 - batchWriter, batchReader, cancel := git.CatFileBatch(ctx, repo.RepoPath()) 203 - defer cancel() 200 + defer r.Close() 201 + gitBatch, err := r.NewBatch(ctx) 202 + if err != nil { 203 + return err 204 + } 205 + defer gitBatch.Close() 204 206 205 207 for _, update := range changes.Updates { 206 - if err := b.addUpdate(ctx, batchWriter, batchReader, sha, update, repo, batch); err != nil { 208 + if err := b.addUpdate(ctx, gitBatch.Writer, gitBatch.Reader, sha, update, repo, batch); err != nil { 207 209 return err 208 210 } 209 211 } 210 - cancel() 212 + gitBatch.Close() 211 213 } 212 214 for _, filename := range changes.RemovedFilenames { 213 215 if err := b.addDelete(filename, repo, batch); err != nil {
+11 -9
modules/indexer/code/elasticsearch/elasticsearch.go
··· 15 15 "code.gitea.io/gitea/modules/analyze" 16 16 "code.gitea.io/gitea/modules/charset" 17 17 "code.gitea.io/gitea/modules/git" 18 + "code.gitea.io/gitea/modules/gitrepo" 18 19 "code.gitea.io/gitea/modules/indexer/code/internal" 19 20 indexer_internal "code.gitea.io/gitea/modules/indexer/internal" 20 21 inner_elasticsearch "code.gitea.io/gitea/modules/indexer/internal/elasticsearch" 21 22 "code.gitea.io/gitea/modules/json" 22 - "code.gitea.io/gitea/modules/log" 23 23 "code.gitea.io/gitea/modules/setting" 24 24 "code.gitea.io/gitea/modules/timeutil" 25 25 "code.gitea.io/gitea/modules/typesniffer" ··· 154 154 func (b *Indexer) Index(ctx context.Context, repo *repo_model.Repository, sha string, changes *internal.RepoChanges) error { 155 155 reqs := make([]elastic.BulkableRequest, 0) 156 156 if len(changes.Updates) > 0 { 157 - // 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! 158 - if err := git.EnsureValidGitRepository(ctx, repo.RepoPath()); err != nil { 159 - log.Error("Unable to open git repo: %s for %-v: %v", repo.RepoPath(), repo, err) 157 + r, err := gitrepo.OpenRepository(ctx, repo) 158 + if err != nil { 160 159 return err 161 160 } 162 - 163 - batchWriter, batchReader, cancel := git.CatFileBatch(ctx, repo.RepoPath()) 164 - defer cancel() 161 + defer r.Close() 162 + batch, err := r.NewBatch(ctx) 163 + if err != nil { 164 + return err 165 + } 166 + defer batch.Close() 165 167 166 168 for _, update := range changes.Updates { 167 - updateReqs, err := b.addUpdate(ctx, batchWriter, batchReader, sha, update, repo) 169 + updateReqs, err := b.addUpdate(ctx, batch.Writer, batch.Reader, sha, update, repo) 168 170 if err != nil { 169 171 return err 170 172 } ··· 172 174 reqs = append(reqs, updateReqs...) 173 175 } 174 176 } 175 - cancel() 177 + batch.Close() 176 178 } 177 179 178 180 for _, filename := range changes.RemovedFilenames {
+3 -1
routers/api/v1/repo/hook_test.go
··· 19 19 20 20 ctx, _ := contexttest.MockAPIContext(t, "user2/repo1/wiki/_pages") 21 21 ctx.SetParams(":id", "1") 22 + contexttest.LoadUser(t, ctx, 2) 22 23 contexttest.LoadRepo(t, ctx, 1) 24 + contexttest.LoadGitRepo(t, ctx) 25 + defer ctx.Repo.GitRepo.Close() 23 26 contexttest.LoadRepoCommit(t, ctx) 24 - contexttest.LoadUser(t, ctx, 2) 25 27 TestHook(ctx) 26 28 assert.EqualValues(t, http.StatusNoContent, ctx.Resp.Status()) 27 29
+2 -10
routers/web/repo/editor_test.go
··· 6 6 import ( 7 7 "testing" 8 8 9 + repo_model "code.gitea.io/gitea/models/repo" 9 10 "code.gitea.io/gitea/models/unittest" 10 11 "code.gitea.io/gitea/modules/git" 11 12 "code.gitea.io/gitea/modules/gitrepo" ··· 45 46 ctx, _ := contexttest.MockContext(t, "user2/repo1") 46 47 ctx.SetParams(":id", "1") 47 48 contexttest.LoadRepo(t, ctx, 1) 48 - contexttest.LoadRepoCommit(t, ctx) 49 49 contexttest.LoadUser(t, ctx, 2) 50 50 contexttest.LoadGitRepo(t, ctx) 51 51 defer ctx.Repo.GitRepo.Close() ··· 57 57 58 58 func TestGetClosestParentWithFiles(t *testing.T) { 59 59 unittest.PrepareTestEnv(t) 60 - ctx, _ := contexttest.MockContext(t, "user2/repo1") 61 - ctx.SetParams(":id", "1") 62 - contexttest.LoadRepo(t, ctx, 1) 63 - contexttest.LoadRepoCommit(t, ctx) 64 - contexttest.LoadUser(t, ctx, 2) 65 - contexttest.LoadGitRepo(t, ctx) 66 - defer ctx.Repo.GitRepo.Close() 67 - 68 - repo := ctx.Repo.Repository 60 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 69 61 branch := repo.DefaultBranch 70 62 gitRepo, _ := gitrepo.OpenRepository(git.DefaultContext, repo) 71 63 defer gitRepo.Close()
+19 -8
services/contexttest/context_tests.go
··· 136 136 assert.FailNow(t, "context is not *context.Context or *context.APIContext") 137 137 } 138 138 139 - gitRepo, err := gitrepo.OpenRepository(ctx, repo.Repository) 140 - require.NoError(t, err) 141 - defer gitRepo.Close() 142 - branch, err := gitRepo.GetHEADBranch() 139 + if repo.GitRepo == nil { 140 + assert.FailNow(t, "must call LoadGitRepo") 141 + } 142 + 143 + branch, err := repo.GitRepo.GetHEADBranch() 143 144 require.NoError(t, err) 144 145 assert.NotNil(t, branch) 145 146 if branch != nil { 146 - repo.Commit, err = gitRepo.GetBranchCommit(branch.Name) 147 + repo.Commit, err = repo.GitRepo.GetBranchCommit(branch.Name) 147 148 require.NoError(t, err) 148 149 } 149 150 } ··· 176 177 177 178 // LoadGitRepo load a git repo into a test context. Requires that ctx.Repo has 178 179 // already been populated. 179 - func LoadGitRepo(t *testing.T, ctx *context.Context) { 180 - require.NoError(t, ctx.Repo.Repository.LoadOwner(ctx)) 180 + func LoadGitRepo(t *testing.T, ctx gocontext.Context) { 181 + var repo *context.Repository 182 + switch ctx := ctx.(type) { 183 + case *context.Context: 184 + repo = ctx.Repo 185 + case *context.APIContext: 186 + repo = ctx.Repo 187 + default: 188 + assert.FailNow(t, "context is not *context.Context or *context.APIContext") 189 + } 190 + 191 + require.NoError(t, repo.Repository.LoadOwner(ctx)) 181 192 var err error 182 - ctx.Repo.GitRepo, err = gitrepo.OpenRepository(ctx, ctx.Repo.Repository) 193 + repo.GitRepo, err = gitrepo.OpenRepository(ctx, repo.Repository) 183 194 require.NoError(t, err) 184 195 } 185 196
+27 -75
services/repository/files/content_test.go
··· 6 6 import ( 7 7 "testing" 8 8 9 + "code.gitea.io/gitea/models/db" 10 + repo_model "code.gitea.io/gitea/models/repo" 9 11 "code.gitea.io/gitea/models/unittest" 10 12 "code.gitea.io/gitea/modules/gitrepo" 11 13 api "code.gitea.io/gitea/modules/structs" 12 - "code.gitea.io/gitea/services/contexttest" 13 14 14 15 _ "code.gitea.io/gitea/models/actions" 15 16 ··· 53 54 54 55 func TestGetContents(t *testing.T) { 55 56 unittest.PrepareTestEnv(t) 56 - ctx, _ := contexttest.MockContext(t, "user2/repo1") 57 - ctx.SetParams(":id", "1") 58 - contexttest.LoadRepo(t, ctx, 1) 59 - contexttest.LoadRepoCommit(t, ctx) 60 - contexttest.LoadUser(t, ctx, 2) 61 - contexttest.LoadGitRepo(t, ctx) 62 - defer ctx.Repo.GitRepo.Close() 57 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 63 58 64 59 treePath := "README.md" 65 - ref := ctx.Repo.Repository.DefaultBranch 60 + ref := repo.DefaultBranch 66 61 67 62 expectedContentsResponse := getExpectedReadmeContentsResponse() 68 63 69 64 t.Run("Get README.md contents with GetContents(ctx, )", func(t *testing.T) { 70 - fileContentResponse, err := GetContents(ctx, ctx.Repo.Repository, treePath, ref, false) 65 + fileContentResponse, err := GetContents(db.DefaultContext, repo, treePath, ref, false) 71 66 assert.EqualValues(t, expectedContentsResponse, fileContentResponse) 72 67 require.NoError(t, err) 73 68 }) 74 69 75 70 t.Run("Get README.md contents with ref as empty string (should then use the repo's default branch) with GetContents(ctx, )", func(t *testing.T) { 76 - fileContentResponse, err := GetContents(ctx, ctx.Repo.Repository, treePath, "", false) 71 + fileContentResponse, err := GetContents(db.DefaultContext, repo, treePath, "", false) 77 72 assert.EqualValues(t, expectedContentsResponse, fileContentResponse) 78 73 require.NoError(t, err) 79 74 }) ··· 81 76 82 77 func TestGetContentsOrListForDir(t *testing.T) { 83 78 unittest.PrepareTestEnv(t) 84 - ctx, _ := contexttest.MockContext(t, "user2/repo1") 85 - ctx.SetParams(":id", "1") 86 - contexttest.LoadRepo(t, ctx, 1) 87 - contexttest.LoadRepoCommit(t, ctx) 88 - contexttest.LoadUser(t, ctx, 2) 89 - contexttest.LoadGitRepo(t, ctx) 90 - defer ctx.Repo.GitRepo.Close() 79 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 91 80 92 81 treePath := "" // root dir 93 - ref := ctx.Repo.Repository.DefaultBranch 82 + ref := repo.DefaultBranch 94 83 95 84 readmeContentsResponse := getExpectedReadmeContentsResponse() 96 85 // because will be in a list, doesn't have encoding and content ··· 102 91 } 103 92 104 93 t.Run("Get root dir contents with GetContentsOrList(ctx, )", func(t *testing.T) { 105 - fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref) 94 + fileContentResponse, err := GetContentsOrList(db.DefaultContext, repo, treePath, ref) 106 95 assert.EqualValues(t, expectedContentsListResponse, fileContentResponse) 107 96 require.NoError(t, err) 108 97 }) 109 98 110 99 t.Run("Get root dir contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList(ctx, )", func(t *testing.T) { 111 - fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, "") 100 + fileContentResponse, err := GetContentsOrList(db.DefaultContext, repo, treePath, "") 112 101 assert.EqualValues(t, expectedContentsListResponse, fileContentResponse) 113 102 require.NoError(t, err) 114 103 }) ··· 116 105 117 106 func TestGetContentsOrListForFile(t *testing.T) { 118 107 unittest.PrepareTestEnv(t) 119 - ctx, _ := contexttest.MockContext(t, "user2/repo1") 120 - ctx.SetParams(":id", "1") 121 - contexttest.LoadRepo(t, ctx, 1) 122 - contexttest.LoadRepoCommit(t, ctx) 123 - contexttest.LoadUser(t, ctx, 2) 124 - contexttest.LoadGitRepo(t, ctx) 125 - defer ctx.Repo.GitRepo.Close() 108 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 126 109 127 110 treePath := "README.md" 128 - ref := ctx.Repo.Repository.DefaultBranch 111 + ref := repo.DefaultBranch 129 112 130 113 expectedContentsResponse := getExpectedReadmeContentsResponse() 131 114 132 115 t.Run("Get README.md contents with GetContentsOrList(ctx, )", func(t *testing.T) { 133 - fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref) 116 + fileContentResponse, err := GetContentsOrList(db.DefaultContext, repo, treePath, ref) 134 117 assert.EqualValues(t, expectedContentsResponse, fileContentResponse) 135 118 require.NoError(t, err) 136 119 }) 137 120 138 121 t.Run("Get README.md contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList(ctx, )", func(t *testing.T) { 139 - fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, "") 122 + fileContentResponse, err := GetContentsOrList(db.DefaultContext, repo, treePath, "") 140 123 assert.EqualValues(t, expectedContentsResponse, fileContentResponse) 141 124 require.NoError(t, err) 142 125 }) ··· 144 127 145 128 func TestGetContentsErrors(t *testing.T) { 146 129 unittest.PrepareTestEnv(t) 147 - ctx, _ := contexttest.MockContext(t, "user2/repo1") 148 - ctx.SetParams(":id", "1") 149 - contexttest.LoadRepo(t, ctx, 1) 150 - contexttest.LoadRepoCommit(t, ctx) 151 - contexttest.LoadUser(t, ctx, 2) 152 - contexttest.LoadGitRepo(t, ctx) 153 - defer ctx.Repo.GitRepo.Close() 130 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 154 131 155 - repo := ctx.Repo.Repository 156 132 treePath := "README.md" 157 133 ref := repo.DefaultBranch 158 134 159 135 t.Run("bad treePath", func(t *testing.T) { 160 136 badTreePath := "bad/tree.md" 161 - fileContentResponse, err := GetContents(ctx, repo, badTreePath, ref, false) 137 + fileContentResponse, err := GetContents(db.DefaultContext, repo, badTreePath, ref, false) 162 138 require.EqualError(t, err, "object does not exist [id: , rel_path: bad]") 163 139 assert.Nil(t, fileContentResponse) 164 140 }) 165 141 166 142 t.Run("bad ref", func(t *testing.T) { 167 143 badRef := "bad_ref" 168 - fileContentResponse, err := GetContents(ctx, repo, treePath, badRef, false) 144 + fileContentResponse, err := GetContents(db.DefaultContext, repo, treePath, badRef, false) 169 145 require.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]") 170 146 assert.Nil(t, fileContentResponse) 171 147 }) ··· 173 149 174 150 func TestGetContentsOrListErrors(t *testing.T) { 175 151 unittest.PrepareTestEnv(t) 176 - ctx, _ := contexttest.MockContext(t, "user2/repo1") 177 - ctx.SetParams(":id", "1") 178 - contexttest.LoadRepo(t, ctx, 1) 179 - contexttest.LoadRepoCommit(t, ctx) 180 - contexttest.LoadUser(t, ctx, 2) 181 - contexttest.LoadGitRepo(t, ctx) 182 - defer ctx.Repo.GitRepo.Close() 152 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 183 153 184 - repo := ctx.Repo.Repository 185 154 treePath := "README.md" 186 155 ref := repo.DefaultBranch 187 156 188 157 t.Run("bad treePath", func(t *testing.T) { 189 158 badTreePath := "bad/tree.md" 190 - fileContentResponse, err := GetContentsOrList(ctx, repo, badTreePath, ref) 159 + fileContentResponse, err := GetContentsOrList(db.DefaultContext, repo, badTreePath, ref) 191 160 require.EqualError(t, err, "object does not exist [id: , rel_path: bad]") 192 161 assert.Nil(t, fileContentResponse) 193 162 }) 194 163 195 164 t.Run("bad ref", func(t *testing.T) { 196 165 badRef := "bad_ref" 197 - fileContentResponse, err := GetContentsOrList(ctx, repo, treePath, badRef) 166 + fileContentResponse, err := GetContentsOrList(db.DefaultContext, repo, treePath, badRef) 198 167 require.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]") 199 168 assert.Nil(t, fileContentResponse) 200 169 }) ··· 202 171 203 172 func TestGetContentsOrListOfEmptyRepos(t *testing.T) { 204 173 unittest.PrepareTestEnv(t) 205 - ctx, _ := contexttest.MockContext(t, "user30/empty") 206 - ctx.SetParams(":id", "52") 207 - contexttest.LoadRepo(t, ctx, 52) 208 - contexttest.LoadUser(t, ctx, 30) 209 - contexttest.LoadGitRepo(t, ctx) 210 - defer ctx.Repo.GitRepo.Close() 211 - 212 - repo := ctx.Repo.Repository 174 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 52}) 213 175 214 176 t.Run("empty repo", func(t *testing.T) { 215 - contents, err := GetContentsOrList(ctx, repo, "", "") 177 + contents, err := GetContentsOrList(db.DefaultContext, repo, "", "") 216 178 require.NoError(t, err) 217 179 assert.Empty(t, contents) 218 180 }) ··· 220 182 221 183 func TestGetBlobBySHA(t *testing.T) { 222 184 unittest.PrepareTestEnv(t) 223 - ctx, _ := contexttest.MockContext(t, "user2/repo1") 224 - contexttest.LoadRepo(t, ctx, 1) 225 - contexttest.LoadRepoCommit(t, ctx) 226 - contexttest.LoadUser(t, ctx, 2) 227 - contexttest.LoadGitRepo(t, ctx) 228 - defer ctx.Repo.GitRepo.Close() 185 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 229 186 230 - sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d" 231 - ctx.SetParams(":id", "1") 232 - ctx.SetParams(":sha", sha) 233 - 234 - gitRepo, err := gitrepo.OpenRepository(ctx, ctx.Repo.Repository) 235 - if err != nil { 236 - t.Fail() 237 - } 187 + gitRepo, err := gitrepo.OpenRepository(db.DefaultContext, repo) 188 + require.NoError(t, err) 189 + defer gitRepo.Close() 238 190 239 - gbr, err := GetBlobBySHA(ctx, ctx.Repo.Repository, gitRepo, ctx.Params(":sha")) 191 + gbr, err := GetBlobBySHA(db.DefaultContext, repo, gitRepo, "65f1bf27bc3bf70f64657658635e66094edbcb4d") 240 192 expectedGBR := &api.GitBlobResponse{ 241 193 Content: "dHJlZSAyYTJmMWQ0NjcwNzI4YTJlMTAwNDllMzQ1YmQ3YTI3NjQ2OGJlYWI2CmF1dGhvciB1c2VyMSA8YWRkcmVzczFAZXhhbXBsZS5jb20+IDE0ODk5NTY0NzkgLTA0MDAKY29tbWl0dGVyIEV0aGFuIEtvZW5pZyA8ZXRoYW50a29lbmlnQGdtYWlsLmNvbT4gMTQ4OTk1NjQ3OSAtMDQwMAoKSW5pdGlhbCBjb21taXQK", 242 194 Encoding: "base64",
+6 -13
services/repository/files/diff_test.go
··· 6 6 import ( 7 7 "testing" 8 8 9 + "code.gitea.io/gitea/models/db" 9 10 repo_model "code.gitea.io/gitea/models/repo" 10 11 "code.gitea.io/gitea/models/unittest" 11 12 "code.gitea.io/gitea/modules/json" ··· 21 22 ctx, _ := contexttest.MockContext(t, "user2/repo1") 22 23 ctx.SetParams(":id", "1") 23 24 contexttest.LoadRepo(t, ctx, 1) 24 - contexttest.LoadRepoCommit(t, ctx) 25 25 contexttest.LoadUser(t, ctx, 2) 26 26 contexttest.LoadGitRepo(t, ctx) 27 27 defer ctx.Repo.GitRepo.Close() ··· 140 140 141 141 func TestGetDiffPreviewErrors(t *testing.T) { 142 142 unittest.PrepareTestEnv(t) 143 - ctx, _ := contexttest.MockContext(t, "user2/repo1") 144 - ctx.SetParams(":id", "1") 145 - contexttest.LoadRepo(t, ctx, 1) 146 - contexttest.LoadRepoCommit(t, ctx) 147 - contexttest.LoadUser(t, ctx, 2) 148 - contexttest.LoadGitRepo(t, ctx) 149 - defer ctx.Repo.GitRepo.Close() 150 - 151 - branch := ctx.Repo.Repository.DefaultBranch 143 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 144 + branch := repo.DefaultBranch 152 145 treePath := "README.md" 153 146 content := "# repo1\n\nDescription for repo1\nthis is a new line" 154 147 155 148 t.Run("empty repo", func(t *testing.T) { 156 - diff, err := GetDiffPreview(ctx, &repo_model.Repository{}, branch, treePath, content) 149 + diff, err := GetDiffPreview(db.DefaultContext, &repo_model.Repository{}, branch, treePath, content) 157 150 assert.Nil(t, diff) 158 151 assert.EqualError(t, err, "repository does not exist [id: 0, uid: 0, owner_name: , name: ]") 159 152 }) 160 153 161 154 t.Run("bad branch", func(t *testing.T) { 162 155 badBranch := "bad_branch" 163 - diff, err := GetDiffPreview(ctx, ctx.Repo.Repository, badBranch, treePath, content) 156 + diff, err := GetDiffPreview(db.DefaultContext, repo, badBranch, treePath, content) 164 157 assert.Nil(t, diff) 165 158 assert.EqualError(t, err, "branch does not exist [name: "+badBranch+"]") 166 159 }) 167 160 168 161 t.Run("empty treePath", func(t *testing.T) { 169 - diff, err := GetDiffPreview(ctx, ctx.Repo.Repository, branch, "", content) 162 + diff, err := GetDiffPreview(db.DefaultContext, repo, branch, "", content) 170 163 assert.Nil(t, diff) 171 164 assert.EqualError(t, err, "path is invalid [path: ]") 172 165 })
+5 -11
services/repository/files/file_test.go
··· 6 6 import ( 7 7 "testing" 8 8 9 + "code.gitea.io/gitea/models/db" 10 + repo_model "code.gitea.io/gitea/models/repo" 9 11 "code.gitea.io/gitea/models/unittest" 10 12 "code.gitea.io/gitea/modules/gitrepo" 11 13 "code.gitea.io/gitea/modules/setting" 12 14 api "code.gitea.io/gitea/modules/structs" 13 - "code.gitea.io/gitea/services/contexttest" 14 15 15 16 "github.com/stretchr/testify/assert" 16 17 "github.com/stretchr/testify/require" ··· 99 100 100 101 func TestGetFileResponseFromCommit(t *testing.T) { 101 102 unittest.PrepareTestEnv(t) 102 - ctx, _ := contexttest.MockContext(t, "user2/repo1") 103 - ctx.SetParams(":id", "1") 104 - contexttest.LoadRepo(t, ctx, 1) 105 - contexttest.LoadRepoCommit(t, ctx) 106 - contexttest.LoadUser(t, ctx, 2) 107 - contexttest.LoadGitRepo(t, ctx) 108 - defer ctx.Repo.GitRepo.Close() 109 103 110 - repo := ctx.Repo.Repository 104 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 111 105 branch := repo.DefaultBranch 112 106 treePath := "README.md" 113 - gitRepo, _ := gitrepo.OpenRepository(ctx, repo) 107 + gitRepo, _ := gitrepo.OpenRepository(db.DefaultContext, repo) 114 108 defer gitRepo.Close() 115 109 commit, _ := gitRepo.GetBranchCommit(branch) 116 110 expectedFileResponse := getExpectedFileResponse() 117 111 118 - fileResponse, err := GetFileResponseFromCommit(ctx, repo, commit, branch, treePath) 112 + fileResponse, err := GetFileResponseFromCommit(db.DefaultContext, repo, commit, branch, treePath) 119 113 require.NoError(t, err) 120 114 assert.EqualValues(t, expectedFileResponse, fileResponse) 121 115 }
-1
services/repository/files/tree_test.go
··· 18 18 unittest.PrepareTestEnv(t) 19 19 ctx, _ := contexttest.MockContext(t, "user2/repo1") 20 20 contexttest.LoadRepo(t, ctx, 1) 21 - contexttest.LoadRepoCommit(t, ctx) 22 21 contexttest.LoadUser(t, ctx, 2) 23 22 contexttest.LoadGitRepo(t, ctx) 24 23 defer ctx.Repo.GitRepo.Close()
+6 -1
tests/integration/repo_view_test.go
··· 52 52 ctx, _ := contexttest.MockContext(t, "user1/readmetest") 53 53 ctx.SetParams(":id", fmt.Sprint(repo.ID)) 54 54 contexttest.LoadRepo(t, ctx, repo.ID) 55 + contexttest.LoadGitRepo(t, ctx) 55 56 contexttest.LoadRepoCommit(t, ctx) 56 - return ctx, f 57 + 58 + return ctx, func() { 59 + f() 60 + ctx.Repo.GitRepo.Close() 61 + } 57 62 } 58 63 59 64 func TestRepoView_FindReadme(t *testing.T) {
+15 -69
tests/integration/repofiles_change_test.go
··· 12 12 13 13 repo_model "code.gitea.io/gitea/models/repo" 14 14 "code.gitea.io/gitea/models/unittest" 15 + user_model "code.gitea.io/gitea/models/user" 15 16 "code.gitea.io/gitea/modules/git" 16 17 "code.gitea.io/gitea/modules/gitrepo" 17 18 "code.gitea.io/gitea/modules/setting" 18 19 api "code.gitea.io/gitea/modules/structs" 19 - "code.gitea.io/gitea/services/contexttest" 20 20 files_service "code.gitea.io/gitea/services/repository/files" 21 21 22 22 "github.com/stretchr/testify/assert" ··· 247 247 func TestChangeRepoFilesForCreate(t *testing.T) { 248 248 // setup 249 249 onGiteaRun(t, func(t *testing.T, u *url.URL) { 250 - ctx, _ := contexttest.MockContext(t, "user2/repo1") 251 - ctx.SetParams(":id", "1") 252 - contexttest.LoadRepo(t, ctx, 1) 253 - contexttest.LoadRepoCommit(t, ctx) 254 - contexttest.LoadUser(t, ctx, 2) 255 - contexttest.LoadGitRepo(t, ctx) 256 - defer ctx.Repo.GitRepo.Close() 257 - 258 - repo := ctx.Repo.Repository 259 - doer := ctx.Doer 250 + doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 251 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 260 252 opts := getCreateRepoFilesOptions(repo) 261 253 262 254 // test ··· 284 276 func TestChangeRepoFilesForUpdate(t *testing.T) { 285 277 // setup 286 278 onGiteaRun(t, func(t *testing.T, u *url.URL) { 287 - ctx, _ := contexttest.MockContext(t, "user2/repo1") 288 - ctx.SetParams(":id", "1") 289 - contexttest.LoadRepo(t, ctx, 1) 290 - contexttest.LoadRepoCommit(t, ctx) 291 - contexttest.LoadUser(t, ctx, 2) 292 - contexttest.LoadGitRepo(t, ctx) 293 - defer ctx.Repo.GitRepo.Close() 294 - 295 - repo := ctx.Repo.Repository 296 - doer := ctx.Doer 279 + doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 280 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 297 281 opts := getUpdateRepoFilesOptions(repo) 298 282 299 283 // test ··· 318 302 func TestChangeRepoFilesForUpdateWithFileMove(t *testing.T) { 319 303 // setup 320 304 onGiteaRun(t, func(t *testing.T, u *url.URL) { 321 - ctx, _ := contexttest.MockContext(t, "user2/repo1") 322 - ctx.SetParams(":id", "1") 323 - contexttest.LoadRepo(t, ctx, 1) 324 - contexttest.LoadRepoCommit(t, ctx) 325 - contexttest.LoadUser(t, ctx, 2) 326 - contexttest.LoadGitRepo(t, ctx) 327 - defer ctx.Repo.GitRepo.Close() 328 - 329 - repo := ctx.Repo.Repository 330 - doer := ctx.Doer 305 + doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 306 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 331 307 opts := getUpdateRepoFilesOptions(repo) 332 308 opts.Files[0].FromTreePath = "README.md" 333 309 opts.Files[0].TreePath = "README_new.md" // new file name, README_new.md ··· 369 345 func TestChangeRepoFilesWithoutBranchNames(t *testing.T) { 370 346 // setup 371 347 onGiteaRun(t, func(t *testing.T, u *url.URL) { 372 - ctx, _ := contexttest.MockContext(t, "user2/repo1") 373 - ctx.SetParams(":id", "1") 374 - contexttest.LoadRepo(t, ctx, 1) 375 - contexttest.LoadRepoCommit(t, ctx) 376 - contexttest.LoadUser(t, ctx, 2) 377 - contexttest.LoadGitRepo(t, ctx) 378 - defer ctx.Repo.GitRepo.Close() 379 - 380 - repo := ctx.Repo.Repository 381 - doer := ctx.Doer 348 + doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 349 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 382 350 opts := getUpdateRepoFilesOptions(repo) 383 351 opts.OldBranch = "" 384 352 opts.NewBranch = "" ··· 405 373 func testDeleteRepoFiles(t *testing.T, u *url.URL) { 406 374 // setup 407 375 unittest.PrepareTestEnv(t) 408 - ctx, _ := contexttest.MockContext(t, "user2/repo1") 409 - ctx.SetParams(":id", "1") 410 - contexttest.LoadRepo(t, ctx, 1) 411 - contexttest.LoadRepoCommit(t, ctx) 412 - contexttest.LoadUser(t, ctx, 2) 413 - contexttest.LoadGitRepo(t, ctx) 414 - defer ctx.Repo.GitRepo.Close() 415 - repo := ctx.Repo.Repository 416 - doer := ctx.Doer 376 + doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 377 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 417 378 opts := getDeleteRepoFilesOptions(repo) 418 379 419 380 t.Run("Delete README.md file", func(t *testing.T) { ··· 444 405 func testDeleteRepoFilesWithoutBranchNames(t *testing.T, u *url.URL) { 445 406 // setup 446 407 unittest.PrepareTestEnv(t) 447 - ctx, _ := contexttest.MockContext(t, "user2/repo1") 448 - ctx.SetParams(":id", "1") 449 - contexttest.LoadRepo(t, ctx, 1) 450 - contexttest.LoadRepoCommit(t, ctx) 451 - contexttest.LoadUser(t, ctx, 2) 452 - contexttest.LoadGitRepo(t, ctx) 453 - defer ctx.Repo.GitRepo.Close() 408 + doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 409 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 454 410 455 - repo := ctx.Repo.Repository 456 - doer := ctx.Doer 457 411 opts := getDeleteRepoFilesOptions(repo) 458 412 opts.OldBranch = "" 459 413 opts.NewBranch = "" ··· 474 428 func TestChangeRepoFilesErrors(t *testing.T) { 475 429 // setup 476 430 onGiteaRun(t, func(t *testing.T, u *url.URL) { 477 - ctx, _ := contexttest.MockContext(t, "user2/repo1") 478 - ctx.SetParams(":id", "1") 479 - contexttest.LoadRepo(t, ctx, 1) 480 - contexttest.LoadRepoCommit(t, ctx) 481 - contexttest.LoadUser(t, ctx, 2) 482 - contexttest.LoadGitRepo(t, ctx) 483 - defer ctx.Repo.GitRepo.Close() 484 - 485 - repo := ctx.Repo.Repository 486 - doer := ctx.Doer 431 + doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 432 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 487 433 488 434 t.Run("bad branch", func(t *testing.T) { 489 435 opts := getUpdateRepoFilesOptions(repo)