This commit is contained in:
JakobDev 2024-05-03 14:57:47 +02:00
parent 150ce04de9
commit ec748ea65d
No known key found for this signature in database
GPG key ID: 39DEF62C3ED6DC4C
6 changed files with 28 additions and 34 deletions

View file

@ -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"`
}

View file

@ -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.

View file

@ -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"
)

View file

@ -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))
}

View file

@ -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
}

View file

@ -163,11 +163,15 @@
</div>
{{if .CanSyncFork}}
<div class="ui positive message gt-df gt-ac">
<div class="gt-f1">
{{ctx.Locale.Tr "repo.sync_fork.text" (printf "<a href='%s'>%s:%s</a>" .BaseBranchLink .Repository.BaseRepo.FullName (.BranchName|Escape) | Safe)}}
<div class="ui positive message tw-flex tw-items-center">
<div class="tw-flex-1">
{{if eq .ForkCommitsBehind 1}}
{{ctx.Locale.Tr "repo.sync_fork.text_1" (printf "<a href='%s'>%s:%s</a>" .BaseBranchLink .Repository.BaseRepo.FullName .BranchName | SafeHTML)}}
{{else}}
{{ctx.Locale.Tr "repo.sync_fork.text_n" .ForkCommitsBehind (printf "<a href='%s'>%s:%s</a>" .BaseBranchLink .Repository.BaseRepo.FullName .BranchName | SafeHTML)}}
{{end}}
</div>
<a role="button" class="ui compact positive button gt-m-0" href="{{.SyncForkLink}}">
<a role="button" class="ui compact positive button tw-m-0" href="{{.SyncForkLink}}">
{{ctx.Locale.Tr "repo.sync_fork.button"}}
</a>
</div>