1
// Copyright 2019 The Gitea Authors. All rights reserved.
2
// SPDX-License-Identifier: MIT
16
"code.gitea.io/gitea/modules/git"
17
"code.gitea.io/gitea/modules/log"
20
// CatFileBatchCheck runs cat-file with --batch-check
21
func CatFileBatchCheck(ctx context.Context, shasToCheckReader *io.PipeReader, catFileCheckWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string) {
23
defer shasToCheckReader.Close()
24
defer catFileCheckWriter.Close()
26
stderr := new(bytes.Buffer)
27
var errbuf strings.Builder
28
cmd := git.NewCommand(ctx, "cat-file", "--batch-check")
29
if err := cmd.Run(&git.RunOpts{
31
Stdin: shasToCheckReader,
32
Stdout: catFileCheckWriter,
35
_ = catFileCheckWriter.CloseWithError(fmt.Errorf("git cat-file --batch-check [%s]: %w - %s", tmpBasePath, err, errbuf.String()))
39
// CatFileBatchCheckAllObjects runs cat-file with --batch-check --batch-all
40
func CatFileBatchCheckAllObjects(ctx context.Context, catFileCheckWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string, errChan chan<- error) {
42
defer catFileCheckWriter.Close()
44
stderr := new(bytes.Buffer)
45
var errbuf strings.Builder
46
cmd := git.NewCommand(ctx, "cat-file", "--batch-check", "--batch-all-objects")
47
if err := cmd.Run(&git.RunOpts{
49
Stdout: catFileCheckWriter,
52
log.Error("git cat-file --batch-check --batch-all-object [%s]: %v - %s", tmpBasePath, err, errbuf.String())
53
err = fmt.Errorf("git cat-file --batch-check --batch-all-object [%s]: %w - %s", tmpBasePath, err, errbuf.String())
54
_ = catFileCheckWriter.CloseWithError(err)
59
// CatFileBatch runs cat-file --batch
60
func CatFileBatch(ctx context.Context, shasToBatchReader *io.PipeReader, catFileBatchWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string) {
62
defer shasToBatchReader.Close()
63
defer catFileBatchWriter.Close()
65
stderr := new(bytes.Buffer)
66
var errbuf strings.Builder
67
if err := git.NewCommand(ctx, "cat-file", "--batch").Run(&git.RunOpts{
69
Stdout: catFileBatchWriter,
70
Stdin: shasToBatchReader,
73
_ = shasToBatchReader.CloseWithError(fmt.Errorf("git rev-list [%s]: %w - %s", tmpBasePath, err, errbuf.String()))
77
// BlobsLessThan1024FromCatFileBatchCheck reads a pipeline from cat-file --batch-check and returns the blobs <1024 in size
78
func BlobsLessThan1024FromCatFileBatchCheck(catFileCheckReader *io.PipeReader, shasToBatchWriter *io.PipeWriter, wg *sync.WaitGroup) {
80
defer catFileCheckReader.Close()
81
scanner := bufio.NewScanner(catFileCheckReader)
83
_ = shasToBatchWriter.CloseWithError(scanner.Err())
86
line := scanner.Text()
90
fields := strings.Split(line, " ")
91
if len(fields) < 3 || fields[1] != "blob" {
94
size, _ := strconv.Atoi(fields[2])
98
toWrite := []byte(fields[0] + "\n")
99
for len(toWrite) > 0 {
100
n, err := shasToBatchWriter.Write(toWrite)
102
_ = catFileCheckReader.CloseWithError(err)
105
toWrite = toWrite[n:]