Files
thatguygriffandClaude Opus 5 87851de921
CI / Tests (push) Successful in 36s
CI / Generated images (push) Successful in 1m1s
Support instances that serve SSH from another hostname
Discovery matched a git remote's host against the instance URL's host, so
a server answering SSH on a different name than its web UI resolved to
nothing — and said so only in a debug log.

Resolution now runs in three stages. A remote host is matched directly,
then against configured aliases, and failing both the instances are asked
for the repository: Gitea publishes its SSH hostname in a repository's
ssh_url, so the right instance identifies itself. What that turns up is
saved as a host alias, so later repositories on the same host resolve
with no lookup at all, and the mapping is visible and editable rather
than hidden. Each unknown host is probed at most once per session.

When nothing resolves the sidebar now names the unmatched host and offers
Add Host Alias, instead of showing an empty section.

Aliases can also be written by hand as "remote-host = instance URL",
accepting =, -> and =>, ignoring ports, and skipping # comments. List
preferences now merge workspace entries onto global ones rather than
letting an empty global array mask them.

Adds Tests/host-aliases.test.js covering both directions: unmatched hosts
reported and nothing persisted, a hand-written alias, detection from
ssh_url, and a later repository resolving from the stored alias without a
probe. 131 checks across three suites.

Also adds CLAUDE.md, and .gitea/workflows/ci.yml running the suites,
script syntax checks, manifest validation, and a generated-image check.
Tools/make-icons.py gains --check, which compares decompressed pixels so
a differing zlib version cannot fail it spuriously.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MQuusXgZC2dzwpJJ1qhtti
2026-08-28 20:10:05 -03:00

80 lines
2.4 KiB
JavaScript

//
// Git remote URL parsing. Covers the four shapes Gitea hands out: HTTPS, SCP
// style SSH, ssh:// URLs, and git:// URLs.
//
function stripCredentials(host) {
const at = host.lastIndexOf("@");
return at === -1 ? host : host.slice(at + 1);
}
function splitPath(path) {
const parts = path
.replace(/^\/+/, "")
.replace(/\.git$/i, "")
.replace(/\/+$/, "")
.split("/")
.filter(Boolean);
if (parts.length < 2) return null;
// Sub-path installs ("https://example.com/gitea/owner/repo") still end with
// owner/repo, so take the last two components.
return { owner: parts[parts.length - 2], name: parts[parts.length - 1] };
}
/** Parses a remote URL into { host, port, owner, name }, or null. */
function parse(url) {
if (!url) return null;
const trimmed = String(url).trim();
let match = trimmed.match(/^[a-z][a-z0-9+.-]*:\/\/([^/]+)(\/.*)$/i);
if (match) {
let authority = stripCredentials(match[1]);
let port = null;
const portMatch = authority.match(/^(.+):(\d+)$/);
if (portMatch) {
authority = portMatch[1];
port = Number(portMatch[2]);
}
const path = splitPath(match[2]);
if (!path) return null;
return { host: authority.toLowerCase(), port: port, owner: path.owner, name: path.name };
}
// SCP-like: git@host:owner/repo.git
match = trimmed.match(/^([^/@]+@)?([^/:]+):(.+)$/);
if (match) {
const path = splitPath(match[3]);
if (!path) return null;
return {
host: match[2].toLowerCase(),
port: null,
owner: path.owner,
name: path.name,
};
}
return null;
}
/**
* The bare host of an instance base URL, a remote URL, or a plain hostname,
* without scheme, credentials, port, or path. Used to compare a remote against
* a configured instance and to normalise both sides of a host alias.
*/
function hostOf(value) {
const text = String(value || "").trim();
if (!text) return null;
const scheme = text.match(/^[a-z][a-z0-9+.-]*:\/\/([^/]+)/i);
// Without a scheme this is already a bare host, possibly carrying
// credentials, a port, or an SCP-style path after a colon.
const authority = scheme ? scheme[1] : text.replace(/[/:].*$/, "");
const host = stripCredentials(authority).replace(/:\d+$/, "").toLowerCase();
return host || null;
}
exports.parse = parse;
exports.hostOf = hostOf;