2019-10-28 12:31:55 -06:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-10-28 12:31:55 -06:00
|
|
|
|
|
|
|
package pipeline
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2022-01-19 16:26:57 -07:00
|
|
|
"context"
|
2019-10-28 12:31:55 -06:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CatFileBatchCheck runs cat-file with --batch-check
|
2022-01-19 16:26:57 -07:00
|
|
|
func CatFileBatchCheck(ctx context.Context, shasToCheckReader *io.PipeReader, catFileCheckWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string) {
|
2019-10-28 12:31:55 -06:00
|
|
|
defer wg.Done()
|
|
|
|
defer shasToCheckReader.Close()
|
|
|
|
defer catFileCheckWriter.Close()
|
|
|
|
|
|
|
|
stderr := new(bytes.Buffer)
|
|
|
|
var errbuf strings.Builder
|
2022-02-06 12:01:47 -07:00
|
|
|
cmd := git.NewCommand(ctx, "cat-file", "--batch-check")
|
2022-03-31 20:55:30 -06:00
|
|
|
if err := cmd.Run(&git.RunOpts{
|
|
|
|
Dir: tmpBasePath,
|
|
|
|
Stdin: shasToCheckReader,
|
|
|
|
Stdout: catFileCheckWriter,
|
|
|
|
Stderr: stderr,
|
2022-02-11 05:47:22 -07:00
|
|
|
}); err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
_ = catFileCheckWriter.CloseWithError(fmt.Errorf("git cat-file --batch-check [%s]: %w - %s", tmpBasePath, err, errbuf.String()))
|
2019-10-28 12:31:55 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CatFileBatchCheckAllObjects runs cat-file with --batch-check --batch-all
|
2022-01-19 16:26:57 -07:00
|
|
|
func CatFileBatchCheckAllObjects(ctx context.Context, catFileCheckWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string, errChan chan<- error) {
|
2019-10-28 12:31:55 -06:00
|
|
|
defer wg.Done()
|
|
|
|
defer catFileCheckWriter.Close()
|
|
|
|
|
|
|
|
stderr := new(bytes.Buffer)
|
|
|
|
var errbuf strings.Builder
|
2022-02-06 12:01:47 -07:00
|
|
|
cmd := git.NewCommand(ctx, "cat-file", "--batch-check", "--batch-all-objects")
|
2022-03-31 20:55:30 -06:00
|
|
|
if err := cmd.Run(&git.RunOpts{
|
|
|
|
Dir: tmpBasePath,
|
|
|
|
Stdout: catFileCheckWriter,
|
|
|
|
Stderr: stderr,
|
2022-02-11 05:47:22 -07:00
|
|
|
}); err != nil {
|
2019-10-28 12:31:55 -06:00
|
|
|
log.Error("git cat-file --batch-check --batch-all-object [%s]: %v - %s", tmpBasePath, err, errbuf.String())
|
2022-10-24 13:29:17 -06:00
|
|
|
err = fmt.Errorf("git cat-file --batch-check --batch-all-object [%s]: %w - %s", tmpBasePath, err, errbuf.String())
|
2019-10-28 12:31:55 -06:00
|
|
|
_ = catFileCheckWriter.CloseWithError(err)
|
|
|
|
errChan <- err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CatFileBatch runs cat-file --batch
|
2022-01-19 16:26:57 -07:00
|
|
|
func CatFileBatch(ctx context.Context, shasToBatchReader *io.PipeReader, catFileBatchWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string) {
|
2019-10-28 12:31:55 -06:00
|
|
|
defer wg.Done()
|
|
|
|
defer shasToBatchReader.Close()
|
|
|
|
defer catFileBatchWriter.Close()
|
|
|
|
|
|
|
|
stderr := new(bytes.Buffer)
|
|
|
|
var errbuf strings.Builder
|
2022-03-31 20:55:30 -06:00
|
|
|
if err := git.NewCommand(ctx, "cat-file", "--batch").Run(&git.RunOpts{
|
|
|
|
Dir: tmpBasePath,
|
|
|
|
Stdout: catFileBatchWriter,
|
|
|
|
Stdin: shasToBatchReader,
|
|
|
|
Stderr: stderr,
|
2022-02-11 05:47:22 -07:00
|
|
|
}); err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
_ = shasToBatchReader.CloseWithError(fmt.Errorf("git rev-list [%s]: %w - %s", tmpBasePath, err, errbuf.String()))
|
2019-10-28 12:31:55 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlobsLessThan1024FromCatFileBatchCheck reads a pipeline from cat-file --batch-check and returns the blobs <1024 in size
|
|
|
|
func BlobsLessThan1024FromCatFileBatchCheck(catFileCheckReader *io.PipeReader, shasToBatchWriter *io.PipeWriter, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
defer catFileCheckReader.Close()
|
|
|
|
scanner := bufio.NewScanner(catFileCheckReader)
|
|
|
|
defer func() {
|
|
|
|
_ = shasToBatchWriter.CloseWithError(scanner.Err())
|
|
|
|
}()
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if len(line) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fields := strings.Split(line, " ")
|
|
|
|
if len(fields) < 3 || fields[1] != "blob" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
size, _ := strconv.Atoi(fields[2])
|
|
|
|
if size > 1024 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
toWrite := []byte(fields[0] + "\n")
|
|
|
|
for len(toWrite) > 0 {
|
|
|
|
n, err := shasToBatchWriter.Write(toWrite)
|
|
|
|
if err != nil {
|
|
|
|
_ = catFileCheckReader.CloseWithError(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
toWrite = toWrite[n:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|