在预提交shell脚本中调用git diff

时间:2013-06-11 14:57:11

标签: git bash sh

我尝试将'git diff'的输出放入shell脚本中的变量中以检查文件是否已更改,但是当在脚本中运行'git diff file'时,我总是得到此输出:

usage: git diff [--no-index] <path> <path>

这是我在脚本中调用的内容

#!/bin/sh
cd /path/to/repo
jsdiff=`git diff file.js`
echo "jsdiff: $jsdiff"

任何想法?

#!/bin/sh
#
# this script is always executed before the commit is typed
#
#

# Before commiting minify and compress javascript if changed

cd /path/to/repo/_js
jsdiff=`git diff main.js`
echo "jsdiff: $jsdiff"

#if [ "$jsdiff" != "" ]
#   then
        # compile js
#       ./googleClosureCompile.sh
#       echo "js minified"
#fi

exit 0;

1 个答案:

答案 0 :(得分:2)

只有在不是回购的目录中运行git diff时才会出现此错误,因此git diff认为您要将其用作普通diff的替代,所以它想要两条路。

这里的问题是,钩子$GIT_DIR内部总是设置为.git所以不要试图发现这个目录git而只是查看.git。如果你{@ 1}} 甚至在你的仓库中 cd somewhere找不到这个git目录,它会认为它不在回购中。所以最简单的方法就是避免在钩子中使用.git

而不是

cd

只做

cd /path/to/repo/_js
jsdiff=`git diff main.js`

检查jsdiff=`git diff _js/main.js` 退出代码也是一件好事。以下是如何做到这一点:

git diff