tg-update.sh: reduce number of checkouts slightly
[topgit/pro.git] / tg-tag.sh
blob2b6e2da5f1ac1245fb74f0ea17ca3f4ff0c98cea
1 #!/bin/sh
2 # TopGit tag 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} [...] tag [-s | -u <key-id>] [-f] [-q] [--no-edit] [-m <msg> | -F <file>] (<tagname> | --refs) [<branch>...]"
10 USAGE="$USAGE$lf Or: ${tgname:-tg} [...] tag (-g | --reflog) [--reflog-message | --commit-message] [--no-type] [-n <number> | -number] [<tagname>]"
11 USAGE="$USAGE$lf Or: ${tgname:-tg} [...] tag (--clear | --delete) <tagname>"
12 USAGE="$USAGE$lf Or: ${tgname:-tg} [...] tag --drop <tagname>@{n}"
14 usage()
16 if [ "${1:-0}" != 0 ]; then
17 printf '%s\n' "$USAGE" >&2
18 else
19 printf '%s\n' "$USAGE"
21 exit ${1:-0}
24 ## Parse options
26 signed=
27 keyid=
28 force=
29 msg=
30 msgfile=
31 noedit=
32 defnoedit=
33 refsonly=
34 maxcount=
35 reflog=
36 outofdateok=
37 anyrefok=
38 defbranch=HEAD
39 stash=
40 reflogmsg=
41 notype=
42 setreflogmsg=
43 quiet=
44 noneok=
45 clear=
46 delete=
47 drop=
49 is_numeric()
51 [ -n "$1" ] || return 1
52 while [ -n "$1" ]; do
53 case "$1" in
54 [0-9]*)
55 set -- "${1#?}";;
57 break;;
58 esac
59 done
60 [ -z "$1" ]
63 while [ $# -gt 0 ]; do case "$1" in
64 -h|--help)
65 usage
67 -q|--quiet)
68 quiet=1
70 --none-ok)
71 noneok=1
73 --clear)
74 clear=1
76 --delete)
77 delete=1
79 --drop)
80 drop=1
82 -g|--reflog|--walk-reflogs)
83 reflog=1
85 --reflog-message)
86 reflogmsg=1
87 setreflogmsg=1
89 --no-reflog-message|--commit-message)
90 reflogmsg=
91 setreflogmsg=1
93 --no-type)
94 notype=1
96 -s|--sign)
97 signed=1
99 -u|--local-user|--local-user=*)
100 case "$1" in --local-user=*)
101 x="$1"
102 shift
103 set -- --local-user "${x#--local-user=}" "$@"
104 esac
105 if [ $# -lt 2 ]; then
106 echo "The $1 option requires an argument" >&2
107 usage 1
109 shift
110 keyid="$1"
112 -f|--force)
113 force=1
115 --no-edit)
116 noedit=1
118 --edit)
119 noedit=0
121 --allow-outdated)
122 outofdateok=1
124 --allow-any)
125 anyrefok=1
127 --refs|--refs-only)
128 refsonly=1
130 -m|--message|--message=*)
131 case "$1" in --message=*)
132 x="$1"
133 shift
134 set -- --message "${x#--message=}" "$@"
135 esac
136 if [ $# -lt 2 ]; then
137 echo "The $1 option requires an argument" >&2
138 usage 1
140 shift
141 msg="$1"
143 -F|--file|--file=*)
144 case "$1" in --file=*)
145 x="$1"
146 shift
147 set -- --file "${x#--file=}" "$@"
148 esac
149 if [ $# -lt 2 ]; then
150 echo "The $1 option requires an argument" >&2
151 usage 1
153 shift
154 msgfile="$1"
156 -n|--max-count|--max-count=*|-[1-9]*)
157 case "$1" in --max-count=*)
158 x="$1"
159 shift
160 set -- --max-count "${x#--max-count=}" "$@"
161 esac
162 case "$1" in -[1-9]*)
163 x="${1#-}"
164 shift
165 set -- -n "$x" "$@"
166 esac
167 if [ $# -lt 2 ]; then
168 echo "The $1 option requires an argument" >&2
169 usage 1
171 shift
172 maxcount="$1"
175 shift
176 break
178 --all)
179 break
181 --stash|--stash"@{"*"}")
182 if [ -n "$reflog" ]; then
183 case "$2" in -[1-9]*)
184 x1="$1"
185 x2="$2"
186 shift
187 shift
188 set -- "$x2" "$x1" "$@"
189 continue
190 esac
192 stash=1
193 defbranch=--all
194 break
196 -?*)
197 echo "Unknown option: $1" >&2
198 usage 1
201 if [ -n "$reflog" ]; then
202 case "$2" in -[1-9]*)
203 x1="$1"
204 x2="$2"
205 shift
206 shift
207 set -- "$x2" "$x1" "$@"
208 continue
209 esac
211 break
213 esac; shift; done
215 [ -z "$stash" -o -n "$reflog$drop$clear$delete" ] || { outofdateok=1; force=1; defnoedit=1; }
216 [ -n "$noedit" ] || noedit="$defnoedit"
217 [ "$noedit" != "0" ] || noedit=
218 [ -z "$reflog" -o -z "$drop$clear$delete$signed$keyid$force$msg$msgfile$noedit$refsonly$outofdateok" ] || usage 1
219 [ -n "$reflog" -o -z "$setreflogmsg$notype$maxcount" ] || usage 1
220 [ -z "$drop$clear$delete" -o -z "$setreflogmsg$notype$maxcount$signed$keyid$force$msg$msgfile$noedit$refsonly$outofdateok" ] || usage 1
221 [ -z "$reflog$drop$clear$delete" -o "$reflog$drop$clear$delete" = "1" ] || usage 1
222 [ -z "$maxcount" ] || is_numeric "$maxcount" || die "invalid count: $maxcount"
223 [ -z "$maxcount" ] || [ $maxcount -gt 0 ] || die "invalid count: $maxcount"
224 [ -z "$msg" -o -z "$msgfile" ] || die "only one -F or -m option is allowed."
225 [ -z "$refsonly" ] || set -- refs..only "$@"
226 [ $# -gt 0 -o -z "$reflog" ] || set -- --stash
227 [ -n "$1" ] || { echo "Tag name required" >&2; usage 1; }
228 tagname="$1"
229 shift
230 [ "$tagname" != "--stash" ] || tagname=refs/tgstash
231 case "$tagname" in --stash"@{"*"}")
232 strip="${tagname#--stash??}"
233 strip="${strip%?}"
234 tagname="refs/tgstash@{$strip}"
235 esac
236 refname="$tagname"
237 sfx=
238 case "$refname" in [!@]*"@{"*"}")
239 sfx="$refname"
240 refname="${refname%@*}"
241 sfx="${sfx#$refname}"
242 esac
243 case "$refname" in HEAD|refs/*);;*)
244 if reftest="$(git rev-parse --revs-only --symbolic-full-name "$refname" -- 2>/dev/null)" &&
245 [ -n "$reftest" ]; then
246 if [ -n "$reflog$drop$clear$delete" ]; then
247 refname="$reftest"
248 else
249 case "$reftest" in
250 refs/tags/*|refs/tgstash)
251 refname="$reftest"
254 refname="refs/tags/$refname"
255 esac
257 else
258 refname="refs/tags/$refname"
260 esac
261 refname="$refname$sfx"
262 reftype=tag
263 case "$refname" in refs/tags/*) tagname="${refname#refs/tags/}";; *) reftype=ref; tagname="$refname"; esac
264 [ -z "$reflog$drop$clear$delete" -o $# -eq 0 ] || usage 1
265 if [ -n "$drop$clear$delete" ]; then
266 if [ -n "$sfx" ]; then
267 [ -z "$clear$delete" ] || die "invalid ref name ($sfx suffix not allowed): $refname"
268 else
269 [ -z "$drop" ] || die "invalid reflog name (@{n} suffix required): $refname"
271 old="$(git rev-parse --verify --quiet --short "${refname%$sfx}" --)" || die "no such ref: ${refname%$sfx}"
272 if [ -n "$delete" ]; then
273 git update-ref -d "$refname" || die "git update-ref -d failed"
274 printf "Deleted $reftype '%s' (was %s)\n" "$tagname" "$old"
275 exit 0
276 elif [ -n "$clear" ]; then
277 [ -f "$git_dir/logs/$refname" ] || die "no reflog found for: $refname"
278 [ -s "$git_dir/logs/$refname" ] || die "empty reflog found for: $refname"
279 cp -p "$git_dir/logs/$refname" "$git_dir/logs/$refname^-+" || die "cp failed"
280 sed -n '$p' <"$git_dir/logs/$refname^-+" >"$git_dir/logs/$refname" || die "reflog clear failed"
281 rm -f "$git_dir/logs/$refname^-+"
282 printf "Cleared $reftype '%s' reflog to single @{0} entry\n" "$tagname"
283 exit 0
284 else
285 old="$(git rev-parse --verify --short "$refname" --)" || exit 1
286 git reflog delete --rewrite --updateref "$refname" || die "reflog drop failed"
287 printf "Dropped $reftype '%s' reflog entry (was %s)\n" "$tagname" "$old"
288 exit 0
291 if [ -n "$reflog" ]; then
292 [ "$refname" = "refs/tgstash" -o -n "$setreflogmsg" ] || reflogmsg=1
293 git rev-parse --verify --quiet "$refname" -- >/dev/null ||
294 die "no such ref: $refname"
295 [ -s "$git_dir/logs/$refname" ] ||
296 die "no reflog present for $reftype: $tagname"
297 showref="$(git rev-parse --revs-only --abbrev-ref=strict "$refname" --)"
298 hashcolor=
299 resetcolor=
300 if git config --get-colorbool color.tgtag; then
301 metacolor="$(git config --get-color color.tgtag.meta)"
302 [ -n "$metacolor" ] || metacolor="$(git config --get-color color.diff.meta "bold")"
303 hashcolor="$(git config --get-color color.tgtag.commit)"
304 [ -n "$hashcolor" ] || hashcolor="$(git config --get-color color.diff.commit "yellow")"
305 datecolor="$(git config --get-color color.tgtag.date "bold blue")"
306 timecolor="$(git config --get-color color.tgtag.time "green")"
307 resetcolor="$(git config --get-color "" reset)"
309 setup_strftime
310 output()
312 sed 's/[^ ][^ ]* //' <"$git_dir/logs/$refname" |
313 awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--]}' |
314 git cat-file --batch-check='%(objectname) %(objecttype) %(rest)' |
316 stashnum=-1
317 lastdate=
318 while read -r newrev type rest; do
319 stashnum=$(( $stashnum + 1 ))
320 [ "$type" != "missing" ] || continue
321 IFS="$tab" read -r cmmttr msg <<-~EOT~
322 $rest
323 ~EOT~
324 ne="${cmmttr% *}"
325 ne="${ne% *}"
326 es="${cmmttr#$ne}"
327 es="${es% *}"
328 es="${es# }"
329 obj="$(git rev-parse --verify --quiet --short "$newrev" --)"
330 extra=
331 [ "$type" = "tag" -o -n "$notype" ] ||
332 extra="$hashcolor($metacolor$type$resetcolor$hashcolor)$resetcolor "
333 if [ -z "$reflogmsg" -o -z "$msg" ]; then
334 objmsg=
335 if [ "$type" = "tag" ]; then
336 objmsg="$(git cat-file tag "$obj" |
337 sed '1,/^$/d' | sed '/^$/,$d')"
338 elif [ "$type" = "commit" ]; then
339 objmsg="$(git log -n 1 --format='format:%s' "$obj" --)"
341 [ -z "$objmsg" ] || msg="$objmsg"
343 read newdate newtime <<-EOT
344 $(strftime "%Y-%m-%d %H:%M:%S" "$es")
346 if [ "$lastdate" != "$newdate" ]; then
347 printf '%s=== %s ===%s\n' "$datecolor" "$newdate" "$resetcolor"
348 lastdate="$newdate"
350 printf '%s %s %s%s@{%s}: %s\n' "$hashcolor$obj$reseutcolor" \
351 "$timecolor$newtime$resetcolor" \
352 "$extra" "$showref" "$stashnum" "$msg"
353 if [ -n "$maxcount" ]; then
354 maxcount=$(( $maxcount - 1 ))
355 [ $maxcount -gt 0 ] || break
357 done
360 page output
361 exit 0
363 [ -z "$signed" -o "$reftype" = "tag" ] || die "signed tags must be under refs/tags"
364 [ $# -gt 0 ] || set -- $defbranch
365 all=
366 if [ $# -eq 1 ] && [ "$1" = "--all" ]; then
367 eval set -- $(git for-each-ref --shell --format="%(refname)" "refs/$topbases")
368 outofdateok=1
369 all=1
370 if [ $# -eq 0 ]; then
371 if [ -n "$quiet" -a -n "$noneok" ]; then
372 exit 0
373 else
374 die "no TopGit branches found"
378 ensure_ident_available
379 branches=
380 allrefs=
381 extrarefs=
382 tgbranches=
383 tgcount=0
384 othercount=0
385 ignore=
386 newlist=
387 while read -r obj typ ref && [ -n "$obj" -a -n "$typ" ]; do
388 [ -n "$ref" -o "$typ" != "missing" ] || die "no such ref: ${obj%???}"
389 case " $ignore " in *" $ref "*) continue; esac
390 if [ "$typ" != "commit" -a "$typ" != "tag" ]; then
391 [ -n "$anyrefok" ] || die "not a committish (is a '$typ') ref: $ref"
392 warn "ignoring non-committish (is a '$typ') ref: $ref"
393 ignore="${ignore:+$ignore }$ref"
394 continue
396 case " $newlist " in *" $ref "*);;*)
397 newlist="${newlist:+$newlist }$ref"
398 esac
399 if [ "$typ" = "tag" ]; then
400 warn "storing as lightweight tag instead of 'tag' object: $ref"
401 ignore="${ignore:+$ignore }$ref"
403 done <<-EOT
405 printf '%s\n' "$@" | sed 's/^\(.*\)$/\1^{} \1/'
406 printf '%s\n' "$@" | sed 's/^\(.*\)$/\1 \1/'
408 git cat-file --batch-check='%(objectname) %(objecttype) %(rest)' 2>/dev/null ||
411 set -- $newlist
412 for b; do
413 sfn="$b"
414 [ -n "$all" ] ||
415 sfn="$(git rev-parse --revs-only --symbolic-full-name "$b" -- 2>/dev/null)" || :
416 [ -n "$sfn" ] || {
417 [ -n "$anyrefok" ] || die "no such symbolic ref name: $b"
418 fullhash="$(git rev-parse --verify --quiet "$b" --)" || die "no such ref: $b"
419 case " $extrarefs " in *" $b "*);;*)
420 warn "including non-symbolic ref only in parents calculation: $b"
421 extrarefs="${extrarefs:+$extrarefs }$fullhash"
422 esac
423 continue
425 case "$sfn" in
426 refs/"$topbases"/*)
427 added=
428 tgish=1
429 ref_exists "refs/heads/${sfn#refs/$topbases/}" || tgish=
430 [ -n "$anyrefok" ] || [ -n "$tgish" ] ||
431 warn "including TopGit base that's missing its head: $sfn"
432 case " $allrefs " in *" $sfn "*);;*)
433 allrefs="${allrefs:+$allrefs }$sfn"
434 esac
435 case " $branches " in *" ${sfn#refs/$topbases/} "*);;*)
436 branches="${branches:+$branches }${sfn#refs/$topbases/}"
437 added=1
438 esac
439 if [ -n "$tgish" ]; then
440 case " $allrefs " in *" refs/heads/${sfn#refs/$topbases/} "*);;*)
441 allrefs="${allrefs:+$allrefs }refs/heads/${sfn#refs/$topbases/}"
442 esac
443 case " $tgbranches " in *" ${sfn#refs/$topbases/} "*);;*)
444 tgbranches="${tgbranches:+$tgbranches }${sfn#refs/$topbases/}"
445 added=1
446 esac
447 [ -z "$added" ] || tgcount=$(( $tgcount + 1 ))
448 else
449 [ -z "$added" ] || othercount=$(( $othercount + 1 ))
452 refs/heads/*)
453 added=
454 tgish=1
455 ref_exists "refs/$topbases/${sfn#refs/heads/}" || tgish=
456 [ -n "$anyrefok" ] || [ -n "$tgish" ] ||
457 die "not a TopGit branch: ${sfn#refs/heads/} (use --allow-any option)"
458 case " $allrefs " in *" $b "*);;*)
459 allrefs="${allrefs:+$allrefs }$sfn"
460 esac
461 case " $branches " in *" ${sfn#refs/heads/} "*);;*)
462 branches="${branches:+$branches }${sfn#refs/heads/}"
463 added=1
464 esac
465 if [ -n "$tgish" ]; then
466 case " $allrefs " in *" refs/$topbases/${sfn#refs/heads/} "*);;*)
467 allrefs="${allrefs:+$allrefs }refs/$topbases/${sfn#refs/heads/}"
468 esac
469 case " $tgbranches " in *" ${sfn#refs/heads/} "*);;*)
470 tgbranches="${tgbranches:+$tgbranches }${sfn#refs/heads/}"
471 added=1
472 esac
473 [ -z "$added" ] || tgcount=$(( $tgcount + 1 ))
474 else
475 [ -z "$added" ] || othercount=$(( $othercount + 1 ))
479 [ -n "$anyrefok" ] || die "refusing to include without --allow-any: $sfn"
480 case " $allrefs " in *" $sfn "*);;*)
481 allrefs="${allrefs:+$allrefs }$sfn"
482 esac
483 case " $branches " in *" ${sfn#refs/} "*);;*)
484 branches="${branches:+$branches }${sfn#refs/}"
485 othercount=$(( $othercount + 1 ))
486 esac
488 esac
489 done
491 [ -n "$force" ] ||
492 ! git rev-parse --verify --quiet "$refname" -- >/dev/null ||
493 die "$reftype '$tagname' already exists"
495 desc="tg branch"
496 descpl="tg branches"
497 if [ $othercount -gt 0 ]; then
498 if [ $tgcount -eq 0 ]; then
499 desc="ref"
500 descpl="refs"
501 else
502 descpl="$descpl and refs"
506 get_dep() {
507 case " $seen_deps " in *" $_dep "*) return 0; esac
508 seen_deps="${seen_deps:+$seen_deps }$_dep"
509 printf 'refs/heads/%s\n' "$_dep"
510 [ -z "$_dep_is_tgish" ] || printf 'refs/%s/%s\n' "$topbases" "$_dep"
513 get_deps_internal()
515 no_remotes=1
516 recurse_deps_exclude=
517 for _b; do
518 case " $recurse_deps_exclude " in *" $_b "*) continue; esac
519 seen_deps=
520 _dep="$_b"; _dep_is_tgish=1; get_dep
521 recurse_deps get_dep "$_b"
522 recurse_deps_exclude="$recurse_deps_exclude $seen_deps"
523 done
526 get_deps()
528 get_deps_internal "$@" | LC_ALL=C sort -u
531 out_of_date=
532 if [ -n "$outofdateok" ]; then
533 if [ -n "$tgbranches" ]; then
534 while read -r dep && [ -n "$dep" ]; do
535 case " $allrefs " in *" $dep "*);;*)
536 ! ref_exists "$dep" ||
537 allrefs="${allrefs:+$allrefs }$dep"
538 esac
539 done <<-EOT
540 $(get_deps $tgbranches)
543 else
544 for b in $tgbranches; do
545 if ! needs_update "$b" >/dev/null; then
546 out_of_date=1
547 echo "branch not up-to-date: $b"
549 done
551 [ -z "$out_of_date" ] || die "all branches to be tagged must be up-to-date"
553 get_refs()
555 printf '%s\n' '-----BEGIN TOPGIT REFS-----'
557 printf '%s\n' $allrefs
558 [ -n "$outofdateok" ] || get_deps $tgbranches
559 } | LC_ALL=C sort -u | sed 's/^\(.*\)$/\1^0 \1/' |
560 git cat-file --batch-check='%(objectname) %(rest)' 2>/dev/null |
561 grep -v ' missing$' || :
562 printf '%s\n' '-----END TOPGIT REFS-----'
565 if [ -n "$refsonly" ]; then
566 get_refs
567 exit 0
570 stripcomments=
571 if [ -n "$msgfile" ]; then
572 if [ "$msgfile" = "-" ]; then
573 git stripspace >"$git_dir/TAG_EDITMSG"
574 else
575 git stripspace <"$msgfile" >"$git_dir/TAG_EDITMSG"
577 elif [ -n "$msg" ]; then
578 printf '%s\n' "$msg" | git stripspace >"$git_dir/TAG_EDITMSG"
579 else
580 case "$branches" in
581 *" "*)
582 if [ ${#branches} -le 60 ]; then
583 printf '%s\n' "tag $descpl $branches"
584 printf '%s\n' "$updmsg"
585 else
586 printf '%s\n' "tag $(( $(printf '%s' "$branches" | wc -w) )) $descpl" ""
587 for b in $branches; do
588 printf '%s\n' "$b"
589 done
593 printf '%s\n' "tag $desc $branches"
595 esac | git stripspace >"$git_dir/TAG_EDITMSG"
596 if [ -z "$noedit" ]; then
598 cat <<EOT
600 # Please enter a message for tg tag:
601 # $tagname
602 # Lines starting with '#' will be ignored.
604 # $descpl to be tagged:
607 for b in $branches; do
608 printf '%s\n' "# $b"
609 done
610 } >>"$git_dir/TAG_EDITMSG"
611 stripcomments=1
612 run_editor "$git_dir/TAG_EDITMSG" ||
613 die "there was a problem with the editor '$tg_editor'"
616 git stripspace ${stripcomments:+ --strip-comments} \
617 <"$git_dir/TAG_EDITMSG" >"$git_dir/TGTAG_FINALMSG"
618 [ -s "$git_dir/TGTAG_FINALMSG" ] || die "no tag message?"
619 echo "" >>"$git_dir/TGTAG_FINALMSG"
620 get_refs >>"$git_dir/TGTAG_FINALMSG"
622 tagtarget=
623 case "$allrefs${extrarefs:+ $extrarefs}" in
624 *" "*)
625 parents="$(git merge-base --independent \
626 $(printf '%s^0 ' $allrefs $extrarefs))" ||
627 die "failed: git merge-base --independent"
628 if [ $(printf '%s\n' "$parents" | wc -l) -eq 1 ]; then
629 tagtarget="$parents"
630 else
631 mttree="$(git hash-object -t tree -w --stdin </dev/null)"
632 tagtarget="$(printf '%s\n' "tg tag branch consolidation" "" $branches |
633 git commit-tree $mttree $(printf -- '-p %s ' $parents))"
637 tagtarget="$allrefs^0"
639 esac
641 init_reflog "$refname"
642 if [ "$reftype" = "tag" -a -n "$signed" ]; then
643 [ -z "$quiet" ] || exec >/dev/null
644 git tag -F "$git_dir/TGTAG_FINALMSG" ${signed:+-s} ${force:+-f} \
645 ${keyid:+-u} ${keyid} "$tagname" "$tagtarget"
646 else
647 obj="$(git rev-parse --verify --quiet "$tagtarget" --)" ||
648 die "invalid object name: $tagtarget"
649 typ="$(git cat-file -t "$tagtarget" 2>/dev/null)" ||
650 die "invalid object name: $tagtarget"
651 id="$(git var GIT_COMMITTER_IDENT 2>/dev/null)" ||
652 die "could not get GIT_COMMITTER_IDENT"
653 newtag="$({
654 printf '%s\n' "object $obj" "type $typ" "tag $tagname" \
655 "tagger $id" ""
656 cat "$git_dir/TGTAG_FINALMSG"
657 } | git mktag)" || die "git mktag failed"
658 old="$(git rev-parse --verify --short --quiet "$refname" --)" || :
659 updmsg=
660 case "$branches" in
661 *" "*)
662 if [ ${#branches} -le 100 ]; then
663 updmsg="$(printf '%s\n' "tgtag: $branches")"
664 else
665 updmsg="$(printf '%s\n' "tgtag: $(( $(printf '%s' "$branches" | wc -w) )) ${descpl#tg }")"
669 updmsg="$(printf '%s\n' "tgtag: $branches")"
671 esac
672 git update-ref -m "$updmsg" "$refname" "$newtag"
673 [ -z "$old" -o -n "$quiet" ] || printf "Updated $reftype '%s' (was %s)\n" "$tagname" "$old"
675 rm -f "$git_dir/TAG_EDITMSG" "$git_dir/TGTAG_FINALMSG"