Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kentcdodds/lineage

src/backfill.ts

44 lines · 1.3 KB · TypeScript
import { collectBranchHistory, ingestMissingFromList } from './ingest.ts'
import { refreshIgnoreIndex } from './ignore-rules.ts'
import { getRepoSettings, ingestSummary } from './storage.ts'

/**
 * Ingest missing mainline commits into package storage, oldest gaps first.
 * Use this for the initial fill and whenever the graph is missing history.
 *
 * @param input - Optional batch size, page cap, and whether to refresh ignore rules
 * @returns How many commits were stored and whether more remain
 *
 * @example
 * import backfill from 'kody:@kentcdodds/lineage/backfill'
 *
 * const progress = await backfill({ limit: 50 })
 */
export default async function backfill(
	input: {
		limit?: number
		maxPages?: number
		refreshIgnore?: boolean
		deadlineMs?: number
	} = {},
) {
	if (input.refreshIgnore) {
		await refreshIgnoreIndex()
	}
	const repo = await getRepoSettings()
	const history = await collectBranchHistory(input.maxPages ?? 40)
	const chronological = [...history].reverse()
	const result = await ingestMissingFromList(chronological, {
		limit: input.limit ?? 80,
		deadlineMs: input.deadlineMs ?? 60_000,
	})
	const summary = await ingestSummary()
	return {
		ok: true,
		repo,
		listed: history.length,
		...result,
		done: result.remaining === 0 && result.errors.length === 0,
		ingest: summary,
	}
}