mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-15 23:01:30 +00:00
refactor(coding-agent): centralize git url detection
This commit is contained in:
parent
4719929f6a
commit
254c00b788
3 changed files with 8 additions and 37 deletions
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
6
packages/coding-agent/src/utils/git.ts
Normal file
6
packages/coding-agent/src/utils/git.ts
Normal 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}/`));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue