diff --git a/git-mirror b/git-mirror new file mode 100755 index 0000000..c0f4e8d --- /dev/null +++ b/git-mirror @@ -0,0 +1,83 @@ +#!/bin/bash +# Directory containing your local git repositories +SOURCE_DIR="$(realpath "data/small/gitea/git/repositories/sean")" +# Your GitHub username +THIRD_PARTY_GIT_USER="seanhly" +# Your personal access tokens +GITHUB_TOKEN="$(pass github-token)" +GITLAB_TOKEN="$(pass gitlab-token)" +CODEBERG_TOKEN="$(pass codeberg-token)" + +cd "$SOURCE_DIR" || exit 1 + +for dir in *; do + # Handle non-bare repos (with .git subdir) + if [ -d "$dir/.git" ]; then + REPO_NAME="${dir%/}" + REPO_PATH="$dir" + # Handle bare repos (ending in .git) + elif [ -d "$dir" ] && [[ "$dir" == *.git ]]; then + REPO_NAME="${dir%.git}" + REPO_PATH="$dir" + else + continue + fi + + echo "Processing $REPO_NAME..." + + # --- GitHub --- + # Create repo on GitHub (if it doesn't exist) + curl -s -u "$THIRD_PARTY_GIT_USER:$GITHUB_TOKEN" \ + -X POST \ + -H "Accept: application/vnd.github+json" \ + https://api.github.com/user/repos \ + -d "{\"name\":\"$REPO_NAME\", \"private\":true}" \ + | grep -q '"full_name"' && echo "Created $REPO_NAME on GitHub." + + cd "$REPO_PATH" || continue + git config --global --add safe.directory "$PWD" + # Set up mirror remote and push to GitHub + git remote remove github-mirror 2>/dev/null + git remote add github-mirror "git@github.com:$THIRD_PARTY_GIT_USER/$REPO_NAME.git" + git push --mirror github-mirror + + # --- GitLab --- + # Check if GitLab repo exists + GITLAB_API="https://gitlab.com/api/v4" + GITLAB_REPO_PATH="$THIRD_PARTY_GIT_USER/$REPO_NAME" + GITLAB_REPO_SSH="git@gitlab.com:$THIRD_PARTY_GIT_USER/$REPO_NAME.git" + + echo Checking if $REPO_NAME exists on GitLab... + REPO_EXISTS=$(curl -s --header "PRIVATE-TOKEN: $GITLAB_TOKEN" \ + "$GITLAB_API/projects/$( + echo -n "$GITLAB_REPO_PATH" | sed 's/\//%2F/g' + )" | grep -c '"id"' + ) + echo "Repository exists check returned: $REPO_EXISTS" + if [ "$REPO_EXISTS" -eq 0 ]; then + echo "Creating $REPO_NAME on GitLab..." + curl -s --header "PRIVATE-TOKEN: $GITLAB_TOKEN" -X POST "$GITLAB_API/projects" \ + --data "name=$REPO_NAME&visibility=private" | grep -q '"id"' && echo "Created $REPO_NAME on GitLab." + fi + # Set up mirror remote and push to GitLab + git remote remove gitlab-mirror 2>/dev/null + git remote add gitlab-mirror "$GITLAB_REPO_SSH" + git push --prune gitlab-mirror 'refs/heads/*:refs/heads/*' 'refs/tags/*:refs/tags/*' + # --- Codeberg --- + CODEBERG_API="https://codeberg.org/api/v1" + CODEBERG_REPO_SSH="git@codeberg.org:$THIRD_PARTY_GIT_USER/$REPO_NAME.git" + echo Checking if $REPO_NAME exists on Codeberg... + CB_REPO_EXISTS=$(curl -s -H "Authorization: token $CODEBERG_TOKEN" "$CODEBERG_API/repos/$THIRD_PARTY_GIT_USER/$REPO_NAME" | grep -c '"id"') + echo "Repository exists check returned: $CB_REPO_EXISTS" + if [ "$CB_REPO_EXISTS" -eq 0 ]; then + echo "Creating $REPO_NAME on Codeberg..." + curl -s -H "Authorization: token $CODEBERG_TOKEN" -X POST "$CODEBERG_API/user/repos" \ + -H "Content-Type: application/json" \ + -d '{"name": "'$REPO_NAME'", "private": true}' | grep -q '"id"' && echo "Created $REPO_NAME on Codeberg." + fi + # Set up mirror remote and push to Codeberg + git remote remove codeberg-mirror 2>/dev/null + git remote add codeberg-mirror "$CODEBERG_REPO_SSH" + git push --prune codeberg-mirror 'refs/heads/*:refs/heads/*' 'refs/tags/*:refs/tags/*' + cd "$SOURCE_DIR" +done