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" -o -z "$force$interact$dryrun$nodeps$nostash" ] || usage
1
104 [ -z "$force$interact$dryrun" -o -z "$list$short$hashonly$deps$rdeps" ] || usage
1
105 [ -z "$deps" -o -z "$rdeps" ] || usage
1
106 [ -n "$list$force$interact$dryrun" ] || list
=1
107 [ -z "$list" -o -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" -o "$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
/*);;*)
126 suffix
="${refname%@*}"
127 suffix
="${refname#$suffix}"
128 refname
="${refname%$suffix}"
129 if reftest
="$(git rev-parse --revs-only --symbolic-full-name "$refname" -- 2>/dev/null)" &&
130 [ -n "$reftest" ]; then
131 refname
="$reftest$suffix"
133 if hash="$(git rev-parse --quiet --verify "$refname$suffix")"; then
136 refname
="refs/tags/$refname$suffix"
141 case "$refname" in refs
/tags
/*) tagname
="${refname#refs/tags/}";; *) reftype
=ref
; tagname
="$refname"; esac
142 git rev-parse
--verify --quiet "$refname^{tag}" -- >/dev
/null || die
"not annotated/signed tag: $refname"
143 tgf
="$(get_temp tag)"
144 trf
="$(get_temp refs)"
145 tagdataref
="$refname^{tag}"
147 git cat-file tag
"$tagdataref" >"$tgf" || die
"cannot read tag: $refname"
148 sed -ne '/^-----BEGIN TOPGIT REFS-----$/,/^-----END TOPGIT REFS-----$/p' <"$tgf" |
149 sed -ne "/^\\($octet20\\) \\(refs\/[^ $tab][^ $tab]*\\)\$/{s//\\2 \\1/;p;}" |
150 sed -e "s,^refs/$oldbases/,refs/$topbases/,g" |
151 sort -u -b -k1,1 >"$trf"
154 # If it's a tag of a tag, dereference it and try again
155 read -r field tagtype
<<-EOT || break
156 $(sed -n '1,/^$/p' <"$tgf" | grep '^type [^ ][^ ]*$' || :)
158 [ "$tagtype" = "tag" ] ||
break
159 read -r field tagdataref
<<-EOT || break
160 $(sed -n '1,/^$/p' <"$tgf" | grep '^object [^ ][^ ]*$' || :)
162 [ -n "$tagdataref" ] ||
break
163 tagdataref
="$tagdataref^{tag}"
164 git rev-parse
--verify --quiet "$tagdataref" -- >/dev
/null ||
break
166 [ -s "$trf" ] || die
"$reftype $tagname does not contain a TOPGIT REFS section"
167 rcnt
=$
(( $
(wc -l <"$trf") ))
168 vcnt
=$
(( $
(cut
-d ' ' -f 2 <"$trf" | git cat-file
--batch-check='%(objectname)' |
grep -v ' missing$' |
wc -l) ))
169 [ "$rcnt" -eq "$vcnt" ] || die
"$reftime $tagname contains $rcnt ref(s) but only $vcnt are still valid"
170 cat "$trf" >"$tg_ref_cache"
177 printf '%s\n' "$1" |
join - "$trf" | cut
-d ' ' -f 2 ||
:
182 topics
="$(get_temp topics)"
183 topics2
="$(get_temp topics)"
184 deplist
="$(get_temp deplist)"
186 sed -e '\,^refs/'"$topbasesrx"'/,!d' -e 's,^refs/'"$topbasesrx"'/,refs/heads/,' -e 's/ .*//'
187 while read -r oneref
; do
188 _rev
="$(get_recorded_ref "$oneref")" && [ -n "$_rev" ] ||
continue
189 printf '%s\n' "$oneref" >>"$topics2"
190 git cat-file blob
"$_rev:.topdeps" 2>/dev
/null |
awk '{print}'
191 done <"$topics" |
sed -e 's,^,refs/heads/,' |
sort -u >"$deplist"
193 join -v 1 "$topics" "$deplist"
198 srt
="$(get_temp sort)"
199 sort -b -k2,2 <"$trf" >"$srt"
200 git merge-base
--independent $
(cut
-d ' ' -f 2 <"$srt") |
202 join -2 2 -o 2.1 - "$srt" |
208 if [ -n "$headstopgit" ]; then
209 show_topgit_heads
"$@"
211 show_indep_heads
"$@"
215 [ $# -ne 0 -o -z "$rdeps$deps" ] ||
{ set -- --heads; headstopgit
=1; }
216 [ $# -ne 1 -o -z "$deps" -o "$1" != "--heads" ] ||
{ deps
=; set --; }
217 if [ $# -eq 1 -a "$1" = "--heads" ]; then
224 ref_exists
"refs/heads/${1#refs/$topbases/}"
227 ref_exists
"refs/$topbases/${1#refs/heads/}"
238 case "$b" in refs
/*) exp
=1; rn
="$b";; *) rn
="refs/heads/$b"; esac
239 ref_exists
"$rn" || die
"not present in tag data (try --list): $rn"
240 case " $refs " in *" $rn "*);;*)
241 refs
="${refs:+$refs }$rn"
242 if [ -z "$list" ] && [ -z "$nodeps" -o -z "$exp" ] && is_tgish
"$rn"; then
245 refs
="$refs refs/heads/${rn#refs/$topbases/}"
248 refs
="$refs refs/$topbases/${rn#refs/heads/}"
256 case "$exclude" in *" refs/heads/$_dep "*) return; esac
257 case " $seen_deps " in *" $_dep "*) return 0; esac
258 seen_deps
="${seen_deps:+$seen_deps }$_dep"
259 [ -z "$tgish" -o -n "$_dep_is_tgish" ] ||
return 0
260 printf 'refs/heads/%s\n' "$_dep"
261 [ -z "$_dep_is_tgish" ] ||
262 printf 'refs/%s/%s\n' "$topbases" "$_dep"
268 recurse_deps_exclude
=
269 while read _b
&& [ -n "$_b" ]; do
270 case "$exclude" in *" $_b "*) continue; esac
271 if ! is_tgish
"$_b"; then
272 [ -z "$tgish" ] ||
continue
276 case "$_b" in refs
/"$topbases"/*) _b
="refs/heads/${_b#refs/$topbases/}"; esac
277 _b
="${_b#refs/heads/}"
278 case " $recurse_deps_exclude " in *" $_b "*) continue; esac
280 _dep
="$_b"; _dep_is_tgish
=1; show_dep
281 recurse_deps show_dep
"$_b"
282 recurse_deps_exclude
="$recurse_deps_exclude $seen_deps"
288 case "$exclude" in *" refs/heads/$_dep "*) return; esac
289 [ -z "$tgish" -o -n "$_dep_is_tgish" ] ||
return 0
290 if [ -n "$hashonly" ]; then
291 printf '%s %s\n' "$_depchain" "$(ref_exists_rev_short "refs
/heads
/$_dep" $short)"
293 printf '%s %s\n' "$_depchain" "$(ref_exists_rev_short "refs
/heads
/$_dep" $short)~$_dep"
302 while read _b
&& [ -n "$_b" ]; do
303 case "$exclude" in *" $_b "*) continue; esac
304 if ! is_tgish
"$_b"; then
305 [ -z "$tgish" ] ||
continue
306 [ -z "$showbreak" ] ||
echo
308 if [ -n "$hashonly" ]; then
309 printf '%s\n' "$(ref_exists_rev_short "refs
/heads
/$_b" $short)"
311 printf '%s\n' "$(ref_exists_rev_short "refs
/heads
/$_b" $short)~$_b"
315 case "$_b" in refs
/"$topbases"/*) _b
="refs/heads/${_b#refs/$topbases/}"; esac
316 _b
="${_b#refs/heads/}"
317 case " $seen_deps " in *" $_b "*) continue; esac
318 seen_deps
="$seen_deps $_b"
319 [ -z "$showbreak" ] ||
echo
322 if [ -n "$hashonly" ]; then
323 printf '%s\n' "$(ref_exists_rev_short "refs
/heads
/$_b" $short)"
325 printf '%s\n' "$(ref_exists_rev_short "refs
/heads
/$_b" $short)~$_b"
328 recurse_deps show_rdep
"$_b"
329 } |
sed -e 's/[^ ][^ ]*[ ]/ /g' -e 's/~/ /'
334 [ -z "$refs" ] ||
sed 'y/ /\n/' <<-EOT
339 if [ -n "$list" ]; then
340 if [ -z "$deps$rdeps" ]; then
341 while read -r name
rev; do
342 case "$exclude" in *" $name "*) continue; esac
343 [ -z "$refs" ] ||
case " $refs " in *" $name "*);;*) continue; esac
344 [ -z "$tgish" ] || is_tgish
"$name" ||
continue
345 if [ -n "$hashonly" ]; then
346 printf '%s\n' "$(git rev-parse --verify --quiet $short "$rev" --)"
348 printf '%s %s\n' "$(git rev-parse --verify --quiet $short "$rev" --)" "$name"
353 if [ -n "$deps" ]; then
354 refslist | show_deps |
sort -u -b -k1,1 |
356 while read -r name
rev; do
357 if [ -n "$hashonly" ]; then
358 printf '%s\n' "$(git rev-parse --verify --quiet $short "$rev" --)"
360 printf '%s %s\n' "$(git rev-parse --verify --quiet $short "$rev" --)" "$name"
365 refslist | show_rdeps
368 insn
="$(get_temp isns)"
371 [ -n "$interact" ] ||
{ printf '%s' "$1"; return 0; }
372 git rev-parse
--verify --quiet --short "$1" --
375 if [ -n "$nodeps" -o -z "$refs" ]; then
376 while read -r name
rev; do
377 case "$exclude" in *" $name "*) continue; esac
378 [ -z "$refs" ] ||
case " $refs " in *" $name "*);;*) continue; esac
379 [ -z "$tgish" ] || is_tgish
"$name" ||
continue
380 printf 'revert %s %s\n' "$(get_short "$rev")" "$name"
381 done <"$trf" |
sort -u -b -k3,3 >"$insn"
383 refslist | show_deps |
sort -u -b -k1,1 |
385 while read -r name
rev; do
386 printf 'revert %s %s\n' "$(get_short "$rev")" "$name"
389 if [ -n "$interact" ]; then
390 count
=$
(( $
(wc -l <"$insn") ))
393 # Revert using $refname data ($count command(s))
396 # r, revert = revert ref to specified hash
398 # Note that changing the hash value shown here will have NO EFFECT.
400 # If you remove a line here THAT REVERT WILL BE SKIPPED.
402 # However, if you remove everything, the revert will be aborted.
404 run_editor
"$insn" ||
405 die
"there was a problem with the editor '$tg_editor'"
406 git stripspace
-s <"$insn" >"$insn"+
407 mv -f "$insn"+ "$insn"
408 [ -s "$insn" ] || die
"nothing to do"
409 while read -r op
hash ref
; do
410 [ "$op" = "r" -o "$op" = "revert" ] ||
411 die
"invalid op in instruction: $op $hash $ref"
412 case "$ref" in refs
/?
*);;*)
413 die
"invalid ref in instruction: $op $hash $ref"
416 die
"unknown ref in instruction: $op $hash $ref"
419 msg
="tgrevert: $reftype $tagname ($(( $(wc -l <"$insn") )) command(s))"
420 [ -n "$dryrun" -o -n "$nostash" ] || tg tag
-q -q --none-ok -m "$msg" --stash || die
"requested --stash failed"
421 refwidth
="$(git config --get --int core.abbrev 2>/dev/null)" ||
:
422 [ -n "$refwidth" ] || refwidth
=7
423 [ $refwidth -ge 4 -a $refwidth -le 40 ] || refwidth
=7
424 nullref
="$(printf '%.*s' $refwidth "$nullsha")"
425 notewidth
=$
(( $refwidth + 4 + $refwidth ))
427 [ -n "$dryrun" ] || srh
="$(git symbolic-ref --quiet HEAD)" ||
:
428 cut
-d ' ' -f 3 <"$insn" |
sort -u -b -k1,1 |
join - "$trf" |
429 while read -r name
rev; do
430 orig
="$(git rev-parse --verify --quiet "$name" --)" ||
:
432 if [ "$rev" != "$orig" ]; then
433 [ -z "$dryrun" -a -n "$quiet" ] ||
434 origsh
="$(git rev-parse --verify --short --quiet "$name" --)" ||
:
435 if [ -z "$dryrun" ]; then
436 if [ -n "$srh" ] && [ "$srh" = "$name" ]; then
437 [ -n "$quiet" ] ||
echo "Detaching HEAD to revert $name"
439 [ -n "$detachat" ] || detachat
="$(make_empty_commit)"
440 git update-ref
-m "tgrevert: detach HEAD to revert $name" --no-deref HEAD
"$detachat"
441 [ -n "$quiet" ] || git
--no-pager log
-n 1 --format=format
:'HEAD is now at %h... %s' HEAD
443 git update-ref
-m "$msg" "$name" "$rev"
445 if [ -n "$dryrun" -o -z "$quiet" ]; then
446 revsh
="$(git rev-parse --verify --short --quiet "$rev" --)" ||
:
447 if [ -n "$origsh" ]; then
449 [ -z "$dryrun" ] || hdr
='-'
450 printf '%s %s -> %s %s\n' "$hdr" "$origsh" "$revsh" "$name"
453 [ -z "$dryrun" ] || hdr
='-'
454 printf '%s %s -> %s %s\n' "$hdr" "$nullref" "$revsh" "$name"
458 : #[ -z "$dryrun" -a -n "$quiet" ] || printf "* %-*s %s\n" $notewidth "[no change]" "$name"