Reputation: 2423
I try to put the output of 'git diff' into a variable in shell script to check if file has changed, but whenn running 'git diff file' in script I always get this output:
usage: git diff [--no-index] <path> <path>
here is what i am calling in the script
#!/bin/sh
cd /path/to/repo
jsdiff=`git diff file.js`
echo "jsdiff: $jsdiff"
any ideas?
#!/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;
Upvotes: 2
Views: 6402
Reputation: 13616
You can get this error only if you run git diff
in a directory which is not a repo, thus git diff
thinks that you want to use it as a replacement for ordinary diff
, so it wants two paths.
The problem here is that inside a hook $GIT_DIR
is always set to .git
so instead of trying to discover this directory git
simply looks at .git
. If you do cd somewhere
even inside your repo git
won't find this .git
directory and it will think that it is not in a repo. So the easiest you can do is just avoid using cd
in hooks.
Instead of
cd /path/to/repo/_js
jsdiff=`git diff main.js`
do just
jsdiff=`git diff _js/main.js`
Also it's better to check git diff
exit code. Here is how to do it:
#!/bin/sh
git diff --quiet _js/main.js || {
# compile js
# ...
}
Upvotes: 2