Created
May 11, 2025 22:39
-
-
Save mzpqnxow/d357338c9f6b584977279c869ff76d23 to your computer and use it in GitHub Desktop.
Migrate github.com URL to a gogs instance
This file contains hidden or 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 | |
# Hacky one-shot github2gogs migration | |
# | |
# Needs minor modifications if you want to push to a user rather than an organization | |
# or if your gogs gitconfig doesn't have r/w ssh set up | |
# | |
# | |
# Also, create a token in Gogs->Profile->Settings->Application and set it | |
# using: | |
# $ export GOGS_TOKEN=<insert token here> | |
# | |
# This will mirror the github repo locally, create an empty repo in the | |
# gogs instance under the organization "github" (which must already exist | |
# and be writable by your user) and then pushes the branches to gogs | |
# | |
# Some notes in-line if you need to modify | |
# | |
# - mzpqnxow | |
# | |
# | |
# As-is, the command: | |
# $ gh2gogs https://github.com/bah/lol123 | |
# | |
# Will do the following: | |
# - clone the repo from github | |
# - create a repo under the organization $GOGS_ORG ("github") | |
# - sync the local github repo to the new gogs repo | |
# | |
# It's sloppy, you can fix it yourself :> | |
# | |
set -euo pipefail | |
echo $# | |
if [ "$#" -ne 1 ] && [ "$#" -ne 2 ]; then | |
echo "Usage: $0 <github_repo_url> [repo_name]" >&2 | |
echo "Example: $0 [email protected]:alice/project.git project" >&2 | |
exit 1 | |
fi | |
# Replace both with your gogs username if you want to push to | |
# your username instead of a gogs org (which must be created already) | |
# but also replace the create_repo_url below ... | |
declare GOGS_ENTITY=github | |
declare GH_REPO="$1" | |
declare REPO_NAME="${2:-$(basename "$GH_REPO")}" | |
# Replace with info specific to the gogs server | |
declare GOGS_HTTP_API=https://gogs.lan:12345/api/v1 | |
declare GOGS=ssh://gogs.lan | |
declare GOGS_RW_URL="$GOGS/$GOGS_ENTITY/$REPO_NAME" | |
# Step 1: Create repo on Gogs via API | |
echo "Creating repository '$REPO_NAME' for org/user '$GOGS_ENTITY' on Gogs..." | |
# If you want to push to a user rather than an org: | |
# create_repo_url="$GOGS_HTTP_API/user/repos" | |
# To push to an org: | |
create_repo_url="$GOGS_HTTP_API/org/$GOGS_ENTITY/repos" | |
echo "Will create repo @ $create_repo_url ..." | |
curl -kvv -sf -X POST "$create_repo_url" \ | |
-H "Content-Type: application/json" \ | |
-H "Authorization: token $GOGS_TOKEN" \ | |
-d "{\"name\":\"$REPO_NAME\", \"private\":false, \"auto_init\":false}" || echo "Failed to create, it exists already or you need to change $create_repo_url !!" | |
declare TMP_DIR="$(mktemp -d)" | |
trap 'rm -rf "$TMP_DIR"' EXIT | |
cd "$TMP_DIR" | |
echo "Cloning from GitHub $GH_REPO" | |
git clone --mirror "$GH_REPO" repo.git | |
cd repo.git | |
echo "Pushing to Gogs ($GOGS_RW_URL) ..." | |
git push --mirror "$GOGS_RW_URL" | |
echo "Done migrating $GH_REPO ..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment