Refactor RepoBranchTagSelector (#32681)

This commit is contained in:
wxiaoguang 2024-12-02 09:41:32 +08:00 committed by GitHub
parent def13ece7c
commit e3e32605a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 275 additions and 288 deletions

View File

@ -43,7 +43,7 @@ func TestTest(t *testing.T) {
elapsed, err := Test()
assert.NoError(t, err)
// mem cache should take from 300ns up to 1ms on modern hardware ...
assert.Less(t, elapsed, SlowCacheThreshold)
assert.Less(t, elapsed, time.Millisecond)
}
func TestGetCache(t *testing.T) {

View File

@ -1,87 +1,57 @@
{{/* Attributes:
* root
* ContainerClasses
* (TODO: search "branch_dropdown" in the template directory)
* Repository
* CurrentRefType: eg. "branch", "tag"
* CurrentRefShortName: eg. "master", "v1.0"
* CurrentTreePath
* RefLinkTemplate: redirect to the link when a branch/tag is selected
* RefFormActionTemplate: change the parent form's action when a branch/tag is selected
* DropdownFixedText: the text to show in the dropdown (mainly used by "release page"), if empty, the text will be the branch/tag name
* ShowTabBranches
* ShowTabTagsTab
* AllowCreateNewRef
Search "repo/branch_dropdown" in the template directory to find all occurrences.
*/}}
{{$defaultSelectedRefName := $.root.BranchName}}
{{if and .root.IsViewTag (not .noTag)}}
{{$defaultSelectedRefName = .root.TagName}}
{{end}}
{{if eq $defaultSelectedRefName ""}}
{{$defaultSelectedRefName = $.root.Repository.DefaultBranch}}
{{end}}
<div class="js-branch-tag-selector {{if .ContainerClasses}}{{.ContainerClasses}}{{end}}"
data-text-release-compare="{{ctx.Locale.Tr "repo.release.compare"}}"
data-text-branches="{{ctx.Locale.Tr "repo.branches"}}"
data-text-tags="{{ctx.Locale.Tr "repo.tags"}}"
data-text-filter-branch="{{ctx.Locale.Tr "repo.pulls.filter_branch"}}"
data-text-filter-tag="{{ctx.Locale.Tr "repo.find_tag"}}"
data-text-default-branch-label="{{ctx.Locale.Tr "repo.default_branch_label"}}"
data-text-create-tag="{{ctx.Locale.Tr "repo.tag.create_tag"}}"
data-text-create-branch="{{ctx.Locale.Tr "repo.branch.create_branch"}}"
data-text-create-ref-from="{{ctx.Locale.Tr "repo.branch.create_from"}}"
data-text-no-results="{{ctx.Locale.Tr "no_results_found"}}"
{{$type := ""}}
{{if and .root.IsViewTag (not .noTag)}}
{{$type = "tag"}}
{{else if .root.IsViewBranch}}
{{$type = "branch"}}
{{else}}
{{$type = "tree"}}
{{end}}
data-current-repo-default-branch="{{.Repository.DefaultBranch}}"
data-current-repo-link="{{.Repository.Link}}"
data-current-tree-path="{{.CurrentTreePath}}"
data-current-ref-type="{{.CurrentRefType}}"
data-current-ref-short-name="{{.CurrentRefShortName}}"
{{$showBranchesInDropdown := not .root.HideBranchesInDropdown}}
data-ref-link-template="{{.RefLinkTemplate}}"
data-ref-form-action-template="{{.RefFormActionTemplate}}"
data-dropdown-fixed-text="{{.DropdownFixedText}}"
data-show-tab-branches="{{.ShowTabBranches}}"
data-show-tab-tags="{{.ShowTabTags}}"
data-allow-create-new-ref="{{.AllowCreateNewRef}}"
<script type="module">
const data = {
'textReleaseCompare': {{ctx.Locale.Tr "repo.release.compare"}},
'textCreateTag': {{ctx.Locale.Tr "repo.tag.create_tag"}},
'textCreateBranch': {{ctx.Locale.Tr "repo.branch.create_branch"}},
'textCreateBranchFrom': {{ctx.Locale.Tr "repo.branch.create_from"}},
'textBranches': {{ctx.Locale.Tr "repo.branches"}},
'textTags': {{ctx.Locale.Tr "repo.tags"}},
'textDefaultBranchLabel': {{ctx.Locale.Tr "repo.default_branch_label"}},
'mode': '{{if or .root.IsViewTag .isTag}}tags{{else}}branches{{end}}',
'showBranchesInDropdown': {{$showBranchesInDropdown}},
'searchFieldPlaceholder': '{{if $.noTag}}{{ctx.Locale.Tr "repo.pulls.filter_branch"}}{{else if $showBranchesInDropdown}}{{ctx.Locale.Tr "repo.filter_branch_and_tag"}}{{else}}{{ctx.Locale.Tr "repo.find_tag"}}{{end}}...',
'branchForm': {{$.branchForm}},
'disableCreateBranch': {{if .disableCreateBranch}}{{.disableCreateBranch}}{{else}}{{not .root.CanCreateBranch}}{{end}},
'setAction': {{.setAction}},
'submitForm': {{.submitForm}},
'viewType': {{$type}},
'refName': {{if and .root.IsViewTag (not .noTag)}}{{.root.TagName}}{{else if .root.IsViewBranch}}{{.root.BranchName}}{{else}}{{ShortSha .root.CommitID}}{{end}},
'commitIdShort': {{ShortSha .root.CommitID}},
'tagName': {{.root.TagName}},
'branchName': {{.root.BranchName}},
'noTag': {{.noTag}},
'defaultSelectedRefName': {{$defaultSelectedRefName}},
'repoDefaultBranch': {{.root.Repository.DefaultBranch}},
'enableFeed': {{.root.EnableFeed}},
'rssURLPrefix': '{{$.root.RepoLink}}/rss/branch/',
'branchURLPrefix': '{{if .branchURLPrefix}}{{.branchURLPrefix}}{{else}}{{$.root.RepoLink}}/{{if $.root.PageIsCommits}}commits{{else}}src{{end}}/branch/{{end}}',
'branchURLSuffix': '{{if .branchURLSuffix}}{{.branchURLSuffix}}{{else}}{{if $.root.TreePath}}/{{PathEscapeSegments $.root.TreePath}}{{end}}{{end}}',
'tagURLPrefix': '{{if .tagURLPrefix}}{{.tagURLPrefix}}{{else if .release}}{{$.root.RepoLink}}/compare/{{else}}{{$.root.RepoLink}}/{{if $.root.PageIsCommits}}commits{{else}}src{{end}}/tag/{{end}}',
'tagURLSuffix': '{{if .tagURLSuffix}}{{.tagURLSuffix}}{{else if .release}}...{{if .release.IsDraft}}{{PathEscapeSegments .release.Target}}{{else}}{{if .release.TagName}}{{PathEscapeSegments .release.TagName}}{{else}}{{PathEscapeSegments .release.Sha1}}{{end}}{{end}}{{else}}{{if $.root.TreePath}}/{{PathEscapeSegments $.root.TreePath}}{{end}}{{end}}',
'repoLink': {{.root.RepoLink}},
'treePath': {{.root.TreePath}},
'branchNameSubURL': {{.root.BranchNameSubURL}},
'noResults': {{ctx.Locale.Tr "no_results_found"}},
};
{{if .release}}
data.release = {
'tagName': {{.release.TagName}},
};
{{end}}
window.config.pageData.branchDropdownDataList = window.config.pageData.branchDropdownDataList || [];
window.config.pageData.branchDropdownDataList.push(data);
</script>
<div class="js-branch-tag-selector {{if .ContainerClasses}}{{.ContainerClasses}}{{end}}">
data-enable-feed="{{ctx.RootData.EnableFeed}}"
>
{{/* show dummy elements before Vue componment is mounted, this code must match the code in BranchTagSelector.vue */}}
<div class="ui dropdown custom branch-selector-dropdown ellipsis-items-nowrap">
<div class="ui button branch-dropdown-button">
<span class="flex-text-block gt-ellipsis">
{{if .release}}
{{ctx.Locale.Tr "repo.release.compare"}}
{{else}}
{{if eq $type "tag"}}
{{if not .DropdownFixedText}}
{{if .ShowTabTags}}
{{svg "octicon-tag"}}
{{else}}
{{else if .ShowTabBranches}}
{{svg "octicon-git-branch"}}
{{end}}
<strong ref="dropdownRefName" class="tw-ml-2 tw-inline-block gt-ellipsis">{{if and .root.IsViewTag (not .noTag)}}{{.root.TagName}}{{else if .root.IsViewBranch}}{{.root.BranchName}}{{else}}{{ShortSha .root.CommitID}}{{end}}</strong>
{{end}}
<strong class="tw-ml-2 tw-inline-block gt-ellipsis">{{Iif .DropdownFixedText .SelectedRefShortName}}</strong>
</span>
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
</div>

View File

@ -66,14 +66,15 @@
</div>
<div class="content">
<p id="cherry-pick-content" class="branch-dropdown"></p>
{{template "repo/branch_dropdown" dict "root" .
"noTag" true "disableCreateBranch" true
"branchForm" "branch-dropdown-form"
"branchURLPrefix" (printf "%s/_cherrypick/%s/" $.RepoLink .CommitID) "branchURLSuffix" ""
"setAction" true "submitForm" true}}
<form method="get" action="{{$.RepoLink}}/_cherrypick/{{.CommitID}}/{{if $.BranchName}}{{PathEscapeSegments $.BranchName}}{{else}}{{PathEscapeSegments $.Repository.DefaultBranch}}{{end}}" id="branch-dropdown-form">
<input type="hidden" name="ref" value="{{if $.BranchName}}{{$.BranchName}}{{else}}{{$.Repository.DefaultBranch}}{{end}}">
<input type="hidden" name="refType" value="branch">
<form method="get">
{{template "repo/branch_dropdown" dict
"Repository" .Repository
"ShowTabBranches" true
"CurrentRefType" "branch"
"CurrentRefShortName" (Iif $.BranchName $.Repository.DefaultBranch)
"RefFormActionTemplate" (print "{RepoLink}/_cherrypick/" .CommitID "/{RefShortName}")
}}
<input type="hidden" id="cherry-pick-type" name="cherry-pick-type"><br>
<button type="submit" id="cherry-pick-submit" class="ui primary button"></button>
</form>

View File

@ -5,7 +5,24 @@
{{template "repo/sub_menu" .}}
<div class="repo-button-row">
<div class="repo-button-row-left">
{{template "repo/branch_dropdown" dict "root" .}}
{{$branchDropdownCurrentRefType := "branch"}}
{{$branchDropdownCurrentRefShortName := .BranchName}}
{{if .IsViewTag}}
{{$branchDropdownCurrentRefType := "tag"}}
{{$branchDropdownCurrentRefShortName := .TagName}}
{{end}}
{{template "repo/branch_dropdown" dict
"Repository" .Repository
"ShowTabBranches" true
"ShowTabTags" true
"CurrentRefType" $branchDropdownCurrentRefType
"CurrentRefShortName" $branchDropdownCurrentRefShortName
"CurrentTreePath" .TreePath
"RefLinkTemplate" "{RepoLink}/commits/{RefType}/{RefShortName}/{TreePath}"
"AllowCreateNewRef" .CanCreateBranch
}}
<a href="{{.RepoLink}}/graph" class="ui basic small compact button">
{{svg "octicon-git-branch"}}
{{ctx.Locale.Tr "repo.commit_graph"}}

View File

@ -47,7 +47,22 @@
{{$isHomepage := (eq $n 0)}}
<div class="repo-button-row" data-is-homepage="{{$isHomepage}}">
<div class="repo-button-row-left">
{{template "repo/branch_dropdown" dict "root" .}}
{{$branchDropdownCurrentRefType := "branch"}}
{{$branchDropdownCurrentRefShortName := .BranchName}}
{{if .IsViewTag}}
{{$branchDropdownCurrentRefType := "tag"}}
{{$branchDropdownCurrentRefShortName := .TagName}}
{{end}}
{{template "repo/branch_dropdown" dict
"Repository" .Repository
"ShowTabBranches" true
"ShowTabTags" true
"CurrentRefType" $branchDropdownCurrentRefType
"CurrentRefShortName" $branchDropdownCurrentRefShortName
"CurrentTreePath" .TreePath
"RefLinkTemplate" "{RepoLink}/src/{RefType}/{RefShortName}/{TreePath}"
"AllowCreateNewRef" .CanCreateBranch
}}
{{if and .CanCompareOrPull .IsViewBranch (not .Repository.IsArchived)}}
{{$cmpBranch := ""}}
{{if ne .Repository.ID .BaseRepo.ID}}

View File

@ -12,7 +12,20 @@
<a class="muted" href="{{if not (and $release.Sha1 ($.Permission.CanRead ctx.Consts.RepoUnitTypeCode))}}#{{else}}{{$.RepoLink}}/src/tag/{{$release.TagName | PathEscapeSegments}}{{end}}" rel="nofollow">{{svg "octicon-tag" 16 "tw-mr-1"}}{{$release.TagName}}</a>
{{if and $release.Sha1 ($.Permission.CanRead ctx.Consts.RepoUnitTypeCode)}}
<a class="muted tw-font-mono" href="{{$.RepoLink}}/src/commit/{{$release.Sha1}}" rel="nofollow">{{svg "octicon-git-commit" 16 "tw-mr-1"}}{{ShortSha $release.Sha1}}</a>
{{template "repo/branch_dropdown" dict "root" $ "release" $release}}
{{$compareTarget := ""}}
{{if $release.IsDraft}}
{{$compareTarget = $release.Target}}
{{else if $release.TagName}}
{{$compareTarget = $release.TagName}}
{{else}}
{{$compareTarget = $release.Sha1}}
{{end}}
{{template "repo/branch_dropdown" dict
"Repository" $.Repository
"ShowTabTags" true
"DropdownFixedText" (ctx.Locale.Tr "repo.release.compare")
"RefLinkTemplate" (print "{RepoLink}/compare/{RefShortName}..." (PathEscapeSegments $compareTarget))
}}
{{end}}
</div>
<div class="ui segment detail">

File diff suppressed because it is too large Load Diff

View File

@ -59,3 +59,5 @@ export type FomanticInitFunction = {
settings?: Record<string, any>,
(...args: any[]): any,
}
export type GitRefType = 'branch' | 'tag';