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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. bufferedReader := bufio.NewReader(catFileBatchReader)
  61. buf := make([]byte, 1025)
  62. for {
  63. // File descriptor line: sha
  64. _, err := bufferedReader.ReadString(' ')
  65. if err != nil {
  66. _ = catFileBatchReader.CloseWithError(err)
  67. break
  68. }
  69. // Throw away the blob
  70. if _, err := bufferedReader.ReadString(' '); err != nil {
  71. _ = catFileBatchReader.CloseWithError(err)
  72. break
  73. }
  74. sizeStr, err := bufferedReader.ReadString('\n')
  75. if err != nil {
  76. _ = catFileBatchReader.CloseWithError(err)
  77. break
  78. }
  79. size, err := strconv.Atoi(sizeStr[:len(sizeStr)-1])
  80. if err != nil {
  81. _ = catFileBatchReader.CloseWithError(err)
  82. break
  83. }
  84. pointerBuf := buf[:size+1]
  85. if _, err := io.ReadFull(bufferedReader, pointerBuf); err != nil {
  86. _ = catFileBatchReader.CloseWithError(err)
  87. break
  88. }
  89. pointerBuf = pointerBuf[:size]
  90. // Now we need to check if the pointerBuf is an LFS pointer
  91. pointer := lfs.IsPointerFile(&pointerBuf)
  92. if pointer == nil {
  93. continue
  94. }
  95. // Then we need to check that this pointer is in the db
  96. if _, err := pr.HeadRepo.GetLFSMetaObjectByOid(pointer.Oid); err != nil {
  97. if err == models.ErrLFSObjectNotExist {
  98. 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)
  99. continue
  100. }
  101. _ = catFileBatchReader.CloseWithError(err)
  102. break
  103. }
  104. // OK we have a pointer that is associated with the head repo
  105. // and is actually a file in the LFS
  106. // Therefore it should be associated with the base repo
  107. pointer.RepositoryID = pr.BaseRepoID
  108. if _, err := models.NewLFSMetaObject(pointer); err != nil {
  109. _ = catFileBatchReader.CloseWithError(err)
  110. break
  111. }
  112. }
  113. }