Skip to content
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

pre-merge-commit hook not triggering #1494

Closed
RainMeoCat opened this issue Aug 14, 2024 · 8 comments · Fixed by #1497 · 4 remaining pull requests
Closed

pre-merge-commit hook not triggering #1494

RainMeoCat opened this issue Aug 14, 2024 · 8 comments · Fixed by #1497 · 4 remaining pull requests

Comments

@RainMeoCat
Copy link
Contributor

I've set up a pre-merge-commit hook using Husky, but it doesn't seem to be triggering when I perform a merge. Here are the details:

Steps to Reproduce:

  1. Add a pre-merge-commit hook with the following command:
echo "echo 'pre-merge-commit hook triggered'" > .husky/pre-merge-commit
  1. Merge a branch that should trigger the pre-merge-commit hook.
  2. Notice that the hook does not run.

Expected Behavior:

The pre-merge-commit hook should trigger and display the message pre-merge-commit hook triggered before the merge is completed.

Actual Behavior:

The pre-merge-commit hook is not triggered at all, and the merge process continues without any output from the hook.

Environment:

Husky Version: ^9.1.4
Node Version: 20.8.0
Git Version: 2.46.0
Operating System: MacOS Sonoma 14.0

Additional Context:

Workaround

There is a workaround to fix this issue by manually adding a pre-merge-commit file in the ./husky/_ directory with the following content:

#!/usr/bin/env sh
. "$(dirname "$0")/h"

After ensuring the file is executable, the issue is resolved. However, when the _ directory is deleted and Husky is re-initialized using the npm run prepare command, the pre-merge-commit file is not generated. This defeats the purpose of using Husky, as the hook needs to be manually recreated each time.

@typicode
Copy link
Owner

Hi,

Try running with HUSKY=2 to get more info.

@RainMeoCat
Copy link
Contributor Author

Hi,

Try running with HUSKY=2 to get more info.

Hi, below is the output result of my use of HUSKY=2 git merge --no-ff.

+++ basename .husky/_/prepare-commit-msg
++ n=prepare-commit-msg
++++ dirname .husky/_/prepare-commit-msg
+++ dirname .husky/_
++ s=.husky/prepare-commit-msg
++ '[' '!' -f .husky/prepare-commit-msg ']'
++ exit 0
+++ basename .husky/_/commit-msg
++ n=commit-msg
++++ dirname .husky/_/commit-msg
+++ dirname .husky/_
++ s=.husky/commit-msg
++ '[' '!' -f .husky/commit-msg ']'
++ '[' -f /Users/lemiochen/.huskyrc ']'
++ i=/Users/lemiochen/.config/husky/init.sh
++ '[' -f /Users/lemiochen/.config/husky/init.sh ']'
++ . /Users/lemiochen/.config/husky/init.sh
+++ export HUSKY=2
+++ HUSKY=2
++ '[' 2 = 0 ']'
++ export PATH=node_modules/.bin:/opt/homebrew/opt/git/libexec/git-core:/Library/Frameworks/Python.framework/Versions/3.12/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin
++ PATH=node_modules/.bin:/opt/homebrew/opt/git/libexec/git-core:/Library/Frameworks/Python.framework/Versions/3.12/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin
++ sh -e .husky/commit-msg .git/MERGE_MSG
Checking commit message...
-----------------------------------
Merge branch 'features/portfolio' into qa
-----------------------------------
Check if the commit message footer contains 'task:'..........skipped
Check if the commit message title start with types..........skipped
++ c=0
++ '[' 0 '!=' 0 ']'
++ '[' 0 = 127 ']'
++ exit 0
Merge made by the 'ort' strategy.
 src/components/tm-monitor/ResultStatusIndicator.vue | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
+++ basename .husky/_/post-merge
++ n=post-merge
++++ dirname .husky/_/post-merge
+++ dirname .husky/_
++ s=.husky/post-merge
++ '[' '!' -f .husky/post-merge ']'
++ exit 0

@typicode
Copy link
Owner

Thanks for the logs.

Here it fails to find .husky/prepare-commit-msg and exits

++ '[' '!' -f .husky/prepare-commit-msg ']'
++ exit 0

But here, it finds .husky/commit-msg and continues the execution of the script

++ '[' '!' -f .husky/commit-msg ']'
++ '[' -f /Users/lemiochen/.huskyrc ']'

Check that there's no typo in your prepare-commit-msg file name.

@RainMeoCat
Copy link
Contributor Author

RainMeoCat commented Aug 15, 2024

Thanks for the logs. 

Here it fails to find .husky/prepare-commit-msg and exits

++ '[' '!' -f .husky/prepare-commit-msg ']'
++ exit 0

But here, it finds .husky/commit-msg and continues the execution of the script

++ '[' '!' -f .husky/commit-msg ']'
++ '[' -f /Users/lemiochen/.huskyrc ']'

Check that there's no typo in your prepare-commit-msg file name.

Thank you for your reply.

However, I do not use the prepare-commit-msg hook file.

Even after creating this file, the pre-merge-commit hook is still not being invoked.

Below is the new log:

