Git 提交前替换某些目录内代码,提交后又恢复它
还包括一些数组操作,遍历目录,文本替换等功能
Linux 版本
#!/bin/bash
# usage:
# git commit: ./push.sh 'commit message'
# set nacos group to DEFAULT_GROUP: ./push.sh set default
# set nacos group to my name: ./push.sh set my
modules=("gigi-auth" "gigi-gateway" "gigi-visual/gigi-monitor")
for i in $( ls -d gigi-modules/*/) #只显示目录
do
modules+=("$i") # 添加元素到数组后面
done
loopModules() {
for i in "${!modules[@]}";
do
changeGroup "${modules[$i]}" "$1"
# echo "${modules[$i]}"
done
}
changeGroup(){
filename=$1/src/main/resources/bootstrap.yml
groupName=$(git config user.name)
myGroupStr="group: $groupName"
defaultGroupStr="group: DEFAULT_GROUP"
if [ "$2" == "default" ]; then
echo "------ $1 恢复为 $defaultGroupStr 参数 ---------"
sed -i "" "s/$myGroupStr/$defaultGroupStr/g" "$filename"
fi
if [ "$2" == "my" ]; then
echo "------ $1 更新为 $myGroupStr 参数 ---------"
sed -i "" "s/$defaultGroupStr/$myGroupStr/g" "$filename"
fi
}
gitPush() {
useBranch="dev"
echo "------ git checkout $useBranch ---------"
git checkout "$useBranch"
loopModules default
echo "------ 提交本分支 ---------"
echo "------ git add --all ---------"
git add --all
echo "------ git commit -m $commitMessage ---------"
git commit -m "$commitMessage"
echo "------ git pull origin $useBranch ---------"
git pull origin "$useBranch"
echo "------ git push origin $useBranch ---------"
git push origin "$useBranch"
loopModules my
}
commitMessage=$1
if [ "$commitMessage" == "" ] ; then
echo "请输入 commit 信息"
exit
elif [ "$commitMessage" == "set" ] ; then
if [ "$2" == "default" ] ; then
loopModules default
elif [ "$2" == "my" ]; then
loopModules my
fi
else
gitPush
fi
###### 以下内容是根据标识行来增加或删除内容
# updateGroup "${modules[$i]}" "add"
# updateGroup "${modules[$i]}" "remove"
updateGroup(){
groupName=$(git config user.name)
groupStr="group: $groupName"
filename=$1/src/main/resources/bootstrap.yml
markStr='# 服务注册地址'
markStrLine=$(awk "/$markStr/{ print NR; exit }" "$filename") # 找出 '服务注册地址' 所在的行数
insertLine=$(($markStrLine+2)) # 行数+2
# echo "$markStrLine"
# echo "$insertLine"
# currentPath=$(pwd)
readInsertLine=$(sed -n "${insertLine}p" "$filename") # 读取改行的字符串
echo "$readInsertLine"
if [[ "$readInsertLine" == *"group"* ]]; then # 对比改行是否含有 group 字符串
if [ "$2" = "remove" ]; then
echo "------ $1 删除 group 参数 ---------"
sed -i "" -e "${insertLine}d" "$filename" # 删除改行
else
echo "------ $1 已经有 group 参数 ---------"
fi
else
if [ "$2" = "add" ]; then
echo "------ $1 插入 group 参数 ---------"
sed -i "" -e "${insertLine}i\\
$groupStr
" "$filename"
fi
fi
}
Windows 版本
windows 新版本的语法与老版本有点不同,在循环体中用 !var! 代替 %var%
:: usage:
:: git commit: ./push.sh 'commit message'
:: set nacos group to DEFAULT_GROUP: ./push.sh set default
:: set nacos group to my name: ./push.sh set my
@echo off
setlocal enabledelayedexpansion
:: get which modules need to change group
SET modules=gigi-auth gigi-gateway gigi-visual\gigi-monitor
list the folds of gigi-modules
(for /f %%i in ('dir .\gigi-modules\ /ad /b') do (
SET modules=!modules!;gigi-modules\%%i
))
:: set group config string
(for /f %%i in ('git config user.name') do (
SET groupName=%%i
))
echo "my group name is %groupName%"
SET myGroupStr="group: %groupName%"
SET defaultGroupStr="group: DEFAULT_GROUP"
:: set git commit message
SET commit_msg=%1
IF "%commit_msg%"=="" (
echo "Please input commit message"
exit
) ELSE IF "%commit_msg%"=="set" (
IF "%2"=="default" (
CALL :loopModules default
) ELSE IF "%2"=="my" (
CALL :loopModules my
)
) ELSE (
CALL :gitPush
)
EXIT /B %ERRORLEVEL%
:loopModules
(for %%m in (%modules%) do (
:: echo %%m
CALL :changeGroup %%m %1
))
EXIT /B 0
:changeGroup
SET filename=%1\src\main\resources\bootstrap.yml
IF "%2"=="default" (
echo "------ %1 reset to %defaultGroupStr% ---------"
powershell -Command "(gc %filename% -encoding utf8) -replace '%myGroupStr%', '%defaultGroupStr%' | Out-File -encoding utf8 %filename%"
) ELSE IF "%2"=="my" (
echo "------ %1 set to %myGroupStr% ---------"
powershell -Command "(gc %filename% -encoding utf8) -replace '%defaultGroupStr%', '%myGroupStr%' | Out-File -encoding utf8 %filename%"
)
EXIT /B 0
:gitPush
SET useBranch=dev
echo "------ git checkout %useBranch% ---------"
git checkout %useBranch%
CALL :loopModules default
echo "------ git add --all ---------"
git add --all
echo "------ git commit -m %commit_msg% ---------"
git commit -m "%commit_msg%"
echo "------ git pull origin %useBranch% ---------"
git pull origin %useBranch%
echo "------ git push origin %useBranch% ---------"
git push origin %useBranch%
CALL :loopModules my
EXIT /B 0