Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-12-13 15:01:58 +01:00
forgejo/services/repository/sync_fork.go

130 lines
2.8 KiB
Go
Raw Normal View History

2024-02-19 16:38:44 +01:00
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2024-02-19 14:11:26 +01:00
package repository
import (
"context"
"fmt"
"slices"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
2024-02-19 16:38:44 +01:00
api "code.gitea.io/gitea/modules/structs"
2024-02-19 14:11:26 +01:00
)
// SyncFork syncs a branch of a fork with the base repo
func SyncFork(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, branch string) error {
2024-03-16 12:48:00 +01:00
err := repo.MustNotBeArchived()
2024-02-19 14:11:26 +01:00
if err != nil {
return err
}
2024-03-16 12:48:00 +01:00
err = repo.GetBaseRepo(ctx)
2024-02-19 14:11:26 +01:00
if err != nil {
return err
}
2024-03-16 12:48:00 +01:00
repo.RepoPath()
err = git.NewCommand(ctx, "fetch", "--force").AddDynamicArguments(repo.BaseRepo.RepoPath(), fmt.Sprintf("%s:%s", branch, branch)).Run(&git.RunOpts{Dir: repo.RepoPath()})
2024-02-19 14:11:26 +01:00
if err != nil {
2024-02-19 16:38:44 +01:00
return err
2024-02-19 14:11:26 +01:00
}
2024-03-16 12:48:00 +01:00
gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
2024-02-19 14:11:26 +01:00
if err != nil {
return err
}
defer gitRepo.Close()
2024-03-16 12:48:00 +01:00
forkBranch, err := gitRepo.GetBranch(branch)
2024-02-19 14:11:26 +01:00
if err != nil {
2024-03-16 12:48:00 +01:00
return err
2024-02-19 14:11:26 +01:00
}
2024-03-16 12:48:00 +01:00
commit, err := forkBranch.GetCommit()
2024-02-19 14:11:26 +01:00
if err != nil {
2024-03-16 12:48:00 +01:00
return err
2024-02-19 14:11:26 +01:00
}
2024-03-16 12:48:00 +01:00
_, err = git_model.UpdateBranch(ctx, repo.ID, doer.ID, branch, commit)
2024-02-19 14:11:26 +01:00
if err != nil {
2024-03-16 12:48:00 +01:00
return err
2024-02-19 14:11:26 +01:00
}
return nil
}
2024-03-16 12:48:00 +01:00
// CanSyncFork returns information about syncing a fork
2024-02-19 16:38:44 +01:00
func GetSyncForkInfo(ctx context.Context, repo *repo_model.Repository, branch string) (*api.SyncForkInfo, error) {
info := new(api.SyncForkInfo)
if !repo.IsFork {
return info, nil
}
2024-03-16 12:48:00 +01:00
if repo.IsArchived {
return info, nil
}
2024-02-19 16:38:44 +01:00
err := repo.GetBaseRepo(ctx)
if err != nil {
return nil, err
}
2024-02-19 14:11:26 +01:00
forkBranch, err := git_model.GetBranch(ctx, repo.ID, branch)
if err != nil {
2024-02-19 16:38:44 +01:00
return nil, err
2024-02-19 14:11:26 +01:00
}
2024-02-19 16:38:44 +01:00
info.ForkCommit = forkBranch.CommitID
2024-02-19 14:11:26 +01:00
baseBranch, err := git_model.GetBranch(ctx, repo.BaseRepo.ID, branch)
if err != nil {
if git_model.IsErrBranchNotExist(err) {
// If the base repo don't have the branch, we don't need to continue
2024-02-19 16:38:44 +01:00
return info, nil
2024-02-19 14:11:26 +01:00
}
2024-02-19 16:38:44 +01:00
return nil, err
2024-02-19 14:11:26 +01:00
}
2024-02-19 16:38:44 +01:00
info.BaseCommit = baseBranch.CommitID
2024-02-19 14:11:26 +01:00
// If both branches has the same latest commit, we don't need to sync
if forkBranch.CommitID == baseBranch.CommitID {
2024-02-19 16:38:44 +01:00
return info, nil
2024-02-19 14:11:26 +01:00
}
// If the fork has newer commits, we can't sync
if forkBranch.CommitTime >= baseBranch.CommitTime {
2024-02-19 16:38:44 +01:00
return info, nil
2024-02-19 14:11:26 +01:00
}
// Check if the latest commit of the fork is also in the base
gitRepo, err := git.OpenRepository(ctx, repo.BaseRepo.RepoPath())
if err != nil {
2024-02-19 16:38:44 +01:00
return nil, err
2024-02-19 14:11:26 +01:00
}
defer gitRepo.Close()
commit, err := gitRepo.GetCommit(forkBranch.CommitID)
if err != nil {
if git.IsErrNotExist(err) {
2024-02-19 16:38:44 +01:00
return info, nil
2024-02-19 14:11:26 +01:00
}
2024-02-19 16:38:44 +01:00
return nil, err
2024-02-19 14:11:26 +01:00
}
branchList, err := commit.GetAllBranches()
if err != nil {
2024-02-19 16:38:44 +01:00
return nil, err
2024-02-19 14:11:26 +01:00
}
2024-02-19 16:38:44 +01:00
info.Allowed = slices.Contains(branchList, branch)
return info, nil
2024-02-19 14:11:26 +01:00
}