Question

I want to use a git hook to force commit messages to comply with a specific format (they should end with #number). I've tried installing this hook, also referenced here, but I keep getting the message:

$ git commit -am "Test"
.git/hooks/commit-msg: line 1: sage_file: command not found
.git/hooks/commit-msg: line 2: syntax error near unexpected token `('
.git/hooks/commit-msg: line 2: `message = File.read(message_file)'

I'm using the scripts exactly as they appear in the examples, and both give the same error. I have tried for instance:

#!/usr/bin/env ruby
message_file = ARGV[0]
message = File.read(message_file)

#starts with # then number, space, and at least 5 words no more than 200
$regex = /(^#[0-9]+ \W*(\w+(\W+|$)){5,200})/

if !$regex.match(message)
puts "Your message is not formatted correctly (example: #XXX at least 5 words)"
exit 1
end

What could be wrong?

Was it helpful?

Solution

It is possible that (a) you're doing this on Windows, and (b) you've accidentally introduced Windows CRLF line endings into your script. Shell scripts don't like those, and can cause the sort of problem you observe.

To set Vim to write Unix-style LF line endings, use

:set ff=unix
:w
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top