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.

delete.go 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. "strings"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/git"
  10. repo_module "code.gitea.io/gitea/modules/repository"
  11. api "code.gitea.io/gitea/modules/structs"
  12. )
  13. // DeleteRepoFileOptions holds the repository delete file options
  14. type DeleteRepoFileOptions struct {
  15. LastCommitID string
  16. OldBranch string
  17. NewBranch string
  18. TreePath string
  19. Message string
  20. SHA string
  21. Author *IdentityOptions
  22. Committer *IdentityOptions
  23. Dates *CommitDateOptions
  24. }
  25. // DeleteRepoFile deletes a file in the given repository
  26. func DeleteRepoFile(repo *models.Repository, doer *models.User, opts *DeleteRepoFileOptions) (*api.FileResponse, error) {
  27. // If no branch name is set, assume the repo's default branch
  28. if opts.OldBranch == "" {
  29. opts.OldBranch = repo.DefaultBranch
  30. }
  31. if opts.NewBranch == "" {
  32. opts.NewBranch = opts.OldBranch
  33. }
  34. // oldBranch must exist for this operation
  35. if _, err := repo_module.GetBranch(repo, opts.OldBranch); err != nil {
  36. return nil, err
  37. }
  38. // A NewBranch can be specified for the file to be created/updated in a new branch.
  39. // Check to make sure the branch does not already exist, otherwise we can't proceed.
  40. // If we aren't branching to a new branch, make sure user can commit to the given branch
  41. if opts.NewBranch != opts.OldBranch {
  42. newBranch, err := repo_module.GetBranch(repo, opts.NewBranch)
  43. if err != nil && !git.IsErrBranchNotExist(err) {
  44. return nil, err
  45. }
  46. if newBranch != nil {
  47. return nil, models.ErrBranchAlreadyExists{
  48. BranchName: opts.NewBranch,
  49. }
  50. }
  51. } else {
  52. protectedBranch, err := repo.GetBranchProtection(opts.OldBranch)
  53. if err != nil {
  54. return nil, err
  55. }
  56. if protectedBranch != nil {
  57. if !protectedBranch.CanUserPush(doer.ID) {
  58. return nil, models.ErrUserCannotCommit{
  59. UserName: doer.LowerName,
  60. }
  61. }
  62. if protectedBranch.RequireSignedCommits {
  63. _, _, _, err := repo.SignCRUDAction(doer, repo.RepoPath(), opts.OldBranch)
  64. if err != nil {
  65. if !models.IsErrWontSign(err) {
  66. return nil, err
  67. }
  68. return nil, models.ErrUserCannotCommit{
  69. UserName: doer.LowerName,
  70. }
  71. }
  72. }
  73. patterns := protectedBranch.GetProtectedFilePatterns()
  74. for _, pat := range patterns {
  75. if pat.Match(strings.ToLower(opts.TreePath)) {
  76. return nil, models.ErrFilePathProtected{
  77. Path: opts.TreePath,
  78. }
  79. }
  80. }
  81. }
  82. }
  83. // Check that the path given in opts.treeName is valid (not a git path)
  84. treePath := CleanUploadFileName(opts.TreePath)
  85. if treePath == "" {
  86. return nil, models.ErrFilenameInvalid{
  87. Path: opts.TreePath,
  88. }
  89. }
  90. message := strings.TrimSpace(opts.Message)
  91. author, committer := GetAuthorAndCommitterUsers(opts.Author, opts.Committer, doer)
  92. t, err := NewTemporaryUploadRepository(repo)
  93. if err != nil {
  94. return nil, err
  95. }
  96. defer t.Close()
  97. if err := t.Clone(opts.OldBranch); err != nil {
  98. return nil, err
  99. }
  100. if err := t.SetDefaultIndex(); err != nil {
  101. return nil, err
  102. }
  103. // Get the commit of the original branch
  104. commit, err := t.GetBranchCommit(opts.OldBranch)
  105. if err != nil {
  106. return nil, err // Couldn't get a commit for the branch
  107. }
  108. // Assigned LastCommitID in opts if it hasn't been set
  109. if opts.LastCommitID == "" {
  110. opts.LastCommitID = commit.ID.String()
  111. } else {
  112. lastCommitID, err := t.gitRepo.ConvertToSHA1(opts.LastCommitID)
  113. if err != nil {
  114. return nil, fmt.Errorf("DeleteRepoFile: Invalid last commit ID: %v", err)
  115. }
  116. opts.LastCommitID = lastCommitID.String()
  117. }
  118. // Get the files in the index
  119. filesInIndex, err := t.LsFiles(opts.TreePath)
  120. if err != nil {
  121. return nil, fmt.Errorf("DeleteRepoFile: %v", err)
  122. }
  123. // Find the file we want to delete in the index
  124. inFilelist := false
  125. for _, file := range filesInIndex {
  126. if file == opts.TreePath {
  127. inFilelist = true
  128. break
  129. }
  130. }
  131. if !inFilelist {
  132. return nil, models.ErrRepoFileDoesNotExist{
  133. Path: opts.TreePath,
  134. }
  135. }
  136. // Get the entry of treePath and check if the SHA given is the same as the file
  137. entry, err := commit.GetTreeEntryByPath(treePath)
  138. if err != nil {
  139. return nil, err
  140. }
  141. if opts.SHA != "" {
  142. // If a SHA was given and the SHA given doesn't match the SHA of the fromTreePath, throw error
  143. if opts.SHA != entry.ID.String() {
  144. return nil, models.ErrSHADoesNotMatch{
  145. Path: treePath,
  146. GivenSHA: opts.SHA,
  147. CurrentSHA: entry.ID.String(),
  148. }
  149. }
  150. } else if opts.LastCommitID != "" {
  151. // If a lastCommitID was given and it doesn't match the commitID of the head of the branch throw
  152. // an error, but only if we aren't creating a new branch.
  153. if commit.ID.String() != opts.LastCommitID && opts.OldBranch == opts.NewBranch {
  154. // CommitIDs don't match, but we don't want to throw a ErrCommitIDDoesNotMatch unless
  155. // this specific file has been edited since opts.LastCommitID
  156. if changed, err := commit.FileChangedSinceCommit(treePath, opts.LastCommitID); err != nil {
  157. return nil, err
  158. } else if changed {
  159. return nil, models.ErrCommitIDDoesNotMatch{
  160. GivenCommitID: opts.LastCommitID,
  161. CurrentCommitID: opts.LastCommitID,
  162. }
  163. }
  164. // The file wasn't modified, so we are good to delete it
  165. }
  166. } else {
  167. // When deleting a file, a lastCommitID or SHA needs to be given to make sure other commits haven't been
  168. // made. We throw an error if one wasn't provided.
  169. return nil, models.ErrSHAOrCommitIDNotProvided{}
  170. }
  171. // Remove the file from the index
  172. if err := t.RemoveFilesFromIndex(opts.TreePath); err != nil {
  173. return nil, err
  174. }
  175. // Now write the tree
  176. treeHash, err := t.WriteTree()
  177. if err != nil {
  178. return nil, err
  179. }
  180. // Now commit the tree
  181. var commitHash string
  182. if opts.Dates != nil {
  183. commitHash, err = t.CommitTreeWithDate(author, committer, treeHash, message, opts.Dates.Author, opts.Dates.Committer)
  184. } else {
  185. commitHash, err = t.CommitTree(author, committer, treeHash, message)
  186. }
  187. if err != nil {
  188. return nil, err
  189. }
  190. // Then push this tree to NewBranch
  191. if err := t.Push(doer, commitHash, opts.NewBranch); err != nil {
  192. return nil, err
  193. }
  194. commit, err = t.GetCommit(commitHash)
  195. if err != nil {
  196. return nil, err
  197. }
  198. file, err := GetFileResponseFromCommit(repo, commit, opts.NewBranch, treePath)
  199. if err != nil {
  200. return nil, err
  201. }
  202. return file, nil
  203. }