t/Makefile: compute TG-TEST-SETTINGS differently
[topgit/pro.git] / tg-migrate-bases.sh
blob472139380d310bb1c87a589a5a20a2fd4525f9c3
1 #!/bin/sh
3 # tg--migrate-bases -- migrate from old top-bases to new {top-bases}
4 # Copyright (C) 2017 Kyle J. McKay
5 # All rights reserved.
6 # License GPLv2+
8 USAGE="\
9 Usage: ${tgname:-tg} [...] migrate-bases (--dry-run | --force) [--no-remotes | --remotes-only]"
11 usage()
13 if [ "${1:-0}" != 0 ]; then
14 printf '%s\n' "$USAGE" >&2
15 else
16 printf '%s\n' "$USAGE"
18 exit ${1:-0}
21 ## Parse options
23 dryrun=
24 force=
25 noremotes=
26 remotesonly=
27 reverse=
28 orphans=1
30 while [ $# -gt 0 ]; do case "$1" in
31 -h|--help)
32 usage
34 -n|--dry-run|--dryrun)
35 dryrun=1
37 -f|--force)
38 force=1
40 --no-remotes)
41 noremotes=1
43 --remotes-only)
44 remotesonly=1
46 --reverse)
47 reverse=1
49 --orphans|--orphan)
50 orphans=1
52 --no-orphans|--no-orphan)
53 orphans=
55 -?*)
56 echo "Unknown option: $1" >&2
57 usage 1
59 esac; shift; done
61 [ "$dryrun$force" = "1" ] || usage 1
62 [ "$noremotes$remotesonly" != "11" ] || usage 1
63 [ $# -eq 0 ] || usage 1
65 remotes=
66 [ -n "$noremotes" ] || remotes="$(git remote)" || :
68 if [ -z "$reverse" ]; then
69 oldbases="top-bases"
70 oldbasesrx="top-bases"
71 newbases="heads/{top-bases}"
72 newbasesrx="heads/[{]top-bases[}]"
73 else
74 oldbases="heads/{top-bases}"
75 oldbasesrx="heads/[{]top-bases[}]"
76 newbases="top-bases"
77 newbasesrx="top-bases"
80 refpats=
81 [ -n "$remotesonly" ] || refpats="refs/$oldbases"
82 [ -z "$remotes" ] || refpats="$refpats$(printf " refs/remotes/%s/${oldbases#heads/}" $remotes)"
83 [ -n "$refpats" ] || exit 0
85 topbraces="{top-bases}"
86 not_orphan_base() {
87 _check=
88 case "$1" in
89 "refs/top-bases"/[!/]*)
90 _check="refs/heads/${1#refs/top-bases/}"
92 "refs/heads/{top-bases}"/[!/]*)
93 _check="refs/heads/${1#refs/heads/$topbraces/}"
95 "refs/remotes"/[!/]*)
96 _rb="${1#refs/remotes/}"
97 _rn="${_rb%%/*}"
98 _rr="${_rb#*/}"
99 case "$_rr" in
100 "top-bases"/[!/]*)
101 _check="refs/remotes/$_rn/${_rr#top-bases/}"
103 "{top-bases}"/[!/]*)
104 _check="refs/remotes/$_rn/${_rr#$topbraces/}"
106 esac
108 esac
109 [ -n "$_check" ] || return 1
110 git rev-parse --verify --quiet "$_check" -- >/dev/null
113 v_transform_base() {
114 _newb=
115 case "$2" in
116 "refs/top-bases"/[!/]*)
117 _newb="refs/heads/{top-bases}/${2#refs/top-bases/}"
118 _wasrevdir=
119 _wasremote=
121 "refs/heads/{top-bases}"/[!/]*)
122 _newb="refs/top-bases/${2#refs/heads/$topbraces/}"
123 _wasremote=
124 _wasrevdir=1
126 "refs/remotes"/[!/]*)
127 _rb="${2#refs/remotes/}"
128 _rn="${_rb%%/*}"
129 _rr="${_rb#*/}"
130 case "$_rr" in
131 "top-bases"/[!/]*)
132 _newb="refs/remotes/$_rn/{top-bases}/${_rr#top-bases/}"
133 _wasrevdir=
134 _wasremote=1
136 "{top-bases}"/[!/]*)
137 _newb="refs/remotes/$_rn/top-bases/${_rr#$topbraces/}"
138 _wasrevdir=1
139 _wasremote=1
141 esac
143 esac
144 if [ -n "$_newb" ] && [ -n "$1" ]; then
145 eval "$1="'"$_newb"'
146 return 0
148 [ -z "$1" ] || eval "$1="
149 return 1
152 for r in $remotes; do
153 nv="+refs/$newbases/*:refs/remotes/$r/${newbases#heads/}/*"
154 if rf="$(git config --get-all "remote.$r.fetch" \
155 "\\+?refs/(top-bases|heads/[{]top-bases[}])/\\*:refs/remotes/$r/(top-bases|[{]top-bases[}])/\\*")" &&
156 [ "$rf" != "$nv" ]; then
157 echo "remote.$r.fetch:"
158 printf ' %s\n' $rf
159 printf ' -> %s\n' "$nv"
160 if [ -n "$force" ]; then
161 git config --replace-all "remote.$r.fetch" "$nv" \
162 "\\+?refs/(top-bases|heads/[{]top-bases[}])/\\*:refs/remotes/$r/(top-bases|[{]top-bases[}])/\\*"
164 elif [ "$rf" != "$nv" ] && rf="$(git config --get-all "remote.$r.fetch" "\\+?refs/(top-bases|heads/[{]top-bases[}])/.*")"; then
165 echo "remote.$r.fetch may need manual updates of:"
166 printf ' %s\n' $rf
168 done
170 while read -r rn rt rh && [ -n "$rn" ] && [ -n "$rt" ] && [ -n "$rh" ]; do
171 if [ -z "$orphans" ] && ! not_orphan_base "$rn"; then
172 echo "skipping orphan base (use --orphans): $rn" >&2
173 continue
175 if [ "$rt" = "tree" ] || [ "$rt" = "blob" ]; then
176 echo "ignoring base with type $rt: $rn" >&2
177 continue
179 if [ "$rt" = "tag" ]; then
180 rnc="$(git rev-parse --verify --quiet "$rh^0" -- 2>/dev/null)" || :
181 if [ -z "$rnc" ]; then
182 echo "ignoring base with type tag of non-commit: $rn" >&2
183 continue
185 echo "warning: resolving base with type tag to commit: $rn" >&2
186 rh="$rnc"
188 v_transform_base newb "$rn" || die "unexpected non-bases ref: $rn"
189 newbrev="$(git rev-parse --verify --quiet "$newb" --)" || :
190 newbtype=
191 [ -z "$newbrev" ] || newbtype="$(git cat-file -t "$newbrev")"
192 if [ "$newbtype" = "tree" ] || [ "$newbtype" = "blob" ]; then
193 echo "warning: $rn" >&2
194 echo " refusing to update existing ref:" >&2
195 echo " $newb" >&2
196 echo " of type $newbtype" >&2
197 continue
199 if [ "$newbtype" = "tag" ]; then
200 newbrev="$(git rev-parse --verify --quiet "$newbrev^0" -- 2>/dev/null)" || :
201 if [ -z "$newbrev" ]; then
202 echo "warning: $rn" >&2
203 echo " refusing to update existing ref:" >&2
204 echo " $newb" >&2
205 echo " of type tag of non-commit" >&2
206 continue
208 echo "warning: $rn" >&2
209 echo " treating existing ref:" >&2
210 echo " $newb" >&2
211 echo " of type tag as the tagged commit" >&2
213 if [ -n "$newbrev" ] && [ "$newbrev" != "rh" ]; then
214 mb="$(git merge-base "$newbrev" "$rh" 2>/dev/null)" || :
215 if [ "$mb" = "$newbrev" ]; then
216 echo "warning: $rn" >&2
217 echo " ignoring existing ref:" >&2
218 echo " $newb" >&2
219 echo " since it's contained in $rn" >&2
220 elif [ "$mb" = "$rh" ]; then
221 echo "warning: $rn" >&2
222 echo " using existing value of ref:" >&2
223 echo " $newb" >&2
224 echo " since it contains $rn" >&2
225 rh="$newbrev"
226 else
227 rd="$(git --no-pager log -n 1 --format='format:%ct' "$rh" --)"
228 newbdt="$(git --no-pager log -n 1 --format='format:%ct' "$newbrev" --)"
229 if [ "$rd" -ge "$newbdt" ]; then
230 echo "warning: $rn" >&2
231 echo " ignoring existing diverged ref:" >&2
232 echo " $newb" >&2
233 echo " since it's got an older committer timestamp" >&2
234 else
235 echo "warning: $rn" >&2
236 echo " using existing value of diverged ref:" >&2
237 echo " $newb" >&2
238 echo " since it's got a newer committer timestamp" >&2
239 rh="$newbrev"
243 printf 'update: %s\n -> %s\n' "$rn" "$newb"
244 if [ -n "$force" ]; then
245 git update-ref "$newb" "$rh"
246 if [ "$(git rev-parse --quiet --verify "$newb" --)" = "$rh" ] && [ "$newb" != "$rn" ]; then
247 git update-ref -d "$rn"
250 done <<EOT
251 $(git for-each-ref --format='%(refname) %(objecttype) %(objectname)' $refpats)
254 maindir="$(cd "$git_common_dir" && pwd -P)"
255 if [ "$git_dir" = "$git_common_dir" ]; then
256 headdir="$maindir"
257 else
258 headdir="$(cd "$git_dir" && pwd -P)"
261 # note that [ -n "$iowopt" ] will be true if linked worktrees are available
262 maybehaslw=
264 [ -n "$iowopt" ] &&
265 [ -d "$maindir/worktrees" ] &&
266 [ $(( $(find "$maindir/worktrees" -mindepth 2 -maxdepth 2 -type f -name HEAD -print 2>/dev/null | wc -l) )) -gt 0 ]
267 then
268 maybehaslw=1
271 # $1 => --git-dir to process (or try to anyway)
272 processed=" "
273 process_one_symref() {
274 case "$processed" in *" $1 "*) return 0; esac
275 processed="$processed$1 "
276 hsymref="$(git --git-dir="$1" symbolic-ref --quiet HEAD -- 2>/dev/null)" &&
277 [ -n "$hsymref" ] || return 0
278 v_transform_base xsymref "$hsymref" || return 0
279 [ -z "$_wasrevdir" ] || [ -n "$reverse" ] || return 0
280 [ -n "$_wasrevdir" ] || [ -z "$reverse" ] || return 0
281 [ -z "$_wasremote" ] || [ -z "$noremotes" ] || return 0
282 [ -n "$_wasremote" ] || [ -z "$remotesonly" ] || return 0
284 appellation="HEAD"
285 [ "$1" != "$headdir" ] || [ -z "$maybehaslw" ] || appellation="$appelation ="
286 if [ -n "$maybehaslw" ]; then
287 if [ "$1" = "$maindir" ]; then
288 workname="main"
289 else
290 workname="${1##*/}"
292 appellation="${appellation:+$appellation }($workname)"
294 [ -z "$appellation" ] || appellation=" [$appellation]"
295 printf 'symref: %s%s\n -> %s\n' "$hsymref" "$appellation" "$xsymref"
296 if [ -n "$force" ]; then
297 git --git-dir="$1" symbolic-ref HEAD "$xsymref" --
301 # always do our HEAD first
302 process_one_symref "$headdir"
303 # then the main HEAD (it will automatically be ignored if a duplicate)
304 process_one_symref "$maindir"
305 # then each worktree (again duplicates will be automatically ignored)
306 if [ -n "$maybehaslw" ]; then
307 while read -r wktree && [ -n "$wktree" ]; do
308 process_one_symref "${wktree%/HEAD}"
309 done <<-EOT
310 $(find "$maindir/worktrees" -mindepth 2 -maxdepth 2 -type f -name HEAD -print 2>/dev/null)