Shahar Hamuzim Rajuan
Shahar Hamuzim Rajuan

Reputation: 6129

How can I automatically detect that a file of a given type has changed, in Git?

My team is working on developing automation tests, and we are facing the following problem: when a developer commits a change that affects the UI of our application, the automation test fails.

Is there a way, in Git, for me to get notified by email whenever a specific file type (in my case .jsp) is changed?

Note that setting up an email notification is not the problem, here; I know how I could use a hook for that. Rather, the problem is about how to detect that a file of a given type (in my case .jsp) has changed. Any idea?

Upvotes: 0

Views: 726

Answers (2)

Shahar Hamuzim Rajuan
Shahar Hamuzim Rajuan

Reputation: 6129

I finally created this hook, as a post-recieve hook:

for file in $(git show --pretty="format:" --name-only $1..$2 $3 | grep -i .jsp)
    do
       git diff $1..$2 -- $file | \
       mail -s "A '*.jsp' file has changed in the Repo" \
       [email protected],[email protected]
    exit
done

Upvotes: 0

Jan Hudec
Jan Hudec

Reputation: 76246

It would be easy with a simple post-receive hook, but it would not be good solution to the ultimate problem.

When developer commits change to the UI, they should also update the tests so they pass again in the same commit or at least in the same push. That means the tests should be in the same repository as the code. Well, since they are intimately tied with the way the UI works, they should.

Then it's just a matter of using Continuous Integration (I am used to Jenkins; there are other options too) and/or automatically deploying the changes to the test environment against which the tests run (see e.g. Deploy a project using Git push).


That said, you can do it with hooks.

First, to find whether any *.jsp files have been changed between revisions $old and $new you call:

git diff --exit-code --quiet $old..$new -- '*.jsp'

and if exits with false (non-zero) status, then some have changed (without the --quiet it will show you the changes, with --name-status it will list the files).

So now you just need to put something like

while read old new ref
do
    if !git diff --exit-code --quiet $old..$new -- '*.jsp'
    then
        git diff --name-status $old $new | \
            mail -s "*.jsp files changed in $ref" \
                [email protected]
    fi
done

to post-receive hook in your repository.

Or you could adjust the command to prevent developers pushing *.jsp files directly to master (e.g. via gitolite) to force them to push such changes to some staging branch where the automation will be updated by the other team and only then integrated by the other team or designated gatekeeper.

Upvotes: 2

Related Questions