2 # TopGit revert command
3 # Copyright (C) 2015 Kyle J. McKay <mackyle@gmail.com>
7 lf
="$(printf '\n.')" && lf
="${lf%?}"
8 tab
="$(printf '\t.')" && tab
="${tab%?}"
9 USAGE
="Usage: ${tgname:-tg} [...] revert (-f | -i | -n) [-q] [--tgish-only] [--no-deps] [--no-stash] [--exclude <ref>...] (<tagname> | --stash) [<ref>...]"
10 USAGE
="$USAGE$lf Or: ${tgname:-tg} [...] revert [-l] [--no-short] [--hash] [--tgish-only] [(--deps | --rdeps)] [--exclude <ref>...] (<tagname> | --stash) [(--heads | <ref>...)]"
14 if [ "${1:-0}" != 0 ]; then
15 printf '%s\n' "$USAGE" >&2
17 printf '%s\n' "$USAGE"
38 while [ $# -gt 0 ]; do case "$1" in
48 --short|
--short=*|
--no-short)
79 [ -n "${1#--exclude=}" ] || die
"--exclude= requires a ref name"
80 case "${1#--exclude=}" in refs/*) rn="${1#--exclude=}";; *) rn="refs/heads/${1#--exclude=} refs/$topbases/${1#--exclude=}"; esac
81 exclude
="$exclude $rn";;
84 [ -n "$1" ] || die
"--exclude requires a ref name"
85 case "$1" in refs
/*) rn
="$1";; *) rn
="refs/heads/$1 refs/$topbases/$1"; esac
86 exclude
="$exclude $rn";;
91 --stash|
--stash"@{"*"}")
95 echo "Unknown option: $1" >&2
102 [ -z "$exclude" ] || exclude
="$exclude "
104 [ -z "$list$short$hashonly" -o -z "$force$interact$dryrun$nodeps$nostash" ] || usage
1
105 [ -z "$force$interact$dryrun" -o -z "$list$short$hashonly$deps$rdeps" ] || usage
1
106 [ -z "$deps" -o -z "$rdeps" ] || usage
1
107 [ -n "$list$force$interact$dryrun" ] || list
=1
108 [ -z "$list" -o -n "$short" ] ||
if [ -n "$hashonly" ]; then short
="--no-short"; else short
="--short"; fi
109 [ -n "$1" ] ||
{ echo "Tag name required" >&2; usage
1; }
112 [ -n "$list" -o "$1" != "--heads" ] || usage
1
113 [ "$tagname" != "--stash" ] || tagname
=refs
/tgstash
114 case "$tagname" in --stash"@{"*"}")
115 strip
="${tagname#--stash??}"
117 tagname
="refs/tgstash@{$strip}"
120 case "$refname" in HEAD|refs
/*);;*)
121 suffix
="${refname%@*}"
122 suffix
="${refname#$suffix}"
123 refname
="${refname%$suffix}"
124 if reftest
="$(git rev-parse --revs-only --symbolic-full-name "$refname" -- 2>/dev/null)" &&
125 [ -n "$reftest" ]; then
126 refname
="$reftest$suffix"
128 if hash="$(git rev-parse --quiet --verify "$refname$suffix")"; then
131 refname
="refs/tags/$refname$suffix"
136 case "$refname" in refs
/tags
/*) tagname
="${refname#refs/tags/}";; *) reftype
=ref
; tagname
="$refname"; esac
137 git rev-parse
--verify --quiet "$refname^{tag}" -- >/dev
/null || die
"not annotated/signed tag: $refname"
138 tgf
="$(get_temp tag)"
139 trf
="$(get_temp refs)"
140 tagdataref
="$refname^{tag}"
142 git cat-file tag
"$tagdataref" >"$tgf" || die
"cannot read tag: $refname"
143 sed -ne '/^-----BEGIN TOPGIT REFS-----$/,/^-----END TOPGIT REFS-----$/p' <"$tgf" |
144 sed -ne "/^\\($octet20\\) \\(refs\/[^ $tab][^ $tab]*\\)\$/{s//\\2 \\1/;p;}" |
145 sed -e "s,^refs/$oldbases/,refs/$topbases/,g" |
146 LC_ALL
=C
sort -u -b -k1,1 >"$trf"
149 # If it's a tag of a tag, dereference it and try again
150 read -r field tagtype
<<-EOT || break
151 $(sed -n '1,/^$/p' <"$tgf" | grep '^type [^ ][^ ]*$' || :)
153 [ "$tagtype" = "tag" ] ||
break
154 read -r field tagdataref
<<-EOT || break
155 $(sed -n '1,/^$/p' <"$tgf" | grep '^object [^ ][^ ]*$' || :)
157 [ -n "$tagdataref" ] ||
break
158 tagdataref
="$tagdataref^{tag}"
159 git rev-parse
--verify --quiet "$tagdataref" -- >/dev
/null ||
break
161 [ -s "$trf" ] || die
"$reftype $tagname does not contain a TOPGIT REFS section"
162 rcnt
=$
(( $
(wc -l <"$trf") ))
163 vcnt
=$
(( $
(cut
-d ' ' -f 2 <"$trf" | git cat-file
--batch-check='%(objectname)' |
grep -v ' missing$' |
wc -l) ))
164 [ "$rcnt" -eq "$vcnt" ] || die
"$reftime $tagname contains $rcnt ref(s) but only $vcnt are still valid"
165 cat "$trf" >"$tg_ref_cache"
170 [ $# -ne 0 -o -z "$rdeps$deps" ] ||
set -- --heads
171 [ $# -ne 1 -o -z "$deps" -o "$1" != "--heads" ] ||
{ deps
=; set --; }
172 if [ $# -eq 1 -a "$1" = "--heads" ]; then
173 srt
="$(get_temp sort)"
174 LC_ALL
=C
sort -b -k2,2 <"$trf" >"$srt"
176 git merge-base
--independent $
(cut
-d ' ' -f 2 <"$srt") |
177 LC_ALL
=C
sort -b -k1,1 |
178 join -2 2 -o 2.1 - "$srt" |
185 ref_exists
"refs/heads/${1#refs/$topbases/}"
188 ref_exists
"refs/$topbases/${1#refs/heads/}"
199 case "$b" in refs
/*) exp
=1; rn
="$b";; *) rn
="refs/heads/$b"; esac
200 ref_exists
"$rn" || die
"not present in tag data (try --list): $rn"
201 case " $refs " in *" $rn "*);;*)
202 refs
="${refs:+$refs }$rn"
203 if [ -z "$list" ] && [ -z "$nodeps" -o -z "$exp" ] && is_tgish
"$rn"; then
206 refs
="$refs refs/heads/${rn#refs/$topbases/}"
209 refs
="$refs refs/$topbases/${rn#refs/heads/}"
217 case "$exclude" in *" refs/heads/$_dep "*) return; esac
218 case " $seen_deps " in *" $_dep "*) return 0; esac
219 seen_deps
="${seen_deps:+$seen_deps }$_dep"
220 [ -z "$tgish" -o -n "$_dep_is_tgish" ] ||
return 0
221 printf 'refs/heads/%s\n' "$_dep"
222 [ -z "$_dep_is_tgish" ] ||
223 printf 'refs/%s/%s\n' "$topbases" "$_dep"
229 recurse_deps_exclude
=
230 while read _b
&& [ -n "$_b" ]; do
231 case "$exclude" in *" $_b "*) continue; esac
232 if ! is_tgish
"$_b"; then
233 [ -z "$tgish" ] ||
continue
237 case "$_b" in refs
/"$topbases"/*) _b
="refs/heads/${_b#refs/$topbases/}"; esac
238 _b
="${_b#refs/heads/}"
239 case " $recurse_deps_exclude " in *" $_b "*) continue; esac
241 _dep
="$_b"; _dep_is_tgish
=1; show_dep
242 recurse_deps show_dep
"$_b"
243 recurse_deps_exclude
="$recurse_deps_exclude $seen_deps"
249 case "$exclude" in *" refs/heads/$_dep "*) return; esac
250 [ -z "$tgish" -o -n "$_dep_is_tgish" ] ||
return 0
251 if [ -n "$hashonly" ]; then
252 printf '%s %s\n' "$_depchain" "$(ref_exists_rev_short "refs
/heads
/$_dep" $short)"
254 printf '%s %s\n' "$_depchain" "$(ref_exists_rev_short "refs
/heads
/$_dep" $short)~$_dep"
263 while read _b
&& [ -n "$_b" ]; do
264 case "$exclude" in *" $_b "*) continue; esac
265 if ! is_tgish
"$_b"; then
266 [ -z "$tgish" ] ||
continue
267 [ -z "$showbreak" ] ||
echo
269 if [ -n "$hashonly" ]; then
270 printf '%s\n' "$(ref_exists_rev_short "refs
/heads
/$_b" $short)"
272 printf '%s\n' "$(ref_exists_rev_short "refs
/heads
/$_b" $short)~$_b"
276 case "$_b" in refs
/"$topbases"/*) _b
="refs/heads/${_b#refs/$topbases/}"; esac
277 _b
="${_b#refs/heads/}"
278 case " $seen_deps " in *" $_b "*) continue; esac
279 seen_deps
="$seen_deps $_b"
280 [ -z "$showbreak" ] ||
echo
283 if [ -n "$hashonly" ]; then
284 printf '%s\n' "$(ref_exists_rev_short "refs
/heads
/$_b" $short)"
286 printf '%s\n' "$(ref_exists_rev_short "refs
/heads
/$_b" $short)~$_b"
289 recurse_deps show_rdep
"$_b"
290 } |
sed -e 's/[^ ][^ ]*[ ]/ /g' -e 's/~/ /'
295 [ -z "$refs" ] ||
sed 'y/ /\n/' <<-EOT
300 if [ -n "$list" ]; then
301 if [ -z "$deps$rdeps" ]; then
302 while read -r name
rev; do
303 case "$exclude" in *" $name "*) continue; esac
304 [ -z "$refs" ] ||
case " $refs " in *" $name "*);;*) continue; esac
305 [ -z "$tgish" ] || is_tgish
"$name" ||
continue
306 if [ -n "$hashonly" ]; then
307 printf '%s\n' "$(git rev-parse --verify --quiet $short "$rev" --)"
309 printf '%s %s\n' "$(git rev-parse --verify --quiet $short "$rev" --)" "$name"
314 if [ -n "$deps" ]; then
315 refslist | show_deps | LC_ALL
=C
sort -u -b -k1,1 |
317 while read -r name
rev; do
318 if [ -n "$hashonly" ]; then
319 printf '%s\n' "$(git rev-parse --verify --quiet $short "$rev" --)"
321 printf '%s %s\n' "$(git rev-parse --verify --quiet $short "$rev" --)" "$name"
326 refslist | show_rdeps
329 insn
="$(get_temp isns)"
332 [ -n "$interact" ] ||
{ printf '%s' "$1"; return 0; }
333 git rev-parse
--verify --quiet --short "$1" --
336 if [ -n "$nodeps" -o -z "$refs" ]; then
337 while read -r name
rev; do
338 case "$exclude" in *" $name "*) continue; esac
339 [ -z "$refs" ] ||
case " $refs " in *" $name "*);;*) continue; esac
340 [ -z "$tgish" ] || is_tgish
"$name" ||
continue
341 printf 'revert %s %s\n' "$(get_short "$rev")" "$name"
342 done <"$trf" | LC_ALL
=C
sort -u -b -k3,3 >"$insn"
344 refslist | show_deps | LC_ALL
=C
sort -u -b -k1,1 |
346 while read -r name
rev; do
347 printf 'revert %s %s\n' "$(get_short "$rev")" "$name"
350 if [ -n "$interact" ]; then
351 count
=$
(( $
(wc -l <"$insn") ))
354 # Revert using $refname data ($count command(s))
357 # r, revert = revert ref to specified hash
359 # Note that changing the hash value shown here will have NO EFFECT.
361 # If you remove a line here THAT REVERT WILL BE SKIPPED.
363 # However, if you remove everything, the revert will be aborted.
365 run_editor
"$insn" ||
366 die
"there was a problem with the editor '$tg_editor'"
367 git stripspace
-s <"$insn" >"$insn"+
368 mv -f "$insn"+ "$insn"
369 [ -s "$insn" ] || die
"nothing to do"
370 while read -r op
hash ref
; do
371 [ "$op" = "r" -o "$op" = "revert" ] ||
372 die
"invalid op in instruction: $op $hash $ref"
373 case "$ref" in refs
/?
*);;*)
374 die
"invalid ref in instruction: $op $hash $ref"
377 die
"unknown ref in instruction: $op $hash $ref"
380 msg
="tgrevert: $reftype $tagname ($(( $(wc -l <"$insn") )) command(s))"
381 [ -n "$dryrun" -o -n "$nostash" ] ||
$tg tag
-q --none-ok -m "$msg" --stash
382 refwidth
="$(git config --get --int core.abbrev 2>/dev/null)" ||
:
383 [ -n "$refwidth" ] || refwidth
=7
384 [ $refwidth -ge 4 -a $refwidth -le 40 ] || refwidth
=7
385 nullref
="$(printf '%.*s' $refwidth "$nullsha")"
386 notewidth
=$
(( $refwidth + 4 + $refwidth ))
388 [ -n "$dryrun" ] || srh
="$(git symbolic-ref --quiet HEAD)" ||
:
389 cut
-d ' ' -f 3 <"$insn" | LC_ALL
=C
sort -u -b -k1,1 |
join - "$trf" |
390 while read -r name
rev; do
391 orig
="$(git rev-parse --verify --quiet "$name" --)" ||
:
393 if [ "$rev" != "$orig" ]; then
394 [ -z "$dryrun" -a -n "$quiet" ] ||
395 origsh
="$(git rev-parse --verify --short --quiet "$name" --)" ||
:
396 if [ -z "$dryrun" ]; then
397 if [ -n "$srh" ] && [ "$srh" = "$name" ]; then
398 [ -n "$quiet" ] ||
echo "Detaching HEAD to revert $name"
400 [ -n "$detachat" ] || detachat
="$(make_empty_commit)"
401 git update-ref
-m "tgrevert: detach HEAD to revert $name" --no-deref HEAD
"$detachat"
402 [ -n "$quiet" ] || git log
-n 1 --format=format
:'HEAD is now at %h... %s' HEAD
404 git update-ref
-m "$msg" "$name" "$rev"
406 if [ -n "$dryrun" -o -z "$quiet" ]; then
407 revsh
="$(git rev-parse --verify --short --quiet "$rev" --)" ||
:
408 if [ -n "$origsh" ]; then
410 [ -z "$dryrun" ] || hdr
='-'
411 printf '%s %s -> %s %s\n' "$hdr" "$origsh" "$revsh" "$name"
414 [ -z "$dryrun" ] || hdr
='-'
415 printf '%s %s -> %s %s\n' "$hdr" "$nullref" "$revsh" "$name"
419 : #[ -z "$dryrun" -a -n "$quiet" ] || printf "* %-*s %s\n" $notewidth "[no change]" "$name"