2

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;
7
  • Are you sure that /path/to/file/file.js exists? If you replace git diff with ls -l, does it work? Commented Jun 11, 2013 at 15:05
  • it does, that is not the problem, maybe the problem refers to calling an application which has to parts like git diff? Commented Jun 11, 2013 at 15:12
  • I just tried apt-get install - it worked :( Commented Jun 11, 2013 at 15:13
  • It seems that the file you are trying to diff is not in the directory with your repo. Commented Jun 11, 2013 at 15:18
  • No, that would be another error. You can get this usage message only if you are running git diff not inside a git repo. Are you sure you don't do cd or something like this before it? Show us your whole pre-commit. Commented Jun 11, 2013 at 15:19

1 Answer 1

2

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
    # ...
}
Sign up to request clarification or add additional context in comments.

6 Comments

@quidage Also It would be better to add --exit-code argument to your git diff command and check for exit code instead of output.
@quidage Please, have a look at my answer, I've updated it a bit.
it worked best for me when i added the revision git diff --exit-code origin -- _js/main.js > /dev/null || { ... }
@quidage Why don't you use --quite? It implies --exit-code and won't output anything, so you won't have to add > /dev/null. I don't know why you had to add origin, that's strange as by default it will compare with the currently checked out branch. Anyway, using master instead of origin is probably a better idea.
Referring your last comment I think this should be a good solution to diff against HEAD: git diff --quiet HEAD~1 -- _js/main.js || {} Do you agree?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.