import { realpath } from "node:fs/promises"; import path from "node:path"; import { errorMessage, GIT_DISCOVERY_COMMAND_TIMEOUT_MS, runGit, } from "./git.js"; import { type PonyProject } from "./project.js"; export interface UpstreamTaskAllocationView { ids: string[]; warning: string | null; } function localOnlyWarning(reason: string): string { return `Task ID allocation is local-only because ${reason}. Creation will continue, but the ID may collide in another clone. Before integration, fetch and review IDs; use \`pony task renumber\` or \`pony task shift\` when available.`; } /** * Refresh only the current branch's configured tracking ref. Allocation remains * deliberately best-effort: repository discovery, upstream configuration, and * transport failures warn instead of blocking local task creation. */ export async function fetchUpstreamTaskIds( project: PonyProject, ): Promise { let repositoryRoot: string; try { repositoryRoot = await realpath( ( await runGit(project.root, ["rev-parse", "--show-toplevel"]) ).stdout.trim(), ); } catch { return { ids: [], warning: localOnlyWarning( "the Pony project is not inside a Git repository", ), }; } let branchRef: string; try { branchRef = ( await runGit(repositoryRoot, ["symbolic-ref", "--quiet", "HEAD"]) ).stdout.trim(); } catch { return { ids: [], warning: localOnlyWarning("Git HEAD is detached or has no named branch"), }; } let upstream: string; try { upstream = ( await runGit(repositoryRoot, [ "for-each-ref", "--format=%(upstream:remotename)%00%(upstream:remoteref)%00%(upstream)", branchRef, ]) ).stdout.trimEnd(); } catch (error) { return { ids: [], warning: localOnlyWarning( `Git upstream discovery failed: ${errorMessage(error)}`, ), }; } const [remoteName, remoteRef, trackingRef] = upstream.split("\0"); if (!remoteName || !remoteRef || !trackingRef) { return { ids: [], warning: localOnlyWarning( "the current branch has no configured upstream", ), }; } try { await runGit( repositoryRoot, ["fetch", "--no-tags", remoteName, `+${remoteRef}:${trackingRef}`], { timeoutMs: GIT_DISCOVERY_COMMAND_TIMEOUT_MS, }, ); } catch (error) { return { ids: [], warning: localOnlyWarning( `fetching ${remoteName} failed: ${errorMessage(error)}`, ), }; } const canonicalProjectRoot = await realpath(project.root); const tasksPath = path .relative(repositoryRoot, path.join(canonicalProjectRoot, ".pony", "tasks")) .split(path.sep) .join(path.posix.sep); if ( !tasksPath || tasksPath === ".." || tasksPath.startsWith(`..${path.posix.sep}`) ) { return { ids: [], warning: localOnlyWarning( "the Pony project is outside the discovered Git repository", ), }; } try { const output = ( await runGit(repositoryRoot, [ "ls-tree", "-r", "--name-only", trackingRef, "--", tasksPath, ]) ).stdout; const ids = output .split("\n") .map( (file) => path.posix.basename(file).match(/^(TASK-\d{3,})\.md$/)?.[1], ) .filter((id): id is string => id !== undefined); return { ids, warning: null }; } catch (error) { return { ids: [], warning: localOnlyWarning( `reading refreshed upstream tasks failed: ${errorMessage(error)}`, ), }; } }