forked from mirror/mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_mirror.sh
118 lines (99 loc) · 2.37 KB
/
git_mirror.sh
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
# Repository mirrorer
# © John Peterson. License GNU GPL 3.
function create_repo() {
name=$1
description=$2
# create repo
json=$(curl -s -u "$user:$pwd" -d "{\"name\":\"$name\"}" https://api.github.com/orgs/$org/repos)
error=$(echo "$json" | jshon -Q -e message -u)
if [ -n "$error" ]; then
echo $error;
return 1
fi
# set description
json=$(curl -s -u "$user:$pwd" -X PATCH -d "{\"name\":\"$name\", \"description\":\"$description\"}" https://api.github.com/repos/$org/$name)
}
function git_mirror() {
# vars
TO=(${1//\// });
to=${TO[0]}
branch=${TO[1]}
type=$2
from=$3
arg=$4
dir=$REPO_HOME/$to
# exceptions
if [ -z "$REPO_HOME" ]; then echo "REPO_HOME not set"; return 1; fi
if [ ! -d "$REPO_HOME" ]; then echo "$REPO_HOME doesn't exist"; return 1; fi
# create boolean
isbzr=`if [[ "$type" =~ "bzr" ]]; then printf true; else printf false; fi`
isgit=`if [[ "$type" =~ "git" ]]; then printf true; else printf false; fi`
issvn=`if [[ "$type" =~ "svn" ]]; then printf true; else printf false; fi`
# create repo
json=$(curl -s https://api.github.com/repos/$org/$to)
error=$(echo "$json" | jshon -Q -e message -u)
if [ -n "$error" ] && [[ "$error" != "API rate limit exceeded"* ]]; then
echo $error;
if ! create_repo $to "$from $arg"; then
return 1
fi
fi
# push command
if [ -n "$branch" ]; then
dir=${dir}_$branch
m="push origin HEAD:$branch"
else
if $isbzr; then
m="push --tags origin HEAD:master"
elif $issvn; then
m="push"
else
m="push --mirror"
fi
fi
# log
echo ►$to $(date -Is)
# pull command
case "$type" in
bzr) c="bzr clone $from ."
p='bzr sync bzr/master';;
cvs) c="cvsimport -i -d $from $arg"
p="$c";;
git) c="clone --mirror $from ."
p='remote update';;
hg) c="hg clone $from ."
p='hg pull';;
svn) c="svn clone $arg $from ."
p="svn rebase";;
*) echo "$type is unsupported"; return 1;;
esac
if $isgit; then
r='set-url --push'
else
r='add'
fi
set -x
# clone
if [[ ! -d $dir || ! ($isgit && -f $dir/HEAD || !$isgit && -f $dir/.git/HEAD) ]]; then
mkdir -p $dir; pushd $dir
git $c
# select branch
if $isbzr; then
git checkout bzr/master
fi
else
pushd $dir
fi
# sync
git $p
git gc --prune=now
git remote $r origin git@github.com:$org/$to.git
git $m
if [[ -n $gitlab_org ]]; then
git remote $r origin git@gitlab.com:$gitlab_org/$to.git
git $m
fi
set +x
popd
}