t1100: test tg migrate-bases command
[topgit/pro.git] / tg-migrate-bases.sh
blobbaf3521853bcdc929b644f23840b25be56e9724f
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/}"
119 "refs/heads/{top-bases}"/[!/]*)
120 _newb="refs/top-bases/${2#refs/heads/$topbraces/}"
122 "refs/remotes"/[!/]*)
123 _rb="${2#refs/remotes/}"
124 _rn="${_rb%%/*}"
125 _rr="${_rb#*/}"
126 case "$_rr" in
127 "top-bases"/[!/]*)
128 _newb="refs/remotes/$_rn/{top-bases}/${_rr#top-bases/}"
130 "{top-bases}"/[!/]*)
131 _newb="refs/remotes/$_rn/top-bases/${_rr#$topbraces/}"
133 esac
135 esac
136 if [ -n "$_newb" ] && [ -n "$1" ]; then
137 eval "$1="'"$_newb"'
138 return 0
140 [ -z "$1" ] || eval "$1="
141 return 1
144 for r in $remotes; do
145 nv="+refs/$newbases/*:refs/remotes/$r/${newbases#heads/}/*"
146 if rf="$(git config --get-all "remote.$r.fetch" \
147 "\\+?refs/(top-bases|heads/[{]top-bases[}])/\\*:refs/remotes/$r/(top-bases|[{]top-bases[}])/\\*")" &&
148 [ "$rf" != "$nv" ]; then
149 echo "remote.$r.fetch:"
150 printf ' %s\n' $rf
151 printf ' -> %s\n' "$nv"
152 if [ -n "$force" ]; then
153 git config --replace-all "remote.$r.fetch" "$nv" \
154 "\\+?refs/(top-bases|heads/[{]top-bases[}])/\\*:refs/remotes/$r/(top-bases|[{]top-bases[}])/\\*"
156 elif [ "$rf" != "$nv" ] && rf="$(git config --get-all "remote.$r.fetch" "\\+?refs/(top-bases|heads/[{]top-bases[}])/.*")"; then
157 echo "remote.$r.fetch may need manual updates of:"
158 printf ' %s\n' $rf
160 done
162 git for-each-ref --format='%(refname) %(objecttype) %(objectname)' $refpats |
163 while read -r rn rt rh; do
164 if [ -z "$orphans" ] && ! not_orphan_base "$rn"; then
165 echo "skipping orphan base (use --orphans): $rn" >&2
166 continue
168 if [ "$rt" = "tree" ] || [ "$rt" = "blob" ]; then
169 echo "ignoring base with type $rt: $rn" >&2
170 continue
172 if [ "$rt" = "tag" ]; then
173 rnc="$(git rev-parse --verify --quiet "$rh^0" -- 2>/dev/null)" || :
174 if [ -z "$rnc" ]; then
175 echo "ignoring base with type tag of non-commit: $rn" >&2
176 continue
178 echo "warning: resolving base with type tag to commit: $rn" >&2
179 rh="$rnc"
181 v_transform_base newb "$rn" || die "unexpected non-bases ref: $rn"
182 newbrev="$(git rev-parse --verify --quiet "$newb" --)" || :
183 newbtype=
184 [ -z "$newbrev" ] || newbtype="$(git cat-file -t "$newbrev")"
185 if [ "$newbtype" = "tree" ] || [ "$newbtype" = "blob" ]; then
186 echo "warning: $rn" >&2
187 echo " refusing to update existing ref:" >&2
188 echo " $newb" >&2
189 echo " of type $newbtype" >&2
190 continue
192 if [ "$newbtype" = "tag" ]; then
193 newbrev="$(git rev-parse --verify --quiet "$newbrev^0" -- 2>/dev/null)" || :
194 if [ -z "$newbrev" ]; then
195 echo "warning: $rn" >&2
196 echo " refusing to update existing ref:" >&2
197 echo " $newb" >&2
198 echo " of type tag of non-commit" >&2
199 continue
201 echo "warning: $rn" >&2
202 echo " treating existing ref:" >&2
203 echo " $newb" >&2
204 echo " of type tag as the tagged commit" >&2
206 if [ -n "$newbrev" ] && [ "$newbrev" != "rh" ]; then
207 mb="$(git merge-base "$newbrev" "$rh" 2>/dev/null)" || :
208 if [ "$mb" = "$newbrev" ]; then
209 echo "warning: $rn" >&2
210 echo " ignoring existing ref:" >&2
211 echo " $newb" >&2
212 echo " since it's contained in $rn" >&2
213 elif [ "$mb" = "$rh" ]; then
214 echo "warning: $rn" >&2
215 echo " using existing value of ref:" >&2
216 echo " $newb" >&2
217 echo " since it contains $rn" >&2
218 rh="$newbrev"
219 else
220 rd="$(git --no-pager log -n 1 --format='format:%ct' "$rh" --)"
221 newbdt="$(git --no-pager log -n 1 --format='format:%ct' "$newbrev" --)"
222 if [ "$rd" -ge "$newbdt" ]; then
223 echo "warning: $rn" >&2
224 echo " ignoring existing diverged ref:" >&2
225 echo " $newb" >&2
226 echo " since it's got an older committer timestamp" >&2
227 else
228 echo "warning: $rn" >&2
229 echo " using existing value of diverged ref:" >&2
230 echo " $newb" >&2
231 echo " since it's got a newer committer timestamp" >&2
232 rh="$newbrev"
236 printf 'update: %s\n -> %s\n' "$rn" "$newb"
237 if [ -n "$force" ]; then
238 git update-ref "$newb" "$rh"
239 if [ "$(git rev-parse --quiet --verify "$newb" --)" = "$rh" ] && [ "$newb" != "$rn" ]; then
240 git update-ref -d "$rn"
243 done
244 [ $? -eq 0 ] || exit 1