From 1ee01e00e298af3db57c30fbcb3789dc9cf181cf Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 14 Jul 2022 20:40:26 +0800 Subject: [PATCH 1/3] remove confusing TrimPrefix(... git.BranchPrefix) --- services/repository/adopt.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/services/repository/adopt.go b/services/repository/adopt.go index 48f049cd28117..6d6611c705f89 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 From 89e5e994ed90823f8bf57743cc16177143d28d99 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 14 Jul 2022 21:23:24 +0800 Subject: [PATCH 2/3] fix GetDefaultBranch behavior to match SetDefaultBranch --- modules/git/repo_branch.go | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/git/repo_branch.go b/modules/git/repo_branch.go index 8e455480e7279..9dee5f381f367 100644 --- a/modules/git/repo_branch.go +++ b/modules/git/repo_branch.go @@ -72,6 +72,7 @@ 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}) + stdout = strings.TrimPrefix(strings.TrimSpace(stdout), BranchPrefix) return stdout, err } From 8a7bd87ef8ee572fba742fdc1a89329407e06dce Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 14 Jul 2022 21:31:57 +0800 Subject: [PATCH 3/3] fix GetDefaultBranch behavior to match SetDefaultBranch --- modules/git/repo_branch.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/modules/git/repo_branch.go b/modules/git/repo_branch.go index 9dee5f381f367..17d243808e9c0 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,8 +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}) - stdout = strings.TrimPrefix(strings.TrimSpace(stdout), BranchPrefix) - 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