gitea源码

api_repo_file_helpers.go 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "context"
  6. "strings"
  7. repo_model "code.gitea.io/gitea/models/repo"
  8. user_model "code.gitea.io/gitea/models/user"
  9. api "code.gitea.io/gitea/modules/structs"
  10. files_service "code.gitea.io/gitea/services/repository/files"
  11. )
  12. func createFileInBranch(user *user_model.User, repo *repo_model.Repository, treePath, branchName, content string) (*api.FilesResponse, error) {
  13. ctx := context.TODO()
  14. opts := &files_service.ChangeRepoFilesOptions{
  15. Files: []*files_service.ChangeRepoFile{
  16. {
  17. Operation: "create",
  18. TreePath: treePath,
  19. ContentReader: strings.NewReader(content),
  20. },
  21. },
  22. OldBranch: branchName,
  23. Author: nil,
  24. Committer: nil,
  25. }
  26. return files_service.ChangeRepoFiles(ctx, repo, user, opts)
  27. }
  28. func deleteFileInBranch(user *user_model.User, repo *repo_model.Repository, treePath, branchName string) (*api.FilesResponse, error) {
  29. ctx := context.TODO()
  30. opts := &files_service.ChangeRepoFilesOptions{
  31. Files: []*files_service.ChangeRepoFile{
  32. {
  33. Operation: "delete",
  34. TreePath: treePath,
  35. },
  36. },
  37. OldBranch: branchName,
  38. Author: nil,
  39. Committer: nil,
  40. }
  41. return files_service.ChangeRepoFiles(ctx, repo, user, opts)
  42. }
  43. func createOrReplaceFileInBranch(user *user_model.User, repo *repo_model.Repository, treePath, branchName, content string) error {
  44. _, err := deleteFileInBranch(user, repo, treePath, branchName)
  45. if err != nil && !files_service.IsErrRepoFileDoesNotExist(err) {
  46. return err
  47. }
  48. _, err = createFileInBranch(user, repo, treePath, branchName, content)
  49. return err
  50. }
  51. func createFile(user *user_model.User, repo *repo_model.Repository, treePath string) (*api.FilesResponse, error) {
  52. return createFileInBranch(user, repo, treePath, repo.DefaultBranch, "This is a NEW file")
  53. }