题
我想使用git挂钩强制提交消息以符合特定格式(它们应该以#number结尾)。我尝试了安装 这个钩, ,也引用 这里, ,但我一直收到消息:
$ 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)'
我正在按照示例中的出现的方式完全使用脚本,并且都会给出相同的错误。例如,我尝试过:
#!/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
怎么了?
解决方案
(a)您可能在Windows上执行此操作,并且(b)您意外地将Windows CRLF线路末尾引入了脚本。 Shell脚本不喜欢这些,并且可能导致您观察到的问题。
要将VIM设置为编写Unix风格的LF线结尾,请使用
:set ff=unix
:w
不隶属于 StackOverflow