選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 && !protectedBranch.CanUserPush(doer.ID) {
  57. return nil, models.ErrUserCannotCommit{
  58. UserName: doer.LowerName,
  59. }
  60. }
  61. if protectedBranch != nil && protectedBranch.RequireSignedCommits {
  62. _, _, err := repo.SignCRUDAction(doer, repo.RepoPath(), opts.OldBranch)
  63. if err != nil {
  64. if !models.IsErrWontSign(err) {
  65. return nil, err
  66. }
  67. return nil, models.ErrUserCannotCommit{
  68. UserName: doer.LowerName,
  69. }
  70. }
  71. }
  72. }
  73. // Check that the path given in opts.treeName is valid (not a git path)
  74. treePath := CleanUploadFileName(opts.TreePath)
  75. if treePath == "" {
  76. return nil, models.ErrFilenameInvalid{
  77. Path: opts.TreePath,
  78. }
  79. }
  80. message := strings.TrimSpace(opts.Message)
  81. author, committer := GetAuthorAndCommitterUsers(opts.Author, opts.Committer, doer)
  82. t, err := NewTemporaryUploadRepository(repo)
  83. if err != nil {
  84. return nil, err
  85. }
  86. defer t.Close()
  87. if err := t.Clone(opts.OldBranch); err != nil {
  88. return nil, err
  89. }
  90. if err := t.SetDefaultIndex(); err != nil {
  91. return nil, err
  92. }
  93. // Get the commit of the original branch
  94. commit, err := t.GetBranchCommit(opts.OldBranch)
  95. if err != nil {
  96. return nil, err // Couldn't get a commit for the branch
  97. }
  98. // Assigned LastCommitID in opts if it hasn't been set
  99. if opts.LastCommitID == "" {
  100. opts.LastCommitID = commit.ID.String()
  101. } else {
  102. lastCommitID, err := t.gitRepo.ConvertToSHA1(opts.LastCommitID)
  103. if err != nil {
  104. return nil, fmt.Errorf("DeleteRepoFile: Invalid last commit ID: %v", err)
  105. }
  106. opts.LastCommitID = lastCommitID.String()
  107. }
  108. // Get the files in the index
  109. filesInIndex, err := t.LsFiles(opts.TreePath)
  110. if err != nil {
  111. return nil, fmt.Errorf("DeleteRepoFile: %v", err)
  112. }
  113. // Find the file we want to delete in the index
  114. inFilelist := false
  115. for _, file := range filesInIndex {
  116. if file == opts.TreePath {
  117. inFilelist = true
  118. break
  119. }
  120. }
  121. if !inFilelist {
  122. return nil, models.ErrRepoFileDoesNotExist{
  123. Path: opts.TreePath,
  124. }
  125. }
  126. // Get the entry of treePath and check if the SHA given is the same as the file
  127. entry, err := commit.GetTreeEntryByPath(treePath)
  128. if err != nil {
  129. return nil, err
  130. }
  131. if opts.SHA != "" {
  132. // If a SHA was given and the SHA given doesn't match the SHA of the fromTreePath, throw error
  133. if opts.SHA != entry.ID.String() {
  134. return nil, models.ErrSHADoesNotMatch{
  135. Path: treePath,
  136. GivenSHA: opts.SHA,
  137. CurrentSHA: entry.ID.String(),
  138. }
  139. }
  140. } else if opts.LastCommitID != "" {
  141. // If a lastCommitID was given and it doesn't match the commitID of the head of the branch throw
  142. // an error, but only if we aren't creating a new branch.
  143. if commit.ID.String() != opts.LastCommitID && opts.OldBranch == opts.NewBranch {
  144. // CommitIDs don't match, but we don't want to throw a ErrCommitIDDoesNotMatch unless
  145. // this specific file has been edited since opts.LastCommitID
  146. if changed, err := commit.FileChangedSinceCommit(treePath, opts.LastCommitID); err != nil {
  147. return nil, err
  148. } else if changed {
  149. return nil, models.ErrCommitIDDoesNotMatch{
  150. GivenCommitID: opts.LastCommitID,
  151. CurrentCommitID: opts.LastCommitID,
  152. }
  153. }
  154. // The file wasn't modified, so we are good to delete it
  155. }
  156. } else {
  157. // When deleting a file, a lastCommitID or SHA needs to be given to make sure other commits haven't been
  158. // made. We throw an error if one wasn't provided.
  159. return nil, models.ErrSHAOrCommitIDNotProvided{}
  160. }
  161. // Remove the file from the index
  162. if err := t.RemoveFilesFromIndex(opts.TreePath); err != nil {
  163. return nil, err
  164. }
  165. // Now write the tree
  166. treeHash, err := t.WriteTree()
  167. if err != nil {
  168. return nil, err
  169. }
  170. // Now commit the tree
  171. var commitHash string
  172. if opts.Dates != nil {
  173. commitHash, err = t.CommitTreeWithDate(author, committer, treeHash, message, opts.Dates.Author, opts.Dates.Committer)
  174. } else {
  175. commitHash, err = t.CommitTree(author, committer, treeHash, message)
  176. }
  177. if err != nil {
  178. return nil, err
  179. }
  180. // Then push this tree to NewBranch
  181. if err := t.Push(doer, commitHash, opts.NewBranch); err != nil {
  182. return nil, err
  183. }
  184. commit, err = t.GetCommit(commitHash)
  185. if err != nil {
  186. return nil, err
  187. }
  188. file, err := GetFileResponseFromCommit(repo, commit, opts.NewBranch, treePath)
  189. if err != nil {
  190. return nil, err
  191. }
  192. return file, nil
  193. }