0
votes

I am trying to figure out what is happening with the path to COMMIT_EDITMSG passed to our commit-msg git hook. In my environment, I am getting the full path passed in, but it seems like in some other environments, only a partial path is passed in and we need to add relative path qualifiers to be able to find the file. So I have ended up using the initial conditional block in the code sample below to find a path to COMMIT_EDITMSG that works (although this has not been tested yet).

Can anyone help explain what is happening here or why? Why are full paths only being provided in some environments. I initially thought it had to do with the fact that I am using multiple worktrees in my environment so a full path is necessary when committing in those worktrees, but I see now that even in my primary worktree I am getting a fully qualified path, but I don't think I was before because I had been using a version of this code that had "../../../" hard-wired into the path and it was working before.

In case it matters I am using VS Code for most of my commits, but I did a test from the command line and saw similar behavior, I think.

#!/bin/sh
#Adapted from https://stackguides.com/questions/26624368/handle-multiple-pre-commit-hooks
#Use this file to combine multiple commit-msg hooks by placing the other hook scripts in the commit-msg.d directory under your specified git hook location

cd "$(dirname "$0")/commit-msg.d"

if [ -r ${1} ] ; then
    message=$(echo "${1}")
else
    message=$(echo "../../../${1}")
fi

for hook in *; do
    bash $hook $message
    RESULT=$?
    if [ $RESULT != 0 ]; then
        echo "commit-msg.d/$hook returned non-zero: $RESULT, abort commit"
        exit $RESULT
    fi
done

exit 0

EDIT - upon further review I am finding that COMMIT_EDITMSG is going to my .git/worktrees/x/ directory instead of directly under .git/ directory now. What could have caused my primary worktree to start behaving like a secondary worktree and how can I reset that behavior?