3 # tg--migrate-bases -- migrate from old top-bases to new {top-bases}
4 # Copyright (C) 2017 Kyle J. McKay
9 Usage: ${tgname:-tg} [...] migrate-bases (--dry-run | --force) [--no-remotes | --remotes-only]"
13 if [ "${1:-0}" != 0 ]; then
14 printf '%s\n' "$USAGE" >&2
16 printf '%s\n' "$USAGE"
30 while [ $# -gt 0 ]; do case "$1" in
34 -n|
--dry-run|
--dryrun)
52 --no-orphans|
--no-orphan)
56 echo "Unknown option: $1" >&2
61 [ "$dryrun$force" = "1" ] || usage
1
62 [ "$noremotes$remotesonly" != "11" ] || usage
1
63 [ $# -eq 0 ] || usage
1
66 [ -n "$noremotes" ] || remotes
="$(git remote)" ||
:
68 if [ -z "$reverse" ]; then
70 oldbasesrx
="top-bases"
71 newbases
="heads/{top-bases}"
72 newbasesrx
="heads/[{]top-bases[}]"
74 oldbases
="heads/{top-bases}"
75 oldbasesrx
="heads/[{]top-bases[}]"
77 newbasesrx
="top-bases"
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}"
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/}"
96 _rb
="${1#refs/remotes/}"
101 _check
="refs/remotes/$_rn/${_rr#top-bases/}"
104 _check
="refs/remotes/$_rn/${_rr#$topbraces/}"
109 [ -n "$_check" ] ||
return 1
110 git rev-parse
--verify --quiet "$_check" -- >/dev
/null
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/}"
128 _newb
="refs/remotes/$_rn/{top-bases}/${_rr#top-bases/}"
131 _newb
="refs/remotes/$_rn/top-bases/${_rr#$topbraces/}"
136 if [ -n "$_newb" ] && [ -n "$1" ]; then
140 [ -z "$1" ] ||
eval "$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:"
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:"
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
168 if [ "$rt" = "tree" ] ||
[ "$rt" = "blob" ]; then
169 echo "ignoring base with type $rt: $rn" >&2
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
178 echo "warning: resolving base with type tag to commit: $rn" >&2
181 v_transform_base newb
"$rn" || die
"unexpected non-bases ref: $rn"
182 newbrev
="$(git rev-parse --verify --quiet "$newb" --)" ||
:
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
189 echo " of type $newbtype" >&2
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
198 echo " of type tag of non-commit" >&2
201 echo "warning: $rn" >&2
202 echo " treating existing ref:" >&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
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
217 echo " since it contains $rn" >&2
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
226 echo " since it's got an older committer timestamp" >&2
228 echo "warning: $rn" >&2
229 echo " using existing value of diverged ref:" >&2
231 echo " since it's got a newer committer timestamp" >&2
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"
244 [ $?
-eq 0 ] ||
exit 1