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.

150 lines
4.4 KiB

Improve listing performance by using go-git (#6478) * Use go-git for tree reading and commit info lookup. Signed-off-by: Filip Navara <navara@emclient.com> * Use TreeEntry.IsRegular() instead of ObjectType that was removed. Signed-off-by: Filip Navara <navara@emclient.com> * Use the treePath to optimize commit info search. Signed-off-by: Filip Navara <navara@emclient.com> * Extract the latest commit at treePath along with the other commits. Signed-off-by: Filip Navara <navara@emclient.com> * Fix listing commit info for a directory that was created in one commit and never modified after. Signed-off-by: Filip Navara <navara@emclient.com> * Avoid nearly all external 'git' invocations when doing directory listing (.editorconfig code path is still hit). Signed-off-by: Filip Navara <navara@emclient.com> * Use go-git for reading blobs. Signed-off-by: Filip Navara <navara@emclient.com> * Make SHA1 type alias for plumbing.Hash in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Make Signature type alias for object.Signature in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Fix GetCommitsInfo for repository with only one commit. Signed-off-by: Filip Navara <navara@emclient.com> * Fix PGP signature verification. Signed-off-by: Filip Navara <navara@emclient.com> * Fix issues with walking commit graph across merges. Signed-off-by: Filip Navara <navara@emclient.com> * Fix typo in condition. Signed-off-by: Filip Navara <navara@emclient.com> * Speed up loading branch list by keeping the repository reference (and thus all the loaded packfile indexes). Signed-off-by: Filip Navara <navara@emclient.com> * Fix lising submodules. Signed-off-by: Filip Navara <navara@emclient.com> * Fix build Signed-off-by: Filip Navara <navara@emclient.com> * Add back commit cache because of name-rev Signed-off-by: Filip Navara <navara@emclient.com> * Fix tests Signed-off-by: Filip Navara <navara@emclient.com> * Fix code style * Fix spelling * Address PR feedback Signed-off-by: Filip Navara <navara@emclient.com> * Update vendor module list Signed-off-by: Filip Navara <navara@emclient.com> * Fix getting trees by commit id Signed-off-by: Filip Navara <navara@emclient.com> * Fix remaining unit test failures * Fix GetTreeBySHA * Avoid running `git name-rev` if not necessary Signed-off-by: Filip Navara <navara@emclient.com> * Move Branch code to git module * Clean up GPG signature verification and fix it for tagged commits * Address PR feedback (import formatting, copyright headers) * Make blob lookup by SHA working * Update tests to use public API * Allow getting content from any type of object through the blob interface * Change test to actually expect the object content that is in the GIT repository * Change one more test to actually expect the object content that is in the GIT repository * Add comments
5 years ago
8 years ago
8 years ago
Improve listing performance by using go-git (#6478) * Use go-git for tree reading and commit info lookup. Signed-off-by: Filip Navara <navara@emclient.com> * Use TreeEntry.IsRegular() instead of ObjectType that was removed. Signed-off-by: Filip Navara <navara@emclient.com> * Use the treePath to optimize commit info search. Signed-off-by: Filip Navara <navara@emclient.com> * Extract the latest commit at treePath along with the other commits. Signed-off-by: Filip Navara <navara@emclient.com> * Fix listing commit info for a directory that was created in one commit and never modified after. Signed-off-by: Filip Navara <navara@emclient.com> * Avoid nearly all external 'git' invocations when doing directory listing (.editorconfig code path is still hit). Signed-off-by: Filip Navara <navara@emclient.com> * Use go-git for reading blobs. Signed-off-by: Filip Navara <navara@emclient.com> * Make SHA1 type alias for plumbing.Hash in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Make Signature type alias for object.Signature in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Fix GetCommitsInfo for repository with only one commit. Signed-off-by: Filip Navara <navara@emclient.com> * Fix PGP signature verification. Signed-off-by: Filip Navara <navara@emclient.com> * Fix issues with walking commit graph across merges. Signed-off-by: Filip Navara <navara@emclient.com> * Fix typo in condition. Signed-off-by: Filip Navara <navara@emclient.com> * Speed up loading branch list by keeping the repository reference (and thus all the loaded packfile indexes). Signed-off-by: Filip Navara <navara@emclient.com> * Fix lising submodules. Signed-off-by: Filip Navara <navara@emclient.com> * Fix build Signed-off-by: Filip Navara <navara@emclient.com> * Add back commit cache because of name-rev Signed-off-by: Filip Navara <navara@emclient.com> * Fix tests Signed-off-by: Filip Navara <navara@emclient.com> * Fix code style * Fix spelling * Address PR feedback Signed-off-by: Filip Navara <navara@emclient.com> * Update vendor module list Signed-off-by: Filip Navara <navara@emclient.com> * Fix getting trees by commit id Signed-off-by: Filip Navara <navara@emclient.com> * Fix remaining unit test failures * Fix GetTreeBySHA * Avoid running `git name-rev` if not necessary Signed-off-by: Filip Navara <navara@emclient.com> * Move Branch code to git module * Clean up GPG signature verification and fix it for tagged commits * Address PR feedback (import formatting, copyright headers) * Make blob lookup by SHA working * Update tests to use public API * Allow getting content from any type of object through the blob interface * Change test to actually expect the object content that is in the GIT repository * Change one more test to actually expect the object content that is in the GIT repository * Add comments
5 years ago
Improve listing performance by using go-git (#6478) * Use go-git for tree reading and commit info lookup. Signed-off-by: Filip Navara <navara@emclient.com> * Use TreeEntry.IsRegular() instead of ObjectType that was removed. Signed-off-by: Filip Navara <navara@emclient.com> * Use the treePath to optimize commit info search. Signed-off-by: Filip Navara <navara@emclient.com> * Extract the latest commit at treePath along with the other commits. Signed-off-by: Filip Navara <navara@emclient.com> * Fix listing commit info for a directory that was created in one commit and never modified after. Signed-off-by: Filip Navara <navara@emclient.com> * Avoid nearly all external 'git' invocations when doing directory listing (.editorconfig code path is still hit). Signed-off-by: Filip Navara <navara@emclient.com> * Use go-git for reading blobs. Signed-off-by: Filip Navara <navara@emclient.com> * Make SHA1 type alias for plumbing.Hash in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Make Signature type alias for object.Signature in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Fix GetCommitsInfo for repository with only one commit. Signed-off-by: Filip Navara <navara@emclient.com> * Fix PGP signature verification. Signed-off-by: Filip Navara <navara@emclient.com> * Fix issues with walking commit graph across merges. Signed-off-by: Filip Navara <navara@emclient.com> * Fix typo in condition. Signed-off-by: Filip Navara <navara@emclient.com> * Speed up loading branch list by keeping the repository reference (and thus all the loaded packfile indexes). Signed-off-by: Filip Navara <navara@emclient.com> * Fix lising submodules. Signed-off-by: Filip Navara <navara@emclient.com> * Fix build Signed-off-by: Filip Navara <navara@emclient.com> * Add back commit cache because of name-rev Signed-off-by: Filip Navara <navara@emclient.com> * Fix tests Signed-off-by: Filip Navara <navara@emclient.com> * Fix code style * Fix spelling * Address PR feedback Signed-off-by: Filip Navara <navara@emclient.com> * Update vendor module list Signed-off-by: Filip Navara <navara@emclient.com> * Fix getting trees by commit id Signed-off-by: Filip Navara <navara@emclient.com> * Fix remaining unit test failures * Fix GetTreeBySHA * Avoid running `git name-rev` if not necessary Signed-off-by: Filip Navara <navara@emclient.com> * Move Branch code to git module * Clean up GPG signature verification and fix it for tagged commits * Address PR feedback (import formatting, copyright headers) * Make blob lookup by SHA working * Update tests to use public API * Allow getting content from any type of object through the blob interface * Change test to actually expect the object content that is in the GIT repository * Change one more test to actually expect the object content that is in the GIT repository * Add comments
5 years ago
Improve listing performance by using go-git (#6478) * Use go-git for tree reading and commit info lookup. Signed-off-by: Filip Navara <navara@emclient.com> * Use TreeEntry.IsRegular() instead of ObjectType that was removed. Signed-off-by: Filip Navara <navara@emclient.com> * Use the treePath to optimize commit info search. Signed-off-by: Filip Navara <navara@emclient.com> * Extract the latest commit at treePath along with the other commits. Signed-off-by: Filip Navara <navara@emclient.com> * Fix listing commit info for a directory that was created in one commit and never modified after. Signed-off-by: Filip Navara <navara@emclient.com> * Avoid nearly all external 'git' invocations when doing directory listing (.editorconfig code path is still hit). Signed-off-by: Filip Navara <navara@emclient.com> * Use go-git for reading blobs. Signed-off-by: Filip Navara <navara@emclient.com> * Make SHA1 type alias for plumbing.Hash in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Make Signature type alias for object.Signature in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Fix GetCommitsInfo for repository with only one commit. Signed-off-by: Filip Navara <navara@emclient.com> * Fix PGP signature verification. Signed-off-by: Filip Navara <navara@emclient.com> * Fix issues with walking commit graph across merges. Signed-off-by: Filip Navara <navara@emclient.com> * Fix typo in condition. Signed-off-by: Filip Navara <navara@emclient.com> * Speed up loading branch list by keeping the repository reference (and thus all the loaded packfile indexes). Signed-off-by: Filip Navara <navara@emclient.com> * Fix lising submodules. Signed-off-by: Filip Navara <navara@emclient.com> * Fix build Signed-off-by: Filip Navara <navara@emclient.com> * Add back commit cache because of name-rev Signed-off-by: Filip Navara <navara@emclient.com> * Fix tests Signed-off-by: Filip Navara <navara@emclient.com> * Fix code style * Fix spelling * Address PR feedback Signed-off-by: Filip Navara <navara@emclient.com> * Update vendor module list Signed-off-by: Filip Navara <navara@emclient.com> * Fix getting trees by commit id Signed-off-by: Filip Navara <navara@emclient.com> * Fix remaining unit test failures * Fix GetTreeBySHA * Avoid running `git name-rev` if not necessary Signed-off-by: Filip Navara <navara@emclient.com> * Move Branch code to git module * Clean up GPG signature verification and fix it for tagged commits * Address PR feedback (import formatting, copyright headers) * Make blob lookup by SHA working * Update tests to use public API * Allow getting content from any type of object through the blob interface * Change test to actually expect the object content that is in the GIT repository * Change one more test to actually expect the object content that is in the GIT repository * Add comments
5 years ago
8 years ago
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package models
  6. import (
  7. "fmt"
  8. "code.gitea.io/gitea/modules/git"
  9. "code.gitea.io/gitea/modules/log"
  10. )
  11. // CanCreateBranch returns true if repository meets the requirements for creating new branches.
  12. func (repo *Repository) CanCreateBranch() bool {
  13. return !repo.IsMirror
  14. }
  15. // GetBranch returns a branch by its name
  16. func (repo *Repository) GetBranch(branch string) (*git.Branch, error) {
  17. gitRepo, err := git.OpenRepository(repo.RepoPath())
  18. if err != nil {
  19. return nil, err
  20. }
  21. return gitRepo.GetBranch(branch)
  22. }
  23. // GetBranches returns all the branches of a repository
  24. func (repo *Repository) GetBranches() ([]*git.Branch, error) {
  25. return git.GetBranchesByPath(repo.RepoPath())
  26. }
  27. // CheckBranchName validates branch name with existing repository branches
  28. func (repo *Repository) CheckBranchName(name string) error {
  29. gitRepo, err := git.OpenRepository(repo.RepoPath())
  30. if err != nil {
  31. return err
  32. }
  33. branches, err := repo.GetBranches()
  34. if err != nil {
  35. return err
  36. }
  37. for _, branch := range branches {
  38. if branch.Name == name {
  39. return ErrBranchAlreadyExists{branch.Name}
  40. } else if (len(branch.Name) < len(name) && branch.Name+"/" == name[0:len(branch.Name)+1]) ||
  41. (len(branch.Name) > len(name) && name+"/" == branch.Name[0:len(name)+1]) {
  42. return ErrBranchNameConflict{branch.Name}
  43. }
  44. }
  45. if _, err := gitRepo.GetTag(name); err == nil {
  46. return ErrTagAlreadyExists{name}
  47. }
  48. return nil
  49. }
  50. // CreateNewBranch creates a new repository branch
  51. func (repo *Repository) CreateNewBranch(doer *User, oldBranchName, branchName string) (err error) {
  52. // Check if branch name can be used
  53. if err := repo.CheckBranchName(branchName); err != nil {
  54. return err
  55. }
  56. if !git.IsBranchExist(repo.RepoPath(), oldBranchName) {
  57. return fmt.Errorf("OldBranch: %s does not exist. Cannot create new branch from this", oldBranchName)
  58. }
  59. basePath, err := CreateTemporaryPath("branch-maker")
  60. if err != nil {
  61. return err
  62. }
  63. defer RemoveTemporaryPath(basePath)
  64. if err := git.Clone(repo.RepoPath(), basePath, git.CloneRepoOptions{
  65. Bare: true,
  66. Shared: true,
  67. }); err != nil {
  68. log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
  69. return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err)
  70. }
  71. gitRepo, err := git.OpenRepository(basePath)
  72. if err != nil {
  73. log.Error("Unable to open temporary repository: %s (%v)", basePath, err)
  74. return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err)
  75. }
  76. if err = gitRepo.CreateBranch(branchName, oldBranchName); err != nil {
  77. log.Error("Unable to create branch: %s from %s. (%v)", branchName, oldBranchName, err)
  78. return fmt.Errorf("Unable to create branch: %s from %s. (%v)", branchName, oldBranchName, err)
  79. }
  80. if err = git.Push(basePath, git.PushOptions{
  81. Remote: "origin",
  82. Branch: branchName,
  83. Env: PushingEnvironment(doer, repo),
  84. }); err != nil {
  85. return fmt.Errorf("Push: %v", err)
  86. }
  87. return nil
  88. }
  89. // CreateNewBranchFromCommit creates a new repository branch
  90. func (repo *Repository) CreateNewBranchFromCommit(doer *User, commit, branchName string) (err error) {
  91. // Check if branch name can be used
  92. if err := repo.CheckBranchName(branchName); err != nil {
  93. return err
  94. }
  95. basePath, err := CreateTemporaryPath("branch-maker")
  96. if err != nil {
  97. return err
  98. }
  99. defer RemoveTemporaryPath(basePath)
  100. if err := git.Clone(repo.RepoPath(), basePath, git.CloneRepoOptions{
  101. Bare: true,
  102. Shared: true,
  103. }); err != nil {
  104. log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
  105. return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err)
  106. }
  107. gitRepo, err := git.OpenRepository(basePath)
  108. if err != nil {
  109. log.Error("Unable to open temporary repository: %s (%v)", basePath, err)
  110. return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err)
  111. }
  112. if err = gitRepo.CreateBranch(branchName, commit); err != nil {
  113. log.Error("Unable to create branch: %s from %s. (%v)", branchName, commit, err)
  114. return fmt.Errorf("Unable to create branch: %s from %s. (%v)", branchName, commit, err)
  115. }
  116. if err = git.Push(basePath, git.PushOptions{
  117. Remote: "origin",
  118. Branch: branchName,
  119. Env: PushingEnvironment(doer, repo),
  120. }); err != nil {
  121. return fmt.Errorf("Push: %v", err)
  122. }
  123. return nil
  124. }