-
Notifications
You must be signed in to change notification settings - Fork 7
/
tg-migrate-bases.sh
325 lines (305 loc) · 8.46 KB
/
tg-migrate-bases.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/bin/sh
# tg--migrate-bases -- migrate from old top-bases to new {top-bases}
# Copyright (C) 2017,2021 Kyle J. McKay
# All rights reserved
# License GPLv2+
USAGE="\
Usage: ${tgname:-tg} [...] migrate-bases (--dry-run | --force) [--no-remotes | --remotes-only]"
usage()
{
if [ "${1:-0}" != 0 ]; then
printf '%s\n' "$USAGE" >&2
else
printf '%s\n' "$USAGE"
fi
exit ${1:-0}
}
## Parse options
dryrun=
force=
noremotes=
remotesonly=
reverse=
orphans=1
while [ $# -gt 0 ]; do case "$1" in
-h|--help)
usage
;;
-n|--dry-run|--dryrun)
dryrun=1
;;
-f|--force)
force=1
;;
--no-remotes)
noremotes=1
;;
--remotes-only)
remotesonly=1
;;
--reverse)
reverse=1
;;
--orphans|--orphan)
orphans=1
;;
--no-orphans|--no-orphan)
orphans=
;;
-?*)
echo "Unknown option: $1" >&2
usage 1
;;
esac; shift; done
[ "$dryrun$force" = "1" ] || usage 1
[ "$noremotes$remotesonly" != "11" ] || usage 1
[ $# -eq 0 ] || usage 1
remotes=
[ -n "$noremotes" ] || remotes="$(git remote)" || :
if [ -z "$reverse" ]; then
oldbases="top-bases"
oldbasesrx="top-bases"
newbases="heads/{top-bases}"
newbasesrx="heads/[{]top-bases[}]"
else
oldbases="heads/{top-bases}"
oldbasesrx="heads/[{]top-bases[}]"
newbases="top-bases"
newbasesrx="top-bases"
fi
refpats=
[ -n "$remotesonly" ] || refpats="refs/$oldbases"
[ -z "$remotes" ] || refpats="$refpats$(printf " refs/remotes/%s/${oldbases#heads/}" $remotes)"
[ -n "$refpats" ] || exit 0
topbraces="{top-bases}"
not_orphan_base() {
_check=
case "$1" in
"refs/top-bases"/[!/]*)
_check="refs/heads/${1#refs/top-bases/}"
;;
"refs/heads/{top-bases}"/[!/]*)
_check="refs/heads/${1#refs/heads/$topbraces/}"
;;
"refs/remotes"/[!/]*)
_rb="${1#refs/remotes/}"
_rn="${_rb%%/*}"
_rr="${_rb#*/}"
case "$_rr" in
"top-bases"/[!/]*)
_check="refs/remotes/$_rn/${_rr#top-bases/}"
;;
"{top-bases}"/[!/]*)
_check="refs/remotes/$_rn/${_rr#$topbraces/}"
;;
esac
;;
esac
[ -n "$_check" ] || return 1
git rev-parse --verify --quiet "$_check^0" -- >/dev/null
}
v_transform_base() {
_newb=
case "$2" in
"refs/top-bases"/[!/]*)
_newb="refs/heads/{top-bases}/${2#refs/top-bases/}"
_wasrevdir=
_wasremote=
;;
"refs/heads/{top-bases}"/[!/]*)
_newb="refs/top-bases/${2#refs/heads/$topbraces/}"
_wasremote=
_wasrevdir=1
;;
"refs/remotes"/[!/]*)
_rb="${2#refs/remotes/}"
_rn="${_rb%%/*}"
_rr="${_rb#*/}"
case "$_rr" in
"top-bases"/[!/]*)
_newb="refs/remotes/$_rn/{top-bases}/${_rr#top-bases/}"
_wasrevdir=
_wasremote=1
;;
"{top-bases}"/[!/]*)
_newb="refs/remotes/$_rn/top-bases/${_rr#$topbraces/}"
_wasrevdir=1
_wasremote=1
;;
esac
;;
esac
if [ -n "$_newb" ] && [ -n "$1" ]; then
eval "$1="'"$_newb"'
return 0
fi
[ -z "$1" ] || eval "$1="
return 1
}
for r in $remotes; do
nv="+refs/$newbases/*:refs/remotes/$r/${newbases#heads/}/*"
if rf="$(git config --get-all "remote.$r.fetch" \
"\\+?refs/(top-bases|heads/[{]top-bases[}])/\\*:refs/remotes/$r/(top-bases|[{]top-bases[}])/\\*")" &&
[ "$rf" != "$nv" ]; then
echo "remote.$r.fetch:"
printf ' %s\n' $rf
printf ' -> %s\n' "$nv"
if [ -n "$force" ]; then
git config --replace-all "remote.$r.fetch" "$nv" \
"\\+?refs/(top-bases|heads/[{]top-bases[}])/\\*:refs/remotes/$r/(top-bases|[{]top-bases[}])/\\*"
fi
elif [ "$rf" != "$nv" ] && rf="$(git config --get-all "remote.$r.fetch" "\\+?refs/(top-bases|heads/[{]top-bases[}])/.*")"; then
echo "remote.$r.fetch may need manual updates of:"
printf ' %s\n' $rf
fi
done
while read -r rn rt rh && [ -n "$rn" ] && [ -n "$rt" ] && [ -n "$rh" ]; do
if [ -z "$orphans" ] && ! not_orphan_base "$rn"; then
echo "skipping orphan base (use --orphans): $rn" >&2
continue
fi
if [ "$rt" = "tree" ] || [ "$rt" = "blob" ]; then
echo "ignoring base with type $rt: $rn" >&2
continue
fi
if [ "$rt" = "tag" ]; then
rnc="$(git rev-parse --verify --quiet "$rh^0" -- 2>/dev/null)" || :
if [ -z "$rnc" ]; then
echo "ignoring base with type tag of non-commit: $rn" >&2
continue
fi
echo "warning: resolving base with type tag to commit: $rn" >&2
rh="$rnc"
fi
v_transform_base newb "$rn" || die "unexpected non-bases ref: $rn"
newbrev="$(git rev-parse --verify --quiet "$newb^0" --)" || :
newbtype=
[ -z "$newbrev" ] || newbtype="$(git cat-file -t "$newbrev")"
if [ "$newbtype" = "tree" ] || [ "$newbtype" = "blob" ]; then
echo "warning: $rn" >&2
echo " refusing to update existing ref:" >&2
echo " $newb" >&2
echo " of type $newbtype" >&2
continue
fi
if [ "$newbtype" = "tag" ]; then
newbrev="$(git rev-parse --verify --quiet "$newbrev^0" -- 2>/dev/null)" || :
if [ -z "$newbrev" ]; then
echo "warning: $rn" >&2
echo " refusing to update existing ref:" >&2
echo " $newb" >&2
echo " of type tag of non-commit" >&2
continue
fi
echo "warning: $rn" >&2
echo " treating existing ref:" >&2
echo " $newb" >&2
echo " of type tag as the tagged commit" >&2
fi
if [ -n "$newbrev" ] && [ "$newbrev" != "rh" ]; then
mb="$(git merge-base "$newbrev" "$rh" 2>/dev/null)" || :
if [ "$mb" = "$newbrev" ]; then
echo "warning: $rn" >&2
echo " ignoring existing ref:" >&2
echo " $newb" >&2
echo " since it's contained in $rn" >&2
elif [ "$mb" = "$rh" ]; then
echo "warning: $rn" >&2
echo " using existing value of ref:" >&2
echo " $newb" >&2
echo " since it contains $rn" >&2
rh="$newbrev"
else
rd="$(git --no-pager log -n 1 --format='format:%ct' "$rh" --)"
newbdt="$(git --no-pager log -n 1 --format='format:%ct' "$newbrev" --)"
if [ "$rd" -ge "$newbdt" ]; then
echo "warning: $rn" >&2
echo " ignoring existing diverged ref:" >&2
echo " $newb" >&2
echo " since it's got an older committer timestamp" >&2
else
echo "warning: $rn" >&2
echo " using existing value of diverged ref:" >&2
echo " $newb" >&2
echo " since it's got a newer committer timestamp" >&2
rh="$newbrev"
fi
fi
fi
printf 'update: %s\n -> %s\n' "$rn" "$newb"
if [ -n "$force" ]; then
git update-ref "$newb" "$rh"
if [ "$(git rev-parse --quiet --verify "$newb^0" --)" = "$rh" ] && [ "$newb" != "$rn" ]; then
git update-ref -d "$rn"
fi
fi
done <<EOT
$(git for-each-ref --format='%(refname) %(objecttype) %(objectname)' $refpats)
EOT
maindir="$(cd "$git_common_dir" && pwd -P)"
if [ "$git_dir" = "$git_common_dir" ]; then
headdir="$maindir"
else
headdir="$(cd "$git_dir" && pwd -P)"
fi
posix_find_worktrees_HEAD() (
# neither -mindepth nor -maxdepth are POSIX, grrrr
# however, POSIX has adopted -path
# $1 is the starting directory and the output lines,
# if any, will all start with "./"
cd "$1" &&
exec find . -path './HEAD' -prune -o -path '*/*/*/*' -prune -o -name HEAD -type f -print
) 2>/dev/null
# note that [ -n "$iowopt" ] will be true if linked worktrees are available
maybehaslw=
if
[ -n "$iowopt" ] &&
[ -d "$maindir/worktrees" ] &&
[ $(( $(posix_find_worktrees_HEAD "$maindir/worktrees" | wc -l) )) -gt 0 ]
then
maybehaslw=1
fi
# $1 => --git-dir to process (or try to anyway)
processed=" "
process_one_symref() {
case "$processed" in *" $1 "*) return 0; esac
processed="$processed$1 "
hsymref="$(git --git-dir="$1" symbolic-ref --quiet HEAD -- 2>/dev/null)" &&
[ -n "$hsymref" ] || return 0
v_transform_base xsymref "$hsymref" || return 0
[ -z "$_wasrevdir" ] || [ -n "$reverse" ] || return 0
[ -n "$_wasrevdir" ] || [ -z "$reverse" ] || return 0
[ -z "$_wasremote" ] || [ -z "$noremotes" ] || return 0
[ -n "$_wasremote" ] || [ -z "$remotesonly" ] || return 0
appellation="HEAD"
[ "$1" != "$headdir" ] || [ -z "$maybehaslw" ] || appellation="$appelation ="
if [ -n "$maybehaslw" ]; then
if [ "$1" = "$maindir" ]; then
workname="main"
else
workname="${1##*/}"
fi
appellation="${appellation:+$appellation }($workname)"
fi
[ -z "$appellation" ] || appellation=" [$appellation]"
printf 'symref: %s%s\n -> %s\n' "$hsymref" "$appellation" "$xsymref"
if [ -n "$force" ]; then
git --git-dir="$1" symbolic-ref HEAD "$xsymref" --
fi
}
# always do our HEAD first
process_one_symref "$headdir"
# then the main HEAD (it will automatically be ignored if a duplicate)
process_one_symref "$maindir"
# then each worktree (again duplicates will be automatically ignored)
if [ -n "$maybehaslw" ]; then
while
read -r wktree &&
wktree="${wktree#./}" &&
[ -n "$wktree" ]
do
process_one_symref "$maindir/worktrees/${wktree%/HEAD}"
done <<-EOT
$(posix_find_worktrees_HEAD "$maindir/worktrees")
EOT
fi