gitea源码

editor_util.go 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2025 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "context"
  6. "fmt"
  7. "path"
  8. "strings"
  9. git_model "code.gitea.io/gitea/models/git"
  10. repo_model "code.gitea.io/gitea/models/repo"
  11. user_model "code.gitea.io/gitea/models/user"
  12. "code.gitea.io/gitea/modules/git"
  13. "code.gitea.io/gitea/modules/json"
  14. "code.gitea.io/gitea/modules/log"
  15. repo_module "code.gitea.io/gitea/modules/repository"
  16. context_service "code.gitea.io/gitea/services/context"
  17. )
  18. // getUniquePatchBranchName Gets a unique branch name for a new patch branch
  19. // It will be in the form of <username>-patch-<num> where <num> is the first branch of this format
  20. // that doesn't already exist. If we exceed 1000 tries or an error is thrown, we just return "" so the user has to
  21. // type in the branch name themselves (will be an empty field)
  22. func getUniquePatchBranchName(ctx context.Context, prefixName string, repo *repo_model.Repository) string {
  23. prefix := prefixName + "-patch-"
  24. for i := 1; i <= 1000; i++ {
  25. branchName := fmt.Sprintf("%s%d", prefix, i)
  26. if exist, err := git_model.IsBranchExist(ctx, repo.ID, branchName); err != nil {
  27. log.Error("getUniquePatchBranchName: %v", err)
  28. return ""
  29. } else if !exist {
  30. return branchName
  31. }
  32. }
  33. return ""
  34. }
  35. // getClosestParentWithFiles Recursively gets the closest path of parent in a tree that has files when a file in a tree is
  36. // deleted. It returns "" for the tree root if no parents other than the root have files.
  37. func getClosestParentWithFiles(gitRepo *git.Repository, branchName, originTreePath string) string {
  38. var f func(treePath string, commit *git.Commit) string
  39. f = func(treePath string, commit *git.Commit) string {
  40. if treePath == "" || treePath == "." {
  41. return ""
  42. }
  43. // see if the tree has entries
  44. if tree, err := commit.SubTree(treePath); err != nil {
  45. return f(path.Dir(treePath), commit) // failed to get the tree, going up a dir
  46. } else if entries, err := tree.ListEntries(); err != nil || len(entries) == 0 {
  47. return f(path.Dir(treePath), commit) // no files in this dir, going up a dir
  48. }
  49. return treePath
  50. }
  51. commit, err := gitRepo.GetBranchCommit(branchName) // must get the commit again to get the latest change
  52. if err != nil {
  53. log.Error("GetBranchCommit: %v", err)
  54. return ""
  55. }
  56. return f(originTreePath, commit)
  57. }
  58. // getContextRepoEditorConfig returns the editorconfig JSON string for given treePath or "null"
  59. func getContextRepoEditorConfig(ctx *context_service.Context, treePath string) string {
  60. ec, _, err := ctx.Repo.GetEditorconfig()
  61. if err == nil {
  62. def, err := ec.GetDefinitionForFilename(treePath)
  63. if err == nil {
  64. jsonStr, _ := json.Marshal(def)
  65. return string(jsonStr)
  66. }
  67. }
  68. return "null"
  69. }
  70. // getParentTreeFields returns list of parent tree names and corresponding tree paths based on given treePath.
  71. // eg: []{"a", "b", "c"}, []{"a", "a/b", "a/b/c"}
  72. // or: []{""}, []{""} for the root treePath
  73. func getParentTreeFields(treePath string) (treeNames, treePaths []string) {
  74. treeNames = strings.Split(treePath, "/")
  75. treePaths = make([]string, len(treeNames))
  76. for i := range treeNames {
  77. treePaths[i] = strings.Join(treeNames[:i+1], "/")
  78. }
  79. return treeNames, treePaths
  80. }
  81. // getUniqueRepositoryName Gets a unique repository name for a user
  82. // It will append a -<num> postfix if the name is already taken
  83. func getUniqueRepositoryName(ctx context.Context, ownerID int64, name string) string {
  84. uniqueName := name
  85. for i := 1; i < 1000; i++ {
  86. _, err := repo_model.GetRepositoryByName(ctx, ownerID, uniqueName)
  87. if err != nil || repo_model.IsErrRepoNotExist(err) {
  88. return uniqueName
  89. }
  90. uniqueName = fmt.Sprintf("%s-%d", name, i)
  91. i++
  92. }
  93. return ""
  94. }
  95. func editorPushBranchToForkedRepository(ctx context.Context, doer *user_model.User, baseRepo *repo_model.Repository, baseBranchName string, targetRepo *repo_model.Repository, targetBranchName string) error {
  96. return git.Push(ctx, baseRepo.RepoPath(), git.PushOptions{
  97. Remote: targetRepo.RepoPath(),
  98. Branch: baseBranchName + ":" + targetBranchName,
  99. Env: repo_module.PushingEnvironment(doer, targetRepo),
  100. })
  101. }