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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 uploader
  5. import (
  6. "fmt"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/git"
  9. )
  10. // DeleteRepoFileOptions holds the repository delete file options
  11. type DeleteRepoFileOptions struct {
  12. LastCommitID string
  13. OldBranch string
  14. NewBranch string
  15. TreePath string
  16. Message string
  17. }
  18. // DeleteRepoFile deletes a file in the given repository
  19. func DeleteRepoFile(repo *models.Repository, doer *models.User, opts *DeleteRepoFileOptions) error {
  20. t, err := NewTemporaryUploadRepository(repo)
  21. defer t.Close()
  22. if err != nil {
  23. return err
  24. }
  25. if err := t.Clone(opts.OldBranch); err != nil {
  26. return err
  27. }
  28. if err := t.SetDefaultIndex(); err != nil {
  29. return err
  30. }
  31. filesInIndex, err := t.LsFiles(opts.TreePath)
  32. if err != nil {
  33. return fmt.Errorf("UpdateRepoFile: %v", err)
  34. }
  35. inFilelist := false
  36. for _, file := range filesInIndex {
  37. if file == opts.TreePath {
  38. inFilelist = true
  39. }
  40. }
  41. if !inFilelist {
  42. return git.ErrNotExist{RelPath: opts.TreePath}
  43. }
  44. if err := t.RemoveFilesFromIndex(opts.TreePath); err != nil {
  45. return err
  46. }
  47. // Now write the tree
  48. treeHash, err := t.WriteTree()
  49. if err != nil {
  50. return err
  51. }
  52. // Now commit the tree
  53. commitHash, err := t.CommitTree(doer, treeHash, opts.Message)
  54. if err != nil {
  55. return err
  56. }
  57. // Then push this tree to NewBranch
  58. if err := t.Push(doer, commitHash, opts.NewBranch); err != nil {
  59. return err
  60. }
  61. // Simulate push event.
  62. oldCommitID := opts.LastCommitID
  63. if opts.NewBranch != opts.OldBranch {
  64. oldCommitID = git.EmptySHA
  65. }
  66. if err = repo.GetOwner(); err != nil {
  67. return fmt.Errorf("GetOwner: %v", err)
  68. }
  69. err = models.PushUpdate(
  70. opts.NewBranch,
  71. models.PushUpdateOptions{
  72. PusherID: doer.ID,
  73. PusherName: doer.Name,
  74. RepoUserName: repo.Owner.Name,
  75. RepoName: repo.Name,
  76. RefFullName: git.BranchPrefix + opts.NewBranch,
  77. OldCommitID: oldCommitID,
  78. NewCommitID: commitHash,
  79. },
  80. )
  81. if err != nil {
  82. return fmt.Errorf("PushUpdate: %v", err)
  83. }
  84. // FIXME: Should we UpdateRepoIndexer(repo) here?
  85. return nil
  86. }