Git 生产模式下分离与合并仓库

笔记 创建于: , 更新于: | Git

介绍

在团队开发过程中,同一个项目经常会用到多个 git 仓库,比如:

为什么要这样呢?因为每个环境的环境变量不一样,比如存储文件的存储桶,正式环境下不可能给开发人员看到,还有数据库之类的都需要另外配置。 当然你也可以用不同的环境变量来实现,实现方式是多种多样的,这里就讲分离仓库的方式,特别适合多人团队的中小型项目。

Linux 版本

还包括一些数组操作,遍历目录,文本替换等功能

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
#!/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

# 假设有 module1 - 3 个目录
modules=("module1" "module2" "module3")
for i in $( ls -d other-modules/*/)  #另外再加目录进 modules 数组里,没有的话可以不用这快代码,只保留初始的 modules
do
  modules+=("$i")   # 添加元素到数组后面
done

loopModules() {
  for i in "${!modules[@]}";
  do
    changeGroup "${modules[$i]}" "$1" # 进每个 module 目录,寻找配置文件,我们这里是 bootstrap.yml
    # 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%

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
:: 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=module1 module2 module3
list the folds of other-modules
(for /f %%i in ('dir .\modules\ /ad /b') do (
  SET modules=!modules!;other-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

如有问题请在下方留言,我会第一时间回复。

笔记标签:

评论 ( 如有任何问题,请在下方留言和讨论 )