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.6KB

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