Sublime Forum

[SOLVED] Pre-commit message hook work from terminal but not from SM

#1

Hello,

I’ve got this simple prepare-commit-msg script to add the name of the branch to my commit message:

#!/bin/bash
COMMIT_MSG_FILE=$1
branch=$(git rev-parse --symbolic --abbrev-ref HEAD)
sed -i "1 s@^@${branch}: @" $1;

If I do git commit or git commit -m "mycommitmessage" from the cli it works.
If I try to commit from SM it fails with this message :

Any clue about this?

0 Likes

#2

Hi @andrea.g,

Thanks for reaching out!

Could you confirm what platform you’re on (e.g. Windows, MacOS, Linux)?

Kind regards,
- Dylan

0 Likes

#3

Hi @djohnston,
sorry for skipping that:

Platform MacOS (Monterey)
Sublime Merge build 2074

0 Likes

#4

Hi @andrea.g,

Thanks for the additional info!

So this is being triggered by sed. On MacOS, sed works a little differently with the -i flag, and requires an extension to be provided for the backup file (when the rename takes place). See the following for a solution: https://stackoverflow.com/questions/4247068/sed-command-with-i-option-failing-on-mac-but-works-on-linux

Hope this helps.

Kind regards,
- Dylan

0 Likes

#5

Hi @djohnston, thank you for the reply.

Digging a little bit deeper seems a $PATH problem on my side, because my env is pointing to GNU sed.

I’ve updated the script, I put it there for future reference if someone has the same problem

#!/bin/bash
# Set $1 as $COMMIT_MSG_FILE
COMMIT_MSG_FILE=$1
# Get branch
branch=$(git rev-parse --symbolic --abbrev-ref HEAD)

# Set which sed we use depending on OS
# from https://stackoverflow.com/questions/3466166/how-to-check-if-running-in-cygwin-mac-or-linux
unameOut="$(uname -s)"
case "${unameOut}" in
    Linux*)     sed=/usr/bin/sed;;
    Darwin*)    sed=/usr/local/opt/gnu-sed/libexec/gnubin/sed;;
    *)          exit 1 ;;
esac

$sed -i "1 s@^@${branch}: @" $1;
0 Likes

#6

@andrea.g, it may be worth looking into this:

0 Likes