Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
40 lines
2.0 KiB
JavaScript
40 lines
2.0 KiB
JavaScript
import { execFile } from 'child_process';
|
|
import { GitError } from './errors.js';
|
|
|
|
// Pfad- und Branch-Validierung (ADR 0003 §2).
|
|
export const validatePath = (p)=>{
|
|
const bad = (msg)=>{ throw new GitError('G08', `Ungültiger Pfad: ${msg}`); };
|
|
if(typeof p !== 'string' || !p) bad('leer');
|
|
if(p.length > 255) bad('länger als 255 Zeichen');
|
|
if(p.startsWith('/') || p.includes('\\') || /[\0-\x1f]/.test(p)) bad('nur relative Pfade mit / erlaubt');
|
|
const parts = p.split('/');
|
|
if(parts.some((s)=>s === '' || s === '.' || s === '..')) bad('kein .., . oder leere Segmente');
|
|
if(parts[0].toLowerCase() === '.git' || parts.some((s)=>s.toLowerCase() === '.git')) bad('nicht unter .git');
|
|
if(!p.endsWith('.md')) bad('Endung .md erforderlich');
|
|
return p;
|
|
};
|
|
|
|
// Ordner (ohne Endung-Pflicht), für brewDir/assetsDir und Dateilisten
|
|
export const validateDir = (d)=>{
|
|
if(d === '' || d === undefined || d === null) return '';
|
|
if(typeof d !== 'string' || d.length > 255 || d.startsWith('/') || d.includes('\\') || /[\0-\x1f]/.test(d))
|
|
throw new GitError('G08', 'Ungültiger Ordner.');
|
|
const parts = d.replace(/\/+$/, '').split('/');
|
|
if(parts.some((s)=>s === '' || s === '.' || s === '..' || s.toLowerCase() === '.git'))
|
|
throw new GitError('G08', 'Ungültiger Ordner.');
|
|
return parts.join('/');
|
|
};
|
|
|
|
// `git check-ref-format` auf refs/heads/<name> (nicht --branch: das expandiert z. B. "@{-1}").
|
|
export const validateBranch = (name)=>new Promise((resolve, reject)=>{
|
|
const bad = ()=>reject(new GitError('G08', 'Ungültiger Branch-Name.'));
|
|
if(typeof name !== 'string' || !name || name.length > 255 || name.startsWith('-') || /[\0-\x1f]/.test(name)) return bad();
|
|
execFile('git', ['check-ref-format', `refs/heads/${name}`], { timeout: 5000 }, (err)=>err ? bad() : resolve(name));
|
|
});
|
|
|
|
export const slugify = (title)=>{
|
|
const s = String(title || '').normalize('NFKD').replace(/[̀-ͯ]/g, '').toLowerCase()
|
|
.replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 80);
|
|
return s || 'brew';
|
|
};
|