3 # Copyright (C) 2015 Kyle J. McKay <mackyle@gmail.com>
7 lf
="$(printf '\n.')" && lf
="${lf%?}"
8 tab
="$(printf '\t.')" && tab
="${tab%?}"
9 USAGE
="Usage: ${tgname:-tg} [...] tag [-s | -u <key-id>] [-f] [-q] [--no-edit] [-m <msg> | -F <file>] (<tagname> | --refs) [<branch>...]"
10 USAGE
="$USAGE$lf Or: ${tgname:-tg} [...] tag (-g | --reflog) [--reflog-message | --commit-message] [--no-type] [-n <number> | -number] [<tagname>]"
11 USAGE
="$USAGE$lf Or: ${tgname:-tg} [...] tag (--clear | --delete) <tagname>"
12 USAGE
="$USAGE$lf Or: ${tgname:-tg} [...] tag --drop <tagname>@{n}"
16 if [ "${1:-0}" != 0 ]; then
17 printf '%s\n' "$USAGE" >&2
19 printf '%s\n' "$USAGE"
51 [ -n "$1" ] ||
return 1
63 while [ $# -gt 0 ]; do case "$1" in
82 -g|
--reflog|
--walk-reflogs)
89 --no-reflog-message|
--commit-message)
99 -u|
--local-user|
--local-user=*)
100 case "$1" in --local-user=*)
103 set -- --local-user "${x#--local-user=}" "$@"
105 if [ $# -lt 2 ]; then
106 echo "The $1 option requires an argument" >&2
130 -m|
--message|
--message=*)
131 case "$1" in --message=*)
134 set -- --message "${x#--message=}" "$@"
136 if [ $# -lt 2 ]; then
137 echo "The $1 option requires an argument" >&2
144 case "$1" in --file=*)
147 set -- --file "${x#--file=}" "$@"
149 if [ $# -lt 2 ]; then
150 echo "The $1 option requires an argument" >&2
156 -n|
--max-count|
--max-count=*|
-[1-9]*)
157 case "$1" in --max-count=*)
160 set -- --max-count "${x#--max-count=}" "$@"
162 case "$1" in -[1-9]*)
167 if [ $# -lt 2 ]; then
168 echo "The $1 option requires an argument" >&2
181 --stash|
--stash"@{"*"}")
182 if [ -n "$reflog" ]; then
183 case "$2" in -[1-9]*)
188 set -- "$x2" "$x1" "$@"
197 echo "Unknown option: $1" >&2
201 if [ -n "$reflog" ]; then
202 case "$2" in -[1-9]*)
207 set -- "$x2" "$x1" "$@"
215 [ -z "$stash" -o -n "$reflog$drop$clear$delete" ] ||
{ outofdateok
=1; force
=1; defnoedit
=1; }
216 [ -n "$noedit" ] || noedit
="$defnoedit"
217 [ "$noedit" != "0" ] || noedit
=
218 [ -z "$reflog" -o -z "$drop$clear$delete$signed$keyid$force$msg$msgfile$noedit$refsonly$outofdateok" ] || usage
1
219 [ -n "$reflog" -o -z "$setreflogmsg$notype$maxcount" ] || usage
1
220 [ -z "$drop$clear$delete" -o -z "$setreflogmsg$notype$maxcount$signed$keyid$force$msg$msgfile$noedit$refsonly$outofdateok" ] || usage
1
221 [ -z "$reflog$drop$clear$delete" -o "$reflog$drop$clear$delete" = "1" ] || usage
1
222 [ -z "$maxcount" ] || is_numeric
"$maxcount" || die
"invalid count: $maxcount"
223 [ -z "$maxcount" ] ||
[ $maxcount -gt 0 ] || die
"invalid count: $maxcount"
224 [ -z "$msg" -o -z "$msgfile" ] || die
"only one -F or -m option is allowed."
225 [ -z "$refsonly" ] ||
set -- refs..only
"$@"
226 [ $# -gt 0 -o -z "$reflog" ] ||
set -- --stash
227 [ -n "$1" ] ||
{ echo "Tag name required" >&2; usage
1; }
230 [ "$tagname" != "--stash" ] || tagname
=refs
/tgstash
231 case "$tagname" in --stash"@{"*"}")
232 strip
="${tagname#--stash??}"
234 tagname
="refs/tgstash@{$strip}"
238 case "$refname" in [!@
]*"@{"*"}")
240 refname
="${refname%@*}"
241 sfx
="${sfx#$refname}"
243 case "$refname" in HEAD|refs
/*);;*)
244 if reftest
="$(git rev-parse --revs-only --symbolic-full-name "$refname" -- 2>/dev/null)" &&
245 [ -n "$reftest" ]; then
246 if [ -n "$reflog$drop$clear$delete" ]; then
250 refs
/tags
/*|refs
/tgstash
)
254 refname
="refs/tags/$refname"
258 refname
="refs/tags/$refname"
261 refname
="$refname$sfx"
263 case "$refname" in refs
/tags
/*) tagname
="${refname#refs/tags/}";; *) reftype
=ref
; tagname
="$refname"; esac
264 [ -z "$reflog$drop$clear$delete" -o $# -eq 0 ] || usage
1
265 if [ -n "$drop$clear$delete" ]; then
266 if [ -n "$sfx" ]; then
267 [ -z "$clear$delete" ] || die
"invalid ref name ($sfx suffix not allowed): $refname"
269 [ -z "$drop" ] || die
"invalid reflog name (@{n} suffix required): $refname"
271 old
="$(git rev-parse --verify --quiet --short "${refname%$sfx}" --)" || die
"no such ref: ${refname%$sfx}"
272 if [ -n "$delete" ]; then
273 git update-ref
-d "$refname" || die
"git update-ref -d failed"
274 printf "Deleted $reftype '%s' (was %s)\n" "$tagname" "$old"
276 elif [ -n "$clear" ]; then
277 [ -f "$git_dir/logs/$refname" ] || die
"no reflog found for: $refname"
278 [ -s "$git_dir/logs/$refname" ] || die
"empty reflog found for: $refname"
279 cp -p "$git_dir/logs/$refname" "$git_dir/logs/$refname^-+" || die
"cp failed"
280 sed -n '$p' <"$git_dir/logs/$refname^-+" >"$git_dir/logs/$refname" || die
"reflog clear failed"
281 rm -f "$git_dir/logs/$refname^-+"
282 printf "Cleared $reftype '%s' reflog to single @{0} entry\n" "$tagname"
285 old
="$(git rev-parse --verify --short "$refname" --)" ||
exit 1
286 git reflog delete
--rewrite --updateref "$refname" || die
"reflog drop failed"
287 printf "Dropped $reftype '%s' reflog entry (was %s)\n" "$tagname" "$old"
291 if [ -n "$reflog" ]; then
292 [ "$refname" = "refs/tgstash" -o -n "$setreflogmsg" ] || reflogmsg
=1
293 git rev-parse
--verify --quiet "$refname" -- >/dev
/null ||
294 die
"no such ref: $refname"
295 [ -s "$git_dir/logs/$refname" ] ||
296 die
"no reflog present for $reftype: $tagname"
297 showref
="$(git rev-parse --revs-only --abbrev-ref=strict "$refname" --)"
300 if git config
--get-colorbool color.tgtag
; then
301 metacolor
="$(git config --get-color color.tgtag.meta)"
302 [ -n "$metacolor" ] || metacolor
="$(git config --get-color color.diff.meta "bold
")"
303 hashcolor
="$(git config --get-color color.tgtag.commit)"
304 [ -n "$hashcolor" ] || hashcolor
="$(git config --get-color color.diff.commit "yellow
")"
305 datecolor
="$(git config --get-color color.tgtag.date "bold blue
")"
306 timecolor
="$(git config --get-color color.tgtag.time "green
")"
307 resetcolor
="$(git config --get-color "" reset)"
312 sed 's/[^ ][^ ]* //' <"$git_dir/logs/$refname" |
313 awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--]}' |
314 git cat-file
--batch-check='%(objectname) %(objecttype) %(rest)' |
318 while read -r newrev
type rest
; do
319 stashnum
=$
(( $stashnum + 1 ))
320 [ "$type" != "missing" ] ||
continue
321 IFS
="$tab" read -r cmmttr msg
<<-~EOT~
329 obj="$(git rev-parse --verify --quiet --short "$newrev" --)"
331 [ "$type" = "tag" -o -n "$notype" ] ||
332 extra="$hashcolor($metacolor$type$resetcolor$hashcolor)$resetcolor "
333 if [ -z "$reflogmsg" -o -z "$msg" ]; then
335 if [ "$type" = "tag" ]; then
336 objmsg="$(git cat-file tag "$obj" |
337 sed '1,/^$/d' | sed '/^$/,$d')"
338 elif [ "$type" = "commit" ]; then
339 objmsg="$(git log -n 1 --format='format:%s' "$obj" --)"
341 [ -z "$objmsg" ] || msg="$objmsg"
343 read newdate newtime <<-EOT
344 $(strftime "%Y-%m-%d %H:%M:%S" "$es")
346 if [ "$lastdate" != "$newdate" ]; then
347 printf '%s=== %s ===%s\n' "$datecolor" "$newdate" "$resetcolor"
350 printf '%s %s %s%s@{%s}: %s\n' "$hashcolor$obj$reseutcolor" \
351 "$timecolor$newtime$resetcolor" \
352 "$extra" "$showref" "$stashnum" "$msg"
353 if [ -n "$maxcount" ]; then
354 maxcount=$(( $maxcount - 1 ))
355 [ $maxcount -gt 0 ] ||
break
363 [ -z "$signed" -o "$reftype" = "tag" ] || die
"signed tags must be under refs/tags"
364 [ $# -gt 0 ] ||
set -- $defbranch
366 if [ $# -eq 1 ] && [ "$1" = "--all" ]; then
367 eval set -- $
(git for-each-ref
--shell --format="%(refname)" "refs/$topbases")
370 if [ $# -eq 0 ]; then
371 if [ -n "$quiet" -a -n "$noneok" ]; then
374 die
"no TopGit branches found"
378 ensure_ident_available
387 while read -r obj typ ref
&& [ -n "$obj" -a -n "$typ" ]; do
388 [ -n "$ref" -o "$typ" != "missing" ] || die
"no such ref: ${obj%???}"
389 case " $ignore " in *" $ref "*) continue; esac
390 if [ "$typ" != "commit" -a "$typ" != "tag" ]; then
391 [ -n "$anyrefok" ] || die
"not a committish (is a '$typ') ref: $ref"
392 warn
"ignoring non-committish (is a '$typ') ref: $ref"
393 ignore
="${ignore:+$ignore }$ref"
396 case " $newlist " in *" $ref "*);;*)
397 newlist
="${newlist:+$newlist }$ref"
399 if [ "$typ" = "tag" ]; then
400 warn
"storing as lightweight tag instead of 'tag' object: $ref"
401 ignore
="${ignore:+$ignore }$ref"
405 printf '%s\n' "$@" | sed 's/^\(.*\)$/\1^{} \1/'
406 printf '%s\n' "$@" | sed 's/^\(.*\)$/\1 \1/'
408 git cat-file --batch-check='%(objectname) %(objecttype) %(rest)' 2>/dev/null ||
415 sfn
="$(git rev-parse --revs-only --symbolic-full-name "$b" -- 2>/dev/null)" ||
:
417 [ -n "$anyrefok" ] || die
"no such symbolic ref name: $b"
418 fullhash
="$(git rev-parse --verify --quiet "$b" --)" || die
"no such ref: $b"
419 case " $extrarefs " in *" $b "*);;*)
420 warn
"including non-symbolic ref only in parents calculation: $b"
421 extrarefs
="${extrarefs:+$extrarefs }$fullhash"
429 ref_exists
"refs/heads/${sfn#refs/$topbases/}" || tgish
=
430 [ -n "$anyrefok" ] ||
[ -n "$tgish" ] ||
431 warn
"including TopGit base that's missing its head: $sfn"
432 case " $allrefs " in *" $sfn "*);;*)
433 allrefs
="${allrefs:+$allrefs }$sfn"
435 case " $branches " in *" ${sfn#refs/$topbases/} "*);;*)
436 branches
="${branches:+$branches }${sfn#refs/$topbases/}"
439 if [ -n "$tgish" ]; then
440 case " $allrefs " in *" refs/heads/${sfn#refs/$topbases/} "*);;*)
441 allrefs
="${allrefs:+$allrefs }refs/heads/${sfn#refs/$topbases/}"
443 case " $tgbranches " in *" ${sfn#refs/$topbases/} "*);;*)
444 tgbranches
="${tgbranches:+$tgbranches }${sfn#refs/$topbases/}"
447 [ -z "$added" ] || tgcount
=$
(( $tgcount + 1 ))
449 [ -z "$added" ] || othercount
=$
(( $othercount + 1 ))
455 ref_exists
"refs/$topbases/${sfn#refs/heads/}" || tgish
=
456 [ -n "$anyrefok" ] ||
[ -n "$tgish" ] ||
457 die
"not a TopGit branch: ${sfn#refs/heads/} (use --allow-any option)"
458 case " $allrefs " in *" $b "*);;*)
459 allrefs
="${allrefs:+$allrefs }$sfn"
461 case " $branches " in *" ${sfn#refs/heads/} "*);;*)
462 branches
="${branches:+$branches }${sfn#refs/heads/}"
465 if [ -n "$tgish" ]; then
466 case " $allrefs " in *" refs/$topbases/${sfn#refs/heads/} "*);;*)
467 allrefs
="${allrefs:+$allrefs }refs/$topbases/${sfn#refs/heads/}"
469 case " $tgbranches " in *" ${sfn#refs/heads/} "*);;*)
470 tgbranches
="${tgbranches:+$tgbranches }${sfn#refs/heads/}"
473 [ -z "$added" ] || tgcount
=$
(( $tgcount + 1 ))
475 [ -z "$added" ] || othercount
=$
(( $othercount + 1 ))
479 [ -n "$anyrefok" ] || die
"refusing to include without --allow-any: $sfn"
480 case " $allrefs " in *" $sfn "*);;*)
481 allrefs
="${allrefs:+$allrefs }$sfn"
483 case " $branches " in *" ${sfn#refs/} "*);;*)
484 branches
="${branches:+$branches }${sfn#refs/}"
485 othercount
=$
(( $othercount + 1 ))
492 ! git rev-parse
--verify --quiet "$refname" -- >/dev
/null ||
493 die
"$reftype '$tagname' already exists"
497 if [ $othercount -gt 0 ]; then
498 if [ $tgcount -eq 0 ]; then
502 descpl
="$descpl and refs"
507 case " $seen_deps " in *" $_dep "*) return 0; esac
508 seen_deps
="${seen_deps:+$seen_deps }$_dep"
509 printf 'refs/heads/%s\n' "$_dep"
510 [ -z "$_dep_is_tgish" ] ||
printf 'refs/%s/%s\n' "$topbases" "$_dep"
516 recurse_deps_exclude
=
518 case " $recurse_deps_exclude " in *" $_b "*) continue; esac
520 _dep
="$_b"; _dep_is_tgish
=1; get_dep
521 recurse_deps get_dep
"$_b"
522 recurse_deps_exclude
="$recurse_deps_exclude $seen_deps"
528 get_deps_internal
"$@" | LC_ALL
=C
sort -u
532 if [ -n "$outofdateok" ]; then
533 if [ -n "$tgbranches" ]; then
534 while read -r dep
&& [ -n "$dep" ]; do
535 case " $allrefs " in *" $dep "*);;*)
536 ! ref_exists
"$dep" ||
537 allrefs
="${allrefs:+$allrefs }$dep"
540 $(get_deps $tgbranches)
544 for b
in $tgbranches; do
545 if ! needs_update
"$b" >/dev
/null
; then
547 echo "branch not up-to-date: $b"
551 [ -z "$out_of_date" ] || die
"all branches to be tagged must be up-to-date"
555 printf '%s\n' '-----BEGIN TOPGIT REFS-----'
557 printf '%s\n' $allrefs
558 [ -n "$outofdateok" ] || get_deps
$tgbranches
559 } | LC_ALL
=C
sort -u |
sed 's/^\(.*\)$/\1^0 \1/' |
560 git cat-file
--batch-check='%(objectname) %(rest)' 2>/dev
/null |
561 grep -v ' missing$' ||
:
562 printf '%s\n' '-----END TOPGIT REFS-----'
565 if [ -n "$refsonly" ]; then
571 if [ -n "$msgfile" ]; then
572 if [ "$msgfile" = "-" ]; then
573 git stripspace
>"$git_dir/TAG_EDITMSG"
575 git stripspace
<"$msgfile" >"$git_dir/TAG_EDITMSG"
577 elif [ -n "$msg" ]; then
578 printf '%s\n' "$msg" | git stripspace
>"$git_dir/TAG_EDITMSG"
582 if [ ${#branches} -le 60 ]; then
583 printf '%s\n' "tag $descpl $branches"
584 printf '%s\n' "$updmsg"
586 printf '%s\n' "tag $(( $(printf '%s' "$branches" | wc -w) )) $descpl" ""
587 for b
in $branches; do
593 printf '%s\n' "tag $desc $branches"
595 esac | git stripspace
>"$git_dir/TAG_EDITMSG"
596 if [ -z "$noedit" ]; then
600 # Please enter a message for tg tag:
602 # Lines starting with '#' will be ignored.
604 # $descpl to be tagged:
607 for b
in $branches; do
610 } >>"$git_dir/TAG_EDITMSG"
612 run_editor
"$git_dir/TAG_EDITMSG" ||
613 die
"there was a problem with the editor '$tg_editor'"
616 git stripspace
${stripcomments:+ --strip-comments} \
617 <"$git_dir/TAG_EDITMSG" >"$git_dir/TGTAG_FINALMSG"
618 [ -s "$git_dir/TGTAG_FINALMSG" ] || die
"no tag message?"
619 echo "" >>"$git_dir/TGTAG_FINALMSG"
620 get_refs
>>"$git_dir/TGTAG_FINALMSG"
623 case "$allrefs${extrarefs:+ $extrarefs}" in
625 parents
="$(git merge-base --independent \
626 $(printf '%s^0 ' $allrefs $extrarefs))" ||
627 die
"failed: git merge-base --independent"
628 if [ $
(printf '%s\n' "$parents" |
wc -l) -eq 1 ]; then
631 mttree
="$(git hash-object -t tree -w --stdin </dev/null)"
632 tagtarget
="$(printf '%s\n' "tg tag branch consolidation
" "" $branches |
633 git commit-tree $mttree $(printf -- '-p %s ' $parents))"
637 tagtarget
="$allrefs^0"
641 init_reflog
"$refname"
642 if [ "$reftype" = "tag" -a -n "$signed" ]; then
643 [ -z "$quiet" ] ||
exec >/dev
/null
644 git tag
-F "$git_dir/TGTAG_FINALMSG" ${signed:+-s} ${force:+-f} \
645 ${keyid:+-u} ${keyid} "$tagname" "$tagtarget"
647 obj
="$(git rev-parse --verify --quiet "$tagtarget" --)" ||
648 die
"invalid object name: $tagtarget"
649 typ
="$(git cat-file -t "$tagtarget" 2>/dev/null)" ||
650 die
"invalid object name: $tagtarget"
651 id
="$(git var GIT_COMMITTER_IDENT 2>/dev/null)" ||
652 die
"could not get GIT_COMMITTER_IDENT"
654 printf '%s\n' "object
$obj" "type $typ" "tag
$tagname" \
656 cat "$git_dir/TGTAG_FINALMSG
"
657 } | git mktag)" || die
"git mktag failed"
658 old
="$(git rev-parse --verify --short --quiet "$refname" --)" ||
:
662 if [ ${#branches} -le 100 ]; then
663 updmsg
="$(printf '%s\n' "tgtag
: $branches")"
665 updmsg
="$(printf '%s\n' "tgtag
: $
(( $
(printf '%s' "$branches" |
wc -w) )) ${descpl#tg }")"
669 updmsg
="$(printf '%s\n' "tgtag
: $branches")"
672 git update-ref
-m "$updmsg" "$refname" "$newtag"
673 [ -z "$old" -o -n "$quiet" ] ||
printf "Updated $reftype '%s' (was %s)\n" "$tagname" "$old"
675 rm -f "$git_dir/TAG_EDITMSG" "$git_dir/TGTAG_FINALMSG"