1
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-01-04 09:17:43 +03:00

revert change to fork

This commit is contained in:
Lunny Xiao 2024-12-12 12:21:40 -08:00
parent 743cebe66f
commit 050fad97d6
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
5 changed files with 32 additions and 23 deletions

View File

@ -21,17 +21,15 @@ func GetRepositoriesByForkID(ctx context.Context, forkID int64) ([]*Repository,
}
// GetForkedRepo checks if given user has already forked a repository with given ID.
func GetForkedRepo(ctx context.Context, ownerID, repoID int64) (*Repository, error) {
func GetForkedRepo(ctx context.Context, ownerID, repoID int64) *Repository {
repo := new(Repository)
has, err := db.GetEngine(ctx).
has, _ := db.GetEngine(ctx).
Where("owner_id=? AND fork_id=?", ownerID, repoID).
Get(repo)
if err != nil {
return nil, err
} else if !has {
return nil, ErrRepoNotExist{ID: repoID}
if has {
return repo
}
return repo, nil
return nil
}
// HasForkedRepo checks if given user has already forked a repository with given ID.
@ -43,6 +41,19 @@ func HasForkedRepo(ctx context.Context, ownerID, repoID int64) bool {
return has
}
// GetUserFork return user forked repository from this repository, if not forked return nil
func GetUserFork(ctx context.Context, repoID, userID int64) (*Repository, error) {
var forkedRepo Repository
has, err := db.GetEngine(ctx).Where("fork_id = ?", repoID).And("owner_id = ?", userID).Get(&forkedRepo)
if err != nil {
return nil, err
}
if !has {
return nil, nil
}
return &forkedRepo, nil
}
// IncrementRepoForkNum increment repository fork number
func IncrementRepoForkNum(ctx context.Context, repoID int64) error {
_, err := db.GetEngine(ctx).Exec("UPDATE `repository` SET num_forks=num_forks+1 WHERE id=?", repoID)
@ -76,8 +87,8 @@ func GetForksByUserAndOrgs(ctx context.Context, user *user_model.User, repo *Rep
if user == nil {
return repoList, nil
}
forkedRepo, err := GetForkedRepo(ctx, repo.ID, user.ID)
if err != nil && !IsErrRepoNotExist(err) {
forkedRepo, err := GetUserFork(ctx, repo.ID, user.ID)
if err != nil {
return repoList, err
}
if forkedRepo != nil {

View File

@ -20,14 +20,14 @@ func TestGetUserFork(t *testing.T) {
repo, err := repo_model.GetRepositoryByID(db.DefaultContext, 10)
assert.NoError(t, err)
assert.NotNil(t, repo)
repo, err = repo_model.GetForkedRepo(db.DefaultContext, repo.ID, 13)
repo, err = repo_model.GetUserFork(db.DefaultContext, repo.ID, 13)
assert.NoError(t, err)
assert.NotNil(t, repo)
repo, err = repo_model.GetRepositoryByID(db.DefaultContext, 9)
assert.NoError(t, err)
assert.NotNil(t, repo)
repo, err = repo_model.GetForkedRepo(db.DefaultContext, repo.ID, 13)
repo, err = repo_model.GetUserFork(db.DefaultContext, repo.ID, 13)
assert.NoError(t, err)
assert.Nil(t, repo)
}

View File

@ -119,6 +119,8 @@ func (ci *CompareInfo) Close() {
}
}
// detectFullRef detects a short name as a branch, tag or commit's full ref name and type.
// It's the same job as git.UnstableGuessRefByShortName but with a database read instead of git read.
func detectFullRef(ctx context.Context, repoID int64, gitRepo *git.Repository, oriRef string) (git.RefName, bool, error) {
b, err := git_model.GetBranch(ctx, repoID, oriRef)
if err != nil && !git_model.IsErrBranchNotExist(err) {
@ -166,13 +168,13 @@ func findHeadRepoFromRootBase(ctx context.Context, baseRepo *repo_model.Reposito
return nil, nil
}
// test if we are lucky
repo, err := repo_model.GetForkedRepo(ctx, headUserID, baseRepo.ID)
if err == nil {
return repo, nil
}
if !repo_model.IsErrRepoNotExist(err) {
repo, err := repo_model.GetUserFork(ctx, headUserID, baseRepo.ID)
if err != nil {
return nil, err
}
if repo != nil {
return repo, nil
}
firstLevelForkedRepo, err := repo_model.GetRepositoriesByForkID(ctx, baseRepo.ID)
if err != nil {

View File

@ -166,11 +166,7 @@ func ForkPost(ctx *context.Context) {
ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), tplFork, &form)
return
}
repo, err := repo_model.GetForkedRepo(ctx, ctxUser.ID, traverseParentRepo.ID)
if err != nil && !repo_model.IsErrRepoNotExist(err) {
ctx.ServerError("GetForkedRepo", err)
return
}
repo := repo_model.GetForkedRepo(ctx, ctxUser.ID, traverseParentRepo.ID)
if repo != nil {
ctx.Redirect(ctxUser.HomeLink() + "/" + url.PathEscape(repo.Name))
return

View File

@ -71,8 +71,8 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork
}
}
forkedRepo, err := repo_model.GetForkedRepo(ctx, opts.BaseRepo.ID, owner.ID)
if err != nil && !repo_model.IsErrRepoNotExist(err) {
forkedRepo, err := repo_model.GetUserFork(ctx, opts.BaseRepo.ID, owner.ID)
if err != nil {
return nil, err
}
if forkedRepo != nil {