summaryrefslogtreecommitdiff
path: root/git-shell-commands/create-mirror
blob: 8d2129cd4e34e35e66cfd4248c9ebcb460a6f823 (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
#!/bin/bash

set -e

if test "${#}" -ne 1; then
    printf "%s\n" "usage: $(basename $0) https://url/repo"
    exit 1
fi

REPO="${1}"
REPO_NAME=$(basename "${REPO}")
REPO_DIR="/srv/git/repos/${REPO_NAME}.git"

if test -d "${REPO_DIR}"; then
    printf "%s\n" "[err] repository ${REPO_NAME}.git already exists at ${REPO_DIR}"
    exit 1
fi

TMP_DIR=$(mktemp -d -p /tmp)
trap 'rm -rf "$TMP_DIR"' EXIT

printf "%s\n" "[inf] cloning upstream repo"
git clone --mirror --quiet "${REPO}" "${TMP_DIR}"

printf "%s\n" "[inf] creating bare fork repository"
mkdir -p "${REPO_DIR}"
git init --bare --quiet "${REPO_DIR}"

cat > "${REPO_DIR}/hooks/post-receive" << "EOL"
#!/bin/sh

agefile="$(git rev-parse --git-dir)"/info/web/last-modified

mkdir -p "$(dirname "$agefile")" &&
git for-each-ref \
        --sort=-authordate --count=1 \
        --format='%(authordate:iso8601)' \
        >"$agefile"
EOL
chmod +x "${REPO_DIR}/hooks/post-receive"

printf "%s\n" "[inf] pushing to bare repo"
git --git-dir="${TMP_DIR}" push --mirror --quiet "${REPO_DIR}"

printf "%s\n" "[inf] configuring bare repo"
git --git-dir="${REPO_DIR}" config core.logallrefupdates true
git --git-dir="${REPO_DIR}" config cgit.section "03. mirrors"
git --git-dir="${REPO_DIR}" remote add upstream "${REPO}"

chown -R git:git "${REPO_DIR}"
chmod -R g-w "${REPO_DIR}"

printf "%s\n" "[inf] mirrored ${REPO}"