From 3d07b952f7789ac783d90e3654e3c43bbfddd7c5 Mon Sep 17 00:00:00 2001 From: bochard Date: Sat, 5 Jul 2025 21:24:42 +0800 Subject: [PATCH] bash script to automate getting content from submodules --- .gitignore | 4 +++- sync-works.sh | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100755 sync-works.sh diff --git a/.gitignore b/.gitignore index 445383f..7dd3715 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -robots.txt \ No newline at end of file +robots.txt +works-tmp/ +.gitmodules \ No newline at end of file diff --git a/sync-works.sh b/sync-works.sh new file mode 100755 index 0000000..53e407d --- /dev/null +++ b/sync-works.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +declare -A submodules=( + ['roman-numeral-converter']='git@github.com:b0chard/roman-numeral-converter.git' + ['palindrome-checker']='git@github.com:b0chard/palindrome-checker.git' + ['temperature-converter']='git@github.com:b0chard/temperature-converter.git' +) + +tmpBase='works-tmp' +worksBase='works' + +mkdir -p "$tmpBase" "$worksBase" + +echo "Checking and adding missing submodules in $tmpBase..." +for submodule in "${!submodules[@]}"; do + tmpPath="$tmpBase/$submodule" + destPath="$worksBase/$submodule" + + if [ ! -d "$tmpPath" ]; then + echo "Adding submodule $submodule to $tmpBase..." + git submodule add -f "${submodules[$submodule]}" "$tmpPath" + echo "Submodule $submodule added successfully to $tmpPath." + else + echo "Submodule $submodule already exists in $tmpBase." + fi +done + +echo "Uploading all submodules in $tmpBase to latest remote commits..." +git submodule update --remote --merge +echo "Submodules has been successfully updated to latest remote commits." + +echo "Synchronizing submodules content from $tmpBase to $worksBase..." +for submodule in "${!submodules[@]}"; do + src="$tmpBase/$submodule" + dest="$worksBase/$submodule" + + if [ -d "$src" ]; then + mkdir -p "$dest" + rsync -av --delete --exclude='.git' "$src/" "$dest/" + echo "Synchronized submodule $submodule successfully." + else + echo "Warning: $src does not exist, skipping synchronization!" + fi +done + +echo "Staging changes in $worksBase..." +git add "$worksBase/" + +echo "Committing changes..." +git commit -m "Sync latest updates from submodules to works/ directory." || echo "Nothing to commit." + +echo "done." \ No newline at end of file -- 2.39.5