Skip to content

Instantly share code, notes, and snippets.

@ericdum
Last active December 19, 2015 09:09
Show Gist options
  • Save ericdum/5931119 to your computer and use it in GitHub Desktop.
Save ericdum/5931119 to your computer and use it in GitHub Desktop.
git commit的时候,刷新css中的资源:图片什么的。如:如果提交发现对anywhere/header.png有更改,那么搜索包含header.png的css文件,然后匹配css中的header.png是否为同一个文件,如果是:加上时间戳以更新缓存。 未完:更改文件后无法加入本次commit。待解决
#!/bin/sh
#刷新css中的资源:图片什么的
#检查流程:
# 1. 检查变更的资源文件 git status -s
# 2. 将取得的文件名在所有css文件中进行搜索
# 3. 在匹配的css中取得符合的资源相对路径
# 4. 检查相对路径指向的是否为目标文件
# 5. 更新匹配的相对路径的时间戳为当前时间
root=`pwd`
css=''
time=`date +%s`
refreshed=0
declare -a modifieds
#检查变更文件
function checkmodified() {
# 1. 检查变更的资源文件 git status -s
git status -s | while read line
do
file=${line/M /}
# 2. 将取得的文件名在所有css文件中进行搜索
if test "$file" != "$line";then
filename=${file##*/}
filename=${filename/./\\.}
echo $file:
# 3. 在匹配的css中取得符合的资源相对路径
for match in $(grep $filename $root -rIl --include=*.css --exclude-dir=.git)
do
# 4. 检查相对路径指向的是否为目标文件
for matchpath in `grep -o [^\(\']*$filename $match`
do
matchrealpath=${match%/*}/$matchpath
# 5. 更新匹配的相对路径的时间戳为当前时间
if test "$matchrealpath" -ef "$file"; then
flag=${matchpath//\//\\\/}
sed s/$flag?*[0-9]*/$flag?$time/ <$match >$match.ericache
mv $match.ericache $match
echo $match updated!
git add $match
#git commit --amend
#refreshed=1
fi
done
done
fi
done
#if test $refreshed = 1; then
#exit 1
#fi
}
checkmodified
exit 0 #完成,提交
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment