gitea源码

lfs.go 4.5KB

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