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.
9 #define USE_THE_REPOSITORY_VARIABLE
10 #define DISABLE_SIGN_COMPARE_WARNINGS
16 #include "environment.h"
20 #include "object-file.h"
21 #include "object-name.h"
22 #include "object-store.h"
25 #include "parse-options.h"
28 #include "gpg-interface.h"
29 #include "oid-array.h"
31 #include "ref-filter.h"
33 #include "write-or-die.h"
34 #include "object-file-convert.h"
37 static const char * const git_tag_usage
[] = {
38 N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] [-e]\n"
39 " [(--trailer <token>[(=|:)<value>])...]\n"
40 " <tagname> [<commit> | <object>]"),
41 N_("git tag -d <tagname>..."),
42 N_("git tag [-n[<num>]] -l [--contains <commit>] [--no-contains <commit>]\n"
43 " [--points-at <object>] [--column[=<options>] | --no-column]\n"
44 " [--create-reflog] [--sort=<key>] [--format=<format>]\n"
45 " [--merged <commit>] [--no-merged <commit>] [<pattern>...]"),
46 N_("git tag -v [--format=<format>] <tagname>..."),
50 static unsigned int colopts
;
51 static int force_sign_annotate
;
52 static int config_sign_tag
= -1; /* unspecified */
54 static int list_tags(struct ref_filter
*filter
, struct ref_sorting
*sorting
,
55 struct ref_format
*format
)
59 if (filter
->lines
== -1)
62 if (!format
->format
) {
64 to_free
= xstrfmt("%s %%(contents:lines=%d)",
65 "%(align:15)%(refname:lstrip=2)%(end)",
67 format
->format
= to_free
;
69 format
->format
= "%(refname:lstrip=2)";
72 if (verify_ref_format(format
))
73 die(_("unable to parse format string"));
74 filter
->with_commit_tag_algo
= 1;
75 filter_and_format_refs(filter
, FILTER_REFS_TAGS
, sorting
, format
);
82 typedef int (*each_tag_name_fn
)(const char *name
, const char *ref
,
83 const struct object_id
*oid
, void *cb_data
);
85 static int for_each_tag_name(const char **argv
, each_tag_name_fn fn
,
89 struct strbuf ref
= STRBUF_INIT
;
93 for (p
= argv
; *p
; p
++) {
95 strbuf_addf(&ref
, "refs/tags/%s", *p
);
96 if (refs_read_ref(get_main_ref_store(the_repository
), ref
.buf
, &oid
)) {
97 error(_("tag '%s' not found."), *p
);
101 if (fn(*p
, ref
.buf
, &oid
, cb_data
))
104 strbuf_release(&ref
);
108 static int collect_tags(const char *name UNUSED
, const char *ref
,
109 const struct object_id
*oid
, void *cb_data
)
111 struct string_list
*ref_list
= cb_data
;
113 string_list_append(ref_list
, ref
);
114 ref_list
->items
[ref_list
->nr
- 1].util
= oiddup(oid
);
118 static int delete_tags(const char **argv
)
121 struct string_list refs_to_delete
= STRING_LIST_INIT_DUP
;
122 struct string_list_item
*item
;
124 result
= for_each_tag_name(argv
, collect_tags
, (void *)&refs_to_delete
);
125 if (refs_delete_refs(get_main_ref_store(the_repository
), NULL
, &refs_to_delete
, REF_NO_DEREF
))
128 for_each_string_list_item(item
, &refs_to_delete
) {
129 const char *name
= item
->string
;
130 struct object_id
*oid
= item
->util
;
131 if (!refs_ref_exists(get_main_ref_store(the_repository
), name
))
132 printf(_("Deleted tag '%s' (was %s)\n"),
134 repo_find_unique_abbrev(the_repository
, oid
, DEFAULT_ABBREV
));
138 string_list_clear(&refs_to_delete
, 0);
142 static int verify_tag(const char *name
, const char *ref UNUSED
,
143 const struct object_id
*oid
, void *cb_data
)
146 struct ref_format
*format
= cb_data
;
147 flags
= GPG_VERIFY_VERBOSE
;
150 flags
= GPG_VERIFY_OMIT_STATUS
;
152 if (gpg_verify_tag(oid
, name
, flags
))
156 pretty_print_ref(name
, oid
, format
);
161 static int do_sign(struct strbuf
*buffer
, struct object_id
**compat_oid
,
162 struct object_id
*compat_oid_buf
)
164 const struct git_hash_algo
*compat
= the_repository
->compat_hash_algo
;
165 struct strbuf sig
= STRBUF_INIT
, compat_sig
= STRBUF_INIT
;
166 struct strbuf compat_buf
= STRBUF_INIT
;
167 char *keyid
= get_signing_key();
170 if (sign_buffer(buffer
, &sig
, keyid
))
174 const struct git_hash_algo
*algo
= the_repository
->hash_algo
;
176 if (convert_object_file(the_repository
,&compat_buf
, algo
, compat
,
177 buffer
->buf
, buffer
->len
, OBJ_TAG
, 1))
179 if (sign_buffer(&compat_buf
, &compat_sig
, keyid
))
181 add_header_signature(&compat_buf
, &sig
, algo
);
182 strbuf_addbuf(&compat_buf
, &compat_sig
);
183 hash_object_file(compat
, compat_buf
.buf
, compat_buf
.len
,
184 OBJ_TAG
, compat_oid_buf
);
185 *compat_oid
= compat_oid_buf
;
189 add_header_signature(buffer
, &compat_sig
, compat
);
191 strbuf_addbuf(buffer
, &sig
);
194 strbuf_release(&sig
);
195 strbuf_release(&compat_sig
);
196 strbuf_release(&compat_buf
);
201 static const char tag_template
[] =
202 N_("\nWrite a message for tag:\n %s\n"
203 "Lines starting with '%s' will be ignored.\n");
205 static const char tag_template_nocleanup
[] =
206 N_("\nWrite a message for tag:\n %s\n"
207 "Lines starting with '%s' will be kept; you may remove them"
208 " yourself if you want to.\n");
210 static int git_tag_config(const char *var
, const char *value
,
211 const struct config_context
*ctx
, void *cb
)
213 if (!strcmp(var
, "tag.gpgsign")) {
214 config_sign_tag
= git_config_bool(var
, value
);
218 if (!strcmp(var
, "tag.sort")) {
220 return config_error_nonbool(var
);
221 string_list_append(cb
, value
);
225 if (!strcmp(var
, "tag.forcesignannotated")) {
226 force_sign_annotate
= git_config_bool(var
, value
);
230 if (starts_with(var
, "column."))
231 return git_column_config(var
, value
, "tag", &colopts
);
233 if (git_color_config(var
, value
, cb
) < 0)
236 return git_default_config(var
, value
, ctx
, cb
);
239 static void write_tag_body(int fd
, const struct object_id
*oid
)
242 enum object_type type
;
243 char *buf
, *sp
, *orig
;
244 struct strbuf payload
= STRBUF_INIT
;
245 struct strbuf signature
= STRBUF_INIT
;
247 orig
= buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
250 if (parse_signature(buf
, size
, &payload
, &signature
)) {
255 sp
= strstr(buf
, "\n\n");
257 if (!sp
|| !size
|| type
!= OBJ_TAG
) {
261 sp
+= 2; /* skip the 2 LFs */
262 write_or_die(fd
, sp
, buf
+ size
- sp
);
265 strbuf_release(&payload
);
266 strbuf_release(&signature
);
269 static int build_tag_object(struct strbuf
*buf
, int sign
, struct object_id
*result
)
271 struct object_id
*compat_oid
= NULL
, compat_oid_buf
;
272 if (sign
&& do_sign(buf
, &compat_oid
, &compat_oid_buf
) < 0)
273 return error(_("unable to sign the tag"));
274 if (write_object_file_flags(buf
->buf
, buf
->len
, OBJ_TAG
, result
,
276 return error(_("unable to write tag file"));
280 struct create_tag_options
{
281 unsigned int message_given
:1;
282 unsigned int use_editor
:1;
291 static const char message_advice_nested_tag
[] =
292 N_("You have created a nested tag. The object referred to by your new tag is\n"
293 "already a tag. If you meant to tag the object that it points to, use:\n"
295 "\tgit tag -f %s %s^{}");
297 static void create_tag(const struct object_id
*object
, const char *object_ref
,
299 struct strbuf
*buf
, struct create_tag_options
*opt
,
300 struct object_id
*prev
, struct object_id
*result
,
301 struct strvec
*trailer_args
, char *path
)
303 enum object_type type
;
304 struct strbuf header
= STRBUF_INIT
;
307 type
= oid_object_info(the_repository
, object
, NULL
);
308 if (type
<= OBJ_NONE
)
309 die(_("bad object type."));
312 advise_if_enabled(ADVICE_NESTED_TAG
, _(message_advice_nested_tag
),
323 git_committer_info(IDENT_STRICT
));
325 should_edit
= opt
->use_editor
|| !opt
->message_given
;
326 if (should_edit
|| trailer_args
->nr
) {
329 /* write the template message before editing: */
330 fd
= xopen(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
332 if (opt
->message_given
&& buf
->len
) {
333 strbuf_complete(buf
, '\n');
334 write_or_die(fd
, buf
->buf
, buf
->len
);
336 } else if (!is_null_oid(prev
)) {
337 write_tag_body(fd
, prev
);
339 struct strbuf buf
= STRBUF_INIT
;
340 strbuf_addch(&buf
, '\n');
341 if (opt
->cleanup_mode
== CLEANUP_ALL
)
342 strbuf_commented_addf(&buf
, comment_line_str
,
343 _(tag_template
), tag
, comment_line_str
);
345 strbuf_commented_addf(&buf
, comment_line_str
,
346 _(tag_template_nocleanup
), tag
, comment_line_str
);
347 write_or_die(fd
, buf
.buf
, buf
.len
);
348 strbuf_release(&buf
);
352 if (trailer_args
->nr
&& amend_file_with_trailers(path
, trailer_args
))
353 die(_("unable to pass trailers to --trailers"));
356 if (launch_editor(path
, buf
, NULL
)) {
358 _("Please supply the message using either -m or -F option.\n"));
361 } else if (trailer_args
->nr
) {
363 if (strbuf_read_file(buf
, path
, 0) < 0)
364 die_errno(_("failed to read '%s'"), path
);
368 if (opt
->cleanup_mode
!= CLEANUP_NONE
)
369 strbuf_stripspace(buf
,
370 opt
->cleanup_mode
== CLEANUP_ALL
? comment_line_str
: NULL
);
372 if (!opt
->message_given
&& !buf
->len
)
373 die(_("no tag message?"));
375 strbuf_insert(buf
, 0, header
.buf
, header
.len
);
376 strbuf_release(&header
);
378 if (build_tag_object(buf
, opt
->sign
, result
) < 0) {
380 fprintf(stderr
, _("The tag message has been left in %s\n"),
386 static void create_reflog_msg(const struct object_id
*oid
, struct strbuf
*sb
)
388 enum object_type type
;
393 const char *subject_start
;
395 char *rla
= getenv("GIT_REFLOG_ACTION");
397 strbuf_addstr(sb
, rla
);
399 strbuf_addstr(sb
, "tag: tagging ");
400 strbuf_add_unique_abbrev(sb
, oid
, DEFAULT_ABBREV
);
403 strbuf_addstr(sb
, " (");
404 type
= oid_object_info(the_repository
, oid
, NULL
);
407 strbuf_addstr(sb
, "object of unknown type");
410 if ((buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
))) {
411 subject_len
= find_commit_subject(buf
, &subject_start
);
412 strbuf_insert(sb
, sb
->len
, subject_start
, subject_len
);
414 strbuf_addstr(sb
, "commit object");
418 if ((c
= lookup_commit_reference(the_repository
, oid
)))
419 strbuf_addf(sb
, ", %s", show_date(c
->date
, 0, DATE_MODE(SHORT
)));
422 strbuf_addstr(sb
, "tree object");
425 strbuf_addstr(sb
, "blob object");
428 strbuf_addstr(sb
, "other tag object");
431 strbuf_addch(sb
, ')');
439 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
441 struct msg_arg
*msg
= opt
->value
;
443 BUG_ON_OPT_NEG(unset
);
448 strbuf_addstr(&(msg
->buf
), "\n\n");
449 strbuf_addstr(&(msg
->buf
), arg
);
454 int cmd_tag(int argc
,
457 struct repository
*repo UNUSED
)
459 struct strbuf buf
= STRBUF_INIT
;
460 struct strbuf ref
= STRBUF_INIT
;
461 struct strbuf reflog_msg
= STRBUF_INIT
;
462 struct object_id object
, prev
;
463 const char *object_ref
, *tag
;
464 struct create_tag_options opt
;
465 char *cleanup_arg
= NULL
;
466 int create_reflog
= 0;
467 int annotate
= 0, force
= 0;
468 int cmdmode
= 0, create_tag_object
= 0;
469 char *msgfile
= NULL
;
470 const char *keyid
= NULL
;
471 struct msg_arg msg
= { .buf
= STRBUF_INIT
};
472 struct ref_transaction
*transaction
;
473 struct strbuf err
= STRBUF_INIT
;
474 struct ref_filter filter
= REF_FILTER_INIT
;
475 struct ref_sorting
*sorting
;
476 struct string_list sorting_options
= STRING_LIST_INIT_DUP
;
477 struct ref_format format
= REF_FORMAT_INIT
;
478 struct strvec trailer_args
= STRVEC_INIT
;
481 struct option options
[] = {
482 OPT_CMDMODE('l', "list", &cmdmode
, N_("list tag names"), 'l'),
484 .type
= OPTION_INTEGER
,
486 .value
= &filter
.lines
,
487 .precision
= sizeof(filter
.lines
),
489 .help
= N_("print <n> lines of each tag message"),
490 .flags
= PARSE_OPT_OPTARG
,
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 .type
= OPTION_CALLBACK
,
525 .long_name
= "points-at",
526 .value
= &filter
.points_at
,
527 .argh
= N_("object"),
528 .help
= N_("print only tags of the object"),
529 .flags
= PARSE_OPT_LASTARG_DEFAULT
,
530 .callback
= parse_opt_object_name
,
531 .defval
= (intptr_t) "HEAD",
533 OPT_STRING( 0 , "format", &format
.format
, N_("format"),
534 N_("format to use for the output")),
535 OPT__COLOR(&format
.use_color
, N_("respect format colors")),
536 OPT_BOOL('i', "ignore-case", &icase
, N_("sorting and filtering are case insensitive")),
540 const char *only_in_list
= NULL
;
543 setup_ref_filter_porcelain_msg();
546 * Try to set sort keys from config. If config does not set any,
547 * fall back on default (refname) sorting.
549 git_config(git_tag_config
, &sorting_options
);
550 if (!sorting_options
.nr
)
551 string_list_append(&sorting_options
, "refname");
553 memset(&opt
, 0, sizeof(opt
));
557 argc
= parse_options(argc
, argv
, prefix
, options
, git_tag_usage
, 0);
562 else if (filter
.with_commit
|| filter
.no_commit
||
563 filter
.reachable_from
|| filter
.unreachable_from
||
564 filter
.points_at
.nr
|| filter
.lines
!= -1)
569 setup_auto_pager("tag", 1);
572 opt
.sign
= cmdmode
? 0 : config_sign_tag
> 0;
576 set_signing_key(keyid
);
578 create_tag_object
= (opt
.sign
|| annotate
|| msg
.given
|| msgfile
||
579 edit_flag
|| trailer_args
.nr
);
581 if ((create_tag_object
|| force
) && (cmdmode
!= 0))
582 usage_with_options(git_tag_usage
, options
);
584 finalize_colopts(&colopts
, -1);
585 if (cmdmode
== 'l' && filter
.lines
!= -1) {
586 if (explicitly_enable_column(colopts
))
587 die(_("options '%s' and '%s' cannot be used together"), "--column", "-n");
590 sorting
= ref_sorting_options(&sorting_options
);
591 ref_sorting_set_sort_flags_all(sorting
, REF_SORTING_ICASE
, icase
);
592 filter
.ignore_case
= icase
;
593 if (cmdmode
== 'l') {
594 if (column_active(colopts
)) {
595 struct column_options copts
;
596 memset(&copts
, 0, sizeof(copts
));
598 if (run_column_filter(colopts
, &copts
))
599 die(_("could not start 'git column'"));
601 filter
.name_patterns
= argv
;
602 ret
= list_tags(&filter
, sorting
, &format
);
603 if (column_active(colopts
))
604 stop_column_filter();
607 if (filter
.lines
!= -1)
609 else if (filter
.with_commit
)
610 only_in_list
= "--contains";
611 else if (filter
.no_commit
)
612 only_in_list
= "--no-contains";
613 else if (filter
.points_at
.nr
)
614 only_in_list
= "--points-at";
615 else if (filter
.reachable_from
)
616 only_in_list
= "--merged";
617 else if (filter
.unreachable_from
)
618 only_in_list
= "--no-merged";
620 die(_("the '%s' option is only allowed in list mode"), only_in_list
);
621 if (cmdmode
== 'd') {
622 ret
= delete_tags(argv
);
625 if (cmdmode
== 'v') {
626 if (format
.format
&& verify_ref_format(&format
))
627 usage_with_options(git_tag_usage
, options
);
628 ret
= for_each_tag_name(argv
, verify_tag
, &format
);
632 if (msg
.given
|| msgfile
) {
633 if (msg
.given
&& msgfile
)
634 die(_("options '%s' and '%s' cannot be used together"), "-F", "-m");
636 strbuf_addbuf(&buf
, &(msg
.buf
));
638 if (!strcmp(msgfile
, "-")) {
639 if (strbuf_read(&buf
, 0, 1024) < 0)
640 die_errno(_("cannot read '%s'"), msgfile
);
642 if (strbuf_read_file(&buf
, msgfile
, 1024) < 0)
643 die_errno(_("could not open or read '%s'"),
651 object_ref
= argc
== 2 ? argv
[1] : "HEAD";
653 die(_("too many arguments"));
655 if (repo_get_oid(the_repository
, object_ref
, &object
))
656 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
658 if (check_tag_ref(&ref
, tag
))
659 die(_("'%s' is not a valid tag name."), tag
);
661 if (refs_read_ref(get_main_ref_store(the_repository
), ref
.buf
, &prev
))
662 oidclr(&prev
, the_repository
->hash_algo
);
664 die(_("tag '%s' already exists"), tag
);
666 opt
.message_given
= msg
.given
|| msgfile
;
667 opt
.use_editor
= edit_flag
;
669 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "strip"))
670 opt
.cleanup_mode
= CLEANUP_ALL
;
671 else if (!strcmp(cleanup_arg
, "verbatim"))
672 opt
.cleanup_mode
= CLEANUP_NONE
;
673 else if (!strcmp(cleanup_arg
, "whitespace"))
674 opt
.cleanup_mode
= CLEANUP_SPACE
;
676 die(_("Invalid cleanup mode %s"), cleanup_arg
);
678 create_reflog_msg(&object
, &reflog_msg
);
680 if (create_tag_object
) {
681 if (force_sign_annotate
&& !annotate
)
683 path
= repo_git_path(the_repository
, "TAG_EDITMSG");
684 create_tag(&object
, object_ref
, tag
, &buf
, &opt
, &prev
, &object
,
685 &trailer_args
, path
);
688 transaction
= ref_store_transaction_begin(get_main_ref_store(the_repository
),
691 ref_transaction_update(transaction
, ref
.buf
, &object
, &prev
,
693 create_reflog
? REF_FORCE_CREATE_REFLOG
: 0,
694 reflog_msg
.buf
, &err
) ||
695 ref_transaction_commit(transaction
, &err
)) {
698 _("The tag message has been left in %s\n"),
703 unlink_or_warn(path
);
706 ref_transaction_free(transaction
);
707 if (force
&& !is_null_oid(&prev
) && !oideq(&prev
, &object
))
708 printf(_("Updated tag '%s' (was %s)\n"), tag
,
709 repo_find_unique_abbrev(the_repository
, &prev
, DEFAULT_ABBREV
));
712 ref_sorting_release(sorting
);
713 ref_filter_clear(&filter
);
714 strbuf_release(&buf
);
715 strbuf_release(&ref
);
716 strbuf_release(&reflog_msg
);
717 strbuf_release(&msg
.buf
);
718 strbuf_release(&err
);
719 strvec_clear(&trailer_args
);