2020-01-12 17:41:17 +05:30
|
|
|
// 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 21:15:36 +05:30
|
|
|
"context"
|
2020-01-12 17:41:17 +05:30
|
|
|
"fmt"
|
2022-06-06 13:31:49 +05:30
|
|
|
"os"
|
|
|
|
"path"
|
2020-01-12 17:41:17 +05:30
|
|
|
"strings"
|
2022-06-06 13:31:49 +05:30
|
|
|
"unicode/utf8"
|
2020-01-12 17:41:17 +05:30
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2021-09-19 17:19:59 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-12 21:21:54 +05:30
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2022-06-06 13:31:49 +05:30
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-01-12 17:41:17 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-06-06 13:31:49 +05:30
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2020-08-12 01:35:34 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2020-01-12 17:41:17 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// CreateRepository creates a repository for the user/organization.
|
2021-12-10 06:57:50 +05:30
|
|
|
func CreateRepository(doer, u *user_model.User, opts models.CreateRepoOptions) (*repo_model.Repository, error) {
|
2020-01-12 17:41:17 +05:30
|
|
|
if !doer.IsAdmin && !u.CanCreateRepo() {
|
2021-12-12 21:18:20 +05:30
|
|
|
return nil, repo_model.ErrReachLimitOfRepo{
|
2020-01-12 17:41:17 +05:30
|
|
|
Limit: u.MaxRepoCreation,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 02:28:54 +05:30
|
|
|
if len(opts.DefaultBranch) == 0 {
|
|
|
|
opts.DefaultBranch = setting.Repository.DefaultBranch
|
|
|
|
}
|
|
|
|
|
2021-01-19 01:30:50 +05:30
|
|
|
// Check if label template exist
|
|
|
|
if len(opts.IssueLabels) > 0 {
|
2022-03-29 12:53:45 +05:30
|
|
|
if _, err := GetLabelTemplateFile(opts.IssueLabels); err != nil {
|
2021-01-19 01:30:50 +05:30
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
repo := &repo_model.Repository{
|
2020-01-12 17:41:17 +05:30
|
|
|
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 10:48:37 +05:30
|
|
|
IsTemplate: opts.IsTemplate,
|
2020-01-12 17:41:17 +05:30
|
|
|
CloseIssuesViaCommitInAnyBranch: setting.Repository.DefaultCloseIssuesViaCommitsInAnyBranch,
|
|
|
|
Status: opts.Status,
|
|
|
|
IsEmpty: !opts.AutoInit,
|
2020-09-19 22:14:55 +05:30
|
|
|
TrustModel: opts.TrustModel,
|
2022-04-01 19:44:36 +05:30
|
|
|
IsMirror: opts.IsMirror,
|
2020-01-12 17:41:17 +05:30
|
|
|
}
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
var rollbackRepo *repo_model.Repository
|
2021-01-19 01:30:50 +05:30
|
|
|
|
2021-09-23 21:15:36 +05:30
|
|
|
if err := db.WithTx(func(ctx context.Context) error {
|
2020-09-25 09:39:23 +05:30
|
|
|
if err := models.CreateRepository(ctx, doer, u, repo, false); err != nil {
|
2020-01-12 17:41:17 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// No need for init mirror.
|
2020-09-25 09:39:23 +05:30
|
|
|
if opts.IsMirror {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
repoPath := repo_model.RepoPath(u.Name, repo.Name)
|
2020-11-28 08:12:08 +05:30
|
|
|
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 09:39:23 +05:30
|
|
|
// 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 21:18:20 +05:30
|
|
|
return repo_model.ErrRepoFilesAlreadyExist{
|
2020-09-25 09:39:23 +05:30
|
|
|
Uname: u.Name,
|
|
|
|
Name: repo.Name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-19 01:30:50 +05:30
|
|
|
if err = initRepository(ctx, repoPath, doer, repo, opts); err != nil {
|
2020-09-25 09:39:23 +05:30
|
|
|
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 17:41:17 +05:30
|
|
|
}
|
2020-09-25 09:39:23 +05:30
|
|
|
return fmt.Errorf("initRepository: %v", err)
|
|
|
|
}
|
2020-01-12 17:41:17 +05:30
|
|
|
|
2020-09-25 09:39:23 +05:30
|
|
|
// Initialize Issue Labels if selected
|
|
|
|
if len(opts.IssueLabels) > 0 {
|
2022-03-29 12:53:45 +05:30
|
|
|
if err = InitializeLabels(ctx, repo.ID, opts.IssueLabels, false); err != nil {
|
2021-01-19 01:30:50 +05:30
|
|
|
rollbackRepo = repo
|
|
|
|
rollbackRepo.OwnerID = u.ID
|
2020-09-25 09:39:23 +05:30
|
|
|
return fmt.Errorf("InitializeLabels: %v", err)
|
2020-01-12 17:41:17 +05:30
|
|
|
}
|
2020-09-25 09:39:23 +05:30
|
|
|
}
|
2020-01-12 17:41:17 +05:30
|
|
|
|
2022-06-06 13:31:49 +05:30
|
|
|
if err := CheckDaemonExportOK(ctx, repo); err != nil {
|
2021-10-14 01:17:02 +05:30
|
|
|
return fmt.Errorf("checkDaemonExportOK: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-04-01 08:25:30 +05:30
|
|
|
if stdout, _, err := git.NewCommand(ctx, "update-server-info").
|
2020-09-25 09:39:23 +05:30
|
|
|
SetDescription(fmt.Sprintf("CreateRepository(git update-server-info): %s", repoPath)).
|
2022-04-01 08:25:30 +05:30
|
|
|
RunStdString(&git.RunOpts{Dir: repoPath}); err != nil {
|
2020-09-25 09:39:23 +05:30
|
|
|
log.Error("CreateRepository(git update-server-info) in %v: Stdout: %s\nError: %v", repo, stdout, err)
|
2021-01-19 01:30:50 +05:30
|
|
|
rollbackRepo = repo
|
|
|
|
rollbackRepo.OwnerID = u.ID
|
2020-09-25 09:39:23 +05:30
|
|
|
return fmt.Errorf("CreateRepository(git update-server-info): %v", err)
|
2020-01-12 17:41:17 +05:30
|
|
|
}
|
|
|
|
return nil
|
2020-09-25 09:39:23 +05:30
|
|
|
}); err != nil {
|
2021-01-19 01:30:50 +05:30
|
|
|
if rollbackRepo != nil {
|
|
|
|
if errDelete := models.DeleteRepository(doer, rollbackRepo.OwnerID, rollbackRepo.ID); errDelete != nil {
|
|
|
|
log.Error("Rollback deleteRepository: %v", errDelete)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 09:39:23 +05:30
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-12 17:41:17 +05:30
|
|
|
|
2020-09-25 09:39:23 +05:30
|
|
|
return repo, nil
|
2020-01-12 17:41:17 +05:30
|
|
|
}
|
2022-06-06 13:31:49 +05:30
|
|
|
|
|
|
|
// UpdateRepoSize updates the repository size, calculating it using util.GetDirectorySize
|
|
|
|
func UpdateRepoSize(ctx context.Context, repo *repo_model.Repository) error {
|
|
|
|
size, err := util.GetDirectorySize(repo.RepoPath())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("updateSize: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-06-12 21:21:54 +05:30
|
|
|
lfsSize, err := git_model.GetRepoLFSSize(ctx, repo.ID)
|
2022-06-06 13:31:49 +05:30
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("updateSize: GetLFSMetaObjects: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return repo_model.UpdateRepoSize(ctx, repo.ID, size+lfsSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckDaemonExportOK creates/removes git-daemon-export-ok for git-daemon...
|
|
|
|
func CheckDaemonExportOK(ctx context.Context, repo *repo_model.Repository) error {
|
|
|
|
if err := repo.GetOwner(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create/Remove git-daemon-export-ok for git-daemon...
|
|
|
|
daemonExportFile := path.Join(repo.RepoPath(), `git-daemon-export-ok`)
|
|
|
|
|
|
|
|
isExist, err := util.IsExist(daemonExportFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to check if %s exists. Error: %v", daemonExportFile, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
isPublic := !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePublic
|
|
|
|
if !isPublic && isExist {
|
|
|
|
if err = util.Remove(daemonExportFile); err != nil {
|
|
|
|
log.Error("Failed to remove %s: %v", daemonExportFile, err)
|
|
|
|
}
|
|
|
|
} else if isPublic && !isExist {
|
|
|
|
if f, err := os.Create(daemonExportFile); err != nil {
|
|
|
|
log.Error("Failed to create %s: %v", daemonExportFile, err)
|
|
|
|
} else {
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateRepository updates a repository with db context
|
|
|
|
func UpdateRepository(ctx context.Context, repo *repo_model.Repository, visibilityChanged bool) (err error) {
|
|
|
|
repo.LowerName = strings.ToLower(repo.Name)
|
|
|
|
|
|
|
|
if utf8.RuneCountInString(repo.Description) > 255 {
|
|
|
|
repo.Description = string([]rune(repo.Description)[:255])
|
|
|
|
}
|
|
|
|
if utf8.RuneCountInString(repo.Website) > 255 {
|
|
|
|
repo.Website = string([]rune(repo.Website)[:255])
|
|
|
|
}
|
|
|
|
|
|
|
|
e := db.GetEngine(ctx)
|
|
|
|
|
|
|
|
if _, err = e.ID(repo.ID).AllCols().Update(repo); err != nil {
|
|
|
|
return fmt.Errorf("update: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = UpdateRepoSize(ctx, repo); err != nil {
|
|
|
|
log.Error("Failed to update size for repository: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if visibilityChanged {
|
|
|
|
if err = repo.GetOwner(ctx); err != nil {
|
|
|
|
return fmt.Errorf("getOwner: %v", err)
|
|
|
|
}
|
|
|
|
if repo.Owner.IsOrganization() {
|
|
|
|
// Organization repository need to recalculate access table when visibility is changed.
|
|
|
|
if err = access_model.RecalculateTeamAccesses(ctx, repo, 0); err != nil {
|
|
|
|
return fmt.Errorf("recalculateTeamAccesses: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If repo has become private, we need to set its actions to private.
|
|
|
|
if repo.IsPrivate {
|
|
|
|
_, err = e.Where("repo_id = ?", repo.ID).Cols("is_private").Update(&models.Action{
|
|
|
|
IsPrivate: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create/Remove git-daemon-export-ok for git-daemon...
|
2022-06-20 18:08:58 +05:30
|
|
|
if err := CheckDaemonExportOK(ctx, repo); err != nil {
|
2022-06-06 13:31:49 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
forkRepos, err := repo_model.GetRepositoriesByForkID(ctx, repo.ID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getRepositoriesByForkID: %v", err)
|
|
|
|
}
|
|
|
|
for i := range forkRepos {
|
|
|
|
forkRepos[i].IsPrivate = repo.IsPrivate || repo.Owner.Visibility == api.VisibleTypePrivate
|
|
|
|
if err = UpdateRepository(ctx, forkRepos[i], true); err != nil {
|
|
|
|
return fmt.Errorf("updateRepository[%d]: %v", forkRepos[i].ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|