+++ basename .husky/_/prepare-commit-msg
++ n=prepare-commit-msg
++++ dirname .husky/_/prepare-commit-msg
+++ dirname .husky/_
++ s=.husky/prepare-commit-msg
++ '[' '!' -f .husky/prepare-commit-msg ']'
++ '[' -f /Users/lemiochen/.huskyrc ']'
++ i=/Users/lemiochen/.config/husky/init.sh
++ '[' -f /Users/lemiochen/.config/husky/init.sh ']'
++ . /Users/lemiochen/.config/husky/init.sh
+++ export HUSKY=2
+++ HUSKY=2
++ '[' 2 = 0 ']'
++ export PATH=node_modules/.bin:/opt/homebrew/opt/git/libexec/git-core:/Library/Frameworks/Python.framework/Versions/3.12/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin
++ PATH=node_modules/.bin:/opt/homebrew/opt/git/libexec/git-core:/Library/Frameworks/Python.framework/Versions/3.12/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin
++ sh -e .husky/prepare-commit-msg .git/MERGE_MSG merge
nothing to do in this hook
++ c=0
++ '[' 0 '!=' 0 ']'
++ '[' 0 = 127 ']'
++ exit 0
+++ basename .husky/_/commit-msg
++ n=commit-msg
++++ dirname .husky/_/commit-msg
+++ dirname .husky/_
++ s=.husky/commit-msg
++ '[' '!' -f .husky/commit-msg ']'
++ '[' -f /Users/lemiochen/.huskyrc ']'
++ i=/Users/lemiochen/.config/husky/init.sh
++ '[' -f /Users/lemiochen/.config/husky/init.sh ']'
++ . /Users/lemiochen/.config/husky/init.sh
+++ export HUSKY=2
+++ HUSKY=2
++ '[' 2 = 0 ']'
++ export PATH=node_modules/.bin:/opt/homebrew/opt/git/libexec/git-core:/Library/Frameworks/Python.framework/Versions/3.12/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin
++ PATH=node_modules/.bin:/opt/homebrew/opt/git/libexec/git-core:/Library/Frameworks/Python.framework/Versions/3.12/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin
++ sh -e .husky/commit-msg .git/MERGE_MSG
Checking commit message...
-----------------------------------
Merge branch 'test' into qa
-----------------------------------
Check if the commit message footer contains 'task:'..........skipped
Check if the commit message title start with types..........skipped
++ c=0
++ '[' 0 '!=' 0 ']'
++ '[' 0 = 127 ']'
++ exit 0
Merge made by the 'ort' strategy.
 .husky/prepare-commit-msg | 3 +++
 1 file changed, 3 insertions(+)
 create mode 100644 .husky/prepare-commit-msg
+++ basename .husky/_/post-merge
++ n=post-merge
++++ dirname .husky/_/post-merge
+++ dirname .husky/_
++ s=.husky/post-merge
++ '[' '!' -f .husky/post-merge ']'
++ exit 0

@typicode
Copy link
Owner

typicode commented Aug 15, 2024

My bad I misread. Can you try this:

cp .husky/_/pre-commit .husky/_/pre-merge-commit
# ensure that you have .husky/pre-merge-commit as well with your code

And retry your merge. Please let me know if it works.

@RainMeoCat
Copy link
Contributor Author

RainMeoCat commented Aug 16, 2024

Hello, thank you for your reply. The pre-merge-commit hook is now being triggered properly. It seems that the issue was due to Husky's initialization process not creating the corresponding file (pre-merge-commit) in the _ directory.

Here is a more comprehensive workaround until a permanent fix is implemented:

  1. Create createHook.js:
const fs = require('fs');
const path = require('path');

// Specify the directory and file name you want to create
const directoryPath = path.join(__dirname, './.husky/_');
const filePath = path.join(directoryPath, 'pre-merge-commit');

// Check if the file already exists, if it does, do not create it
if (fs.existsSync(filePath)) {
    console.log(`pre-merge-commit git hook file already exists at ${filePath}`);
    return;
}

// Ensure the directory exists, if not, create it
if (!fs.existsSync(directoryPath)) {
    fs.mkdirSync(directoryPath, { recursive: true });
}

const fileContent = `#!/usr/bin/env sh
. "$(dirname "$0")/h"`;
// Create the file and write the content (if the file does not exist)
fs.writeFileSync(filePath, fileContent, { flag: 'w' });
// Set the file permissions to -rwxr-xr-x
fs.chmodSync(filePath, 0o755);

console.log(`pre-merge-commit git hook file has been created at ${filePath}`);
  1. Adjust the package.json script:
"scripts": {
    "prepare": "husky"
},

Change it to:

"scripts": {
    "prepare": "husky && node createHook.js"
},
  1. Run npm install again, and you should see that the .husky/_ directory now contains the file needed for the pre-merge-commit hook to function correctly.

@typicode
Copy link
Owner

Do you want to make the fix? Just add the missing hook after pre-commit there https://github.com/typicode/husky/blob/main/index.js#L5

RainMeoCat added a commit to RainMeoCat/husky that referenced this issue Aug 20, 2024
support pre-merge-commit hook

fixes: typicode#1494
@RainMeoCat
Copy link
Contributor Author

Do you want to make the fix? Just add the missing hook after pre-commit there https://github.com/typicode/husky/blob/main/index.js#L5

Sure, I've already made the fix and submitted a pull request. You can check and review it here: #1497.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment