Skip to content

Commit 4c6d91a

Browse files
committed
refactor
1 parent a885f07 commit 4c6d91a

File tree

8 files changed

+176
-180
lines changed

8 files changed

+176
-180
lines changed

modules/git/batch.go

Lines changed: 6 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -8,151 +8,17 @@ import (
88
"context"
99
)
1010

11-
type batchCatFile struct {
12-
cancel context.CancelFunc
13-
Reader *bufio.Reader
14-
Writer WriteCloserError
15-
}
16-
17-
func (b *batchCatFile) Close() {
18-
if b.cancel != nil {
19-
b.cancel()
20-
b.Reader = nil
21-
b.Writer = nil
22-
b.cancel = nil
23-
}
24-
}
25-
26-
type Batch interface {
27-
Write([]byte) (int, error)
28-
WriteCheck([]byte) (int, error)
11+
type CatFileBatch interface {
12+
Write([]byte) (int, error) // query object contents
2913
Reader() *bufio.Reader
30-
CheckReader() *bufio.Reader
31-
Close()
32-
}
33-
34-
// batchCatFileWithCheck implements the Batch interface using the "cat-file --batch" command and "cat-file --batch-check" command
35-
// ref: https://git-scm.com/docs/git-cat-file#Documentation/git-cat-file.txt---batch
36-
// To align with --batch-command, we creates the two commands both at the same time if git version is lower than 2.36
37-
type batchCatFileWithCheck struct {
38-
ctx context.Context
39-
repoPath string
40-
batch *batchCatFile
41-
batchCheck *batchCatFile
42-
}
43-
44-
var _ Batch = &batchCatFileWithCheck{}
45-
46-
// newBatchCatFileWithCheck creates a new batch and a new batch check for the given repository, the Close must be invoked before release the batch
47-
func newBatchCatFileWithCheck(ctx context.Context, repoPath string) (*batchCatFileWithCheck, error) {
48-
// 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!
49-
if err := ensureValidGitRepository(ctx, repoPath); err != nil {
50-
return nil, err
51-
}
52-
53-
return &batchCatFileWithCheck{
54-
ctx: ctx,
55-
repoPath: repoPath,
56-
}, nil
57-
}
58-
59-
func (b *batchCatFileWithCheck) getBatch() *batchCatFile {
60-
if b.batch != nil {
61-
return b.batch
62-
}
63-
b.batch = newCatFileBatch(b.ctx, b.repoPath, BatchArg)
64-
return b.batch
65-
}
66-
67-
func (b *batchCatFileWithCheck) getBatchCheck() *batchCatFile {
68-
if b.batchCheck != nil {
69-
return b.batchCheck
70-
}
71-
b.batchCheck = newCatFileBatch(b.ctx, b.repoPath, BatchCheckArg)
72-
return b.batchCheck
73-
}
74-
75-
func (b *batchCatFileWithCheck) Write(bs []byte) (int, error) {
76-
return b.getBatch().Writer.Write(bs)
77-
}
78-
79-
func (b *batchCatFileWithCheck) WriteCheck(bs []byte) (int, error) {
80-
return b.getBatchCheck().Writer.Write(bs)
81-
}
82-
83-
func (b *batchCatFileWithCheck) Reader() *bufio.Reader {
84-
return b.getBatch().Reader
85-
}
8614

87-
func (b *batchCatFileWithCheck) CheckReader() *bufio.Reader {
88-
return b.getBatchCheck().Reader
89-
}
90-
91-
func (b *batchCatFileWithCheck) Close() {
92-
if b.batch != nil {
93-
b.batch.Close()
94-
b.batch = nil
95-
}
96-
if b.batchCheck != nil {
97-
b.batchCheck.Close()
98-
b.batchCheck = nil
99-
}
100-
}
101-
102-
// batchCommandCatFile implements the Batch interface using the "cat-file --batch-command" command
103-
// ref: https://git-scm.com/docs/git-cat-file#Documentation/git-cat-file.txt---batch-command
104-
type batchCommandCatFile struct {
105-
ctx context.Context
106-
repoPath string
107-
batch *batchCatFile
108-
}
109-
110-
var _ Batch = &batchCommandCatFile{}
111-
112-
func newBatchCommandCatFile(ctx context.Context, repoPath string) (*batchCommandCatFile, error) {
113-
// 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!
114-
if err := ensureValidGitRepository(ctx, repoPath); err != nil {
115-
return nil, err
116-
}
117-
118-
return &batchCommandCatFile{
119-
ctx: ctx,
120-
repoPath: repoPath,
121-
}, nil
122-
}
123-
124-
func (b *batchCommandCatFile) getBatch() *batchCatFile {
125-
if b.batch != nil {
126-
return b.batch
127-
}
128-
b.batch = newCatFileBatch(b.ctx, b.repoPath, BatchCommandArg)
129-
return b.batch
130-
}
131-
132-
func (b *batchCommandCatFile) Write(bs []byte) (int, error) {
133-
return b.getBatch().Writer.Write(append([]byte("contents "), bs...))
134-
}
135-
136-
func (b *batchCommandCatFile) WriteCheck(bs []byte) (int, error) {
137-
return b.getBatch().Writer.Write(append([]byte("info "), bs...))
138-
}
139-
140-
func (b *batchCommandCatFile) Reader() *bufio.Reader {
141-
return b.getBatch().Reader
142-
}
143-
144-
func (b *batchCommandCatFile) CheckReader() *bufio.Reader {
145-
return b.getBatch().Reader
146-
}
15+
WriteCheck([]byte) (int, error) // query object info
16+
CheckReader() *bufio.Reader
14717

148-
func (b *batchCommandCatFile) Close() {
149-
if b.batch != nil {
150-
b.batch.Close()
151-
b.batch = nil
152-
}
18+
Close()
15319
}
15420

155-
func NewBatch(ctx context.Context, repoPath string) (Batch, error) {
21+
func NewBatch(ctx context.Context, repoPath string) (CatFileBatch, error) {
15622
if DefaultFeatures().SupportCatFileBatchCommand {
15723
return newBatchCommandCatFile(ctx, repoPath)
15824
}

modules/git/batch_command.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package git
5+
6+
import (
7+
"bufio"
8+
"context"
9+
10+
"code.gitea.io/gitea/modules/git/gitcmd"
11+
)
12+
13+
// catFileBatchCommand implements the CatFileBatch interface using the "cat-file --batch-command" command
14+
// for git version >= 2.36
15+
// ref: https://git-scm.com/docs/git-cat-file#Documentation/git-cat-file.txt---batch-command
16+
type catFileBatchCommand struct {
17+
ctx context.Context
18+
repoPath string
19+
batch *catFileBatchCommunicator
20+
}
21+
22+
var _ CatFileBatch = (*catFileBatchCommand)(nil)
23+
24+
func newBatchCommandCatFile(ctx context.Context, repoPath string) (*catFileBatchCommand, error) { //nolint:unparam // no error
25+
return &catFileBatchCommand{
26+
ctx: ctx,
27+
repoPath: repoPath,
28+
}, nil
29+
}
30+
31+
func (b *catFileBatchCommand) getBatch() *catFileBatchCommunicator {
32+
if b.batch != nil {
33+
return b.batch
34+
}
35+
b.batch = newCatFileBatch(b.ctx, b.repoPath, gitcmd.NewCommand("cat-file", "--batch-command"))
36+
return b.batch
37+
}
38+
39+
func (b *catFileBatchCommand) Write(bs []byte) (int, error) {
40+
return b.getBatch().writer.Write(append([]byte("contents "), bs...))
41+
}
42+
43+
func (b *catFileBatchCommand) WriteCheck(bs []byte) (int, error) {
44+
return b.getBatch().writer.Write(append([]byte("info "), bs...))
45+
}
46+
47+
func (b *catFileBatchCommand) Reader() *bufio.Reader {
48+
return b.getBatch().reader
49+
}
50+
51+
func (b *catFileBatchCommand) CheckReader() *bufio.Reader {
52+
return b.getBatch().reader
53+
}
54+
55+
func (b *catFileBatchCommand) Close() {
56+
if b.batch != nil {
57+
b.batch.Close()
58+
b.batch = nil
59+
}
60+
}

modules/git/batch_legacy.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package git
5+
6+
import (
7+
"bufio"
8+
"context"
9+
10+
"code.gitea.io/gitea/modules/git/gitcmd"
11+
)
12+
13+
// catFileBatchLegacy implements the CatFileBatch interface using the "cat-file --batch" command and "cat-file --batch-check" command
14+
// for git version < 2.36
15+
// to align with "--batch-command", it creates the two commands for querying object contents and object info separately
16+
// ref: https://git-scm.com/docs/git-cat-file#Documentation/git-cat-file.txt---batch
17+
type catFileBatchLegacy struct {
18+
ctx context.Context
19+
repoPath string
20+
batch *catFileBatchCommunicator
21+
batchCheck *catFileBatchCommunicator
22+
}
23+
24+
var _ CatFileBatch = (*catFileBatchLegacy)(nil)
25+
26+
// newBatchCatFileWithCheck creates a new batch and a new batch check for the given repository, the Close must be invoked before release the batch
27+
func newBatchCatFileWithCheck(ctx context.Context, repoPath string) (*catFileBatchLegacy, error) {
28+
// 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!
29+
if err := ensureValidGitRepository(ctx, repoPath); err != nil {
30+
return nil, err
31+
}
32+
33+
return &catFileBatchLegacy{
34+
ctx: ctx,
35+
repoPath: repoPath,
36+
}, nil
37+
}
38+
39+
func (b *catFileBatchLegacy) getBatch() *catFileBatchCommunicator {
40+
if b.batch != nil {
41+
return b.batch
42+
}
43+
b.batch = newCatFileBatch(b.ctx, b.repoPath, gitcmd.NewCommand("cat-file", "--batch"))
44+
return b.batch
45+
}
46+
47+
func (b *catFileBatchLegacy) getBatchCheck() *catFileBatchCommunicator {
48+
if b.batchCheck != nil {
49+
return b.batchCheck
50+
}
51+
b.batchCheck = newCatFileBatch(b.ctx, b.repoPath, gitcmd.NewCommand("cat-file", "--batch-check"))
52+
return b.batchCheck
53+
}
54+
55+
func (b *catFileBatchLegacy) Write(bs []byte) (int, error) {
56+
return b.getBatch().writer.Write(bs)
57+
}
58+
59+
func (b *catFileBatchLegacy) WriteCheck(bs []byte) (int, error) {
60+
return b.getBatchCheck().writer.Write(bs)
61+
}
62+
63+
func (b *catFileBatchLegacy) Reader() *bufio.Reader {
64+
return b.getBatch().reader
65+
}
66+
67+
func (b *catFileBatchLegacy) CheckReader() *bufio.Reader {
68+
return b.getBatchCheck().reader
69+
}
70+
71+
func (b *catFileBatchLegacy) Close() {
72+
if b.batch != nil {
73+
b.batch.Close()
74+
b.batch = nil
75+
}
76+
if b.batchCheck != nil {
77+
b.batchCheck.Close()
78+
b.batchCheck = nil
79+
}
80+
}

0 commit comments

Comments
 (0)