Created
April 11, 2022 20:20
-
-
Save curtisbelt/150797f47aa03d7a656631270bca7346 to your computer and use it in GitHub Desktop.
Refresh All Premier Repos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Clones/refreshes repositories for VIP premier sites (production environments) | |
# Dependencies: jq, git, vipgo | |
vipgo api GET "/sites?pagesize=550&support_package=enterprise%20support&environment_name=production" > ./premier-production-environments.json | |
sites=$( jq -cr '.data[] | @base64' ./premier-production-environments.json ) | |
for site in ${sites}; do | |
echo "" | |
site=$(echo "${site}" | base64 --decode) | |
client_site_id=$( echo "${site}" | jq -r '.client_site_id' ) | |
repository=$( echo "${site}" | jq -r '.source_repo' ) | |
repository=${repository#wpcomvip/} | |
repository_dir="$HOME/code/github.com/wpcomvip/${repository}" | |
branch=$( echo "${site}" | jq -r '.source_repo_branch' ) | |
cli-echo() { | |
echo "$1 [${client_site_id}]" | |
} | |
if ! [ -d "${repository_dir}/.git" ]; then | |
cli-echo "Cloning wpcomvip/${repository} #${branch}" | |
# ℹ️ Delete directory to avoid "Fatal: destination path already exists and is not an empty directory." | |
rm -rf "${repository_dir}" | |
# --------- CLONE -------------------------------------------------------------------------------------------------- | |
git clone --recurse-submodules -b ${branch} [email protected]:wpcomvip/${repository}.git ${repository_dir} | |
else | |
cd "${repository_dir}" || { | |
echo "${repository_dir} somehow didn't exist" && exit | |
} | |
cli-echo "Refreshing wpcomvip/${repository} #${branch}" | |
# --------- FETCH -------------------------------------------------------------------------------------------------- | |
git fetch | |
# --------- CHECKOUT ----------------------------------------------------------------------------------------------- | |
# ⚠️ Do not use --recurse-submodule here, submodule discrepencies will cause fatal error. | |
# See https://lore.kernel.org/git/[email protected]/T/ | |
git switch --discard-changes "${branch}" | |
# --------- RESET -------------------------------------------------------------------------------------------------- | |
# ✅ Forcefully resets tracked files on superproject + initialized submodules | |
# ❌ Does NOT delete untracked files/directories | |
# ❌ Does NOT initialize new submodules found in .gitmodules | |
# ℹ️ @{u} is shorthand for "current branch from upstream" | |
# ⚠️ Must run first without --recurse-submodules, or submodule discrepencies will cause fatal error. | |
# See https://lore.kernel.org/git/[email protected]/T/ | |
git reset --hard '@{u}' | |
git reset --recurse-submodules --hard '@{u}' | |
# --------- UPDATE SUBMODULES -------------------------------------------------------------------------------------- | |
# ✅ Initializes .git/config with new submodules found in .gitmodules | |
# ✅ Updates initialized submodules to match what the superproject expects by: | |
# - cloning missing submodules | |
# - fetching missing commits in submodules | |
# - updating the working tree of the submodules | |
# ❌ Does NOT delete untracked files/directories | |
git submodule update --init --force --recursive --jobs 4 | |
# --------- DELETE UNTRACKED --------------------------------------------------------------------------------------- | |
# ✅ Deletes ALL untracked files/directories on superproject + initialized submodules | |
# ℹ️ -x Allow deletion of untracked files regardless of gitignore rules. | |
# ℹ️ -d Allow recurse into untracked directories | |
# ℹ️ -f Allows deletion of files/directories, except for directories with a .git subdirectory | |
# ℹ️ -f Using this option twice allows deletion of directories with a .git subdirectory | |
git clean -xdff && git submodule foreach --recursive git clean -xdff | |
fi | |
done #sites |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment