Skip to content

Instantly share code, notes, and snippets.

@BernhardRode
Created December 2, 2024 15:43
Show Gist options
  • Save BernhardRode/a91d809d629acc2a4db612dde477ee1f to your computer and use it in GitHub Desktop.
Save BernhardRode/a91d809d629acc2a4db612dde477ee1f to your computer and use it in GitHub Desktop.
#!/bin/bash
# Configuration
GERRIT_BASE_URL="https://gerrit.example.com"
GITHUB_ORG="your-github-org"
GITHUB_TOKEN="your-personal-access-token"
REPO_LIST="repos.txt" # A file containing the list of Gerrit repositories
TEMP_DIR="/tmp/gerrit-to-github"
# Check dependencies
if ! command -v git &>/dev/null; then
echo "Error: git is not installed."
exit 1
fi
if ! command -v curl &>/dev/null; then
echo "Error: curl is not installed."
exit 1
fi
# Prepare temporary directory
mkdir -p "$TEMP_DIR"
cd "$TEMP_DIR" || exit
# Process each repository
while IFS= read -r repo_name; do
if [ -z "$repo_name" ]; then
continue
fi
echo "Migrating repository: $repo_name"
# Clone the Gerrit repository
git clone --mirror "$GERRIT_BASE_URL/$repo_name" "$repo_name.git"
if [ $? -ne 0 ]; then
echo "Error: Failed to clone $repo_name. Skipping."
continue
fi
cd "$repo_name.git" || exit
# Check for and initialize submodules
if git ls-tree -r HEAD | grep -q '^160000'; then
echo "Submodules detected in $repo_name. Cloning submodules."
# Convert the repo from a bare mirror to a working directory
git config --unset core.bare
git checkout -f
# Update submodules
git submodule update --init --recursive
if [ $? -ne 0 ]; then
echo "Error: Failed to initialize submodules for $repo_name. Skipping."
cd ..
rm -rf "$repo_name.git"
continue
fi
fi
# Create the GitHub repository
curl -s -H "Authorization: token $GITHUB_TOKEN" \
-d '{"name":"'$repo_name'","private":true}' \
"https://api.github.com/orgs/$GITHUB_ORG/repos"
if [ $? -ne 0 ]; then
echo "Error: Failed to create GitHub repository $repo_name. Skipping."
cd ..
rm -rf "$repo_name.git"
continue
fi
# Push to GitHub, including submodules
git remote add github "https://$GITHUB_TOKEN@github.com/$GITHUB_ORG/$repo_name.git"
git push --mirror github
if [ $? -ne 0 ]; then
echo "Error: Failed to push $repo_name to GitHub."
cd ..
rm -rf "$repo_name.git"
continue
fi
echo "Successfully migrated $repo_name to GitHub."
cd ..
rm -rf "$repo_name.git"
done < "$REPO_LIST"
# Cleanup
echo "Migration completed. Temporary files removed."
cd ~
rm -rf "$TEMP_DIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment