@@ -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