本站源代码
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

95 lines
3.1KB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package pipeline
  5. import (
  6. "bufio"
  7. "bytes"
  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(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("cat-file", "--batch-check")
  24. if err := cmd.RunInDirFullPipeline(tmpBasePath, catFileCheckWriter, stderr, shasToCheckReader); err != nil {
  25. _ = catFileCheckWriter.CloseWithError(fmt.Errorf("git cat-file --batch-check [%s]: %v - %s", tmpBasePath, err, errbuf.String()))
  26. }
  27. }
  28. // CatFileBatchCheckAllObjects runs cat-file with --batch-check --batch-all
  29. func CatFileBatchCheckAllObjects(catFileCheckWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string, errChan chan<- error) {
  30. defer wg.Done()
  31. defer catFileCheckWriter.Close()
  32. stderr := new(bytes.Buffer)
  33. var errbuf strings.Builder
  34. cmd := git.NewCommand("cat-file", "--batch-check", "--batch-all-objects")
  35. if err := cmd.RunInDirPipeline(tmpBasePath, catFileCheckWriter, stderr); err != nil {
  36. log.Error("git cat-file --batch-check --batch-all-object [%s]: %v - %s", tmpBasePath, err, errbuf.String())
  37. err = fmt.Errorf("git cat-file --batch-check --batch-all-object [%s]: %v - %s", tmpBasePath, err, errbuf.String())
  38. _ = catFileCheckWriter.CloseWithError(err)
  39. errChan <- err
  40. }
  41. }
  42. // CatFileBatch runs cat-file --batch
  43. func CatFileBatch(shasToBatchReader *io.PipeReader, catFileBatchWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath string) {
  44. defer wg.Done()
  45. defer shasToBatchReader.Close()
  46. defer catFileBatchWriter.Close()
  47. stderr := new(bytes.Buffer)
  48. var errbuf strings.Builder
  49. if err := git.NewCommand("cat-file", "--batch").RunInDirFullPipeline(tmpBasePath, catFileBatchWriter, stderr, shasToBatchReader); err != nil {
  50. _ = shasToBatchReader.CloseWithError(fmt.Errorf("git rev-list [%s]: %v - %s", tmpBasePath, err, errbuf.String()))
  51. }
  52. }
  53. // BlobsLessThan1024FromCatFileBatchCheck reads a pipeline from cat-file --batch-check and returns the blobs <1024 in size
  54. func BlobsLessThan1024FromCatFileBatchCheck(catFileCheckReader *io.PipeReader, shasToBatchWriter *io.PipeWriter, wg *sync.WaitGroup) {
  55. defer wg.Done()
  56. defer catFileCheckReader.Close()
  57. scanner := bufio.NewScanner(catFileCheckReader)
  58. defer func() {
  59. _ = shasToBatchWriter.CloseWithError(scanner.Err())
  60. }()
  61. for scanner.Scan() {
  62. line := scanner.Text()
  63. if len(line) == 0 {
  64. continue
  65. }
  66. fields := strings.Split(line, " ")
  67. if len(fields) < 3 || fields[1] != "blob" {
  68. continue
  69. }
  70. size, _ := strconv.Atoi(fields[2])
  71. if size > 1024 {
  72. continue
  73. }
  74. toWrite := []byte(fields[0] + "\n")
  75. for len(toWrite) > 0 {
  76. n, err := shasToBatchWriter.Write(toWrite)
  77. if err != nil {
  78. _ = catFileCheckReader.CloseWithError(err)
  79. break
  80. }
  81. toWrite = toWrite[n:]
  82. }
  83. }
  84. }
上海开阖软件有限公司 沪ICP备12045867号-1