tg.sh: next version is 0.19.13
[topgit/pro.git] / tg-revert.sh
blob33ffe8505edb48ff23990d5d982197d5260596b4
1 #!/bin/sh
2 # TopGit revert command
3 # Copyright (C) 2015 Kyle J. McKay <mackyle@gmail.com>
4 # All rights reserved.
5 # GPLv2
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>...)]"
10 usage()
12 if [ "${1:-0}" != 0 ]; then
13 printf '%s\n' "$USAGE" >&2
14 else
15 printf '%s\n' "$USAGE"
17 exit ${1:-0}
20 ## Parse options
22 force=
23 interact=
24 dryrun=
25 list=
26 deps=
27 rdeps=
28 tgish=
29 nodeps=
30 nostash=
31 exclude=
32 quiet=
33 short=
34 hashonly=
35 headstopgit=
37 while [ $# -gt 0 ]; do case "$1" in
38 -h|--help)
39 usage
41 -q|--quiet)
42 quiet=1
44 -l|--list)
45 list=1
47 --short|--short=*|--no-short)
48 short="$1"
50 --hash|--hash-only)
51 hashonly=1
53 --deps|--deps-only)
54 deps=1
56 --rdeps)
57 rdeps=1
59 --tgish-only)
60 tgish=1
62 -f|--force)
63 force=1
65 -i|--interactive)
66 interact=1
68 -n|--dry-run)
69 dryrun=1
71 --no-deps)
72 nodeps=1
74 --no-stash)
75 nostash=1
77 --exclude=*)
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";;
81 --exclude)
82 shift
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";;
86 --)
87 shift
88 break
90 --stash|--stash"@{"*"}")
91 break
93 -?*)
94 echo "Unknown option: $1" >&2
95 usage 1
98 break
100 esac; shift; done
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; }
109 tagname="$1"
110 shift
111 [ "$1" != "--heads-independent" ] || { shift; set -- --heads "$@"; }
112 if [ "$1" = "--topgit-heads" ]; then
113 shift
114 headstopgit=1
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??}"
121 strip="${strip%?}"
122 tagname="refs/tgstash@{$strip}"
123 esac
124 refname="$tagname"
125 case "$refname" in HEAD|refs/*);;*)
126 _pfx="@{"
127 _refonly="${refname%%"$_pfx"*}"
128 suffix="${refname#"$_refonly"}"
129 refname="$_refonly"
132 reftest="$(git rev-parse --revs-only --symbolic-full-name "$refname" -- 2>/dev/null)" &&
133 [ -n "$reftest" ]
134 } ||
136 reftest="$(git rev-parse --revs-only --symbolic-full-name "refs/tags/$refname" -- 2>/dev/null)" &&
137 [ -n "$reftest" ]
139 then
140 refname="$reftest$suffix"
141 else
142 if hash="$(git rev-parse --quiet --verify "$refname$suffix")"; then
143 refname="$hash"
144 else
145 refname="refs/tags/$refname$suffix"
148 esac
149 reftype=tag
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}"
155 while
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"
161 ! [ -s "$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
174 done
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"
180 create_ref_dirs
181 tg_ref_cache_only=1
182 tg_read_only=1
184 get_recorded_ref()
186 printf '%s\n' "$1" | join - "$trf" | cut -d ' ' -f 2 || :
189 show_topgit_heads()
191 topics="$(get_temp topics)"
192 topics2="$(get_temp topics)"
193 deplist="$(get_temp deplist)"
194 <"$trf" >"$topics" \
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"
201 topics="$topics2"
202 join -v 1 "$topics" "$deplist"
205 show_indep_heads()
207 srt="$(get_temp sort)"
208 sort -b -k2,2 <"$trf" >"$srt"
209 git merge-base --independent $(cut -d ' ' -f 2 <"$srt") |
210 sort -b -k1,1 |
211 join -2 2 -o 2.1 - "$srt" |
212 sort
215 show_heads()
217 if [ -n "$headstopgit" ]; then
218 show_topgit_heads "$@"
219 else
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
227 set -- $(show_heads)
230 is_tgish() {
231 case "$1" in
232 refs/"$topbases"/*)
233 ref_exists "refs/heads/${1#refs/$topbases/}"
235 refs/heads/*)
236 ref_exists "refs/$topbases/${1#refs/heads/}"
241 esac
244 refs=
245 for b; do
246 exp=
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
252 case "$rn" in
253 refs/"$topbases"/*)
254 refs="$refs refs/heads/${rn#refs/$topbases/}"
256 refs/heads/*)
257 refs="$refs refs/$topbases/${rn#refs/heads/}"
259 esac
261 esac
262 done
264 show_dep() {
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"
274 show_deps()
276 no_remotes=1
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
282 printf '%s\n' "$_b"
283 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
288 seen_deps=
289 _dep="$_b"; _dep_is_tgish=1; show_dep
290 recurse_deps show_dep "$_b"
291 recurse_deps_exclude="$recurse_deps_exclude $seen_deps"
292 done
295 show_rdep()
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"
302 else
303 printf '%s %s\n' "$_depchain" "${_depshort}~$_dep"
307 show_rdeps()
309 no_remotes=1
310 show_break=
311 seen_deps=
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
318 showbreak=1
319 v_ref_exists_rev_short _bshort "refs/heads/$_b" $short
320 if [ -n "$hashonly" ]; then
321 printf '%s\n' "$_bshort"
322 else
323 printf '%s\n' "${_bshort}~$_b"
325 continue
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
332 showbreak=1
333 v_ref_exists_rev_short _bshort "refs/heads/$_b" $short
335 if [ -n "$hashonly" ]; then
336 printf '%s\n' "$_bshort"
337 else
338 printf '%s\n' "${_bshort}~$_b"
340 recurse_preorder=1
341 recurse_deps show_rdep "$_b"
342 } | sed -e 's/[^ ][^ ]*[ ]/ /g' -e 's/~/ /'
343 done
346 refslist() {
347 [ -z "$refs" ] || sed 'y/ /\n/' <<-EOT
348 $refs
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'
358 else
359 <"$trf" sed -n 's/^\([^ ][^ ]*\) \([^ ][^ ]*\)$/\2 \1/p'
361 exit
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" --)"
369 else
370 printf '%s %s\n' "$(git rev-parse --verify --quiet $short "$rev" --)" "$name"
372 done <"$trf"
373 exit 0
375 if [ -n "$deps" ]; then
376 refslist | show_deps | sort -u -b -k1,1 |
377 join - "$trf" |
378 while read -r name rev; do
379 if [ -n "$hashonly" ]; then
380 printf '%s\n' "$(git rev-parse --verify --quiet $short "$rev" --)"
381 else
382 printf '%s %s\n' "$(git rev-parse --verify --quiet $short "$rev" --)" "$name"
384 done
385 exit 0
387 refslist | show_rdeps
388 exit 0
390 insn="$(get_temp isns)"
392 get_short() {
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"
404 else
405 refslist | show_deps | sort -u -b -k1,1 |
406 join - "$trf" |
407 while read -r name rev; do
408 printf 'revert %s %s\n' "$(get_short "$rev")" "$name"
409 done >"$insn"
411 if [ -n "$interact" ]; then
412 count=$(( $(wc -l <"$insn") ))
413 cat <<EOT >>"$insn"
415 # Revert using $refname data ($count command(s))
417 # Commands:
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"
436 esac
437 ref_exists "$ref" ||
438 die "unknown ref in instruction: $op $hash $ref"
439 done <"$insn"
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"
445 else
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 ))
454 srh=
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^{}" --)" || :
459 init_reflog "$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"
466 detachat="$orig"
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
476 hdr=' '
477 [ -z "$dryrun" ] || hdr='-'
478 printf '%s %s -> %s %s\n' "$hdr" "$origsh" "$revsh" "$name"
479 else
480 hdr='*'
481 [ -z "$dryrun" ] || hdr='-'
482 printf '%s %s -> %s %s\n' "$hdr" "$nullref" "$revsh" "$name"
485 else
486 : #[ -z "$dryrun" ] && [ -n "$quiet" ] || printf "* %-*s %s\n" $notewidth "[no change]" "$name"
488 done
490 exit 0