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) [(--heads | <ref>...)]"
12 if [ "${1:-0}" != 0 ]; then
13 printf '%s\n' "$USAGE" >&2
15 printf '%s\n' "$USAGE"
36 while [ $# -gt 0 ]; do case "$1" in
46 --short|
--short=*|
--no-short)
77 [ -n "${1#--exclude=}" ] || die
"--exclude= requires a ref name"
78 case "${1#--exclude=}" in refs/*) rn="${1#--exclude=}";; *) rn="refs/heads/${1#--exclude=} refs/$topbases/${1#--exclude=}"; esac
79 exclude
="$exclude $rn";;
82 [ -n "$1" ] || die
"--exclude requires a ref name"
83 case "$1" in refs
/*) rn
="$1";; *) rn
="refs/heads/$1 refs/$topbases/$1"; esac
84 exclude
="$exclude $rn";;
89 --stash|
--stash"@{"*"}")
93 echo "Unknown option: $1" >&2
100 [ -z "$exclude" ] || exclude
="$exclude "
102 [ -z "$list$short$hashonly" -o -z "$force$interact$dryrun$nodeps$nostash" ] || usage
1
103 [ -z "$force$interact$dryrun" -o -z "$list$short$hashonly$deps$rdeps" ] || usage
1
104 [ -z "$deps" -o -z "$rdeps" ] || usage
1
105 [ -n "$list$force$interact$dryrun" ] || list
=1
106 [ -z "$list" -o -n "$short" ] ||
if [ -n "$hashonly" ]; then short
="--no-short"; else short
="--short"; fi
107 [ -n "$1" ] ||
{ echo "Tag name required" >&2; usage
1; }
110 [ -n "$list" -o "$1" != "--heads" ] || usage
1
111 [ "$tagname" != "--stash" ] || tagname
=refs
/tgstash
112 case "$tagname" in --stash"@{"*"}")
113 strip
="${tagname#--stash??}"
115 tagname
="refs/tgstash@{$strip}"
118 case "$refname" in HEAD|refs
/*);;*)
119 suffix
="${refname%@*}"
120 suffix
="${refname#$suffix}"
121 refname
="${refname%$suffix}"
122 if reftest
="$(git rev-parse --revs-only --symbolic-full-name "$refname" -- 2>/dev/null)" &&
123 [ -n "$reftest" ]; then
124 refname
="$reftest$suffix"
126 if hash="$(git rev-parse --quiet --verify "$refname$suffix")"; then
129 refname
="refs/tags/$refname$suffix"
134 case "$refname" in refs
/tags
/*) tagname
="${refname#refs/tags/}";; *) reftype
=ref
; tagname
="$refname"; esac
135 git rev-parse
--verify --quiet "$refname^{tag}" -- >/dev
/null || die
"not annotated/signed tag: $refname"
136 tgf
="$(get_temp tag)"
137 trf
="$(get_temp refs)"
138 tagdataref
="$refname^{tag}"
140 git cat-file tag
"$tagdataref" >"$tgf" || die
"cannot read tag: $refname"
141 sed -ne '/^-----BEGIN TOPGIT REFS-----$/,/^-----END TOPGIT REFS-----$/p' <"$tgf" |
142 sed -ne "/^\\($octet20\\) \\(refs\/[^ $tab][^ $tab]*\\)\$/{s//\\2 \\1/;p;}" |
143 sed -e "s,^refs/$oldbases/,refs/$topbases/,g" |
144 LC_ALL
=C
sort -u -b -k1,1 >"$trf"
147 # If it's a tag of a tag, dereference it and try again
148 read -r field tagtype
<<-EOT || break
149 $(sed -n '1,/^$/p' <"$tgf" | grep '^type [^ ][^ ]*$' || :)
151 [ "$tagtype" = "tag" ] ||
break
152 read -r field tagdataref
<<-EOT || break
153 $(sed -n '1,/^$/p' <"$tgf" | grep '^object [^ ][^ ]*$' || :)
155 [ -n "$tagdataref" ] ||
break
156 tagdataref
="$tagdataref^{tag}"
157 git rev-parse
--verify --quiet "$tagdataref" -- >/dev
/null ||
break
159 [ -s "$trf" ] || die
"$reftype $tagname does not contain a TOPGIT REFS section"
160 rcnt
=$
(( $
(wc -l <"$trf") ))
161 vcnt
=$
(( $
(cut
-d ' ' -f 2 <"$trf" | git cat-file
--batch-check='%(objectname)' |
grep -v ' missing$' |
wc -l) ))
162 [ "$rcnt" -eq "$vcnt" ] || die
"$reftime $tagname contains $rcnt ref(s) but only $vcnt are still valid"
163 cat "$trf" >"$tg_ref_cache"
168 [ $# -ne 0 -o -z "$rdeps$deps" ] ||
set -- --heads
169 [ $# -ne 1 -o -z "$deps" -o "$1" != "--heads" ] ||
{ deps
=; set --; }
170 if [ $# -eq 1 -a "$1" = "--heads" ]; then
171 srt
="$(get_temp sort)"
172 LC_ALL
=C
sort -b -k2,2 <"$trf" >"$srt"
174 git merge-base
--independent $
(cut
-d ' ' -f 2 <"$srt") |
175 LC_ALL
=C
sort -b -k1,1 |
176 join -2 2 -o 2.1 - "$srt" |
183 ref_exists
"refs/heads/${1#refs/$topbases/}"
186 ref_exists
"refs/$topbases/${1#refs/heads/}"
197 case "$b" in refs
/*) exp
=1; rn
="$b";; *) rn
="refs/heads/$b"; esac
198 ref_exists
"$rn" || die
"not present in tag data (try --list): $rn"
199 case " $refs " in *" $rn "*);;*)
200 refs
="${refs:+$refs }$rn"
201 if [ -z "$list" ] && [ -z "$nodeps" -o -z "$exp" ] && is_tgish
"$rn"; then
204 refs
="$refs refs/heads/${rn#refs/$topbases/}"
207 refs
="$refs refs/$topbases/${rn#refs/heads/}"
215 case "$exclude" in *" refs/heads/$_dep "*) return; esac
216 case " $seen_deps " in *" $_dep "*) return 0; esac
217 seen_deps
="${seen_deps:+$seen_deps }$_dep"
218 [ -z "$tgish" -o -n "$_dep_is_tgish" ] ||
return 0
219 printf 'refs/heads/%s\n' "$_dep"
220 [ -z "$_dep_is_tgish" ] ||
221 printf 'refs/%s/%s\n' "$topbases" "$_dep"
227 recurse_deps_exclude
=
228 while read _b
&& [ -n "$_b" ]; do
229 case "$exclude" in *" $_b "*) continue; esac
230 if ! is_tgish
"$_b"; then
231 [ -z "$tgish" ] ||
continue
235 case "$_b" in refs
/"$topbases"/*) _b
="refs/heads/${_b#refs/$topbases/}"; esac
236 _b
="${_b#refs/heads/}"
237 case " $recurse_deps_exclude " in *" $_b "*) continue; esac
239 _dep
="$_b"; _dep_is_tgish
=1; show_dep
240 recurse_deps show_dep
"$_b"
241 recurse_deps_exclude
="$recurse_deps_exclude $seen_deps"
247 case "$exclude" in *" refs/heads/$_dep "*) return; esac
248 [ -z "$tgish" -o -n "$_dep_is_tgish" ] ||
return 0
249 if [ -n "$hashonly" ]; then
250 printf '%s %s\n' "$_depchain" "$(ref_exists_rev_short "refs
/heads
/$_dep" $short)"
252 printf '%s %s\n' "$_depchain" "$(ref_exists_rev_short "refs
/heads
/$_dep" $short)~$_dep"
261 while read _b
&& [ -n "$_b" ]; do
262 case "$exclude" in *" $_b "*) continue; esac
263 if ! is_tgish
"$_b"; then
264 [ -z "$tgish" ] ||
continue
265 [ -z "$showbreak" ] ||
echo
267 if [ -n "$hashonly" ]; then
268 printf '%s\n' "$(ref_exists_rev_short "refs
/heads
/$_b" $short)"
270 printf '%s\n' "$(ref_exists_rev_short "refs
/heads
/$_b" $short)~$_b"
274 case "$_b" in refs
/"$topbases"/*) _b
="refs/heads/${_b#refs/$topbases/}"; esac
275 _b
="${_b#refs/heads/}"
276 case " $seen_deps " in *" $_b "*) continue; esac
277 seen_deps
="$seen_deps $_b"
278 [ -z "$showbreak" ] ||
echo
281 if [ -n "$hashonly" ]; then
282 printf '%s\n' "$(ref_exists_rev_short "refs
/heads
/$_b" $short)"
284 printf '%s\n' "$(ref_exists_rev_short "refs
/heads
/$_b" $short)~$_b"
287 recurse_deps show_rdep
"$_b"
288 } |
sed -e 's/[^ ][^ ]*[ ]/ /g' -e 's/~/ /'
293 [ -z "$refs" ] ||
sed 'y/ /\n/' <<-EOT
298 if [ -n "$list" ]; then
299 if [ -z "$deps$rdeps" ]; then
300 while read -r name
rev; do
301 case "$exclude" in *" $name "*) continue; esac
302 [ -z "$refs" ] ||
case " $refs " in *" $name "*);;*) continue; esac
303 [ -z "$tgish" ] || is_tgish
"$name" ||
continue
304 if [ -n "$hashonly" ]; then
305 printf '%s\n' "$(git rev-parse --verify --quiet $short "$rev" --)"
307 printf '%s %s\n' "$(git rev-parse --verify --quiet $short "$rev" --)" "$name"
312 if [ -n "$deps" ]; then
313 refslist | show_deps | LC_ALL
=C
sort -u -b -k1,1 |
315 while read -r name
rev; do
316 if [ -n "$hashonly" ]; then
317 printf '%s\n' "$(git rev-parse --verify --quiet $short "$rev" --)"
319 printf '%s %s\n' "$(git rev-parse --verify --quiet $short "$rev" --)" "$name"
324 refslist | show_rdeps
327 insn
="$(get_temp isns)"
330 [ -n "$interact" ] ||
{ printf '%s' "$1"; return 0; }
331 git rev-parse
--verify --quiet --short "$1" --
334 if [ -n "$nodeps" -o -z "$refs" ]; then
335 while read -r name
rev; do
336 case "$exclude" in *" $name "*) continue; esac
337 [ -z "$refs" ] ||
case " $refs " in *" $name "*);;*) continue; esac
338 [ -z "$tgish" ] || is_tgish
"$name" ||
continue
339 printf 'revert %s %s\n' "$(get_short "$rev")" "$name"
340 done <"$trf" | LC_ALL
=C
sort -u -b -k3,3 >"$insn"
342 refslist | show_deps | LC_ALL
=C
sort -u -b -k1,1 |
344 while read -r name
rev; do
345 printf 'revert %s %s\n' "$(get_short "$rev")" "$name"
348 if [ -n "$interact" ]; then
349 count
=$
(( $
(wc -l <"$insn") ))
352 # Revert using $refname data ($count command(s))
355 # r, revert = revert ref to specified hash
357 # Note that changing the hash value shown here will have NO EFFECT.
359 # If you remove a line here THAT REVERT WILL BE SKIPPED.
361 # However, if you remove everything, the revert will be aborted.
363 run_editor
"$insn" ||
364 die
"there was a problem with the editor '$tg_editor'"
365 git stripspace
-s <"$insn" >"$insn"+
366 mv -f "$insn"+ "$insn"
367 [ -s "$insn" ] || die
"nothing to do"
368 while read -r op
hash ref
; do
369 [ "$op" = "r" -o "$op" = "revert" ] ||
370 die
"invalid op in instruction: $op $hash $ref"
371 case "$ref" in refs
/?
*);;*)
372 die
"invalid ref in instruction: $op $hash $ref"
375 die
"unknown ref in instruction: $op $hash $ref"
378 msg
="tgrevert: $reftype $tagname ($(( $(wc -l <"$insn") )) command(s))"
379 [ -n "$dryrun" -o -n "$nostash" ] ||
$tg tag
-q -q --none-ok -m "$msg" --stash || die
"requested --stash failed"
380 refwidth
="$(git config --get --int core.abbrev 2>/dev/null)" ||
:
381 [ -n "$refwidth" ] || refwidth
=7
382 [ $refwidth -ge 4 -a $refwidth -le 40 ] || refwidth
=7
383 nullref
="$(printf '%.*s' $refwidth "$nullsha")"
384 notewidth
=$
(( $refwidth + 4 + $refwidth ))
386 [ -n "$dryrun" ] || srh
="$(git symbolic-ref --quiet HEAD)" ||
:
387 cut
-d ' ' -f 3 <"$insn" | LC_ALL
=C
sort -u -b -k1,1 |
join - "$trf" |
388 while read -r name
rev; do
389 orig
="$(git rev-parse --verify --quiet "$name" --)" ||
:
391 if [ "$rev" != "$orig" ]; then
392 [ -z "$dryrun" -a -n "$quiet" ] ||
393 origsh
="$(git rev-parse --verify --short --quiet "$name" --)" ||
:
394 if [ -z "$dryrun" ]; then
395 if [ -n "$srh" ] && [ "$srh" = "$name" ]; then
396 [ -n "$quiet" ] ||
echo "Detaching HEAD to revert $name"
398 [ -n "$detachat" ] || detachat
="$(make_empty_commit)"
399 git update-ref
-m "tgrevert: detach HEAD to revert $name" --no-deref HEAD
"$detachat"
400 [ -n "$quiet" ] || git log
-n 1 --format=format
:'HEAD is now at %h... %s' HEAD
402 git update-ref
-m "$msg" "$name" "$rev"
404 if [ -n "$dryrun" -o -z "$quiet" ]; then
405 revsh
="$(git rev-parse --verify --short --quiet "$rev" --)" ||
:
406 if [ -n "$origsh" ]; then
408 [ -z "$dryrun" ] || hdr
='-'
409 printf '%s %s -> %s %s\n' "$hdr" "$origsh" "$revsh" "$name"
412 [ -z "$dryrun" ] || hdr
='-'
413 printf '%s %s -> %s %s\n' "$hdr" "$nullref" "$revsh" "$name"
417 : #[ -z "$dryrun" -a -n "$quiet" ] || printf "* %-*s %s\n" $notewidth "[no change]" "$name"