Skip to content

Commit fcd0962

Browse files
authored
Simplify context ref name (#33267)
1 parent cbf933e commit fcd0962

File tree

12 files changed

+27
-80
lines changed

12 files changed

+27
-80
lines changed

Diff for: routers/web/repo/branch.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,11 @@ func CreateBranch(ctx *context.Context) {
193193

194194
if form.CreateTag {
195195
target := ctx.Repo.CommitID
196-
if ctx.Repo.IsViewBranch {
196+
if ctx.Repo.RefFullName.IsBranch() {
197197
target = ctx.Repo.BranchName
198198
}
199199
err = release_service.CreateNewTag(ctx, ctx.Doer, ctx.Repo.Repository, target, form.NewBranchName, "")
200-
} else if ctx.Repo.IsViewBranch {
200+
} else if ctx.Repo.RefFullName.IsBranch() {
201201
err = repo_service.CreateNewBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, ctx.Repo.BranchName, form.NewBranchName)
202202
} else {
203203
err = repo_service.CreateNewBranchFromCommit(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, ctx.Repo.CommitID, form.NewBranchName)

Diff for: routers/web/repo/commit.go

-6
Original file line numberDiff line numberDiff line change
@@ -390,12 +390,6 @@ func Diff(ctx *context.Context) {
390390
}
391391
}
392392

393-
ctx.Data["BranchName"], err = commit.GetBranchName()
394-
if err != nil {
395-
ctx.ServerError("commit.GetBranchName", err)
396-
return
397-
}
398-
399393
ctx.HTML(http.StatusOK, tplCommitPage)
400394
}
401395

Diff for: routers/web/repo/release.go

-4
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,6 @@ func getReleaseInfos(ctx *context.Context, opts *repo_model.FindReleasesOptions)
148148
func Releases(ctx *context.Context) {
149149
ctx.Data["PageIsReleaseList"] = true
150150
ctx.Data["Title"] = ctx.Tr("repo.release.releases")
151-
ctx.Data["IsViewBranch"] = false
152-
ctx.Data["IsViewTag"] = true
153151

154152
listOptions := db.ListOptions{
155153
Page: ctx.FormInt("page"),
@@ -194,8 +192,6 @@ func Releases(ctx *context.Context) {
194192
func TagsList(ctx *context.Context) {
195193
ctx.Data["PageIsTagList"] = true
196194
ctx.Data["Title"] = ctx.Tr("repo.release.tags")
197-
ctx.Data["IsViewBranch"] = false
198-
ctx.Data["IsViewTag"] = true
199195
ctx.Data["CanCreateRelease"] = ctx.Repo.CanWrite(unit.TypeReleases) && !ctx.Repo.Repository.IsArchived
200196

201197
namePattern := ctx.FormTrim("q")

Diff for: routers/web/repo/view_file.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
232232
ctx.Data["CanEditFile"] = true
233233
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.edit_this_file")
234234
}
235-
} else if !ctx.Repo.IsViewBranch {
235+
} else if !ctx.Repo.RefFullName.IsBranch() {
236236
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.must_be_on_a_branch")
237237
} else if !ctx.Repo.CanWriteToBranch(ctx, ctx.Doer, ctx.Repo.BranchName) {
238238
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.fork_before_edit")
@@ -305,7 +305,7 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
305305
ctx.Data["CanDeleteFile"] = true
306306
ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.delete_this_file")
307307
}
308-
} else if !ctx.Repo.IsViewBranch {
308+
} else if !ctx.Repo.RefFullName.IsBranch() {
309309
ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.must_be_on_a_branch")
310310
} else if !ctx.Repo.CanWriteToBranch(ctx, ctx.Doer, ctx.Repo.BranchName) {
311311
ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.must_have_write_access")

Diff for: routers/web/repo/view_home.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func prepareHomeSidebarLatestRelease(ctx *context.Context) {
178178
}
179179

180180
func prepareUpstreamDivergingInfo(ctx *context.Context) {
181-
if !ctx.Repo.Repository.IsFork || !ctx.Repo.IsViewBranch || ctx.Repo.TreePath != "" {
181+
if !ctx.Repo.Repository.IsFork || !ctx.Repo.RefFullName.IsBranch() || ctx.Repo.TreePath != "" {
182182
return
183183
}
184184
upstreamDivergingInfo, err := repo_service.GetUpstreamDivergingInfo(ctx, ctx.Repo.Repository, ctx.Repo.BranchName)

Diff for: services/context/repo.go

+10-29
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,15 @@ type Repository struct {
5353
RepoLink string
5454
GitRepo *git.Repository
5555

56-
// these fields indicate the current ref type, for example: ".../src/branch/master" means IsViewBranch=true
57-
IsViewBranch bool
58-
IsViewTag bool
59-
IsViewCommit bool
60-
56+
// RefFullName is the full ref name that the user is viewing
6157
RefFullName git.RefName
62-
BranchName string
63-
TagName string
58+
BranchName string // it is the RefFullName's short name if its type is "branch"
59+
TagName string // it is the RefFullName's short name if its type is "tag"
6460
TreePath string
6561

66-
// Commit it is always set to the commit for the branch or tag
67-
Commit *git.Commit
68-
CommitID string
69-
62+
// Commit it is always set to the commit for the branch or tag, or just the commit that the user is viewing
63+
Commit *git.Commit
64+
CommitID string
7065
CommitsCount int64
7166

7267
PullRequest *PullRequest
@@ -79,7 +74,7 @@ func (r *Repository) CanWriteToBranch(ctx context.Context, user *user_model.User
7974

8075
// CanEnableEditor returns true if repository is editable and user has proper access level.
8176
func (r *Repository) CanEnableEditor(ctx context.Context, user *user_model.User) bool {
82-
return r.IsViewBranch && r.CanWriteToBranch(ctx, user, r.BranchName) && r.Repository.CanEnableEditor() && !r.Repository.IsArchived
77+
return r.RefFullName.IsBranch() && r.CanWriteToBranch(ctx, user, r.BranchName) && r.Repository.CanEnableEditor() && !r.Repository.IsArchived
8378
}
8479

8580
// CanCreateBranch returns true if repository is editable and user has proper access level.
@@ -174,15 +169,9 @@ func (r *Repository) GetCommitsCount() (int64, error) {
174169
if r.Commit == nil {
175170
return 0, nil
176171
}
177-
var contextName string
178-
if r.IsViewBranch {
179-
contextName = r.BranchName
180-
} else if r.IsViewTag {
181-
contextName = r.TagName
182-
} else {
183-
contextName = r.CommitID
184-
}
185-
return cache.GetInt64(r.Repository.GetCommitsCountCacheKey(contextName, r.IsViewBranch || r.IsViewTag), func() (int64, error) {
172+
contextName := r.RefFullName.ShortName()
173+
isRef := r.RefFullName.IsBranch() || r.RefFullName.IsTag()
174+
return cache.GetInt64(r.Repository.GetCommitsCountCacheKey(contextName, isRef), func() (int64, error) {
186175
return r.Commit.CommitsCount()
187176
})
188177
}
@@ -798,7 +787,6 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) {
798787
// Empty repository does not have reference information.
799788
if ctx.Repo.Repository.IsEmpty {
800789
// assume the user is viewing the (non-existent) default branch
801-
ctx.Repo.IsViewBranch = true
802790
ctx.Repo.BranchName = ctx.Repo.Repository.DefaultBranch
803791
ctx.Repo.RefFullName = git.RefNameFromBranch(ctx.Repo.BranchName)
804792
// these variables are used by the template to "add/upload" new files
@@ -834,7 +822,6 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) {
834822
ctx.ServerError("GetBranchCommit", err)
835823
return
836824
}
837-
ctx.Repo.IsViewBranch = true
838825
} else { // there is a path in request
839826
guessLegacyPath := refType == ""
840827
if guessLegacyPath {
@@ -853,7 +840,6 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) {
853840
}
854841

855842
if refType == git.RefTypeBranch && ctx.Repo.GitRepo.IsBranchExist(refShortName) {
856-
ctx.Repo.IsViewBranch = true
857843
ctx.Repo.BranchName = refShortName
858844
ctx.Repo.RefFullName = git.RefNameFromBranch(refShortName)
859845

@@ -864,7 +850,6 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) {
864850
}
865851
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
866852
} else if refType == git.RefTypeTag && ctx.Repo.GitRepo.IsTagExist(refShortName) {
867-
ctx.Repo.IsViewTag = true
868853
ctx.Repo.RefFullName = git.RefNameFromTag(refShortName)
869854
ctx.Repo.TagName = refShortName
870855

@@ -879,7 +864,6 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) {
879864
}
880865
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
881866
} else if git.IsStringLikelyCommitID(ctx.Repo.GetObjectFormat(), refShortName, 7) {
882-
ctx.Repo.IsViewCommit = true
883867
ctx.Repo.RefFullName = git.RefNameFromCommit(refShortName)
884868
ctx.Repo.CommitID = refShortName
885869

@@ -915,13 +899,10 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) {
915899
ctx.Data["RefTypeNameSubURL"] = ctx.Repo.RefTypeNameSubURL()
916900
ctx.Data["TreePath"] = ctx.Repo.TreePath
917901

918-
ctx.Data["IsViewBranch"] = ctx.Repo.IsViewBranch
919902
ctx.Data["BranchName"] = ctx.Repo.BranchName
920903

921-
ctx.Data["IsViewTag"] = ctx.Repo.IsViewTag
922904
ctx.Data["TagName"] = ctx.Repo.TagName
923905

924-
ctx.Data["IsViewCommit"] = ctx.Repo.IsViewCommit
925906
ctx.Data["CommitID"] = ctx.Repo.CommitID
926907

927908
ctx.Data["CanCreateBranch"] = ctx.Repo.CanCreateBranch() // only used by the branch selector dropdown: AllowCreateNewRef

Diff for: templates/repo/blame.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<div class="file-header-right file-actions tw-flex tw-items-center tw-flex-wrap">
1919
<div class="ui buttons">
2020
<a class="ui tiny button" href="{{$.RawFileLink}}">{{ctx.Locale.Tr "repo.file_raw"}}</a>
21-
{{if not .IsViewCommit}}
21+
{{if or .RefFullName.IsBranch .RefFullName.IsTag}}
2222
<a class="ui tiny button" href="{{.RepoLink}}/src/commit/{{.CommitID | PathEscape}}/{{.TreePath | PathEscapeSegments}}">{{ctx.Locale.Tr "repo.file_permalink"}}</a>
2323
{{end}}
2424
<a class="ui tiny button" href="{{.RepoLink}}/src/{{.RefTypeNameSubURL}}/{{.TreePath | PathEscapeSegments}}">{{ctx.Locale.Tr "repo.normal_view"}}</a>

Diff for: templates/repo/commit_page.tmpl

+1-3
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,11 @@
5454
<p id="cherry-pick-content" class="branch-dropdown"></p>
5555

5656
<form method="get">
57-
{{/*FIXME: CurrentRefShortName seems not making sense here (old code),
58-
because the "commit page" has no "$.BranchName" info, so only using DefaultBranch should be enough */}}
5957
{{template "repo/branch_dropdown" dict
6058
"Repository" .Repository
6159
"ShowTabBranches" true
6260
"CurrentRefType" "branch"
63-
"CurrentRefShortName" (or $.BranchName $.Repository.DefaultBranch)
61+
"CurrentRefShortName" $.Repository.DefaultBranch
6462
"RefFormActionTemplate" (print "{RepoLink}/_cherrypick/" .CommitID "/{RefShortName}")
6563
}}
6664
<input type="hidden" id="cherry-pick-type" name="cherry-pick-type"><br>

Diff for: templates/repo/commits.tmpl

+3-14
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,13 @@
55
{{template "repo/sub_menu" .}}
66
<div class="repo-button-row">
77
<div class="repo-button-row-left">
8-
{{- /* for /owner/repo/commits/branch/the-name */ -}}
9-
{{- $branchDropdownCurrentRefType := "branch" -}}
10-
{{- $branchDropdownCurrentRefShortName := .BranchName -}}
11-
{{- if .IsViewTag -}}
12-
{{- /* for /owner/repo/commits/tag/the-name */ -}}
13-
{{- $branchDropdownCurrentRefType = "tag" -}}
14-
{{- $branchDropdownCurrentRefShortName = .TagName -}}
15-
{{- else if .IsViewCommit -}}
16-
{{- /* for /owner/repo/commits/commit/000000 */ -}}
17-
{{- $branchDropdownCurrentRefType = "commit" -}}
18-
{{- $branchDropdownCurrentRefShortName = ShortSha .CommitID -}}
19-
{{- end -}}
8+
{{- /* for /owner/repo/commits/{RefType}/{RefShortName} */ -}}
209
{{- template "repo/branch_dropdown" dict
2110
"Repository" .Repository
2211
"ShowTabBranches" true
2312
"ShowTabTags" true
24-
"CurrentRefType" $branchDropdownCurrentRefType
25-
"CurrentRefShortName" $branchDropdownCurrentRefShortName
13+
"CurrentRefType" .RefFullName.RefType
14+
"CurrentRefShortName" .RefFullName.ShortName
2615
"CurrentTreePath" .TreePath
2716
"RefLinkTemplate" "{RepoLink}/commits/{RefType}/{RefShortName}/{TreePath}"
2817
"AllowCreateNewRef" .CanCreateBranch

Diff for: templates/repo/home.tmpl

+5-16
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,19 @@
2424
{{template "repo/sub_menu" .}}
2525
<div class="repo-button-row">
2626
<div class="repo-button-row-left">
27-
{{- /* for repo home (default branch) and /owner/repo/src/branch/the-name */ -}}
28-
{{- $branchDropdownCurrentRefType := "branch" -}}
29-
{{- $branchDropdownCurrentRefShortName := .BranchName -}}
30-
{{- if .IsViewTag -}}
31-
{{- /* for /owner/repo/src/tag/the-name */ -}}
32-
{{- $branchDropdownCurrentRefType = "tag" -}}
33-
{{- $branchDropdownCurrentRefShortName = .TagName -}}
34-
{{- else if .IsViewCommit -}}
35-
{{- /* for /owner/repo/src/commit/000000 */ -}}
36-
{{- $branchDropdownCurrentRefType = "commit" -}}
37-
{{- $branchDropdownCurrentRefShortName = ShortSha .CommitID -}}
38-
{{- end -}}
27+
{{- /* for repo home (default branch) and /owner/repo/src/{RefType}/{RefShortName} */ -}}
3928
{{- template "repo/branch_dropdown" dict
4029
"Repository" .Repository
4130
"ShowTabBranches" true
4231
"ShowTabTags" true
43-
"CurrentRefType" $branchDropdownCurrentRefType
44-
"CurrentRefShortName" $branchDropdownCurrentRefShortName
32+
"CurrentRefType" .RefFullName.RefType
33+
"CurrentRefShortName" .RefFullName.ShortName
4534
"CurrentTreePath" .TreePath
4635
"RefLinkTemplate" "{RepoLink}/src/{RefType}/{RefShortName}/{TreePath}"
4736
"AllowCreateNewRef" .CanCreateBranch
4837
"ShowViewAllRefsEntry" true
4938
-}}
50-
{{if and .CanCompareOrPull .IsViewBranch (not .Repository.IsArchived)}}
39+
{{if and .CanCompareOrPull .RefFullName.IsBranch (not .Repository.IsArchived)}}
5140
{{$cmpBranch := ""}}
5241
{{if ne .Repository.ID .BaseRepo.ID}}
5342
{{$cmpBranch = printf "%s/%s:" (.Repository.OwnerName|PathEscape) (.Repository.Name|PathEscape)}}
@@ -65,7 +54,7 @@
6554
<a href="{{.Repository.Link}}/find/{{.RefTypeNameSubURL}}" class="ui compact basic button">{{ctx.Locale.Tr "repo.find_file.go_to_file"}}</a>
6655
{{end}}
6756

68-
{{if and .CanWriteCode .IsViewBranch (not .Repository.IsMirror) (not .Repository.IsArchived) (not .IsViewFile)}}
57+
{{if and .CanWriteCode .RefFullName.IsBranch (not .Repository.IsMirror) (not .Repository.IsArchived) (not .IsViewFile)}}
6958
<button class="ui dropdown basic compact jump button"{{if not .Repository.CanEnableEditor}} disabled{{end}}>
7059
{{ctx.Locale.Tr "repo.editor.add_file"}}
7160
{{svg "octicon-triangle-down" 14 "dropdown icon"}}

Diff for: templates/repo/release/list.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"Repository" $.Repository
2525
"ShowTabTags" true
2626
"DropdownFixedText" (ctx.Locale.Tr "repo.release.compare")
27-
"RefLinkTemplate" (print "{RepoLink}/compare/{RefShortName}..." (PathEscapeSegments $compareTarget))
27+
"RefLinkTemplate" (print "{RepoLink}/compare/{RefShortName}" "..." (PathEscapeSegments $compareTarget))
2828
}}
2929
{{end}}
3030
</div>

Diff for: templates/repo/view_file.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
{{if not .ReadmeInList}}
4343
<div class="ui buttons tw-mr-1">
4444
<a class="ui mini basic button" href="{{$.RawFileLink}}">{{ctx.Locale.Tr "repo.file_raw"}}</a>
45-
{{if not .IsViewCommit}}
45+
{{if or .RefFullName.IsBranch .RefFullName.IsTag}}
4646
<a class="ui mini basic button" href="{{.RepoLink}}/src/commit/{{PathEscape .CommitID}}/{{PathEscapeSegments .TreePath}}">{{ctx.Locale.Tr "repo.file_permalink"}}</a>
4747
{{end}}
4848
{{if .IsRepresentableAsText}}

0 commit comments

Comments
 (0)