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.

revlist.go 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. "strings"
  11. "sync"
  12. "code.gitea.io/gitea/modules/git"
  13. "code.gitea.io/gitea/modules/log"
  14. )
  15. // RevListAllObjects runs rev-list --objects --all and writes to a pipewriter
  16. func RevListAllObjects(revListWriter *io.PipeWriter, wg *sync.WaitGroup, basePath string, errChan chan<- error) {
  17. defer wg.Done()
  18. defer revListWriter.Close()
  19. stderr := new(bytes.Buffer)
  20. var errbuf strings.Builder
  21. cmd := git.NewCommand("rev-list", "--objects", "--all")
  22. if err := cmd.RunInDirPipeline(basePath, revListWriter, stderr); err != nil {
  23. log.Error("git rev-list --objects --all [%s]: %v - %s", basePath, err, errbuf.String())
  24. err = fmt.Errorf("git rev-list --objects --all [%s]: %v - %s", basePath, err, errbuf.String())
  25. _ = revListWriter.CloseWithError(err)
  26. errChan <- err
  27. }
  28. }
  29. // RevListObjects run rev-list --objects from headSHA to baseSHA
  30. func RevListObjects(revListWriter *io.PipeWriter, wg *sync.WaitGroup, tmpBasePath, headSHA, baseSHA string, errChan chan<- error) {
  31. defer wg.Done()
  32. defer revListWriter.Close()
  33. stderr := new(bytes.Buffer)
  34. var errbuf strings.Builder
  35. cmd := git.NewCommand("rev-list", "--objects", headSHA, "--not", baseSHA)
  36. if err := cmd.RunInDirPipeline(tmpBasePath, revListWriter, stderr); err != nil {
  37. log.Error("git rev-list [%s]: %v - %s", tmpBasePath, err, errbuf.String())
  38. errChan <- fmt.Errorf("git rev-list [%s]: %v - %s", tmpBasePath, err, errbuf.String())
  39. }
  40. }
  41. // BlobsFromRevListObjects reads a RevListAllObjects and only selects blobs
  42. func BlobsFromRevListObjects(revListReader *io.PipeReader, shasToCheckWriter *io.PipeWriter, wg *sync.WaitGroup) {
  43. defer wg.Done()
  44. defer revListReader.Close()
  45. scanner := bufio.NewScanner(revListReader)
  46. defer func() {
  47. _ = shasToCheckWriter.CloseWithError(scanner.Err())
  48. }()
  49. for scanner.Scan() {
  50. line := scanner.Text()
  51. if len(line) == 0 {
  52. continue
  53. }
  54. fields := strings.Split(line, " ")
  55. if len(fields) < 2 || len(fields[1]) == 0 {
  56. continue
  57. }
  58. toWrite := []byte(fields[0] + "\n")
  59. for len(toWrite) > 0 {
  60. n, err := shasToCheckWriter.Write(toWrite)
  61. if err != nil {
  62. _ = revListReader.CloseWithError(err)
  63. break
  64. }
  65. toWrite = toWrite[n:]
  66. }
  67. }
  68. }