Question

How could we write a commit-msg hook, for appending a string [XYZ] to all commits, except when the original commit message has [exclude-XYZ] in it somewhere?

I am a beginner with shell scripting and here is what I have tried unsuccessfully so far:

if echo "$1" | grep "[exclude-XYZ]"
then 
else echo "$1 [XYZ]" >> "$1"
fi
Was it helpful?

Solution

You're echoing the filename, rather than checking its contents. Try something like:

if fgrep '[exclude-XYZ]' -- "$1" >/dev/null; then 
  :
else
  echo "[XYZ]" >> "$1"
fi

Or, as a one-liner:

fgrep '[exclude-XYZ]' -- "$1" >/dev/null || echo '[XYZ]' >> "$1"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top