2020-01-12 17:41:17 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-12 17:41:17 +05:30
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2021-09-23 21:15:36 +05:30
|
|
|
"context"
|
2020-01-12 17:41:17 +05:30
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
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"
|
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"
|
2021-11-16 19:00:11 +05:30
|
|
|
"code.gitea.io/gitea/modules/notification"
|
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
2020-06-07 06:15:12 +05:30
|
|
|
"code.gitea.io/gitea/modules/structs"
|
2021-09-14 21:46:40 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2020-01-12 17:41:17 +05:30
|
|
|
)
|
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
// ErrForkAlreadyExist represents a "ForkAlreadyExist" kind of error.
|
|
|
|
type ErrForkAlreadyExist struct {
|
|
|
|
Uname string
|
|
|
|
RepoName string
|
|
|
|
ForkName string
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrForkAlreadyExist checks if an error is an ErrForkAlreadyExist.
|
|
|
|
func IsErrForkAlreadyExist(err error) bool {
|
|
|
|
_, ok := err.(ErrForkAlreadyExist)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ErrForkAlreadyExist) Error() string {
|
|
|
|
return fmt.Sprintf("repository is already forked by user [uname: %s, repo path: %s, fork path: %s]", err.Uname, err.RepoName, err.ForkName)
|
|
|
|
}
|
|
|
|
|
2022-10-18 11:20:37 +05:30
|
|
|
func (err ErrForkAlreadyExist) Unwrap() error {
|
|
|
|
return util.ErrAlreadyExist
|
|
|
|
}
|
|
|
|
|
2021-12-12 21:18:20 +05:30
|
|
|
// ForkRepoOptions contains the fork repository options
|
|
|
|
type ForkRepoOptions struct {
|
|
|
|
BaseRepo *repo_model.Repository
|
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
2020-01-12 17:41:17 +05:30
|
|
|
// ForkRepository forks a repository
|
2022-03-30 00:43:41 +05:30
|
|
|
func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts ForkRepoOptions) (*repo_model.Repository, error) {
|
|
|
|
forkedRepo, err := repo_model.GetUserFork(ctx, opts.BaseRepo.ID, owner.ID)
|
2020-01-12 17:41:17 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if forkedRepo != nil {
|
2022-08-25 08:01:57 +05:30
|
|
|
return nil, ErrForkAlreadyExist{
|
2020-01-12 17:41:17 +05:30
|
|
|
Uname: owner.Name,
|
2021-08-28 14:07:14 +05:30
|
|
|
RepoName: opts.BaseRepo.FullName(),
|
2020-01-12 17:41:17 +05:30
|
|
|
ForkName: forkedRepo.FullName(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
repo := &repo_model.Repository{
|
2020-01-12 17:41:17 +05:30
|
|
|
OwnerID: owner.ID,
|
|
|
|
Owner: owner,
|
|
|
|
OwnerName: owner.Name,
|
2021-08-28 14:07:14 +05:30
|
|
|
Name: opts.Name,
|
|
|
|
LowerName: strings.ToLower(opts.Name),
|
|
|
|
Description: opts.Description,
|
|
|
|
DefaultBranch: opts.BaseRepo.DefaultBranch,
|
|
|
|
IsPrivate: opts.BaseRepo.IsPrivate || opts.BaseRepo.Owner.Visibility == structs.VisibleTypePrivate,
|
|
|
|
IsEmpty: opts.BaseRepo.IsEmpty,
|
2020-01-12 17:41:17 +05:30
|
|
|
IsFork: true,
|
2021-08-28 14:07:14 +05:30
|
|
|
ForkID: opts.BaseRepo.ID,
|
2020-01-12 17:41:17 +05:30
|
|
|
}
|
|
|
|
|
2021-08-28 14:07:14 +05:30
|
|
|
oldRepoPath := opts.BaseRepo.RepoPath()
|
2020-01-12 17:41:17 +05:30
|
|
|
|
2021-09-14 21:46:40 +05:30
|
|
|
needsRollback := false
|
|
|
|
rollbackFn := func() {
|
|
|
|
if !needsRollback {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
repoPath := repo_model.RepoPath(owner.Name, repo.Name)
|
2021-09-14 21:46:40 +05:30
|
|
|
|
|
|
|
if exists, _ := util.IsExist(repoPath); !exists {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// As the transaction will be failed and hence database changes will be destroyed we only need
|
|
|
|
// to delete the related repository on the filesystem
|
|
|
|
if errDelete := util.RemoveAll(repoPath); errDelete != nil {
|
|
|
|
log.Error("Failed to remove fork repo")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
needsRollbackInPanic := true
|
|
|
|
defer func() {
|
|
|
|
panicErr := recover()
|
|
|
|
if panicErr == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if needsRollbackInPanic {
|
|
|
|
rollbackFn()
|
|
|
|
}
|
|
|
|
panic(panicErr)
|
|
|
|
}()
|
|
|
|
|
2022-11-13 01:48:50 +05:30
|
|
|
err = db.WithTx(ctx, func(txCtx context.Context) error {
|
2022-08-25 08:01:57 +05:30
|
|
|
if err = repo_module.CreateRepositoryByExample(txCtx, doer, owner, repo, false); err != nil {
|
2020-01-12 17:41:17 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-06 13:31:49 +05:30
|
|
|
if err = repo_model.IncrementRepoForkNum(txCtx, opts.BaseRepo.ID); err != nil {
|
2020-01-12 17:41:17 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-15 02:45:28 +05:30
|
|
|
// copy lfs files failure should not be ignored
|
2022-06-12 21:21:54 +05:30
|
|
|
if err = git_model.CopyLFS(txCtx, repo, opts.BaseRepo); err != nil {
|
2021-04-15 02:45:28 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-14 21:46:40 +05:30
|
|
|
needsRollback = true
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
repoPath := repo_model.RepoPath(owner.Name, repo.Name)
|
2022-03-31 17:26:22 +05:30
|
|
|
if stdout, _, err := git.NewCommand(txCtx,
|
2022-10-23 20:14:45 +05:30
|
|
|
"clone", "--bare").AddDynamicArguments(oldRepoPath, repoPath).
|
2021-08-28 14:07:14 +05:30
|
|
|
SetDescription(fmt.Sprintf("ForkRepository(git clone): %s to %s", opts.BaseRepo.FullName(), repo.FullName())).
|
2022-04-01 08:25:30 +05:30
|
|
|
RunStdBytes(&git.RunOpts{Timeout: 10 * time.Minute}); err != nil {
|
2021-08-28 14:07:14 +05:30
|
|
|
log.Error("Fork Repository (git clone) Failed for %v (from %v):\nStdout: %s\nError: %v", repo, opts.BaseRepo, stdout, err)
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("git clone: %w", err)
|
2020-01-12 17:41:17 +05:30
|
|
|
}
|
|
|
|
|
2022-06-06 13:31:49 +05:30
|
|
|
if err := repo_module.CheckDaemonExportOK(txCtx, repo); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("checkDaemonExportOK: %w", err)
|
2021-10-14 01:17:02 +05:30
|
|
|
}
|
|
|
|
|
2022-04-01 08:25:30 +05:30
|
|
|
if stdout, _, err := git.NewCommand(txCtx, "update-server-info").
|
2020-01-12 17:41:17 +05:30
|
|
|
SetDescription(fmt.Sprintf("ForkRepository(git update-server-info): %s", repo.FullName())).
|
2022-04-01 08:25:30 +05:30
|
|
|
RunStdString(&git.RunOpts{Dir: repoPath}); err != nil {
|
2020-01-12 17:41:17 +05:30
|
|
|
log.Error("Fork Repository (git update-server-info) failed for %v:\nStdout: %s\nError: %v", repo, stdout, err)
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("git update-server-info: %w", err)
|
2020-01-12 17:41:17 +05:30
|
|
|
}
|
|
|
|
|
2021-11-16 19:00:11 +05:30
|
|
|
if err = repo_module.CreateDelegateHooks(repoPath); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("createDelegateHooks: %w", err)
|
2020-01-12 17:41:17 +05:30
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2021-09-14 21:46:40 +05:30
|
|
|
needsRollbackInPanic = false
|
2020-01-12 17:41:17 +05:30
|
|
|
if err != nil {
|
2021-09-14 21:46:40 +05:30
|
|
|
rollbackFn()
|
2020-01-12 17:41:17 +05:30
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-15 02:45:28 +05:30
|
|
|
// even if below operations failed, it could be ignored. And they will be retried
|
2022-06-06 13:31:49 +05:30
|
|
|
if err := repo_module.UpdateRepoSize(ctx, repo); err != nil {
|
2020-01-12 17:41:17 +05:30
|
|
|
log.Error("Failed to update size for repository: %v", err)
|
|
|
|
}
|
2021-12-10 06:57:50 +05:30
|
|
|
if err := repo_model.CopyLanguageStat(opts.BaseRepo, repo); err != nil {
|
2022-02-12 08:48:06 +05:30
|
|
|
log.Error("Copy language stat from oldRepo failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-03-30 00:43:41 +05:30
|
|
|
gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
|
2022-02-12 08:48:06 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("Open created git repository failed: %v", err)
|
|
|
|
} else {
|
|
|
|
defer gitRepo.Close()
|
|
|
|
if err := repo_module.SyncReleasesWithTags(repo, gitRepo); err != nil {
|
|
|
|
log.Error("Sync releases from git tags failed: %v", err)
|
|
|
|
}
|
2020-04-08 17:43:04 +05:30
|
|
|
}
|
2020-09-25 09:39:23 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
notification.NotifyForkRepository(ctx, doer, opts.BaseRepo, repo)
|
2021-11-16 19:00:11 +05:30
|
|
|
|
2020-09-25 09:39:23 +05:30
|
|
|
return repo, nil
|
2020-01-12 17:41:17 +05:30
|
|
|
}
|
2021-09-14 22:37:08 +05:30
|
|
|
|
|
|
|
// ConvertForkToNormalRepository convert the provided repo from a forked repo to normal repo
|
2021-12-10 06:57:50 +05:30
|
|
|
func ConvertForkToNormalRepository(repo *repo_model.Repository) error {
|
2022-11-13 01:48:50 +05:30
|
|
|
err := db.WithTx(db.DefaultContext, func(ctx context.Context) error {
|
2022-12-03 08:18:26 +05:30
|
|
|
repo, err := repo_model.GetRepositoryByID(ctx, repo.ID)
|
2021-09-14 22:37:08 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !repo.IsFork {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-06 13:31:49 +05:30
|
|
|
if err := repo_model.DecrementRepoForkNum(ctx, repo.ForkID); err != nil {
|
2021-09-14 22:37:08 +05:30
|
|
|
log.Error("Unable to decrement repo fork num for old root repo %d of repository %-v whilst converting from fork. Error: %v", repo.ForkID, repo, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
repo.IsFork = false
|
|
|
|
repo.ForkID = 0
|
|
|
|
|
2022-06-06 13:31:49 +05:30
|
|
|
if err := repo_module.UpdateRepository(ctx, repo, false); err != nil {
|
2021-09-14 22:37:08 +05:30
|
|
|
log.Error("Unable to update repository %-v whilst converting from fork. Error: %v", repo, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|