You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

catfile.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package pipeline
  4. import (
  5. "bufio"
  6. "bytes"
  7. "context"
  8. "fmt"
  9. "io"
  10. "strconv"
  11. "strings"
  12. "sync"
  13. "code.gitea.io/gitea/modules/git"
  14. "code.gitea.io/gitea/modules/log"
  15. )
  16. // CatFileBatchCheck runs cat-file with --batch-check
  17. func CatFileBatchCheck(ctx context.Context, shasToCheckReader *io.PipeReader, catFileCheckWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string) {
  18. defer wg.Done()
  19. defer shasToCheckReader.Close()
  20. defer catFileCheckWriter.Close()
  21. stderr := new(bytes.Buffer)
  22. var errbuf strings.Builder
  23. cmd := git.NewCommand(ctx, "cat-file", "--batch-check")
  24. if err := cmd.Run(&git.RunOpts{
  25. Dir: tmpBasePath,
  26. Stdin: shasToCheckReader,
  27. Stdout: catFileCheckWriter,
  28. Stderr: stderr,
  29. }); err != nil {
  30. _ = catFileCheckWriter.CloseWithError(fmt.Errorf("git cat-file --batch-check [%s]: %w - %s", tmpBasePath, err, errbuf.String()))
  31. }
  32. }
  33. // CatFileBatchCheckAllObjects runs cat-file with --batch-check --batch-all
  34. func CatFileBatchCheckAllObjects(ctx context.Context, catFileCheckWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string, errChan chan<- error) {
  35. defer wg.Done()
  36. defer catFileCheckWriter.Close()
  37. stderr := new(bytes.Buffer)
  38. var errbuf strings.Builder
  39. cmd := git.NewCommand(ctx, "cat-file", "--batch-check", "--batch-all-objects")
  40. if err := cmd.Run(&git.RunOpts{
  41. Dir: tmpBasePath,
  42. Stdout: catFileCheckWriter,
  43. Stderr: stderr,
  44. }); err != nil {
  45. log.Error("git cat-file --batch-check --batch-all-object [%s]: %v - %s", tmpBasePath, err, errbuf.String())
  46. err = fmt.Errorf("git cat-file --batch-check --batch-all-object [%s]: %w - %s", tmpBasePath, err, errbuf.String())
  47. _ = catFileCheckWriter.CloseWithError(err)
  48. errChan <- err
  49. }
  50. }
  51. // CatFileBatch runs cat-file --batch
  52. func CatFileBatch(ctx context.Context, shasToBatchReader *io.PipeReader, catFileBatchWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string) {
  53. defer wg.Done()
  54. defer shasToBatchReader.Close()
  55. defer catFileBatchWriter.Close()
  56. stderr := new(bytes.Buffer)
  57. var errbuf strings.Builder
  58. if err := git.NewCommand(ctx, "cat-file", "--batch").Run(&git.RunOpts{
  59. Dir: tmpBasePath,
  60. Stdout: catFileBatchWriter,
  61. Stdin: shasToBatchReader,
  62. Stderr: stderr,
  63. }); err != nil {
  64. _ = shasToBatchReader.CloseWithError(fmt.Errorf("git rev-list [%s]: %w - %s", tmpBasePath, err, errbuf.String()))
  65. }
  66. }
  67. // BlobsLessThan1024FromCatFileBatchCheck reads a pipeline from cat-file --batch-check and returns the blobs <1024 in size
  68. func BlobsLessThan1024FromCatFileBatchCheck(catFileCheckReader *io.PipeReader, shasToBatchWriter *io.PipeWriter, wg *sync.WaitGroup) {
  69. defer wg.Done()
  70. defer catFileCheckReader.Close()
  71. scanner := bufio.NewScanner(catFileCheckReader)
  72. defer func() {
  73. _ = shasToBatchWriter.CloseWithError(scanner.Err())
  74. }()
  75. for scanner.Scan() {
  76. line := scanner.Text()
  77. if len(line) == 0 {
  78. continue
  79. }
  80. fields := strings.Split(line, " ")
  81. if len(fields) < 3 || fields[1] != "blob" {
  82. continue
  83. }
  84. size, _ := strconv.Atoi(fields[2])
  85. if size > 1024 {
  86. continue
  87. }
  88. toWrite := []byte(fields[0] + "\n")
  89. for len(toWrite) > 0 {
  90. n, err := shasToBatchWriter.Write(toWrite)
  91. if err != nil {
  92. _ = catFileCheckReader.CloseWithError(err)
  93. break
  94. }
  95. toWrite = toWrite[n:]
  96. }
  97. }
  98. }