-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
borg.sh
executable file
·252 lines (218 loc) · 6.68 KB
/
borg.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/bin/sh
# Copyright (C) 2016-2024 Jonas Bernoulli
#
# Author: Jonas Bernoulli <[email protected]>
# SPDX-License-Identifier: GPL-3.0-or-later
script=$(basename $0)
USAGE="$script clone [<drone>...]
or: $script checkout [--reset-hard] [<drone>...]"
OPTIONS_SPEC=
SUBDIRECTORY_OK=Yes
. "$(git --exec-path)/git-sh-setup"
require_work_tree
wt_prefix=$(git rev-parse --show-prefix)
cd_to_toplevel
GIT_PROTOCOL_FROM_USER=0
export GIT_PROTOCOL_FROM_USER
set -eu
usage() { die "usage: $USAGE"; }
super=$(pwd)
command=
path=
reset_hard=1
module_name () {
git config -f .gitmodules --list |
sed -n "s|^submodule.\([^.]*\).path=$1\$|\1|p"
}
module_hash () {
git submodule foreach 'echo $path $sha1' |
sed -n "s|^$1 \([^.]*\)|\1|p"
}
clone () {
path="$1"
modsdir="$2/modules"
shift 2
echo "--- [$path] ---"
cd "$super"
name=$(module_name "$path")
push_remote=$(git config --includes -f .gitmodules remote.pushDefault || true)
push_match=$(git config --includes -f .gitmodules --get-all remote.pushMatch || true)
if [ "$(git config submodule.$name.active)" != true ]
then
echo "Skipping $path (not initialized)"
else
if [ ! -e "$path"/.git ]
then
url=$(git config --includes -f .gitmodules submodule.$name.url)
args=
rename=
echo "Cloning $path from origin ($url)"
case "$name,$url" in
elpa-admin,*git.savannah.gnu.org*/git/emacs/elpa.git)
args="--no-tags --single-branch --branch $name"
rename=$name
;;
*,*git.savannah.gnu.org*/git/emacs/elpa.git)
args="--no-tags --single-branch --branch externals/$name"
rename=externals/$name
;;
*,*git.savannah.gnu.org*/git/emacs/nongnu.git)
args="--no-tags --single-branch --branch elpa/$name"
rename=elpa/$name
;;
esac
mkdir -p "$modsdir"
git clone "$url" "$path" $args \
--separate-git-dir "$modsdir/$name" ||
echo "Cloning failed"
if [ -n "$rename" ]
then
cd "$path"
echo "Renaming branch $rename to main"
git branch -m $rename main
fi
fi
git config --includes -f .gitmodules --get-all submodule.$name.remote |
while read -r remote url refspec
do
cd "$super"
if [ ! -e "$path"/.git ]
then
echo "Cloning $path from $remote ($url)"
mkdir -p "$modsdir"
if git clone "$remote_url" "$path" \
--separate-git-dir "$modsdir/$name"
then
git remote rename origin "$remote"
else
echo "Cloning failed"
fi
else
cd "$path"
if ! git remote | grep -q "^$remote\$"
then
echo "Augmenting $path with remote $remote ($url)"
args=
case "$url" in
*git.savannah.gnu.org*/git/emacs/elpa.git)
args="--no-tags -t externals/$name" ;;
*git.savannah.gnu.org*/git/emacs/nongnu.git)
args="--no-tags -t elpa/$name" ;;
esac
git remote add "$remote" "$url" $args
git fetch "$remote" || echo "fetch failed"
fi
fi
if [ -e "$super/$path/.git" ]
then
cd "$super/$path"
if ! git config remote.pushDefault > /dev/null &&
[ "$remote" = "$push_remote" ]
then
echo "Setting remote.pushDefault for $path to $remote"
git config remote.pushDefault "$remote"
fi
fi
done
if [ -e "$super/$path/.git" ]
then
cd "$super/$path"
if [ -z "$(git config remote.pushDefault)" ]
then
url=$(git config remote.origin.url)
for p in $push_match
do
case $url in
*$p*)
echo "Setting remote.pushDefault for $path to origin"
git config remote.pushDefault origin ;;
esac
done
fi
else
git config submodule.$name.active false
fi
fi
echo
}
checkout () {
path="$1"
shift
echo "--- [$path] ---"
cd "$super"
name=$(module_name "$path")
hash=$(module_hash "$path")
if [ "$(git config submodule.$name.active)" != "true" ]
then
echo "Skipping $path (submodule inactive)"
else
cd "$path"
head=$(git rev-parse HEAD)
if [ "$head" != "$hash" ]
then
if [ -z "$reset_hard" ]
then
echo "Skipping $path (--reset-hard not specified)"
echo " HEAD: $head"
echo "expected: $hash"
elif [ -n "$(git status --porcelain=v1 --ignored)" ]
then
echo "Skipping $path (due to uncommitted changes)"
echo " HEAD: $head"
echo "expected: $hash"
git status --porcelain=v1 --ignored
else
echo "Checkout $path ($hash)"
echo "HEAD was $(git log --no-walk --format='%h %s' HEAD)"
if ! git reset --hard "$hash"
then
echo "Checkout of '$hash' into submodule path '$path' failed"
fi
fi
fi
fi
echo
}
cmd_clone () {
while [ $# -ne 0 ]
do
case "$1" in
--) shift; break;;
-*) usage;;
*) break;;
esac
shift
done
gitdir="$(realpath "$(git rev-parse --git-dir)")"
if [ $# -ne 0 ]
then
for path in "$@"; do clone $path $gitdir; done
else
git ls-files -s | grep ^160000 | cut -f2 |
while read path; do clone $path $gitdir; done
fi
}
cmd_checkout () {
while [ $# -ne 0 ]
do
case "$1" in
--reset-hard) reset_hard=1 ;;
--) shift; break;;
-*) usage;;
*) break;;
esac
shift
done
if [ $# -ne 0 ]
then
for path in "$@"; do checkout $path; done
else
git ls-files -s | grep ^160000 | cut -f2 |
while read path; do checkout $path; done
fi
}
command=$1
case "$command" in
clone|checkout) shift; "cmd_$command" "$@" ;;
*) usage ;;
esac