Copy a lot of repos to Gitea

Github is owned by Microsoft. They have embraced it. They could get a wild hair any old time, then extend and extinguish Github use by non-Windows machines.

I installed Gitea on my Qotom fanless server just to have another place to backup my projects.

I did the Gitea installation based on the Arch Linux Wiki entry. I didn’t find the installation too onerous. I chose PostgreSQL as the database, due to past experience with both MariaDB and PostgreSQL. Installing and configuring PostgreSQL was an interesting side quest.

Claw back all my repos from Github.com

It turns out I have about 161 different repos in my github.com account. That’s too many to clone by hand, I’d make mistakes, miss a few, etc.

A github.com command line client exists that can do this. As an avid Arch Linux user, I got the gh executable by installing the github-cli package with pacman -Sy github-cli.

When gh existed on my laptop, I had to log in: gh auth login.

This causes gh to get Firefox to do some stuff, somehow. I just played along.

I was able to run this script to clone all 161 of my github.com repositories:

#!/usr/bin/env bash
set -eou pipefail

# Retrieve all repos in my github.com account

# gh - Github's command line tool
# "bediger4000" - my github user name
# --limit 4000 is totally arbitrary, default was too small

gh repo list bediger4000 --limit 4000 |
while read -r repo _
do
    echo $(date -Iseconds) "$repo"
    gh repo clone "$repo" "$repo"
done

Create Empty Repos in Gitea

A way to just “push” a repo to Gitea doesn’t exist. You have to create an empty repo of the correct name, set some git options, then push the repo to Gitea.

Another side quest: obtaining an authorization token. There’s lots of complicated ways to do this, but since I was self-hosting, I used Gitea’s web server.

The usual weak and lazy language on the official Gitea web page docs says: Settings | Applications | Generate New Token. Which settings, you lazy so-and-so?

I found that “Settings” menu item by clicking my avi in the upper right hand corner of my Gitea profile page. After the “Settings” page loads, I found “Applications” as a tomfool flat design button on the left hand side of the screen, in the middle of a column of such flat design buffoonery. Clicking the “Applications” zone earns you a page where you can find “Generate New Token”. The token is a string of 40 lowercase ASCII letters and digits.

I ran this script to create empty repos in Gitea on my Qotom fanless server:

#!/usr/bin/env bash
set -eou pipefail

MY_TOKEN="a8bc0163d6b457e2ff3150dc7ac5e2090cad0d87"

while read REPO
do
    echo STARTING $(date -Iseconds) $REPO
curl \
  -X POST "http://modest4:3000/api/v1/user/repos" \
  -H "accept: application/json" \
  -H "Authorization: token $MY_TOKEN" \
  -H "Content-Type: application/json" \
  -d "{\"name\": \"$REPO\"}" \
  -i
    echo DONE $(date -Iseconds) $REPO
    echo
done << END_REPOS
9board
GoKilo
NudeDetectorPHP
Self-replicating-go
...
zeros-to-end
zigzag-programming-problem
END_REPOS

I find “here documents” a lot more obvious than having another file, or a large number of items in for REPO in many many other strings... style for-loops.

Push the Repos into Gitea

I used the following script to set every default branch to “main” (some of these repos are pretty dang old), and push them to Gitea.

#!/usr/bin/env bash
set -eou pipefail

DIR=$PWD

while read REPO
do
    echo STARTING $(date -Iseconds) $REPO
    echo $REPO > /dev/tty
    cd bediger4000/$REPO

    git branch -M main
    git remote add neworigin "gitea@modest4:bediger4000/$REPO.git"
    git push neworigin --all
    git push neworigin --tags

    cd $DIR
    echo DONE $(date -Iseconds) $REPO
    echo
done << END_REPOS
9board
GoKilo
NudeDetectorPHP
Self-replicating-go
...
zeros-to-end
zigzag-programming-problem
END_REPOS

This took quite a while to run, some minutes, but at the end, my Gitea instance had all my Github repos in it.

I haven’t used Gitea much, but it looks like a decent clone of Github.