blob: 521576e80411ff35939ab6171a101c3a6eaed0a4 (
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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
#!/bin/bash
# Copyright © 2014, 2016 Luke Shumaker <lukeshu@sbcglobal.net>
#
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See the COPYING file for more details.
# Depends on the 'gitget' and 'libremessages' commands. On Parabola
# GNU/Linux-libre, those are the 'gitget' and 'librelib' packages,
# respectively.
#
# For other systems, they both live at:
# https://git.parabola.nu/packages/libretools.git/
set -o pipefail
set -e
. libremessages
usage() {
print 'Usage %s CONFIG-FILE' "${0##*/}"
}
main() {
if [[ $# != 1 ]]; then
usage
exit
fi
declare -g cfg_file="$1"
local r=0
while read -r repo; do
handle-repo "$repo" || r=$?
done < <(cfg-list-repos)
return $r
}
handle-repo() {
[[ $# == 1 ]] || panic
local repo=$1
local local url upstream downstreams downstream r=0
# read configuration
local="$(cfg-get "repo.$repo.local")"
url="$(cfg-get "repo.$repo.url")" || true
upstream="$(cfg-get "repo.$repo.upstream")" || true
downstreams=($(cfg-get-all "repo.$repo.downstream")) || true
# download
if [[ -n "$upstream" ]]; then
download "$upstream" "$local"
fi
# ensure that $local exists
test -f "$local"/HEAD
# upload
for downstream in "${downstreams[@]}"; do
upload "$url" "$local" "$downstream" || r=$?
done
return $r
}
download() {
[[ $# == 3 ]] || panic
local repo=$1
local remote=$2
local local=$3
# download the repository
local url
url="$(remote "$remote" pull-url)"
gitget -f -n "$repo" "$url" "$local"
# download the metadata
remote "$remote" get-meta > "$local/git-mirror.tmp"
git config --file "$local/config" --rename-section git-mirror git-mirror-bak
local IFS='='
while read -r key val; do
git config --file "$local/config" --add git-mirror."$key" "$val"
done < "$local/git-mirror.tmp"
rm -f "$local/git-mirror.tmp"
git config --file "$local/config" --remove-section git-mirror-bak
}
upload() {
[[ $# == 3 ]] || panic
local clonable_url=$1
local local=$2
local remote=$3
# push metadata
{
printf '%q ' set-meta "${remote#*:}"
[[ -z "$clonable_url" ]] || printf '%q ' "mirror=$clonable_url"
git config --file "$local/config" --get-regexp '^git-mirror[.]' -z|sed -z 's/ /=/'|xargs -0r printf '%q '
} | account "${remote%%:*}"
# push repository
local repo_mode
repo_mode=$(remote "$remote" repo-mode)
if [[ $repo_mode == passive ]]; then
local push_url
push_url="$(remote "$remote" push-url)"
cd "$local" && git push --mirror "$push_url"
fi
}
# Spawn an 'account.type' helper. It will read commands from stdin.
account() {
[[ $# == 1 ]] || panic
local account=$1
local account_type
account_type="$(cfg-get "account.$account.type")"
{
cfg --list -z|sed -zn "s=^account[.]$account[.]=config =p"|grep -z -v '^type='|xargs -r0 printf '%s\n'
cat
} | git mirror-"$type" "$account"
}
# `account` is awkward to use; so let's wrap it.
remote() {
[[ $# > 1 ]] || panic
local remote=$1
[[ $remote = *:* ]]
local account="${remote%%:*}"
local path="${remote#*:}"
printf '%q ' "$2" "$path" "${@:3}" | account "$account"
}
cfg() {
git config --file "$cfg_file" "$@"
}
cfg-get() {
[[ $# == 1 ]] || panic
cfg --get-all "$1"
}
cfg-get-all() {
[[ $# == 1 ]] || panic
cfg --get-all "$1"
}
cfg-list-repos() {
[[ $# == 0 ]] || panic
cfg --name-only --get-regexp '^repo[.].*[.].*$' -z|sed -z -e 's|^repo[.]||' -e 's|[.][^.]*$||'|sort -zu|xargs -r0 printf '%s\n'
}
main "$@"
|