本站源代码
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

192 lines
5.3KB

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