aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2022-07-16 08:10:02 +0800
committerGitHub <noreply@github.com>2022-07-16 08:10:02 +0800
commitfee0e4dbeac842e606846eda1d311fed352da3b8 (patch)
tree2d6daef4b8bc38266f298b6d296eafbf910539b8
parent57e0bf43eb3d93933aac351958599b6623ddf978 (diff)
downloadgitea-fee0e4dbeac842e606846eda1d311fed352da3b8.tar.gz
gitea-fee0e4dbeac842e606846eda1d311fed352da3b8.zip
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
-rw-r--r--modules/git/repo_branch.go10
-rw-r--r--services/repository/adopt.go2
2 files changed, 9 insertions, 3 deletions
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
diff --git a/services/repository/adopt.go b/services/repository/adopt.go
index 48f049cd28..6d6611c705 100644
--- a/services/repository/adopt.go
+++ b/services/repository/adopt.go
@@ -143,8 +143,6 @@ func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, r
return fmt.Errorf("setDefaultBranch: %v", err)
}
}
-
- repo.DefaultBranch = strings.TrimPrefix(repo.DefaultBranch, git.BranchPrefix)
}
branches, _, _ := gitRepo.GetBranchNames(0, 0)
found := false