Skip to content

Commit

Permalink
feat: skip pre-commit if nothing is in the staging area (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgiolaga authored Oct 26, 2024
1 parent d8bebb7 commit bb607d6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions install.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ if (os.platform() === 'win32') {
}

const precommitContent = '#!/usr/bin/env bash' + os.EOL +
'if git diff --cached --quiet; then' + os.EOL +
' echo "No staged changes detected, skipping pre-commit hook."' + os.EOL +
' exit 0' + os.EOL +
'fi' + os.EOL +
hookRelativeUnixPath + os.EOL +
'RESULT=$?' + os.EOL +
'[ $RESULT -ne 0 ] && exit 1' + os.EOL +
Expand Down
23 changes: 23 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,27 @@ test('pre-commit', async (t) => {
hook.run()
})
})

await t.test('skips pre-commit if no staged changes', (t) => {
const Hook = proxyquire('.', {
'node:child_process': {
execSync (command) {
if (command === 'git diff --cached --quiet') {
throw new Error('No staged changes')
}
}
}
})

const hook = new Hook(
function (code, lines) {
t.assert.strictEqual(code, 0, 'exit code should be 0')
t.assert.strictEqual(lines, undefined, 'lines should be undefined')
},
{ ignorestatus: true }
)

hook.config.run = ['example-pass']
hook.run()
})
})

0 comments on commit bb607d6

Please sign in to comment.