gitea源码

branch.go 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package gitrepo
  4. import (
  5. "context"
  6. "code.gitea.io/gitea/modules/git"
  7. "code.gitea.io/gitea/modules/git/gitcmd"
  8. )
  9. // GetBranchesByPath returns a branch by its path
  10. // if limit = 0 it will not limit
  11. func GetBranchesByPath(ctx context.Context, repo Repository, skip, limit int) ([]string, int, error) {
  12. gitRepo, err := OpenRepository(ctx, repo)
  13. if err != nil {
  14. return nil, 0, err
  15. }
  16. defer gitRepo.Close()
  17. return gitRepo.GetBranchNames(skip, limit)
  18. }
  19. func GetBranchCommitID(ctx context.Context, repo Repository, branch string) (string, error) {
  20. gitRepo, err := OpenRepository(ctx, repo)
  21. if err != nil {
  22. return "", err
  23. }
  24. defer gitRepo.Close()
  25. return gitRepo.GetBranchCommitID(branch)
  26. }
  27. // SetDefaultBranch sets default branch of repository.
  28. func SetDefaultBranch(ctx context.Context, repo Repository, name string) error {
  29. _, _, err := gitcmd.NewCommand("symbolic-ref", "HEAD").
  30. AddDynamicArguments(git.BranchPrefix+name).
  31. RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)})
  32. return err
  33. }
  34. // GetDefaultBranch gets default branch of repository.
  35. func GetDefaultBranch(ctx context.Context, repo Repository) (string, error) {
  36. return git.GetDefaultBranch(ctx, repoPath(repo))
  37. }
  38. // IsReferenceExist returns true if given reference exists in the repository.
  39. func IsReferenceExist(ctx context.Context, repo Repository, name string) bool {
  40. return git.IsReferenceExist(ctx, repoPath(repo), name)
  41. }
  42. // IsBranchExist returns true if given branch exists in the repository.
  43. func IsBranchExist(ctx context.Context, repo Repository, name string) bool {
  44. return IsReferenceExist(ctx, repo, git.BranchPrefix+name)
  45. }