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.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 git.IsErrNotExist(err) {
  44. return nil, err
  45. }
  46. if newBranch != nil {
  47. return nil, models.ErrBranchAlreadyExists{
  48. BranchName: opts.NewBranch,
  49. }
  50. }
  51. } else if protected, _ := repo.IsProtectedBranchForPush(opts.OldBranch, doer); protected {
  52. return nil, models.ErrUserCannotCommit{
  53. UserName: doer.LowerName,
  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.Author, opts.Committer, doer)
  65. t, err := NewTemporaryUploadRepository(repo)
  66. if err != nil {
  67. return nil, err
  68. }
  69. defer t.Close()
  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. } else {
  85. lastCommitID, err := t.gitRepo.ConvertToSHA1(opts.LastCommitID)
  86. if err != nil {
  87. return nil, fmt.Errorf("DeleteRepoFile: Invalid last commit ID: %v", err)
  88. }
  89. opts.LastCommitID = lastCommitID.String()
  90. }
  91. // Get the files in the index
  92. filesInIndex, err := t.LsFiles(opts.TreePath)
  93. if err != nil {
  94. return nil, fmt.Errorf("DeleteRepoFile: %v", err)
  95. }
  96. // Find the file we want to delete in the index
  97. inFilelist := false
  98. for _, file := range filesInIndex {
  99. if file == opts.TreePath {
  100. inFilelist = true
  101. break
  102. }
  103. }
  104. if !inFilelist {
  105. return nil, models.ErrRepoFileDoesNotExist{
  106. Path: opts.TreePath,
  107. }
  108. }
  109. // Get the entry of treePath and check if the SHA given is the same as the file
  110. entry, err := commit.GetTreeEntryByPath(treePath)
  111. if err != nil {
  112. return nil, err
  113. }
  114. if opts.SHA != "" {
  115. // If a SHA was given and the SHA given doesn't match the SHA of the fromTreePath, throw error
  116. if opts.SHA != entry.ID.String() {
  117. return nil, models.ErrSHADoesNotMatch{
  118. Path: treePath,
  119. GivenSHA: opts.SHA,
  120. CurrentSHA: entry.ID.String(),
  121. }
  122. }
  123. } else if opts.LastCommitID != "" {
  124. // If a lastCommitID was given and it doesn't match the commitID of the head of the branch throw
  125. // an error, but only if we aren't creating a new branch.
  126. if commit.ID.String() != opts.LastCommitID && opts.OldBranch == opts.NewBranch {
  127. // CommitIDs don't match, but we don't want to throw a ErrCommitIDDoesNotMatch unless
  128. // this specific file has been edited since opts.LastCommitID
  129. if changed, err := commit.FileChangedSinceCommit(treePath, opts.LastCommitID); err != nil {
  130. return nil, err
  131. } else if changed {
  132. return nil, models.ErrCommitIDDoesNotMatch{
  133. GivenCommitID: opts.LastCommitID,
  134. CurrentCommitID: opts.LastCommitID,
  135. }
  136. }
  137. // The file wasn't modified, so we are good to delete it
  138. }
  139. } else {
  140. // When deleting a file, a lastCommitID or SHA needs to be given to make sure other commits haven't been
  141. // made. We throw an error if one wasn't provided.
  142. return nil, models.ErrSHAOrCommitIDNotProvided{}
  143. }
  144. // Remove the file from the index
  145. if err := t.RemoveFilesFromIndex(opts.TreePath); err != nil {
  146. return nil, err
  147. }
  148. // Now write the tree
  149. treeHash, err := t.WriteTree()
  150. if err != nil {
  151. return nil, err
  152. }
  153. // Now commit the tree
  154. var commitHash string
  155. if opts.Dates != nil {
  156. commitHash, err = t.CommitTreeWithDate(author, committer, treeHash, message, opts.Dates.Author, opts.Dates.Committer)
  157. } else {
  158. commitHash, err = t.CommitTree(author, committer, treeHash, message)
  159. }
  160. if err != nil {
  161. return nil, err
  162. }
  163. // Then push this tree to NewBranch
  164. if err := t.Push(doer, commitHash, opts.NewBranch); err != nil {
  165. return nil, err
  166. }
  167. commit, err = t.GetCommit(commitHash)
  168. if err != nil {
  169. return nil, err
  170. }
  171. file, err := GetFileResponseFromCommit(repo, commit, opts.NewBranch, treePath)
  172. if err != nil {
  173. return nil, err
  174. }
  175. return file, nil
  176. }