Skip to content

Commit f4e286b

Browse files
committed
refactor
1 parent 5ba8849 commit f4e286b

File tree

10 files changed

+97
-79
lines changed

10 files changed

+97
-79
lines changed

models/git/branch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ type RecentlyPushedNewBranch struct {
472472
// if opts.CommitAfterUnix is 0, we will find the branches that were committed to in the last 2 hours
473473
// if opts.ListOptions is not set, we will only display top 2 latest branches.
474474
// Protected branches will be skipped since they are unlikely to be used to create new PRs.
475-
func FindRecentlyPushedNewBranches(ctx context.Context, doer *user_model.User, opts *FindRecentlyPushedNewBranchesOptions) ([]*RecentlyPushedNewBranch, error) {
475+
func FindRecentlyPushedNewBranches(ctx context.Context, doer *user_model.User, opts FindRecentlyPushedNewBranchesOptions) ([]*RecentlyPushedNewBranch, error) {
476476
if doer == nil {
477477
return []*RecentlyPushedNewBranch{}, nil
478478
}

models/repo/repo.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,13 @@ func (repo *Repository) AllowsPulls(ctx context.Context) bool {
652652
}
653653

654654
// CanEnableEditor returns true if repository meets the requirements of web editor.
655+
// FIXME: most CanEnableEditor calls should be replaced with CanContentChange
656+
// And all other like CanCreateBranch / CanEnablePulls should also be updated
655657
func (repo *Repository) CanEnableEditor() bool {
658+
return repo.CanContentChange()
659+
}
660+
661+
func (repo *Repository) CanContentChange() bool {
656662
return !repo.IsMirror && !repo.IsArchived
657663
}
658664

routers/utils/branch.go

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package repo
5+
6+
import (
7+
git_model "code.gitea.io/gitea/models/git"
8+
access_model "code.gitea.io/gitea/models/perm/access"
9+
unit_model "code.gitea.io/gitea/models/unit"
10+
"code.gitea.io/gitea/modules/log"
11+
"code.gitea.io/gitea/services/context"
12+
repo_service "code.gitea.io/gitea/services/repository"
13+
)
14+
15+
type RecentBranchesPromptDataStruct struct {
16+
RecentlyPushedNewBranches []*git_model.RecentlyPushedNewBranch
17+
}
18+
19+
func prepareRecentlyPushedNewBranches(ctx *context.Context) {
20+
if ctx.Doer == nil {
21+
return
22+
}
23+
if err := ctx.Repo.Repository.GetBaseRepo(ctx); err != nil {
24+
log.Error("GetBaseRepo: %v", err)
25+
return
26+
}
27+
28+
opts := git_model.FindRecentlyPushedNewBranchesOptions{
29+
Repo: ctx.Repo.Repository,
30+
BaseRepo: ctx.Repo.Repository,
31+
}
32+
if ctx.Repo.Repository.IsFork {
33+
opts.BaseRepo = ctx.Repo.Repository.BaseRepo
34+
}
35+
36+
baseRepoPerm, err := access_model.GetUserRepoPermission(ctx, opts.BaseRepo, ctx.Doer)
37+
if err != nil {
38+
log.Error("GetUserRepoPermission: %v", err)
39+
return
40+
}
41+
if !opts.Repo.CanContentChange() || !opts.BaseRepo.CanContentChange() {
42+
return
43+
}
44+
if !opts.BaseRepo.UnitEnabled(ctx, unit_model.TypePullRequests) || !baseRepoPerm.CanRead(unit_model.TypePullRequests) {
45+
return
46+
}
47+
48+
var finalBranches []*git_model.RecentlyPushedNewBranch
49+
branches, err := git_model.FindRecentlyPushedNewBranches(ctx, ctx.Doer, opts)
50+
if err != nil {
51+
log.Error("FindRecentlyPushedNewBranches failed: %v", err)
52+
return
53+
}
54+
55+
for _, branch := range branches {
56+
divergingInfo, err := repo_service.GetBranchDivergingInfo(ctx,
57+
branch.BranchRepo, branch.BranchName, // "base" repo for diverging info
58+
opts.BaseRepo, opts.BaseRepo.DefaultBranch, // "head" repo for diverging info
59+
)
60+
if err != nil {
61+
log.Error("GetBranchDivergingInfo failed: %v", err)
62+
continue
63+
}
64+
branchRepoHasNewCommits := divergingInfo.BaseHasNewCommits
65+
baseRepoCommitsBehind := divergingInfo.HeadCommitsBehind
66+
if branchRepoHasNewCommits || baseRepoCommitsBehind > 0 {
67+
finalBranches = append(finalBranches, branch)
68+
}
69+
}
70+
if len(finalBranches) > 0 {
71+
ctx.Data["RecentBranchesPromptData"] = RecentBranchesPromptDataStruct{finalBranches}
72+
}
73+
}

routers/web/repo/issue_list.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"code.gitea.io/gitea/modules/optional"
2626
"code.gitea.io/gitea/modules/setting"
2727
"code.gitea.io/gitea/modules/util"
28-
"code.gitea.io/gitea/routers/utils"
2928
"code.gitea.io/gitea/routers/web/shared/issue"
3029
shared_user "code.gitea.io/gitea/routers/web/shared/user"
3130
"code.gitea.io/gitea/services/context"
@@ -768,7 +767,7 @@ func Issues(ctx *context.Context) {
768767
}
769768
ctx.Data["Title"] = ctx.Tr("repo.pulls")
770769
ctx.Data["PageIsPullList"] = true
771-
utils.PrepareRecentlyPushedNewBranches(ctx)
770+
prepareRecentlyPushedNewBranches(ctx)
772771
} else {
773772
MustEnableIssues(ctx)
774773
if ctx.Written() {

routers/web/repo/view_home.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"code.gitea.io/gitea/modules/setting"
2828
"code.gitea.io/gitea/modules/svg"
2929
"code.gitea.io/gitea/modules/util"
30-
"code.gitea.io/gitea/routers/utils"
3130
"code.gitea.io/gitea/routers/web/feed"
3231
"code.gitea.io/gitea/services/context"
3332
repo_service "code.gitea.io/gitea/services/repository"
@@ -421,7 +420,7 @@ func Home(ctx *context.Context) {
421420
prepareHomeSidebarRepoTopics,
422421
checkOutdatedBranch,
423422
prepareToRenderDirOrFile(entry),
424-
utils.PrepareRecentlyPushedNewBranches,
423+
prepareRecentlyPushedNewBranches,
425424
}
426425

427426
if isTreePathRoot {
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
{{range .RecentlyPushedNewBranches}}
2-
<div class="ui positive message tw-flex tw-items-center tw-gap-2">
3-
<div class="tw-flex-1 tw-break-anywhere">
4-
{{$timeSince := DateUtils.TimeSince .CommitTime}}
5-
{{$branchLink := HTMLFormat `<a href="%s">%s</a>` .BranchLink .BranchDisplayName}}
1+
{{/* Template Attributes:
2+
* RecentBranchesPromptData
3+
*/}}
4+
{{$data := .RecentBranchesPromptData}}
5+
{{if $data}}
6+
{{range $recentBranch := $data.RecentlyPushedNewBranches}}
7+
<div class="ui positive message flex-text-block">
8+
<div class="tw-flex-1">
9+
{{$timeSince := DateUtils.TimeSince $recentBranch.CommitTime}}
10+
{{$branchLink := HTMLFormat `<a href="%s">%s</a>` $recentBranch.BranchLink .BranchDisplayName}}
611
{{ctx.Locale.Tr "repo.pulls.recently_pushed_new_branches" $branchLink $timeSince}}
712
</div>
8-
<a role="button" class="ui compact green button tw-m-0" href="{{QueryBuild .BranchCompareURL "expand" 1}}">
13+
<a role="button" class="ui compact green button" href="{{QueryBuild $recentBranch.BranchCompareURL "expand" 1}}">
914
{{ctx.Locale.Tr "repo.pulls.compare_changes"}}
1015
</a>
1116
</div>
17+
{{end}}
1218
{{end}}

templates/repo/home.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</div>
1616
{{end}}
1717

18-
{{template "repo/code/recently_pushed_new_branches" .}}
18+
{{template "repo/code/recently_pushed_new_branches" dict "RecentBranchesPromptData" .RecentBranchesPromptData}}
1919

2020
<div class="{{Iif $showSidebar "repo-grid-filelist-sidebar" "repo-grid-filelist-only"}}">
2121
<div class="repo-home-filelist">

templates/repo/issue/list.tmpl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
<div class="ui container">
55
{{template "base/alert" .}}
66

7-
{{if .PageIsPullList}}
8-
{{template "repo/code/recently_pushed_new_branches" .}}
9-
{{end}}
7+
{{template "repo/code/recently_pushed_new_branches" dict "RecentBranchesPromptData" .RecentBranchesPromptData}}
108

119
{{if .PinnedIssues}}
1210
<div id="issue-pins" {{if .IsRepoAdmin}}data-is-repo-admin{{end}}>

templates/repo/view.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</div>
1515
{{end}}
1616

17-
{{template "repo/code/recently_pushed_new_branches" .}}
17+
{{template "repo/code/recently_pushed_new_branches" dict "RecentBranchesPromptData" .RecentBranchesPromptData}}
1818

1919
<div class="repo-view-container">
2020
<div class="tw-flex tw-flex-col repo-view-file-tree-container not-mobile {{if not .UserSettingCodeViewShowFileTree}}tw-hidden{{end}}" {{if .IsSigned}}data-user-is-signed-in{{end}}>

0 commit comments

Comments
 (0)