2020-01-12 07:11:17 -05:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-12 07:11:17 -05:00
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2021-09-23 11:45:36 -04:00
|
|
|
"context"
|
2020-01-12 07:11:17 -05:00
|
|
|
"fmt"
|
2022-06-06 04:01:49 -04:00
|
|
|
"os"
|
|
|
|
"path"
|
2023-01-13 13:54:02 -05:00
|
|
|
"path/filepath"
|
2020-01-12 07:11:17 -05:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2022-08-24 22:31:57 -04:00
|
|
|
activities_model "code.gitea.io/gitea/models/activities"
|
2021-09-19 07:49:59 -04:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-12 11:51:54 -04:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2022-08-24 22:31:57 -04:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
|
|
|
"code.gitea.io/gitea/models/perm"
|
2022-06-06 04:01:49 -04:00
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-09 20:27:50 -05:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2022-08-24 22:31:57 -04:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2022-08-24 22:31:57 -04:00
|
|
|
"code.gitea.io/gitea/models/webhook"
|
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"
|
2022-06-06 04:01:49 -04:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2020-08-11 16:05:34 -04:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2020-01-12 07:11:17 -05:00
|
|
|
)
|
|
|
|
|
2022-08-24 22:31:57 -04:00
|
|
|
// CreateRepositoryByExample creates a repository for the user/organization.
|
2023-02-04 01:48:38 -05:00
|
|
|
func CreateRepositoryByExample(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository, overwriteOrAdopt, isFork bool) (err error) {
|
2022-08-24 22:31:57 -04:00
|
|
|
if err = repo_model.IsUsableRepoName(repo.Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-28 14:14:26 -04:00
|
|
|
has, err := repo_model.IsRepositoryModelExist(ctx, u, repo.Name)
|
2022-08-24 22:31:57 -04:00
|
|
|
if err != nil {
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("IsRepositoryExist: %w", err)
|
2022-08-24 22:31:57 -04:00
|
|
|
} else if has {
|
|
|
|
return repo_model.ErrRepoAlreadyExist{
|
|
|
|
Uname: u.Name,
|
|
|
|
Name: repo.Name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
repoPath := repo_model.RepoPath(u.Name, repo.Name)
|
|
|
|
isExist, err := util.IsExist(repoPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to check if %s exists. Error: %v", repoPath, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !overwriteOrAdopt && isExist {
|
|
|
|
log.Error("Files already exist in %s and we are not going to adopt or delete.", repoPath)
|
|
|
|
return repo_model.ErrRepoFilesAlreadyExist{
|
|
|
|
Uname: u.Name,
|
|
|
|
Name: repo.Name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = db.Insert(ctx, repo); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = repo_model.DeleteRedirect(ctx, u.ID, repo.Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert units for repo
|
2023-02-04 01:48:38 -05:00
|
|
|
defaultUnits := unit.DefaultRepoUnits
|
|
|
|
if isFork {
|
|
|
|
defaultUnits = unit.DefaultForkRepoUnits
|
|
|
|
}
|
|
|
|
units := make([]repo_model.RepoUnit, 0, len(defaultUnits))
|
|
|
|
for _, tp := range defaultUnits {
|
2022-08-24 22:31:57 -04:00
|
|
|
if tp == unit.TypeIssues {
|
|
|
|
units = append(units, repo_model.RepoUnit{
|
|
|
|
RepoID: repo.ID,
|
|
|
|
Type: tp,
|
|
|
|
Config: &repo_model.IssuesConfig{
|
|
|
|
EnableTimetracker: setting.Service.DefaultEnableTimetracking,
|
|
|
|
AllowOnlyContributorsToTrackTime: setting.Service.DefaultAllowOnlyContributorsToTrackTime,
|
|
|
|
EnableDependencies: setting.Service.DefaultEnableDependencies,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
} else if tp == unit.TypePullRequests {
|
|
|
|
units = append(units, repo_model.RepoUnit{
|
|
|
|
RepoID: repo.ID,
|
|
|
|
Type: tp,
|
|
|
|
Config: &repo_model.PullRequestsConfig{AllowMerge: true, AllowRebase: true, AllowRebaseMerge: true, AllowSquash: true, DefaultMergeStyle: repo_model.MergeStyle(setting.Repository.PullRequest.DefaultMergeStyle), AllowRebaseUpdate: true},
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
units = append(units, repo_model.RepoUnit{
|
|
|
|
RepoID: repo.ID,
|
|
|
|
Type: tp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = db.Insert(ctx, units); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remember visibility preference.
|
|
|
|
u.LastRepoVisibility = repo.IsPrivate
|
|
|
|
if err = user_model.UpdateUserCols(ctx, u, "last_repo_visibility"); err != nil {
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("UpdateUserCols: %w", err)
|
2022-08-24 22:31:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = user_model.IncrUserRepoNum(ctx, u.ID); err != nil {
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("IncrUserRepoNum: %w", err)
|
2022-08-24 22:31:57 -04:00
|
|
|
}
|
|
|
|
u.NumRepos++
|
|
|
|
|
|
|
|
// Give access to all members in teams with access to all repositories.
|
|
|
|
if u.IsOrganization() {
|
|
|
|
teams, err := organization.FindOrgTeams(ctx, u.ID)
|
|
|
|
if err != nil {
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("FindOrgTeams: %w", err)
|
2022-08-24 22:31:57 -04:00
|
|
|
}
|
|
|
|
for _, t := range teams {
|
|
|
|
if t.IncludesAllRepositories {
|
|
|
|
if err := models.AddRepository(ctx, t, repo); err != nil {
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("AddRepository: %w", err)
|
2022-08-24 22:31:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if isAdmin, err := access_model.IsUserRepoAdmin(ctx, repo, doer); err != nil {
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("IsUserRepoAdmin: %w", err)
|
2022-08-24 22:31:57 -04:00
|
|
|
} else if !isAdmin {
|
|
|
|
// Make creator repo admin if it wasn't assigned automatically
|
2022-12-09 21:46:31 -05:00
|
|
|
if err = AddCollaborator(ctx, repo, doer); err != nil {
|
|
|
|
return fmt.Errorf("AddCollaborator: %w", err)
|
2022-08-24 22:31:57 -04:00
|
|
|
}
|
2022-12-09 21:46:31 -05:00
|
|
|
if err = repo_model.ChangeCollaborationAccessMode(ctx, repo, doer.ID, perm.AccessModeAdmin); err != nil {
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("ChangeCollaborationAccessModeCtx: %w", err)
|
2022-08-24 22:31:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if err = access_model.RecalculateAccesses(ctx, repo); err != nil {
|
|
|
|
// Organization automatically called this in AddRepository method.
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("RecalculateAccesses: %w", err)
|
2022-08-24 22:31:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if setting.Service.AutoWatchNewRepos {
|
|
|
|
if err = repo_model.WatchRepo(ctx, doer.ID, repo.ID, true); err != nil {
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("WatchRepo: %w", err)
|
2022-08-24 22:31:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = webhook.CopyDefaultWebhooksToRepo(ctx, repo.ID); err != nil {
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("CopyDefaultWebhooksToRepo: %w", err)
|
2022-08-24 22:31:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateRepoOptions contains the create repository options
|
|
|
|
type CreateRepoOptions struct {
|
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
OriginalURL string
|
|
|
|
GitServiceType api.GitServiceType
|
|
|
|
Gitignores string
|
|
|
|
IssueLabels string
|
|
|
|
License string
|
|
|
|
Readme string
|
|
|
|
DefaultBranch string
|
|
|
|
IsPrivate bool
|
|
|
|
IsMirror bool
|
|
|
|
IsTemplate bool
|
|
|
|
AutoInit bool
|
|
|
|
Status repo_model.RepositoryStatus
|
|
|
|
TrustModel repo_model.TrustModelType
|
|
|
|
MirrorInterval string
|
|
|
|
}
|
|
|
|
|
2020-01-12 07:11:17 -05:00
|
|
|
// CreateRepository creates a repository for the user/organization.
|
2022-08-24 22:31:57 -04:00
|
|
|
func CreateRepository(doer, u *user_model.User, opts 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 {
|
2023-04-10 04:44:02 -04:00
|
|
|
if _, err := LoadTemplateLabelsByDisplayName(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,
|
2022-04-01 10:14:36 -04:00
|
|
|
IsMirror: opts.IsMirror,
|
2023-02-05 05:12:31 -05:00
|
|
|
DefaultBranch: opts.DefaultBranch,
|
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
|
|
|
|
2022-11-12 15:18:50 -05:00
|
|
|
if err := db.WithTx(db.DefaultContext, func(ctx context.Context) error {
|
2023-02-04 01:48:38 -05:00
|
|
|
if err := CreateRepositoryByExample(ctx, doer, u, repo, false, 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
|
|
|
}
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("initRepository: %w", err)
|
2020-09-25 00:09:23 -04:00
|
|
|
}
|
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
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("InitializeLabels: %w", 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
|
|
|
|
2022-06-06 04:01:49 -04:00
|
|
|
if err := CheckDaemonExportOK(ctx, repo); err != nil {
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("checkDaemonExportOK: %w", err)
|
2021-10-13 15:47:02 -04:00
|
|
|
}
|
|
|
|
|
2022-03-31 22:55:30 -04: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)).
|
2022-03-31 22:55:30 -04:00
|
|
|
RunStdString(&git.RunOpts{Dir: repoPath}); err != nil {
|
2020-09-25 00:09:23 -04:00
|
|
|
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
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("CreateRepository(git update-server-info): %w", 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
|
|
|
}
|
2022-06-06 04:01:49 -04:00
|
|
|
|
2023-01-13 13:54:02 -05:00
|
|
|
const notRegularFileMode = os.ModeSymlink | os.ModeNamedPipe | os.ModeSocket | os.ModeDevice | os.ModeCharDevice | os.ModeIrregular
|
|
|
|
|
|
|
|
// getDirectorySize returns the disk consumption for a given path
|
|
|
|
func getDirectorySize(path string) (int64, error) {
|
|
|
|
var size int64
|
|
|
|
err := filepath.WalkDir(path, func(_ string, info os.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) { // ignore the error because the file maybe deleted during traversing.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
f, err := info.Info()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if (f.Mode() & notRegularFileMode) == 0 {
|
|
|
|
size += f.Size()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return size, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateRepoSize updates the repository size, calculating it using getDirectorySize
|
2022-06-06 04:01:49 -04:00
|
|
|
func UpdateRepoSize(ctx context.Context, repo *repo_model.Repository) error {
|
2023-01-13 13:54:02 -05:00
|
|
|
size, err := getDirectorySize(repo.RepoPath())
|
2022-06-06 04:01:49 -04:00
|
|
|
if err != nil {
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("updateSize: %w", err)
|
2022-06-06 04:01:49 -04:00
|
|
|
}
|
|
|
|
|
2022-06-12 11:51:54 -04:00
|
|
|
lfsSize, err := git_model.GetRepoLFSSize(ctx, repo.ID)
|
2022-06-06 04:01:49 -04:00
|
|
|
if err != nil {
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("updateSize: GetLFSMetaObjects: %w", err)
|
2022-06-06 04:01:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-02-18 07:11:03 -05:00
|
|
|
if err := repo.LoadOwner(ctx); err != nil {
|
2022-06-06 04:01:49 -04:00
|
|
|
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)
|
|
|
|
|
|
|
|
e := db.GetEngine(ctx)
|
|
|
|
|
|
|
|
if _, err = e.ID(repo.ID).AllCols().Update(repo); err != nil {
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("update: %w", err)
|
2022-06-06 04:01:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = UpdateRepoSize(ctx, repo); err != nil {
|
|
|
|
log.Error("Failed to update size for repository: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if visibilityChanged {
|
2023-02-18 07:11:03 -05:00
|
|
|
if err = repo.LoadOwner(ctx); err != nil {
|
|
|
|
return fmt.Errorf("LoadOwner: %w", err)
|
2022-06-06 04:01:49 -04:00
|
|
|
}
|
|
|
|
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 {
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("recalculateTeamAccesses: %w", err)
|
2022-06-06 04:01:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If repo has become private, we need to set its actions to private.
|
|
|
|
if repo.IsPrivate {
|
2022-08-24 22:31:57 -04:00
|
|
|
_, err = e.Where("repo_id = ?", repo.ID).Cols("is_private").Update(&activities_model.Action{
|
2022-06-06 04:01:49 -04:00
|
|
|
IsPrivate: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-05 09:25:43 -04:00
|
|
|
|
|
|
|
if err = repo_model.ClearRepoStars(ctx, repo.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-06 04:01:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create/Remove git-daemon-export-ok for git-daemon...
|
2022-06-20 08:38:58 -04:00
|
|
|
if err := CheckDaemonExportOK(ctx, repo); err != nil {
|
2022-06-06 04:01:49 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
forkRepos, err := repo_model.GetRepositoriesByForkID(ctx, repo.ID)
|
|
|
|
if err != nil {
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("getRepositoriesByForkID: %w", err)
|
2022-06-06 04:01:49 -04:00
|
|
|
}
|
|
|
|
for i := range forkRepos {
|
|
|
|
forkRepos[i].IsPrivate = repo.IsPrivate || repo.Owner.Visibility == api.VisibleTypePrivate
|
|
|
|
if err = UpdateRepository(ctx, forkRepos[i], true); err != nil {
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("updateRepository[%d]: %w", forkRepos[i].ID, err)
|
2022-06-06 04:01:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|