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.

upload.go 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 repofiles
  5. import (
  6. "fmt"
  7. "os"
  8. "path"
  9. "strings"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/git"
  12. "code.gitea.io/gitea/modules/lfs"
  13. "code.gitea.io/gitea/modules/setting"
  14. )
  15. // UploadRepoFileOptions contains the uploaded repository file options
  16. type UploadRepoFileOptions struct {
  17. LastCommitID string
  18. OldBranch string
  19. NewBranch string
  20. TreePath string
  21. Message string
  22. Files []string // In UUID format.
  23. }
  24. type uploadInfo struct {
  25. upload *models.Upload
  26. lfsMetaObject *models.LFSMetaObject
  27. }
  28. func cleanUpAfterFailure(infos *[]uploadInfo, t *TemporaryUploadRepository, original error) error {
  29. for _, info := range *infos {
  30. if info.lfsMetaObject == nil {
  31. continue
  32. }
  33. if !info.lfsMetaObject.Existing {
  34. if err := t.repo.RemoveLFSMetaObjectByOid(info.lfsMetaObject.Oid); err != nil {
  35. original = fmt.Errorf("%v, %v", original, err)
  36. }
  37. }
  38. }
  39. return original
  40. }
  41. // UploadRepoFiles uploads files to the given repository
  42. func UploadRepoFiles(repo *models.Repository, doer *models.User, opts *UploadRepoFileOptions) error {
  43. if len(opts.Files) == 0 {
  44. return nil
  45. }
  46. uploads, err := models.GetUploadsByUUIDs(opts.Files)
  47. if err != nil {
  48. return fmt.Errorf("GetUploadsByUUIDs [uuids: %v]: %v", opts.Files, err)
  49. }
  50. t, err := NewTemporaryUploadRepository(repo)
  51. defer t.Close()
  52. if err != nil {
  53. return err
  54. }
  55. if err := t.Clone(opts.OldBranch); err != nil {
  56. return err
  57. }
  58. if err := t.SetDefaultIndex(); err != nil {
  59. return err
  60. }
  61. names := make([]string, len(uploads))
  62. infos := make([]uploadInfo, len(uploads))
  63. for i, upload := range uploads {
  64. names[i] = upload.Name
  65. infos[i] = uploadInfo{upload: upload}
  66. }
  67. filename2attribute2info, err := t.CheckAttribute("filter", names...)
  68. if err != nil {
  69. return err
  70. }
  71. // Copy uploaded files into repository.
  72. for i, uploadInfo := range infos {
  73. file, err := os.Open(uploadInfo.upload.LocalPath())
  74. if err != nil {
  75. return err
  76. }
  77. defer file.Close()
  78. var objectHash string
  79. if filename2attribute2info[uploadInfo.upload.Name] != nil && filename2attribute2info[uploadInfo.upload.Name]["filter"] == "lfs" {
  80. // Handle LFS
  81. // FIXME: Inefficient! this should probably happen in models.Upload
  82. oid, err := models.GenerateLFSOid(file)
  83. if err != nil {
  84. return err
  85. }
  86. fileInfo, err := file.Stat()
  87. if err != nil {
  88. return err
  89. }
  90. uploadInfo.lfsMetaObject = &models.LFSMetaObject{Oid: oid, Size: fileInfo.Size(), RepositoryID: t.repo.ID}
  91. if objectHash, err = t.HashObject(strings.NewReader(uploadInfo.lfsMetaObject.Pointer())); err != nil {
  92. return err
  93. }
  94. infos[i] = uploadInfo
  95. } else {
  96. if objectHash, err = t.HashObject(file); err != nil {
  97. return err
  98. }
  99. }
  100. // Add the object to the index
  101. if err := t.AddObjectToIndex("100644", objectHash, path.Join(opts.TreePath, uploadInfo.upload.Name)); err != nil {
  102. return err
  103. }
  104. }
  105. // Now write the tree
  106. treeHash, err := t.WriteTree()
  107. if err != nil {
  108. return err
  109. }
  110. // make author and committer the doer
  111. author := doer
  112. committer := doer
  113. // Now commit the tree
  114. commitHash, err := t.CommitTree(author, committer, treeHash, opts.Message)
  115. if err != nil {
  116. return err
  117. }
  118. // Now deal with LFS objects
  119. for _, uploadInfo := range infos {
  120. if uploadInfo.lfsMetaObject == nil {
  121. continue
  122. }
  123. uploadInfo.lfsMetaObject, err = models.NewLFSMetaObject(uploadInfo.lfsMetaObject)
  124. if err != nil {
  125. // OK Now we need to cleanup
  126. return cleanUpAfterFailure(&infos, t, err)
  127. }
  128. // Don't move the files yet - we need to ensure that
  129. // everything can be inserted first
  130. }
  131. // OK now we can insert the data into the store - there's no way to clean up the store
  132. // once it's in there, it's in there.
  133. contentStore := &lfs.ContentStore{BasePath: setting.LFS.ContentPath}
  134. for _, uploadInfo := range infos {
  135. if uploadInfo.lfsMetaObject == nil {
  136. continue
  137. }
  138. if !contentStore.Exists(uploadInfo.lfsMetaObject) {
  139. file, err := os.Open(uploadInfo.upload.LocalPath())
  140. if err != nil {
  141. return cleanUpAfterFailure(&infos, t, err)
  142. }
  143. defer file.Close()
  144. // FIXME: Put regenerates the hash and copies the file over.
  145. // I guess this strictly ensures the soundness of the store but this is inefficient.
  146. if err := contentStore.Put(uploadInfo.lfsMetaObject, file); err != nil {
  147. // OK Now we need to cleanup
  148. // Can't clean up the store, once uploaded there they're there.
  149. return cleanUpAfterFailure(&infos, t, err)
  150. }
  151. }
  152. }
  153. // Then push this tree to NewBranch
  154. if err := t.Push(doer, commitHash, opts.NewBranch); err != nil {
  155. return err
  156. }
  157. // Simulate push event.
  158. oldCommitID := opts.LastCommitID
  159. if opts.NewBranch != opts.OldBranch {
  160. oldCommitID = git.EmptySHA
  161. }
  162. if err = repo.GetOwner(); err != nil {
  163. return fmt.Errorf("GetOwner: %v", err)
  164. }
  165. err = models.PushUpdate(
  166. opts.NewBranch,
  167. models.PushUpdateOptions{
  168. PusherID: doer.ID,
  169. PusherName: doer.Name,
  170. RepoUserName: repo.Owner.Name,
  171. RepoName: repo.Name,
  172. RefFullName: git.BranchPrefix + opts.NewBranch,
  173. OldCommitID: oldCommitID,
  174. NewCommitID: commitHash,
  175. },
  176. )
  177. if err != nil {
  178. return fmt.Errorf("PushUpdate: %v", err)
  179. }
  180. // FIXME: Should we models.UpdateRepoIndexer(repo) here?
  181. return models.DeleteUploads(uploads...)
  182. }