openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
102 lines (101 loc) • 3.03 kB
JavaScript
import hostedGitInfo from "hosted-git-info";
//#region src/agents/utils/git.ts
/**
* Git source parsing helpers.
*
* Normalizes git-style package references into host/path identity.
*/
function stripRef(e) {
const t = e.indexOf("://");
const n = e.startsWith("git@") ? e.indexOf(":") + 1 : e.indexOf("/", t < 0 ? 0 : t + 3) + 1;
if (n <= 0) return e;
const r = e.slice(n).search(/[?#]/);
const i = r < 0 ? e.length : n + r;
let a = e.indexOf("@", n);
if (a === n) a = e.indexOf("@", n + 1);
if (a <= n || a === i - 1 || a >= i) return e;
return t < 0 ? e.slice(0, a) : e.slice(0, a) + e.slice(i);
}
function hasUnsafePathSegments(e) {
const t = e.split(/[?#]/, 1)[0] ?? "";
return t.includes("\\") || /(?:^|\/)(?:\.|%2e){1,2}(?:\/|$)/i.test(t);
}
function parseGenericGitUrl(e) {
let t;
let n;
const r = e.match(/^git@([^:]+):(.+)$/);
if (r) {
t = r[1] ?? "";
n = r[2] ?? "";
} else if (/^(?:https?|ssh|git):\/\//.test(e)) {
const r = URL.parse(e);
if (!r) return null;
t = r.hostname;
n = r.pathname.replace(/^\/+/, "");
} else {
const r = e.indexOf("/");
if (r < 0) return null;
t = e.slice(0, r);
n = e.slice(r + 1);
if (!t.includes(".") && t !== "localhost") return null;
}
return normalizeGitSource(t, n);
}
function normalizeGitSource(e, t) {
const n = normalizeGitPath(t);
return isSafeGitHost(e) && n ? {
type: "git",
host: e,
path: n
} : null;
}
function isSafeGitHost(e) {
return Boolean(e) && !e.includes("/") && !e.includes("\\") && e !== "." && e !== "..";
}
function normalizeGitPath(e) {
const t = e.replace(/\.git$/, "").replace(/^\/+/, "").split("/");
if (t.length < 2) return null;
if (t.some((e) => !e || e === "." || e === ".." || e.includes("\\"))) return null;
return t.join("/");
}
function parseHostedGitUrl(e) {
const t = [e];
if (!e.includes("://") && !e.startsWith("git@")) t.push(`https://${e}`);
for (const e of t) {
const t = hostedGitInfo.fromUrl(e);
if (!t) continue;
const n = normalizeGitSource(t.domain || "", `${t.user}/${t.project}`);
if (n) return n;
}
return null;
}
/**
* Parse git source into a GitSource.
*
* Rules:
* - With git: prefix, accept all historical shorthand forms.
* - Without git: prefix, only accept explicit protocol URLs.
*/
function parseGitUrl(e) {
const t = e.trim();
const n = t.startsWith("git:");
const r = n ? t.slice(4).trim() : t;
if (!n && !/^(https?|ssh|git):\/\//i.test(r)) return null;
const i = stripRef(r);
if (hasUnsafePathSegments(i)) return null;
return parseHostedGitUrl(i) ?? parseGenericGitUrl(i);
}
//#endregion
//#region src/config/cloud-worker-project-profiles.ts
/** Normalize a Git origin URL to a lowercase host/path repository identity. */
function normalizeCloudRepo(e) {
const t = e.trim();
if (!t) return;
const n = parseGitUrl(`git:${t}`);
if (!n) return;
const r = n.path.split("/").filter(Boolean);
if (!n.host || r.length < 2) return;
return `${n.host}/${r.join("/")}`.toLowerCase();
}
//#endregion
export { parseGitUrl as n, normalizeCloudRepo as t };