【问题标题】:No such file or directory error on commit hook提交挂钩上没有此类文件或目录错误
【发布时间】:2018-10-06 14:38:40
【问题描述】:

这是我的pre-commit 钩子

#!/bin/sh

echo "pre-commit started"

filename="$1"
lineno=0

error() {
    echo "$1"
    exit 1
}

while read -r line
do
    [[ "$line" =~ ^#.* ]] && continue

    let lineno+=1
    length=${#line}

    if [[ $lineno -eq 1 ]]; then
        [[ $length -gt 50 ]] && error "Limit the subject line to 50 characters"
        [[ ! "$line" =~ ^[A-Z].*$ ]] && error "Capitalise the subject line"
        [[ "$line" == *. ]] && error "Do not end the subject line with a period"
    fi

    [[ $lineno -eq 2 ]] && [[ -n $line ]] && error "Separate subject from body with a blank line"
    [[ $lineno -gt 1 ]] && [[ $length -gt 72 ]] && error "Wrap the body at 72 characters"
done < "$filename"
exit 0

我在运行时遇到此错误

› git commit -m "sfrewr"
pre-commit started
/Users/me/.git-templates/hooks/pre-commit: line 28: : No such file or directory
[master 6950a43] sfrewr
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 89

28 的行是

done < "$filename"

【问题讨论】:

    标签: git githooks


    【解决方案1】:

    Git 的 pre-commit 钩子不接受任何参数,但您的 shell 脚本需要一个。尝试从空文件名读取时,您会收到您看到的错误。

    由于您似乎正在尝试对提交消息进行健全性检查(一个值得称赞的目标),您可能希望改用 commit-msg 挂钩。如果您想这样做,它既可以拒绝您的消息,也可以对其进行编辑。您可以通过运行man githooks 了解更多关于哪些钩子的作用。

    【讨论】:

    • 它有效,但我仍然收到line 28: : No such file or directory 消息
    • 您是否删除了旧的pre-commit 挂钩?只要存在旧挂钩,您就会收到该错误。
    • 我可以建议使用更结构化的方式来管理钩子吗?看看pypi.org/project/hooks4git
    【解决方案2】:

    我遇到了同样的错误。然后我意识到我在项目文件夹的名称中使用了一个非英文字符。 :/

    【讨论】:

      猜你喜欢
      • 2017-10-11
      • 2016-02-26
      • 1970-01-01
      • 2021-12-20
      • 2019-05-03
      • 1970-01-01
      • 1970-01-01
      • 2020-11-28
      相关资源
      最近更新 更多