Skip to content

feat: only remove some hooks that are not in preserveUnused option #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/giant-shirts-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"simple-git-hooks": minor
---

feat: only remove some hooks that are not in `preserveUnused` option
20 changes: 16 additions & 4 deletions simple-git-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const fs = require('fs')
const path = require('path')
const url = require('url')

const CONFIG_ERROR = '[ERROR] Config was not found! Please add `.simple-git-hooks.js` or `simple-git-hooks.js` or `.simple-git-hooks.json` or `simple-git-hooks.json` or `simple-git-hooks` entry in package.json.\r\nCheck README for details'

const VALID_GIT_HOOKS = [
'applypatch-msg',
'pre-applypatch',
Expand Down Expand Up @@ -164,7 +166,7 @@ async function setHooksFromConfig(projectRootPath=process.cwd(), argv=process.ar
const config = await _getConfig(projectRootPath, customConfigPath)

if (!config) {
throw('[ERROR] Config was not found! Please add `.simple-git-hooks.cjs` or `.simple-git-hooks.js` or `.simple-git-hooks.mjs` or `simple-git-hooks.cjs` or `simple-git-hooks.js` or `simple-git-hooks.mjs` or `.simple-git-hooks.json` or `simple-git-hooks.json` or `simple-git-hooks` entry in package.json.\r\nCheck README for details')
throw(CONFIG_ERROR)
}

const preserveUnused = Array.isArray(config.preserveUnused) ? config.preserveUnused : config.preserveUnused ? VALID_GIT_HOOKS: []
Expand Down Expand Up @@ -212,9 +214,19 @@ function _setHook(hook, command, projectRoot=process.cwd()) {
* Deletes all git hooks
* @param {string} projectRoot
*/
function removeHooks(projectRoot=process.cwd()) {
for (let configEntry of VALID_GIT_HOOKS) {
_removeHook(configEntry, projectRoot)
async function removeHooks(projectRoot = process.cwd()) {
const customConfigPath = _getCustomConfigPath(process.argv)
const config = await _getConfig(projectRoot, customConfigPath)

if (!config) {
throw (CONFIG_ERROR)
}

const preserveUnused = Array.isArray(config.preserveUnused) ? config.preserveUnused : []
for (const configEntry of VALID_GIT_HOOKS) {
if(!preserveUnused.includes(configEntry)) {
_removeHook(configEntry, projectRoot)
}
}
}

Expand Down
33 changes: 32 additions & 1 deletion simple-git-hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ describe("Simple Git Hooks tests", () => {
);
expect(isEqual(installedHooks, { "pre-commit": TEST_SCRIPT })).toBe(true);

simpleGitHooks.removeHooks(PROJECT_WITH_CONF_IN_PACKAGE_JSON);
await simpleGitHooks.removeHooks(PROJECT_WITH_CONF_IN_PACKAGE_JSON);

installedHooks = getInstalledGitHooks(
path.normalize(
Expand Down Expand Up @@ -458,6 +458,37 @@ describe("Simple Git Hooks tests", () => {
})
).toBe(true);
});

it("creates git hooks and removes hooks which are not in preserveUnused", async () => {
createGitHooksFolder(PROJECT_WITH_UNUSED_CONF_IN_PACKAGE_JSON);

const installedHooksDir = path.normalize(
path.join(
PROJECT_WITH_UNUSED_CONF_IN_PACKAGE_JSON,
".git",
"hooks"
)
);

fs.writeFileSync(
path.resolve(installedHooksDir, "commit-msg"),
"# do nothing"
);

let installedHooks = getInstalledGitHooks(installedHooksDir);

expect(isEqual(installedHooks, { "commit-msg": "# do nothing" })).toBe(true);

await simpleGitHooks.setHooksFromConfig(PROJECT_WITH_UNUSED_CONF_IN_PACKAGE_JSON);

installedHooks = getInstalledGitHooks(installedHooksDir);
expect(isEqual(installedHooks, { "pre-commit": TEST_SCRIPT, "commit-msg": "# do nothing" })).toBe(true);

await simpleGitHooks.removeHooks(PROJECT_WITH_UNUSED_CONF_IN_PACKAGE_JSON);

installedHooks = getInstalledGitHooks(installedHooksDir);
expect(isEqual(installedHooks, { "commit-msg": "# do nothing" })).toBe(true);
});
});

describe("CLI tests", () => {
Expand Down
4 changes: 2 additions & 2 deletions uninstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ const {removeHooks} = require("./simple-git-hooks");
/**
* Removes the pre-commit from command in config by default
*/
function uninstall() {
async function uninstall() {
console.log("[INFO] Removing git hooks from .git/hooks")

try {
removeHooks()
await removeHooks()
console.log("[INFO] Successfully removed all git hooks")
} catch (e) {
console.log("[INFO] Couldn't remove git hooks. Reason: " + e)
Expand Down
Loading