mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-04-19 13:39:26 +00:00
Some checks failed
/ release (push) Waiting to run
testing / backend-checks (push) Has been skipped
testing / frontend-checks (push) Has been skipped
testing / test-unit (push) Has been skipped
testing / test-e2e (push) Has been skipped
testing / test-mysql (push) Has been skipped
testing / test-pgsql (push) Has been skipped
testing / test-sqlite (push) Has been skipped
testing / test-remote-cacher (redis) (push) Has been skipped
testing / test-remote-cacher (valkey) (push) Has been skipped
testing / test-remote-cacher (garnet) (push) Has been skipped
testing / test-remote-cacher (redict) (push) Has been skipped
testing / security-check (push) Has been skipped
Integration tests for the release process / release-simulation (push) Has been cancelled
This allows syncing a branch of a fork with a branch of the base repo. It looks like this:  This is only possible, if the fork don't have commits that are not in the main repo. The feature is already working, but it is missing Finetuning, a better API, translations and tests, so this is currently WIP. It is also not tested with go-git. <!--start release-notes-assistant--> ## Release notes <!--URL:https://codeberg.org/forgejo/forgejo--> - Features - [PR](https://codeberg.org/forgejo/forgejo/pulls/2364): <!--number 2364 --><!--line 0 --><!--description c3luYyBmb3Jrcw==-->sync forks<!--description--> <!--end release-notes-assistant--> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2364 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: JakobDev <jakobdev@gmx.de> Co-committed-by: JakobDev <jakobdev@gmx.de>
113 lines
2.5 KiB
Go
113 lines
2.5 KiB
Go
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repository
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"slices"
|
|
|
|
git_model "forgejo.org/models/git"
|
|
repo_model "forgejo.org/models/repo"
|
|
user_model "forgejo.org/models/user"
|
|
"forgejo.org/modules/git"
|
|
repo_module "forgejo.org/modules/repository"
|
|
api "forgejo.org/modules/structs"
|
|
)
|
|
|
|
// 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 {
|
|
err := repo.MustNotBeArchived()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = repo.GetBaseRepo(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = git.Push(ctx, repo.BaseRepo.RepoPath(), git.PushOptions{
|
|
Remote: repo.RepoPath(),
|
|
Branch: fmt.Sprintf("%s:%s", branch, branch),
|
|
Env: repo_module.PushingEnvironment(doer, repo),
|
|
})
|
|
|
|
return err
|
|
}
|
|
|
|
// CanSyncFork returns information about syncing a fork
|
|
func GetSyncForkInfo(ctx context.Context, repo *repo_model.Repository, branch string) (*api.SyncForkInfo, error) {
|
|
info := new(api.SyncForkInfo)
|
|
|
|
if !repo.IsFork {
|
|
return info, nil
|
|
}
|
|
|
|
if repo.IsArchived {
|
|
return info, nil
|
|
}
|
|
|
|
err := repo.GetBaseRepo(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
forkBranch, err := git_model.GetBranch(ctx, repo.ID, branch)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
info.ForkCommit = forkBranch.CommitID
|
|
|
|
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
|
|
return info, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
info.BaseCommit = baseBranch.CommitID
|
|
|
|
// If both branches has the same latest commit, we don't need to sync
|
|
if forkBranch.CommitID == baseBranch.CommitID {
|
|
return info, nil
|
|
}
|
|
|
|
// Check if the latest commit of the fork is also in the base
|
|
gitRepo, err := git.OpenRepository(ctx, repo.BaseRepo.RepoPath())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer gitRepo.Close()
|
|
|
|
commit, err := gitRepo.GetCommit(forkBranch.CommitID)
|
|
if err != nil {
|
|
if git.IsErrNotExist(err) {
|
|
return info, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
branchList, err := commit.GetAllBranches()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !slices.Contains(branchList, branch) {
|
|
return info, nil
|
|
}
|
|
|
|
diff, err := git.GetDivergingCommits(ctx, repo.BaseRepo.RepoPath(), baseBranch.CommitID, forkBranch.CommitID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
info.Allowed = true
|
|
info.CommitsBehind = diff.Behind
|
|
|
|
return info, nil
|
|
}
|