Git多仓库及其子仓库项目拉取

Git多仓库及其子仓库项目拉取

  • 背景
    • 脚本
    • 备注

背景

在公司项目中遇到了一个项目的各个子模块在不同git仓库中,于是将所有git仓库目录放到一个子目录下,并写了一个简单的同步脚本。

脚本

#!/usr/bin/env bash

#!/usr/bin/env bash

if test -z "$1"; then
  $1 = dev
fi

if test -z "$2"; then
  $2 = ./
fi

workPath=$(readlink -f $2)

for file in $workPath/*; do
  if test -d ${file}; then
    #  filePath=$(readlink -f ${file})
    cd ${file}
    if test -f pom.xml; then
      mvn clean &
    fi
    #  check if contains git repository
    if test -d .git; then

      echo "================update git repository in file: "${file}"================"
      # change to target brach
      remoteDevBranchName=$(git branch -a | grep $1 | grep origin)
      if test -n "$remoteDevBranchName"; then
        echo "================change to $1 branch: "${file}"================"
        subLocalDevBranch=$(git branch | grep $1 | grep -v origin)
        currentBranch=$(git branch --show-current)
        if test -z "$localDevBranch"; then
          git checkout -b $1 --track $remoteDevBranchName
          git branch --set-upstream-to=$remoteDevBranchName
        else
          if [ "$subLocalDevBranch" -ne "$currentBranch" ]; then
            git checkout $localDevBranch
          fi
          git branch --set-upstream-to=$remoteDevBranchName
        fi
        git pull
      fi
      #  update submodule if exists
      if test -f .gitmodules; then
        git submodule init
        git submodule update
        for submoduleFile in $(cat .gitmodules | grep path | awk -F= '{print $2}'); do
          echo "================update submodule: "${submoduleFile}"================"
          cd ${submoduleFile}
          # change to target brach
          subRemoteDevBranchName=$(git branch -a | grep $1 | grep origin)
          if test -n "$subRemoteDevBranchName"; then
            echo "================change to $1 branch: "${file}"================"
            subLocalDevBranch=$(git branch | grep $1 | grep -v origin)
            subCurrentBranch=$(git branch --show-current)
            if test -z "$subLocalDevBranch"; then
              git checkout -b $1 --track $subRemoteDevBranchName
              git branch --set-upstream-to=$subRemoteDevBranchName
            else
              if [ "$subLocalDevBranch" -ne "$currentBranch" ]; then
                git checkout $subLocalDevBranch
              fi
              git branch --set-upstream-to=$subRemoteDevBranchName
            fi
            git pull

          fi
          cd -
        done
        #maven build
        if test -f pom.xml; then
          mvn clean install
        fi
      fi
    fi

    cd ${workPath}
  fi
done


备注

以上脚本在windows系统中用git bash可执行

你可能感兴趣的:(工具脚本,Git)