Skip to content

Commit 0646fc1

Browse files
committed
clarify names, add error log
1 parent c00d809 commit 0646fc1

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

routers/web/repo/view_home.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,17 @@ func prepareRecentlyPushedNewBranches(ctx *context.Context) {
222222
}
223223

224224
for _, branch := range branches {
225-
divergingInfo, err := repo_service.GetBranchDivergingInfo(ctx, branch.BranchRepo, opts.BaseRepo, branch.BranchName, opts.BaseRepo.DefaultBranch)
225+
divergingInfo, err := repo_service.GetBranchDivergingInfo(ctx,
226+
branch.BranchRepo, branch.BranchName, // "base" repo for diverging info
227+
opts.BaseRepo, opts.BaseRepo.DefaultBranch, // "head" repo for diverging info
228+
)
226229
if err != nil {
230+
log.Error("GetBranchDivergingInfo failed: %v", err)
227231
continue
228232
}
229-
// Base is the pushed branch (for fork branch or local pushed branch perspective)
230-
if divergingInfo.BaseHasNewCommits || divergingInfo.CommitsBehind > 0 {
233+
branchRepoHasNewCommits := divergingInfo.BaseHasNewCommits
234+
baseRepoCommitsBehind := divergingInfo.HeadCommitsBehind
235+
if branchRepoHasNewCommits || baseRepoCommitsBehind > 0 {
231236
finalBranches = append(finalBranches, branch)
232237
}
233238
}

services/repository/branch.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -667,14 +667,16 @@ func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitR
667667
return nil
668668
}
669669

670+
// BranchDivergingInfo contains the information about the divergence of a head branch to the base branch.
671+
// This struct is also used in templates, so it needs to search for all references before changing it.
670672
type BranchDivergingInfo struct {
671673
BaseHasNewCommits bool
672-
CommitsBehind int
673-
CommitsAhead int
674+
HeadCommitsBehind int
675+
HeadCommitsAhead int
674676
}
675677

676-
// getBranchDivergingInfo returns the information about the divergence of a patch branch to the base branch.
677-
func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo, headRepo *repo_model.Repository, baseBranch, headBranch string) (*BranchDivergingInfo, error) {
678+
// GetBranchDivergingInfo returns the information about the divergence of a patch branch to the base branch.
679+
func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo *repo_model.Repository, baseBranch string, headRepo *repo_model.Repository, headBranch string) (*BranchDivergingInfo, error) {
678680
headGitBranch, err := git_model.GetBranch(ctx, headRepo.ID, headBranch)
679681
if err != nil {
680682
return nil, err
@@ -717,6 +719,6 @@ func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo, headRepo *repo_
717719
return info, nil
718720
}
719721

720-
info.CommitsBehind, info.CommitsAhead = diff.Behind, diff.Ahead
722+
info.HeadCommitsBehind, info.HeadCommitsAhead = diff.Behind, diff.Ahead
721723
return info, nil
722724
}

services/repository/merge_upstream.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,18 @@ func MergeUpstream(ctx context.Context, doer *user_model.User, repo *repo_model.
7070
}
7171

7272
// GetUpstreamDivergingInfo returns the information about the divergence between the fork repository's branch and the base repository's default branch.
73-
func GetUpstreamDivergingInfo(ctx reqctx.RequestContext, repo *repo_model.Repository, branch string) (*BranchDivergingInfo, error) {
74-
if !repo.IsFork {
73+
func GetUpstreamDivergingInfo(ctx reqctx.RequestContext, forkRepo *repo_model.Repository, forkBranch string) (*BranchDivergingInfo, error) {
74+
if !forkRepo.IsFork {
7575
return nil, util.NewInvalidArgumentErrorf("repo is not a fork")
7676
}
7777

78-
if repo.IsArchived {
78+
if forkRepo.IsArchived {
7979
return nil, util.NewInvalidArgumentErrorf("repo is archived")
8080
}
8181

82-
if err := repo.GetBaseRepo(ctx); err != nil {
82+
if err := forkRepo.GetBaseRepo(ctx); err != nil {
8383
return nil, err
8484
}
8585

86-
return GetBranchDivergingInfo(ctx, repo.BaseRepo, repo, repo.BaseRepo.DefaultBranch, branch)
86+
return GetBranchDivergingInfo(ctx, forkRepo.BaseRepo, forkRepo.BaseRepo.DefaultBranch, forkRepo, forkBranch)
8787
}

templates/repo/code/upstream_diverging_info.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
{{if and .UpstreamDivergingInfo (or .UpstreamDivergingInfo.BaseHasNewCommits .UpstreamDivergingInfo.CommitsBehind)}}
1+
{{if and .UpstreamDivergingInfo (or .UpstreamDivergingInfo.BaseHasNewCommits .UpstreamDivergingInfo.HeadCommitsBehind)}}
22
<div class="ui message flex-text-block">
33
<div class="tw-flex-1">
44
{{$upstreamLink := printf "%s/src/branch/%s" .Repository.BaseRepo.Link (.Repository.BaseRepo.DefaultBranch|PathEscapeSegments)}}
55
{{$upstreamHtml := HTMLFormat `<a href="%s">%s:%s</a>` $upstreamLink .Repository.BaseRepo.FullName .Repository.BaseRepo.DefaultBranch}}
6-
{{if .UpstreamDivergingInfo.CommitsBehind}}
7-
{{ctx.Locale.TrN .UpstreamDivergingInfo.CommitsBehind "repo.pulls.upstream_diverging_prompt_behind_1" "repo.pulls.upstream_diverging_prompt_behind_n" .UpstreamDivergingInfo.CommitsBehind $upstreamHtml}}
6+
{{if .UpstreamDivergingInfo.HeadCommitsBehind}}
7+
{{ctx.Locale.TrN .UpstreamDivergingInfo.HeadCommitsBehind "repo.pulls.upstream_diverging_prompt_behind_1" "repo.pulls.upstream_diverging_prompt_behind_n" .UpstreamDivergingInfo.HeadCommitsBehind $upstreamHtml}}
88
{{else}}
99
{{ctx.Locale.Tr "repo.pulls.upstream_diverging_prompt_base_newer" $upstreamHtml}}
1010
{{end}}

0 commit comments

Comments
 (0)