From 3e3d0b5a9d4582e175212151473c8c1d5b81d5bd Mon Sep 17 00:00:00 2001 From: Ade Attwood Date: Tue, 17 Sep 2024 08:29:42 +0100 Subject: [PATCH] feat: sapling scm support Now you can run this inside a sapling repo. It will automatically pick what source control you are using by checking for the root repo. --- src/index.ts | 42 ++++++++++++++++++++++++++++++++++++++---- src/report.ts | 2 +- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 035f69a..d5dd175 100644 --- a/src/index.ts +++ b/src/index.ts @@ -48,6 +48,40 @@ export const validate = async (argv: Argv) => { } }; +async function isGitRepo() { + const isGit = await exec(`git rev-parse --is-inside-work-tree`); + return isGit.code === 0; +} + +async function isSaplingRepo() { + const isSapling = await exec(`sl root`); + return isSapling.code === 0; +} + +async function getDiff(): Promise { + if (await isGitRepo()) { + const diffText = await exec(`git diff origin/HEAD...HEAD`); + if (diffText.code > 0) { + error("Error loading the diff\n\n" + diffText.stderr); + return undefined; + } + + return diffText.stdout; + } + + if (await isSaplingRepo()) { + const diffText = await exec(`sl diff -g -r '. % public()'`); + if (diffText.code > 0) { + error("Error loading the diff\n\n" + diffText.stderr); + return undefined; + } + + return diffText.stdout; + } + + error("Unable to get a diff no repo was found\n"); +} + export const run = async (argv = process.argv) => { const parsed: Argv = await yargs(hideBin(argv)).options(options).argv; const validationError = await validate(parsed); @@ -62,12 +96,12 @@ export const run = async (argv = process.argv) => { return lcovDiff(baseCoverage, compareCoverage); } - const diffText = await exec(`git diff origin/HEAD...HEAD`); - if (diffText.code > 0) { - return error("Error loading the diff\n\n" + diffText.stderr); + const diffText = await getDiff(); + if (!diffText) { + return; } - const diff = parseDiff.default(diffText.stdout); + const diff = parseDiff.default(diffText); const { percentage } = report(diff, baseCoverage); process.exit(percentage > 90 ? 0 : 1); diff --git a/src/report.ts b/src/report.ts index 77fdf81..5ee8426 100644 --- a/src/report.ts +++ b/src/report.ts @@ -1,7 +1,7 @@ const getCoverageForFile = (file: any, coverage: any) => { for (const cov of coverage) { const report: { [k: number]: number } = {}; - if (cov.file === file.to) { + if (cov.file.replace("./", "") === file.to) { for (const detail of cov.lines.details) { report[detail.line] = detail.hit; }