Git pull check if file changed bash

I am running the following bash script in cronjob to update my website

#!/bin/bash

cd /home/username/public_html
git pull origin master

This cronjob starts every 5 minutes, so if I made the switch to git repo, it can be pulled to the website.

What I want to do, every time this script is run, checks if a particular file has been modified, run the curl / wget command if the file has been modified.

I need to track changes: style.css and script.js

What am I doing if this file is modified, update the script version number in my website template so that the cache is cleared and the latest changes are loaded by the css and js file.

<link href="/css/style.css?REVISION_NUMBER_HERE" rel="stylesheet" type="text/css">

I can easily increase the JS / CSS version number with a curl query since it is stored in mysql DB.

Here is an example of git output result, so we can grep for this style.css / script.js as a result

 * branch            master     -> FETCH_HEAD
Updating cbd0d46..36aeed6
Fast-forward
 admin/cf.php         |   16 ++++++++++++++++
 css/style.css        |   47 +++++++++++++++++++++++++++--------------------
 include/common.php   |    1 +
 templates/header.tpl |    4 ++--
 templates/index.tpl  |   13 ++++++-------
 5 files changed, 52 insertions(+), 29 deletions(-)

@torek .

bash scripting, git.

[root@server70 public_html]# git diff --name-only 8613bd3ed718b27ca680bdf33e0a4b32b0f58b8c HEAD | egrep '.css|.js|.tpl'
css/style.css
templates/header.tpl
templates/index.tpl
templates/user/delete.tpl
[root@server70 public_html]#

git rev . bash script

[root@server70 public_html]# sh 1.sh
File changed css/style.css
File changed templates/header.tpl
File changed templates/index.tpl
File changed templates/user/delete.tpl
[root@server70 public_html]# cat 1.sh
#!/bin/bash


CSS_JS_CHANGED=0

git diff --name-only 8613bd3ed718b27ca680bdf33e0a4b32b0f58b8c HEAD | egrep '.css|.js|.tpl' |
while read name; do
   CSS_JS_CHANGED=1
   echo "File changed $name"
done

if [ "$CSS_JS_CHANGED" = "1" ]; then
    echo expression evaluated as true
fi

[root@server70 public_html]#

script , , . script .

, CSS_JS_CHANGED, 0. , , 1. script, CSS_JS_CHANGED 1.

- CSS_JS_CHANGED 1 while. -, ?

** 2 **

,

bash noob, - script , :)

3

CSS_JS_CHANGED.

[root@server70 public_html]# cat update
#!/bin/bash
# */5 * * * * /home/USERNAME/public_html/update >/dev/null 2>&1


cd /home/USERNAME/public_html
OLD_HEAD=$(git rev-parse HEAD)
git pull origin master
NEW_HEAD=$(git rev-parse HEAD)

[ $OLD_HEAD = $NEW_HEAD ] && exit 0

git diff --name-only $OLD_HEAD $NEW_HEAD | egrep '.css|.js' |
while read name; do
   touch /tmp/css_js_update
done

if [ -f "/tmp/css_js_update" ]
then
    /usr/bin/curl 'http://mywebsite.com/admin/cf.php?js_version_update=mywebsite'
    rm -f /tmp/css_js_update
fi
[root@server70 public_html]#

.css/.js:)

@torek

+3
1

, , : " ?"

-, " , ". - , , "", - git, - , .

, git pull ( , ). , git merge, SHA-1. , , :

OLD_STYLE_ID=$(git rev-parse HEAD:css/style.css)
OLD_SCRIPT_ID=$(git rev-parse HEAD:script.js) # fix path here if/as needed

fetch-and-merge/pull, SHA-1:

git pull origin master
NEW_STYLE_ID=$(git rev-parse HEAD:css/style.css)
NEW_SCRIPT_ID=$(git rev-parse HEAD:script.js) # again, fix path if needed

, , ; , , .

HEAD, , - (-), :

OLD_HEAD=$(git rev-parse HEAD)
git pull origin master
NEW_HEAD=$(git rev-parse HEAD)
[ $OLD_HEAD = $NEW_HEAD ] && exit 0 # no change to repo = no change to any files

, :

git diff --name-only ${OLD_HEAD} ${NEW_HEAD} -- css/style.css script.js |
while read name; do
    ...
done

, git diff --name-only, ( ), .

+3

All Articles