refactor(coding-agent): centralize git url detection

This commit is contained in:
Mario Zechner 2026-01-24 01:02:16 +01:00
parent 4719929f6a
commit 254c00b788
3 changed files with 8 additions and 37 deletions

View file

@ -5,6 +5,7 @@ import { homedir, tmpdir } from "node:os";
import { basename, dirname, join, relative, resolve } from "node:path";
import { minimatch } from "minimatch";
import { CONFIG_DIR_NAME } from "../config.js";
import { looksLikeGitUrl } from "../utils/git.js";
import type { PackageSource, SettingsManager } from "./settings-manager.js";
export interface PathMetadata {
@ -522,7 +523,7 @@ export class DefaultPackageManager implements PackageManager {
};
}
if (source.startsWith("git:") || this.looksLikeGitUrl(source)) {
if (source.startsWith("git:") || looksLikeGitUrl(source)) {
const repoSpec = source.startsWith("git:") ? source.slice("git:".length).trim() : source;
const [repo, ref] = repoSpec.split("@");
const normalized = repo.replace(/^https?:\/\//, "").replace(/\.git$/, "");
@ -542,12 +543,6 @@ export class DefaultPackageManager implements PackageManager {
return { type: "local", path: source };
}
private looksLikeGitUrl(source: string): boolean {
const gitHosts = ["github.com", "gitlab.com", "bitbucket.org", "codeberg.org"];
const normalized = source.replace(/^https?:\/\//, "");
return gitHosts.some((host) => normalized.startsWith(`${host}/`));
}
/**
* Get a unique identity for a package, ignoring version/ref.
* Used to detect when the same package is in both global and project settings.

View file

@ -195,39 +195,9 @@ export class SettingsManager {
}
}
// Migrate npm:/git: sources from extensions array to packages array
if (Array.isArray(settings.extensions)) {
const localExtensions: string[] = [];
const packageSources: string[] = [];
for (const ext of settings.extensions) {
if (typeof ext !== "string") continue;
if (ext.startsWith("npm:") || ext.startsWith("git:") || SettingsManager.looksLikeGitUrl(ext)) {
packageSources.push(ext);
} else {
localExtensions.push(ext);
}
}
if (packageSources.length > 0) {
const existingPackages = Array.isArray(settings.packages) ? settings.packages : [];
settings.packages = [...existingPackages, ...packageSources];
settings.extensions = localExtensions.length > 0 ? localExtensions : undefined;
if (settings.extensions === undefined) {
delete settings.extensions;
}
}
}
return settings as Settings;
}
private static looksLikeGitUrl(source: string): boolean {
const gitHosts = ["github.com", "gitlab.com", "bitbucket.org", "codeberg.org"];
const normalized = source.replace(/^https?:\/\//, "");
return gitHosts.some((host) => normalized.startsWith(`${host}/`));
}
private loadProjectSettings(): Settings {
// In-memory mode: return stored in-memory project settings
if (!this.persist) {

View file

@ -0,0 +1,6 @@
const GIT_HOSTS = ["github.com", "gitlab.com", "bitbucket.org", "codeberg.org"];
export function looksLikeGitUrl(source: string): boolean {
const normalized = source.replace(/^https?:\/\//, "");
return GIT_HOSTS.some((host) => normalized.startsWith(`${host}/`));
}