2020-01-12 07:11:17 -05:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2021-09-23 11:45:36 -04:00
|
|
|
"context"
|
2020-01-12 07:11:17 -05:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2021-09-19 07:49:59 -04:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-09 20:27:50 -05:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-01-12 07:11:17 -05:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-08-11 16:05:34 -04:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2020-01-12 07:11:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// CreateRepository creates a repository for the user/organization.
|
2021-12-09 20:27:50 -05:00
|
|
|
func CreateRepository(doer, u *user_model.User, opts models.CreateRepoOptions) (*repo_model.Repository, error) {
|
2020-01-12 07:11:17 -05:00
|
|
|
if !doer.IsAdmin && !u.CanCreateRepo() {
|
2021-12-12 10:48:20 -05:00
|
|
|
return nil, repo_model.ErrReachLimitOfRepo{
|
2020-01-12 07:11:17 -05:00
|
|
|
Limit: u.MaxRepoCreation,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:58:54 -04:00
|
|
|
if len(opts.DefaultBranch) == 0 {
|
|
|
|
opts.DefaultBranch = setting.Repository.DefaultBranch
|
|
|
|
}
|
|
|
|
|
2021-01-18 15:00:50 -05:00
|
|
|
// Check if label template exist
|
|
|
|
if len(opts.IssueLabels) > 0 {
|
2022-03-29 03:23:45 -04:00
|
|
|
if _, err := GetLabelTemplateFile(opts.IssueLabels); err != nil {
|
2021-01-18 15:00:50 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-09 20:27:50 -05:00
|
|
|
repo := &repo_model.Repository{
|
2020-01-12 07:11:17 -05:00
|
|
|
OwnerID: u.ID,
|
|
|
|
Owner: u,
|
|
|
|
OwnerName: u.Name,
|
|
|
|
Name: opts.Name,
|
|
|
|
LowerName: strings.ToLower(opts.Name),
|
|
|
|
Description: opts.Description,
|
|
|
|
OriginalURL: opts.OriginalURL,
|
|
|
|
OriginalServiceType: opts.GitServiceType,
|
|
|
|
IsPrivate: opts.IsPrivate,
|
|
|
|
IsFsckEnabled: !opts.IsMirror,
|
2020-09-25 01:18:37 -04:00
|
|
|
IsTemplate: opts.IsTemplate,
|
2020-01-12 07:11:17 -05:00
|
|
|
CloseIssuesViaCommitInAnyBranch: setting.Repository.DefaultCloseIssuesViaCommitsInAnyBranch,
|
|
|
|
Status: opts.Status,
|
|
|
|
IsEmpty: !opts.AutoInit,
|
2020-09-19 12:44:55 -04:00
|
|
|
TrustModel: opts.TrustModel,
|
2020-01-12 07:11:17 -05:00
|
|
|
}
|
|
|
|
|
2021-12-09 20:27:50 -05:00
|
|
|
var rollbackRepo *repo_model.Repository
|
2021-01-18 15:00:50 -05:00
|
|
|
|
2021-09-23 11:45:36 -04:00
|
|
|
if err := db.WithTx(func(ctx context.Context) error {
|
2020-09-25 00:09:23 -04:00
|
|
|
if err := models.CreateRepository(ctx, doer, u, repo, false); err != nil {
|
2020-01-12 07:11:17 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// No need for init mirror.
|
2020-09-25 00:09:23 -04:00
|
|
|
if opts.IsMirror {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-09 20:27:50 -05:00
|
|
|
repoPath := repo_model.RepoPath(u.Name, repo.Name)
|
2020-11-27 21:42:08 -05:00
|
|
|
isExist, err := util.IsExist(repoPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to check if %s exists. Error: %v", repoPath, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if isExist {
|
2020-09-25 00:09:23 -04:00
|
|
|
// repo already exists - We have two or three options.
|
|
|
|
// 1. We fail stating that the directory exists
|
|
|
|
// 2. We create the db repository to go with this data and adopt the git repo
|
|
|
|
// 3. We delete it and start afresh
|
|
|
|
//
|
|
|
|
// Previously Gitea would just delete and start afresh - this was naughty.
|
|
|
|
// So we will now fail and delegate to other functionality to adopt or delete
|
|
|
|
log.Error("Files already exist in %s and we are not going to adopt or delete.", repoPath)
|
2021-12-12 10:48:20 -05:00
|
|
|
return repo_model.ErrRepoFilesAlreadyExist{
|
2020-09-25 00:09:23 -04:00
|
|
|
Uname: u.Name,
|
|
|
|
Name: repo.Name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 15:00:50 -05:00
|
|
|
if err = initRepository(ctx, repoPath, doer, repo, opts); err != nil {
|
2020-09-25 00:09:23 -04:00
|
|
|
if err2 := util.RemoveAll(repoPath); err2 != nil {
|
|
|
|
log.Error("initRepository: %v", err)
|
|
|
|
return fmt.Errorf(
|
|
|
|
"delete repo directory %s/%s failed(2): %v", u.Name, repo.Name, err2)
|
2020-01-12 07:11:17 -05:00
|
|
|
}
|
2020-09-25 00:09:23 -04:00
|
|
|
return fmt.Errorf("initRepository: %v", err)
|
|
|
|
}
|
2020-01-12 07:11:17 -05:00
|
|
|
|
2020-09-25 00:09:23 -04:00
|
|
|
// Initialize Issue Labels if selected
|
|
|
|
if len(opts.IssueLabels) > 0 {
|
2022-03-29 03:23:45 -04:00
|
|
|
if err = InitializeLabels(ctx, repo.ID, opts.IssueLabels, false); err != nil {
|
2021-01-18 15:00:50 -05:00
|
|
|
rollbackRepo = repo
|
|
|
|
rollbackRepo.OwnerID = u.ID
|
2020-09-25 00:09:23 -04:00
|
|
|
return fmt.Errorf("InitializeLabels: %v", err)
|
2020-01-12 07:11:17 -05:00
|
|
|
}
|
2020-09-25 00:09:23 -04:00
|
|
|
}
|
2020-01-12 07:11:17 -05:00
|
|
|
|
2021-12-09 20:27:50 -05:00
|
|
|
if err := models.CheckDaemonExportOK(ctx, repo); err != nil {
|
2021-10-13 15:47:02 -04:00
|
|
|
return fmt.Errorf("checkDaemonExportOK: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-02-06 14:01:47 -05:00
|
|
|
if stdout, err := git.NewCommand(ctx, "update-server-info").
|
2020-09-25 00:09:23 -04:00
|
|
|
SetDescription(fmt.Sprintf("CreateRepository(git update-server-info): %s", repoPath)).
|
|
|
|
RunInDir(repoPath); err != nil {
|
|
|
|
log.Error("CreateRepository(git update-server-info) in %v: Stdout: %s\nError: %v", repo, stdout, err)
|
2021-01-18 15:00:50 -05:00
|
|
|
rollbackRepo = repo
|
|
|
|
rollbackRepo.OwnerID = u.ID
|
2020-09-25 00:09:23 -04:00
|
|
|
return fmt.Errorf("CreateRepository(git update-server-info): %v", err)
|
2020-01-12 07:11:17 -05:00
|
|
|
}
|
|
|
|
return nil
|
2020-09-25 00:09:23 -04:00
|
|
|
}); err != nil {
|
2021-01-18 15:00:50 -05:00
|
|
|
if rollbackRepo != nil {
|
|
|
|
if errDelete := models.DeleteRepository(doer, rollbackRepo.OwnerID, rollbackRepo.ID); errDelete != nil {
|
|
|
|
log.Error("Rollback deleteRepository: %v", errDelete)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 00:09:23 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-12 07:11:17 -05:00
|
|
|
|
2020-09-25 00:09:23 -04:00
|
|
|
return repo, nil
|
2020-01-12 07:11:17 -05:00
|
|
|
}
|