2020-01-12 20:11:17 +08:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-12 20:11:17 +08:00
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2021-09-23 23:45:36 +08:00
|
|
|
"context"
|
2020-01-12 20:11:17 +08:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-09-19 19:49:59 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-12 23:51:54 +08:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2024-11-19 12:08:32 +08:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-01-12 20:11:17 +08:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 04:09:51 +08:00
|
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
2020-01-12 20:11:17 +08:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-11-16 21:30:11 +08:00
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
2020-06-07 08:45:12 +08:00
|
|
|
"code.gitea.io/gitea/modules/structs"
|
2021-09-15 00:16:40 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2023-09-06 02:37:47 +08:00
|
|
|
notify_service "code.gitea.io/gitea/services/notify"
|
2024-11-19 12:08:32 +08:00
|
|
|
|
|
|
|
"xorm.io/builder"
|
2020-01-12 20:11:17 +08:00
|
|
|
)
|
|
|
|
|
2022-08-25 10:31:57 +08:00
|
|
|
// 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 13:50:37 +08:00
|
|
|
func (err ErrForkAlreadyExist) Unwrap() error {
|
|
|
|
return util.ErrAlreadyExist
|
|
|
|
}
|
|
|
|
|
2021-12-12 23:48:20 +08:00
|
|
|
// ForkRepoOptions contains the fork repository options
|
|
|
|
type ForkRepoOptions struct {
|
2023-09-29 09:48:39 +08:00
|
|
|
BaseRepo *repo_model.Repository
|
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
SingleBranch string
|
2021-12-12 23:48:20 +08:00
|
|
|
}
|
|
|
|
|
2020-01-12 20:11:17 +08:00
|
|
|
// ForkRepository forks a repository
|
2022-03-30 03:13:41 +08:00
|
|
|
func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts ForkRepoOptions) (*repo_model.Repository, error) {
|
2024-03-04 16:16:03 +08:00
|
|
|
if err := opts.BaseRepo.LoadOwner(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if user_model.IsUserBlockedBy(ctx, doer, opts.BaseRepo.Owner.ID) {
|
|
|
|
return nil, user_model.ErrBlockedUser
|
|
|
|
}
|
|
|
|
|
2022-12-28 05:21:14 +08:00
|
|
|
// Fork is prohibited, if user has reached maximum limit of repositories
|
|
|
|
if !owner.CanForkRepo() {
|
|
|
|
return nil, repo_model.ErrReachLimitOfRepo{
|
|
|
|
Limit: owner.MaxRepoCreation,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-30 03:13:41 +08:00
|
|
|
forkedRepo, err := repo_model.GetUserFork(ctx, opts.BaseRepo.ID, owner.ID)
|
2020-01-12 20:11:17 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if forkedRepo != nil {
|
2022-08-25 10:31:57 +08:00
|
|
|
return nil, ErrForkAlreadyExist{
|
2020-01-12 20:11:17 +08:00
|
|
|
Uname: owner.Name,
|
2021-08-28 16:37:14 +08:00
|
|
|
RepoName: opts.BaseRepo.FullName(),
|
2020-01-12 20:11:17 +08:00
|
|
|
ForkName: forkedRepo.FullName(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-29 09:48:39 +08:00
|
|
|
defaultBranch := opts.BaseRepo.DefaultBranch
|
|
|
|
if opts.SingleBranch != "" {
|
|
|
|
defaultBranch = opts.SingleBranch
|
|
|
|
}
|
2021-12-10 09:27:50 +08:00
|
|
|
repo := &repo_model.Repository{
|
2024-01-20 00:05:02 +08:00
|
|
|
OwnerID: owner.ID,
|
|
|
|
Owner: owner,
|
|
|
|
OwnerName: owner.Name,
|
|
|
|
Name: opts.Name,
|
|
|
|
LowerName: strings.ToLower(opts.Name),
|
|
|
|
Description: opts.Description,
|
|
|
|
DefaultBranch: defaultBranch,
|
|
|
|
IsPrivate: opts.BaseRepo.IsPrivate || opts.BaseRepo.Owner.Visibility == structs.VisibleTypePrivate,
|
|
|
|
IsEmpty: opts.BaseRepo.IsEmpty,
|
|
|
|
IsFork: true,
|
|
|
|
ForkID: opts.BaseRepo.ID,
|
|
|
|
ObjectFormatName: opts.BaseRepo.ObjectFormatName,
|
2020-01-12 20:11:17 +08:00
|
|
|
}
|
|
|
|
|
2021-08-28 16:37:14 +08:00
|
|
|
oldRepoPath := opts.BaseRepo.RepoPath()
|
2020-01-12 20:11:17 +08:00
|
|
|
|
2021-09-15 00:16:40 +08:00
|
|
|
needsRollback := false
|
|
|
|
rollbackFn := func() {
|
|
|
|
if !needsRollback {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-10 09:27:50 +08:00
|
|
|
repoPath := repo_model.RepoPath(owner.Name, repo.Name)
|
2021-09-15 00:16:40 +08:00
|
|
|
|
|
|
|
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 04:18:50 +08:00
|
|
|
err = db.WithTx(ctx, func(txCtx context.Context) error {
|
2023-02-04 14:48:38 +08:00
|
|
|
if err = repo_module.CreateRepositoryByExample(txCtx, doer, owner, repo, false, true); err != nil {
|
2020-01-12 20:11:17 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-06 16:01:49 +08:00
|
|
|
if err = repo_model.IncrementRepoForkNum(txCtx, opts.BaseRepo.ID); err != nil {
|
2020-01-12 20:11:17 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-15 05:15:28 +08:00
|
|
|
// copy lfs files failure should not be ignored
|
2022-06-12 23:51:54 +08:00
|
|
|
if err = git_model.CopyLFS(txCtx, repo, opts.BaseRepo); err != nil {
|
2021-04-15 05:15:28 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:16:40 +08:00
|
|
|
needsRollback = true
|
|
|
|
|
2023-09-29 09:48:39 +08:00
|
|
|
cloneCmd := git.NewCommand(txCtx, "clone", "--bare")
|
|
|
|
if opts.SingleBranch != "" {
|
|
|
|
cloneCmd.AddArguments("--single-branch", "--branch").AddDynamicArguments(opts.SingleBranch)
|
|
|
|
}
|
2021-12-10 09:27:50 +08:00
|
|
|
repoPath := repo_model.RepoPath(owner.Name, repo.Name)
|
2023-09-29 09:48:39 +08:00
|
|
|
if stdout, _, err := cloneCmd.AddDynamicArguments(oldRepoPath, repoPath).
|
2021-08-28 16:37:14 +08:00
|
|
|
SetDescription(fmt.Sprintf("ForkRepository(git clone): %s to %s", opts.BaseRepo.FullName(), repo.FullName())).
|
2022-04-01 10:55:30 +08:00
|
|
|
RunStdBytes(&git.RunOpts{Timeout: 10 * time.Minute}); err != nil {
|
2021-08-28 16:37:14 +08:00
|
|
|
log.Error("Fork Repository (git clone) Failed for %v (from %v):\nStdout: %s\nError: %v", repo, opts.BaseRepo, stdout, err)
|
2022-10-25 03:29:17 +08:00
|
|
|
return fmt.Errorf("git clone: %w", err)
|
2020-01-12 20:11:17 +08:00
|
|
|
}
|
|
|
|
|
2022-06-06 16:01:49 +08:00
|
|
|
if err := repo_module.CheckDaemonExportOK(txCtx, repo); err != nil {
|
2022-10-25 03:29:17 +08:00
|
|
|
return fmt.Errorf("checkDaemonExportOK: %w", err)
|
2021-10-14 03:47:02 +08:00
|
|
|
}
|
|
|
|
|
2022-04-01 10:55:30 +08:00
|
|
|
if stdout, _, err := git.NewCommand(txCtx, "update-server-info").
|
2020-01-12 20:11:17 +08:00
|
|
|
SetDescription(fmt.Sprintf("ForkRepository(git update-server-info): %s", repo.FullName())).
|
2022-04-01 10:55:30 +08:00
|
|
|
RunStdString(&git.RunOpts{Dir: repoPath}); err != nil {
|
2020-01-12 20:11:17 +08:00
|
|
|
log.Error("Fork Repository (git update-server-info) failed for %v:\nStdout: %s\nError: %v", repo, stdout, err)
|
2022-10-25 03:29:17 +08:00
|
|
|
return fmt.Errorf("git update-server-info: %w", err)
|
2020-01-12 20:11:17 +08:00
|
|
|
}
|
|
|
|
|
2021-11-16 21:30:11 +08:00
|
|
|
if err = repo_module.CreateDelegateHooks(repoPath); err != nil {
|
2022-10-25 03:29:17 +08:00
|
|
|
return fmt.Errorf("createDelegateHooks: %w", err)
|
2020-01-12 20:11:17 +08:00
|
|
|
}
|
2023-06-29 18:03:20 +08:00
|
|
|
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 04:09:51 +08:00
|
|
|
gitRepo, err := gitrepo.OpenRepository(txCtx, repo)
|
2023-06-29 18:03:20 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("OpenRepository: %w", err)
|
|
|
|
}
|
|
|
|
defer gitRepo.Close()
|
|
|
|
|
|
|
|
_, err = repo_module.SyncRepoBranchesWithRepo(txCtx, repo, gitRepo, doer.ID)
|
|
|
|
return err
|
2020-01-12 20:11:17 +08:00
|
|
|
})
|
2021-09-15 00:16:40 +08:00
|
|
|
needsRollbackInPanic = false
|
2020-01-12 20:11:17 +08:00
|
|
|
if err != nil {
|
2021-09-15 00:16:40 +08:00
|
|
|
rollbackFn()
|
2020-01-12 20:11:17 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-15 05:15:28 +08:00
|
|
|
// even if below operations failed, it could be ignored. And they will be retried
|
2022-06-06 16:01:49 +08:00
|
|
|
if err := repo_module.UpdateRepoSize(ctx, repo); err != nil {
|
2020-01-12 20:11:17 +08:00
|
|
|
log.Error("Failed to update size for repository: %v", err)
|
|
|
|
}
|
2023-10-11 12:24:07 +08:00
|
|
|
if err := repo_model.CopyLanguageStat(ctx, opts.BaseRepo, repo); err != nil {
|
2022-02-12 11:18:06 +08:00
|
|
|
log.Error("Copy language stat from oldRepo failed: %v", err)
|
|
|
|
}
|
|
|
|
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 04:09:51 +08:00
|
|
|
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
|
2022-02-12 11:18:06 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Open created git repository failed: %v", err)
|
|
|
|
} else {
|
|
|
|
defer gitRepo.Close()
|
2023-09-25 21:17:37 +08:00
|
|
|
if err := repo_module.SyncReleasesWithTags(ctx, repo, gitRepo); err != nil {
|
2022-02-12 11:18:06 +08:00
|
|
|
log.Error("Sync releases from git tags failed: %v", err)
|
|
|
|
}
|
2020-04-08 20:13:04 +08:00
|
|
|
}
|
2020-09-25 12:09:23 +08:00
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
notify_service.ForkRepository(ctx, doer, opts.BaseRepo, repo)
|
2021-11-16 21:30:11 +08:00
|
|
|
|
2020-09-25 12:09:23 +08:00
|
|
|
return repo, nil
|
2020-01-12 20:11:17 +08:00
|
|
|
}
|
2021-09-15 01:07:08 +08:00
|
|
|
|
|
|
|
// ConvertForkToNormalRepository convert the provided repo from a forked repo to normal repo
|
2023-03-01 06:17:51 +08:00
|
|
|
func ConvertForkToNormalRepository(ctx context.Context, repo *repo_model.Repository) error {
|
|
|
|
err := db.WithTx(ctx, func(ctx context.Context) error {
|
2022-12-03 10:48:26 +08:00
|
|
|
repo, err := repo_model.GetRepositoryByID(ctx, repo.ID)
|
2021-09-15 01:07:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !repo.IsFork {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-06 16:01:49 +08:00
|
|
|
if err := repo_model.DecrementRepoForkNum(ctx, repo.ForkID); err != nil {
|
2021-09-15 01:07:08 +08:00
|
|
|
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 16:01:49 +08:00
|
|
|
if err := repo_module.UpdateRepository(ctx, repo, false); err != nil {
|
2021-09-15 01:07:08 +08:00
|
|
|
log.Error("Unable to update repository %-v whilst converting from fork. Error: %v", repo, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2024-11-19 12:08:32 +08:00
|
|
|
|
|
|
|
type findForksOptions struct {
|
|
|
|
db.ListOptions
|
|
|
|
RepoID int64
|
|
|
|
Doer *user_model.User
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opts findForksOptions) ToConds() builder.Cond {
|
|
|
|
return builder.Eq{"fork_id": opts.RepoID}.And(
|
|
|
|
repo_model.AccessibleRepositoryCondition(opts.Doer, unit.TypeInvalid),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindForks returns all the forks of the repository
|
|
|
|
func FindForks(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, listOptions db.ListOptions) ([]*repo_model.Repository, int64, error) {
|
|
|
|
return db.FindAndCount[repo_model.Repository](ctx, findForksOptions{
|
|
|
|
ListOptions: listOptions,
|
|
|
|
RepoID: repo.ID,
|
|
|
|
Doer: doer,
|
|
|
|
})
|
|
|
|
}
|