I have the following git hook:
#!/usr/bin/env bash
# This hook works for branches named such as "123-description" and will add "[#123]" to the commit message.
# get current branch
branchName=`git rev-parse --abbrev-ref HEAD`
# search issue id in the branch name, such a "123-description" or "XXX-123-description"
issueId=$(echo $branchName | sed -nE 's,([A-Z]?-?[0-9]+)-.+,\1,p')
# only prepare commit message if pattern matched and issue id was found
if [[ ! -z $issueId ]]; then
# $1 is the name of the file containing the commit message
# sed -i.bak -e "1s/^/\n\n[$issueId]\n/" $1
echo -e "#$issueId ""$(cat $1)" > "$1"
# echo -e "[$issueId]\n""$(cat $1)" > "$1"
# sed -i.bak -e "1s/^/$TRIMMED /" $1
fi
It worked fine on Ubuntu but for some reason it doesn’t work on NixOS.
I tried to execute the individual commands and they are working just fine.
It doesn’t show me any error messages so I’m currently a bit lost how I should continue debugging this.
debug your hook, what does this give you (or what ever hook your using)
bash -x .git/hooks/pre-commit
It just hangs at the cat:
~/playground/foo 99-test $ bash -x ~/.config/git/hooks/prepare-commit-msg
++ git rev-parse --abbrev-ref HEAD
+ branchName=99-test
++ echo 99-test
++ sed -nE 's,([A-Z]?-?[0-9]+)-.+,\1,p'
+ issueId=99
+ [[ ! -z 99 ]]
++ cat
Okay when I provide a file name it seems to work:
~/playground/foo]$ bash -x ~/.config/git/hooks/prepare-commit-msg foo.txt
++ git rev-parse --abbrev-ref HEAD
+ branchName=99-test
++ echo 99-test
++ sed -nE 's,([A-Z]?-?[0-9]+)-.+,\1,p'
+ issueId=99
+ [[ ! -z 99 ]]
++ cat foo.txt
+ echo -e '#99 a line'
git commit
doesn’t get the line however:
NobbZ
December 26, 2021, 12:39pm
7
Dumb question… Did you remember to make it executable?
Found the problem by the way.
The reason why it worked on Ubuntu is because they have most likely a global git config where the core.hooksPath
is configured.
On NixOS this isn’t the case so you have to set it yourself: