summaryrefslogtreecommitdiffstats
path: root/templates
diff options
context:
space:
mode:
authorGiteabot <teabot@gitea.io>2024-02-28 23:25:53 +0800
committerGitHub <noreply@github.com>2024-02-28 15:25:53 +0000
commit222f93822eb4d03f5001ef665377a115bc27ccb6 (patch)
treeb6d95238c0d2009eb3b88a3a2ba559b67c9d3778 /templates
parenteabcfd3f7d9321fcf03e52977c178a96627a68da (diff)
downloadgitea-222f93822eb4d03f5001ef665377a115bc27ccb6.tar.gz
gitea-222f93822eb4d03f5001ef665377a115bc27ccb6.zip
Fix URL calculation in clone input box (#29470) (#29473)
Backport #29470 by @silverwind Ported the function as-is and added comments so we don't forget about this in the future. Fixes: https://github.com/go-gitea/gitea/issues/29462 Co-authored-by: silverwind <me@silverwind.io>
Diffstat (limited to 'templates')
-rw-r--r--templates/repo/clone_script.tmpl22
1 files changed, 15 insertions, 7 deletions
diff --git a/templates/repo/clone_script.tmpl b/templates/repo/clone_script.tmpl
index 0797b400d8..8b951d8fc4 100644
--- a/templates/repo/clone_script.tmpl
+++ b/templates/repo/clone_script.tmpl
@@ -24,14 +24,22 @@
const btn = isSSH ? sshBtn : httpsBtn;
if (!btn) return;
- let link = btn.getAttribute('data-link');
- if (link.startsWith('http://') || link.startsWith('https://')) {
- // use current protocol/host as the clone link
- const url = new URL(link);
- url.protocol = window.location.protocol;
- url.host = window.location.host;
- link = url.toString();
+ // NOTE: Keep this function in sync with the one in the js folder
+ function toOriginUrl(urlStr) {
+ try {
+ if (urlStr.startsWith('http://') || urlStr.startsWith('https://') || urlStr.startsWith('/')) {
+ const {origin, protocol, hostname, port} = window.location;
+ const url = new URL(urlStr, origin);
+ url.protocol = protocol;
+ url.hostname = hostname;
+ url.port = port || (protocol === 'https:' ? '443' : '80');
+ return url.toString();
+ }
+ } catch {}
+ return urlStr;
}
+ const link = toOriginUrl(btn.getAttribute('data-link'));
+
for (const el of document.getElementsByClassName('js-clone-url')) {
el[el.nodeName === 'INPUT' ? 'value' : 'textContent'] = link;
}