Skip to content

Instantly share code, notes, and snippets.

@minyoad
Last active Aug 24, 2020
Embed
What would you like to do?
git hooks pre-commit to automatic update *.js *.css version
#!/bin/bash
###
## change js and css file version by append ?v111111
##
## usage: copy to .git/hooks/pre-commit
## chmod a+x .git/hooks/pre-commit
#
##
modified_files=$(git diff --cached --name-only)
#echo $modified_files
for f in $modified_files
do
#echo $f
ext="${f##*.}"
fname=`basename $f`
if [ -e "$f" ] && ([[ $ext == css ]] || [[ $ext == js ]] ); then
include_files=`grep src -r -e "$fname" |awk -F: '{print $1}' |uniq`
for f2 in $include_files
do
ver=`stat -c %Y $f2`
sed -i "s/$fname.*\"/$fname\?v$ver\"/g" $f2
git add $f2
done
git add $f
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment