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

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