2 # TopGit revert command
3 # Copyright (C) 2015 Kyle J. McKay <mackyle@gmail.com>
7 USAGE
="Usage: ${tgname:-tg} [...] revert (-f | -i | -n) [-q] [--tgish-only] [--no-deps] [--no-stash] [--exclude <ref>...] (<tagname> | --stash) [<ref>...]"
8 USAGE
="$USAGE$lf Or: ${tgname:-tg} [...] revert [-l] [--no-short] [--hash] [--tgish-only] [(--deps | --rdeps)] [--exclude <ref>...] (<tagname> | --stash) [(--[topgit-]heads] | <ref>...)]"
12 if [ "${1:-0}" != 0 ]; then
13 printf '%s\n' "$USAGE" >&2
15 printf '%s\n' "$USAGE"
37 while [ $# -gt 0 ]; do case "$1" in
47 --short|
--short=*|
--no-short)
78 [ -n "${1#--exclude=}" ] || die
"--exclude= requires a ref name"
79 case "${1#--exclude=}" in refs/*) rn="${1#--exclude=}";; *) rn="refs/heads/${1#--exclude=} refs/$topbases/${1#--exclude=}"; esac
80 exclude
="$exclude $rn";;
83 [ -n "$1" ] || die
"--exclude requires a ref name"
84 case "$1" in refs
/*) rn
="$1";; *) rn
="refs/heads/$1 refs/$topbases/$1"; esac
85 exclude
="$exclude $rn";;
90 --stash|
--stash"@{"*"}")
94 echo "Unknown option: $1" >&2
101 [ -z "$exclude" ] || exclude
="$exclude "
103 [ -z "$list$short$hashonly" ] ||
[ -z "$force$interact$dryrun$nodeps$nostash" ] || usage
1
104 [ -z "$force$interact$dryrun" ] ||
[ -z "$list$short$hashonly$deps$rdeps" ] || usage
1
105 [ -z "$deps" ] ||
[ -z "$rdeps" ] || usage
1
106 [ -n "$list$force$interact$dryrun" ] || list
=1
107 [ -z "$list" ] ||
[ -n "$short" ] ||
if [ -n "$hashonly" ]; then short
="--no-short"; else short
="--short"; fi
108 [ -n "$1" ] ||
{ echo "Tag name required" >&2; usage
1; }
111 [ "$1" != "--heads-independent" ] ||
{ shift; set -- --heads "$@"; }
112 if [ "$1" = "--topgit-heads" ]; then
115 set -- "--heads" "$@"
117 [ -n "$list" ] ||
[ "$1" != "--heads" ] || usage
1
118 [ "$tagname" != "--stash" ] || tagname
=refs
/tgstash
119 case "$tagname" in --stash"@{"*"}")
120 strip
="${tagname#--stash??}"
122 tagname
="refs/tgstash@{$strip}"
125 case "$refname" in HEAD|refs
/*);;*)
127 _refonly
="${refname%%"$_pfx"*}"
128 suffix
="${refname#"$_refonly"}"
132 reftest
="$(git rev-parse --revs-only --symbolic-full-name "$refname" -- 2>/dev/null)" &&
136 reftest
="$(git rev-parse --revs-only --symbolic-full-name "refs
/tags
/$refname" -- 2>/dev/null)" &&
140 refname
="$reftest$suffix"
142 if hash="$(git rev-parse --quiet --verify "$refname$suffix")"; then
145 refname
="refs/tags/$refname$suffix"
150 case "$refname" in refs
/tags
/*) tagname
="${refname#refs/tags/}";; *) reftype
=ref
; tagname
="$refname"; esac
151 git rev-parse
--verify --quiet "$refname^{tag}" -- >/dev
/null || die
"not annotated/signed tag: $refname"
152 tgf
="$(get_temp tag)"
153 trf
="$(get_temp refs)"
154 tagdataref
="$refname^{tag}"
156 git cat-file tag
"$tagdataref" >"$tgf"t || die
"cannot read tag: $refname"
157 sed -ne '/^-----BEGIN TOPGIT REFS-----$/,/^-----END TOPGIT REFS-----$/p' <"$tgf"t |
158 sed -ne "/^\\($octet20\\) \\(refs\/[^ $tab][^ $tab]*\\)\$/{s//\\2 \\1/;p;}" |
159 sed -e "s,^refs/$oldbases/,refs/$topbases/,g" |
160 sort -u -b -k1,1 >"$trf"
163 # If it's a tag of a tag, dereference it and try again
164 read -r field tagtype
<<-EOT || break
165 $(sed -n '1,/^$/p' <"$tgf" | grep '^type [^ ][^ ]*$' || :)
167 [ "$tagtype" = "tag" ] ||
break
168 read -r field tagdataref
<<-EOT || break
169 $(sed -n '1,/^$/p' <"$tgf" | grep '^object [^ ][^ ]*$' || :)
171 [ -n "$tagdataref" ] ||
break
172 tagdataref
="$tagdataref^{tag}"
173 git rev-parse
--verify --quiet "$tagdataref" -- >/dev
/null ||
break
175 [ -s "$trf" ] || die
"$reftype $tagname does not contain a TOPGIT REFS section"
176 rcnt
=$
(( $
(wc -l <"$trf") ))
177 vcnt
=$
(( $
(cut
-d ' ' -f 2 <"$trf" | git cat-file
$gcfbopt --batch-check='%(objectname)' |
grep -v ' missing$' |
wc -l) ))
178 [ "$rcnt" -eq "$vcnt" ] || die
"$reftype $tagname contains $rcnt ref(s) but only $vcnt are still valid"
179 cat "$trf" >"$tg_ref_cache"
186 printf '%s\n' "$1" |
join - "$trf" | cut
-d ' ' -f 2 ||
:
191 topics
="$(get_temp topics)"
192 topics2
="$(get_temp topics)"
193 deplist
="$(get_temp deplist)"
195 sed -e '\,^refs/'"$topbasesrx"'/,!d' -e 's,^refs/'"$topbasesrx"'/,refs/heads/,' -e 's/ .*//'
196 while read -r oneref
; do
197 _rev
="$(get_recorded_ref "$oneref")" && [ -n "$_rev" ] ||
continue
198 printf '%s\n' "$oneref" >>"$topics2"
199 git cat-file blob
"$_rev:.topdeps" 2>/dev
/null |
awk '{print}'
200 done <"$topics" |
sed -e 's,^,refs/heads/,' |
sort -u >"$deplist"
202 join -v 1 "$topics" "$deplist"
207 srt
="$(get_temp sort)"
208 sort -b -k2,2 <"$trf" >"$srt"
209 git merge-base
--independent $
(cut
-d ' ' -f 2 <"$srt") |
211 join -2 2 -o 2.1 - "$srt" |
217 if [ -n "$headstopgit" ]; then
218 show_topgit_heads
"$@"
220 show_indep_heads
"$@"
224 [ $# -ne 0 ] ||
[ -z "$rdeps$deps" ] ||
{ set -- --heads; headstopgit
=1; }
225 [ $# -ne 1 ] ||
[ -z "$deps" ] ||
[ "$1" != "--heads" ] ||
{ deps
=; set --; }
226 if [ $# -eq 1 ] && [ "$1" = "--heads" ]; then
233 ref_exists
"refs/heads/${1#refs/$topbases/}"
236 ref_exists
"refs/$topbases/${1#refs/heads/}"
247 case "$b" in refs
/*) exp
=1; rn
="$b";; *) rn
="refs/heads/$b"; esac
248 ref_exists
"$rn" || die
"not present in tag data (try --list): $rn"
249 case " $refs " in *" $rn "*);;*)
250 refs
="${refs:+$refs }$rn"
251 if [ -z "$list" ] && { [ -z "$nodeps" ] ||
[ -z "$exp" ]; } && is_tgish
"$rn"; then
254 refs
="$refs refs/heads/${rn#refs/$topbases/}"
257 refs
="$refs refs/$topbases/${rn#refs/heads/}"
265 case "$exclude" in *" refs/heads/$_dep "*) return; esac
266 case " $seen_deps " in *" $_dep "*) return 0; esac
267 seen_deps
="${seen_deps:+$seen_deps }$_dep"
268 [ -z "$tgish" ] ||
[ -n "$_dep_is_tgish" ] ||
return 0
269 printf 'refs/heads/%s\n' "$_dep"
270 [ -z "$_dep_is_tgish" ] ||
271 printf 'refs/%s/%s\n' "$topbases" "$_dep"
277 recurse_deps_exclude
=
278 while read _b
&& [ -n "$_b" ]; do
279 case "$exclude" in *" $_b "*) continue; esac
280 if ! is_tgish
"$_b"; then
281 [ -z "$tgish" ] ||
continue
285 case "$_b" in refs
/"$topbases"/*) _b
="refs/heads/${_b#refs/$topbases/}"; esac
286 _b
="${_b#refs/heads/}"
287 case " $recurse_deps_exclude " in *" $_b "*) continue; esac
289 _dep
="$_b"; _dep_is_tgish
=1; show_dep
290 recurse_deps show_dep
"$_b"
291 recurse_deps_exclude
="$recurse_deps_exclude $seen_deps"
297 case "$exclude" in *" refs/heads/$_dep "*) return; esac
298 [ -z "$tgish" ] ||
[ -n "$_dep_is_tgish" ] ||
return 0
299 v_ref_exists_rev_short _depshort
"refs/heads/$_dep" $short
300 if [ -n "$hashonly" ]; then
301 printf '%s %s\n' "$_depchain" "$_depshort"
303 printf '%s %s\n' "$_depchain" "${_depshort}~$_dep"
312 [ "$short" != "--short" ] || v_get_core_abbrev _dummy
313 while read _b
&& [ -n "$_b" ]; do
314 case "$exclude" in *" $_b "*) continue; esac
315 if ! is_tgish
"$_b"; then
316 [ -z "$tgish" ] ||
continue
317 [ -z "$showbreak" ] ||
echo
319 v_ref_exists_rev_short _bshort
"refs/heads/$_b" $short
320 if [ -n "$hashonly" ]; then
321 printf '%s\n' "$_bshort"
323 printf '%s\n' "${_bshort}~$_b"
327 case "$_b" in refs
/"$topbases"/*) _b
="refs/heads/${_b#refs/$topbases/}"; esac
328 _b
="${_b#refs/heads/}"
329 case " $seen_deps " in *" $_b "*) continue; esac
330 seen_deps
="$seen_deps $_b"
331 [ -z "$showbreak" ] ||
echo
333 v_ref_exists_rev_short _bshort
"refs/heads/$_b" $short
335 if [ -n "$hashonly" ]; then
336 printf '%s\n' "$_bshort"
338 printf '%s\n' "${_bshort}~$_b"
341 recurse_deps show_rdep
"$_b"
342 } |
sed -e 's/[^ ][^ ]*[ ]/ /g' -e 's/~/ /'
347 [ -z "$refs" ] ||
sed 'y/ /\n/' <<-EOT
352 if [ -n "$list" ]; then
353 if [ -z "$deps$rdeps" ]; then
354 # accelerate showing everything in full
355 if [ -z "$refs$exclude" ] && [ z
"$short" = z
"--no-short" ]; then
356 if [ -n "$hashonly" ]; then
357 <"$trf" sed -n 's/^[^ ][^ ]* \([^ ][^ ]*\)$/\1/p'
359 <"$trf" sed -n 's/^\([^ ][^ ]*\) \([^ ][^ ]*\)$/\2 \1/p'
363 while read -r name
rev; do
364 case "$exclude" in *" $name "*) continue; esac
365 [ -z "$refs" ] ||
case " $refs " in *" $name "*);;*) continue; esac
366 [ -z "$tgish" ] || is_tgish
"$name" ||
continue
367 if [ -n "$hashonly" ]; then
368 printf '%s\n' "$(git rev-parse --verify --quiet $short "$rev" --)"
370 printf '%s %s\n' "$(git rev-parse --verify --quiet $short "$rev" --)" "$name"
375 if [ -n "$deps" ]; then
376 refslist | show_deps |
sort -u -b -k1,1 |
378 while read -r name
rev; do
379 if [ -n "$hashonly" ]; then
380 printf '%s\n' "$(git rev-parse --verify --quiet $short "$rev" --)"
382 printf '%s %s\n' "$(git rev-parse --verify --quiet $short "$rev" --)" "$name"
387 refslist | show_rdeps
390 insn
="$(get_temp isns)"
393 [ -n "$interact" ] ||
{ printf '%s' "$1"; return 0; }
394 git rev-parse
--verify --quiet --short "$1" --
397 if [ -n "$nodeps" ] ||
[ -z "$refs" ]; then
398 while read -r name
rev; do
399 case "$exclude" in *" $name "*) continue; esac
400 [ -z "$refs" ] ||
case " $refs " in *" $name "*);;*) continue; esac
401 [ -z "$tgish" ] || is_tgish
"$name" ||
continue
402 printf 'revert %s %s\n' "$(get_short "$rev")" "$name"
403 done <"$trf" |
sort -u -b -k3,3 >"$insn"
405 refslist | show_deps |
sort -u -b -k1,1 |
407 while read -r name
rev; do
408 printf 'revert %s %s\n' "$(get_short "$rev")" "$name"
411 if [ -n "$interact" ]; then
412 count
=$
(( $
(wc -l <"$insn") ))
415 # Revert using $refname data ($count command(s))
418 # r, revert = revert ref to specified hash
420 # Note that changing the hash value shown here will have NO EFFECT.
422 # If you remove a line here THAT REVERT WILL BE SKIPPED.
424 # However, if you remove everything, the revert will be aborted.
426 run_editor
"$insn" ||
427 die
"there was a problem with the editor '$tg_editor'"
428 git stripspace
-s <"$insn" >"$insn"+
429 mv -f "$insn"+ "$insn"
430 [ -s "$insn" ] || die
"nothing to do"
431 while read -r op
hash ref
; do
432 [ "$op" = "r" ] ||
[ "$op" = "revert" ] ||
433 die
"invalid op in instruction: $op $hash $ref"
434 case "$ref" in refs
/?
*);;*)
435 die
"invalid ref in instruction: $op $hash $ref"
438 die
"unknown ref in instruction: $op $hash $ref"
441 msg
="tgrevert: $reftype $tagname ($(( $(wc -l <"$insn") )) command(s))"
442 if [ -z "$dryrun" ]; then
443 if [ -z "$nostash" ]; then
444 tg tag
-q -q --allow-any --none-ok -m "$msg" --stash || die
"requested --stash failed"
446 tg tag
--allow-any --none-ok --anonymous || die
"anonymous --stash failed"
449 refwidth
="$(git config --get --int core.abbrev 2>/dev/null)" ||
:
450 [ -n "$refwidth" ] || refwidth
=7
451 [ $refwidth -ge 4 ] && [ $refwidth -le 40 ] || refwidth
=7
452 nullref
="$(printf '%.*s' $refwidth "$nullsha")"
453 notewidth
=$
(( $refwidth + 4 + $refwidth ))
455 [ -n "$dryrun" ] || srh
="$(git symbolic-ref --quiet HEAD)" ||
:
456 cut
-d ' ' -f 3 <"$insn" |
sort -u -b -k1,1 |
join - "$trf" |
457 while read -r name
rev; do
458 orig
="$(git rev-parse --verify --quiet "$name^
{}" --)" ||
:
460 if [ "$rev" != "$orig" ]; then
461 [ -z "$dryrun" ] && [ -n "$quiet" ] ||
462 origsh
="$(git rev-parse --verify --short --quiet "$name" --)" ||
:
463 if [ -z "$dryrun" ]; then
464 if [ -n "$srh" ] && [ "$srh" = "$name" ]; then
465 [ -n "$quiet" ] || warn
"detaching HEAD to revert $name"
467 [ -n "$detachat" ] || detachat
="$(make_empty_commit)"
468 git update-ref
-m "tgrevert: detach HEAD to revert $name" --no-deref HEAD
"$detachat" || die
"detach failed"
469 [ -n "$quiet" ] || warn
"$(git --no-pager log -n 1 --format=format:'HEAD is now at %h... %s' HEAD)"
471 git update-ref
-m "$msg" "$name" "$rev"
473 if [ -n "$dryrun" ] ||
[ -z "$quiet" ]; then
474 revsh
="$(git rev-parse --verify --short --quiet "$rev" --)" ||
:
475 if [ -n "$origsh" ]; then
477 [ -z "$dryrun" ] || hdr
='-'
478 printf '%s %s -> %s %s\n' "$hdr" "$origsh" "$revsh" "$name"
481 [ -z "$dryrun" ] || hdr
='-'
482 printf '%s %s -> %s %s\n' "$hdr" "$nullref" "$revsh" "$name"
486 : #[ -z "$dryrun" ] && [ -n "$quiet" ] || printf "* %-*s %s\n" $notewidth "[no change]" "$name"