mirror of
https://github.com/garrytan/gstack.git
synced 2026-05-11 06:57:25 +08:00
fix+test(gbrain-sync): handle empty-slug edge in constrainSourceId, add no-origin and basename-empty regression tests
PR #1330 (merged in the prior commit) addressed the dot-in-host and length-overflow cases for source-id derivation, but constrainSourceId silently returned "${prefix}-" when the input sanitized to an empty slug — invalid per gbrain's `^[a-z0-9](?:[a-z0-9-]{0,30}[a-z0-9])?$` validator on the trailing hyphen. Adds an explicit empty-slug branch that falls back to a sha1-prefixed id ("gstack-code-<6hex>") so the output stays gbrain-valid for every input shape. Two new regression tests cover the corners PR #1330's coverage left exposed: - no-origin fallback: a cwd repo with no `origin` remote configured must still derive a valid id from the basename. - basename-sanitizes-to-empty: a repo whose path basename is all non-alnum (e.g. "___") must produce the hash-only fallback, not an invalid trailing-hyphen id. Both run the CLI inside temp git repos for genuine end-to-end coverage (matches the pattern PR #1330 established for its own four remote-shape cases). Co-Authored-By: Richard Dubach <radubach@gmail.com>
This commit is contained in:
@@ -188,6 +188,14 @@ function deriveCodeSourceId(repoPath: string): string {
|
||||
function constrainSourceId(prefix: string, raw: string): string {
|
||||
const MAX = 32;
|
||||
const slug = raw.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
|
||||
// Empty slug after sanitize (e.g. raw was all non-alnum like "___") would
|
||||
// produce "${prefix}-" which fails gbrain's validator on the trailing
|
||||
// hyphen. Fall back to a deterministic hash of the original input so the
|
||||
// result is stable across runs of the same repo.
|
||||
if (!slug) {
|
||||
const hash = createHash("sha1").update(raw || "_empty").digest("hex").slice(0, 6);
|
||||
return `${prefix}-${hash}`;
|
||||
}
|
||||
const full = `${prefix}-${slug}`;
|
||||
if (full.length <= MAX) return full;
|
||||
const hash = createHash("sha1").update(slug).digest("hex").slice(0, 6);
|
||||
|
||||
Reference in New Issue
Block a user