commit-msg挂钩中缺少提交消息文件

问题描述 投票:1回答:1

我正在尝试编写commit-msg钩子来格式化提交消息,以使其以最大列宽换行:

#!/bin/bash

format_max_column_width() {
  MAX_LINE_LENGTH_CHARS=50

  cat "$1" | grep -v "^Bug: |^Change-Id: |^Signed-off-by: |^CC: " > body
  cat "$1" | grep "^Bug: |^Change-Id: |^Signed-off-by: |^CC: " > footer
  fmt -w "$MAX_LINE_LENGTH_CHARS" body > body
  cat body > "$1"
  cat footer >> "$1"
  rm body footer
}

format_max_column_width

由于某种原因,当我提交时,由于$ 1似乎为空,所以出现以下错误。

cat: '': No such file or directory
cat: '': No such file or directory
.git/hooks/commit-msg: line 9: : No such file or directory
.git/hooks/commit-msg: line 10: : No such file or directory

此外,如果我仅输入echo $1,将不会打印任何内容,从而证实了这一理论。发生了什么事?

bash git githooks pre-commit-hook commit-message
1个回答
2
投票

在shell函数$1内部意味着” 函数]的第一个参数”,而不是脚本。您需要将第一个脚本参数进一步传递给函数:

format_max_column_width "$1"
© www.soinside.com 2019 - 2024. All rights reserved.