feat: sync forks (#2364)
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>
This commit is contained in:
parent
3272e3588a
commit
8296a23d79
15 changed files with 723 additions and 4 deletions
|
@ -1355,6 +1355,12 @@ func Routes() *web.Route {
|
|||
m.Post("", bind(api.UpdateRepoAvatarOption{}), repo.UpdateAvatar)
|
||||
m.Delete("", repo.DeleteAvatar)
|
||||
}, reqAdmin(), reqToken())
|
||||
m.Group("/sync_fork", func() {
|
||||
m.Get("", reqRepoReader(unit.TypeCode), repo.SyncForkDefaultInfo)
|
||||
m.Post("", mustNotBeArchived, reqRepoWriter(unit.TypeCode), repo.SyncForkDefault)
|
||||
m.Get("/{branch}", reqRepoReader(unit.TypeCode), repo.SyncForkBranchInfo)
|
||||
m.Post("/{branch}", mustNotBeArchived, reqRepoWriter(unit.TypeCode), repo.SyncForkBranch)
|
||||
})
|
||||
|
||||
m.Get("/{ball_type:tarball|zipball|bundle}/*", reqRepoReader(unit.TypeCode), repo.DownloadArchive)
|
||||
}, repoAssignment(), checkTokenPublicOnly())
|
||||
|
|
185
routers/api/v1/repo/sync_fork.go
Normal file
185
routers/api/v1/repo/sync_fork.go
Normal file
|
@ -0,0 +1,185 @@
|
|||
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package repo
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
git_model "forgejo.org/models/git"
|
||||
"forgejo.org/services/context"
|
||||
repo_service "forgejo.org/services/repository"
|
||||
)
|
||||
|
||||
func getSyncForkInfo(ctx *context.APIContext, branch string) {
|
||||
if !ctx.Repo.Repository.IsFork {
|
||||
ctx.Error(http.StatusBadRequest, "NoFork", "The Repo must be a fork")
|
||||
return
|
||||
}
|
||||
|
||||
syncForkInfo, err := repo_service.GetSyncForkInfo(ctx, ctx.Repo.Repository, branch)
|
||||
if err != nil {
|
||||
if git_model.IsErrBranchNotExist(err) {
|
||||
ctx.NotFound(err, branch)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Error(http.StatusInternalServerError, "GetSyncForkInfo", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, syncForkInfo)
|
||||
}
|
||||
|
||||
// SyncForkBranchInfo returns information about syncing the default fork branch with the base branch
|
||||
func SyncForkDefaultInfo(ctx *context.APIContext) {
|
||||
// swagger:operation GET /repos/{owner}/{repo}/sync_fork repository repoSyncForkDefaultInfo
|
||||
// ---
|
||||
// summary: Gets information about syncing the fork default branch with the base branch
|
||||
// produces:
|
||||
// - application/json
|
||||
// parameters:
|
||||
// - name: owner
|
||||
// in: path
|
||||
// description: owner of the repo
|
||||
// type: string
|
||||
// required: true
|
||||
// - name: repo
|
||||
// in: path
|
||||
// description: name of the repo
|
||||
// type: string
|
||||
// required: true
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/SyncForkInfo"
|
||||
// "400":
|
||||
// "$ref": "#/responses/error"
|
||||
// "404":
|
||||
// "$ref": "#/responses/notFound"
|
||||
getSyncForkInfo(ctx, ctx.Repo.Repository.DefaultBranch)
|
||||
}
|
||||
|
||||
// SyncForkBranchInfo returns information about syncing a fork branch with the base branch
|
||||
func SyncForkBranchInfo(ctx *context.APIContext) {
|
||||
// swagger:operation GET /repos/{owner}/{repo}/sync_fork/{branch} repository repoSyncForkBranchInfo
|
||||
// ---
|
||||
// summary: Gets information about syncing a fork branch with the base branch
|
||||
// produces:
|
||||
// - application/json
|
||||
// parameters:
|
||||
// - name: owner
|
||||
// in: path
|
||||
// description: owner of the repo
|
||||
// type: string
|
||||
// required: true
|
||||
// - name: repo
|
||||
// in: path
|
||||
// description: name of the repo
|
||||
// type: string
|
||||
// required: true
|
||||
// - name: branch
|
||||
// in: path
|
||||
// description: The branch
|
||||
// type: string
|
||||
// required: true
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/SyncForkInfo"
|
||||
// "400":
|
||||
// "$ref": "#/responses/error"
|
||||
// "404":
|
||||
// "$ref": "#/responses/notFound"
|
||||
getSyncForkInfo(ctx, ctx.Params("branch"))
|
||||
}
|
||||
|
||||
func syncForkBranch(ctx *context.APIContext, branch string) {
|
||||
if !ctx.Repo.Repository.IsFork {
|
||||
ctx.Error(http.StatusBadRequest, "NoFork", "The Repo must be a fork")
|
||||
return
|
||||
}
|
||||
|
||||
syncForkInfo, err := repo_service.GetSyncForkInfo(ctx, ctx.Repo.Repository, branch)
|
||||
if err != nil {
|
||||
if git_model.IsErrBranchNotExist(err) {
|
||||
ctx.NotFound(err, branch)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Error(http.StatusInternalServerError, "GetSyncForkInfo", err)
|
||||
return
|
||||
}
|
||||
|
||||
if !syncForkInfo.Allowed {
|
||||
ctx.Error(http.StatusBadRequest, "NotAllowed", "You can't sync this branch")
|
||||
return
|
||||
}
|
||||
|
||||
err = repo_service.SyncFork(ctx, ctx.Doer, ctx.Repo.Repository, branch)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "SyncFork", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// SyncForkBranch syncs the default of a fork with the base branch
|
||||
func SyncForkDefault(ctx *context.APIContext) {
|
||||
// swagger:operation POST /repos/{owner}/{repo}/sync_fork repository repoSyncForkDefault
|
||||
// ---
|
||||
// summary: Syncs the default branch of a fork with the base branch
|
||||
// produces:
|
||||
// - application/json
|
||||
// parameters:
|
||||
// - name: owner
|
||||
// in: path
|
||||
// description: owner of the repo
|
||||
// type: string
|
||||
// required: true
|
||||
// - name: repo
|
||||
// in: path
|
||||
// description: name of the repo
|
||||
// type: string
|
||||
// required: true
|
||||
// responses:
|
||||
// "204":
|
||||
// "$ref": "#/responses/empty"
|
||||
// "400":
|
||||
// "$ref": "#/responses/error"
|
||||
// "404":
|
||||
// "$ref": "#/responses/notFound"
|
||||
syncForkBranch(ctx, ctx.Repo.Repository.DefaultBranch)
|
||||
}
|
||||
|
||||
// SyncForkBranch syncs a fork branch with the base branch
|
||||
func SyncForkBranch(ctx *context.APIContext) {
|
||||
// swagger:operation POST /repos/{owner}/{repo}/sync_fork/{branch} repository repoSyncForkBranch
|
||||
// ---
|
||||
// summary: Syncs a fork branch with the base branch
|
||||
// produces:
|
||||
// - application/json
|
||||
// parameters:
|
||||
// - name: owner
|
||||
// in: path
|
||||
// description: owner of the repo
|
||||
// type: string
|
||||
// required: true
|
||||
// - name: repo
|
||||
// in: path
|
||||
// description: name of the repo
|
||||
// type: string
|
||||
// required: true
|
||||
// - name: branch
|
||||
// in: path
|
||||
// description: The branch
|
||||
// type: string
|
||||
// required: true
|
||||
// responses:
|
||||
// "204":
|
||||
// "$ref": "#/responses/empty"
|
||||
// "400":
|
||||
// "$ref": "#/responses/error"
|
||||
// "404":
|
||||
// "$ref": "#/responses/notFound"
|
||||
syncForkBranch(ctx, ctx.Params("branch"))
|
||||
}
|
|
@ -448,3 +448,10 @@ type swaggerCompare struct {
|
|||
// in:body
|
||||
Body api.Compare `json:"body"`
|
||||
}
|
||||
|
||||
// SyncForkInfo
|
||||
// swagger:response SyncForkInfo
|
||||
type swaggerSyncForkInfo struct {
|
||||
// in:body
|
||||
Body []api.SyncForkInfo `json:"body"`
|
||||
}
|
||||
|
|
|
@ -782,3 +782,27 @@ func PrepareBranchList(ctx *context.Context) {
|
|||
}
|
||||
ctx.Data["Branches"] = brs
|
||||
}
|
||||
|
||||
func SyncFork(ctx *context.Context) {
|
||||
redirectURL := fmt.Sprintf("%s/src/branch/%s", ctx.Repo.RepoLink, util.PathEscapeSegments(ctx.Repo.BranchName))
|
||||
branch := ctx.Params("branch")
|
||||
|
||||
syncForkInfo, err := repo_service.GetSyncForkInfo(ctx, ctx.Repo.Repository, branch)
|
||||
if err != nil {
|
||||
ctx.ServerError("GetSyncForkInfo", err)
|
||||
return
|
||||
}
|
||||
|
||||
if !syncForkInfo.Allowed {
|
||||
ctx.Redirect(redirectURL)
|
||||
return
|
||||
}
|
||||
|
||||
err = repo_service.SyncFork(ctx, ctx.Doer, ctx.Repo.Repository, branch)
|
||||
if err != nil {
|
||||
ctx.ServerError("SyncFork", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Redirect(redirectURL)
|
||||
}
|
||||
|
|
|
@ -52,6 +52,7 @@ import (
|
|||
"forgejo.org/routers/web/feed"
|
||||
"forgejo.org/services/context"
|
||||
issue_service "forgejo.org/services/issue"
|
||||
repo_service "forgejo.org/services/repository"
|
||||
files_service "forgejo.org/services/repository/files"
|
||||
|
||||
"github.com/nektos/act/pkg/model"
|
||||
|
@ -1154,6 +1155,21 @@ PostRecentBranchCheck:
|
|||
}
|
||||
}
|
||||
|
||||
if ctx.Repo.Repository.IsFork && ctx.Repo.IsViewBranch && len(ctx.Repo.TreePath) == 0 && ctx.Repo.CanWriteToBranch(ctx, ctx.Doer, ctx.Repo.BranchName) {
|
||||
syncForkInfo, err := repo_service.GetSyncForkInfo(ctx, ctx.Repo.Repository, ctx.Repo.BranchName)
|
||||
if err != nil {
|
||||
ctx.ServerError("CanSync", err)
|
||||
return
|
||||
}
|
||||
|
||||
if syncForkInfo.Allowed {
|
||||
ctx.Data["CanSyncFork"] = true
|
||||
ctx.Data["ForkCommitsBehind"] = syncForkInfo.CommitsBehind
|
||||
ctx.Data["SyncForkLink"] = fmt.Sprintf("%s/sync_fork/%s", ctx.Repo.RepoLink, util.PathEscapeSegments(ctx.Repo.BranchName))
|
||||
ctx.Data["BaseBranchLink"] = fmt.Sprintf("%s/src/branch/%s", ctx.Repo.Repository.BaseRepo.HTMLURL(), util.PathEscapeSegments(ctx.Repo.BranchName))
|
||||
}
|
||||
}
|
||||
|
||||
ctx.Data["Paths"] = paths
|
||||
|
||||
branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()
|
||||
|
|
|
@ -1592,6 +1592,8 @@ func registerRoutes(m *web.Route) {
|
|||
}, context.RepoRef(), reqRepoCodeReader)
|
||||
}
|
||||
m.Get("/commit/{sha:([a-f0-9]{4,64})}.{ext:patch|diff}", repo.MustBeNotEmpty, reqRepoCodeReader, repo.RawDiff)
|
||||
|
||||
m.Get("/sync_fork/{branch}", context.RepoMustNotBeArchived(), repo.MustBeNotEmpty, reqRepoCodeWriter, repo.SyncFork)
|
||||
}, ignSignIn, context.RepoAssignment, context.UnitTypes())
|
||||
|
||||
m.Post("/{username}/{reponame}/lastcommit/*", ignSignInAndCsrf, context.RepoAssignment, context.UnitTypes(), context.RepoRefByType(context.RepoRefCommit), reqRepoCodeReader, repo.LastCommit)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue