Prepare datamodel for multi-environment (#1765)
Co-authored-by: justsomelegs <[email protected]> Co-authored-by: codex <[email protected]> Co-authored-by: Cursor Agent <[email protected]> Co-authored-by: cursor[bot] <206951365+cursor[bot]@users.noreply.github.com>
This commit is contained in:
121 files changed
+10195
-4958
No files matched your search
@@ -1,7 +1,54 @@
|
||||
import type { GitStatusRemoteResult, GitStatusResult } from "@t3tools/contracts";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { applyGitStatusStreamEvent } from "./git";
|
||||
import {
|
||||
applyGitStatusStreamEvent,
|
||||
normalizeGitRemoteUrl,
|
||||
parseGitHubRepositoryNameWithOwnerFromRemoteUrl,
|
||||
} from "./git";
|
||||
|
||||
describe("normalizeGitRemoteUrl", () => {
|
||||
it("canonicalizes equivalent GitHub remotes across protocol variants", () => {
|
||||
expect(normalizeGitRemoteUrl("[email protected]:T3Tools/T3Code.git")).toBe(
|
||||
"github.com/t3tools/t3code",
|
||||
);
|
||||
expect(normalizeGitRemoteUrl("https://github.com/T3Tools/T3Code.git")).toBe(
|
||||
"github.com/t3tools/t3code",
|
||||
);
|
||||
expect(normalizeGitRemoteUrl("ssh://[email protected]/T3Tools/T3Code")).toBe(
|
||||
"github.com/t3tools/t3code",
|
||||
);
|
||||
});
|
||||
|
||||
it("preserves nested group paths for providers like GitLab", () => {
|
||||
expect(normalizeGitRemoteUrl("[email protected]:T3Tools/platform/T3Code.git")).toBe(
|
||||
"gitlab.com/t3tools/platform/t3code",
|
||||
);
|
||||
expect(normalizeGitRemoteUrl("https://gitlab.com/T3Tools/platform/T3Code.git")).toBe(
|
||||
"gitlab.com/t3tools/platform/t3code",
|
||||
);
|
||||
});
|
||||
|
||||
it("drops explicit ports from URL-shaped remotes", () => {
|
||||
expect(normalizeGitRemoteUrl("https://gitlab.company.com:8443/team/project.git")).toBe(
|
||||
"gitlab.company.com/team/project",
|
||||
);
|
||||
expect(normalizeGitRemoteUrl("ssh://[email protected]:2222/team/project.git")).toBe(
|
||||
"gitlab.company.com/team/project",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("parseGitHubRepositoryNameWithOwnerFromRemoteUrl", () => {
|
||||
it("extracts the owner and repository from common GitHub remote shapes", () => {
|
||||
expect(
|
||||
parseGitHubRepositoryNameWithOwnerFromRemoteUrl("[email protected]:T3Tools/T3Code.git"),
|
||||
).toBe("T3Tools/T3Code");
|
||||
expect(
|
||||
parseGitHubRepositoryNameWithOwnerFromRemoteUrl("https://github.com/T3Tools/T3Code.git"),
|
||||
).toBe("T3Tools/T3Code");
|
||||
});
|
||||
});
|
||||
|
||||
describe("applyGitStatusStreamEvent", () => {
|
||||
it("treats a remote-only update as a repository when local state is missing", () => {
|
||||
|
||||
@@ -80,6 +80,56 @@ export function deriveLocalBranchNameFromRemoteRef(branchName: string): string {
|
||||
return branchName.slice(firstSeparatorIndex + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize a git remote URL into a stable comparison key.
|
||||
*/
|
||||
export function normalizeGitRemoteUrl(value: string): string {
|
||||
const normalized = value
|
||||
.trim()
|
||||
.replace(/\/+$/g, "")
|
||||
.replace(/\.git$/i, "")
|
||||
.toLowerCase();
|
||||
|
||||
if (/^(?:ssh|https?|git):\/\//i.test(normalized)) {
|
||||
try {
|
||||
const url = new URL(normalized);
|
||||
const repositoryPath = url.pathname
|
||||
.split("/")
|
||||
.filter((segment) => segment.length > 0)
|
||||
.join("/");
|
||||
if (url.hostname && repositoryPath.includes("/")) {
|
||||
return `${url.hostname}/${repositoryPath}`;
|
||||
}
|
||||
} catch {
|
||||
return normalized;
|
||||
}
|
||||
}
|
||||
|
||||
const scpStyleHostAndPath = /^git@([^:/\s]+)[:/]([^/\s]+(?:\/[^/\s]+)+)$/i.exec(normalized);
|
||||
if (scpStyleHostAndPath?.[1] && scpStyleHostAndPath[2]) {
|
||||
return `${scpStyleHostAndPath[1]}/${scpStyleHostAndPath[2]}`;
|
||||
}
|
||||
|
||||
return normalized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Best-effort parse of a GitHub `owner/repo` identifier from common remote URL shapes.
|
||||
*/
|
||||
export function parseGitHubRepositoryNameWithOwnerFromRemoteUrl(url: string | null): string | null {
|
||||
const trimmed = url?.trim() ?? "";
|
||||
if (trimmed.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const match =
|
||||
/^(?:git@github\.com:|ssh:\/\/git@github\.com\/|https:\/\/github\.com\/|git:\/\/github\.com\/)([^/\s]+\/[^/\s]+?)(?:\.git)?\/?$/i.exec(
|
||||
trimmed,
|
||||
);
|
||||
const repositoryNameWithOwner = match?.[1]?.trim() ?? "";
|
||||
return repositoryNameWithOwner.length > 0 ? repositoryNameWithOwner : null;
|
||||
}
|
||||
|
||||
function deriveLocalBranchNameCandidatesFromRemoteRef(
|
||||
branchName: string,
|
||||
remoteName?: string,
|
||||
|
||||
Reference in new issue
Block a user