Skip to content

Commit fee0e4d

Browse files
authored
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
1 parent 57e0bf4 commit fee0e4d

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

Diff for: modules/git/repo_branch.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package git
77

88
import (
99
"context"
10+
"errors"
1011
"fmt"
1112
"strings"
1213
)
@@ -72,7 +73,14 @@ func (repo *Repository) SetDefaultBranch(name string) error {
7273
// GetDefaultBranch gets default branch of repository.
7374
func (repo *Repository) GetDefaultBranch() (string, error) {
7475
stdout, _, err := NewCommand(repo.Ctx, "symbolic-ref", "HEAD").RunStdString(&RunOpts{Dir: repo.Path})
75-
return stdout, err
76+
if err != nil {
77+
return "", err
78+
}
79+
stdout = strings.TrimSpace(stdout)
80+
if !strings.HasPrefix(stdout, BranchPrefix) {
81+
return "", errors.New("the HEAD is not a branch: " + stdout)
82+
}
83+
return strings.TrimPrefix(stdout, BranchPrefix), nil
7684
}
7785

7886
// GetBranch returns a branch by it's name

Diff for: services/repository/adopt.go

-2
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,6 @@ func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, r
143143
return fmt.Errorf("setDefaultBranch: %v", err)
144144
}
145145
}
146-
147-
repo.DefaultBranch = strings.TrimPrefix(repo.DefaultBranch, git.BranchPrefix)
148146
}
149147
branches, _, _ := gitRepo.GetBranchNames(0, 0)
150148
found := false

0 commit comments

Comments
 (0)