Skip to content

Instantly share code, notes, and snippets.

@rockandska
Created July 11, 2017 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rockandska/9d2e1985e117711fed0087609f152e2e to your computer and use it in GitHub Desktop.
Save rockandska/9d2e1985e117711fed0087609f152e2e to your computer and use it in GitHub Desktop.
Git submodules YAML config dumper : .gitmodules to .gitmodules.yml
#!/usr/bin/env bash
set -euo pipefail
_init(){
_gitmodules='
[submodule "dependencies/submodules/bash"]
path = dependencies/submodules/bash
url = https://git.savannah.gnu.org/git/bash.git
branch = bash-4.4
shallow = true
[submodule "dependencies/submodules/git"]
path = dependencies/submodules/git
url = https://github.com/git/git
branch = v2.13.0
shallow = true
[submodule "dependencies/submodules/curl"]
path = dependencies/submodules/curl
url = https://github.com/curl/curl
branch = curl-7_54_0
shallow = true
'
_gitmodules=${_gitmodules//$'\t'$'\t'/}
read -r -d '' _gitmodules2yaml <<-'EOF' || true
#!/usr/bin/awk -f
# Original awk by : http://www.linuxquestions.org/questions/programming-9/using-awk-to-get-blocks-of-data-from-a-text-file-4175426447/page2.html#post4784158
BEGIN {
RS = "\\[submodule";
FS = "\n";
block = 0;
YAML_INDENT=" "
}
$1 ~ /"]$/ {
sub(/"]$/,"",$1)
sub(/ "/,"",$1)
gsub(/'/,"''",$1)
if(block==0){print "---\n'submodule':"}
print YAML_INDENT"'"$1"':"
for (i = 2; i <= NF-1; i++) {
gsub(/'/,"''",$i)
sub(/^(\t| )+/,"",$i)
sub(/ = /,"': '",$i)
print YAML_INDENT""YAML_INDENT"'"$i"'"
}
block++;
}
END {
if(block==0) {
print "No submodule found !" > "/dev/stderr"
exit 2
}
}
EOF
}
_main() {
if [ -n "${1-}" ];then
test -f "$1" || { >&2 echo "File '$1' doesn't exist or is not a regular file"; return 2; }
test -r "$1" || { >&2 echo "The read permission is not granted on file '$1'"; return 2; }
awk -f <(echo "$_gitmodules2yaml") $1 || { >&2 echo "An error occured with the awk parser!"; return 2; }
else
echo -e "\nUsage : ./gitmodules2yaml.bash .gitmodules"
echo -e "\n--------------------------------------------"
pr -W"150" -e8 -tm <(echo -e "\n# The .gitmodules exemple\n";echo "$_gitmodules") <(echo -e "\n# The YAML generated\n";awk -f <(echo "$_gitmodules2yaml") <(echo -ne "$_gitmodules")) || { >&2 echo "Error during the paste of the .gitmodulesexemple and the yaml result"; return 2; }
exit 1
fi
}
_init
_main "$@"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment