You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

delete.go 5.9 kB

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