MCPcopy
hub / github.com/garrytan/gstack / deriveCodeSourceId

Function deriveCodeSourceId

bin/gstack-gbrain-sync.ts:343–361  ·  view source on GitHub ↗

* Derive a host- and worktree-aware source id for the cwd code corpus. * * Pattern: `gstack-code- - ` where slug comes from origin * (org/repo) and hostpathhash8 is the first 8 hex chars of * sha1(`${hostname}::${absolute repo path}`). Folding hostname into the hash * keeps C

(repoPath: string)

Source from the content-addressed store, hash-verified

341 * with a hashed-tail fallback when the combined slug exceeds budget.
342 */
343function deriveCodeSourceId(repoPath: string): string {
344 const host = process.env.GSTACK_HOSTNAME || hostname();
345 const hostPathHash = createHash("sha1").update(`${host}::${repoPath}`).digest("hex").slice(0, 8);
346 const remote = canonicalizeRemote(originUrl());
347 if (remote) {
348 const segs = remote.split("/").filter(Boolean);
349 const slugSource = segs.slice(-2).join("-");
350 const fullId = constrainSourceId("gstack-code", `${slugSource}-${hostPathHash}`);
351 // If the org+repo+hostpathhash fits cleanly (suffix preserved), use it.
352 if (fullId.endsWith(`-${hostPathHash}`)) return fullId;
353 // Otherwise drop the org prefix and retry with just repo+hostpathhash so
354 // the repo name stays readable. If that still doesn't fit,
355 // constrainSourceId falls back to a deterministic hash-only form.
356 const repoOnly = segs[segs.length - 1] || "repo";
357 return constrainSourceId("gstack-code", `${repoOnly}-${hostPathHash}`);
358 }
359 const base = repoPath.split("/").pop() || "repo";
360 return constrainSourceId("gstack-code", `${base}-${hostPathHash}`);
361}
362
363/**
364 * Pre-pathhash source id, kept for orphan detection only.

Callers 3

runCodeImportFunction · 0.85
runDreamFunction · 0.85
mainFunction · 0.85

Calls 3

canonicalizeRemoteFunction · 0.90
originUrlFunction · 0.85
constrainSourceIdFunction · 0.85

Tested by

no test coverage detected