I cannot commit to a certain OSS repo because the dumb
-type terminal of my Emacs (GUI version) caused an error on githook script.
TL;DR
I enabled the color function in Emacs terminal and managed to make commit on the repo.
Problem Faced
- Fixing bug on a certain OSS
- The OSS repository standardized commit messages by running commitlint on Githook managed by husky
- Cannot commit from Emacs (GUI one) because magit (v.20240920.1135) fails with ‘husky - commit-msg hook exited with code 1 (error)’
- The error did not occur on macOS terminal
Investigate what occurs
Below is the script husky runs on every commit:
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
cd frontend && yarn run commitlint --edit $1
branch="$(git rev-parse --abbrev-ref HEAD)"
color_red="$(tput setaf 1)"
bold="$(tput bold)"
reset="$(tput sgr0)"
if [ "$branch" = "main" ]; then
echo "${color_red}${bold}You can't commit directly to the main branch${reset}"
exit 1
fi
if [ "$branch" = "develop" ]; then
echo "${color_red}${bold}You can't commit directly to the develop branch${reset}"
exit 1
fi
I added several echo
s because I did not have any idea what was happening:
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
echo "1----------------------"
cd frontend && yarn run commitlint --edit $1
echo "2----------------------"
branch="$(git rev-parse --abbrev-ref HEAD)"
echo "3----------------------"
color_red="$(tput setaf 1)"
echo "4----------------------"
bold="$(tput bold)"
echo "5----------------------"
reset="$(tput sgr0)"
echo "6----------------------"
if [ "$branch" = "main" ]; then
echo "${color_red}${bold}You can't commit directly to the main branch${reset}"
exit 1
fi
if [ "$branch" = "develop" ]; then
echo "${color_red}${bold}You can't commit directly to the develop branch${reset}"
exit 1
fi
Then I got the following output:
hint: Waiting for your editor to close the file...
Waiting for Emacs...
1----------------------
yarn run v1.22.19
$ commitlint --edit $1 --edit .git/COMMIT_EDITMSG
✨ Done in 1.53s.
2----------------------
3----------------------
husky - commit-msg hook exited with code 1 (error)
Thus the problem seems to occur on
color_red="$(tput setaf 1)"
The command sets ANSI color to terminal foreground (cf: https://www.ibm.com/docs/en/aix/7.2?topic=t-tput-command).
To investigate the problem relies around tput
command, I updated hook script as below:
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
echo "1----------------------"
cd frontend && yarn run commitlint --edit $1
echo "2----------------------"
branch="$(git rev-parse --abbrev-ref HEAD)"
echo "3----------------------"
echo "3.5----------------------"
which tput
tput setaf 1
color_red="$(tput setaf 1)"
echo "4----------------------"
bold="$(tput bold)"
echo "5----------------------"
reset="$(tput sgr0)"
echo "6----------------------"
if [ "$branch" = "main" ]; then
echo "${color_red}${bold}You can't commit directly to the main branch${reset}"
exit 1
fi
if [ "$branch" = "develop" ]; then
echo "${color_red}${bold}You can't commit directly to the develop branch${reset}"
exit 1
fi
I confirmed that tput setaf 1
exits successfully on macOS terminal.
I got output ‘3.5—–’ but still did not get ‘4——’
hint: Waiting for your editor to close the file...
Waiting for Emacs...
1----------------------
yarn run v1.22.19
$ commitlint --edit $1 --edit .git/COMMIT_EDITMSG
✨ Done in 1.50s.
2----------------------
3----------------------
3.5----------------------
/usr/bin/tput
husky - commit-msg hook exited with code 1 (error)
The result shows that Emacs can use tput
command but fails to set the color to its terminal.
Fixit
I added echo $TERM
in the script and found out the terminal env in my Emacs is set to dumb
.
So I switched the Emacs terminal to the one with the color function by adding following line to my init.el
:
(setenv "TERM" "xterm-256color")
And finally managed to make commit on the repo!! 🎉