From ec748ea65d99d6d25efabc1d9dffb881d170b894 Mon Sep 17 00:00:00 2001 From: JakobDev Date: Fri, 3 May 2024 14:57:47 +0200 Subject: [PATCH] Update --- modules/structs/fork.go | 7 +++--- options/locale/locale_en-US.ini | 3 ++- routers/api/v1/repo/sync_fork.go | 2 +- routers/web/repo/view.go | 1 + services/repository/sync_fork.go | 37 +++++++++++--------------------- templates/repo/home.tmpl | 12 +++++++---- 6 files changed, 28 insertions(+), 34 deletions(-) diff --git a/modules/structs/fork.go b/modules/structs/fork.go index 3c46102245..8fc73647bd 100644 --- a/modules/structs/fork.go +++ b/modules/structs/fork.go @@ -13,7 +13,8 @@ type CreateForkOption struct { // SyncForkInfo information about syncing a fork type SyncForkInfo struct { - Allowed bool `json:"allowed"` - ForkCommit string `json:"fork_commit"` - BaseCommit string `json:"base_commit"` + Allowed bool `json:"allowed"` + ForkCommit string `json:"fork_commit"` + BaseCommit string `json:"base_commit"` + CommitsBehind int `json:"commits_behind"` } diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 48a5bcab38..c5afbaf7d2 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1145,7 +1145,8 @@ archive.title_date = This repository has been archived on %s. You can view files archive.issue.nocomment = This repo is archived. You cannot comment on issues. archive.pull.nocomment = This repo is archived. You cannot comment on pull requests. -sync_fork.text = This branch is behind %s +sync_fork.text_1 = This branch is 1 commit behind %s +sync_fork.text_n = This branch is %d commits behind %s sync_fork.button = Sync form.reach_limit_of_creation_1 = The owner has already reached the limit of %d repository. diff --git a/routers/api/v1/repo/sync_fork.go b/routers/api/v1/repo/sync_fork.go index a6e2a57129..359b4e0a74 100644 --- a/routers/api/v1/repo/sync_fork.go +++ b/routers/api/v1/repo/sync_fork.go @@ -4,7 +4,7 @@ import ( "net/http" git_model "code.gitea.io/gitea/models/git" - "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/services/context" repo_service "code.gitea.io/gitea/services/repository" ) diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index 2f47a1b32c..2e204b763f 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -1146,6 +1146,7 @@ PostRecentBranchCheck: 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)) } diff --git a/services/repository/sync_fork.go b/services/repository/sync_fork.go index c87aa06c06..5e12c2cf92 100644 --- a/services/repository/sync_fork.go +++ b/services/repository/sync_fork.go @@ -27,30 +27,7 @@ func SyncFork(ctx context.Context, doer *user_model.User, repo *repo_model.Repos return err } - repo.RepoPath() - - err = git.NewCommand(ctx, "fetch", "--force").AddDynamicArguments(repo.BaseRepo.RepoPath(), fmt.Sprintf("%s:%s", branch, branch)).Run(&git.RunOpts{Dir: repo.RepoPath()}) - if err != nil { - return err - } - - gitRepo, err := git.OpenRepository(ctx, repo.RepoPath()) - if err != nil { - return err - } - defer gitRepo.Close() - - forkBranch, err := gitRepo.GetBranch(branch) - if err != nil { - return err - } - - commit, err := forkBranch.GetCommit() - if err != nil { - return err - } - - _, err = git_model.UpdateBranch(ctx, repo.ID, doer.ID, branch, commit) + err = git.NewCommand(ctx, "fetch").AddDynamicArguments(repo.BaseRepo.RepoPath(), fmt.Sprintf("%s:%s", branch, branch)).Run(&git.RunOpts{Dir: repo.RepoPath()}) if err != nil { return err } @@ -123,7 +100,17 @@ func GetSyncForkInfo(ctx context.Context, repo *repo_model.Repository, branch st return nil, err } - info.Allowed = slices.Contains(branchList, branch) + 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 } diff --git a/templates/repo/home.tmpl b/templates/repo/home.tmpl index b5075c0e03..6eebdb19ff 100644 --- a/templates/repo/home.tmpl +++ b/templates/repo/home.tmpl @@ -163,11 +163,15 @@ {{if .CanSyncFork}} -
-
- {{ctx.Locale.Tr "repo.sync_fork.text" (printf "%s:%s" .BaseBranchLink .Repository.BaseRepo.FullName (.BranchName|Escape) | Safe)}} +
+
+ {{if eq .ForkCommitsBehind 1}} + {{ctx.Locale.Tr "repo.sync_fork.text_1" (printf "%s:%s" .BaseBranchLink .Repository.BaseRepo.FullName .BranchName | SafeHTML)}} + {{else}} + {{ctx.Locale.Tr "repo.sync_fork.text_n" .ForkCommitsBehind (printf "%s:%s" .BaseBranchLink .Repository.BaseRepo.FullName .BranchName | SafeHTML)}} + {{end}}
- + {{ctx.Locale.Tr "repo.sync_fork.button"}}