Skip to content

Commit 2f39de1

Browse files
committed
adjust batch argument
1 parent a8e8890 commit 2f39de1

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

modules/git/batch.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ func (b *batchCatFileWithCheck) getBatch() *batchCatFile {
6060
if b.batch != nil {
6161
return b.batch
6262
}
63-
b.batch = newCatFileBatch(b.ctx, b.repoPath, "--batch")
63+
b.batch = newCatFileBatch(b.ctx, b.repoPath, BatchArg)
6464
return b.batch
6565
}
6666

6767
func (b *batchCatFileWithCheck) getBatchCheck() *batchCatFile {
6868
if b.batchCheck != nil {
6969
return b.batchCheck
7070
}
71-
b.batchCheck = newCatFileBatch(b.ctx, b.repoPath, "--batch-check")
71+
b.batchCheck = newCatFileBatch(b.ctx, b.repoPath, BatchCheckArg)
7272
return b.batchCheck
7373
}
7474

@@ -125,7 +125,7 @@ func (b *batchCommandCatFile) getBatch() *batchCatFile {
125125
if b.batch != nil {
126126
return b.batch
127127
}
128-
b.batch = newCatFileBatch(b.ctx, b.repoPath, "--batch-command")
128+
b.batch = newCatFileBatch(b.ctx, b.repoPath, BatchCommandArg)
129129
return b.batch
130130
}
131131

modules/git/batch_reader.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import (
77
"bufio"
88
"bytes"
99
"context"
10+
"fmt"
1011
"io"
1112
"math"
1213
"strconv"
1314
"strings"
1415

1516
"code.gitea.io/gitea/modules/git/gitcmd"
16-
"code.gitea.io/gitea/modules/git/internal"
1717
"code.gitea.io/gitea/modules/log"
1818

1919
"github.com/djherbis/buffer"
@@ -41,11 +41,21 @@ func ensureValidGitRepository(ctx context.Context, repoPath string) error {
4141
return nil
4242
}
4343

44+
const (
45+
BatchArg = iota
46+
BatchCheckArg
47+
BatchCommandArg
48+
)
49+
50+
func isValidBatchArg(arg int) bool {
51+
return arg == BatchArg || arg == BatchCheckArg || arg == BatchCommandArg
52+
}
53+
4454
// newCatFileBatch opens git cat-file --batch in the provided repo and returns a stdin pipe, a stdout reader and cancel function
4555
// batchArg is the argument to pass to cat-file --batch, it could be "--batch", "--batch-command" or "--batch-check".
46-
func newCatFileBatch(ctx context.Context, repoPath, batchArg string) *batchCatFile {
47-
if batchArg != "--batch" && batchArg != "--batch-command" && batchArg != "--batch-check" {
48-
panic("invalid batchArg: " + batchArg)
56+
func newCatFileBatch(ctx context.Context, repoPath string, batchArg int) *batchCatFile {
57+
if !isValidBatchArg(batchArg) {
58+
panic(fmt.Sprintf("invalid batchArg: %d", batchArg))
4959
}
5060

5161
// We often want to feed the commits in order into cat-file --batch, followed by their trees and sub trees as necessary.
@@ -69,8 +79,16 @@ func newCatFileBatch(ctx context.Context, repoPath, batchArg string) *batchCatFi
6979

7080
go func() {
7181
stderr := strings.Builder{}
72-
err := gitcmd.NewCommand("cat-file").
73-
AddArguments(internal.CmdArg(batchArg)).
82+
cmd := gitcmd.NewCommand("cat-file")
83+
switch batchArg {
84+
case BatchArg:
85+
cmd.AddArguments("--batch")
86+
case BatchCheckArg:
87+
cmd.AddArguments("--batch-check")
88+
case BatchCommandArg:
89+
cmd.AddArguments("--batch-command")
90+
}
91+
err := cmd.
7492
WithDir(repoPath).
7593
WithStdin(batchStdinReader).
7694
WithStdout(batchStdoutWriter).

0 commit comments

Comments
 (0)