tg-tag.sh: allow choosing a consolidation commit's first parent
[topgit/pro.git] / tg-tag.sh
blob91558c8d9ffbb413d339524c7d65ec5c6291a39e
1 #!/bin/sh
2 # TopGit tag command
3 # Copyright (C) 2015,2017 Kyle J. McKay <mackyle@gmail.com>
4 # All rights reserved.
5 # GPLv2
7 USAGE="\
8 Usage: ${tgname:-tg} [...] tag [-s | -u <key-id>] [-f] [-q] [--no-edit] [-m <msg> | -F <file>] [--tree <treeish>] (<tagname> | --refs) [<branch>...]
9 Or: ${tgname:-tg} [...] tag (-g | --reflog) [--reflog-message | --commit-message] [--no-type] [-n <number> | -number] [<tagname>]
10 Or: ${tgname:-tg} [...] tag (--clear | --delete) <tagname>
11 Or: ${tgname:-tg} [...] tag --drop <tagname>@{n}"
13 usage()
15 if [ "${1:-0}" != 0 ]; then
16 printf '%s\n' "$USAGE" >&2
17 else
18 printf '%s\n' "$USAGE"
20 exit ${1:-0}
23 ## Parse options
25 signed=
26 keyid=
27 force=
28 msg=
29 msgfile=
30 noedit=
31 defnoedit=
32 refsonly=
33 maxcount=
34 reflog=
35 outofdateok=
36 anyrefok=
37 defbranch=HEAD
38 stash=
39 anonymous=
40 reflogmsg=
41 notype=
42 setreflogmsg=
43 quiet=0
44 noneok=
45 clear=
46 delete=
47 drop=
48 anonymous=
49 treeish=
51 is_numeric()
53 [ -n "$1" ] || return 1
54 while [ -n "$1" ]; do
55 case "$1" in
56 [0-9]*)
57 set -- "${1#?}";;
59 break;;
60 esac
61 done
62 [ -z "$1" ]
65 while [ $# -gt 0 ]; do case "$1" in
66 -h|--help)
67 usage
69 -q|--quiet)
70 quiet=$(( $quiet + 1 ))
72 --none-ok)
73 noneok=1
75 --clear)
76 clear=1
78 --delete)
79 delete=1
81 --drop)
82 drop=1
84 -g|--reflog|--walk-reflogs)
85 reflog=1
87 --reflog-message)
88 reflogmsg=1
89 setreflogmsg=1
91 --no-reflog-message|--commit-message)
92 reflogmsg=
93 setreflogmsg=1
95 --no-type)
96 notype=1
98 -s|--sign)
99 signed=1
101 -u|--local-user|--local-user=*)
102 case "$1" in --local-user=*)
103 x="$1"
104 shift
105 set -- --local-user "${x#--local-user=}" "$@"
106 esac
107 if [ $# -lt 2 ]; then
108 echo "The $1 option requires an argument" >&2
109 usage 1
111 shift
112 keyid="$1"
114 -f|--force)
115 force=1
117 --no-edit)
118 noedit=1
120 --edit)
121 noedit=0
123 --allow-outdated)
124 outofdateok=1
126 --allow-any)
127 anyrefok=1
129 --tree|--tree=*)
130 case "$1" in --tree=*)
131 x="$1"
132 shift
133 set -- --tree "${x#--tree=}" "$@"
134 esac
135 if [ $# -lt 2 ]; then
136 echo "The $1 option requires an argument" >&2
137 usage 1
139 shift
140 treeish="$(git rev-parse --quiet --verify "$1^{tree}" --)" || {
141 echo "Not a valid treeish: $1" >&2
142 exit 1
145 --refs|--refs-only)
146 refsonly=1
148 -m|--message|--message=*)
149 case "$1" in --message=*)
150 x="$1"
151 shift
152 set -- --message "${x#--message=}" "$@"
153 esac
154 if [ $# -lt 2 ]; then
155 echo "The $1 option requires an argument" >&2
156 usage 1
158 shift
159 msg="$1"
161 -F|--file|--file=*)
162 case "$1" in --file=*)
163 x="$1"
164 shift
165 set -- --file "${x#--file=}" "$@"
166 esac
167 if [ $# -lt 2 ]; then
168 echo "The $1 option requires an argument" >&2
169 usage 1
171 shift
172 msgfile="$1"
174 -n|--max-count|--max-count=*|-[1-9]*)
175 case "$1" in --max-count=*)
176 x="$1"
177 shift
178 set -- --max-count "${x#--max-count=}" "$@"
179 esac
180 case "$1" in -[1-9]*)
181 x="${1#-}"
182 shift
183 set -- -n "$x" "$@"
184 esac
185 if [ $# -lt 2 ]; then
186 echo "The $1 option requires an argument" >&2
187 usage 1
189 shift
190 maxcount="$1"
193 shift
194 break
196 --all)
197 break
199 --stash|--stash"@{"*"}")
200 if [ -n "$reflog" ]; then
201 case "$2" in -[1-9]*)
202 x1="$1"
203 x2="$2"
204 shift
205 shift
206 set -- "$x2" "$x1" "$@"
207 continue
208 esac
210 stash=1
211 defbranch=--all
212 break
214 --anonymous)
215 anonymous=1
216 defbranch=--all
217 quiet=2
218 break
220 -?*)
221 echo "Unknown option: $1" >&2
222 usage 1
225 if [ -n "$reflog" ]; then
226 case "$2" in -[1-9]*)
227 x1="$1"
228 x2="$2"
229 shift
230 shift
231 set -- "$x2" "$x1" "$@"
232 continue
233 esac
235 break
237 esac; shift; done
239 [ "$stash$anonymous" != "11" ] || usage 1
240 [ -z "$stash$anonymous" -o -n "$reflog$drop$clear$delete" ] || { outofdateok=1; force=1; defnoedit=1; }
241 [ -n "$noedit" ] || noedit="$defnoedit"
242 [ "$noedit" != "0" ] || noedit=
243 [ -z "$reflog" -o -z "$drop$clear$delete$signed$keyid$force$msg$msgfile$noedit$treeish$refsonly$outofdateok" ] || usage 1
244 [ -n "$reflog" -o -z "$setreflogmsg$notype$maxcount" ] || usage 1
245 [ -z "$drop$clear$delete" -o -z "$setreflogmsg$notype$maxcount$signed$keyid$force$msg$msgfile$noedit$treeish$refsonly$outofdateok" ] || usage 1
246 [ -z "$reflog$drop$clear$delete" -o "$reflog$drop$clear$delete" = "1" ] || usage 1
247 [ -z "$maxcount" ] || is_numeric "$maxcount" || die "invalid count: $maxcount"
248 [ -z "$maxcount" ] || [ $maxcount -gt 0 ] || die "invalid count: $maxcount"
249 [ -z "$msg" -o -z "$msgfile" ] || die "only one -F or -m option is allowed."
250 [ -z "$refsonly" ] || set -- refs..only "$@"
251 [ $# -gt 0 -o -z "$reflog" ] || set -- --stash
252 [ -n "$1" ] || { echo "Tag name required" >&2; usage 1; }
253 tagname="$1"
254 shift
255 [ "$tagname" != "--stash" ] || tagname=refs/tgstash
256 [ "$tagname" != "--anonymous" ] || tagname=TG_STASH
257 case "$tagname" in --stash"@{"*"}")
258 strip="${tagname#--stash??}"
259 strip="${strip%?}"
260 tagname="refs/tgstash@{$strip}"
261 esac
262 refname="$tagname"
263 sfx=
264 case "$refname" in [!@]*"@{"*"}")
265 sfx="$refname"
266 refname="${refname%@*}"
267 sfx="${sfx#$refname}"
268 esac
269 case "$refname" in HEAD|TG_STASH|refs/*);;*)
270 if reftest="$(git rev-parse --revs-only --symbolic-full-name "$refname" -- 2>/dev/null)" &&
271 [ -n "$reftest" ]; then
272 if [ -n "$reflog$drop$clear$delete" ]; then
273 refname="$reftest"
274 else
275 case "$reftest" in
276 refs/tags/*|refs/tgstash)
277 refname="$reftest"
280 refname="refs/tags/$refname"
281 esac
283 else
284 refname="refs/tags/$refname"
286 esac
287 refname="$refname$sfx"
288 reftype=tag
289 case "$refname" in refs/tags/*) tagname="${refname#refs/tags/}";; *) reftype=ref; tagname="$refname"; esac
290 logbase="$git_common_dir"
291 [ "$refname" != "HEAD" ] || logbase="$git_dir"
292 [ -z "$reflog$drop$clear$delete" -o $# -eq 0 ] || usage 1
293 if [ -n "$drop$clear$delete" ]; then
294 if [ -n "$sfx" ]; then
295 [ -z "$clear$delete" ] || die "invalid ref name ($sfx suffix not allowed): $refname"
296 else
297 [ -z "$drop" ] || die "invalid reflog name (@{n} suffix required): $refname"
299 old="$(git rev-parse --verify --quiet --short "${refname%$sfx}" --)" || die "no such ref: ${refname%$sfx}"
300 if [ -n "$delete" ]; then
301 git update-ref -d "$refname" || die "git update-ref -d failed"
302 printf "Deleted $reftype '%s' (was %s)\n" "$tagname" "$old"
303 exit 0
304 elif [ -n "$clear" ]; then
305 [ -f "$logbase/logs/$refname" ] || die "no reflog found for: $refname"
306 [ -s "$logbase/logs/$refname" ] || die "empty reflog found for: $refname"
307 cp -p "$logbase/logs/$refname" "$logbase/logs/$refname^-+" || die "cp failed"
308 sed -n '$p' <"$logbase/logs/$refname^-+" >"$logbase/logs/$refname" || die "reflog clear failed"
309 rm -f "$logbase/logs/$refname^-+"
310 printf "Cleared $reftype '%s' reflog to single @{0} entry\n" "$tagname"
311 exit 0
312 else
313 old="$(git rev-parse --verify --short "$refname" --)" || exit 1
314 git reflog delete --rewrite --updateref "$refname" || die "reflog drop failed"
315 printf "Dropped $reftype '%s' reflog entry (was %s)\n" "$tagname" "$old"
316 exit 0
319 if [ -n "$reflog" ]; then
320 [ "$refname" = "refs/tgstash" -o -n "$setreflogmsg" ] || reflogmsg=1
321 git rev-parse --verify --quiet "$refname" -- >/dev/null ||
322 die "no such ref: $refname"
323 [ -s "$logbase/logs/$refname" ] ||
324 die "no reflog present for $reftype: $tagname"
325 showref="$(git rev-parse --revs-only --abbrev-ref=strict "$refname" --)"
326 hashcolor=
327 resetcolor=
328 if git config --get-colorbool color.tgtag; then
329 metacolor="$(git config --get-color color.tgtag.meta)"
330 [ -n "$metacolor" ] || metacolor="$(git config --get-color color.diff.meta "bold")"
331 hashcolor="$(git config --get-color color.tgtag.commit)"
332 [ -n "$hashcolor" ] || hashcolor="$(git config --get-color color.diff.commit "yellow")"
333 datecolor="$(git config --get-color color.tgtag.date "bold blue")"
334 timecolor="$(git config --get-color color.tgtag.time "green")"
335 resetcolor="$(git config --get-color "" reset)"
337 setup_strftime
338 output()
340 sed 's/[^ ][^ ]* //' <"$logbase/logs/$refname" |
341 awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--]}' |
342 git cat-file --batch-check='%(objectname) %(objecttype) %(rest)' |
344 stashnum=-1
345 lastdate=
346 while read -r newrev type rest; do
347 stashnum=$(( $stashnum + 1 ))
348 [ "$type" != "missing" ] || continue
349 IFS="$tab" read -r cmmttr msg <<-~EOT~
350 $rest
351 ~EOT~
352 ne="${cmmttr% *}"
353 ne="${ne% *}"
354 es="${cmmttr#$ne}"
355 es="${es% *}"
356 es="${es# }"
357 obj="$(git rev-parse --verify --quiet --short "$newrev" --)"
358 extra=
359 [ "$type" = "tag" -o -n "$notype" ] ||
360 extra="$hashcolor($metacolor$type$resetcolor$hashcolor)$resetcolor "
361 if [ -z "$reflogmsg" -o -z "$msg" ]; then
362 objmsg=
363 if [ "$type" = "tag" ]; then
364 objmsg="$(git cat-file tag "$obj" |
365 sed '1,/^$/d' | sed '/^$/,$d')"
366 elif [ "$type" = "commit" ]; then
367 objmsg="$(git --no-pager log -n 1 --format='format:%s' "$obj" --)"
369 [ -z "$objmsg" ] || msg="$objmsg"
371 read newdate newtime <<-EOT
372 $(strftime "%Y-%m-%d %H:%M:%S" "$es")
374 if [ "$lastdate" != "$newdate" ]; then
375 printf '%s=== %s ===%s\n' "$datecolor" "$newdate" "$resetcolor"
376 lastdate="$newdate"
378 printf '%s %s %s%s@{%s}: %s\n' "$hashcolor$obj$reseutcolor" \
379 "$timecolor$newtime$resetcolor" \
380 "$extra" "$showref" "$stashnum" "$msg"
381 if [ -n "$maxcount" ]; then
382 maxcount=$(( $maxcount - 1 ))
383 [ $maxcount -gt 0 ] || break
385 done
388 page output
389 exit 0
391 [ -z "$signed" -o "$reftype" = "tag" ] || die "signed tags must be under refs/tags"
392 [ $# -gt 0 ] || set -- $defbranch
393 all=
394 if [ $# -eq 1 ] && [ "$1" = "--all" ]; then
395 eval set -- $(git for-each-ref --shell --format="%(refname)" "refs/$topbases")
396 outofdateok=1
397 all=1
398 if [ $# -eq 0 ]; then
399 if [ "$quiet" -gt 0 -a -n "$noneok" ]; then
400 exit 0
401 else
402 die "no TopGit branches found"
406 ensure_ident_available
407 branches=
408 allrefs=
409 extrarefs=
410 tgbranches=
411 tgcount=0
412 othercount=0
413 ignore=
414 newlist=
415 firstprnt=
416 for arg in "$@"; do
417 case "$arg" in "~"?*)
418 [ -z "$firstprnt" ] || die "only one first parent may be specified with ~"
419 firstprnt="$(git rev-parse --verify --quiet "${arg#?}^0" -- 2>/dev/null)" && [ -n "$firstprnt" ] ||
420 die "not a commit-ish: ${arg#?}"
421 esac
422 done
423 while read -r obj typ ref && [ -n "$obj" -a -n "$typ" ]; do
424 [ -n "$ref" -o "$typ" != "missing" ] || die "no such ref: ${obj%???}"
425 case " $ignore " in *" $ref "*) continue; esac
426 if [ "$typ" != "commit" -a "$typ" != "tag" ]; then
427 [ -n "$anyrefok" ] || die "not a committish (is a '$typ') ref: $ref"
428 [ "$quiet" -ge 2 ] || warn "ignoring non-committish (is a '$typ') ref: $ref"
429 ignore="${ignore:+$ignore }$ref"
430 continue
432 case " $newlist " in *" $ref "*);;*)
433 newlist="${newlist:+$newlist }$ref"
434 esac
435 if [ "$typ" = "tag" ]; then
436 [ "$quiet" -ge 2 ] || warn "storing as lightweight tag instead of 'tag' object: $ref"
437 ignore="${ignore:+$ignore }$ref"
439 done <<-EOT
441 printf '%s\n' "$@" | sed 's/^~//; s/^\(.*\)$/\1^{} \1/'
442 printf '%s\n' "$@" | sed 's/^~//; s/^\(.*\)$/\1 \1/'
444 git cat-file --batch-check='%(objectname) %(objecttype) %(rest)' 2>/dev/null ||
447 set -- $newlist
448 for b; do
449 sfn="$b"
450 [ -n "$all" ] ||
451 sfn="$(git rev-parse --revs-only --symbolic-full-name "$b" -- 2>/dev/null)" || :
452 [ -n "$sfn" ] || {
453 [ -n "$anyrefok" ] || die "no such symbolic ref name: $b"
454 fullhash="$(git rev-parse --verify --quiet "$b" --)" || die "no such ref: $b"
455 case " $extrarefs " in *" $b "*);;*)
456 [ "$quiet" -ge 2 ] || warn "including non-symbolic ref only in parents calculation: $b"
457 extrarefs="${extrarefs:+$extrarefs }$fullhash"
458 esac
459 continue
461 case "$sfn" in
462 refs/"$topbases"/*)
463 added=
464 tgish=1
465 ref_exists "refs/heads/${sfn#refs/$topbases/}" || tgish=
466 [ -n "$anyrefok" ] || [ -n "$tgish" ] || [ "$quiet" -ge 2 ] ||
467 warn "including TopGit base that's missing its head: $sfn"
468 case " $allrefs " in *" $sfn "*);;*)
469 allrefs="${allrefs:+$allrefs }$sfn"
470 esac
471 case " $branches " in *" ${sfn#refs/$topbases/} "*);;*)
472 branches="${branches:+$branches }${sfn#refs/$topbases/}"
473 added=1
474 esac
475 if [ -n "$tgish" ]; then
476 case " $allrefs " in *" refs/heads/${sfn#refs/$topbases/} "*);;*)
477 allrefs="${allrefs:+$allrefs }refs/heads/${sfn#refs/$topbases/}"
478 esac
479 case " $tgbranches " in *" ${sfn#refs/$topbases/} "*);;*)
480 tgbranches="${tgbranches:+$tgbranches }${sfn#refs/$topbases/}"
481 added=1
482 esac
483 [ -z "$added" ] || tgcount=$(( $tgcount + 1 ))
484 else
485 [ -z "$added" ] || othercount=$(( $othercount + 1 ))
488 refs/heads/*)
489 added=
490 tgish=1
491 ref_exists "refs/$topbases/${sfn#refs/heads/}" || tgish=
492 [ -n "$anyrefok" ] || [ -n "$tgish" ] ||
493 die "not a TopGit branch: ${sfn#refs/heads/} (use --allow-any option)"
494 case " $allrefs " in *" $b "*);;*)
495 allrefs="${allrefs:+$allrefs }$sfn"
496 esac
497 case " $branches " in *" ${sfn#refs/heads/} "*);;*)
498 branches="${branches:+$branches }${sfn#refs/heads/}"
499 added=1
500 esac
501 if [ -n "$tgish" ]; then
502 case " $allrefs " in *" refs/$topbases/${sfn#refs/heads/} "*);;*)
503 allrefs="${allrefs:+$allrefs }refs/$topbases/${sfn#refs/heads/}"
504 esac
505 case " $tgbranches " in *" ${sfn#refs/heads/} "*);;*)
506 tgbranches="${tgbranches:+$tgbranches }${sfn#refs/heads/}"
507 added=1
508 esac
509 [ -z "$added" ] || tgcount=$(( $tgcount + 1 ))
510 else
511 [ -z "$added" ] || othercount=$(( $othercount + 1 ))
515 [ -n "$anyrefok" ] || die "refusing to include without --allow-any: $sfn"
516 case " $allrefs " in *" $sfn "*);;*)
517 allrefs="${allrefs:+$allrefs }$sfn"
518 esac
519 case " $branches " in *" ${sfn#refs/} "*);;*)
520 branches="${branches:+$branches }${sfn#refs/}"
521 othercount=$(( $othercount + 1 ))
522 esac
524 esac
525 done
527 [ -n "$force" ] ||
528 ! git rev-parse --verify --quiet "$refname" -- >/dev/null ||
529 die "$reftype '$tagname' already exists"
531 desc="tg branch"
532 descpl="tg branches"
533 if [ $othercount -gt 0 ]; then
534 if [ $tgcount -eq 0 ]; then
535 desc="ref"
536 descpl="refs"
537 else
538 descpl="$descpl and refs"
542 get_dep() {
543 case " $seen_deps " in *" $_dep "*) return 0; esac
544 seen_deps="${seen_deps:+$seen_deps }$_dep"
545 printf 'refs/heads/%s\n' "$_dep"
546 [ -z "$_dep_is_tgish" ] || printf 'refs/%s/%s\n' "$topbases" "$_dep"
549 get_deps_internal()
551 no_remotes=1
552 recurse_deps_exclude=
553 for _b; do
554 case " $recurse_deps_exclude " in *" $_b "*) continue; esac
555 seen_deps=
556 _dep="$_b"; _dep_is_tgish=1; get_dep
557 recurse_deps get_dep "$_b"
558 recurse_deps_exclude="$recurse_deps_exclude $seen_deps"
559 done
562 get_deps()
564 get_deps_internal "$@" | sort -u
567 out_of_date=
568 if [ -n "$outofdateok" ]; then
569 if [ -n "$tgbranches" ]; then
570 while read -r dep && [ -n "$dep" ]; do
571 case " $allrefs " in *" $dep "*);;*)
572 ! ref_exists "$dep" ||
573 allrefs="${allrefs:+$allrefs }$dep"
574 esac
575 done <<-EOT
576 $(get_deps $tgbranches)
579 else
580 for b in $tgbranches; do
581 if ! needs_update "$b" >/dev/null; then
582 out_of_date=1
583 echo "branch not up-to-date: $b"
585 done
587 [ -z "$out_of_date" ] || die "all branches to be tagged must be up-to-date"
589 get_refs()
591 printf '%s\n' '-----BEGIN TOPGIT REFS-----'
593 printf '%s\n' $allrefs
594 [ -n "$outofdateok" ] || get_deps $tgbranches
595 } | sort -u | sed 's/^\(.*\)$/\1^0 \1/' |
596 git cat-file --batch-check='%(objectname) %(rest)' 2>/dev/null |
597 grep -v ' missing$' || :
598 printf '%s\n' '-----END TOPGIT REFS-----'
601 if [ -n "$refsonly" ]; then
602 get_refs
603 exit 0
606 stripcomments=
607 if [ -n "$msgfile" ]; then
608 if [ "$msgfile" = "-" ]; then
609 git stripspace >"$git_dir/TAG_EDITMSG"
610 else
611 git stripspace <"$msgfile" >"$git_dir/TAG_EDITMSG"
613 elif [ -n "$msg" ]; then
614 printf '%s\n' "$msg" | git stripspace >"$git_dir/TAG_EDITMSG"
615 else
616 case "$branches" in
617 *" "*)
618 if [ ${#branches} -le 60 ]; then
619 printf '%s\n' "tag $descpl $branches"
620 printf '%s\n' "$updmsg"
621 else
622 printf '%s\n' "tag $(( $(printf '%s' "$branches" | wc -w) )) $descpl" ""
623 for b in $branches; do
624 printf '%s\n' "$b"
625 done
629 printf '%s\n' "tag $desc $branches"
631 esac | git stripspace >"$git_dir/TAG_EDITMSG"
632 if [ -z "$noedit" ]; then
634 cat <<EOT
636 # Please enter a message for tg tag:
637 # $tagname
638 # Lines starting with '#' will be ignored.
640 # $descpl to be tagged:
643 for b in $branches; do
644 printf '%s\n' "# $b"
645 done
646 } >>"$git_dir/TAG_EDITMSG"
647 stripcomments=1
648 run_editor "$git_dir/TAG_EDITMSG" ||
649 die "there was a problem with the editor '$tg_editor'"
652 git stripspace ${stripcomments:+ --strip-comments} \
653 <"$git_dir/TAG_EDITMSG" >"$git_dir/TGTAG_FINALMSG"
654 [ -s "$git_dir/TGTAG_FINALMSG" ] || die "no tag message?"
655 echo "" >>"$git_dir/TGTAG_FINALMSG"
656 get_refs >>"$git_dir/TGTAG_FINALMSG"
658 v_count_args() { eval "$1="'$(( $# - 1 ))'; }
660 tagtarget=
661 case "$allrefs${extrarefs:+ $extrarefs}" in
662 *" "*)
663 parents="$(git merge-base --independent \
664 $(printf '%s^0 ' $allrefs $extrarefs))" ||
665 die "failed: git merge-base --independent"
668 if [ -n "$firstprnt" ]; then
669 parents="$(git rev-parse --quiet --verify "$allrefs^0" --)" ||
670 die "failed: git rev-parse $allrefs^0"
671 else
672 parents="$allrefs^0"
675 esac
676 if [ -n "$firstprnt" ]; then
677 oldparents="$parents"
678 parents="$firstprnt"
679 for acmt in $oldparents; do
680 [ "$acmt" = "$firstprnt" ] || parents="$parents $acmt"
681 done
682 unset oldparents
684 v_count_args pcnt $parents
685 if [ $pcnt -eq 1 ]; then
686 tagtarget="$parents"
687 [ -z "$treeish" ] ||
688 [ "$(git rev-parse --quiet --verify "$tagtarget^{tree}" --)" = "$treeish" ] ||
689 tagtarget=
691 if [ -z "$tagtarget" ]; then
692 tagtree="${treeish:-$firstprnt}"
693 [ -n "$tagtree" ] || tagtree="$(git hash-object -t tree -w --stdin </dev/null)"
694 tagtarget="$(printf '%s\n' "tg tag branch consolidation" "" $branches |
695 git commit-tree $tagtree^{tree} $(printf -- '-p %s ' $parents))"
698 init_reflog "$refname"
699 if [ "$reftype" = "tag" -a -n "$signed" ]; then
700 [ "$quiet" -eq 0 ] || exec >/dev/null
701 git tag -F "$git_dir/TGTAG_FINALMSG" ${signed:+-s} ${force:+-f} \
702 ${keyid:+-u} ${keyid} "$tagname" "$tagtarget"
703 else
704 obj="$(git rev-parse --verify --quiet "$tagtarget" --)" ||
705 die "invalid object name: $tagtarget"
706 typ="$(git cat-file -t "$tagtarget" 2>/dev/null)" ||
707 die "invalid object name: $tagtarget"
708 id="$(git var GIT_COMMITTER_IDENT 2>/dev/null)" ||
709 die "could not get GIT_COMMITTER_IDENT"
710 newtag="$({
711 printf '%s\n' "object $obj" "type $typ" "tag $tagname" \
712 "tagger $id" ""
713 cat "$git_dir/TGTAG_FINALMSG"
714 } | git mktag)" || die "git mktag failed"
715 old="$(git rev-parse --verify --short --quiet "$refname" --)" || :
716 updmsg=
717 case "$branches" in
718 *" "*)
719 if [ ${#branches} -le 100 ]; then
720 updmsg="$(printf '%s\n' "tgtag: $branches")"
721 else
722 updmsg="$(printf '%s\n' "tgtag: $(( $(printf '%s' "$branches" | wc -w) )) ${descpl#tg }")"
726 updmsg="$(printf '%s\n' "tgtag: $branches")"
728 esac
729 git update-ref -m "$updmsg" "$refname" "$newtag"
730 [ -z "$old" -o "$quiet" -gt 0 ] || printf "Updated $reftype '%s' (was %s)\n" "$tagname" "$old"
732 rm -f "$git_dir/TAG_EDITMSG" "$git_dir/TGTAG_FINALMSG"