|
|
@ -15,12 +15,15 @@ import ( |
|
|
|
"code.gitea.io/gitea/models/db" |
|
|
|
"code.gitea.io/gitea/models/db" |
|
|
|
"code.gitea.io/gitea/modules/git" |
|
|
|
"code.gitea.io/gitea/modules/git" |
|
|
|
"code.gitea.io/gitea/modules/log" |
|
|
|
"code.gitea.io/gitea/modules/log" |
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/notification" |
|
|
|
|
|
|
|
repo_module "code.gitea.io/gitea/modules/repository" |
|
|
|
"code.gitea.io/gitea/modules/setting" |
|
|
|
"code.gitea.io/gitea/modules/setting" |
|
|
|
"code.gitea.io/gitea/modules/util" |
|
|
|
"code.gitea.io/gitea/modules/util" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/gobwas/glob" |
|
|
|
"github.com/gobwas/glob" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// AdoptRepository adopts a repository for the user/organization.
|
|
|
|
// AdoptRepository adopts pre-existing repository files for the user/organization.
|
|
|
|
func AdoptRepository(doer, u *models.User, opts models.CreateRepoOptions) (*models.Repository, error) { |
|
|
|
func AdoptRepository(doer, u *models.User, opts models.CreateRepoOptions) (*models.Repository, error) { |
|
|
|
if !doer.IsAdmin && !u.CanCreateRepo() { |
|
|
|
if !doer.IsAdmin && !u.CanCreateRepo() { |
|
|
|
return nil, models.ErrReachLimitOfRepo{ |
|
|
|
return nil, models.ErrReachLimitOfRepo{ |
|
|
@ -90,9 +93,97 @@ func AdoptRepository(doer, u *models.User, opts models.CreateRepoOptions) (*mode |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
notification.NotifyCreateRepository(doer, u, repo) |
|
|
|
|
|
|
|
|
|
|
|
return repo, nil |
|
|
|
return repo, 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 := repo_module.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 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// DeleteUnadoptedRepository deletes unadopted repository files from the filesystem
|
|
|
|
// DeleteUnadoptedRepository deletes unadopted repository files from the filesystem
|
|
|
|
func DeleteUnadoptedRepository(doer, u *models.User, repoName string) error { |
|
|
|
func DeleteUnadoptedRepository(doer, u *models.User, repoName string) error { |
|
|
|
if err := models.IsUsableRepoName(repoName); err != nil { |
|
|
|
if err := models.IsUsableRepoName(repoName); err != nil { |