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.

lfs.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2019 The Gitea Authors.
  2. // All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package pull
  6. import (
  7. "bufio"
  8. "io"
  9. "strconv"
  10. "sync"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/git/pipeline"
  13. "code.gitea.io/gitea/modules/lfs"
  14. "code.gitea.io/gitea/modules/log"
  15. )
  16. // LFSPush pushes lfs objects referred to in new commits in the head repository from the base repository
  17. func LFSPush(tmpBasePath, mergeHeadSHA, mergeBaseSHA string, pr *models.PullRequest) error {
  18. // Now we have to implement git lfs push
  19. // git rev-list --objects --filter=blob:limit=1k HEAD --not base
  20. // pass blob shas in to git cat-file --batch-check (possibly unnecessary)
  21. // ensure only blobs and <=1k size then pass in to git cat-file --batch
  22. // to read each sha and check each as a pointer
  23. // Then if they are lfs -> add them to the baseRepo
  24. revListReader, revListWriter := io.Pipe()
  25. shasToCheckReader, shasToCheckWriter := io.Pipe()
  26. catFileCheckReader, catFileCheckWriter := io.Pipe()
  27. shasToBatchReader, shasToBatchWriter := io.Pipe()
  28. catFileBatchReader, catFileBatchWriter := io.Pipe()
  29. errChan := make(chan error, 1)
  30. wg := sync.WaitGroup{}
  31. wg.Add(6)
  32. // Create the go-routines in reverse order.
  33. // 6. Take the output of cat-file --batch and check if each file in turn
  34. // to see if they're pointers to files in the LFS store associated with
  35. // the head repo and add them to the base repo if so
  36. go createLFSMetaObjectsFromCatFileBatch(catFileBatchReader, &wg, pr)
  37. // 5. Take the shas of the blobs and batch read them
  38. go pipeline.CatFileBatch(shasToBatchReader, catFileBatchWriter, &wg, tmpBasePath)
  39. // 4. From the provided objects restrict to blobs <=1k
  40. go pipeline.BlobsLessThan1024FromCatFileBatchCheck(catFileCheckReader, shasToBatchWriter, &wg)
  41. // 3. Run batch-check on the objects retrieved from rev-list
  42. go pipeline.CatFileBatchCheck(shasToCheckReader, catFileCheckWriter, &wg, tmpBasePath)
  43. // 2. Check each object retrieved rejecting those without names as they will be commits or trees
  44. go pipeline.BlobsFromRevListObjects(revListReader, shasToCheckWriter, &wg)
  45. // 1. Run rev-list objects from mergeHead to mergeBase
  46. go pipeline.RevListObjects(revListWriter, &wg, tmpBasePath, mergeHeadSHA, mergeBaseSHA, errChan)
  47. wg.Wait()
  48. select {
  49. case err, has := <-errChan:
  50. if has {
  51. return err
  52. }
  53. default:
  54. }
  55. return nil
  56. }
  57. func createLFSMetaObjectsFromCatFileBatch(catFileBatchReader *io.PipeReader, wg *sync.WaitGroup, pr *models.PullRequest) {
  58. defer wg.Done()
  59. defer catFileBatchReader.Close()
  60. contentStore := lfs.NewContentStore()
  61. bufferedReader := bufio.NewReader(catFileBatchReader)
  62. buf := make([]byte, 1025)
  63. for {
  64. // File descriptor line: sha
  65. _, err := bufferedReader.ReadString(' ')
  66. if err != nil {
  67. _ = catFileBatchReader.CloseWithError(err)
  68. break
  69. }
  70. // Throw away the blob
  71. if _, err := bufferedReader.ReadString(' '); err != nil {
  72. _ = catFileBatchReader.CloseWithError(err)
  73. break
  74. }
  75. sizeStr, err := bufferedReader.ReadString('\n')
  76. if err != nil {
  77. _ = catFileBatchReader.CloseWithError(err)
  78. break
  79. }
  80. size, err := strconv.Atoi(sizeStr[:len(sizeStr)-1])
  81. if err != nil {
  82. _ = catFileBatchReader.CloseWithError(err)
  83. break
  84. }
  85. pointerBuf := buf[:size+1]
  86. if _, err := io.ReadFull(bufferedReader, pointerBuf); err != nil {
  87. _ = catFileBatchReader.CloseWithError(err)
  88. break
  89. }
  90. pointerBuf = pointerBuf[:size]
  91. // Now we need to check if the pointerBuf is an LFS pointer
  92. pointer, _ := lfs.ReadPointerFromBuffer(pointerBuf)
  93. if !pointer.IsValid() {
  94. continue
  95. }
  96. exist, _ := contentStore.Exists(pointer)
  97. if !exist {
  98. continue
  99. }
  100. // Then we need to check that this pointer is in the db
  101. if _, err := pr.HeadRepo.GetLFSMetaObjectByOid(pointer.Oid); err != nil {
  102. if err == models.ErrLFSObjectNotExist {
  103. log.Warn("During merge of: %d in %-v, there is a pointer to LFS Oid: %s which although present in the LFS store is not associated with the head repo %-v", pr.Index, pr.BaseRepo, pointer.Oid, pr.HeadRepo)
  104. continue
  105. }
  106. _ = catFileBatchReader.CloseWithError(err)
  107. break
  108. }
  109. // OK we have a pointer that is associated with the head repo
  110. // and is actually a file in the LFS
  111. // Therefore it should be associated with the base repo
  112. meta := &models.LFSMetaObject{Pointer: pointer}
  113. meta.RepositoryID = pr.BaseRepoID
  114. if _, err := models.NewLFSMetaObject(meta); err != nil {
  115. _ = catFileBatchReader.CloseWithError(err)
  116. break
  117. }
  118. }
  119. }