转自:https://blog.csdn.net/yang_quan_yang/article/details/103522275
为每一个modified文件成diff文件并且checkout为原来文件重命名为.old,例如再Android 源码里面下面:
生成:
#!/bin/bash
# 不用diff old的文件 add 上 即可;
# ./creat_patch.sh . 2 n
# 1 :clean for test
# c: only clear *.diff *.old
ignore_list=(
"./creat_pacth.sh"
) #数组类型
clean()
{
echo $(find -name "*.diff")
rm -rf $(find -name "*.diff")
echo $(find -name "*.old")
rm -rf $(find -name "*.old")
}
filter_file()
{
o="old"
d="diff"
j=0
i=0
plat=${1}
while (($j < ${#ignore_list[@]})) #while循环
do
result=$(echo $file |grep "${ignore_list[$j]}")#过滤忽略的文件
if [ -n "$result" ]
then
echo "======while============="
echo $result
echo "=====++WH end==========="
break
else
echo "======operate diff======"
echo $file
let ++i
echo $i
content=$(git diff -p $file) #获取diff的的内容
echo $content
if [ "$content" != "" ];then #如果不是空就进行 diff 和cp操作
$(git diff -p $file > $file.$d)
$(git checkout $file)
$(cp $file $file.$o)
$(git apply $file.$d)
echo "======op end========="
fi
fi
let ++j
done
}
read_file() #查找目录下的所有子文件
{
dir=`ls $1`
for i in $dir
do
if [ -d $1"/"$i ]
then
read_file $1"/"$i #递归方法
else
file="$1"/"$i"
filter_file $file
fi
done
}
if [ $3 == "c" ] #通过第三个参数来判断是否进行清除
then
clean
exit 0
fi
read_file $1
echo $(find -name *.diff)
echo $(find -name *.old)
if [ $2 == "1" ] #通过第2个参数进行判断是否进行测试时清除操作
then
clean
echo "=========================="
echo "clean done"
echo "=========================="
fi
使用方式是:
./creat_patch.sh . 2 n