Skip to content

Commit 5dde21a

Browse files
committed
refactor
1 parent 872918f commit 5dde21a

File tree

17 files changed

+117
-61
lines changed

17 files changed

+117
-61
lines changed

modules/git/batch.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
)
1010

1111
type CatFileBatch interface {
12-
Write([]byte) (int, error) // query object contents
13-
Reader() *bufio.Reader
12+
QueryContent([]byte) (int, error)
13+
ContentReader() *bufio.Reader
1414

15-
WriteCheck([]byte) (int, error) // query object info
16-
CheckReader() *bufio.Reader
15+
QueryInfo([]byte) (int, error)
16+
InfoReader() *bufio.Reader
1717

1818
Close()
1919
}

modules/git/batch_command.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ func (b *catFileBatchCommand) getBatch() *catFileBatchCommunicator {
3636
return b.batch
3737
}
3838

39-
func (b *catFileBatchCommand) Write(bs []byte) (int, error) {
39+
func (b *catFileBatchCommand) QueryContent(bs []byte) (int, error) {
4040
return b.getBatch().writer.Write(append([]byte("contents "), bs...))
4141
}
4242

43-
func (b *catFileBatchCommand) WriteCheck(bs []byte) (int, error) {
43+
func (b *catFileBatchCommand) QueryInfo(bs []byte) (int, error) {
4444
return b.getBatch().writer.Write(append([]byte("info "), bs...))
4545
}
4646

47-
func (b *catFileBatchCommand) Reader() *bufio.Reader {
47+
func (b *catFileBatchCommand) ContentReader() *bufio.Reader {
4848
return b.getBatch().reader
4949
}
5050

51-
func (b *catFileBatchCommand) CheckReader() *bufio.Reader {
51+
func (b *catFileBatchCommand) InfoReader() *bufio.Reader {
5252
return b.getBatch().reader
5353
}
5454

modules/git/batch_legacy.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,19 @@ func (b *catFileBatchLegacy) getBatchCheck() *catFileBatchCommunicator {
5252
return b.batchCheck
5353
}
5454

55-
func (b *catFileBatchLegacy) Write(bs []byte) (int, error) {
55+
func (b *catFileBatchLegacy) QueryContent(bs []byte) (int, error) {
5656
return b.getBatch().writer.Write(bs)
5757
}
5858

59-
func (b *catFileBatchLegacy) WriteCheck(bs []byte) (int, error) {
59+
func (b *catFileBatchLegacy) QueryInfo(bs []byte) (int, error) {
6060
return b.getBatchCheck().writer.Write(bs)
6161
}
6262

63-
func (b *catFileBatchLegacy) Reader() *bufio.Reader {
63+
func (b *catFileBatchLegacy) ContentReader() *bufio.Reader {
6464
return b.getBatch().reader
6565
}
6666

67-
func (b *catFileBatchLegacy) CheckReader() *bufio.Reader {
67+
func (b *catFileBatchLegacy) InfoReader() *bufio.Reader {
6868
return b.getBatchCheck().reader
6969
}
7070

modules/git/batch_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package git
5+
6+
import (
7+
"io"
8+
"path/filepath"
9+
"testing"
10+
11+
"code.gitea.io/gitea/modules/test"
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
func TestCatFileBatch(t *testing.T) {
17+
defer test.MockVariableValue(&DefaultFeatures().SupportCatFileBatchCommand)()
18+
DefaultFeatures().SupportCatFileBatchCommand = false
19+
t.Run("LegacyCheck", testCatFileBatch)
20+
DefaultFeatures().SupportCatFileBatchCommand = true
21+
t.Run("BatchCommand", testCatFileBatch)
22+
}
23+
24+
func testCatFileBatch(t *testing.T) {
25+
batch, err := NewBatch(t.Context(), filepath.Join(testReposDir, "repo1_bare"))
26+
require.NoError(t, err)
27+
defer batch.Close()
28+
29+
t.Run("QueryInfo", func(t *testing.T) {
30+
_, err = batch.QueryInfo([]byte("e2129701f1a4d54dc44f03c93bca0a2aec7c5449\n"))
31+
require.NoError(t, err)
32+
33+
sha, typ, sz, err := ReadBatchLine(batch.InfoReader())
34+
require.NoError(t, err)
35+
assert.Equal(t, "e2129701f1a4d54dc44f03c93bca0a2aec7c5449", string(sha))
36+
assert.Equal(t, "blob", typ)
37+
assert.EqualValues(t, 6, sz)
38+
})
39+
40+
t.Run("QueryContent", func(t *testing.T) {
41+
_, err = batch.QueryContent([]byte("e2129701f1a4d54dc44f03c93bca0a2aec7c5449\n"))
42+
require.NoError(t, err)
43+
44+
sha, typ, sz, err := ReadBatchLine(batch.ContentReader())
45+
require.NoError(t, err)
46+
assert.Equal(t, "e2129701f1a4d54dc44f03c93bca0a2aec7c5449", string(sha))
47+
assert.Equal(t, "blob", typ)
48+
assert.EqualValues(t, 6, sz)
49+
50+
content, err := io.ReadAll(io.LimitReader(batch.ContentReader(), sz))
51+
require.NoError(t, err)
52+
require.Equal(t, "file1\n", string(content))
53+
})
54+
}

modules/git/blob_nogogit.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ func (b *Blob) DataAsync() (io.ReadCloser, error) {
3131
return nil, err
3232
}
3333

34-
_, err = batch.Write([]byte(b.ID.String() + "\n"))
34+
_, err = batch.QueryContent([]byte(b.ID.String() + "\n"))
3535
if err != nil {
3636
cancel()
3737
return nil, err
3838
}
39-
rd := batch.Reader()
39+
rd := batch.ContentReader()
4040
_, _, size, err := ReadBatchLine(rd)
4141
if err != nil {
4242
cancel()
@@ -74,12 +74,12 @@ func (b *Blob) Size() int64 {
7474
return 0
7575
}
7676
defer cancel()
77-
_, err = batch.WriteCheck([]byte(b.ID.String() + "\n"))
77+
_, err = batch.QueryInfo([]byte(b.ID.String() + "\n"))
7878
if err != nil {
7979
log.Debug("error whilst reading size for %s in %s. Error: %v", b.ID.String(), b.repo.Path, err)
8080
return 0
8181
}
82-
_, _, b.size, err = ReadBatchLine(batch.CheckReader())
82+
_, _, b.size, err = ReadBatchLine(batch.InfoReader())
8383
if err != nil {
8484
log.Debug("error whilst reading size for %s in %s. Error: %v", b.ID.String(), b.repo.Path, err)
8585
return 0

modules/git/git.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,22 +180,24 @@ func InitFull() (err error) {
180180
return syncGitConfig(context.Background())
181181
}
182182

183+
// RunGitTests helps to init the git module and run tests.
184+
// FIXME: GIT-PACKAGE-DEPENDENCY: the dependency is not right, setting.Git.HomePath is initialized in this package but used in gitcmd package
183185
func RunGitTests(m interface{ Run() int }) {
184-
fatalf := func(format string, args ...any) {
186+
fatalf := func(exitCode int, format string, args ...any) {
185187
_, _ = fmt.Fprintf(os.Stderr, format, args...)
186-
os.Exit(1)
188+
os.Exit(exitCode)
187189
}
188190
gitHomePath, cleanup, err := tempdir.OsTempDir("gitea-test").MkdirTempRandom("git-home")
189191
if err != nil {
190-
fatalf("unable to create temp dir: %w", err)
192+
fatalf(1, "unable to create temp dir: %w", err)
191193
}
192194
defer cleanup()
193195

194196
setting.Git.HomePath = gitHomePath
195197
if err = InitFull(); err != nil {
196-
fatalf("failed to call Init: %w", err)
198+
fatalf(1, "failed to call Init: %w", err)
197199
}
198200
if exitCode := m.Run(); exitCode != 0 {
199-
fatalf("run test failed, ExitCode=%d", exitCode)
201+
fatalf(exitCode, "run test failed, ExitCode=%d", exitCode)
200202
}
201203
}

modules/git/gitcmd/command_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
func TestMain(m *testing.M) {
18-
// FIXME: the package dependency is not right
18+
// FIXME: GIT-PACKAGE-DEPENDENCY: the dependency is not right.
1919
// "setting.Git.HomePath" is initialized in "git" package but really used in "gitcmd" package
2020
gitHomePath, cleanup, err := tempdir.OsTempDir("gitea-test").MkdirTempRandom("git-home")
2121
if err != nil {

modules/git/languagestats/language_stats_nogogit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ func GetLanguageStats(repo *git.Repository, commitID string) (map[string]int64,
2929
defer cancel()
3030

3131
writeID := func(id string) error {
32-
_, err := batch.Write([]byte(id + "\n"))
32+
_, err := batch.QueryContent([]byte(id + "\n"))
3333
return err
3434
}
3535

3636
if err := writeID(commitID); err != nil {
3737
return nil, err
3838
}
39-
batchReader := batch.Reader()
39+
batchReader := batch.ContentReader()
4040
shaBytes, typ, size, err := git.ReadBatchLine(batchReader)
4141
if typ != "commit" {
4242
log.Debug("Unable to get commit for: %s. Err: %v", commitID, err)

modules/git/pipeline/lfs_nogogit.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ func FindLFSFile(repo *git.Repository, objectID git.ObjectID) ([]*LFSResult, err
6161
fnameBuf := make([]byte, 4096)
6262
modeBuf := make([]byte, 40)
6363
workingShaBuf := make([]byte, objectID.Type().FullLength()/2)
64-
batchReader := batch.Reader()
64+
batchReader := batch.ContentReader()
6565

6666
for scan.Scan() {
6767
// Get the next commit ID
6868
commitID := scan.Bytes()
6969

7070
// push the commit to the cat-file --batch process
71-
_, err := batch.Write(append(commitID, '\n'))
71+
_, err := batch.QueryContent(append(commitID, '\n'))
7272
if err != nil {
7373
return nil, err
7474
}
@@ -90,7 +90,7 @@ func FindLFSFile(repo *git.Repository, objectID git.ObjectID) ([]*LFSResult, err
9090
if err != nil {
9191
return nil, err
9292
}
93-
_, err = batch.Write([]byte(id + "\n"))
93+
_, err = batch.QueryContent([]byte(id + "\n"))
9494
if err != nil {
9595
return nil, err
9696
}
@@ -105,7 +105,7 @@ func FindLFSFile(repo *git.Repository, objectID git.ObjectID) ([]*LFSResult, err
105105
return nil, err
106106
}
107107

108-
if _, err := batch.Write([]byte(curCommit.Tree.ID.String() + "\n")); err != nil {
108+
if _, err := batch.QueryContent([]byte(curCommit.Tree.ID.String() + "\n")); err != nil {
109109
return nil, err
110110
}
111111
curPath = ""
@@ -137,7 +137,7 @@ func FindLFSFile(repo *git.Repository, objectID git.ObjectID) ([]*LFSResult, err
137137
return nil, err
138138
}
139139
if len(trees) > 0 {
140-
_, err := batch.Write(append(trees[len(trees)-1], '\n'))
140+
_, err := batch.QueryContent(append(trees[len(trees)-1], '\n'))
141141
if err != nil {
142142
return nil, err
143143
}

modules/git/repo_branch_nogogit.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ func (repo *Repository) IsObjectExist(name string) bool {
2929
return false
3030
}
3131
defer cancel()
32-
_, err = batch.WriteCheck([]byte(name + "\n"))
32+
_, err = batch.QueryInfo([]byte(name + "\n"))
3333
if err != nil {
3434
log.Debug("Error writing to CatFileBatchCheck %v", err)
3535
return false
3636
}
37-
sha, _, _, err := ReadBatchLine(batch.CheckReader())
37+
sha, _, _, err := ReadBatchLine(batch.InfoReader())
3838
return err == nil && bytes.HasPrefix(sha, []byte(strings.TrimSpace(name)))
3939
}
4040

@@ -50,12 +50,12 @@ func (repo *Repository) IsReferenceExist(name string) bool {
5050
return false
5151
}
5252
defer cancel()
53-
_, err = batch.WriteCheck([]byte(name + "\n"))
53+
_, err = batch.QueryInfo([]byte(name + "\n"))
5454
if err != nil {
5555
log.Debug("Error writing to CatFileBatchCheck %v", err)
5656
return false
5757
}
58-
_, _, _, err = ReadBatchLine(batch.CheckReader())
58+
_, _, _, err = ReadBatchLine(batch.InfoReader())
5959
return err == nil
6060
}
6161

0 commit comments

Comments
 (0)