Nic*_*mou 5 git bash git-submodules
I would like to make a shell function that takes .gitmodules and iterates over each module executing certain commands based off of each submodules properties (e.g. <PATH> or <URL> or <BRANCH>).
?? The default format of .gitmodules:
[submodule "PATH"]
path = <PATH>
url = <URL>
[submodule "PATH"]
path = <PATH>
url = <URL>
branch = <BRANCH>
Run Code Online (Sandbox Code Playgroud)
?? Pseudocode:
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'
}
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
.gitmodules是一个类似.gitconfig文件,因此您可以使用git config它来读取它。例如,从 a 中读取所有值.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]}'
Run Code Online (Sandbox Code Playgroud)
git config -f .gitmodules -l 打印出类似的东西
submodule.native/inotify_simple.path=native/inotify_simple
submodule.native/inotify_simple.url=https://github.com/chrisjbillington/inotify_simple
Run Code Online (Sandbox Code Playgroud)
和awk输出将是
submodule native/inotify_simple path native/inotify_simple
submodule native/inotify_simple url https://github.com/chrisjbillington/inotify_simple
Run Code Online (Sandbox Code Playgroud)
在@phd和Restore git submodules from .gitmodules(@phd向我指出的方向)的帮助下,我能够构建我需要的函数。
\n\ninstall_submodules()\xe2\x9a\xa0\xef\xb8\x8f注意:假设$REPO_PATH已声明并初始化。
\xe2\x9a\xa0\xef\xb8\x8f我的答案是改编自/sf/answers/3728874901/。
\n\ninstall_submodules() {\n git -C "${REPO_PATH}" config -f .gitmodules --get-regexp \'^submodule\\..*\\.path$\' |\n while read -r KEY MODULE_PATH\n do\n # If the module\'s path exists, remove it.\n # This is done b/c the module\'s path is currently \n # not a valid git repo and adding the submodule will cause an error.\n [ -d "${MODULE_PATH}" ] && sudo rm -rf "${MODULE_PATH}"\n\n NAME="$(echo "${KEY}" | sed \'s/^submodule\\.\\(.*\\)\\.path$/\\1/\')"\n\n url_key="$(echo "${KEY}" | sed \'s/\\.path$/.url/\')"\n branch_key="$(echo "${KEY}" | sed \'s/\\.path$/.branch/\')"\n\n URL="$(git config -f .gitmodules --get "${url_key}")"\n BRANCH="$(git config -f .gitmodules --get "${branch_key}" || echo "master")"\n\n git -C "${REPO_PATH}" submodule add --force -b "${BRANCH}" --name "${NAME}" "${URL}" "${MODULE_PATH}" || continue\n done\n\n git -C "${REPO_PATH}" submodule update --init --recursive\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
960 次 |
| 最近记录: |