Git Post Commit 挂钩将更改的文件转换为变量

问题描述

这里是 Git Hook 新手

我有一个 git post-commit 钩子,我正在修改文件。我想在一个变量中获取文件路径+文件名(减去扩展名)。然后将此变量传递给转换器

#!/bin/bash
#.git/hooks/post-commit
# An example hook script that is called after a successful
# commit is made.
#
# To enable this hook,rename this file to "post-commit".

echo "post-commit started"
IFS=$'\n'
git diff --name-only HEAD^ HEAD | grep '\.json$' |
while read file; do
   echo "$file"
   A="$file" | cut -d'.' -f 1
   #Don't get anything in A
   echo "$A"
   B="$A" +".html"
   #Don't get anything in B
   echo "$B"
   ## Python to convert goes here
   ## generate-schema-doc $A $B
 done
 echo "post-commit finished"

解决方法

这不是关于 git hooks,而是关于 bash 语言。 首先这一行:

A="$file" | cut -d'.' -f 1

$file 可能不是 shell 命令,即使是,您也应该指示 shell 您要实际执行它。

A=`echo "$file" | cut -d "." -f 1`   

您在加入 $A.html 时遇到另一个错误。您可以通过以下方式简化:

B="$A.html"