批量删除远程无效分支的shell脚本

2024-01-14  本文已影响0人  靖哥哥编程

!/bin/bash

shell script delete remote branch and local branch

if [[ "" == 1 || "" ==2 ]]; then
echo "please add reponsitory path and delete date"
echo "example: ./delete_mr.sh ./npu-codebase 365 ###删除超过365天时间的分支"
exit
fi

reponsitory_name="1" echo "You are going to delete【"reponsitory_name"】 branchs !!!!!!";
echo " "
cd reponsitory_name; days="2"

for branch in git branch -r --no-merged | grep -v HEAD;
do

分支名称

branch_name=echo $branch | grep '/' | cut -d '/' -f 2-;

分支最后提交时间

branch_timestamp=git show --format="%ct" $branch | head -n 1;

当前系统时间

cur_sec_and_ns=date '+%s-%N';
cur_sec=${cur_sec_and_ns%-*};

时间差

time_different=[cur_sec-branch_timestamp];

阈值g

time_require=[3600*24*days];

if [[ "" !=  "$branch_name"
        && "HEAD" != "$branch_name" 
        && "master" != "$branch_name"
        && $time_different -ge $time_require
        ]]; then
    echo "deleting current branch: " `git show --format="%ci %cr" $branch | head -n 1` $branch_name
    echo "branch_name: " $branch_name
    echo "delete local "$branch_name
    local_result=`git branch -d $branch_name`
    echo "delete remote "$branch_name
    remote_result=`git push origin --delete $branch_name`
    echo "delete over"
    echo ""
fi          

done
echo "!!!!! game over !!!!!";

上一篇 下一篇

猜你喜欢

热点阅读