0

I've looked here but my question wasn't answered. I have a script that keeps a number of files up-to-date. The repository consists of a number of bash and python scripts. One of the scripts runs on an hourly cronjob and looks like this:

#! /bin/bash

git fetch origin && git reset --hard origin/dev && git clean -f -d

python -m compileall .
# Set permissions
chmod -R 744 *

Essentially it updates all the scripts to the current content of GitHub. One of the scripts is code for a daemon. When that changes I want to restart the daemon. There's no clue in the output from the git commands regarding which files were changed. So, how can I do that?

To complicate matters, I think the python -m compileall makes git think all files have changed. But I found this question that seems to work.

[EDIT] Additional bonus question added: Based on the answer given below by @behzad.nouri I have modified the code thus:

#! /bin/bash

branch=$(cat ~/bin/gitbin.branch)
git fetch origin && \
DIFFdmn=$(git --no-pager diff --name-only $branch..origin/$branch -- ./testdaemon/daemon.py) && \
DIFFlib=$(git --no-pager diff --name-only $branch..origin/$branch -- ./testdaemon/libdaemon.py) && \
git reset --hard origin/dev && git clean -f -d

python -m compileall .
# Set permissions
chmod -R 744 *

if [[ -n "$DIFFdmn" ]]; then
    logger -t 02-update-scripts "daemon has changed"
    ./testdaemon/daemon.py restart
fi

if [[ -n "$DIFFlib" ]]; then
    logger -t 02-update-scripts "daemonlib has changed"
    ./testdaemon/daemon.py restart
fi

where ~/bin/gitbin.branch should contain the name of the branch to sync with. This works for the branch called dev but fails for the master-branch (when trying to define the DIFFdmn variable) with the message:

fatal: bad revision 'master..origin/master'

Any suggestions are very welcome.

Community
  • 1
  • 1
Mausy5043
  • 906
  • 2
  • 17
  • 39

2 Answers2

3
git diff --name-only

gives the name of files which have changed. To avoid python -m compileall issue, you need to compare against local branch, as opposed to working directory, as in:

git diff --name-only dev..origin/dev

if you only care about one file, pass it to the diff command:

git diff --name-only dev..origin/dev -- path/to/daemon.file

and on the bash side you can check the out-put by -n:

DIFF=$(git --no-pager diff --name-only dev..origin/dev -- path/to/daemon.file)

if [[ -n "$DIFF" ]]
then 
    echo "daemon has changed"
fi
behzad.nouri
  • 74,723
  • 18
  • 126
  • 124
  • Thanks. That seems to work. I'll insert the `DIFF=...` before the `git reset`. That way I can detect the file-change and restart the daemon after the compile. – Mausy5043 Apr 12 '15 at 14:23
  • But, it seems this doesn't work on the master-branch (replacing `dev..origin/dev` by `master..origin/master`. Any suggestions for that case? – Mausy5043 Apr 16 '15 at 15:45
0

While accepting the answer given by @behzad.nouri I would like to answer the bonus question myself. So at least the question can be closed and answers to the problems posed are hopefully helpful to others.

In the bonus question the code fails because git diff apparently can't compare beyond the current branch. First you need to switch branches using git checkout $branch and then do the git diff.

So, something like this:

branch=$(cat ~/bin/gitbin.branch)
git checkout $branch
git fetch origin && \
DIFFdmn=$(git --no-pager diff --name-only $branch..origin/$branch -- ./testdaemon/daemon.py) && \
DIFFlib=$(git --no-pager diff --name-only $branch..origin/$branch -- ./testdaemon/libdaemon.py) && \
git reset --hard origin/$branch && git clean -f -d

That should work.

Mausy5043
  • 906
  • 2
  • 17
  • 39