背景
在多个项目频繁切换、快速迭代过程中,我们在本地开发环境创建过许多feature、bugfix分支没有清理。我们期望能够提供一键清空本地无用分支的脚本。
脚本功能
- 展示当前目录下所有仓库缓存信息
- 删除当前目录下所有仓库缓存信息
- 展示当前目录下所有仓库分支
- 切换至目标分支并拉取最新代码
- 删除所有其他本地分支
功能演示
windows下.bat文件操作小demo演示图片
linux下.sh文件操作大demo演示视频
https://live.youkuaiyun.com/v/324037
实现
核心:批量操作+自定义指令
bat脚本
@REM 目标:清理本地开发环境
@REM 删除本地所有分支和stash缓存,保留${targetBranch}分支
@echo off
setlocal EnableDelayedExpansion
echo Welcome to the roubing999 tool...
echo.
set dp=%~dp0
setlocal
set yn=n
set /p yn="Whether to show the cache in stash (y | n)? "
call :confirm !yn!
echo.
if !yn!==y (
call :show_stashes %CD%
)
echo -----查看当前目录下所有仓库中的stash缓存数据完成-----
endlocal
setlocal
set yn=n
set /p yn="Whether to delete the cache in stash (y | n)? "
call :confirm !yn!
echo.
if !yn!==y (
call :clear_stashes %CD%
)
echo -----批量删除当前目录下所有仓库中的stash缓存数据完成-----
endlocal
setlocal
set yn=n
set /p yn="Whether to show all branches (y | n)? "
call :confirm !yn!
echo.
if !yn!==y (
call :show_branches %CD%
echo -----查看当前目录下的所有仓库分支完成-----
)
endlocal
setlocal
set yn=n
set /p yn="Whether to continue the batch delete (y | n)? "
call :confirm !yn!
if !yn!==n (
echo exit.
exit
)
echo.
endlocal
setlocal
set branch=develop
set /p branch="Please enter the target branch (develop): "
echo target branch: %branch%
set now=%date% %time%
set now=!now: =-!
echo now: %now%
call :switch_and_update_branch %CD% %branch% %now%
echo -----批量删除本地分支完成-----
endlocal
echo.
echo Finished.
exit
:show_branches
for /d %%i in (%1\*) do (
cd %%i
if exist .git (
set p=%%i
echo The branches under the repo path !p:%dp%=.\!
git branch
) else (
call :show_branches %%i
)
)
goto :EOF
:switch_and_update_branch
for /d %%i in (%1\*) do (
cd %%i
if exist .git (
set p=%%i
echo.
echo current path: !p:%dp%=.\!
git stash save %3
set hasTargetBranch=n
for /f "delims=" %%j in ('git branch') do (
echo %%j | findStr %2 > nul && set hasTargetBranch=y
)
if !hasTargetBranch!==y (
git switch %2
) else (
git remote update origin --prune
git checkout -b develop origin/develop
)
git branch | xargs git branch -D
git pull
) else (
call :switch_and_update_branch %%i %2 %3
)
)
goto :EOF
:show_stashes
for /d %%i in (%1\*) do (
cd %%i
if exist .git (
set p=%%i
echo current repo path !p:%dp%=.\!
git stash list
) else (
call :show_stashes %%i
)
)
goto :EOF
:clear_stashes
for /d %%i in (%1\*) do (
cd %%i
if exist .git (
set p=%%i
echo current repo path !p:%dp%=.\!
git stash clear
) else (
call :clear_stashes %%i
)
)
goto :EOF
:confirm
if !yn!==y (
echo.
) else if !yn!==n (
echo.
) else if !yn!==-1 (
echo exit.
exit
) else (
echo error.
set /p yn="input error value: !yn!, please re-enter (y | n): "
call :confirm !yn!
)
goto :EOF
shell脚本
# 目标:清理本地开发环境
# 删除本地所有分支和stash缓存,保留${targetBranch}分支
#!/bin/bash
echo Welcome to the roubing999 tool...
function confirm {
if [[ $yn == "y" ]]
then
return
elif [[ $yn == "n" || $yn == "" ]]
then
yn="n"
elif [[ $yn == -1 ]]
then
exit
else
echo -e "\033[31m error \033[0m"
read -p "input error value: ${yn}, please re-enter (y | n): " yn
confirm yn
fi
}
function show_stashes {
for path in $(ls $1)
do
if test -d $1/${path}
then
if test -e $1/$path/.git
then
echo -e "\033[34m current repo path $1/$path\033[0m"
cd $1/$path
git stash list
else
show_stashes $1/${path}
fi
fi
done
}
function clear_stashes {
for path in $(ls $1)
do
if test -d $1/${path}
then
if test -e $1/$path/.git
then
echo -e "\033[34m current repo path $1/$path\033[0m"
cd $1/$path
git stash clear
else
clear_stashes $1/${path}
fi
fi
done
}
function show_branches {
for path in $(ls $1)
do
if test -d $1/${path}
then
if test -e $1/$path/.git
then
echo -e "\033[34m current repo path $1/$path\033[0m"
cd $1/$path
git branch
else
show_branches $1/${path}
fi
fi
done
}
function switch_and_update_branch {
for path in $(ls $1)
do
if test -d $1/${path}
then
if test -e $1/$path/.git
then
echo -e "\033[34m current repo path $1/$path\033[0m"
cd $1/$path
git stash save $3
hasTargetBranch="n"
for branch in $(git branch)
do
if [[ $branch == $2 ]]
then
hasTargetBranch="y"
fi
done
if [[ $hasTargetBranch == "y" ]]
then
git switch $2
else
# 在本地未找到目标分支,则尝试更新与远程的关联,切换到默认目标分支
git remote update origin --prune
git checkout -b develop origin/develop
fi
git branch | xargs git branch -D
git pull
else
switch_and_update_branch $1/${path} $2 $3
fi
fi
done
}
# 主流程
CURRENT_DIR=$(cd `dirname $0`; pwd)
read -p "Whether to show the cache in stash (y | n)? " yn
confirm yn
if [[ $yn == "y" ]]
then
show_stashes $CURRENT_DIR
echo -e "\033[32m -----查看当前目录下所有仓库中的stash缓存数据完成-----\033[0m \033[34m"
fi
read -p "Whether to delete the cache in stash (y | n)? " yn
confirm yn
if [[ $yn == "y" ]]
then
clear_stashes $CURRENT_DIR
echo -e "\033[32m -----批量删除当前目录下所有仓库中的stash缓存数据完成-----\033[0m \033[34m"
fi
read -p "Whether to show all branches (y | n)? " yn
confirm yn
if [[ $yn == "y" ]]
then
show_branches $CURRENT_DIR
echo -e "\033[32m -----查看当前目录下的所有仓库分支完成-----\033[0m \033[34m"
fi
read -p "Whether to continue the batch delete (y | n)? " yn
confirm yn
if [[ $yn != "y" ]]
then
exit
fi
read -p "Please enter the target branch (develop): " branch
if [[ $branch == "" ]]
then
branch="develop"
fi
echo "target branch: "$branch
now=`date +%Y/%m/%d-%H:%M:%S`
echo "now: "$now
switch_and_update_branch $CURRENT_DIR $branch $now
echo -e "\033[32m -----批量删除当前本地分支完成-----\033[0m \033[34m"