4 * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>,
5 * Carlos Rica <jasampler@gmail.com>
6 * Based on git-tag.sh and mktag.c by Linus Torvalds.
13 #include "environment.h"
17 #include "object-name.h"
18 #include "object-store-ll.h"
21 #include "parse-options.h"
24 #include "gpg-interface.h"
25 #include "oid-array.h"
27 #include "ref-filter.h"
29 #include "write-or-die.h"
30 #include "object-file-convert.h"
33 static const char * const git_tag_usage
[] = {
34 N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] [-e]\n"
35 " [(--trailer <token>[(=|:)<value>])...]\n"
36 " <tagname> [<commit> | <object>]"),
37 N_("git tag -d <tagname>..."),
38 N_("git tag [-n[<num>]] -l [--contains <commit>] [--no-contains <commit>]\n"
39 " [--points-at <object>] [--column[=<options>] | --no-column]\n"
40 " [--create-reflog] [--sort=<key>] [--format=<format>]\n"
41 " [--merged <commit>] [--no-merged <commit>] [<pattern>...]"),
42 N_("git tag -v [--format=<format>] <tagname>..."),
46 static unsigned int colopts
;
47 static int force_sign_annotate
;
48 static int config_sign_tag
= -1; /* unspecified */
50 static int list_tags(struct ref_filter
*filter
, struct ref_sorting
*sorting
,
51 struct ref_format
*format
)
55 if (filter
->lines
== -1)
58 if (!format
->format
) {
60 to_free
= xstrfmt("%s %%(contents:lines=%d)",
61 "%(align:15)%(refname:lstrip=2)%(end)",
63 format
->format
= to_free
;
65 format
->format
= "%(refname:lstrip=2)";
68 if (verify_ref_format(format
))
69 die(_("unable to parse format string"));
70 filter
->with_commit_tag_algo
= 1;
71 filter_and_format_refs(filter
, FILTER_REFS_TAGS
, sorting
, format
);
78 typedef int (*each_tag_name_fn
)(const char *name
, const char *ref
,
79 const struct object_id
*oid
, void *cb_data
);
81 static int for_each_tag_name(const char **argv
, each_tag_name_fn fn
,
85 struct strbuf ref
= STRBUF_INIT
;
89 for (p
= argv
; *p
; p
++) {
91 strbuf_addf(&ref
, "refs/tags/%s", *p
);
92 if (refs_read_ref(get_main_ref_store(the_repository
), ref
.buf
, &oid
)) {
93 error(_("tag '%s' not found."), *p
);
97 if (fn(*p
, ref
.buf
, &oid
, cb_data
))
100 strbuf_release(&ref
);
104 static int collect_tags(const char *name UNUSED
, const char *ref
,
105 const struct object_id
*oid
, void *cb_data
)
107 struct string_list
*ref_list
= cb_data
;
109 string_list_append(ref_list
, ref
);
110 ref_list
->items
[ref_list
->nr
- 1].util
= oiddup(oid
);
114 static int delete_tags(const char **argv
)
117 struct string_list refs_to_delete
= STRING_LIST_INIT_DUP
;
118 struct string_list_item
*item
;
120 result
= for_each_tag_name(argv
, collect_tags
, (void *)&refs_to_delete
);
121 if (refs_delete_refs(get_main_ref_store(the_repository
), NULL
, &refs_to_delete
, REF_NO_DEREF
))
124 for_each_string_list_item(item
, &refs_to_delete
) {
125 const char *name
= item
->string
;
126 struct object_id
*oid
= item
->util
;
127 if (!refs_ref_exists(get_main_ref_store(the_repository
), name
))
128 printf(_("Deleted tag '%s' (was %s)\n"),
130 repo_find_unique_abbrev(the_repository
, oid
, DEFAULT_ABBREV
));
134 string_list_clear(&refs_to_delete
, 0);
138 static int verify_tag(const char *name
, const char *ref UNUSED
,
139 const struct object_id
*oid
, void *cb_data
)
142 struct ref_format
*format
= cb_data
;
143 flags
= GPG_VERIFY_VERBOSE
;
146 flags
= GPG_VERIFY_OMIT_STATUS
;
148 if (gpg_verify_tag(oid
, name
, flags
))
152 pretty_print_ref(name
, oid
, format
);
157 static int do_sign(struct strbuf
*buffer
, struct object_id
**compat_oid
,
158 struct object_id
*compat_oid_buf
)
160 const struct git_hash_algo
*compat
= the_repository
->compat_hash_algo
;
161 struct strbuf sig
= STRBUF_INIT
, compat_sig
= STRBUF_INIT
;
162 struct strbuf compat_buf
= STRBUF_INIT
;
163 const char *keyid
= get_signing_key();
166 if (sign_buffer(buffer
, &sig
, keyid
))
170 const struct git_hash_algo
*algo
= the_repository
->hash_algo
;
172 if (convert_object_file(&compat_buf
, algo
, compat
,
173 buffer
->buf
, buffer
->len
, OBJ_TAG
, 1))
175 if (sign_buffer(&compat_buf
, &compat_sig
, keyid
))
177 add_header_signature(&compat_buf
, &sig
, algo
);
178 strbuf_addbuf(&compat_buf
, &compat_sig
);
179 hash_object_file(compat
, compat_buf
.buf
, compat_buf
.len
,
180 OBJ_TAG
, compat_oid_buf
);
181 *compat_oid
= compat_oid_buf
;
185 add_header_signature(buffer
, &compat_sig
, compat
);
187 strbuf_addbuf(buffer
, &sig
);
190 strbuf_release(&sig
);
191 strbuf_release(&compat_sig
);
192 strbuf_release(&compat_buf
);
196 static const char tag_template
[] =
197 N_("\nWrite a message for tag:\n %s\n"
198 "Lines starting with '%s' will be ignored.\n");
200 static const char tag_template_nocleanup
[] =
201 N_("\nWrite a message for tag:\n %s\n"
202 "Lines starting with '%s' will be kept; you may remove them"
203 " yourself if you want to.\n");
205 static int git_tag_config(const char *var
, const char *value
,
206 const struct config_context
*ctx
, void *cb
)
208 if (!strcmp(var
, "tag.gpgsign")) {
209 config_sign_tag
= git_config_bool(var
, value
);
213 if (!strcmp(var
, "tag.sort")) {
215 return config_error_nonbool(var
);
216 string_list_append(cb
, value
);
220 if (!strcmp(var
, "tag.forcesignannotated")) {
221 force_sign_annotate
= git_config_bool(var
, value
);
225 if (starts_with(var
, "column."))
226 return git_column_config(var
, value
, "tag", &colopts
);
228 if (git_color_config(var
, value
, cb
) < 0)
231 return git_default_config(var
, value
, ctx
, cb
);
234 static void write_tag_body(int fd
, const struct object_id
*oid
)
237 enum object_type type
;
238 char *buf
, *sp
, *orig
;
239 struct strbuf payload
= STRBUF_INIT
;
240 struct strbuf signature
= STRBUF_INIT
;
242 orig
= buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
245 if (parse_signature(buf
, size
, &payload
, &signature
)) {
250 sp
= strstr(buf
, "\n\n");
252 if (!sp
|| !size
|| type
!= OBJ_TAG
) {
256 sp
+= 2; /* skip the 2 LFs */
257 write_or_die(fd
, sp
, buf
+ size
- sp
);
260 strbuf_release(&payload
);
261 strbuf_release(&signature
);
264 static int build_tag_object(struct strbuf
*buf
, int sign
, struct object_id
*result
)
266 struct object_id
*compat_oid
= NULL
, compat_oid_buf
;
267 if (sign
&& do_sign(buf
, &compat_oid
, &compat_oid_buf
) < 0)
268 return error(_("unable to sign the tag"));
269 if (write_object_file_flags(buf
->buf
, buf
->len
, OBJ_TAG
, result
,
271 return error(_("unable to write tag file"));
275 struct create_tag_options
{
276 unsigned int message_given
:1;
277 unsigned int use_editor
:1;
286 static const char message_advice_nested_tag
[] =
287 N_("You have created a nested tag. The object referred to by your new tag is\n"
288 "already a tag. If you meant to tag the object that it points to, use:\n"
290 "\tgit tag -f %s %s^{}");
292 static void create_tag(const struct object_id
*object
, const char *object_ref
,
294 struct strbuf
*buf
, struct create_tag_options
*opt
,
295 struct object_id
*prev
, struct object_id
*result
,
296 struct strvec
*trailer_args
, char *path
)
298 enum object_type type
;
299 struct strbuf header
= STRBUF_INIT
;
302 type
= oid_object_info(the_repository
, object
, NULL
);
303 if (type
<= OBJ_NONE
)
304 die(_("bad object type."));
307 advise_if_enabled(ADVICE_NESTED_TAG
, _(message_advice_nested_tag
),
318 git_committer_info(IDENT_STRICT
));
320 should_edit
= opt
->use_editor
|| !opt
->message_given
;
321 if (should_edit
|| trailer_args
->nr
) {
324 /* write the template message before editing: */
325 fd
= xopen(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
327 if (opt
->message_given
&& buf
->len
) {
328 strbuf_complete(buf
, '\n');
329 write_or_die(fd
, buf
->buf
, buf
->len
);
331 } else if (!is_null_oid(prev
)) {
332 write_tag_body(fd
, prev
);
334 struct strbuf buf
= STRBUF_INIT
;
335 strbuf_addch(&buf
, '\n');
336 if (opt
->cleanup_mode
== CLEANUP_ALL
)
337 strbuf_commented_addf(&buf
, comment_line_str
,
338 _(tag_template
), tag
, comment_line_str
);
340 strbuf_commented_addf(&buf
, comment_line_str
,
341 _(tag_template_nocleanup
), tag
, comment_line_str
);
342 write_or_die(fd
, buf
.buf
, buf
.len
);
343 strbuf_release(&buf
);
347 if (trailer_args
->nr
&& amend_file_with_trailers(path
, trailer_args
))
348 die(_("unable to pass trailers to --trailers"));
351 if (launch_editor(path
, buf
, NULL
)) {
353 _("Please supply the message using either -m or -F option.\n"));
356 } else if (trailer_args
->nr
) {
358 if (strbuf_read_file(buf
, path
, 0) < 0)
359 die_errno(_("failed to read '%s'"), path
);
363 if (opt
->cleanup_mode
!= CLEANUP_NONE
)
364 strbuf_stripspace(buf
,
365 opt
->cleanup_mode
== CLEANUP_ALL
? comment_line_str
: NULL
);
367 if (!opt
->message_given
&& !buf
->len
)
368 die(_("no tag message?"));
370 strbuf_insert(buf
, 0, header
.buf
, header
.len
);
371 strbuf_release(&header
);
373 if (build_tag_object(buf
, opt
->sign
, result
) < 0) {
375 fprintf(stderr
, _("The tag message has been left in %s\n"),
381 static void create_reflog_msg(const struct object_id
*oid
, struct strbuf
*sb
)
383 enum object_type type
;
388 const char *subject_start
;
390 char *rla
= getenv("GIT_REFLOG_ACTION");
392 strbuf_addstr(sb
, rla
);
394 strbuf_addstr(sb
, "tag: tagging ");
395 strbuf_add_unique_abbrev(sb
, oid
, DEFAULT_ABBREV
);
398 strbuf_addstr(sb
, " (");
399 type
= oid_object_info(the_repository
, oid
, NULL
);
402 strbuf_addstr(sb
, "object of unknown type");
405 if ((buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
))) {
406 subject_len
= find_commit_subject(buf
, &subject_start
);
407 strbuf_insert(sb
, sb
->len
, subject_start
, subject_len
);
409 strbuf_addstr(sb
, "commit object");
413 if ((c
= lookup_commit_reference(the_repository
, oid
)))
414 strbuf_addf(sb
, ", %s", show_date(c
->date
, 0, DATE_MODE(SHORT
)));
417 strbuf_addstr(sb
, "tree object");
420 strbuf_addstr(sb
, "blob object");
423 strbuf_addstr(sb
, "other tag object");
426 strbuf_addch(sb
, ')');
434 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
436 struct msg_arg
*msg
= opt
->value
;
438 BUG_ON_OPT_NEG(unset
);
443 strbuf_addstr(&(msg
->buf
), "\n\n");
444 strbuf_addstr(&(msg
->buf
), arg
);
449 static int strbuf_check_tag_ref(struct strbuf
*sb
, const char *name
)
455 strbuf_addf(sb
, "refs/tags/%s", name
);
457 return check_refname_format(sb
->buf
, 0);
460 int cmd_tag(int argc
, const char **argv
, const char *prefix
)
462 struct strbuf buf
= STRBUF_INIT
;
463 struct strbuf ref
= STRBUF_INIT
;
464 struct strbuf reflog_msg
= STRBUF_INIT
;
465 struct object_id object
, prev
;
466 const char *object_ref
, *tag
;
467 struct create_tag_options opt
;
468 char *cleanup_arg
= NULL
;
469 int create_reflog
= 0;
470 int annotate
= 0, force
= 0;
471 int cmdmode
= 0, create_tag_object
= 0;
472 char *msgfile
= NULL
;
473 const char *keyid
= NULL
;
474 struct msg_arg msg
= { .buf
= STRBUF_INIT
};
475 struct ref_transaction
*transaction
;
476 struct strbuf err
= STRBUF_INIT
;
477 struct ref_filter filter
= REF_FILTER_INIT
;
478 struct ref_sorting
*sorting
;
479 struct string_list sorting_options
= STRING_LIST_INIT_DUP
;
480 struct ref_format format
= REF_FORMAT_INIT
;
481 struct strvec trailer_args
= STRVEC_INIT
;
484 struct option options
[] = {
485 OPT_CMDMODE('l', "list", &cmdmode
, N_("list tag names"), 'l'),
486 { OPTION_INTEGER
, 'n', NULL
, &filter
.lines
, N_("n"),
487 N_("print <n> lines of each tag message"),
488 PARSE_OPT_OPTARG
, NULL
, 1 },
489 OPT_CMDMODE('d', "delete", &cmdmode
, N_("delete tags"), 'd'),
490 OPT_CMDMODE('v', "verify", &cmdmode
, N_("verify tags"), 'v'),
492 OPT_GROUP(N_("Tag creation options")),
493 OPT_BOOL('a', "annotate", &annotate
,
494 N_("annotated tag, needs a message")),
495 OPT_CALLBACK_F('m', "message", &msg
, N_("message"),
496 N_("tag message"), PARSE_OPT_NONEG
, parse_msg_arg
),
497 OPT_FILENAME('F', "file", &msgfile
, N_("read message from file")),
498 OPT_PASSTHRU_ARGV(0, "trailer", &trailer_args
, N_("trailer"),
499 N_("add custom trailer(s)"), PARSE_OPT_NONEG
),
500 OPT_BOOL('e', "edit", &edit_flag
, N_("force edit of tag message")),
501 OPT_BOOL('s', "sign", &opt
.sign
, N_("annotated and GPG-signed tag")),
502 OPT_CLEANUP(&cleanup_arg
),
503 OPT_STRING('u', "local-user", &keyid
, N_("key-id"),
504 N_("use another key to sign the tag")),
505 OPT__FORCE(&force
, N_("replace the tag if exists"), 0),
506 OPT_BOOL(0, "create-reflog", &create_reflog
, N_("create a reflog")),
508 OPT_GROUP(N_("Tag listing options")),
509 OPT_COLUMN(0, "column", &colopts
, N_("show tag list in columns")),
510 OPT_CONTAINS(&filter
.with_commit
, N_("print only tags that contain the commit")),
511 OPT_NO_CONTAINS(&filter
.no_commit
, N_("print only tags that don't contain the commit")),
512 OPT_WITH(&filter
.with_commit
, N_("print only tags that contain the commit")),
513 OPT_WITHOUT(&filter
.no_commit
, N_("print only tags that don't contain the commit")),
514 OPT_MERGED(&filter
, N_("print only tags that are merged")),
515 OPT_NO_MERGED(&filter
, N_("print only tags that are not merged")),
516 OPT_BOOL(0, "omit-empty", &format
.array_opts
.omit_empty
,
517 N_("do not output a newline after empty formatted refs")),
518 OPT_REF_SORT(&sorting_options
),
520 OPTION_CALLBACK
, 0, "points-at", &filter
.points_at
, N_("object"),
521 N_("print only tags of the object"), PARSE_OPT_LASTARG_DEFAULT
,
522 parse_opt_object_name
, (intptr_t) "HEAD"
524 OPT_STRING( 0 , "format", &format
.format
, N_("format"),
525 N_("format to use for the output")),
526 OPT__COLOR(&format
.use_color
, N_("respect format colors")),
527 OPT_BOOL('i', "ignore-case", &icase
, N_("sorting and filtering are case insensitive")),
531 const char *only_in_list
= NULL
;
534 setup_ref_filter_porcelain_msg();
537 * Try to set sort keys from config. If config does not set any,
538 * fall back on default (refname) sorting.
540 git_config(git_tag_config
, &sorting_options
);
541 if (!sorting_options
.nr
)
542 string_list_append(&sorting_options
, "refname");
544 memset(&opt
, 0, sizeof(opt
));
548 argc
= parse_options(argc
, argv
, prefix
, options
, git_tag_usage
, 0);
553 else if (filter
.with_commit
|| filter
.no_commit
||
554 filter
.reachable_from
|| filter
.unreachable_from
||
555 filter
.points_at
.nr
|| filter
.lines
!= -1)
560 setup_auto_pager("tag", 1);
563 opt
.sign
= cmdmode
? 0 : config_sign_tag
> 0;
567 set_signing_key(keyid
);
569 create_tag_object
= (opt
.sign
|| annotate
|| msg
.given
|| msgfile
||
570 edit_flag
|| trailer_args
.nr
);
572 if ((create_tag_object
|| force
) && (cmdmode
!= 0))
573 usage_with_options(git_tag_usage
, options
);
575 finalize_colopts(&colopts
, -1);
576 if (cmdmode
== 'l' && filter
.lines
!= -1) {
577 if (explicitly_enable_column(colopts
))
578 die(_("options '%s' and '%s' cannot be used together"), "--column", "-n");
581 sorting
= ref_sorting_options(&sorting_options
);
582 ref_sorting_set_sort_flags_all(sorting
, REF_SORTING_ICASE
, icase
);
583 filter
.ignore_case
= icase
;
584 if (cmdmode
== 'l') {
585 if (column_active(colopts
)) {
586 struct column_options copts
;
587 memset(&copts
, 0, sizeof(copts
));
589 if (run_column_filter(colopts
, &copts
))
590 die(_("could not start 'git column'"));
592 filter
.name_patterns
= argv
;
593 ret
= list_tags(&filter
, sorting
, &format
);
594 if (column_active(colopts
))
595 stop_column_filter();
598 if (filter
.lines
!= -1)
600 else if (filter
.with_commit
)
601 only_in_list
= "--contains";
602 else if (filter
.no_commit
)
603 only_in_list
= "--no-contains";
604 else if (filter
.points_at
.nr
)
605 only_in_list
= "--points-at";
606 else if (filter
.reachable_from
)
607 only_in_list
= "--merged";
608 else if (filter
.unreachable_from
)
609 only_in_list
= "--no-merged";
611 die(_("the '%s' option is only allowed in list mode"), only_in_list
);
612 if (cmdmode
== 'd') {
613 ret
= delete_tags(argv
);
616 if (cmdmode
== 'v') {
617 if (format
.format
&& verify_ref_format(&format
))
618 usage_with_options(git_tag_usage
, options
);
619 ret
= for_each_tag_name(argv
, verify_tag
, &format
);
623 if (msg
.given
|| msgfile
) {
624 if (msg
.given
&& msgfile
)
625 die(_("options '%s' and '%s' cannot be used together"), "-F", "-m");
627 strbuf_addbuf(&buf
, &(msg
.buf
));
629 if (!strcmp(msgfile
, "-")) {
630 if (strbuf_read(&buf
, 0, 1024) < 0)
631 die_errno(_("cannot read '%s'"), msgfile
);
633 if (strbuf_read_file(&buf
, msgfile
, 1024) < 0)
634 die_errno(_("could not open or read '%s'"),
642 object_ref
= argc
== 2 ? argv
[1] : "HEAD";
644 die(_("too many arguments"));
646 if (repo_get_oid(the_repository
, object_ref
, &object
))
647 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
649 if (strbuf_check_tag_ref(&ref
, tag
))
650 die(_("'%s' is not a valid tag name."), tag
);
652 if (refs_read_ref(get_main_ref_store(the_repository
), ref
.buf
, &prev
))
655 die(_("tag '%s' already exists"), tag
);
657 opt
.message_given
= msg
.given
|| msgfile
;
658 opt
.use_editor
= edit_flag
;
660 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "strip"))
661 opt
.cleanup_mode
= CLEANUP_ALL
;
662 else if (!strcmp(cleanup_arg
, "verbatim"))
663 opt
.cleanup_mode
= CLEANUP_NONE
;
664 else if (!strcmp(cleanup_arg
, "whitespace"))
665 opt
.cleanup_mode
= CLEANUP_SPACE
;
667 die(_("Invalid cleanup mode %s"), cleanup_arg
);
669 create_reflog_msg(&object
, &reflog_msg
);
671 if (create_tag_object
) {
672 if (force_sign_annotate
&& !annotate
)
674 path
= git_pathdup("TAG_EDITMSG");
675 create_tag(&object
, object_ref
, tag
, &buf
, &opt
, &prev
, &object
,
676 &trailer_args
, path
);
679 transaction
= ref_store_transaction_begin(get_main_ref_store(the_repository
),
682 ref_transaction_update(transaction
, ref
.buf
, &object
, &prev
,
684 create_reflog
? REF_FORCE_CREATE_REFLOG
: 0,
685 reflog_msg
.buf
, &err
) ||
686 ref_transaction_commit(transaction
, &err
)) {
689 _("The tag message has been left in %s\n"),
694 unlink_or_warn(path
);
697 ref_transaction_free(transaction
);
698 if (force
&& !is_null_oid(&prev
) && !oideq(&prev
, &object
))
699 printf(_("Updated tag '%s' (was %s)\n"), tag
,
700 repo_find_unique_abbrev(the_repository
, &prev
, DEFAULT_ABBREV
));
703 ref_sorting_release(sorting
);
704 ref_filter_clear(&filter
);
705 strbuf_release(&buf
);
706 strbuf_release(&ref
);
707 strbuf_release(&reflog_msg
);
708 strbuf_release(&msg
.buf
);
709 strbuf_release(&err
);
710 strvec_clear(&trailer_args
);