aboutsummaryrefslogtreecommitdiff
path: root/bin/mirror
blob: c8c0bdc64dc52cc2a4317a2b48775e73576eea8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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"