From fee0e4dbeac842e606846eda1d311fed352da3b8 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 16 Jul 2022 08:10:02 +0800 Subject: Remove confusing TrimPrefix(... git.BranchPrefix) (#20369) Make Repository.GetDefaultBranch return the real branch name, instead of the ref name. Then there is no need to do TrimPrefix for repo.DefaultBranch --- modules/git/repo_branch.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'modules/git') diff --git a/modules/git/repo_branch.go b/modules/git/repo_branch.go index 8e455480e7..17d243808e 100644 --- a/modules/git/repo_branch.go +++ b/modules/git/repo_branch.go @@ -7,6 +7,7 @@ package git import ( "context" + "errors" "fmt" "strings" ) @@ -72,7 +73,14 @@ func (repo *Repository) SetDefaultBranch(name string) error { // GetDefaultBranch gets default branch of repository. func (repo *Repository) GetDefaultBranch() (string, error) { stdout, _, err := NewCommand(repo.Ctx, "symbolic-ref", "HEAD").RunStdString(&RunOpts{Dir: repo.Path}) - return stdout, err + if err != nil { + return "", err + } + stdout = strings.TrimSpace(stdout) + if !strings.HasPrefix(stdout, BranchPrefix) { + return "", errors.New("the HEAD is not a branch: " + stdout) + } + return strings.TrimPrefix(stdout, BranchPrefix), nil } // GetBranch returns a branch by it's name -- cgit v1.2.3