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 5.7KB

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