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 [ "$tagname" != "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 set -- $
(git for-each-ref
--format="%(refname)" refs
/top-bases
)
370 if [ $# -eq 0 ]; then
371 if [ -n "$quiet" -a -n "$noneok" ]; then
374 die
"no TopGit branches found"
386 while read -r obj typ ref
&& [ -n "$obj" -a -n "$typ" ]; do
387 [ -n "$ref" -o "$typ" != "missing" ] || die
"no such ref: ${obj%???}"
388 case " $ignore " in *" $ref "*) continue; esac
389 if [ "$typ" != "commit" -a "$typ" != "tag" ]; then
390 [ -n "$anyrefok" ] || die
"not a committish (is a '$typ') ref: $ref"
391 warn
"ignoring non-committish (is a '$typ') ref: $ref"
392 ignore
="${ignore:+$ignore }$ref"
395 case " $newlist " in *" $ref "*) :;; *)
396 newlist
="${newlist:+$newlist }$ref"
398 if [ "$typ" = "tag" ]; then
399 warn
"storing as lightweight tag instead of 'tag' object: $ref"
400 ignore
="${ignore:+$ignore }$ref"
404 printf '%s\n' "$@" | sed 's/^\(.*\)$/\1^{} \1/'
405 printf '%s\n' "$@" | sed 's/^\(.*\)$/\1 \1/'
407 git cat-file --batch-check='%(objectname) %(objecttype) %(rest)' 2>/dev/null ||
414 sfn
="$(git rev-parse --revs-only --symbolic-full-name "$b" -- 2>/dev/null || :)"
416 [ -n "$anyrefok" ] || die
"no such symbolic ref name: $b"
417 fullhash
="$(git rev-parse --verify --quiet "$b" --)" || die
"no such ref: $b"
418 case " $extrarefs " in *" $b "*) :;; *)
419 warn
"including non-symbolic ref only in parents calculation: $b"
420 extrarefs
="${extrarefs:+$extrarefs }$fullhash"
428 ref_exists
"refs/top-bases/${sfn#refs/heads/}" || tgish
=
429 [ -n "$anyrefok" ] ||
[ -n "$tgish" ] ||
430 die
"not a TopGit branch: ${sfn#refs/heads/} (use --allow-any option)"
431 case " $allrefs " in *" $b "*) :;; *)
432 allrefs
="${allrefs:+$allrefs }$sfn"
434 case " $branches " in *" ${sfn#refs/heads/} "*) :;; *)
435 branches
="${branches:+$branches }${sfn#refs/heads/}"
438 if [ -n "$tgish" ]; then
439 case " $allrefs " in *" refs/top-bases/${sfn#refs/heads/} "*) :;; *)
440 allrefs
="${allrefs:+$allrefs }refs/top-bases/${sfn#refs/heads/}"
442 case " $tgbranches " in *" ${sfn#refs/heads/} "*) :;; *)
443 tgbranches
="${tgbranches:+$tgbranches }${sfn#refs/heads/}"
446 [ -z "$added" ] || tgcount
=$
(( $tgcount + 1 ))
448 [ -z "$added" ] || othercount
=$
(( $othercount + 1 ))
454 ref_exists
"refs/heads/${sfn#refs/top-bases/}" || tgish
=
455 [ -n "$anyrefok" ] ||
[ -n "$tgish" ] ||
456 warn
"including TopGit base that's missing its head: $sfn"
457 case " $allrefs " in *" $sfn "*) :;; *)
458 allrefs
="${allrefs:+$allrefs }$sfn"
460 case " $branches " in *" ${sfn#refs/top-bases/} "*) :;; *)
461 branches
="${branches:+$branches }${sfn#refs/top-bases/}"
464 if [ -n "$tgish" ]; then
465 case " $allrefs " in *" refs/heads/${sfn#refs/top-bases/} "*) :;; *)
466 allrefs
="${allrefs:+$allrefs }refs/heads/${sfn#refs/top-bases/}"
468 case " $tgbranches " in *" ${sfn#refs/top-bases/} "*) :;; *)
469 tgbranches
="${tgbranches:+$tgbranches }${sfn#refs/top-bases/}"
472 [ -z "$added" ] || tgcount
=$
(( $tgcount + 1 ))
474 [ -z "$added" ] || othercount
=$
(( $othercount + 1 ))
478 [ -n "$anyrefok" ] || die
"refusing to include without --allow-any: $sfn"
479 case " $allrefs " in *" $sfn "*) :;; *)
480 allrefs
="${allrefs:+$allrefs }$sfn"
482 case " $branches " in *" ${sfn#refs/} "*) :;; *)
483 branches
="${branches:+$branches }${sfn#refs/}"
484 othercount
=$
(( $othercount + 1 ))
491 ! git rev-parse
--verify --quiet "$refname" -- >/dev
/null ||
492 die
"$reftype '$tagname' already exists"
496 if [ $othercount -gt 0 ]; then
497 if [ $tgcount -eq 0 ]; then
501 descpl
="$descpl and refs"
506 case " $seen_deps " in *" $_dep "*) return 0; esac
507 seen_deps
="${seen_deps:+$seen_deps }$_dep"
508 printf 'refs/heads/%s\n' "$_dep"
509 [ -z "$_dep_is_tgish" ] ||
printf 'refs/top-bases/%s\n' "$_dep"
515 recurse_deps_exclude
=
517 case " $recurse_deps_exclude " in *" $_b "*) continue; esac
519 _dep
="$_b"; _dep_is_tgish
=1; get_dep
520 recurse_deps get_dep
"$_b"
521 recurse_deps_exclude
="$recurse_deps_exclude $seen_deps"
527 get_deps_internal
"$@" | LC_ALL
=C
sort -u
531 if [ -n "$outofdateok" ]; then
532 if [ -n "$tgbranches" ]; then
533 while read -r dep
&& [ -n "$dep" ]; do
534 case " $allrefs " in *" $dep "*) :;; *)
535 ! ref_exists
"$dep" ||
536 allrefs
="${allrefs:+$allrefs }$dep"
539 $(get_deps $tgbranches)
543 for b
in $tgbranches; do
544 if ! needs_update
"$b" >/dev
/null
; then
546 echo "branch not up-to-date: $b"
550 [ -z "$out_of_date" ] || die
"all branches to be tagged must be up-to-date"
554 printf '%s\n' '-----BEGIN TOPGIT REFS-----'
556 printf '%s\n' $allrefs
557 [ -n "$outofdateok" ] || get_deps
$tgbranches
558 } | LC_ALL
=C
sort -u |
sed 's/^\(.*\)$/\1^0 \1/' |
559 git cat-file
--batch-check='%(objectname) %(rest)' 2>/dev
/null |
560 grep -v ' missing$' ||
:
561 printf '%s\n' '-----END TOPGIT REFS-----'
564 if [ -n "$refsonly" ]; then
570 if [ -n "$msgfile" ]; then
571 if [ "$msgfile" = "-" ]; then
572 git stripspace
>"$git_dir/TAG_EDITMSG"
574 git stripspace
<"$msgfile" >"$git_dir/TAG_EDITMSG"
576 elif [ -n "$msg" ]; then
577 printf '%s\n' "$msg" | git stripspace
>"$git_dir/TAG_EDITMSG"
581 if [ ${#branches} -le 60 ]; then
582 printf '%s\n' "tag $descpl $branches"
583 printf '%s\n' "$updmsg"
585 printf '%s\n' "tag $(( $(printf '%s' "$branches" | wc -w) )) $descpl" ""
586 for b
in $branches; do
592 printf '%s\n' "tag $desc $branches"
594 esac | git stripspace
>"$git_dir/TAG_EDITMSG"
595 if [ -z "$noedit" ]; then
599 # Please enter a message for tg tag:
601 # Lines starting with '#' will be ignored.
603 # $descpl to be tagged:
606 for b
in $branches; do
609 } >>"$git_dir/TAG_EDITMSG"
611 run_editor
"$git_dir/TAG_EDITMSG" || \
612 die
"there was a problem with the editor '$tg_editor'"
615 git stripspace
${stripcomments:+ --strip-comments} \
616 <"$git_dir/TAG_EDITMSG" >"$git_dir/TGTAG_FINALMSG"
617 [ -s "$git_dir/TGTAG_FINALMSG" ] || die
"no tag message?"
618 echo "" >>"$git_dir/TGTAG_FINALMSG"
619 get_refs
>>"$git_dir/TGTAG_FINALMSG"
622 case "$allrefs${extrarefs:+ $extrarefs}" in
624 parents
="$(git merge-base --independent \
625 $(printf '%s^0 ' $allrefs $extrarefs))" || \
626 die
"failed: git merge-base --independent"
627 if [ $
(printf '%s\n' "$parents" |
wc -l) -eq 1 ]; then
630 mttree
="$(git hash-object -t tree -w --stdin </dev/null)"
631 tagtarget
="$(printf '%s\n' "tg tag branch consolidation
" "" $branches | \
632 git commit-tree $mttree $(printf -- '-p %s ' $parents))"
636 tagtarget
="$allrefs^0"
640 if [ -n "$logrefupdates" -o "$refname" = "refs/tgstash" ]; then
641 mkdir
-p "$git_dir/logs/$(dirname "$refname")" 2>/dev
/null ||
:
642 { >>"$git_dir/logs/$refname" ||
:; } 2>/dev
/null
644 if [ "$reftype" = "tag" -a -n "$signed" ]; then
645 [ -z "$quiet" ] ||
exec >/dev
/null
646 git tag
-F "$git_dir/TGTAG_FINALMSG" ${signed:+-s} ${force:+-f} \
647 ${keyid:+-u} ${keyid} "$tagname" "$tagtarget"
649 obj
="$(git rev-parse --verify --quiet "$tagtarget" --)" || \
650 die
"invalid object name: $tagtarget"
651 typ
="$(git cat-file -t "$tagtarget" 2>/dev/null)" || \
652 die
"invalid object name: $tagtarget"
653 id
="$(git var GIT_COMMITTER_IDENT 2>/dev/null)" || \
654 die
"could not get GIT_COMMITTER_IDENT"
656 printf '%s\n' "object
$obj" "type $typ" "tag
$tagname" \
658 cat "$git_dir/TGTAG_FINALMSG
"
659 } | git mktag)" || die
"git mktag failed"
660 old
="$(git rev-parse --verify --short --quiet "$refname" -- || :)"
664 if [ ${#branches} -le 100 ]; then
665 updmsg
="$(printf '%s\n' "tgtag
: $branches")"
667 updmsg
="$(printf '%s\n' "tgtag
: $
(( $
(printf '%s' "$branches" |
wc -w) )) ${descpl#tg }")"
671 updmsg
="$(printf '%s\n' "tgtag
: $branches")"
674 git update-ref
-m "$updmsg" "$refname" "$newtag"
675 [ -z "$old" -o -n "$quiet" ] ||
printf "Updated $reftype '%s' (was %s)\n" "$tagname" "$old"
677 rm -f "$git_dir/TAG_EDITMSG" "$git_dir/TGTAG_FINALMSG"