Skip to content

Commit f2ef337

Browse files
committed
Refactor clone button
1 parent e48f3b0 commit f2ef337

File tree

12 files changed

+99
-81
lines changed

12 files changed

+99
-81
lines changed

models/repo/repo.go

-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,6 @@ func (repo *Repository) DescriptionHTML(ctx context.Context) template.HTML {
533533
type CloneLink struct {
534534
SSH string
535535
HTTPS string
536-
Git string
537536
}
538537

539538
// ComposeHTTPSCloneURL returns HTTPS clone URL based on given owner and repository name.

modules/context/repo.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -540,15 +540,22 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) {
540540
// If multiple forks are available or if the user can fork to another account, but there is already a fork: open selection dialog
541541
ctx.Data["ShowForkModal"] = len(userAndOrgForks) > 1 || (canSignedUserFork && len(userAndOrgForks) > 0)
542542

543-
ctx.Data["DisableSSH"] = setting.SSH.Disabled
544-
ctx.Data["ExposeAnonSSH"] = setting.SSH.ExposeAnonymous
545-
ctx.Data["DisableHTTP"] = setting.Repository.DisableHTTPGit
543+
ctx.Data["RepoCloneLink"] = repo.CloneLink()
544+
545+
cloneButtonShowHTTPS := !setting.Repository.DisableHTTPGit
546+
cloneButtonShowSSH := !setting.SSH.Disabled && (ctx.IsSigned || setting.SSH.ExposeAnonymous)
547+
if !cloneButtonShowHTTPS && !cloneButtonShowSSH {
548+
// at least we had to show one link, so we just show the HTTPS
549+
cloneButtonShowHTTPS = true
550+
}
551+
ctx.Data["CloneButtonShowHTTPS"] = cloneButtonShowHTTPS
552+
ctx.Data["CloneButtonShowSSH"] = cloneButtonShowSSH
553+
ctx.Data["CloneButtonOriginLink"] = ctx.Data["RepoCloneLink"] // it may be rewritten to the WikiCloneLink by the router middleware
554+
546555
ctx.Data["RepoSearchEnabled"] = setting.Indexer.RepoIndexerEnabled
547556
if setting.Indexer.RepoIndexerEnabled {
548557
ctx.Data["CodeIndexerUnavailable"] = !code_indexer.IsAvailable()
549558
}
550-
ctx.Data["CloneLink"] = repo.CloneLink()
551-
ctx.Data["WikiCloneLink"] = repo.WikiCloneLink()
552559

553560
if ctx.IsSigned {
554561
ctx.Data["IsWatchingRepo"] = repo_model.IsWatching(ctx.Doer.ID, repo.ID)

routers/web/repo/setting.go

+1
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,7 @@ func DeployKeysPost(ctx *context.Context) {
10701070
form := web.GetForm(ctx).(*forms.AddKeyForm)
10711071
ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
10721072
ctx.Data["PageIsSettingsKeys"] = true
1073+
ctx.Data["DisableSSH"] = setting.SSH.Disabled
10731074

10741075
keys, err := asymkey_model.ListDeployKeys(ctx, &asymkey_model.ListDeployKeysOptions{RepoID: ctx.Repo.Repository.ID})
10751076
if err != nil {

routers/web/repo/wiki.go

-7
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,6 @@ func WikiPost(ctx *context.Context) {
409409

410410
// Wiki renders single wiki page
411411
func Wiki(ctx *context.Context) {
412-
ctx.Data["PageIsWiki"] = true
413412
ctx.Data["CanWriteWiki"] = ctx.Repo.CanWrite(unit.TypeWiki) && !ctx.Repo.Repository.IsArchived
414413

415414
switch ctx.FormString("action") {
@@ -474,7 +473,6 @@ func Wiki(ctx *context.Context) {
474473

475474
// WikiRevision renders file revision list of wiki page
476475
func WikiRevision(ctx *context.Context) {
477-
ctx.Data["PageIsWiki"] = true
478476
ctx.Data["CanWriteWiki"] = ctx.Repo.CanWrite(unit.TypeWiki) && !ctx.Repo.Repository.IsArchived
479477

480478
if !ctx.Repo.Repository.HasWiki() {
@@ -519,7 +517,6 @@ func WikiPages(ctx *context.Context) {
519517
}
520518

521519
ctx.Data["Title"] = ctx.Tr("repo.wiki.pages")
522-
ctx.Data["PageIsWiki"] = true
523520
ctx.Data["CanWriteWiki"] = ctx.Repo.CanWrite(unit.TypeWiki) && !ctx.Repo.Repository.IsArchived
524521

525522
wikiRepo, commit, err := findWikiRepoCommit(ctx)
@@ -624,7 +621,6 @@ func WikiRaw(ctx *context.Context) {
624621
// NewWiki render wiki create page
625622
func NewWiki(ctx *context.Context) {
626623
ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
627-
ctx.Data["PageIsWiki"] = true
628624

629625
if !ctx.Repo.Repository.HasWiki() {
630626
ctx.Data["title"] = "Home"
@@ -640,7 +636,6 @@ func NewWiki(ctx *context.Context) {
640636
func NewWikiPost(ctx *context.Context) {
641637
form := web.GetForm(ctx).(*forms.NewWikiForm)
642638
ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
643-
ctx.Data["PageIsWiki"] = true
644639

645640
if ctx.HasError() {
646641
ctx.HTML(http.StatusOK, tplWikiNew)
@@ -676,7 +671,6 @@ func NewWikiPost(ctx *context.Context) {
676671

677672
// EditWiki render wiki modify page
678673
func EditWiki(ctx *context.Context) {
679-
ctx.Data["PageIsWiki"] = true
680674
ctx.Data["PageIsWikiEdit"] = true
681675

682676
if !ctx.Repo.Repository.HasWiki() {
@@ -696,7 +690,6 @@ func EditWiki(ctx *context.Context) {
696690
func EditWikiPost(ctx *context.Context) {
697691
form := web.GetForm(ctx).(*forms.NewWikiForm)
698692
ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
699-
ctx.Data["PageIsWiki"] = true
700693

701694
if ctx.HasError() {
702695
ctx.HTML(http.StatusOK, tplWikiNew)

routers/web/web.go

+1
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,7 @@ func RegisterRoutes(m *web.Route) {
947947
m.Get("/commit/{sha:[a-f0-9]{7,40}}.{ext:patch|diff}", repo.RawDiff)
948948
}, repo.MustEnableWiki, func(ctx *context.Context) {
949949
ctx.Data["PageIsWiki"] = true
950+
ctx.Data["CloneButtonOriginLink"] = ctx.Repo.Repository.WikiCloneLink()
950951
})
951952

952953
m.Group("/wiki", func() {

templates/base/head.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<meta name="keywords" content="{{MetaKeywords}}">
1313
<meta name="referrer" content="no-referrer" />
1414
{{if .GoGetImport}}
15-
<meta name="go-import" content="{{.GoGetImport}} git {{.CloneLink.HTTPS}}">
15+
<meta name="go-import" content="{{.GoGetImport}} git {{.RepoCloneLink.HTTPS}}">
1616
<meta name="go-source" content="{{.GoGetImport}} _ {{.GoDocDirectory}} {{.GoDocFile}}">
1717
{{end}}
1818
{{if .FeedURL}}

templates/repo/clone_buttons.tmpl

+18-36
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,24 @@
1-
{{if not $.DisableHTTP}}
2-
<button class="ui basic clone button no-transition" id="repo-clone-https" data-link="{{if $.PageIsWiki}}{{$.WikiCloneLink.HTTPS}}{{else}}{{$.CloneLink.HTTPS}}{{end}}">
1+
<!-- at least there will be one button (by context/repo.go) -->
2+
{{if $.CloneButtonShowHTTPS}}
3+
<button class="ui basic clone button no-transition" id="repo-clone-https" data-link="{{$.CloneButtonOriginLink.HTTPS}}">
34
{{if UseHTTPS}}HTTPS{{else}}HTTP{{end}}
45
</button>
56
{{end}}
6-
{{if and (not $.DisableSSH) (or $.IsSigned $.ExposeAnonSSH)}}
7-
<button class="ui basic clone button no-transition" id="repo-clone-ssh" data-link="{{if $.PageIsWiki}}{{$.WikiCloneLink.SSH}}{{else}}{{$.CloneLink.SSH}}{{end}}">
7+
{{if $.CloneButtonShowSSH}}
8+
<button class="ui basic clone button no-transition" id="repo-clone-ssh" data-link="{{$.CloneButtonOriginLink.SSH}}">
89
SSH
910
</button>
1011
{{end}}
11-
{{if not $.DisableHTTP}}
12-
<input id="repo-clone-url" value="{{if $.PageIsWiki}}{{$.WikiCloneLink.HTTPS}}{{else}}{{$.CloneLink.HTTPS}}{{end}}" readonly>
13-
{{else if and (not .DisableSSH) (or $.IsSigned $.ExposeAnonSSH)}}
14-
<input id="repo-clone-url" value="{{if $.PageIsWiki}}{{$.WikiCloneLink.SSH}}{{else}}{{$.CloneLink.SSH}}{{end}}" readonly>
15-
{{end}}
16-
{{if or (not $.DisableHTTP) (and (not $.DisableSSH) (or $.IsSigned $.ExposeAnonSSH))}}
17-
<button class="ui basic icon button tooltip" id="clipboard-btn" data-content="{{.i18n.Tr "copy_url"}}" data-clipboard-target="#repo-clone-url">
18-
{{svg "octicon-paste"}}
19-
</button>
20-
{{end}}
21-
{{if not (and $.DisableHTTP $.DisableSSH)}}
22-
<script>
23-
<!-- /* eslint-disable */ -->
24-
window.config.pageData['repoCloneButtons']= {httpsDisabled: {{$.DisableHTTP}}};
25-
</script>
26-
<script>
27-
(() => {
28-
const tmplData = window.config.pageData.repoCloneButtons;
29-
const isSSH = tmplData.httpsDisabled || localStorage.getItem('repo-clone-protocol') === 'ssh';
30-
const sshButton = document.getElementById('repo-clone-ssh');
31-
const httpsButton = document.getElementById('repo-clone-https');
32-
const input = document.getElementById('repo-clone-url');
33-
if (input) input.value = (isSSH ? sshButton : httpsButton).getAttribute('data-link');
34-
if (sshButton) sshButton.classList[isSSH ? 'add' : 'remove']('primary');
35-
if (httpsButton) httpsButton.classList[isSSH ? 'remove' : 'add']('primary');
36-
setTimeout(() => {
37-
if (sshButton) sshButton.classList.remove('no-transition');
38-
if (httpsButton) httpsButton.classList.remove('no-transition');
39-
}, 100);
40-
})();
41-
</script>
42-
{{end}}
12+
<!-- the value will be update by initRepoCloneLink, the code below is used to avoid UI flicking -->
13+
<input id="repo-clone-url" value="" readonly>
14+
<script>
15+
(() => {
16+
const proto = localStorage.getItem('repo-clone-protocol') || 'https';
17+
const btn = document.getElementById(`repo-clone-${proto}`);
18+
// it's ok if we didn't find the btn here, initRepoCloneLink will take all the work
19+
document.getElementById('repo-clone-url').value = btn ? btn.getAttribute('data-link') : '';
20+
})();
21+
</script>
22+
<button class="ui basic icon button tooltip" id="clipboard-btn" data-content="{{.i18n.Tr "copy_url"}}" data-clipboard-target="#repo-clone-url">
23+
{{svg "octicon-paste"}}
24+
</button>

templates/repo/empty.tmpl

+15-10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<h4 class="ui top attached header">
1919
{{.i18n.Tr "repo.quick_guide"}}
2020
</h4>
21-
<div class="ui attached guide table segment">
21+
<div class="ui attached guide table segment empty-repo-guide">
2222
<div class="item">
2323
<h3>{{.i18n.Tr "repo.clone_this_repo"}} <small>{{.i18n.Tr "repo.clone_helper" "http://git-scm.com/book/en/Git-Basics-Getting-a-Git-Repository" | Str2html}}</small></h3>
2424
<div class="ui action small input">
@@ -37,7 +37,7 @@ git init
3737
{{if ne .Repository.DefaultBranch "master"}}git checkout -b {{.Repository.DefaultBranch}}{{end}}
3838
git add README.md
3939
git commit -m "first commit"
40-
git remote add origin <span class="clone-url">{{$.CloneLink.HTTPS}}</span>
40+
git remote add origin <span class="clone-url"></span>
4141
git push -u origin {{.Repository.DefaultBranch}}</code></pre>
4242
</div>
4343
</div>
@@ -46,18 +46,23 @@ git push -u origin {{.Repository.DefaultBranch}}</code></pre>
4646
<div class="item">
4747
<h3>{{.i18n.Tr "repo.push_exist_repo"}}</h3>
4848
<div class="markup">
49-
<pre><code>git remote add origin <span class="clone-url">{{$.CloneLink.HTTPS}}</span>
49+
<pre><code>git remote add origin <span class="clone-url"></span>
5050
git push -u origin {{.Repository.DefaultBranch}}</code></pre>
5151
</div>
5252
</div>
53-
<script defer>
54-
/* eslint-disable no-undef */
55-
const cloneUrls = document.getElementsByClassName('clone-url');
56-
if (cloneUrls) {
57-
for (let i = 0; i < cloneUrls.length; i++) {
58-
cloneUrls[i].textContent = (isSSH ? sshButton : httpsButton).getAttribute('data-link');
53+
<!-- the clone-url content will be update by initRepoCloneLink, the code below is used to avoid UI flicking -->
54+
<script>
55+
(() => {
56+
const proto = localStorage.getItem('repo-clone-protocol') || 'https';
57+
const btn = document.getElementById(`repo-clone-${proto}`);
58+
const cloneUrls = document.getElementsByClassName('clone-url');
59+
// it's ok if we didn't find the btn here, initRepoCloneLink will take all the work
60+
if (btn) {
61+
for (let i = 0; i < cloneUrls.length; i++) {
62+
cloneUrls[i].textContent = btn.getAttribute('data-link');
63+
}
5964
}
60-
}
65+
})();
6166
</script>
6267
{{end}}
6368
{{else}}

templates/repo/home.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
<a class="item archive-link" href="{{$.RepoLink}}/archive/{{PathEscapeSegments $.BranchName}}.zip" rel="nofollow">{{svg "octicon-file-zip" 16 "mr-3"}}{{.i18n.Tr "repo.download_zip"}}</a>
126126
<a class="item archive-link" href="{{$.RepoLink}}/archive/{{PathEscapeSegments $.BranchName}}.tar.gz" rel="nofollow">{{svg "octicon-file-zip" 16 "mr-3"}}{{.i18n.Tr "repo.download_tar"}}</a>
127127
<a class="item archive-link" href="{{$.RepoLink}}/archive/{{PathEscapeSegments $.BranchName}}.bundle" rel="nofollow">{{svg "octicon-package" 16 "mr-3"}}{{.i18n.Tr "repo.download_bundle"}}</a>
128-
<a class="item" href="vscode://vscode.git/clone?url={{if $.PageIsWiki}}{{$.WikiCloneLink.HTTPS}}{{else}}{{$.CloneLink.HTTPS}}{{end}}">{{svg "gitea-vscode" 16 "mr-3"}}{{.i18n.Tr "repo.clone_in_vsc"}}</a>
128+
<a class="item" href="vscode://vscode.git/clone?url={{$.RepoCloneLink.HTTPS}}">{{svg "gitea-vscode" 16 "mr-3"}}{{.i18n.Tr "repo.clone_in_vsc"}}</a>
129129
</div>
130130
</button>
131131
</div>

templates/repo/migrate/migrating.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{template "base/head" .}}
2-
<div class="page-content repository quickstart">
2+
<div class="page-content repository">
33
{{template "repo/header" .}}
44
<div class="ui container">
55
<div class="ui grid">

web_src/js/features/repo-common.js

+47-17
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,56 @@ export function initRepoArchiveLinks() {
4343
});
4444
}
4545

46-
export function initRepoClone() {
47-
// Quick start and repository home
48-
$('#repo-clone-ssh').on('click', function () {
49-
$('.clone-url').text($(this).data('link'));
50-
$('#repo-clone-url').val($(this).data('link'));
51-
$(this).addClass('primary');
52-
$('#repo-clone-https').removeClass('primary');
46+
export function initRepoCloneLink() {
47+
const defaultGitProtocol = 'https'; // ssh or https
48+
49+
const $repoCloneSsh = $('#repo-clone-ssh');
50+
const $repoCloneHttps = $('#repo-clone-https');
51+
const $inputLink = $('#repo-clone-url');
52+
53+
if ((!$repoCloneSsh.length && !$repoCloneHttps.length) || !$inputLink.length) {
54+
return;
55+
}
56+
57+
const updateUi = () => {
58+
let isSSH = (localStorage.getItem('repo-clone-protocol') || defaultGitProtocol) === 'ssh';
59+
// there must be at least one clone button (by context/repo.go). if no ssh, then there must be https.
60+
if (isSSH && $repoCloneSsh.length === 0) {
61+
isSSH = false;
62+
} else if (!isSSH && $repoCloneHttps.length === 0) {
63+
isSSH = true;
64+
}
65+
const cloneLink = (isSSH ? $repoCloneSsh : $repoCloneHttps).attr('data-link');
66+
$inputLink.val(cloneLink);
67+
if (isSSH) {
68+
$repoCloneSsh.addClass('primary');
69+
$repoCloneHttps.removeClass('primary');
70+
} else {
71+
$repoCloneSsh.removeClass('primary');
72+
$repoCloneHttps.addClass('primary');
73+
}
74+
// the empty repo guide
75+
$('.quickstart .empty-repo-guide .clone-url').text(cloneLink);
76+
};
77+
updateUi();
78+
79+
setTimeout(() => {
80+
// restore animation after first init
81+
$repoCloneSsh.removeClass('no-transition');
82+
$repoCloneHttps.removeClass('no-transition');
83+
}, 100);
84+
85+
$repoCloneSsh.on('click', () => {
5386
localStorage.setItem('repo-clone-protocol', 'ssh');
87+
updateUi();
5488
});
55-
$('#repo-clone-https').on('click', function () {
56-
$('.clone-url').text($(this).data('link'));
57-
$('#repo-clone-url').val($(this).data('link'));
58-
$(this).addClass('primary');
59-
if ($('#repo-clone-ssh').length > 0) {
60-
$('#repo-clone-ssh').removeClass('primary');
61-
localStorage.setItem('repo-clone-protocol', 'https');
62-
}
89+
$repoCloneHttps.on('click', () => {
90+
localStorage.setItem('repo-clone-protocol', 'https');
91+
updateUi();
6392
});
64-
$('#repo-clone-url').on('click', function () {
65-
$(this).select();
93+
94+
$inputLink.on('click', () => {
95+
$inputLink.select();
6696
});
6797
}
6898

web_src/js/features/repo-legacy.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {svg} from '../svg.js';
1616
import {htmlEscape} from 'escape-goat';
1717
import {initRepoBranchTagDropdown} from '../components/RepoBranchTagDropdown.js';
1818
import {
19-
initRepoClone,
19+
initRepoCloneLink,
2020
initRepoCommonBranchOrTagDropdown,
2121
initRepoCommonFilterSearchDropdown,
2222
initRepoCommonLanguageStats,
@@ -498,7 +498,7 @@ export function initRepository() {
498498
initRepoCommonFilterSearchDropdown('.choose.branch .dropdown');
499499
}
500500

501-
initRepoClone();
501+
initRepoCloneLink();
502502
initRepoCommonLanguageStats();
503503
initRepoSettingBranches();
504504

0 commit comments

Comments
 (0)