t6100: test tg tag reflog manipulations
[topgit/pro.git] / tg-tag.sh
blobb341e20737a83015babfacd052afd7702a4ce06a
1 #!/bin/sh
2 # TopGit tag command
3 # Copyright (C) 2015,2017,2018 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" ] || [ -n "$reflog$drop$clear$delete" ] || { outofdateok=1; force=1; defnoedit=1; }
241 [ -n "$noedit" ] || noedit="$defnoedit"
242 [ "$noedit" != "0" ] || noedit=
243 [ -z "$reflog" ] || [ -z "$drop$clear$delete$signed$keyid$force$msg$msgfile$noedit$treeish$refsonly$outofdateok" ] || usage 1
244 [ -n "$reflog" ] || [ -z "$setreflogmsg$notype$maxcount" ] || usage 1
245 [ -z "$drop$clear$delete" ] || [ -z "$setreflogmsg$notype$maxcount$signed$keyid$force$msg$msgfile$noedit$treeish$refsonly$outofdateok" ] || usage 1
246 [ -z "$reflog$drop$clear$delete" ] || [ "$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" ] || [ -z "$msgfile" ] || die "only one -F or -m option is allowed."
250 [ -z "$refsonly" ] || set -- refs..only "$@"
251 [ $# -gt 0 ] || [ -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 sfxis0=
265 case "$refname" in [!@]*"@{"*"}")
266 _pfx="@{"
267 _refonly="${refname%%"$_pfx"*}"
268 sfx="${refname#"$_refonly"}"
269 refname="$_refonly"
270 _numonly="${sfx#??}"
271 _numonly="${_numonly%?}"
272 [ "${_numonly#[0-9]}" != "$_numonly" ] && [ "${_numonly#*[!0-9]}" = "$_numonly" ] || die "invalid suffix: \"$sfx\""
273 if [ "${_numonly#*[!0]}" = "$_numonly" ]; then
274 # transform @{0000000} etc. into @{0}
275 sfx="@{0}"
276 sfxis0=1
277 else
278 # remove any leading zeros
279 _ld0="${_numonly%%[!0]*}"
280 [ -z "$_ld0" ] || _numonly="${_numonly#$_ld0}"
281 sfx="@{$_numonly}"
283 esac
284 case "$refname" in HEAD|TG_STASH|refs/*);;*)
285 if reftest="$(git rev-parse --revs-only --symbolic-full-name "$refname" -- 2>/dev/null)" &&
286 [ -n "$reftest" ]; then
287 if [ -n "$reflog$drop$clear$delete" ]; then
288 refname="$reftest"
289 else
290 case "$reftest" in
291 refs/tags/*|refs/tgstash)
292 refname="$reftest"
295 refname="refs/tags/$refname"
296 esac
298 else
299 refname="refs/tags/$refname"
301 esac
302 refname="$refname$sfx"
303 reftype=tag
304 case "$refname" in refs/tags/*) tagname="${refname#refs/tags/}";; *) reftype=ref; tagname="$refname"; esac
305 logbase="$git_common_dir"
306 [ "${refname%$sfx}" != "HEAD" ] || logbase="$git_dir"
307 [ -z "$reflog$drop$clear$delete" ] || [ $# -eq 0 ] || usage 1
308 if [ -n "$drop$clear$delete" ]; then
309 if [ -n "$sfx" ]; then
310 [ -z "$clear$delete" ] || die "invalid ref name ($sfx suffix not allowed): $refname"
311 else
312 [ -z "$drop" ] || die "invalid reflog name (@{n} suffix required): $refname"
314 old="$(git rev-parse --verify --quiet --short "${refname%$sfx}" --)" || die "no such ref: ${refname%$sfx}"
315 if [ -n "$delete" ]; then
316 git update-ref -d "$refname" || die "git update-ref -d failed"
317 printf "Deleted $reftype '%s' (was %s)\n" "$tagname" "$old"
318 exit 0
319 elif [ -n "$clear" ]; then
320 [ -f "$logbase/logs/$refname" ] || die "no reflog found for: $refname"
321 [ -s "$logbase/logs/$refname" ] || die "empty reflog found for: $refname"
322 cp -p "$logbase/logs/$refname" "$logbase/logs/$refname^-+" || die "cp failed"
323 sed -n '$p' <"$logbase/logs/$refname^-+" >"$logbase/logs/$refname" || die "reflog clear failed"
324 rm -f "$logbase/logs/$refname^-+"
325 printf "Cleared $reftype '%s' reflog to single @{0} entry\n" "$tagname"
326 exit 0
327 else
328 old="$(git rev-parse --verify --short "$refname" --)" || exit 1
329 [ -z "$sfxis0" ] || ! git symbolic-ref -q "${refname%$sfx}" -- >/dev/null 2>&1 || sfxis0=
330 if [ -n "$sfxis0" ]; then
331 [ -f "$logbase/logs/${refname%$sfx}" ] || die "no reflog found for: ${refname%$sfx}"
332 [ -s "$logbase/logs/${refname%$sfx}" ] || die "empty reflog found for: ${refname%$sfx}"
333 # make sure @{1} is valid via pseudo stale-fix before using --updateref
334 cnt="$(( $(wc -l <"$logbase/logs/${refname%$sfx}") ))"
335 lastcnt=
336 at1=
337 while
338 # avoid using --updateref if @{0} is the only entry (i.e. less than 2 lines in log)
339 [ $cnt -ge 2 ] && [ "$cnt" != "$lastcnt" ] &&
340 at1="$(git rev-parse --verify --quiet "${refname%$sfx}@{1}" -- 2>/dev/null)" &&
341 [ -n "$at1" ] &&
342 ! git rev-list --no-walk --objects "$at1" -- >/dev/null 2>&1
344 # poor man's --stale-fix that's faster and actually works reliably
345 git reflog delete --rewrite "${refname%$sfx}@{1}" >/dev/null 2>&1 ||
346 die "pseudo stale-fix failed for broken ${refname%$sfx}@{1}"
347 lastcnt="$cnt"
348 at1=
349 cnt="$(( $(wc -l <"$logbase/logs/${refname%$sfx}") ))"
350 done
351 # avoid using --updateref if @{0} is the only entry (i.e. less than 2 lines in log)
352 [ -n "$at1" ] && [ $cnt -ge 2 ] || sfxis0=
354 git reflog delete --rewrite ${sfxis0:+--updateref} "$refname" || die "reflog drop failed"
355 if [ -n "$sfxis0" ]; then
356 # check if we need to clean up
357 check="$(git rev-parse --verify --quiet "${refname%$sfx}" --)" || :
358 [ "${check#*[!0]}" != "$check" ] || check= # all 0's or empty is bad
359 # Git versions prior to 2.4.0 might need some clean up
360 [ -n "$check" ] || git update-ref -d "${refname%$sfx}" >/dev/null 2>&1 || :
362 printf "Dropped $reftype '%s' reflog entry (was %s)\n" "$tagname" "$old"
363 exit 0
366 if [ -n "$reflog" ]; then
367 [ "$refname" = "refs/tgstash" ] || [ -n "$setreflogmsg" ] || reflogmsg=1
368 git rev-parse --verify --quiet "$refname" -- >/dev/null ||
369 die "no such ref: $refname"
370 [ -s "$logbase/logs/$refname" ] ||
371 die "no reflog present for $reftype: $tagname"
372 showref="$refname"
373 [ "$refname" = "HEAD" ] || showref="$(git rev-parse --revs-only --abbrev-ref=strict "$refname" --)"
374 hashcolor=
375 resetcolor=
376 if git config --get-colorbool color.tgtag; then
377 metacolor="$(git config --get-color color.tgtag.meta)"
378 [ -n "$metacolor" ] || metacolor="$(git config --get-color color.diff.meta "bold")"
379 hashcolor="$(git config --get-color color.tgtag.commit)"
380 [ -n "$hashcolor" ] || hashcolor="$(git config --get-color color.diff.commit "yellow")"
381 datecolor="$(git config --get-color color.tgtag.date "bold blue")"
382 timecolor="$(git config --get-color color.tgtag.time "green")"
383 resetcolor="$(git config --get-color "" reset)"
385 setup_strftime
386 output()
388 sed 's/[^ ][^ ]* //' <"$logbase/logs/$refname" |
389 awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--]}' |
390 git cat-file --batch-check='%(objectname) %(objecttype) %(rest)' |
392 stashnum=-1
393 lastdate=
394 while read -r newrev type rest; do
395 stashnum=$(( $stashnum + 1 ))
396 [ "$type" != "missing" ] || continue
397 IFS="$tab" read -r cmmttr msg <<-~EOT~
398 $rest
399 ~EOT~
400 ne="${cmmttr% *}"
401 ne="${ne% *}"
402 es="${cmmttr#$ne}"
403 es="${es% *}"
404 es="${es# }"
405 obj="$(git rev-parse --verify --quiet --short "$newrev" --)"
406 extra=
407 [ "$type" = "tag" ] || [ -n "$notype" ] ||
408 extra="$hashcolor($metacolor$type$resetcolor$hashcolor)$resetcolor "
409 if [ -z "$reflogmsg" ] || [ -z "$msg" ]; then
410 objmsg=
411 if [ "$type" = "tag" ]; then
412 objmsg="$(git cat-file tag "$obj" |
413 sed '1,/^$/d' | sed '/^$/,$d')"
414 elif [ "$type" = "commit" ]; then
415 objmsg="$(git --no-pager log -n 1 --format='format:%s' "$obj" --)"
417 [ -z "$objmsg" ] || msg="$objmsg"
419 read newdate newtime <<-EOT
420 $(strftime "%Y-%m-%d %H:%M:%S" "$es")
422 if [ "$lastdate" != "$newdate" ]; then
423 printf '%s=== %s ===%s\n' "$datecolor" "$newdate" "$resetcolor"
424 lastdate="$newdate"
426 printf '%s %s %s%s@{%s}: %s\n' "$hashcolor$obj$reseutcolor" \
427 "$timecolor$newtime$resetcolor" \
428 "$extra" "$showref" "$stashnum" "$msg"
429 if [ -n "$maxcount" ]; then
430 maxcount=$(( $maxcount - 1 ))
431 [ $maxcount -gt 0 ] || break
433 done
436 page output
437 exit 0
439 [ -z "$signed" ] || [ "$reftype" = "tag" ] || die "signed tags must be under refs/tags"
440 [ $# -gt 0 ] || set -- $defbranch
441 all=
442 if [ $# -eq 1 ] && [ "$1" = "--all" ]; then
443 eval set -- $(git for-each-ref --shell --format="%(refname)" "refs/$topbases")
444 outofdateok=1
445 all=1
446 if [ $# -eq 0 ]; then
447 if [ "$quiet" -gt 0 ] && [ -n "$noneok" ]; then
448 exit 0
449 else
450 die "no TopGit branches found"
454 ensure_ident_available
455 branches=
456 allrefs=
457 extrarefs=
458 tgbranches=
459 tgcount=0
460 othercount=0
461 ignore=
462 newlist=
463 firstprnt=
464 for arg in "$@"; do
465 case "$arg" in "~"?*)
466 [ -z "$firstprnt" ] || die "only one first parent may be specified with ~"
467 firstprnt="$(git rev-parse --verify --quiet "${arg#?}^0" -- 2>/dev/null)" && [ -n "$firstprnt" ] ||
468 die "not a commit-ish: ${arg#?}"
469 esac
470 done
471 while read -r obj typ ref && [ -n "$obj" ] && [ -n "$typ" ]; do
472 [ -n "$ref" ] || [ "$typ" != "missing" ] || die "no such ref: ${obj%???}"
473 case " $ignore " in *" $ref "*) continue; esac
474 if [ "$typ" != "commit" ] && [ "$typ" != "tag" ]; then
475 [ -n "$anyrefok" ] || die "not a committish (is a '$typ') ref: $ref"
476 [ "$quiet" -ge 2 ] || warn "ignoring non-committish (is a '$typ') ref: $ref"
477 ignore="${ignore:+$ignore }$ref"
478 continue
480 case " $newlist " in *" $ref "*);;*)
481 newlist="${newlist:+$newlist }$ref"
482 esac
483 if [ "$typ" = "tag" ]; then
484 [ "$quiet" -ge 2 ] || warn "storing as lightweight tag instead of 'tag' object: $ref"
485 ignore="${ignore:+$ignore }$ref"
487 done <<-EOT
489 printf '%s\n' "$@" | sed 's/^~//; s/^\(.*\)$/\1^{} \1/'
490 printf '%s\n' "$@" | sed 's/^~//; s/^\(.*\)$/\1 \1/'
492 git cat-file --batch-check='%(objectname) %(objecttype) %(rest)' 2>/dev/null ||
495 set -- $newlist
496 for b; do
497 sfn="$b"
498 [ -n "$all" ] ||
499 sfn="$(git rev-parse --revs-only --symbolic-full-name "$b" -- 2>/dev/null)" || :
500 [ -n "$sfn" ] || {
501 [ -n "$anyrefok" ] || die "no such symbolic ref name: $b"
502 fullhash="$(git rev-parse --verify --quiet "$b" --)" || die "no such ref: $b"
503 case " $extrarefs " in *" $b "*);;*)
504 [ "$quiet" -ge 2 ] || warn "including non-symbolic ref only in parents calculation: $b"
505 extrarefs="${extrarefs:+$extrarefs }$fullhash"
506 esac
507 continue
509 case "$sfn" in
510 refs/"$topbases"/*)
511 added=
512 tgish=1
513 ref_exists "refs/heads/${sfn#refs/$topbases/}" || tgish=
514 [ -n "$anyrefok" ] || [ -n "$tgish" ] || [ "$quiet" -ge 2 ] ||
515 warn "including TopGit base that's missing its head: $sfn"
516 case " $allrefs " in *" $sfn "*);;*)
517 allrefs="${allrefs:+$allrefs }$sfn"
518 esac
519 case " $branches " in *" ${sfn#refs/$topbases/} "*);;*)
520 branches="${branches:+$branches }${sfn#refs/$topbases/}"
521 added=1
522 esac
523 if [ -n "$tgish" ]; then
524 case " $allrefs " in *" refs/heads/${sfn#refs/$topbases/} "*);;*)
525 allrefs="${allrefs:+$allrefs }refs/heads/${sfn#refs/$topbases/}"
526 esac
527 case " $tgbranches " in *" ${sfn#refs/$topbases/} "*);;*)
528 tgbranches="${tgbranches:+$tgbranches }${sfn#refs/$topbases/}"
529 added=1
530 esac
531 [ -z "$added" ] || tgcount=$(( $tgcount + 1 ))
532 else
533 [ -z "$added" ] || othercount=$(( $othercount + 1 ))
536 refs/heads/*)
537 added=
538 tgish=1
539 ref_exists "refs/$topbases/${sfn#refs/heads/}" || tgish=
540 [ -n "$anyrefok" ] || [ -n "$tgish" ] ||
541 die "not a TopGit branch: ${sfn#refs/heads/} (use --allow-any option)"
542 case " $allrefs " in *" $b "*);;*)
543 allrefs="${allrefs:+$allrefs }$sfn"
544 esac
545 case " $branches " in *" ${sfn#refs/heads/} "*);;*)
546 branches="${branches:+$branches }${sfn#refs/heads/}"
547 added=1
548 esac
549 if [ -n "$tgish" ]; then
550 case " $allrefs " in *" refs/$topbases/${sfn#refs/heads/} "*);;*)
551 allrefs="${allrefs:+$allrefs }refs/$topbases/${sfn#refs/heads/}"
552 esac
553 case " $tgbranches " in *" ${sfn#refs/heads/} "*);;*)
554 tgbranches="${tgbranches:+$tgbranches }${sfn#refs/heads/}"
555 added=1
556 esac
557 [ -z "$added" ] || tgcount=$(( $tgcount + 1 ))
558 else
559 [ -z "$added" ] || othercount=$(( $othercount + 1 ))
563 [ -n "$anyrefok" ] || die "refusing to include without --allow-any: $sfn"
564 case " $allrefs " in *" $sfn "*);;*)
565 allrefs="${allrefs:+$allrefs }$sfn"
566 esac
567 case " $branches " in *" ${sfn#refs/} "*);;*)
568 branches="${branches:+$branches }${sfn#refs/}"
569 othercount=$(( $othercount + 1 ))
570 esac
572 esac
573 done
575 [ -n "$force" ] ||
576 ! git rev-parse --verify --quiet "$refname" -- >/dev/null ||
577 die "$reftype '$tagname' already exists"
579 desc="tg branch"
580 descpl="tg branches"
581 if [ $othercount -gt 0 ]; then
582 if [ $tgcount -eq 0 ]; then
583 desc="ref"
584 descpl="refs"
585 else
586 descpl="$descpl and refs"
590 get_dep() {
591 case " $seen_deps " in *" $_dep "*) return 0; esac
592 seen_deps="${seen_deps:+$seen_deps }$_dep"
593 printf 'refs/heads/%s\n' "$_dep"
594 [ -z "$_dep_is_tgish" ] || printf 'refs/%s/%s\n' "$topbases" "$_dep"
597 get_deps_internal()
599 no_remotes=1
600 recurse_deps_exclude=
601 for _b; do
602 case " $recurse_deps_exclude " in *" $_b "*) continue; esac
603 seen_deps=
604 _dep="$_b"; _dep_is_tgish=1; get_dep
605 recurse_deps get_dep "$_b"
606 recurse_deps_exclude="$recurse_deps_exclude $seen_deps"
607 done
610 get_deps()
612 get_deps_internal "$@" | sort -u
615 out_of_date=
616 if [ -n "$outofdateok" ]; then
617 if [ -n "$tgbranches" ]; then
618 while read -r dep && [ -n "$dep" ]; do
619 case " $allrefs " in *" $dep "*);;*)
620 ! ref_exists "$dep" ||
621 allrefs="${allrefs:+$allrefs }$dep"
622 esac
623 done <<-EOT
624 $(get_deps $tgbranches)
627 else
628 for b in $tgbranches; do
629 if ! needs_update "$b" >/dev/null; then
630 out_of_date=1
631 echo "branch not up-to-date: $b"
633 done
635 [ -z "$out_of_date" ] || die "all branches to be tagged must be up-to-date"
637 get_refs()
639 printf '%s\n' '-----BEGIN TOPGIT REFS-----'
641 printf '%s\n' $allrefs
642 [ -n "$outofdateok" ] || get_deps $tgbranches
643 } | sort -u | sed 's/^\(.*\)$/\1^0 \1/' |
644 git cat-file --batch-check='%(objectname) %(rest)' 2>/dev/null |
645 grep -v ' missing$' || :
646 printf '%s\n' '-----END TOPGIT REFS-----'
649 if [ -n "$refsonly" ]; then
650 get_refs
651 exit 0
654 stripcomments=
655 if [ -n "$msgfile" ]; then
656 if [ "$msgfile" = "-" ]; then
657 git stripspace >"$git_dir/TAG_EDITMSG"
658 else
659 git stripspace <"$msgfile" >"$git_dir/TAG_EDITMSG"
661 elif [ -n "$msg" ]; then
662 printf '%s\n' "$msg" | git stripspace >"$git_dir/TAG_EDITMSG"
663 else
664 case "$branches" in
665 *" "*)
666 if [ ${#branches} -le 60 ]; then
667 printf '%s\n' "tag $descpl $branches"
668 printf '%s\n' "$updmsg"
669 else
670 printf '%s\n' "tag $(( $(printf '%s' "$branches" | wc -w) )) $descpl" ""
671 for b in $branches; do
672 printf '%s\n' "$b"
673 done
677 printf '%s\n' "tag $desc $branches"
679 esac | git stripspace >"$git_dir/TAG_EDITMSG"
680 if [ -z "$noedit" ]; then
682 cat <<EOT
684 # Please enter a message for tg tag:
685 # $tagname
686 # Lines starting with '#' will be ignored.
688 # $descpl to be tagged:
691 for b in $branches; do
692 printf '%s\n' "# $b"
693 done
694 } >>"$git_dir/TAG_EDITMSG"
695 stripcomments=1
696 run_editor "$git_dir/TAG_EDITMSG" ||
697 die "there was a problem with the editor '$tg_editor'"
700 git stripspace ${stripcomments:+ --strip-comments} \
701 <"$git_dir/TAG_EDITMSG" >"$git_dir/TGTAG_FINALMSG"
702 [ -s "$git_dir/TGTAG_FINALMSG" ] || die "no tag message?"
703 echo "" >>"$git_dir/TGTAG_FINALMSG"
704 get_refs >>"$git_dir/TGTAG_FINALMSG"
706 v_count_args() { eval "$1="'$(( $# - 1 ))'; }
708 tagtarget=
709 case "$allrefs${extrarefs:+ $extrarefs}" in
710 *" "*)
711 parents="$(git merge-base --independent \
712 $(printf '%s^0 ' $allrefs $extrarefs))" ||
713 die "failed: git merge-base --independent"
716 if [ -n "$firstprnt" ]; then
717 parents="$(git rev-parse --quiet --verify "$allrefs^0" --)" ||
718 die "failed: git rev-parse $allrefs^0"
719 else
720 parents="$allrefs^0"
723 esac
724 if [ -n "$firstprnt" ]; then
725 oldparents="$parents"
726 parents="$firstprnt"
727 for acmt in $oldparents; do
728 [ "$acmt" = "$firstprnt" ] || parents="$parents $acmt"
729 done
730 unset oldparents
732 v_count_args pcnt $parents
733 if [ $pcnt -eq 1 ]; then
734 tagtarget="$parents"
735 [ -z "$treeish" ] ||
736 [ "$(git rev-parse --quiet --verify "$tagtarget^{tree}" --)" = "$treeish" ] ||
737 tagtarget=
739 if [ -z "$tagtarget" ]; then
740 tagtree="${treeish:-$firstprnt}"
741 [ -n "$tagtree" ] || tagtree="$(git hash-object -t tree -w --stdin </dev/null)"
742 tagtarget="$(printf '%s\n' "tg tag branch consolidation" "" $branches |
743 git commit-tree $tagtree^{tree} $(printf -- '-p %s ' $parents))"
746 init_reflog "$refname"
747 if [ "$reftype" = "tag" ] && [ -n "$signed" ]; then
748 [ "$quiet" -eq 0 ] || exec >/dev/null
749 git tag -F "$git_dir/TGTAG_FINALMSG" ${signed:+-s} ${force:+-f} \
750 ${keyid:+-u} ${keyid} "$tagname" "$tagtarget"
751 else
752 obj="$(git rev-parse --verify --quiet "$tagtarget" --)" ||
753 die "invalid object name: $tagtarget"
754 typ="$(git cat-file -t "$tagtarget" 2>/dev/null)" ||
755 die "invalid object name: $tagtarget"
756 id="$(git var GIT_COMMITTER_IDENT 2>/dev/null)" ||
757 die "could not get GIT_COMMITTER_IDENT"
758 newtag="$({
759 printf '%s\n' "object $obj" "type $typ" "tag $tagname" \
760 "tagger $id" ""
761 cat "$git_dir/TGTAG_FINALMSG"
762 } | git mktag)" || die "git mktag failed"
763 old="$(git rev-parse --verify --short --quiet "$refname" --)" || :
764 updmsg=
765 case "$branches" in
766 *" "*)
767 if [ ${#branches} -le 100 ]; then
768 updmsg="$(printf '%s\n' "tgtag: $branches")"
769 else
770 updmsg="$(printf '%s\n' "tgtag: $(( $(printf '%s' "$branches" | wc -w) )) ${descpl#tg }")"
774 updmsg="$(printf '%s\n' "tgtag: $branches")"
776 esac
777 git update-ref -m "$updmsg" "$refname" "$newtag"
778 [ -z "$old" ] || [ "$quiet" -gt 0 ] || printf "Updated $reftype '%s' (was %s)\n" "$tagname" "$old"
780 rm -f "$git_dir/TAG_EDITMSG" "$git_dir/TGTAG_FINALMSG"