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" ] ||
[ -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; }
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" ] ||
[ $# -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" ] ||
[ -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" ] || [ -n "$notype" ] ||
360 extra="$hashcolor($metacolor$type$resetcolor$hashcolor)$resetcolor "
361 if [ -z "$reflogmsg" ] || [ -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 --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"
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" ] ||
[ "$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 ] && [ -n "$noneok" ]; then
402 die
"no TopGit branches found"
406 ensure_ident_available
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#?}"
423 while read -r obj typ ref
&& [ -n "$obj" ] && [ -n "$typ" ]; do
424 [ -n "$ref" ] ||
[ "$typ" != "missing" ] || die
"no such ref: ${obj%???}"
425 case " $ignore " in *" $ref "*) continue; esac
426 if [ "$typ" != "commit" ] && [ "$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"
432 case " $newlist " in *" $ref "*);;*)
433 newlist
="${newlist:+$newlist }$ref"
435 if [ "$typ" = "tag" ]; then
436 [ "$quiet" -ge 2 ] || warn
"storing as lightweight tag instead of 'tag' object: $ref"
437 ignore
="${ignore:+$ignore }$ref"
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 ||
451 sfn
="$(git rev-parse --revs-only --symbolic-full-name "$b" -- 2>/dev/null)" ||
:
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"
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"
471 case " $branches " in *" ${sfn#refs/$topbases/} "*);;*)
472 branches
="${branches:+$branches }${sfn#refs/$topbases/}"
475 if [ -n "$tgish" ]; then
476 case " $allrefs " in *" refs/heads/${sfn#refs/$topbases/} "*);;*)
477 allrefs
="${allrefs:+$allrefs }refs/heads/${sfn#refs/$topbases/}"
479 case " $tgbranches " in *" ${sfn#refs/$topbases/} "*);;*)
480 tgbranches
="${tgbranches:+$tgbranches }${sfn#refs/$topbases/}"
483 [ -z "$added" ] || tgcount
=$
(( $tgcount + 1 ))
485 [ -z "$added" ] || othercount
=$
(( $othercount + 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"
497 case " $branches " in *" ${sfn#refs/heads/} "*);;*)
498 branches
="${branches:+$branches }${sfn#refs/heads/}"
501 if [ -n "$tgish" ]; then
502 case " $allrefs " in *" refs/$topbases/${sfn#refs/heads/} "*);;*)
503 allrefs
="${allrefs:+$allrefs }refs/$topbases/${sfn#refs/heads/}"
505 case " $tgbranches " in *" ${sfn#refs/heads/} "*);;*)
506 tgbranches
="${tgbranches:+$tgbranches }${sfn#refs/heads/}"
509 [ -z "$added" ] || tgcount
=$
(( $tgcount + 1 ))
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"
519 case " $branches " in *" ${sfn#refs/} "*);;*)
520 branches
="${branches:+$branches }${sfn#refs/}"
521 othercount
=$
(( $othercount + 1 ))
528 ! git rev-parse
--verify --quiet "$refname" -- >/dev
/null ||
529 die
"$reftype '$tagname' already exists"
533 if [ $othercount -gt 0 ]; then
534 if [ $tgcount -eq 0 ]; then
538 descpl
="$descpl and refs"
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"
552 recurse_deps_exclude
=
554 case " $recurse_deps_exclude " in *" $_b "*) continue; esac
556 _dep
="$_b"; _dep_is_tgish
=1; get_dep
557 recurse_deps get_dep
"$_b"
558 recurse_deps_exclude
="$recurse_deps_exclude $seen_deps"
564 get_deps_internal
"$@" |
sort -u
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"
576 $(get_deps $tgbranches)
580 for b
in $tgbranches; do
581 if ! needs_update
"$b" >/dev
/null
; then
583 echo "branch not up-to-date: $b"
587 [ -z "$out_of_date" ] || die
"all branches to be tagged must be up-to-date"
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
607 if [ -n "$msgfile" ]; then
608 if [ "$msgfile" = "-" ]; then
609 git stripspace
>"$git_dir/TAG_EDITMSG"
611 git stripspace
<"$msgfile" >"$git_dir/TAG_EDITMSG"
613 elif [ -n "$msg" ]; then
614 printf '%s\n' "$msg" | git stripspace
>"$git_dir/TAG_EDITMSG"
618 if [ ${#branches} -le 60 ]; then
619 printf '%s\n' "tag $descpl $branches"
620 printf '%s\n' "$updmsg"
622 printf '%s\n' "tag $(( $(printf '%s' "$branches" | wc -w) )) $descpl" ""
623 for b
in $branches; do
629 printf '%s\n' "tag $desc $branches"
631 esac | git stripspace
>"$git_dir/TAG_EDITMSG"
632 if [ -z "$noedit" ]; then
636 # Please enter a message for tg tag:
638 # Lines starting with '#' will be ignored.
640 # $descpl to be tagged:
643 for b
in $branches; do
646 } >>"$git_dir/TAG_EDITMSG"
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 ))'; }
661 case "$allrefs${extrarefs:+ $extrarefs}" in
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"
676 if [ -n "$firstprnt" ]; then
677 oldparents
="$parents"
679 for acmt
in $oldparents; do
680 [ "$acmt" = "$firstprnt" ] || parents
="$parents $acmt"
684 v_count_args pcnt
$parents
685 if [ $pcnt -eq 1 ]; then
688 [ "$(git rev-parse --quiet --verify "$tagtarget^
{tree
}" --)" = "$treeish" ] ||
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" ] && [ -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"
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"
711 printf '%s\n' "object
$obj" "type $typ" "tag
$tagname" \
713 cat "$git_dir/TGTAG_FINALMSG
"
714 } | git mktag)" || die
"git mktag failed"
715 old
="$(git rev-parse --verify --short --quiet "$refname" --)" ||
:
719 if [ ${#branches} -le 100 ]; then
720 updmsg
="$(printf '%s\n' "tgtag
: $branches")"
722 updmsg
="$(printf '%s\n' "tgtag
: $
(( $
(printf '%s' "$branches" |
wc -w) )) ${descpl#tg }")"
726 updmsg
="$(printf '%s\n' "tgtag
: $branches")"
729 git update-ref
-m "$updmsg" "$refname" "$newtag"
730 [ -z "$old" ] ||
[ "$quiet" -gt 0 ] ||
printf "Updated $reftype '%s' (was %s)\n" "$tagname" "$old"
732 rm -f "$git_dir/TAG_EDITMSG" "$git_dir/TGTAG_FINALMSG"