#!/bin/bash
function rand(){
min=$1
max=$(($2-$min+1))
num=$(($RANDOM+1000000000)) #增加一个10位的数再求余
echo $(($num%$max+$min))
}
end_cond="xxxxxxxxxxxx" ##结束条件
root_path="/xxxxx/gittest"
log_file=$root_path/log_all_new.log ##log文件目录
re_cmd_path=$root_path/re_cmd ##新目录
temp_file=$root_path/temp.log ##缓存文件
source_path=$root_path/cpp-bcad ##代码原始目录
shell_path=$root_path/start.sh ##脚本目录
# temp_dir=$root_path/temp_dir ##缓存目录
diff_file=$root_path/diff_file.txt ##对比文件
diff_file_temp=$root_path/diff_status.txt
#echo $log_file
# echo $source_path
line_number=1
commit_last_hash="yyyyyyyyyyyyyyy"
msg=
commit_log=
commit_hash=
begin_time_sec=1538391527 ## 2018-10-1 18:58:47
one_day=86400
commit_step=4
length=${#array_name[@]}
length=$[length - 1]
##清楚缓存
echo "" > $diff_file
echo "" > $diff_file_temp
while :
do
echo "line_number-------- ${line_number}"
msg=`tail -n $line_number $log_file | head -n 1`
echo $msg > $temp_file
commit_log=`awk -F '###' '{print $2}' $temp_file`
commit_hash=`awk -F '###' '{print $3}' $temp_file`
# echo "commit_log: " $commit_log
# echo "end_cond: " $end_cond
# echo "commit_hash: "$commit_hash
cd "${source_path}"
echo "switch dir: ${source_path} "
git checkout $commit_hash
echo "commit_hash : $commit_hash"
echo "commit_last_hash : $commit_last_hash"
echo "" > $diff_file ##清空
git diff $commit_last_hash $commit_hash --name-status > $diff_file ##对比文件目录
#cat $diff_file
##从$diff_file读取 修改文件
while read line
do
echo $line > $diff_file_temp
status=`awk -F ' ' '{print $1}' $diff_file_temp`
file_name=`awk -F ' ' '{print $2}' $diff_file_temp`
reg='^R.*'
if [[ "$status" = "D" ]]; then
# echo "will delete $re_cmd_path/$file_name"
rm -rf $re_cmd_path/$file_name
elif [[ "$status" =~ $reg ]]; then
mv_file_dir=`awk -F ' ' '{print $3}' $diff_file_temp`
move_to_path=$re_cmd_path/$mv_file_dir
move_to_path=${move_to_path%/*}
if [ ! -d "$move_to_path" ]; then
# echo "create dir ${file_dir}"
mkdir -p $move_to_path
fi
if ! mv $re_cmd_path/$file_name $re_cmd_path/$mv_file_dir
then
exit 1
fi
else
file_path=$re_cmd_path/$file_name
file_dir=${file_path%/*}
if [ ! -d "$file_dir" ]; then
# echo "create dir ${file_dir}"
mkdir -p $file_dir
fi
if ! cp -r $source_path/$file_name $re_cmd_path/$file_name
then
#statements
echo "copy file error. ${file_name}"
exit 1;
fi
fi
done < $diff_file
commit_email="email"
commit_name="user name"
cd "$re_cmd_path"
git add .
`git config user.name ${commit_name}`
`git config user.email ${commit_email}`
rd=$(rand 1000 3600)
flag=$(($line_number % $commit_step))
if [[ $flag = 0 ]]; then
begin_time_sec=$[begin_time_sec + one_day]
fi
commit_sec=$[begin_time_sec + rd]
commit_time=`date -r $commit_sec`
git commit -m "$commit_log" --date "$commit_time"
if [[ $line_number = 700 ]]; then
exit 1;
fi
##end ....
if [[ "$end_cond" = "$commit_hash" ]]; then
#statements
echo "end ......"
break
fi
commit_last_hash=$commit_hash
line_number=$[line_number + 1]
done