Skip to content

Instantly share code, notes, and snippets.

@GJivan
Created August 31, 2025 17:06
Show Gist options
  • Select an option

  • Save GJivan/ade79fa0ae275f9d6c9c1a05d05ef14b to your computer and use it in GitHub Desktop.

Select an option

Save GJivan/ade79fa0ae275f9d6c9c1a05d05ef14b to your computer and use it in GitHub Desktop.
Repo Clones
#!/bin/bash
# Exit immediately if a command exits with a non-zero status.
set -e
#################################################################
# CONFIGURE THESE VARIABLES (Should match mirror_version.sh) #
#################################################################
# Your personal GitHub username where the mirrored repos exist.
DEST_USER="your-github-username"
# The base suffix used when creating the repos.
SUFFIX_BASE="-merge-"
# The list of *original* repositories. The script will find all
# numbered versions (e.g., repo-alpha-merge-1, repo-alpha-merge-2) to delete.
REPOS_TO_MIRROR=(
"work-org-A/repo-alpha"
"work-org-A/project-phoenix"
"work-org-B/data-services"
)
#################################################################
# SCRIPT LOGIC (No changes needed below) #
#################################################################
echo ">>> Starting repository cleanup process for user: ${DEST_USER}"
repos_to_delete=()
echo ">>> Searching for all mirrored versions to delete..."
for item in "${REPOS_TO_MIRROR[@]}"; do
BASE_REPO_NAME=$(basename "$item")
# Find all repo names that start with the base name and suffix, followed by a number.
# The `|| true` prevents the script from exiting if grep finds no matches.
matches=$(gh repo list ${DEST_USER} --limit 1000 --json name -q '.[].name' | grep "^${BASE_REPO_NAME}${SUFFIX_BASE}[0-9]\+$" || true)
if [ -n "$matches" ]; then
while IFS= read -r line; do
repos_to_delete+=("$line")
done <<< "$matches"
fi
done
if [ ${#repos_to_delete[@]} -eq 0 ]; then
echo "--- No matching repositories found to delete. All clean!"
exit 0
fi
echo
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "The following ${#repos_to_delete[@]} repositories will be PERMANENTLY DELETED:"
echo "-----------------------------------------------------------------"
for repo in "${repos_to_delete[@]}"; do
echo " - ${DEST_USER}/${repo}"
done
echo "-----------------------------------------------------------------"
echo "This action cannot be undone."
echo
read -p "Are you absolutely sure you want to proceed? (Type 'yes' to confirm): " confirmation
if [ "$confirmation" != "yes" ]; then
echo "Aborted by user. No repositories were deleted."
exit 1
fi
echo
echo ">>> User confirmed. Proceeding with deletion..."
for repo in "${repos_to_delete[@]}"; do
echo "--- Deleting ${DEST_USER}/${repo}..."
# The --yes flag confirms the deletion prompt from gh cli itself.
gh repo delete "${DEST_USER}/${repo}" --yes
done
echo
echo "✅ Cleanup complete. All specified repositories have been deleted."
#!/bin/bash
# Exit immediately if a command exits with a non-zero status.
set -e
#################################################################
# CONFIGURE THESE VARIABLES #
#################################################################
# MANUALLY SET THE VERSION NUMBER FOR THIS RUN.
# Change this number to create a new set of mirrored repos (e.g., 2, 3, etc.)
VERSION_NUMBER=1
# Your personal GitHub username where the new repos will be created.
DEST_USER="your-github-username"
# Set the visibility of the new repository ('private', 'public', or 'internal').
# For work code, 'private' is STRONGLY recommended.
REPO_VISIBILITY="private"
# The base suffix to be added to the repo names.
SUFFIX_BASE="-merge-"
# --- Define the repositories to mirror ---
# Format: "source-organization/repository-name"
REPOS_TO_MIRROR=(
"work-org-A/repo-alpha"
"work-org-A/project-phoenix"
"work-org-B/data-services"
)
#################################################################
# SCRIPT LOGIC (No changes needed below) #
#################################################################
echo ">>> Starting repository mirror process for VERSION: ${VERSION_NUMBER}"
for item in "${REPOS_TO_MIRROR[@]}"; do
# Split "org/repo" into SOURCE_ORG and BASE_REPO_NAME
SOURCE_ORG=$(dirname "$item")
BASE_REPO_NAME=$(basename "$item")
# Construct the destination repo name using the manually set version number
DEST_REPO_NAME="${BASE_REPO_NAME}${SUFFIX_BASE}${VERSION_NUMBER}"
echo "======================================================================"
echo ">>> Processing: '${item}'"
echo " New repo will be named: '${DEST_REPO_NAME}'"
SOURCE_URL="https://github.com/${SOURCE_ORG}/${BASE_REPO_NAME}.git"
DEST_URL="https://github.com/${DEST_USER}/${DEST_REPO_NAME}.git"
TEMP_DIR="${BASE_REPO_NAME}.git"
# 1. Create the new repository.
echo ">>> Step 1: Creating new repository '${DEST_USER}/${DEST_REPO_NAME}'..."
# The if-condition prevents the script from failing if the repo already exists.
if gh repo view "${DEST_USER}/${DEST_REPO_NAME}" > /dev/null 2>&1; then
echo "--- Warning: Repository '${DEST_USER}/${DEST_REPO_NAME}' already exists. Skipping creation."
else
gh repo create "${DEST_USER}/${DEST_REPO_NAME}" --${REPO_VISIBILITY} --description "Personal mirror v${VERSION_NUMBER} of ${item} for pipeline testing."
fi
# 2. Create a bare clone of the source repository.
echo ">>> Step 2: Creating a bare clone of the source..."
rm -rf "${TEMP_DIR}" # Clean up previous run just in case
git clone --bare "${SOURCE_URL}"
# 3. Mirror-push to your new repository.
echo ">>> Step 3: Mirror-pushing to '${DEST_URL}'..."
(cd "${TEMP_DIR}" && git push --mirror "${DEST_URL}")
# 4. Clean up the temporary local directory.
echo ">>> Step 4: Cleaning up..."
rm -rf "${TEMP_DIR}"
echo "✅ Success! '${item}' has been mirrored to '${DEST_USER}/${DEST_REPO_NAME}'"
done
echo
echo "🎉 All repositories for version ${VERSION_NUMBER} have been mirrored successfully!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment