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.
8 #define USE_THE_REPOSITORY_VARIABLE
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 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
);
197 static const char tag_template
[] =
198 N_("\nWrite a message for tag:\n %s\n"
199 "Lines starting with '%s' will be ignored.\n");
201 static const char tag_template_nocleanup
[] =
202 N_("\nWrite a message for tag:\n %s\n"
203 "Lines starting with '%s' will be kept; you may remove them"
204 " yourself if you want to.\n");
206 static int git_tag_config(const char *var
, const char *value
,
207 const struct config_context
*ctx
, void *cb
)
209 if (!strcmp(var
, "tag.gpgsign")) {
210 config_sign_tag
= git_config_bool(var
, value
);
214 if (!strcmp(var
, "tag.sort")) {
216 return config_error_nonbool(var
);
217 string_list_append(cb
, value
);
221 if (!strcmp(var
, "tag.forcesignannotated")) {
222 force_sign_annotate
= git_config_bool(var
, value
);
226 if (starts_with(var
, "column."))
227 return git_column_config(var
, value
, "tag", &colopts
);
229 if (git_color_config(var
, value
, cb
) < 0)
232 return git_default_config(var
, value
, ctx
, cb
);
235 static void write_tag_body(int fd
, const struct object_id
*oid
)
238 enum object_type type
;
239 char *buf
, *sp
, *orig
;
240 struct strbuf payload
= STRBUF_INIT
;
241 struct strbuf signature
= STRBUF_INIT
;
243 orig
= buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
246 if (parse_signature(buf
, size
, &payload
, &signature
)) {
251 sp
= strstr(buf
, "\n\n");
253 if (!sp
|| !size
|| type
!= OBJ_TAG
) {
257 sp
+= 2; /* skip the 2 LFs */
258 write_or_die(fd
, sp
, buf
+ size
- sp
);
261 strbuf_release(&payload
);
262 strbuf_release(&signature
);
265 static int build_tag_object(struct strbuf
*buf
, int sign
, struct object_id
*result
)
267 struct object_id
*compat_oid
= NULL
, compat_oid_buf
;
268 if (sign
&& do_sign(buf
, &compat_oid
, &compat_oid_buf
) < 0)
269 return error(_("unable to sign the tag"));
270 if (write_object_file_flags(buf
->buf
, buf
->len
, OBJ_TAG
, result
,
272 return error(_("unable to write tag file"));
276 struct create_tag_options
{
277 unsigned int message_given
:1;
278 unsigned int use_editor
:1;
287 static const char message_advice_nested_tag
[] =
288 N_("You have created a nested tag. The object referred to by your new tag is\n"
289 "already a tag. If you meant to tag the object that it points to, use:\n"
291 "\tgit tag -f %s %s^{}");
293 static void create_tag(const struct object_id
*object
, const char *object_ref
,
295 struct strbuf
*buf
, struct create_tag_options
*opt
,
296 struct object_id
*prev
, struct object_id
*result
,
297 struct strvec
*trailer_args
, char *path
)
299 enum object_type type
;
300 struct strbuf header
= STRBUF_INIT
;
303 type
= oid_object_info(the_repository
, object
, NULL
);
304 if (type
<= OBJ_NONE
)
305 die(_("bad object type."));
308 advise_if_enabled(ADVICE_NESTED_TAG
, _(message_advice_nested_tag
),
319 git_committer_info(IDENT_STRICT
));
321 should_edit
= opt
->use_editor
|| !opt
->message_given
;
322 if (should_edit
|| trailer_args
->nr
) {
325 /* write the template message before editing: */
326 fd
= xopen(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
328 if (opt
->message_given
&& buf
->len
) {
329 strbuf_complete(buf
, '\n');
330 write_or_die(fd
, buf
->buf
, buf
->len
);
332 } else if (!is_null_oid(prev
)) {
333 write_tag_body(fd
, prev
);
335 struct strbuf buf
= STRBUF_INIT
;
336 strbuf_addch(&buf
, '\n');
337 if (opt
->cleanup_mode
== CLEANUP_ALL
)
338 strbuf_commented_addf(&buf
, comment_line_str
,
339 _(tag_template
), tag
, comment_line_str
);
341 strbuf_commented_addf(&buf
, comment_line_str
,
342 _(tag_template_nocleanup
), tag
, comment_line_str
);
343 write_or_die(fd
, buf
.buf
, buf
.len
);
344 strbuf_release(&buf
);
348 if (trailer_args
->nr
&& amend_file_with_trailers(path
, trailer_args
))
349 die(_("unable to pass trailers to --trailers"));
352 if (launch_editor(path
, buf
, NULL
)) {
354 _("Please supply the message using either -m or -F option.\n"));
357 } else if (trailer_args
->nr
) {
359 if (strbuf_read_file(buf
, path
, 0) < 0)
360 die_errno(_("failed to read '%s'"), path
);
364 if (opt
->cleanup_mode
!= CLEANUP_NONE
)
365 strbuf_stripspace(buf
,
366 opt
->cleanup_mode
== CLEANUP_ALL
? comment_line_str
: NULL
);
368 if (!opt
->message_given
&& !buf
->len
)
369 die(_("no tag message?"));
371 strbuf_insert(buf
, 0, header
.buf
, header
.len
);
372 strbuf_release(&header
);
374 if (build_tag_object(buf
, opt
->sign
, result
) < 0) {
376 fprintf(stderr
, _("The tag message has been left in %s\n"),
382 static void create_reflog_msg(const struct object_id
*oid
, struct strbuf
*sb
)
384 enum object_type type
;
389 const char *subject_start
;
391 char *rla
= getenv("GIT_REFLOG_ACTION");
393 strbuf_addstr(sb
, rla
);
395 strbuf_addstr(sb
, "tag: tagging ");
396 strbuf_add_unique_abbrev(sb
, oid
, DEFAULT_ABBREV
);
399 strbuf_addstr(sb
, " (");
400 type
= oid_object_info(the_repository
, oid
, NULL
);
403 strbuf_addstr(sb
, "object of unknown type");
406 if ((buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
))) {
407 subject_len
= find_commit_subject(buf
, &subject_start
);
408 strbuf_insert(sb
, sb
->len
, subject_start
, subject_len
);
410 strbuf_addstr(sb
, "commit object");
414 if ((c
= lookup_commit_reference(the_repository
, oid
)))
415 strbuf_addf(sb
, ", %s", show_date(c
->date
, 0, DATE_MODE(SHORT
)));
418 strbuf_addstr(sb
, "tree object");
421 strbuf_addstr(sb
, "blob object");
424 strbuf_addstr(sb
, "other tag object");
427 strbuf_addch(sb
, ')');
435 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
437 struct msg_arg
*msg
= opt
->value
;
439 BUG_ON_OPT_NEG(unset
);
444 strbuf_addstr(&(msg
->buf
), "\n\n");
445 strbuf_addstr(&(msg
->buf
), arg
);
450 static int strbuf_check_tag_ref(struct strbuf
*sb
, const char *name
)
456 strbuf_addf(sb
, "refs/tags/%s", name
);
458 return check_refname_format(sb
->buf
, 0);
461 int cmd_tag(int argc
,
464 struct repository
*repo UNUSED
)
466 struct strbuf buf
= STRBUF_INIT
;
467 struct strbuf ref
= STRBUF_INIT
;
468 struct strbuf reflog_msg
= STRBUF_INIT
;
469 struct object_id object
, prev
;
470 const char *object_ref
, *tag
;
471 struct create_tag_options opt
;
472 char *cleanup_arg
= NULL
;
473 int create_reflog
= 0;
474 int annotate
= 0, force
= 0;
475 int cmdmode
= 0, create_tag_object
= 0;
476 char *msgfile
= NULL
;
477 const char *keyid
= NULL
;
478 struct msg_arg msg
= { .buf
= STRBUF_INIT
};
479 struct ref_transaction
*transaction
;
480 struct strbuf err
= STRBUF_INIT
;
481 struct ref_filter filter
= REF_FILTER_INIT
;
482 struct ref_sorting
*sorting
;
483 struct string_list sorting_options
= STRING_LIST_INIT_DUP
;
484 struct ref_format format
= REF_FORMAT_INIT
;
485 struct strvec trailer_args
= STRVEC_INIT
;
488 struct option options
[] = {
489 OPT_CMDMODE('l', "list", &cmdmode
, N_("list tag names"), 'l'),
490 { OPTION_INTEGER
, 'n', NULL
, &filter
.lines
, N_("n"),
491 N_("print <n> lines of each tag message"),
492 PARSE_OPT_OPTARG
, NULL
, 1 },
493 OPT_CMDMODE('d', "delete", &cmdmode
, N_("delete tags"), 'd'),
494 OPT_CMDMODE('v', "verify", &cmdmode
, N_("verify tags"), 'v'),
496 OPT_GROUP(N_("Tag creation options")),
497 OPT_BOOL('a', "annotate", &annotate
,
498 N_("annotated tag, needs a message")),
499 OPT_CALLBACK_F('m', "message", &msg
, N_("message"),
500 N_("tag message"), PARSE_OPT_NONEG
, parse_msg_arg
),
501 OPT_FILENAME('F', "file", &msgfile
, N_("read message from file")),
502 OPT_PASSTHRU_ARGV(0, "trailer", &trailer_args
, N_("trailer"),
503 N_("add custom trailer(s)"), PARSE_OPT_NONEG
),
504 OPT_BOOL('e', "edit", &edit_flag
, N_("force edit of tag message")),
505 OPT_BOOL('s', "sign", &opt
.sign
, N_("annotated and GPG-signed tag")),
506 OPT_CLEANUP(&cleanup_arg
),
507 OPT_STRING('u', "local-user", &keyid
, N_("key-id"),
508 N_("use another key to sign the tag")),
509 OPT__FORCE(&force
, N_("replace the tag if exists"), 0),
510 OPT_BOOL(0, "create-reflog", &create_reflog
, N_("create a reflog")),
512 OPT_GROUP(N_("Tag listing options")),
513 OPT_COLUMN(0, "column", &colopts
, N_("show tag list in columns")),
514 OPT_CONTAINS(&filter
.with_commit
, N_("print only tags that contain the commit")),
515 OPT_NO_CONTAINS(&filter
.no_commit
, N_("print only tags that don't contain the commit")),
516 OPT_WITH(&filter
.with_commit
, N_("print only tags that contain the commit")),
517 OPT_WITHOUT(&filter
.no_commit
, N_("print only tags that don't contain the commit")),
518 OPT_MERGED(&filter
, N_("print only tags that are merged")),
519 OPT_NO_MERGED(&filter
, N_("print only tags that are not merged")),
520 OPT_BOOL(0, "omit-empty", &format
.array_opts
.omit_empty
,
521 N_("do not output a newline after empty formatted refs")),
522 OPT_REF_SORT(&sorting_options
),
524 OPTION_CALLBACK
, 0, "points-at", &filter
.points_at
, N_("object"),
525 N_("print only tags of the object"), PARSE_OPT_LASTARG_DEFAULT
,
526 parse_opt_object_name
, (intptr_t) "HEAD"
528 OPT_STRING( 0 , "format", &format
.format
, N_("format"),
529 N_("format to use for the output")),
530 OPT__COLOR(&format
.use_color
, N_("respect format colors")),
531 OPT_BOOL('i', "ignore-case", &icase
, N_("sorting and filtering are case insensitive")),
535 const char *only_in_list
= NULL
;
538 setup_ref_filter_porcelain_msg();
541 * Try to set sort keys from config. If config does not set any,
542 * fall back on default (refname) sorting.
544 git_config(git_tag_config
, &sorting_options
);
545 if (!sorting_options
.nr
)
546 string_list_append(&sorting_options
, "refname");
548 memset(&opt
, 0, sizeof(opt
));
552 argc
= parse_options(argc
, argv
, prefix
, options
, git_tag_usage
, 0);
557 else if (filter
.with_commit
|| filter
.no_commit
||
558 filter
.reachable_from
|| filter
.unreachable_from
||
559 filter
.points_at
.nr
|| filter
.lines
!= -1)
564 setup_auto_pager("tag", 1);
567 opt
.sign
= cmdmode
? 0 : config_sign_tag
> 0;
571 set_signing_key(keyid
);
573 create_tag_object
= (opt
.sign
|| annotate
|| msg
.given
|| msgfile
||
574 edit_flag
|| trailer_args
.nr
);
576 if ((create_tag_object
|| force
) && (cmdmode
!= 0))
577 usage_with_options(git_tag_usage
, options
);
579 finalize_colopts(&colopts
, -1);
580 if (cmdmode
== 'l' && filter
.lines
!= -1) {
581 if (explicitly_enable_column(colopts
))
582 die(_("options '%s' and '%s' cannot be used together"), "--column", "-n");
585 sorting
= ref_sorting_options(&sorting_options
);
586 ref_sorting_set_sort_flags_all(sorting
, REF_SORTING_ICASE
, icase
);
587 filter
.ignore_case
= icase
;
588 if (cmdmode
== 'l') {
589 if (column_active(colopts
)) {
590 struct column_options copts
;
591 memset(&copts
, 0, sizeof(copts
));
593 if (run_column_filter(colopts
, &copts
))
594 die(_("could not start 'git column'"));
596 filter
.name_patterns
= argv
;
597 ret
= list_tags(&filter
, sorting
, &format
);
598 if (column_active(colopts
))
599 stop_column_filter();
602 if (filter
.lines
!= -1)
604 else if (filter
.with_commit
)
605 only_in_list
= "--contains";
606 else if (filter
.no_commit
)
607 only_in_list
= "--no-contains";
608 else if (filter
.points_at
.nr
)
609 only_in_list
= "--points-at";
610 else if (filter
.reachable_from
)
611 only_in_list
= "--merged";
612 else if (filter
.unreachable_from
)
613 only_in_list
= "--no-merged";
615 die(_("the '%s' option is only allowed in list mode"), only_in_list
);
616 if (cmdmode
== 'd') {
617 ret
= delete_tags(argv
);
620 if (cmdmode
== 'v') {
621 if (format
.format
&& verify_ref_format(&format
))
622 usage_with_options(git_tag_usage
, options
);
623 ret
= for_each_tag_name(argv
, verify_tag
, &format
);
627 if (msg
.given
|| msgfile
) {
628 if (msg
.given
&& msgfile
)
629 die(_("options '%s' and '%s' cannot be used together"), "-F", "-m");
631 strbuf_addbuf(&buf
, &(msg
.buf
));
633 if (!strcmp(msgfile
, "-")) {
634 if (strbuf_read(&buf
, 0, 1024) < 0)
635 die_errno(_("cannot read '%s'"), msgfile
);
637 if (strbuf_read_file(&buf
, msgfile
, 1024) < 0)
638 die_errno(_("could not open or read '%s'"),
646 object_ref
= argc
== 2 ? argv
[1] : "HEAD";
648 die(_("too many arguments"));
650 if (repo_get_oid(the_repository
, object_ref
, &object
))
651 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
653 if (strbuf_check_tag_ref(&ref
, tag
))
654 die(_("'%s' is not a valid tag name."), tag
);
656 if (refs_read_ref(get_main_ref_store(the_repository
), ref
.buf
, &prev
))
657 oidclr(&prev
, the_repository
->hash_algo
);
659 die(_("tag '%s' already exists"), tag
);
661 opt
.message_given
= msg
.given
|| msgfile
;
662 opt
.use_editor
= edit_flag
;
664 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "strip"))
665 opt
.cleanup_mode
= CLEANUP_ALL
;
666 else if (!strcmp(cleanup_arg
, "verbatim"))
667 opt
.cleanup_mode
= CLEANUP_NONE
;
668 else if (!strcmp(cleanup_arg
, "whitespace"))
669 opt
.cleanup_mode
= CLEANUP_SPACE
;
671 die(_("Invalid cleanup mode %s"), cleanup_arg
);
673 create_reflog_msg(&object
, &reflog_msg
);
675 if (create_tag_object
) {
676 if (force_sign_annotate
&& !annotate
)
678 path
= git_pathdup("TAG_EDITMSG");
679 create_tag(&object
, object_ref
, tag
, &buf
, &opt
, &prev
, &object
,
680 &trailer_args
, path
);
683 transaction
= ref_store_transaction_begin(get_main_ref_store(the_repository
),
686 ref_transaction_update(transaction
, ref
.buf
, &object
, &prev
,
688 create_reflog
? REF_FORCE_CREATE_REFLOG
: 0,
689 reflog_msg
.buf
, &err
) ||
690 ref_transaction_commit(transaction
, &err
)) {
693 _("The tag message has been left in %s\n"),
698 unlink_or_warn(path
);
701 ref_transaction_free(transaction
);
702 if (force
&& !is_null_oid(&prev
) && !oideq(&prev
, &object
))
703 printf(_("Updated tag '%s' (was %s)\n"), tag
,
704 repo_find_unique_abbrev(the_repository
, &prev
, DEFAULT_ABBREV
));
707 ref_sorting_release(sorting
);
708 ref_filter_clear(&filter
);
709 ref_format_clear(&format
);
710 strbuf_release(&buf
);
711 strbuf_release(&ref
);
712 strbuf_release(&reflog_msg
);
713 strbuf_release(&msg
.buf
);
714 strbuf_release(&err
);
715 strvec_clear(&trailer_args
);