// Copyright 2024 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT package git import ( "context" "gotea.dev/modules/git/gitrepo" "gotea.dev/modules/got" ) // GetBranchesByPath returns a branch by its path // if limit = 0 it will not limit func GetBranchesByPath(ctx context.Context, repo RepositoryFacade, skip, limit int) ([]string, int, error) { gitRepo, err := OpenRepository(ctx, repo) if err != nil { return nil, 0, err } defer gitRepo.Close() return gitRepo.GetBranchNames(ctx, skip, limit) } func GetBranchCommitID(ctx context.Context, repo RepositoryFacade, branch string) (string, error) { return got.BranchCommitID(ctx, gitrepo.RepoLocalPath(repo), branch) } // SetDefaultBranch sets default branch of repository. func SetDefaultBranch(ctx context.Context, repo RepositoryFacade, name string) error { return got.SetDefaultBranch(ctx, gitrepo.RepoLocalPath(repo), name) } // GetDefaultBranch gets default branch of repository. func GetDefaultBranch(ctx context.Context, repo RepositoryFacade) (string, error) { return got.DefaultBranch(ctx, gitrepo.RepoLocalPath(repo)) } // IsReferenceExist returns true if the given reference name or object ID exists // in the repository. It accepts full/abbreviated object IDs, refs and tags; // it does not resolve path names. func IsReferenceExist(ctx context.Context, repo RepositoryFacade, name string) bool { if name == "" { return false } exists, err := got.ObjectExists(ctx, gitrepo.RepoLocalPath(repo), name) if err != nil { return false } return exists } // IsBranchExist returns true if given branch exists in the repository. func IsBranchExist(ctx context.Context, repo RepositoryFacade, name string) bool { return IsReferenceExist(ctx, repo, BranchPrefix+name) } // DeleteBranch delete a branch by name on repository. // // force is accepted for signature compatibility and ignored: got has no // equivalent of git's "branch -d refuses unmerged", and every caller in gotea // passes true. The deletion goes through got.DeleteBranchRef, which only ever // touches refs/heads/. func DeleteBranch(ctx context.Context, repo RepositoryFacade, name string, force bool) error { _ = force return got.DeleteBranchRef(ctx, gitrepo.RepoLocalPath(repo), name) } // CreateBranch create a new branch func CreateBranch(ctx context.Context, repo RepositoryFacade, branch, oldbranchOrCommit string) error { return got.CreateBranch(ctx, gitrepo.RepoLocalPath(repo), branch, oldbranchOrCommit) } // RenameBranch rename a branch func RenameBranch(ctx context.Context, repo RepositoryFacade, from, to string) error { return got.RenameBranch(ctx, gitrepo.RepoLocalPath(repo), from, to) }