From 7b499d76382e8f548b7001e814719ac6d49a3549 Mon Sep 17 00:00:00 2001 From: Olivers Vitins Date: Fri, 8 Mar 2024 23:03:11 +0200 Subject: [PATCH] fix(commands): commit crash upon missing HEAD (#19) --- src/commands/commit.ts | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/src/commands/commit.ts b/src/commands/commit.ts index d5b5714..4e298ac 100644 --- a/src/commands/commit.ts +++ b/src/commands/commit.ts @@ -69,28 +69,32 @@ const command = new Command("commit") process.exit(0); } - const changedLines = ( - ( - await simpleGit().diff(["--numstat", stageAll ? "HEAD" : "--cached"]) - ).match(/\d+/gm) || [] - ).reduce((partialSum, num) => partialSum + Number(num), 0); + try { + const changedLines = ( + ( + await simpleGit().diff(["--numstat", stageAll ? "HEAD" : "--cached"]) + ).match(/\d+/gm) || [] + ).reduce((partialSum, num) => partialSum + Number(num), 0); - if (changedLines > 250 && !sudo) { - const proceedCommitting = await confirm({ - message: - "You are about to commit changes to more than 250 lines, are you sure you want to proceed?", - initialValue: false, - }); + if (changedLines > 250 && !sudo) { + const proceedCommitting = await confirm({ + message: + "You are about to commit changes to more than 250 lines, are you sure you want to proceed?", + initialValue: false, + }); - if (isCancel(proceedCommitting)) { - cancel("Commit creation cancelled"); - process.exit(0); - } - - if (!proceedCommitting) { - cancel("Cancelled, please split this commit into smaller ones"); - process.exit(0); + if (isCancel(proceedCommitting)) { + cancel("Commit creation cancelled"); + process.exit(0); + } + + if (!proceedCommitting) { + cancel("Cancelled, please split this commit into smaller ones"); + process.exit(0); + } } + } catch { + note("HEAD hasn't been found, skipping commit line amount check"); } const type: string | symbol = await select({