gitea源码

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package git
  5. import (
  6. "context"
  7. "errors"
  8. "strings"
  9. "code.gitea.io/gitea/modules/git/gitcmd"
  10. )
  11. // BranchPrefix base dir of the branch information file store on git
  12. const BranchPrefix = "refs/heads/"
  13. // IsReferenceExist returns true if given reference exists in the repository.
  14. func IsReferenceExist(ctx context.Context, repoPath, name string) bool {
  15. _, _, err := gitcmd.NewCommand("show-ref", "--verify").AddDashesAndList(name).RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath})
  16. return err == nil
  17. }
  18. // IsBranchExist returns true if given branch exists in the repository.
  19. func IsBranchExist(ctx context.Context, repoPath, name string) bool {
  20. return IsReferenceExist(ctx, repoPath, BranchPrefix+name)
  21. }
  22. func GetDefaultBranch(ctx context.Context, repoPath string) (string, error) {
  23. stdout, _, err := gitcmd.NewCommand("symbolic-ref", "HEAD").RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath})
  24. if err != nil {
  25. return "", err
  26. }
  27. stdout = strings.TrimSpace(stdout)
  28. if !strings.HasPrefix(stdout, BranchPrefix) {
  29. return "", errors.New("the HEAD is not a branch: " + stdout)
  30. }
  31. return strings.TrimPrefix(stdout, BranchPrefix), nil
  32. }
  33. // DeleteBranchOptions Option(s) for delete branch
  34. type DeleteBranchOptions struct {
  35. Force bool
  36. }
  37. // DeleteBranch delete a branch by name on repository.
  38. func (repo *Repository) DeleteBranch(name string, opts DeleteBranchOptions) error {
  39. cmd := gitcmd.NewCommand("branch")
  40. if opts.Force {
  41. cmd.AddArguments("-D")
  42. } else {
  43. cmd.AddArguments("-d")
  44. }
  45. cmd.AddDashesAndList(name)
  46. _, _, err := cmd.RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path})
  47. return err
  48. }
  49. // CreateBranch create a new branch
  50. func (repo *Repository) CreateBranch(branch, oldbranchOrCommit string) error {
  51. cmd := gitcmd.NewCommand("branch")
  52. cmd.AddDashesAndList(branch, oldbranchOrCommit)
  53. _, _, err := cmd.RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path})
  54. return err
  55. }
  56. // AddRemote adds a new remote to repository.
  57. func (repo *Repository) AddRemote(name, url string, fetch bool) error {
  58. cmd := gitcmd.NewCommand("remote", "add")
  59. if fetch {
  60. cmd.AddArguments("-f")
  61. }
  62. cmd.AddDynamicArguments(name, url)
  63. _, _, err := cmd.RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path})
  64. return err
  65. }
  66. // RenameBranch rename a branch
  67. func (repo *Repository) RenameBranch(from, to string) error {
  68. _, _, err := gitcmd.NewCommand("branch", "-m").AddDynamicArguments(from, to).RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path})
  69. return err
  70. }