2019-10-28 18:31:55 +00:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-10-28 18:31:55 +00:00
|
|
|
|
|
|
|
package pipeline
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2022-01-19 23:26:57 +00:00
|
|
|
"context"
|
2019-10-28 18:31:55 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RevListAllObjects runs rev-list --objects --all and writes to a pipewriter
|
2022-01-19 23:26:57 +00:00
|
|
|
func RevListAllObjects(ctx context.Context, revListWriter *io.PipeWriter, wg *sync.WaitGroup, basePath string, errChan chan<- error) {
|
2019-10-28 18:31:55 +00:00
|
|
|
defer wg.Done()
|
|
|
|
defer revListWriter.Close()
|
|
|
|
|
|
|
|
stderr := new(bytes.Buffer)
|
|
|
|
var errbuf strings.Builder
|
2022-02-06 19:01:47 +00:00
|
|
|
cmd := git.NewCommand(ctx, "rev-list", "--objects", "--all")
|
2022-04-01 02:55:30 +00:00
|
|
|
if err := cmd.Run(&git.RunOpts{
|
|
|
|
Dir: basePath,
|
|
|
|
Stdout: revListWriter,
|
|
|
|
Stderr: stderr,
|
2022-02-11 12:47:22 +00:00
|
|
|
}); err != nil {
|
2019-10-28 18:31:55 +00:00
|
|
|
log.Error("git rev-list --objects --all [%s]: %v - %s", basePath, err, errbuf.String())
|
2022-10-24 19:29:17 +00:00
|
|
|
err = fmt.Errorf("git rev-list --objects --all [%s]: %w - %s", basePath, err, errbuf.String())
|
2019-10-28 18:31:55 +00:00
|
|
|
_ = revListWriter.CloseWithError(err)
|
|
|
|
errChan <- err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RevListObjects run rev-list --objects from headSHA to baseSHA
|
2022-01-19 23:26:57 +00:00
|
|
|
func RevListObjects(ctx context.Context, revListWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath, headSHA, baseSHA string, errChan chan<- error) {
|
2019-10-28 18:31:55 +00:00
|
|
|
defer wg.Done()
|
|
|
|
defer revListWriter.Close()
|
|
|
|
stderr := new(bytes.Buffer)
|
|
|
|
var errbuf strings.Builder
|
2023-03-07 20:07:35 +00:00
|
|
|
cmd := git.NewCommand(ctx, "rev-list", "--objects").AddDynamicArguments(headSHA)
|
|
|
|
if baseSHA != "" {
|
|
|
|
cmd = cmd.AddArguments("--not").AddDynamicArguments(baseSHA)
|
|
|
|
}
|
2022-04-01 02:55:30 +00:00
|
|
|
if err := cmd.Run(&git.RunOpts{
|
|
|
|
Dir: tmpBasePath,
|
|
|
|
Stdout: revListWriter,
|
|
|
|
Stderr: stderr,
|
2022-02-11 12:47:22 +00:00
|
|
|
}); err != nil {
|
2019-10-28 18:31:55 +00:00
|
|
|
log.Error("git rev-list [%s]: %v - %s", tmpBasePath, err, errbuf.String())
|
2022-10-24 19:29:17 +00:00
|
|
|
errChan <- fmt.Errorf("git rev-list [%s]: %w - %s", tmpBasePath, err, errbuf.String())
|
2019-10-28 18:31:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlobsFromRevListObjects reads a RevListAllObjects and only selects blobs
|
|
|
|
func BlobsFromRevListObjects(revListReader *io.PipeReader, shasToCheckWriter *io.PipeWriter, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
defer revListReader.Close()
|
|
|
|
scanner := bufio.NewScanner(revListReader)
|
|
|
|
defer func() {
|
|
|
|
_ = shasToCheckWriter.CloseWithError(scanner.Err())
|
|
|
|
}()
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if len(line) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fields := strings.Split(line, " ")
|
|
|
|
if len(fields) < 2 || len(fields[1]) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
toWrite := []byte(fields[0] + "\n")
|
|
|
|
for len(toWrite) > 0 {
|
|
|
|
n, err := shasToCheckWriter.Write(toWrite)
|
|
|
|
if err != nil {
|
|
|
|
_ = revListReader.CloseWithError(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
toWrite = toWrite[n:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|