import { entitiesShell } from "../entities.shell"; import { entitiesConfig } from "../intershell-config/intershell-config"; import type { ParsedBranch } from "./branch.types"; export class EntityBranch { parseByName(branchName: string): ParsedBranch { const config = entitiesConfig.getConfig().branch; const validPrefixes = config.prefixes; // Split the branch name by "/" to get all parts const parts = branchName.split("/"); // Find the first valid prefix in the parts let foundPrefixIndex = -1; let foundPrefix = ""; // Only check for prefixes if they are not null if (validPrefixes !== null) { for (let i = 0; i < parts.length; i++) { if (validPrefixes.includes(parts[i])) { foundPrefixIndex = i; foundPrefix = parts[i]; break; } } } // If we found a valid prefix, use everything from that point as the name if (foundPrefixIndex !== -1) { const nameParts = parts.slice(foundPrefixIndex); return { prefix: foundPrefix, name: nameParts.join("/"), fullName: branchName, }; } // If no valid prefix found, use the entire name return { prefix: undefined, name: branchName, fullName: branchName, }; } async getCurrentBranch(): Promise { const result = await entitiesShell.gitBranchShowCurrent(); return result.text().trim(); } validate(input: string | ParsedBranch): true | string { const branch = typeof input === "string" ? this.parseByName(input) : input; return this.mainValidator(branch); } private mainValidator(branch: ParsedBranch): true | string { const config = entitiesConfig.getConfig().branch; if (!branch.fullName) return "branch name is empty"; if (config.name.minLength && branch.fullName.length < config.name.minLength) return `branch name should be at least ${config.name.minLength} characters long`; if (config.name.maxLength && branch.fullName.length > config.name.maxLength) return `branch name should be max ${config.name.maxLength} characters, received: ${branch.fullName.length}`; if (config.name.allowedCharacters && !config.name.allowedCharacters.test(branch.fullName)) return "branch name can only contain letters, numbers, hyphens, underscores, and forward slashes"; if (config.name.noConsecutiveSeparators && /[-_/]{2,}/.test(branch.fullName)) return "branch name should not have consecutive separators"; if (config.name.noLeadingTrailingSeparators && /(^[-_/]|[-_/]$)/.test(branch.fullName)) return "branch name should not start or end with separators"; if (branch.fullName === config.defaultBranch) { return true; } // Skip prefix validation if prefixes is null if (config.prefixes === null) { return true; } if (config.prefixes.length === 0) { return true; } const prefix = branch.prefix ?? ""; const name = branch.name; if (!prefix) { return `branch name should have a prefix. valid prefixes: ${config.prefixes.join(", ")}`; } if (!name) { return "branch name should have a name"; } return true; } }