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