bash - 使用 Bash 将 .gitmodules 转换为可解析的迭代格式

标签 bash git git-submodules

背景

我想制作一个接受 .gitmodules 的 shell 函数并迭代每个模块,根据每个子模块的属性(例如 <PATH><URL><BRANCH> )执行某些命令。

➡️ .gitmodules的默认格式:

[submodule "PATH"]
    path = <PATH>
    url = <URL>
[submodule "PATH"]
    path = <PATH>
    url = <URL>
    branch = <BRANCH>

➡️伪代码:

def install_modules() {
    modules = new list

    fill each index of the modules list with each submodule & its properties

    iteratate over modules
       if module @ 'path' contains a specified 'branch':
          git submodule add -b 'branch' 'url' 'path'
       else:
          git submodule add 'url' 'path'
}

⚠️ 当前 install_modules()

# currently works for grabbing the first line of the file
# doesn't work for each line after.
install_modules() {
    declare -A regex

    regex["module"]='\[submodule "(.*)"\]'
    regex["url"]='url = "(.*)"'
    regex["branch"]='branch = "(.*)"'

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    cat < ".gitmodules" | while read -r LINE; do
        if [[ $LINE =~ ${regex[module]} ]]; then
            PATH=${BASH_REMATCH[1]}

            echo "$PATH"
        fi
    done
}

最佳答案

.gitmodules 是一个类似于 .gitconfig 的文件,因此您可以使用 git config 来读取它。例如,从 .gitmodules 中读取所有值,按 = (key=value) 拆分值,并按 拆分键。:

git config -f .gitmodules -l | awk '{split($0, a, /=/); split(a[1], b, /\./); print b[1], b[2], b[3], a[2]}'

git config -f .gitmodules -l 打印类似的东西

submodule.native/inotify_simple.path=native/inotify_simple
submodule.native/inotify_simple.url=https://github.com/chrisjbillington/inotify_simple

awk 输出将是

submodule native/inotify_simple path native/inotify_simple
submodule native/inotify_simple url https://github.com/chrisjbillington/inotify_simple

关于bash - 使用 Bash 将 .gitmodules 转换为可解析的迭代格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53896924/

相关文章:

linux - 为目录中的每个文件运行 shell 脚本

bash - 在 HISTIGNORE 中忽略 'history numlines' 的模式,bash

bash - 如何在 vimscript 中使用 sleep 仅用于命令行栏?

git - 拆分一个 git 存储库以同时处理两个项目

推送 : object 15abe3addde5ad5f7d25e8f0f220d2e9faf3cb22:contains entries pointing to null 时出现 Git 错误

linux - bash 脚本 - 我想检查 XLS 是否为空。如果是,我什么都不想做。如果不是,我想做点什么

java - 如何使用 egit/eclipse 配置工作区,以便我的更改转到 fork 上的分支

Git Diff - 指示行移动

linux - Git Clean 过滤器在空文件上中断

django - 在 git 项目中创建子模块