From: bochard Date: Thu, 17 Jul 2025 10:24:42 +0000 (+0800) Subject: first commit X-Git-Url: https://git.bochard.net/?a=commitdiff_plain;h=8f8b9d938b38d1e140a24a228e38f04d004fd62e;p=sync-works.sh.git first commit --- 8f8b9d938b38d1e140a24a228e38f04d004fd62e diff --git a/README.md b/README.md new file mode 100644 index 0000000..2b7d98a --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# sync-works.sh +Update Git submodules on a final directory and removing the `.git` file. + +## process... +1. Read the list of submodules in the `works-list.txt` file. +2. Checks if submodules from the txt file exists within `works-tmp` folder. If not, add it. +3. Update the submodules in their remote latest commit. +4. Copy the submodules from the `works-tmp` directory to the `works` directory while removing `.git` contents simultaneously. +5. Automatically stage and commit the changes. \ No newline at end of file diff --git a/sync-works.sh b/sync-works.sh new file mode 100755 index 0000000..435d855 --- /dev/null +++ b/sync-works.sh @@ -0,0 +1,64 @@ +#!/bin/bash + +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[0;33m' +BLUE='\033[0;34m' +NC='\033[0m' + +declare -A works + +echo -e "${BLUE}Reading works-list.txt file...${NC}"; +while IFS=' ' read -r name url || [ -n "$name" ]; do + works[$name]="$url" +done < works-list.txt + +tmpBase='works-tmp' +worksBase='works' + +mkdir -p "$tmpBase" "$worksBase" + +echo -e "${BLUE}Checking and adding missing submodules in $tmpBase...${NC}" +for work in "${!works[@]}"; do + tmpPath="$tmpBase/$work" + destPath="$worksBase/$work" + + if [ ! -d "$tmpPath" ]; then + echo -e "${BLUE}Adding submodule $work to $tmpBase...${NC}" + git submodule add -f "${works[$work]}" "$tmpPath" + echo -e "${GREEN}Submodule $work added successfully to $tmpPath.${NC}" + else + echo -e "${YELLOW}Submodule $work already exists in $tmpBase.${NC}" + fi +done + +echo -e "${BLUE}Uploading all submodules in $tmpBase to latest remote commits...${NC}" +git submodule update --remote --merge +echo -e "${GREEN}Submodules has been successfully updated to latest remote commits.${NC}" + +echo -e "${BLUE}Synchronizing submodules content from $tmpBase to $worksBase...${NC}" +for work in "${!works[@]}"; do + src="$tmpBase/$work" + dest="$worksBase/$work" + + if [ -d "$src" ]; then + mkdir -p "$dest" + rsync -av --delete --exclude='.git' "$src/" "$dest/" + echo -e "${GREEN}Synchronized submodule $work successfully.${NC}" + else + echo -e "${RED}Warning: $src does not exist, skipping synchronization!${NC}" + fi +done + +echo -e "${BLUE}Staging changes in $worksBase...${NC}" +git add "$worksBase/" +echo -e "${GREEN}Changes staged.${NC}" + +echo -e "${BLUE}Committing changes...${NC}" +if git commit -m "Sync latest updates from submodules to works/ directory."; then + echo -e "${GREEN}Changes committed.${NC}" +else + echo -e "${RED}Nothing to commit.${NC}" +fi + +echo -e "${GREEN}done.${NC}" \ No newline at end of file diff --git a/works-list.txt b/works-list.txt new file mode 100644 index 0000000..da9147f --- /dev/null +++ b/works-list.txt @@ -0,0 +1,3 @@ +temperature-converter git@github.com:b0chard/temperature-converter.git +roman-numeral-converter git@github.com:b0chard/roman-numeral-converter.git +palindrom-checker git@github.com:b0chard/palindrome-checker.git \ No newline at end of file