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.

206 lines
5.2 KiB

Refactor editor upload, update and delete to use git plumbing and add LFS support (#5702) * Use git plumbing for upload: #5621 repo_editor.go: UploadRepoFile * Use git plumbing for upload: #5621 repo_editor.go: GetDiffPreview * Use git plumbing for upload: #5621 repo_editor.go: DeleteRepoFile * Use git plumbing for upload: #5621 repo_editor.go: UploadRepoFiles * Move branch checkout functions out of repo_editor.go as they are no longer used there * BUGFIX: The default permissions should be 100644 This is a change from the previous code but is more in keeping with the default behaviour of git. Signed-off-by: Andrew Thornton <art27@cantab.net> * Standardise cleanUploadFilename to more closely match git See verify_path in: https://github.com/git/git/blob/7f4e64169352e03476b0ea64e7e2973669e491a2/read-cache.c#L951 Signed-off-by: Andrew Thornton <art27@cantab.net> * Redirect on bad paths Signed-off-by: Andrew Thornton <art27@cantab.net> * Refactor to move the uploading functions out to a module Signed-off-by: Andrew Thornton <art27@cantab.net> * Add LFS support Signed-off-by: Andrew Thornton <art27@cantab.net> * Update upload.go attribution header Upload.go is essentially the remnants of repo_editor.go. The remaining code is essentially unchanged from the Gogs code, hence the Gogs attribution. * Delete upload files after session committed * Ensure that GIT_AUTHOR_NAME etc. are valid for git see #5774 Signed-off-by: Andrew Thornton <art27@cantab.net> * Add in test cases per @lafriks comment * Add space between gitea and github imports Signed-off-by: Andrew Thornton <art27@cantab.net> * more examples in TestCleanUploadName Signed-off-by: Andrew Thornton <art27@cantab.net> * fix formatting Signed-off-by: Andrew Thornton <art27@cantab.net> * Set the SSH_ORIGINAL_COMMAND to ensure hooks are run Signed-off-by: Andrew Thornton <art27@cantab.net> * Switch off SSH_ORIGINAL_COMMAND Signed-off-by: Andrew Thornton <art27@cantab.net>
5 years ago
  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 uploader
  5. import (
  6. "fmt"
  7. "os"
  8. "path"
  9. "strings"
  10. "code.gitea.io/gitea/modules/lfs"
  11. "code.gitea.io/gitea/modules/setting"
  12. "code.gitea.io/git"
  13. "code.gitea.io/gitea/models"
  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. // Now commit the tree
  111. commitHash, err := t.CommitTree(doer, treeHash, opts.Message)
  112. if err != nil {
  113. return err
  114. }
  115. // Now deal with LFS objects
  116. for _, uploadInfo := range infos {
  117. if uploadInfo.lfsMetaObject == nil {
  118. continue
  119. }
  120. uploadInfo.lfsMetaObject, err = models.NewLFSMetaObject(uploadInfo.lfsMetaObject)
  121. if err != nil {
  122. // OK Now we need to cleanup
  123. return cleanUpAfterFailure(&infos, t, err)
  124. }
  125. // Don't move the files yet - we need to ensure that
  126. // everything can be inserted first
  127. }
  128. // OK now we can insert the data into the store - there's no way to clean up the store
  129. // once it's in there, it's in there.
  130. contentStore := &lfs.ContentStore{BasePath: setting.LFS.ContentPath}
  131. for _, uploadInfo := range infos {
  132. if uploadInfo.lfsMetaObject == nil {
  133. continue
  134. }
  135. if !contentStore.Exists(uploadInfo.lfsMetaObject) {
  136. file, err := os.Open(uploadInfo.upload.LocalPath())
  137. if err != nil {
  138. return cleanUpAfterFailure(&infos, t, err)
  139. }
  140. defer file.Close()
  141. // FIXME: Put regenerates the hash and copies the file over.
  142. // I guess this strictly ensures the soundness of the store but this is inefficient.
  143. if err := contentStore.Put(uploadInfo.lfsMetaObject, file); err != nil {
  144. // OK Now we need to cleanup
  145. // Can't clean up the store, once uploaded there they're there.
  146. return cleanUpAfterFailure(&infos, t, err)
  147. }
  148. }
  149. }
  150. // Then push this tree to NewBranch
  151. if err := t.Push(doer, commitHash, opts.NewBranch); err != nil {
  152. return err
  153. }
  154. // Simulate push event.
  155. oldCommitID := opts.LastCommitID
  156. if opts.NewBranch != opts.OldBranch {
  157. oldCommitID = git.EmptySHA
  158. }
  159. if err = repo.GetOwner(); err != nil {
  160. return fmt.Errorf("GetOwner: %v", err)
  161. }
  162. err = models.PushUpdate(
  163. opts.NewBranch,
  164. models.PushUpdateOptions{
  165. PusherID: doer.ID,
  166. PusherName: doer.Name,
  167. RepoUserName: repo.Owner.Name,
  168. RepoName: repo.Name,
  169. RefFullName: git.BranchPrefix + opts.NewBranch,
  170. OldCommitID: oldCommitID,
  171. NewCommitID: commitHash,
  172. },
  173. )
  174. if err != nil {
  175. return fmt.Errorf("PushUpdate: %v", err)
  176. }
  177. // FIXME: Should we models.UpdateRepoIndexer(repo) here?
  178. return models.DeleteUploads(uploads...)
  179. }