tg.sh: avoid modifying a symbolic link pre-commit hook
[topgit/pro.git] / tg-revert.sh
blob04f44180af45ff0579a8725041d6fd21069a7adc
1 #!/bin/sh
2 # TopGit revert command
3 # Copyright (C) 2015 Kyle J. McKay <mackyle@gmail.com>
4 # All rights reserved.
5 # GPLv2
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>...)]"
12 usage()
14 if [ "${1:-0}" != 0 ]; then
15 printf '%s\n' "$USAGE" >&2
16 else
17 printf '%s\n' "$USAGE"
19 exit ${1:-0}
22 ## Parse options
24 force=
25 interact=
26 dryrun=
27 list=
28 deps=
29 rdeps=
30 tgish=
31 nodeps=
32 nostash=
33 exclude=
34 quiet=
35 short=
36 hashonly=
38 while [ $# -gt 0 ]; do case "$1" in
39 -h|--help)
40 usage
42 -q|--quiet)
43 quiet=1
45 -l|--list)
46 list=1
48 --short|--short=*|--no-short)
49 short="$1"
51 --hash|--hash-only)
52 hashonly=1
54 --deps|--deps-only)
55 deps=1
57 --rdeps)
58 rdeps=1
60 --tgish-only)
61 tgish=1
63 -f|--force)
64 force=1
66 -i|--interactive)
67 interact=1
69 -n|--dry-run)
70 dryrun=1
72 --no-deps)
73 nodeps=1
75 --no-stash)
76 nostash=1
78 --exclude=*)
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";;
82 --exclude)
83 shift
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";;
87 --)
88 shift
89 break
91 --stash|--stash"@{"*"}")
92 break
94 -?*)
95 echo "Unknown option: $1" >&2
96 usage 1
99 break
101 esac; shift; done
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; }
110 tagname="$1"
111 shift
112 [ -n "$list" -o "$1" != "--heads" ] || usage 1
113 [ "$tagname" != "--stash" ] || tagname=refs/tgstash
114 case "$tagname" in --stash"@{"*"}")
115 strip="${tagname#--stash??}"
116 strip="${strip%?}"
117 tagname="refs/tgstash@{$strip}"
118 esac
119 refname="$tagname"
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"
127 else
128 if hash="$(git rev-parse --quiet --verify "$refname$suffix")"; then
129 refname="$hash"
130 else
131 refname="refs/tags/$refname$suffix"
134 esac
135 reftype=tag
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}"
141 while
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"
147 ! [ -s "$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
160 done
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"
166 create_ref_dirs
167 tg_ref_cache_only=1
168 tg_read_only=1
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"
175 set -- $(
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" |
179 LC_ALL=C sort)
182 is_tgish() {
183 case "$1" in
184 refs/"$topbases"/*)
185 ref_exists "refs/heads/${1#refs/$topbases/}"
187 refs/heads/*)
188 ref_exists "refs/$topbases/${1#refs/heads/}"
193 esac
196 refs=
197 for b; do
198 exp=
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
204 case "$rn" in
205 refs/"$topbases"/*)
206 refs="$refs refs/heads/${rn#refs/$topbases/}"
208 refs/heads/*)
209 refs="$refs refs/$topbases/${rn#refs/heads/}"
211 esac
213 esac
214 done
216 show_dep() {
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"
226 show_deps()
228 no_remotes=1
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
234 printf '%s\n' "$_b"
235 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
240 seen_deps=
241 _dep="$_b"; _dep_is_tgish=1; show_dep
242 recurse_deps show_dep "$_b"
243 recurse_deps_exclude="$recurse_deps_exclude $seen_deps"
244 done
247 show_rdep()
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)"
253 else
254 printf '%s %s\n' "$_depchain" "$(ref_exists_rev_short "refs/heads/$_dep" $short)~$_dep"
258 show_rdeps()
260 no_remotes=1
261 show_break=
262 seen_deps=
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
268 showbreak=1
269 if [ -n "$hashonly" ]; then
270 printf '%s\n' "$(ref_exists_rev_short "refs/heads/$_b" $short)"
271 else
272 printf '%s\n' "$(ref_exists_rev_short "refs/heads/$_b" $short)~$_b"
274 continue
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
281 showbreak=1
283 if [ -n "$hashonly" ]; then
284 printf '%s\n' "$(ref_exists_rev_short "refs/heads/$_b" $short)"
285 else
286 printf '%s\n' "$(ref_exists_rev_short "refs/heads/$_b" $short)~$_b"
288 recurse_preorder=1
289 recurse_deps show_rdep "$_b"
290 } | sed -e 's/[^ ][^ ]*[ ]/ /g' -e 's/~/ /'
291 done
294 refslist() {
295 [ -z "$refs" ] || sed 'y/ /\n/' <<-EOT
296 $refs
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" --)"
308 else
309 printf '%s %s\n' "$(git rev-parse --verify --quiet $short "$rev" --)" "$name"
311 done <"$trf"
312 exit 0
314 if [ -n "$deps" ]; then
315 refslist | show_deps | LC_ALL=C sort -u -b -k1,1 |
316 join - "$trf" |
317 while read -r name rev; do
318 if [ -n "$hashonly" ]; then
319 printf '%s\n' "$(git rev-parse --verify --quiet $short "$rev" --)"
320 else
321 printf '%s %s\n' "$(git rev-parse --verify --quiet $short "$rev" --)" "$name"
323 done
324 exit 0
326 refslist | show_rdeps
327 exit 0
329 insn="$(get_temp isns)"
331 get_short() {
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"
343 else
344 refslist | show_deps | LC_ALL=C sort -u -b -k1,1 |
345 join - "$trf" |
346 while read -r name rev; do
347 printf 'revert %s %s\n' "$(get_short "$rev")" "$name"
348 done >"$insn"
350 if [ -n "$interact" ]; then
351 count=$(( $(wc -l <"$insn") ))
352 cat <<EOT >>"$insn"
354 # Revert using $refname data ($count command(s))
356 # Commands:
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"
375 esac
376 ref_exists "$ref" ||
377 die "unknown ref in instruction: $op $hash $ref"
378 done <"$insn"
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 ))
387 srh=
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" --)" || :
392 init_reflog "$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"
399 detachat="$orig"
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
409 hdr=' '
410 [ -z "$dryrun" ] || hdr='-'
411 printf '%s %s -> %s %s\n' "$hdr" "$origsh" "$revsh" "$name"
412 else
413 hdr='*'
414 [ -z "$dryrun" ] || hdr='-'
415 printf '%s %s -> %s %s\n' "$hdr" "$nullref" "$revsh" "$name"
418 else
419 : #[ -z "$dryrun" -a -n "$quiet" ] || printf "* %-*s %s\n" $notewidth "[no change]" "$name"
421 done
423 exit 0