Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix URL calculation in clone input box #29470

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions templates/repo/clone_script.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions web_src/js/webcomponents/GiteaOriginUrl.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Convert an absolute or relative URL to an absolute URL with the current origin
// Convert an absolute or relative URL to an absolute URL with the current origin. It only
// processes absolute HTTP/HTTPS URLs or relative URLs like '/xxx' or '//host/xxx'.
// NOTE: Keep this function in sync with clone_script.tmpl
export function toOriginUrl(urlStr) {
try {
// only process absolute HTTP/HTTPS URL or relative URLs ('/xxx' or '//host/xxx')
if (urlStr.startsWith('http://') || urlStr.startsWith('https://') || urlStr.startsWith('/')) {
const {origin, protocol, hostname, port} = window.location;
const url = new URL(urlStr, origin);
Expand Down
Loading