Restore git submodules from .gitmodules

前端 未结 7 606
粉色の甜心
粉色の甜心 2020-12-02 04:47

I have a folder, which was a git repo. It contains some files and .gitmodules file. Now, when I do git init and then git submodule init, the latter

7条回答
  •  忘掉有多难
    2020-12-02 05:37

    For zsh users, try my function which has DRY_RUN=1 support to see what commands will be ran and only uses git to parse the file instead of sed.

    gsub_file() {(
      set -eu
    
      cd "$(git rev-parse --show-toplevel)"
    
      submodule_paths=(
        "${(@f)$(git config --file ./.gitmodules --get-regexp "path" | awk '{ print $2 }')}"
      )
      submodule_urls=(
        "${(@f)$(git config --file ./.gitmodules --get-regexp "url" | awk '{ print $2 }')}"
      )
      submodule_branches=(
        "${(@f)$(git config --file ./.gitmodules --get-regexp "branch" | awk '{ print $2 }')}"
      )
    
      sh_c() {
        echo + "$*"
        if [ "${DRY_RUN-}" ]; then
          return
        fi
        eval "$@"
      }
    
      for (( i=1; i <= ${#submodule_paths[@]}; i++ )) do
        p="${submodule_paths[$i]}"
        if [ -d "$p" ]; then
          continue
        fi
    
        url="${submodule_urls[$i]}"
        unset b
        if [ "${submodule_branches[$i]-}" ]; then
          b="-b ${submodule_branches[$i]}" 
        fi
        sh_c git submodule add "${b-}" "$url" "$p"
      done
    )}
    

提交回复
热议问题