mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-22 09:13:10 +00:00
Set DefaultBranch to master when importing a new repo if possible
This commit is contained in:
parent
c9901bbba5
commit
edb7967dc7
1 changed files with 27 additions and 25 deletions
|
@ -662,6 +662,33 @@ func MigrateRepository(u *User, opts MigrateRepoOptions) (*Repository, error) {
|
|||
return repo, fmt.Errorf("Clone: %v", err)
|
||||
}
|
||||
|
||||
// Check if repository is empty.
|
||||
_, stderr, err := com.ExecCmdDir(repoPath, "git", "log", "-1")
|
||||
if err != nil {
|
||||
if strings.Contains(stderr, "fatal: bad default revision 'HEAD'") {
|
||||
repo.IsBare = true
|
||||
} else {
|
||||
return repo, fmt.Errorf("check bare: %v - %s", err, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
if !repo.IsBare {
|
||||
// Try to get HEAD branch and set it as default branch.
|
||||
gitRepo, err := git.OpenRepository(repoPath)
|
||||
if err != nil {
|
||||
log.Error(4, "OpenRepository: %v", err)
|
||||
return repo, nil
|
||||
}
|
||||
headBranch, err := gitRepo.GetHEADBranch()
|
||||
if err != nil {
|
||||
log.Error(4, "GetHEADBranch: %v", err)
|
||||
return repo, nil
|
||||
}
|
||||
if headBranch != nil {
|
||||
repo.DefaultBranch = headBranch.Name
|
||||
}
|
||||
}
|
||||
|
||||
if opts.IsMirror {
|
||||
if _, err = x.InsertOne(&Mirror{
|
||||
RepoID: repo.ID,
|
||||
|
@ -696,31 +723,6 @@ func CleanUpMigrateInfo(repo *Repository, repoPath string) (*Repository, error)
|
|||
return repo, fmt.Errorf("save config file: %v", err)
|
||||
}
|
||||
|
||||
// Check if repository is empty.
|
||||
_, stderr, err := com.ExecCmdDir(repoPath, "git", "log", "-1")
|
||||
if err != nil {
|
||||
if strings.Contains(stderr, "fatal: bad default revision 'HEAD'") {
|
||||
repo.IsBare = true
|
||||
} else {
|
||||
return repo, fmt.Errorf("check bare: %v - %s", err, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// Try to get HEAD branch and set it as default branch.
|
||||
gitRepo, err := git.OpenRepository(repoPath)
|
||||
if err != nil {
|
||||
log.Error(4, "OpenRepository: %v", err)
|
||||
return repo, nil
|
||||
}
|
||||
headBranch, err := gitRepo.GetHEADBranch()
|
||||
if err != nil {
|
||||
log.Error(4, "GetHEADBranch: %v", err)
|
||||
return repo, nil
|
||||
}
|
||||
if headBranch != nil {
|
||||
repo.DefaultBranch = headBranch.Name
|
||||
}
|
||||
|
||||
return repo, UpdateRepository(repo, false)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue