~janbaudisch/git-mirror

ref: fb8cb0de8997818865a99597bccc3671e96b2e71 git-mirror/git-mirror -rwxr-xr-x 2.3 KiB
fb8cb0deJan Baudisch readme: add systemd/cron prerequisite 2 years ago
                                                                                
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/sh

help() {
    echo "git-mirror"
    echo ""
    echo "A tool for mirroring a remote git repository to another one."
    echo ""
    echo "Usage:"
    echo "    git-mirror setup [path] [primary] [mirror]"
    echo "        Sets up a new folder for mirroring a remote git repository."
    echo "        path - The path where the primary repository should be cloned. Must not exist."
    echo "        primary - git URL for the primary git repository (the one to be mirrored)."
    echo "        mirror - git URL for the repository to be mirrored to. Must already exist and be writable from the host."
    echo ""
    echo "    git-mirror update [path]"
    echo "        Updates the mirror repository with the primary. Only works if the given directory was set up with this tool."
    echo "        path - The directory which was set up using the setup command."
}

check_git() {
    command -v git >/dev/null 2>&1 || {
        echo >&2 "Could not find git. Please make sure it is installed and included in your path."
        exit 1
    }
}

setup() {
    path=$1
    primary=$2
    mirror=$3

    [ -z "$path" ] && echo >&2 "No path given." && exit 1
    [ -z "$primary" ] && echo >&2 "No primary repository given." && exit 1
    [ -z "$mirror" ] && echo >&2 "No mirror repository given." && exit 1

    [ -d "$path" ] && echo >&2 "The given path (${path}) already exists." && exit 1

    check_git

    mkdir -p $path

    echo "Cloning primary ..."
    git clone --mirror $primary $path

    echo ""
    echo "Adding mirror remote ..."
    $(cd $path && git remote add --mirror=fetch mirror $mirror) || exit $?

    echo ""
    echo "Mirror directory set up."

    echo ""
    echo "Running update ..."

    echo ""

    update $1
}

update() {
    path=$1

    [ -z "$path" ] && echo >&2 "No path given." && exit 1

    [ ! -d "$path" ] && echo >&2 "The given path (${path}) does not exist." && exit 1

    check_git

    echo "Fetching primary ..."
    $(cd $path && git fetch origin) || exit $?

    echo ""
    echo "Pushing to mirror ..."
    $(cd $path && git push mirror --all --follow-tags) || exit $?

    echo ""
    echo "Mirror up to date."
}

case "$1" in
("setup")
    setup $2 $3 $4
    ;;

("update")
    update $2
    ;;

("help")
    help
    ;;

*)
    help
    exit 1
    ;;
esac