aboutsummaryrefslogtreecommitdiffstats
path: root/roles/jails/22_forge/files/git-mirrors.sh
blob: 8388d246dbd5bbb3219d467d527925da7288b934 (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
#!/bin/sh
# Run hourly via cron.
# Handles mirror-to (push) and mirror-from (fetch) remotes for all repos.

REPOS_DIR="/var/db/repos"
LOG="/var/log/git-mirrors.log"
export GIT_SSH_COMMAND="ssh -i /var/db/repos/.ssh/forge_deploy -F /var/db/repos/.ssh/config -o IdentitiesOnly=yes"
export GIT_CONFIG_GLOBAL="/var/db/repos/.gitconfig"

echo "$(date) Starting mirror run" >> "$LOG"

for repo in "$REPOS_DIR"/*.git "$REPOS_DIR"/.*.git; do
    [ -d "$repo" ] || continue
    name="$(basename "$repo" .git)"

    # mirror-to: push all refs to remote
    if git -C "$repo" remote get-url mirror-to >/dev/null 2>&1; then
        remote=$(git -C "$repo" remote get-url mirror-to)
        echo "$(date) [push] $name -> $remote" >> "$LOG"
        if git -C "$repo" push --mirror mirror-to >> "$LOG" 2>&1; then
            echo "$(date) [push] OK $name" >> "$LOG"
        else
            echo "$(date) [push] FAILED $name" >> "$LOG"
        fi
    fi

    # mirror-from: fetch all refs from remote
    if git -C "$repo" remote get-url mirror-from >/dev/null 2>&1; then
        remote=$(git -C "$repo" remote get-url mirror-from)
        echo "$(date) [fetch] $name <- $remote" >> "$LOG"
        if git -C "$repo" fetch --prune mirror-from '+refs/*:refs/*' >> "$LOG" 2>&1; then
            echo "$(date) [fetch] OK $name" >> "$LOG"
        else
            echo "$(date) [fetch] FAILED $name" >> "$LOG"
        fi
    fi
done

echo "$(date) Mirror run complete" >> "$LOG"