forked from NYANDEV/forgejo
Move some functions into services/repository (#17660)
This commit is contained in:
parent
447428f446
commit
48ccd325a1
19 changed files with 114 additions and 136 deletions
|
@ -196,92 +196,6 @@ func checkInitRepository(owner, name string) (err error) {
|
|||
return nil
|
||||
}
|
||||
|
||||
func adoptRepository(ctx context.Context, repoPath string, u *models.User, repo *models.Repository, opts models.CreateRepoOptions) (err error) {
|
||||
isExist, err := util.IsExist(repoPath)
|
||||
if err != nil {
|
||||
log.Error("Unable to check if %s exists. Error: %v", repoPath, err)
|
||||
return err
|
||||
}
|
||||
if !isExist {
|
||||
return fmt.Errorf("adoptRepository: path does not already exist: %s", repoPath)
|
||||
}
|
||||
|
||||
if err := createDelegateHooks(repoPath); err != nil {
|
||||
return fmt.Errorf("createDelegateHooks: %v", err)
|
||||
}
|
||||
|
||||
// Re-fetch the repository from database before updating it (else it would
|
||||
// override changes that were done earlier with sql)
|
||||
if repo, err = models.GetRepositoryByIDCtx(ctx, repo.ID); err != nil {
|
||||
return fmt.Errorf("getRepositoryByID: %v", err)
|
||||
}
|
||||
|
||||
repo.IsEmpty = false
|
||||
gitRepo, err := git.OpenRepository(repo.RepoPath())
|
||||
if err != nil {
|
||||
return fmt.Errorf("openRepository: %v", err)
|
||||
}
|
||||
defer gitRepo.Close()
|
||||
if len(opts.DefaultBranch) > 0 {
|
||||
repo.DefaultBranch = opts.DefaultBranch
|
||||
|
||||
if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
|
||||
return fmt.Errorf("setDefaultBranch: %v", err)
|
||||
}
|
||||
} else {
|
||||
repo.DefaultBranch, err = gitRepo.GetDefaultBranch()
|
||||
if err != nil {
|
||||
repo.DefaultBranch = setting.Repository.DefaultBranch
|
||||
if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
|
||||
return fmt.Errorf("setDefaultBranch: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
repo.DefaultBranch = strings.TrimPrefix(repo.DefaultBranch, git.BranchPrefix)
|
||||
}
|
||||
branches, _, _ := gitRepo.GetBranches(0, 0)
|
||||
found := false
|
||||
hasDefault := false
|
||||
hasMaster := false
|
||||
hasMain := false
|
||||
for _, branch := range branches {
|
||||
if branch == repo.DefaultBranch {
|
||||
found = true
|
||||
break
|
||||
} else if branch == setting.Repository.DefaultBranch {
|
||||
hasDefault = true
|
||||
} else if branch == "master" {
|
||||
hasMaster = true
|
||||
} else if branch == "main" {
|
||||
hasMain = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
if hasDefault {
|
||||
repo.DefaultBranch = setting.Repository.DefaultBranch
|
||||
} else if hasMaster {
|
||||
repo.DefaultBranch = "master"
|
||||
} else if hasMain {
|
||||
repo.DefaultBranch = "main"
|
||||
} else if len(branches) > 0 {
|
||||
repo.DefaultBranch = branches[0]
|
||||
} else {
|
||||
repo.IsEmpty = true
|
||||
repo.DefaultBranch = setting.Repository.DefaultBranch
|
||||
}
|
||||
|
||||
if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
|
||||
return fmt.Errorf("setDefaultBranch: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err = models.UpdateRepositoryCtx(ctx, repo, false); err != nil {
|
||||
return fmt.Errorf("updateRepository: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// InitRepository initializes README and .gitignore if needed.
|
||||
func initRepository(ctx context.Context, repoPath string, u *models.User, repo *models.Repository, opts models.CreateRepoOptions) (err error) {
|
||||
if err = checkInitRepository(repo.OwnerName, repo.Name); err != nil {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue