0

I'm trying to write my first git hook for prepare-commit-msg , I've put this together which behaves exactly as I want it on my Fedora box:

#!/usr/bin/bash
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3
TRELLO_ID='XXX' # our trello project id
branchName=`git rev-parse --abbrev-ref HEAD`
IFS=/ read commitType issueNumber commitDesc <<< $(echo $branchName)
if [[ -z $issueNumber ]]; then
  exit 1
fi
firstLine=$(head -n1 $1)
if [ -z "$firstLine"  ] ;then #Check that this is not an amend by checking that the first line is empty
        sed -i'.bak' -e "1s/^/Issue $issueNumber: \n/" $COMMIT_MSG_FILE
        sed -i'.bak' -e "2a\
                Issue link: https://trello.com/c/$TRELLO_ID/$issueNumber \n" $COMMIT_MSG_FILE
fi

I've shared this with a team member who's running on a mac and this keeps failing. Extra info about the mac setup is that it is running zsh but I dont know if that is relevant. I'm not familiar with macs so I wasn't able to help other that echoing a lot in this file when running git commit. For the record I'm also not that experience with bash.

At first I thought it was an issue with sed compatibility as other questions I found here revolve around it but after further testing it seems this already fails when splitting the branch name (we're using the convention <type>/<ticket-number>/<desc> ). I've also noticed some importance in syntax using single and double quotes around the file.

One last curiosity, on his mac if we run $ which bash on his terminal we get /bin/bash, does that mean the shebang is also wrong? how do people keep scripts like this compatible between linux and mac with so many little details? I also considered rewriting this on python which could bring easier compatibility but I really would like to understand what's needed to make this work on both systems.

Thanks!

7
  • 1
    do you know which step fails? what does "fail" mean in this case?
    – Joe
    Commented Sep 3, 2020 at 21:15
  • 4
    I'd probably use #!/usr/bin/env bash across the two platforms, but I'm not sure if that's your issue
    – Joe
    Commented Sep 3, 2020 at 21:16
  • 2
    how do people keep scripts like this compatible between linux and mac with so many little details? I try to write mine in the common subset of plain /bin/sh. That means no <<< operators, no [[ tests, and the like, though.
    – torek
    Commented Sep 3, 2020 at 21:17
  • 1
    sed -i doesn't work in a consistent way between macOS and Linux. The argument must be part of the -i on Linux and a separate argument on macOS. Try using Perl or Ruby with their -i flag instead.
    – bk2204
    Commented Sep 3, 2020 at 23:25
  • 1
    @bk2204 mv -- foo foo.bak && sed '...' foo.bak >foo is a valid portable alternative to the erratic implementation of the -i option.
    – Léa Gris
    Commented Sep 4, 2020 at 0:23

2 Answers 2

2

Write you script like that with the sh shebang and check it with https://www.shellcheck.net/

If everything is ok with the sh shebang, it means the script will work ok with any version of bash, zsh, or whatever POSIX shell interpreter. MacOS's BSD type environment may have tools with different syntax or at least lacking the GNU CoreUtils specific features, so be cautious not to use these specific options when running commands.

#!/usr/bin/env sh

COMMIT_MSG_FILE="$1"
COMMIT_SOURCE="$2"
SHA1="$3"
TRELLO_ID='XXX' # our trello project id
branchName=$(git rev-parse --abbrev-ref HEAD)
IFS=/
# Split branchName at / into arguments
# shellcheck disable=SC2086 # intended word splitting
set -- $branchName
commitType="$1"
issueNumber="$2"
commitDesc="$3"
if [ -z "$issueNumber" ]; then
  exit 1
fi
firstLine=$(head -n1 "$COMMIT_MSG_FILE")
if [ -z "$firstLine"  ] ;then #Check that this is not an amend by checking that the first line is empty
        mv -- "$COMMIT_MSG_FILE" "$COMMIT_MSG_FILE.bak" &&
        sed -e "1s/^/Issue $issueNumber: \n/;2a\
                Issue link: https://trello.com/c/$TRELLO_ID/$issueNumber \n" "$COMMIT_MSG_FILE.bak" >"$COMMIT_MSG_FILE"
fi
1
  • thank you! that website is kinda what i was after, some way to submit/process a script that will warn or complain about non mutually compatible stuff. Also the snippet worked great, just changed 2a to 1a to match the format im going after.
    – keponk
    Commented Sep 4, 2020 at 22:10
0

I've faced pretty similar situation with Mac, and then realised we have people with windows also.

Not sure it's a relevant thing, but, if you have javascript application, then you may use https://github.com/typicode/husky to make hooks a bit different way. In addition to platforms support, such hooks may be committed into repository, which eliminates need of manual setup by newcomers.

Probably there are other similar solutions for solutions implemented in other languages.

Not the answer you're looking for? Browse other questions tagged or ask your own question.