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

Refactor editor upload, update and delete to use git plumbing and add LFS support (#5702) * Use git plumbing for upload: #5621 repo_editor.go: UploadRepoFile * Use git plumbing for upload: #5621 repo_editor.go: GetDiffPreview * Use git plumbing for upload: #5621 repo_editor.go: DeleteRepoFile * Use git plumbing for upload: #5621 repo_editor.go: UploadRepoFiles * Move branch checkout functions out of repo_editor.go as they are no longer used there * BUGFIX: The default permissions should be 100644 This is a change from the previous code but is more in keeping with the default behaviour of git. Signed-off-by: Andrew Thornton <art27@cantab.net> * Standardise cleanUploadFilename to more closely match git See verify_path in: https://github.com/git/git/blob/7f4e64169352e03476b0ea64e7e2973669e491a2/read-cache.c#L951 Signed-off-by: Andrew Thornton <art27@cantab.net> * Redirect on bad paths Signed-off-by: Andrew Thornton <art27@cantab.net> * Refactor to move the uploading functions out to a module Signed-off-by: Andrew Thornton <art27@cantab.net> * Add LFS support Signed-off-by: Andrew Thornton <art27@cantab.net> * Update upload.go attribution header Upload.go is essentially the remnants of repo_editor.go. The remaining code is essentially unchanged from the Gogs code, hence the Gogs attribution. * Delete upload files after session committed * Ensure that GIT_AUTHOR_NAME etc. are valid for git see #5774 Signed-off-by: Andrew Thornton <art27@cantab.net> * Add in test cases per @lafriks comment * Add space between gitea and github imports Signed-off-by: Andrew Thornton <art27@cantab.net> * more examples in TestCleanUploadName Signed-off-by: Andrew Thornton <art27@cantab.net> * fix formatting Signed-off-by: Andrew Thornton <art27@cantab.net> * Set the SSH_ORIGINAL_COMMAND to ensure hooks are run Signed-off-by: Andrew Thornton <art27@cantab.net> * Switch off SSH_ORIGINAL_COMMAND Signed-off-by: Andrew Thornton <art27@cantab.net>
6 years ago
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/git"
  8. "code.gitea.io/gitea/models"
  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. }