背景
我想制作一个接受 .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/