diff options
| author | Bobby <[email protected]> | 2025-11-19 17:42:53 +0530 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-11-19 17:42:53 +0530 |
| commit | 1c3f97cc2a3115e38235f4ddd940be63c4363d68 (patch) | |
| tree | c530cd353c9cb0db7eb120a94be59ca5d899fe5a /bin/mirror | |
| parent | 61f3d6ee6f3740b394e43b9f963cb3703b588709 (diff) | |
| download | cgitconf-1c3f97cc2a3115e38235f4ddd940be63c4363d68.tar.xz cgitconf-1c3f97cc2a3115e38235f4ddd940be63c4363d68.zip | |
Add script to mirror GitHub repositories
This script creates a mirror of a GitHub repository, checks for necessary credentials, and fetches repository metadata.
Diffstat (limited to 'bin/mirror')
| -rw-r--r-- | bin/mirror | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/bin/mirror b/bin/mirror new file mode 100644 index 0000000..c8c0bdc --- /dev/null +++ b/bin/mirror @@ -0,0 +1,83 @@ +#!/bin/bash + +# Directory where mirrors are stored +REPO_DIR="/root/shifoogit/repos" +CREDS_FILE="/root/shifoogit/.git-creds/credentials" + +# ---- Check input ---- +if [ -z "$1" ]; then + echo "Usage: mirror <github-url>" + exit 1 +fi + +URL="$1" + +# ---- Check GitHub Token ---- +if [ -z "$GITHUB_TOKEN" ]; then + echo "Error: GITHUB_TOKEN not set." + echo "Run: export GITHUB_TOKEN=ghp_xxxxxxxxxxxxx" + exit 1 +fi + +# ---- Ensure credentials file exists ---- +if [ ! -f "$CREDS_FILE" ]; then + echo "ERROR: Credentials file not found at:" + echo " $CREDS_FILE" + echo + echo "Create it with:" + echo " mkdir -p /root/shifoogit/.git-creds" + echo " echo \"https://USERNAME:[email protected]\" > $CREDS_FILE" + echo " chmod 600 $CREDS_FILE" + exit 1 +fi + +# Git should use the custom credentials file +git config --global credential.helper "store --file=$CREDS_FILE" + +# ---- Extract owner + repo ---- +OWNER=$(echo "$URL" | sed -E 's#https?://github.com/([^/]+)/.*#\1#') +REPO_NAME=$(basename "$URL" .git) +TARGET="$REPO_DIR/${REPO_NAME}" + +# ---- Check for existing mirror ---- +if [ -d "$TARGET" ]; then + echo "Error: Mirror already exists: $TARGET" + exit 1 +fi + +echo "Mirroring $URL ..." + +# Create mirror as UID 1000 +setpriv --reuid 1000 --regid 1000 --clear-groups \ + git clone --mirror "$URL" "$TARGET" + +if [ $? -ne 0 ]; then + echo "❌ Failed to mirror $URL" + exit 1 +fi + +# ---- Fetch metadata from GitHub ---- +API_URL="https://api.github.com/repos/$OWNER/$REPO_NAME" +JSON=$(curl -s -H "Authorization: token $GITHUB_TOKEN" "$API_URL") + +DESC=$(echo "$JSON" | jq -r '.description // "No description provided."') + +# ---- Write repo description ---- +echo "$DESC" > "$TARGET/description" +chown 1000:1000 "$TARGET/description" + +# ---- Write last-modified using latest commit timestamp ---- +LAST_COMMIT=$(git -C "$TARGET" log -1 --format="%ci") + +mkdir -p "$TARGET/info/web" +echo "$LAST_COMMIT" > "$TARGET/info/web/last-modified" + +chown -R 1000:1000 "$TARGET/info" + +echo +echo "✔ Mirror created successfully" +echo "✔ Repo metadata written" +echo "✔ last-modified generated: $LAST_COMMIT_DATE" +echo +echo "View in CGit:" +echo " https://git.shi.foo/$REPO_NAME" |
