Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

Extract branch sanitization to shared contracts module

Julius Marmingeb1608f41adff
diff --git a/apps/server/src/git/Layers/CodexTextGeneration.ts b/apps/server/src/git/Layers/CodexTextGeneration.ts
index 313ce8d99..4436c81de 100644
--- a/apps/server/src/git/Layers/CodexTextGeneration.ts
+++ b/apps/server/src/git/Layers/CodexTextGeneration.ts
@@ -3,6 +3,8 @@ import { randomUUID } from "node:crypto";
import { Effect, FileSystem, Layer, Option, Path, Schema, Stream } from "effect";
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
+import { sanitizeBranchFragment, sanitizeFeatureBranchName } from "@t3tools/contracts";
+
import { resolveAttachmentPath } from "../../attachmentStore.ts";
import { ServerConfig } from "../../config.ts";
import { TextGenerationError } from "../Errors.ts";
@@ -93,32 +95,6 @@ function sanitizePrTitle(raw: string): string {
return "Update project changes";
}
-function sanitizeBranchName(raw: string): string {
- const normalized = raw
- .trim()
- .toLowerCase()
- .replace(/['"`]/g, "")
- .replace(/^[./\s_-]+|[./\s_-]+$/g, "");
-
- const branchFragment = normalized
- .replace(/[^a-z0-9/_-]+/g, "-")
- .replace(/\/+/g, "/")
- .replace(/-+/g, "-")
- .replace(/^[./_-]+|[./_-]+$/g, "")
- .slice(0, 64)
- .replace(/[./_-]+$/g, "");
-
- return branchFragment.length > 0 ? branchFragment : "update";
-}
-
-function sanitizeFeatureBranchName(raw: string): string {
- const sanitized = sanitizeBranchName(raw);
- if (sanitized.includes("/")) {
- return sanitized.startsWith("feature/") ? sanitized : `feature/${sanitized}`;
- }
- return `feature/${sanitized}`;
-}
-
const makeCodexTextGeneration = Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
@@ -475,7 +451,7 @@ const makeCodexTextGeneration = Effect.gen(function* () {
});
return {
- branch: sanitizeBranchName(generated.branch),
+ branch: sanitizeBranchFragment(generated.branch),
} satisfies BranchNameGenerationResult;
});
};
diff --git a/apps/server/src/git/Layers/GitManager.ts b/apps/server/src/git/Layers/GitManager.ts
index 92d6fffce..b7282b405 100644
--- a/apps/server/src/git/Layers/GitManager.ts
+++ b/apps/server/src/git/Layers/GitManager.ts
@@ -1,6 +1,7 @@
import { randomUUID } from "node:crypto";
import { Effect, FileSystem, Layer, Path } from "effect";
+import { sanitizeFeatureBranchName } from "@t3tools/contracts";
import { GitManagerError } from "../Errors.ts";
import { GitManager, type GitManagerShape } from "../Services/GitManager.ts";
@@ -104,32 +105,6 @@ function sanitizeCommitMessage(generated: {
};
}
-function sanitizeBranchFragment(raw: string): string {
- const normalized = raw
- .trim()
- .toLowerCase()
- .replace(/['"`]/g, "")
- .replace(/^[./\s_-]+|[./\s_-]+$/g, "");
-
- const branchFragment = normalized
- .replace(/[^a-z0-9/_-]+/g, "-")
- .replace(/\/+/g, "/")
- .replace(/-+/g, "-")
- .replace(/^[./_-]+|[./_-]+$/g, "")
- .slice(0, 64)
- .replace(/[./_-]+$/g, "");
-
- return branchFragment.length > 0 ? branchFragment : "update";
-}
-
-function sanitizeFeatureBranchName(raw: string): string {
- const sanitized = sanitizeBranchFragment(raw);
- if (sanitized.includes("/")) {
- return sanitized.startsWith("feature/") ? sanitized : `feature/${sanitized}`;
- }
- return `feature/${sanitized}`;
-}
-
function formatCommitMessage(subject: string, body: string): string {
const trimmedBody = body.trim();
if (trimmedBody.length === 0) {
@@ -324,16 +299,14 @@ export const makeGitManager = Effect.gen(function* () {
branch: input.branch,
stagedSummary: limitContext(context.stagedSummary, 8_000),
stagedPatch: limitContext(context.stagedPatch, 50_000),
- ...(input.includeBranch !== undefined ? { includeBranch: input.includeBranch } : {}),
+ ...(input.includeBranch ? { includeBranch: true } : {}),
})
.pipe(Effect.map((result) => sanitizeCommitMessage(result)));
return {
subject: generated.subject,
body: generated.body,
- ...(generated.branch !== undefined
- ? { branch: sanitizeFeatureBranchName(generated.branch) }
- : {}),
+ ...(generated.branch !== undefined ? { branch: generated.branch } : {}),
commitMessage: formatCommitMessage(generated.subject, generated.body),
};
});
diff --git a/apps/web/src/components/GitActionsControl.logic.ts b/apps/web/src/components/GitActionsControl.logic.ts
index 23cd63e62..bbea434f7 100644
--- a/apps/web/src/components/GitActionsControl.logic.ts
+++ b/apps/web/src/components/GitActionsControl.logic.ts
@@ -1,7 +1,8 @@
-import type {
- GitRunStackedActionResult,
- GitStackedAction,
- GitStatusResult,
+import {
+ type GitRunStackedActionResult,
+ type GitStackedAction,
+ type GitStatusResult,
+ sanitizeBranchFragment,
} from "@t3tools/contracts";
export type GitActionIconName = "commit" | "push" | "pr";
@@ -321,24 +322,6 @@ export function resolveDefaultBranchActionDialogCopy(input: {
};
}
-function sanitizeBranchFragment(raw: string): string {
- const normalized = raw
- .trim()
- .toLowerCase()
- .replace(/['"`]/g, "")
- .replace(/^[./\s_-]+|[./\s_-]+$/g, "");
-
- const branchFragment = normalized
- .replace(/[^a-z0-9/_-]+/g, "-")
- .replace(/\/+/g, "/")
- .replace(/-+/g, "-")
- .replace(/^[./_-]+|[./_-]+$/g, "")
- .slice(0, 64)
- .replace(/[./_-]+$/g, "");
-
- return branchFragment.length > 0 ? branchFragment : "update";
-}
-
export function resolveAutoFeatureBranchName(
existingBranchNames: readonly string[],
preferredBranch?: string,
diff --git a/packages/contracts/src/git.ts b/packages/contracts/src/git.ts
index 12bd739dc..7e7708b74 100644
--- a/packages/contracts/src/git.ts
+++ b/packages/contracts/src/git.ts
@@ -179,3 +179,39 @@ export const GitPullResult = Schema.Struct({
upstreamBranch: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
});
export type GitPullResult = typeof GitPullResult.Type;
+
+// Branch name utilities
+
+/**
+ * Sanitize an arbitrary string into a valid, lowercase git branch fragment.
+ * Strips quotes, collapses separators, limits to 64 chars.
+ */
+export function sanitizeBranchFragment(raw: string): string {
+ const normalized = raw
+ .trim()
+ .toLowerCase()
+ .replace(/['"`]/g, "")
+ .replace(/^[./\s_-]+|[./\s_-]+$/g, "");
+
+ const branchFragment = normalized
+ .replace(/[^a-z0-9/_-]+/g, "-")
+ .replace(/\/+/g, "/")
+ .replace(/-+/g, "-")
+ .replace(/^[./_-]+|[./_-]+$/g, "")
+ .slice(0, 64)
+ .replace(/[./_-]+$/g, "");
+
+ return branchFragment.length > 0 ? branchFragment : "update";
+}
+
+/**
+ * Sanitize a string into a `feature/…` branch name.
+ * Preserves an existing `feature/` prefix or slash-separated namespace.
+ */
+export function sanitizeFeatureBranchName(raw: string): string {
+ const sanitized = sanitizeBranchFragment(raw);
+ if (sanitized.includes("/")) {
+ return sanitized.startsWith("feature/") ? sanitized : `feature/${sanitized}`;
+ }
+ return `feature/${sanitized}`;
+}