Skip to content

Instantly share code, notes, and snippets.

@ruanjf
Last active December 9, 2016 10:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruanjf/5999964 to your computer and use it in GitHub Desktop.
Save ruanjf/5999964 to your computer and use it in GitHub Desktop.
获取git两次commit变化的文件
#!/bin/sh
#-------------------------------------------------
# 本脚步实现git的不同commit间的增量文件提取,使用说明:
# wkp 工作目录 默认值当前目录(以“/”结尾)
# cpp 要复制的文件前缀 默认值当前目录(以“/”结尾)
# upn 待更新的临时文件夹 默认值update_tmp
# commit 指定git commit进行比较
#
# author: ruanjiefeng@gmail.com
# update: 2016-05-12 23:46:37
#-------------------------------------------------
# git 工作目录
#wkp="/root/.jenkins/jobs/tw/workspace/"
if [ ! -n "$wkp" ]; then
# for jenkins
if [ -n "$WORKSPACE" ]; then
wkp=$WORKSPACE/
else
wkp=`pwd`/
fi
fi
#wkp=${WORKSPACE}/
if [ ! -s ${wkp} ]; then
echo "can not found workspace (${wkp}), program exit"
exit 1
fi
echo "workspace ${wkp}"
# 需更新的部分
if [ ! -n "$cpp" ]; then
#cpp="$wkp"
#cpp="src/main/webapp/"
cpp="./"
fi
echo "update dir ${cpp}"
#cpp=${wkp}${cpp}
# 指定增量包开始位置
if [ $commit ] && [ "$commit" != "" ] ; then
echo "use commit ${commit}"
lp="$commit"
fi
# 更新临时文件夹名称
if [ ! -n "$upn" ]; then
upn="update_tmp"
fi
upnP="${upn}/"
updateW="${wkp}${upnP}"
echo "update tmp ${upnP}"
cd $wkp
update="true"
deleteW="false"
fileP="${wkp}update_lastPull"
echo "update key update_lastPull"
if [ ! -s ${fileP} ]; then
update="false"
fi
echo "update model: ${update}"
if [ ! -n "$lp" ]; then
lp=`cat ${fileP}`
fi
hd=`cat ${wkp}.git/HEAD`
#if [[ $hd == ref:* ]]; then
# hd=`cat ${wkp}.git/${hd:5}`
#fi
if [ "$lp" = "$hd" ]; then
echo "no file change"
rm -rf ${upn}_${lp}
exit 0
fi
if [ "$update" = "true" ]; then
if [ -s ${updateW} ]; then
llp="${upn}_${lp}"
echo "backup ${upn} to ${llp}"
if [ -s ${wkp}${llp} ]; then
rm -rf ${llp}
fi
# 是否保留备份上次的增量文件,存在but变量则备份
if [ $but ] ; then
mv -f $upn ${llp}
else
rm -rf $upn
fi
fi
echo "create $upnP"
mkdir -p $updateW
sp="\t"
if [ "$cpp" != "./" ] ; then
sp="$sp$cpp"
fi
td=`git diff --diff-filter=ADM --name-status $lp -- $cpp | awk -F"$sp" '
{
if ($1 == "D") {
{ print "echo \""$2"\" >> "uw"delFiles.txt;" }
} else {
{ print "mkdir -p \"\`dirname \""uw$2"\"\`\";" }
{ print "cp -f \""uc$2"\" \""uw$2"\";" }
}
}
' uw="${upn}/" uc="$cpp"`
echo "copy files to ${upnP}"
echo $td
eval $td
fi
echo "update to commit $hd"
if [ "$update" = "false" ]; then
echo "create $updateW"
mkdir -p $updateW
if [ "$cpp" = "./" ] ; then
cp -R `ls -A | grep -v ".git\|${upn}"` $updateW
#cp -R `ls -A | grep -vE ".git|${upn}"` $updateW
else
cp -R ${cpp}* $upnP
#cp -R ${cpp}* $updateW
fi
fi
#echo -n "$hd" > $fileP
echo "$hd" > $fileP
if [ "$deleteW" = "true" ]; then
echo "delete $updateW"
rm -rf $updateW
fi
# for jenkins
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment