3 # Copyright (C) 2015,2017,2018 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}"
265 case "$refname" in [!@
]*"@{"*"}")
267 _refonly
="${refname%%"$_pfx"*}"
268 sfx
="${refname#"$_refonly"}"
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}
278 # remove any leading zeros
279 _ld0
="${_numonly%%[!0]*}"
280 [ -z "$_ld0" ] || _numonly
="${_numonly#$_ld0}"
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
291 refs
/tags
/*|refs
/tgstash
)
295 refname
="refs/tags/$refname"
299 refname
="refs/tags/$refname"
302 refname
="$refname$sfx"
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"
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"
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"
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}") ))"
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)" &&
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}"
349 cnt
="$(( $(wc -l <"$logbase/logs
/${refname%$sfx}") ))"
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"
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"
373 [ "$refname" = "HEAD" ] || showref
="$(git rev-parse --revs-only --abbrev-ref=strict "$refname" --)"
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)"
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)' |
394 while read -r newrev
type rest
; do
395 stashnum
=$
(( $stashnum + 1 ))
396 [ "$type" != "missing" ] ||
continue
397 IFS
="$tab" read -r cmmttr msg
<<-~EOT~
405 obj="$(git rev-parse --verify --quiet --short "$newrev" --)"
407 [ "$type" = "tag" ] || [ -n "$notype" ] ||
408 extra="$hashcolor($metacolor$type$resetcolor$hashcolor)$resetcolor "
409 if [ -z "$reflogmsg" ] || [ -z "$msg" ]; then
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"
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
439 [ -z "$signed" ] ||
[ "$reftype" = "tag" ] || die
"signed tags must be under refs/tags"
440 [ $# -gt 0 ] ||
set -- $defbranch
442 if [ $# -eq 1 ] && [ "$1" = "--all" ]; then
443 eval set -- $
(git for-each-ref
--shell --format="%(refname)" "refs/$topbases")
446 if [ $# -eq 0 ]; then
447 if [ "$quiet" -gt 0 ] && [ -n "$noneok" ]; then
450 die
"no TopGit branches found"
454 ensure_ident_available
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#?}"
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"
480 case " $newlist " in *" $ref "*);;*)
481 newlist
="${newlist:+$newlist }$ref"
483 if [ "$typ" = "tag" ]; then
484 [ "$quiet" -ge 2 ] || warn
"storing as lightweight tag instead of 'tag' object: $ref"
485 ignore
="${ignore:+$ignore }$ref"
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 ||
499 sfn
="$(git rev-parse --revs-only --symbolic-full-name "$b" -- 2>/dev/null)" ||
:
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"
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"
519 case " $branches " in *" ${sfn#refs/$topbases/} "*);;*)
520 branches
="${branches:+$branches }${sfn#refs/$topbases/}"
523 if [ -n "$tgish" ]; then
524 case " $allrefs " in *" refs/heads/${sfn#refs/$topbases/} "*);;*)
525 allrefs
="${allrefs:+$allrefs }refs/heads/${sfn#refs/$topbases/}"
527 case " $tgbranches " in *" ${sfn#refs/$topbases/} "*);;*)
528 tgbranches
="${tgbranches:+$tgbranches }${sfn#refs/$topbases/}"
531 [ -z "$added" ] || tgcount
=$
(( $tgcount + 1 ))
533 [ -z "$added" ] || othercount
=$
(( $othercount + 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"
545 case " $branches " in *" ${sfn#refs/heads/} "*);;*)
546 branches
="${branches:+$branches }${sfn#refs/heads/}"
549 if [ -n "$tgish" ]; then
550 case " $allrefs " in *" refs/$topbases/${sfn#refs/heads/} "*);;*)
551 allrefs
="${allrefs:+$allrefs }refs/$topbases/${sfn#refs/heads/}"
553 case " $tgbranches " in *" ${sfn#refs/heads/} "*);;*)
554 tgbranches
="${tgbranches:+$tgbranches }${sfn#refs/heads/}"
557 [ -z "$added" ] || tgcount
=$
(( $tgcount + 1 ))
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"
567 case " $branches " in *" ${sfn#refs/} "*);;*)
568 branches
="${branches:+$branches }${sfn#refs/}"
569 othercount
=$
(( $othercount + 1 ))
576 ! git rev-parse
--verify --quiet "$refname" -- >/dev
/null ||
577 die
"$reftype '$tagname' already exists"
581 if [ $othercount -gt 0 ]; then
582 if [ $tgcount -eq 0 ]; then
586 descpl
="$descpl and refs"
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"
600 recurse_deps_exclude
=
602 case " $recurse_deps_exclude " in *" $_b "*) continue; esac
604 _dep
="$_b"; _dep_is_tgish
=1; get_dep
605 recurse_deps get_dep
"$_b"
606 recurse_deps_exclude
="$recurse_deps_exclude $seen_deps"
612 get_deps_internal
"$@" |
sort -u
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"
624 $(get_deps $tgbranches)
628 for b
in $tgbranches; do
629 if ! needs_update
"$b" >/dev
/null
; then
631 echo "branch not up-to-date: $b"
635 [ -z "$out_of_date" ] || die
"all branches to be tagged must be up-to-date"
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
655 if [ -n "$msgfile" ]; then
656 if [ "$msgfile" = "-" ]; then
657 git stripspace
>"$git_dir/TAG_EDITMSG"
659 git stripspace
<"$msgfile" >"$git_dir/TAG_EDITMSG"
661 elif [ -n "$msg" ]; then
662 printf '%s\n' "$msg" | git stripspace
>"$git_dir/TAG_EDITMSG"
666 if [ ${#branches} -le 60 ]; then
667 printf '%s\n' "tag $descpl $branches"
668 printf '%s\n' "$updmsg"
670 printf '%s\n' "tag $(( $(printf '%s' "$branches" | wc -w) )) $descpl" ""
671 for b
in $branches; do
677 printf '%s\n' "tag $desc $branches"
679 esac | git stripspace
>"$git_dir/TAG_EDITMSG"
680 if [ -z "$noedit" ]; then
684 # Please enter a message for tg tag:
686 # Lines starting with '#' will be ignored.
688 # $descpl to be tagged:
691 for b
in $branches; do
694 } >>"$git_dir/TAG_EDITMSG"
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 ))'; }
709 case "$allrefs${extrarefs:+ $extrarefs}" in
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"
724 if [ -n "$firstprnt" ]; then
725 oldparents
="$parents"
727 for acmt
in $oldparents; do
728 [ "$acmt" = "$firstprnt" ] || parents
="$parents $acmt"
732 v_count_args pcnt
$parents
733 if [ $pcnt -eq 1 ]; then
736 [ "$(git rev-parse --quiet --verify "$tagtarget^
{tree
}" --)" = "$treeish" ] ||
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"
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"
759 printf '%s\n' "object
$obj" "type $typ" "tag
$tagname" \
761 cat "$git_dir/TGTAG_FINALMSG
"
762 } | git mktag)" || die
"git mktag failed"
763 old
="$(git rev-parse --verify --short --quiet "$refname" --)" ||
:
767 if [ ${#branches} -le 100 ]; then
768 updmsg
="$(printf '%s\n' "tgtag
: $branches")"
770 updmsg
="$(printf '%s\n' "tgtag
: $
(( $
(printf '%s' "$branches" |
wc -w) )) ${descpl#tg }")"
774 updmsg
="$(printf '%s\n' "tgtag
: $branches")"
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"