]> source.dussan.org Git - gitea.git/commitdiff
Remove confusing TrimPrefix(... git.BranchPrefix) (#20369)
authorwxiaoguang <wxiaoguang@gmail.com>
Sat, 16 Jul 2022 00:10:02 +0000 (08:10 +0800)
committerGitHub <noreply@github.com>
Sat, 16 Jul 2022 00:10:02 +0000 (08:10 +0800)
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
services/repository/adopt.go

index 8e455480e7279e9686385f27b71be92eb03ded63..17d243808e9c0fc9bad85fb2529d6b858e664765 100644 (file)
@@ -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
index 48f049cd281174cddaef76f9e748dd4dc97e0ef1..6d6611c705f895e7ae3ca09c5b2f737087d5259e 100644 (file)
@@ -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