Skip to content

Commit 222f938

Browse files
GiteaBotsilverwind
andauthored
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: #29462 Co-authored-by: silverwind <[email protected]>
1 parent eabcfd3 commit 222f938

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

templates/repo/clone_script.tmpl

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,22 @@
2424
const btn = isSSH ? sshBtn : httpsBtn;
2525
if (!btn) return;
2626

27-
let link = btn.getAttribute('data-link');
28-
if (link.startsWith('http://') || link.startsWith('https://')) {
29-
// use current protocol/host as the clone link
30-
const url = new URL(link);
31-
url.protocol = window.location.protocol;
32-
url.host = window.location.host;
33-
link = url.toString();
27+
// NOTE: Keep this function in sync with the one in the js folder
28+
function toOriginUrl(urlStr) {
29+
try {
30+
if (urlStr.startsWith('http://') || urlStr.startsWith('https://') || urlStr.startsWith('/')) {
31+
const {origin, protocol, hostname, port} = window.location;
32+
const url = new URL(urlStr, origin);
33+
url.protocol = protocol;
34+
url.hostname = hostname;
35+
url.port = port || (protocol === 'https:' ? '443' : '80');
36+
return url.toString();
37+
}
38+
} catch {}
39+
return urlStr;
3440
}
41+
const link = toOriginUrl(btn.getAttribute('data-link'));
42+
3543
for (const el of document.getElementsByClassName('js-clone-url')) {
3644
el[el.nodeName === 'INPUT' ? 'value' : 'textContent'] = link;
3745
}

web_src/js/webcomponents/GiteaOriginUrl.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
// Convert an absolute or relative URL to an absolute URL with the current origin
1+
// Convert an absolute or relative URL to an absolute URL with the current origin. It only
2+
// processes absolute HTTP/HTTPS URLs or relative URLs like '/xxx' or '//host/xxx'.
3+
// NOTE: Keep this function in sync with clone_script.tmpl
24
export function toOriginUrl(urlStr) {
35
try {
4-
// only process absolute HTTP/HTTPS URL or relative URLs ('/xxx' or '//host/xxx')
56
if (urlStr.startsWith('http://') || urlStr.startsWith('https://') || urlStr.startsWith('/')) {
67
const {origin, protocol, hostname, port} = window.location;
78
const url = new URL(urlStr, origin);

0 commit comments

Comments
 (0)