3 # Copyright (C) 2015,2017 Kyle J. McKay <mackyle@gmail.com>
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}"
15 if [ "${1:-0}" != 0 ]; then
16 printf '%s\n' "$USAGE" >&2
18 printf '%s\n' "$USAGE"
53 [ -n "$1" ] ||
return 1
65 while [ $# -gt 0 ]; do case "$1" in
70 quiet
=$
(( $quiet + 1 ))
84 -g|
--reflog|
--walk-reflogs)
91 --no-reflog-message|
--commit-message)
101 -u|
--local-user|
--local-user=*)
102 case "$1" in --local-user=*)
105 set -- --local-user "${x#--local-user=}" "$@"
107 if [ $# -lt 2 ]; then
108 echo "The $1 option requires an argument" >&2
130 case "$1" in --tree=*)
133 set -- --tree "${x#--tree=}" "$@"
135 if [ $# -lt 2 ]; then
136 echo "The $1 option requires an argument" >&2
140 treeish
="$(git rev-parse --quiet --verify "$1^
{tree
}" --)" ||
{
141 echo "Not a valid treeish: $1" >&2
148 -m|
--message|
--message=*)
149 case "$1" in --message=*)
152 set -- --message "${x#--message=}" "$@"
154 if [ $# -lt 2 ]; then
155 echo "The $1 option requires an argument" >&2
162 case "$1" in --file=*)
165 set -- --file "${x#--file=}" "$@"
167 if [ $# -lt 2 ]; then
168 echo "The $1 option requires an argument" >&2
174 -n|
--max-count|
--max-count=*|
-[1-9]*)
175 case "$1" in --max-count=*)
178 set -- --max-count "${x#--max-count=}" "$@"
180 case "$1" in -[1-9]*)
185 if [ $# -lt 2 ]; then
186 echo "The $1 option requires an argument" >&2
199 --stash|
--stash"@{"*"}")
200 if [ -n "$reflog" ]; then
201 case "$2" in -[1-9]*)
206 set -- "$x2" "$x1" "$@"
221 echo "Unknown option: $1" >&2
225 if [ -n "$reflog" ]; then
226 case "$2" in -[1-9]*)
231 set -- "$x2" "$x1" "$@"
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; }
255 [ "$tagname" != "--stash" ] || tagname
=refs
/tgstash
256 [ "$tagname" != "--anonymous" ] || tagname
=TG_STASH
257 case "$tagname" in --stash"@{"*"}")
258 strip
="${tagname#--stash??}"
260 tagname
="refs/tgstash@{$strip}"
264 case "$refname" in [!@
]*"@{"*"}")
266 refname
="${refname%@*}"
267 sfx
="${sfx#$refname}"
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
276 refs
/tags
/*|refs
/tgstash
)
280 refname
="refs/tags/$refname"
284 refname
="refs/tags/$refname"
287 refname
="$refname$sfx"
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"
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"
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"
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"
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" --)"
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)"
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)' |
346 while read -r newrev
type rest
; do
347 stashnum
=$
(( $stashnum + 1 ))
348 [ "$type" != "missing" ] ||
continue
349 IFS
="$tab" read -r cmmttr msg
<<-~EOT~
357 obj="$(git rev-parse --verify --quiet --short "$newrev" --)"
359 [ "$type" = "tag" -o -n "$notype" ] ||
360 extra="$hashcolor($metacolor$type$resetcolor$hashcolor)$resetcolor "
361 if [ -z "$reflogmsg" -o -z "$msg" ]; then
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 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"
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
391 [ -z "$signed" -o "$reftype" = "tag" ] || die
"signed tags must be under refs/tags"
392 [ $# -gt 0 ] ||
set -- $defbranch
394 if [ $# -eq 1 ] && [ "$1" = "--all" ]; then
395 eval set -- $
(git for-each-ref
--shell --format="%(refname)" "refs/$topbases")
398 if [ $# -eq 0 ]; then
399 if [ "$quiet" -gt 0 -a -n "$noneok" ]; then
402 die
"no TopGit branches found"
406 ensure_ident_available
415 while read -r obj typ ref
&& [ -n "$obj" -a -n "$typ" ]; do
416 [ -n "$ref" -o "$typ" != "missing" ] || die
"no such ref: ${obj%???}"
417 case " $ignore " in *" $ref "*) continue; esac
418 if [ "$typ" != "commit" -a "$typ" != "tag" ]; then
419 [ -n "$anyrefok" ] || die
"not a committish (is a '$typ') ref: $ref"
420 [ "$quiet" -ge 2 ] || warn
"ignoring non-committish (is a '$typ') ref: $ref"
421 ignore
="${ignore:+$ignore }$ref"
424 case " $newlist " in *" $ref "*);;*)
425 newlist
="${newlist:+$newlist }$ref"
427 if [ "$typ" = "tag" ]; then
428 [ "$quiet" -ge 2 ] || warn
"storing as lightweight tag instead of 'tag' object: $ref"
429 ignore
="${ignore:+$ignore }$ref"
433 printf '%s\n' "$@" | sed 's/^\(.*\)$/\1^{} \1/'
434 printf '%s\n' "$@" | sed 's/^\(.*\)$/\1 \1/'
436 git cat-file --batch-check='%(objectname) %(objecttype) %(rest)' 2>/dev/null ||
443 sfn
="$(git rev-parse --revs-only --symbolic-full-name "$b" -- 2>/dev/null)" ||
:
445 [ -n "$anyrefok" ] || die
"no such symbolic ref name: $b"
446 fullhash
="$(git rev-parse --verify --quiet "$b" --)" || die
"no such ref: $b"
447 case " $extrarefs " in *" $b "*);;*)
448 [ "$quiet" -ge 2 ] || warn
"including non-symbolic ref only in parents calculation: $b"
449 extrarefs
="${extrarefs:+$extrarefs }$fullhash"
457 ref_exists
"refs/heads/${sfn#refs/$topbases/}" || tgish
=
458 [ -n "$anyrefok" ] ||
[ -n "$tgish" ] ||
[ "$quiet" -ge 2 ] ||
459 warn
"including TopGit base that's missing its head: $sfn"
460 case " $allrefs " in *" $sfn "*);;*)
461 allrefs
="${allrefs:+$allrefs }$sfn"
463 case " $branches " in *" ${sfn#refs/$topbases/} "*);;*)
464 branches
="${branches:+$branches }${sfn#refs/$topbases/}"
467 if [ -n "$tgish" ]; then
468 case " $allrefs " in *" refs/heads/${sfn#refs/$topbases/} "*);;*)
469 allrefs
="${allrefs:+$allrefs }refs/heads/${sfn#refs/$topbases/}"
471 case " $tgbranches " in *" ${sfn#refs/$topbases/} "*);;*)
472 tgbranches
="${tgbranches:+$tgbranches }${sfn#refs/$topbases/}"
475 [ -z "$added" ] || tgcount
=$
(( $tgcount + 1 ))
477 [ -z "$added" ] || othercount
=$
(( $othercount + 1 ))
483 ref_exists
"refs/$topbases/${sfn#refs/heads/}" || tgish
=
484 [ -n "$anyrefok" ] ||
[ -n "$tgish" ] ||
485 die
"not a TopGit branch: ${sfn#refs/heads/} (use --allow-any option)"
486 case " $allrefs " in *" $b "*);;*)
487 allrefs
="${allrefs:+$allrefs }$sfn"
489 case " $branches " in *" ${sfn#refs/heads/} "*);;*)
490 branches
="${branches:+$branches }${sfn#refs/heads/}"
493 if [ -n "$tgish" ]; then
494 case " $allrefs " in *" refs/$topbases/${sfn#refs/heads/} "*);;*)
495 allrefs
="${allrefs:+$allrefs }refs/$topbases/${sfn#refs/heads/}"
497 case " $tgbranches " in *" ${sfn#refs/heads/} "*);;*)
498 tgbranches
="${tgbranches:+$tgbranches }${sfn#refs/heads/}"
501 [ -z "$added" ] || tgcount
=$
(( $tgcount + 1 ))
503 [ -z "$added" ] || othercount
=$
(( $othercount + 1 ))
507 [ -n "$anyrefok" ] || die
"refusing to include without --allow-any: $sfn"
508 case " $allrefs " in *" $sfn "*);;*)
509 allrefs
="${allrefs:+$allrefs }$sfn"
511 case " $branches " in *" ${sfn#refs/} "*);;*)
512 branches
="${branches:+$branches }${sfn#refs/}"
513 othercount
=$
(( $othercount + 1 ))
520 ! git rev-parse
--verify --quiet "$refname" -- >/dev
/null ||
521 die
"$reftype '$tagname' already exists"
525 if [ $othercount -gt 0 ]; then
526 if [ $tgcount -eq 0 ]; then
530 descpl
="$descpl and refs"
535 case " $seen_deps " in *" $_dep "*) return 0; esac
536 seen_deps
="${seen_deps:+$seen_deps }$_dep"
537 printf 'refs/heads/%s\n' "$_dep"
538 [ -z "$_dep_is_tgish" ] ||
printf 'refs/%s/%s\n' "$topbases" "$_dep"
544 recurse_deps_exclude
=
546 case " $recurse_deps_exclude " in *" $_b "*) continue; esac
548 _dep
="$_b"; _dep_is_tgish
=1; get_dep
549 recurse_deps get_dep
"$_b"
550 recurse_deps_exclude
="$recurse_deps_exclude $seen_deps"
556 get_deps_internal
"$@" | LC_ALL
=C
sort -u
560 if [ -n "$outofdateok" ]; then
561 if [ -n "$tgbranches" ]; then
562 while read -r dep
&& [ -n "$dep" ]; do
563 case " $allrefs " in *" $dep "*);;*)
564 ! ref_exists
"$dep" ||
565 allrefs
="${allrefs:+$allrefs }$dep"
568 $(get_deps $tgbranches)
572 for b
in $tgbranches; do
573 if ! needs_update
"$b" >/dev
/null
; then
575 echo "branch not up-to-date: $b"
579 [ -z "$out_of_date" ] || die
"all branches to be tagged must be up-to-date"
583 printf '%s\n' '-----BEGIN TOPGIT REFS-----'
585 printf '%s\n' $allrefs
586 [ -n "$outofdateok" ] || get_deps
$tgbranches
587 } | LC_ALL
=C
sort -u |
sed 's/^\(.*\)$/\1^0 \1/' |
588 git cat-file
--batch-check='%(objectname) %(rest)' 2>/dev
/null |
589 grep -v ' missing$' ||
:
590 printf '%s\n' '-----END TOPGIT REFS-----'
593 if [ -n "$refsonly" ]; then
599 if [ -n "$msgfile" ]; then
600 if [ "$msgfile" = "-" ]; then
601 git stripspace
>"$git_dir/TAG_EDITMSG"
603 git stripspace
<"$msgfile" >"$git_dir/TAG_EDITMSG"
605 elif [ -n "$msg" ]; then
606 printf '%s\n' "$msg" | git stripspace
>"$git_dir/TAG_EDITMSG"
610 if [ ${#branches} -le 60 ]; then
611 printf '%s\n' "tag $descpl $branches"
612 printf '%s\n' "$updmsg"
614 printf '%s\n' "tag $(( $(printf '%s' "$branches" | wc -w) )) $descpl" ""
615 for b
in $branches; do
621 printf '%s\n' "tag $desc $branches"
623 esac | git stripspace
>"$git_dir/TAG_EDITMSG"
624 if [ -z "$noedit" ]; then
628 # Please enter a message for tg tag:
630 # Lines starting with '#' will be ignored.
632 # $descpl to be tagged:
635 for b
in $branches; do
638 } >>"$git_dir/TAG_EDITMSG"
640 run_editor
"$git_dir/TAG_EDITMSG" ||
641 die
"there was a problem with the editor '$tg_editor'"
644 git stripspace
${stripcomments:+ --strip-comments} \
645 <"$git_dir/TAG_EDITMSG" >"$git_dir/TGTAG_FINALMSG"
646 [ -s "$git_dir/TGTAG_FINALMSG" ] || die
"no tag message?"
647 echo "" >>"$git_dir/TGTAG_FINALMSG"
648 get_refs
>>"$git_dir/TGTAG_FINALMSG"
650 v_count_args
() { eval "$1="'$(( $# - 1 ))'; }
653 case "$allrefs${extrarefs:+ $extrarefs}" in
655 parents
="$(git merge-base --independent \
656 $(printf '%s^0 ' $allrefs $extrarefs))" ||
657 die
"failed: git merge-base --independent"
663 v_count_args pcnt
$parents
664 if [ $pcnt -eq 1 ]; then
667 [ "$(git rev-parse --quiet --verify "$tagtarget^
{tree
}" --)" = "$treeish" ] ||
670 if [ -z "$tagtarget" ]; then
672 [ -n "$tagtree" ] || tagtree
="$(git hash-object -t tree -w --stdin </dev/null)"
673 tagtarget
="$(printf '%s\n' "tg tag branch consolidation
" "" $branches |
674 git commit-tree $tagtree $(printf -- '-p %s ' $parents))"
677 init_reflog
"$refname"
678 if [ "$reftype" = "tag" -a -n "$signed" ]; then
679 [ "$quiet" -eq 0 ] ||
exec >/dev
/null
680 git tag
-F "$git_dir/TGTAG_FINALMSG" ${signed:+-s} ${force:+-f} \
681 ${keyid:+-u} ${keyid} "$tagname" "$tagtarget"
683 obj
="$(git rev-parse --verify --quiet "$tagtarget" --)" ||
684 die
"invalid object name: $tagtarget"
685 typ
="$(git cat-file -t "$tagtarget" 2>/dev/null)" ||
686 die
"invalid object name: $tagtarget"
687 id
="$(git var GIT_COMMITTER_IDENT 2>/dev/null)" ||
688 die
"could not get GIT_COMMITTER_IDENT"
690 printf '%s\n' "object
$obj" "type $typ" "tag
$tagname" \
692 cat "$git_dir/TGTAG_FINALMSG
"
693 } | git mktag)" || die
"git mktag failed"
694 old
="$(git rev-parse --verify --short --quiet "$refname" --)" ||
:
698 if [ ${#branches} -le 100 ]; then
699 updmsg
="$(printf '%s\n' "tgtag
: $branches")"
701 updmsg
="$(printf '%s\n' "tgtag
: $
(( $
(printf '%s' "$branches" |
wc -w) )) ${descpl#tg }")"
705 updmsg
="$(printf '%s\n' "tgtag
: $branches")"
708 git update-ref
-m "$updmsg" "$refname" "$newtag"
709 [ -z "$old" -o "$quiet" -gt 0 ] ||
printf "Updated $reftype '%s' (was %s)\n" "$tagname" "$old"
711 rm -f "$git_dir/TAG_EDITMSG" "$git_dir/TGTAG_FINALMSG"