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 "run-command.h"
22 #include "parse-options.h"
25 #include "gpg-interface.h"
26 #include "oid-array.h"
28 #include "ref-filter.h"
30 #include "write-or-die.h"
32 static const char * const git_tag_usage
[] = {
33 N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] [-e]\n"
34 " <tagname> [<commit> | <object>]"),
35 N_("git tag -d <tagname>..."),
36 N_("git tag [-n[<num>]] -l [--contains <commit>] [--no-contains <commit>]\n"
37 " [--points-at <object>] [--column[=<options>] | --no-column]\n"
38 " [--create-reflog] [--sort=<key>] [--format=<format>]\n"
39 " [--merged <commit>] [--no-merged <commit>] [<pattern>...]"),
40 N_("git tag -v [--format=<format>] <tagname>..."),
44 static unsigned int colopts
;
45 static int force_sign_annotate
;
46 static int config_sign_tag
= -1; /* unspecified */
47 static int omit_empty
= 0;
49 static int list_tags(struct ref_filter
*filter
, struct ref_sorting
*sorting
,
50 struct ref_format
*format
)
52 struct ref_array array
;
53 struct strbuf output
= STRBUF_INIT
;
54 struct strbuf err
= STRBUF_INIT
;
58 memset(&array
, 0, sizeof(array
));
60 if (filter
->lines
== -1)
63 if (!format
->format
) {
65 to_free
= xstrfmt("%s %%(contents:lines=%d)",
66 "%(align:15)%(refname:lstrip=2)%(end)",
68 format
->format
= to_free
;
70 format
->format
= "%(refname:lstrip=2)";
73 if (verify_ref_format(format
))
74 die(_("unable to parse format string"));
75 filter
->with_commit_tag_algo
= 1;
76 filter_refs(&array
, filter
, FILTER_REFS_TAGS
);
77 filter_ahead_behind(the_repository
, format
, &array
);
78 ref_array_sort(sorting
, &array
);
80 for (i
= 0; i
< array
.nr
; i
++) {
81 strbuf_reset(&output
);
83 if (format_ref_array_item(array
.items
[i
], format
, &output
, &err
))
85 fwrite(output
.buf
, 1, output
.len
, stdout
);
86 if (output
.len
|| !omit_empty
)
91 strbuf_release(&output
);
92 ref_array_clear(&array
);
98 typedef int (*each_tag_name_fn
)(const char *name
, const char *ref
,
99 const struct object_id
*oid
, void *cb_data
);
101 static int for_each_tag_name(const char **argv
, each_tag_name_fn fn
,
105 struct strbuf ref
= STRBUF_INIT
;
107 struct object_id oid
;
109 for (p
= argv
; *p
; p
++) {
111 strbuf_addf(&ref
, "refs/tags/%s", *p
);
112 if (read_ref(ref
.buf
, &oid
)) {
113 error(_("tag '%s' not found."), *p
);
117 if (fn(*p
, ref
.buf
, &oid
, cb_data
))
120 strbuf_release(&ref
);
124 static int collect_tags(const char *name UNUSED
, const char *ref
,
125 const struct object_id
*oid
, void *cb_data
)
127 struct string_list
*ref_list
= cb_data
;
129 string_list_append(ref_list
, ref
);
130 ref_list
->items
[ref_list
->nr
- 1].util
= oiddup(oid
);
134 static int delete_tags(const char **argv
)
137 struct string_list refs_to_delete
= STRING_LIST_INIT_DUP
;
138 struct string_list_item
*item
;
140 result
= for_each_tag_name(argv
, collect_tags
, (void *)&refs_to_delete
);
141 if (delete_refs(NULL
, &refs_to_delete
, REF_NO_DEREF
))
144 for_each_string_list_item(item
, &refs_to_delete
) {
145 const char *name
= item
->string
;
146 struct object_id
*oid
= item
->util
;
147 if (!ref_exists(name
))
148 printf(_("Deleted tag '%s' (was %s)\n"),
150 repo_find_unique_abbrev(the_repository
, oid
, DEFAULT_ABBREV
));
154 string_list_clear(&refs_to_delete
, 0);
158 static int verify_tag(const char *name
, const char *ref UNUSED
,
159 const struct object_id
*oid
, void *cb_data
)
162 struct ref_format
*format
= cb_data
;
163 flags
= GPG_VERIFY_VERBOSE
;
166 flags
= GPG_VERIFY_OMIT_STATUS
;
168 if (gpg_verify_tag(oid
, name
, flags
))
172 pretty_print_ref(name
, oid
, format
);
177 static int do_sign(struct strbuf
*buffer
)
179 return sign_buffer(buffer
, buffer
, get_signing_key());
182 static const char tag_template
[] =
183 N_("\nWrite a message for tag:\n %s\n"
184 "Lines starting with '%c' will be ignored.\n");
186 static const char tag_template_nocleanup
[] =
187 N_("\nWrite a message for tag:\n %s\n"
188 "Lines starting with '%c' will be kept; you may remove them"
189 " yourself if you want to.\n");
191 static int git_tag_config(const char *var
, const char *value
,
192 const struct config_context
*ctx
, void *cb
)
194 if (!strcmp(var
, "tag.gpgsign")) {
195 config_sign_tag
= git_config_bool(var
, value
);
199 if (!strcmp(var
, "tag.sort")) {
201 return config_error_nonbool(var
);
202 string_list_append(cb
, value
);
206 if (!strcmp(var
, "tag.forcesignannotated")) {
207 force_sign_annotate
= git_config_bool(var
, value
);
211 if (starts_with(var
, "column."))
212 return git_column_config(var
, value
, "tag", &colopts
);
214 if (git_color_config(var
, value
, cb
) < 0)
217 return git_default_config(var
, value
, ctx
, cb
);
220 static void write_tag_body(int fd
, const struct object_id
*oid
)
223 enum object_type type
;
224 char *buf
, *sp
, *orig
;
225 struct strbuf payload
= STRBUF_INIT
;
226 struct strbuf signature
= STRBUF_INIT
;
228 orig
= buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
231 if (parse_signature(buf
, size
, &payload
, &signature
)) {
236 sp
= strstr(buf
, "\n\n");
238 if (!sp
|| !size
|| type
!= OBJ_TAG
) {
242 sp
+= 2; /* skip the 2 LFs */
243 write_or_die(fd
, sp
, buf
+ size
- sp
);
246 strbuf_release(&payload
);
247 strbuf_release(&signature
);
250 static int build_tag_object(struct strbuf
*buf
, int sign
, struct object_id
*result
)
252 if (sign
&& do_sign(buf
) < 0)
253 return error(_("unable to sign the tag"));
254 if (write_object_file(buf
->buf
, buf
->len
, OBJ_TAG
, result
) < 0)
255 return error(_("unable to write tag file"));
259 struct create_tag_options
{
260 unsigned int message_given
:1;
261 unsigned int use_editor
:1;
270 static const char message_advice_nested_tag
[] =
271 N_("You have created a nested tag. The object referred to by your new tag is\n"
272 "already a tag. If you meant to tag the object that it points to, use:\n"
274 "\tgit tag -f %s %s^{}");
276 static void create_tag(const struct object_id
*object
, const char *object_ref
,
278 struct strbuf
*buf
, struct create_tag_options
*opt
,
279 struct object_id
*prev
, struct object_id
*result
, char *path
)
281 enum object_type type
;
282 struct strbuf header
= STRBUF_INIT
;
284 type
= oid_object_info(the_repository
, object
, NULL
);
285 if (type
<= OBJ_NONE
)
286 die(_("bad object type."));
289 advise_if_enabled(ADVICE_NESTED_TAG
, _(message_advice_nested_tag
),
300 git_committer_info(IDENT_STRICT
));
302 if (!opt
->message_given
|| opt
->use_editor
) {
305 /* write the template message before editing: */
306 fd
= xopen(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
308 if (opt
->message_given
) {
309 write_or_die(fd
, buf
->buf
, buf
->len
);
311 } else if (!is_null_oid(prev
)) {
312 write_tag_body(fd
, prev
);
314 struct strbuf buf
= STRBUF_INIT
;
315 strbuf_addch(&buf
, '\n');
316 if (opt
->cleanup_mode
== CLEANUP_ALL
)
317 strbuf_commented_addf(&buf
, comment_line_char
,
318 _(tag_template
), tag
, comment_line_char
);
320 strbuf_commented_addf(&buf
, comment_line_char
,
321 _(tag_template_nocleanup
), tag
, comment_line_char
);
322 write_or_die(fd
, buf
.buf
, buf
.len
);
323 strbuf_release(&buf
);
327 if (launch_editor(path
, buf
, NULL
)) {
329 _("Please supply the message using either -m or -F option.\n"));
334 if (opt
->cleanup_mode
!= CLEANUP_NONE
)
335 strbuf_stripspace(buf
,
336 opt
->cleanup_mode
== CLEANUP_ALL
? comment_line_char
: '\0');
338 if (!opt
->message_given
&& !buf
->len
)
339 die(_("no tag message?"));
341 strbuf_insert(buf
, 0, header
.buf
, header
.len
);
342 strbuf_release(&header
);
344 if (build_tag_object(buf
, opt
->sign
, result
) < 0) {
346 fprintf(stderr
, _("The tag message has been left in %s\n"),
352 static void create_reflog_msg(const struct object_id
*oid
, struct strbuf
*sb
)
354 enum object_type type
;
359 const char *subject_start
;
361 char *rla
= getenv("GIT_REFLOG_ACTION");
363 strbuf_addstr(sb
, rla
);
365 strbuf_addstr(sb
, "tag: tagging ");
366 strbuf_add_unique_abbrev(sb
, oid
, DEFAULT_ABBREV
);
369 strbuf_addstr(sb
, " (");
370 type
= oid_object_info(the_repository
, oid
, NULL
);
373 strbuf_addstr(sb
, "object of unknown type");
376 if ((buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
))) {
377 subject_len
= find_commit_subject(buf
, &subject_start
);
378 strbuf_insert(sb
, sb
->len
, subject_start
, subject_len
);
380 strbuf_addstr(sb
, "commit object");
384 if ((c
= lookup_commit_reference(the_repository
, oid
)))
385 strbuf_addf(sb
, ", %s", show_date(c
->date
, 0, DATE_MODE(SHORT
)));
388 strbuf_addstr(sb
, "tree object");
391 strbuf_addstr(sb
, "blob object");
394 strbuf_addstr(sb
, "other tag object");
397 strbuf_addch(sb
, ')');
405 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
407 struct msg_arg
*msg
= opt
->value
;
409 BUG_ON_OPT_NEG(unset
);
414 strbuf_addstr(&(msg
->buf
), "\n\n");
415 strbuf_addstr(&(msg
->buf
), arg
);
420 static int strbuf_check_tag_ref(struct strbuf
*sb
, const char *name
)
426 strbuf_addf(sb
, "refs/tags/%s", name
);
428 return check_refname_format(sb
->buf
, 0);
431 int cmd_tag(int argc
, const char **argv
, const char *prefix
)
433 struct strbuf buf
= STRBUF_INIT
;
434 struct strbuf ref
= STRBUF_INIT
;
435 struct strbuf reflog_msg
= STRBUF_INIT
;
436 struct object_id object
, prev
;
437 const char *object_ref
, *tag
;
438 struct create_tag_options opt
;
439 char *cleanup_arg
= NULL
;
440 int create_reflog
= 0;
441 int annotate
= 0, force
= 0;
442 int cmdmode
= 0, create_tag_object
= 0;
443 char *msgfile
= NULL
;
444 const char *keyid
= NULL
;
445 struct msg_arg msg
= { .buf
= STRBUF_INIT
};
446 struct ref_transaction
*transaction
;
447 struct strbuf err
= STRBUF_INIT
;
448 struct ref_filter filter
= REF_FILTER_INIT
;
449 struct ref_sorting
*sorting
;
450 struct string_list sorting_options
= STRING_LIST_INIT_DUP
;
451 struct ref_format format
= REF_FORMAT_INIT
;
454 struct option options
[] = {
455 OPT_CMDMODE('l', "list", &cmdmode
, N_("list tag names"), 'l'),
456 { OPTION_INTEGER
, 'n', NULL
, &filter
.lines
, N_("n"),
457 N_("print <n> lines of each tag message"),
458 PARSE_OPT_OPTARG
, NULL
, 1 },
459 OPT_CMDMODE('d', "delete", &cmdmode
, N_("delete tags"), 'd'),
460 OPT_CMDMODE('v', "verify", &cmdmode
, N_("verify tags"), 'v'),
462 OPT_GROUP(N_("Tag creation options")),
463 OPT_BOOL('a', "annotate", &annotate
,
464 N_("annotated tag, needs a message")),
465 OPT_CALLBACK_F('m', "message", &msg
, N_("message"),
466 N_("tag message"), PARSE_OPT_NONEG
, parse_msg_arg
),
467 OPT_FILENAME('F', "file", &msgfile
, N_("read message from file")),
468 OPT_BOOL('e', "edit", &edit_flag
, N_("force edit of tag message")),
469 OPT_BOOL('s', "sign", &opt
.sign
, N_("annotated and GPG-signed tag")),
470 OPT_CLEANUP(&cleanup_arg
),
471 OPT_STRING('u', "local-user", &keyid
, N_("key-id"),
472 N_("use another key to sign the tag")),
473 OPT__FORCE(&force
, N_("replace the tag if exists"), 0),
474 OPT_BOOL(0, "create-reflog", &create_reflog
, N_("create a reflog")),
476 OPT_GROUP(N_("Tag listing options")),
477 OPT_COLUMN(0, "column", &colopts
, N_("show tag list in columns")),
478 OPT_CONTAINS(&filter
.with_commit
, N_("print only tags that contain the commit")),
479 OPT_NO_CONTAINS(&filter
.no_commit
, N_("print only tags that don't contain the commit")),
480 OPT_WITH(&filter
.with_commit
, N_("print only tags that contain the commit")),
481 OPT_WITHOUT(&filter
.no_commit
, N_("print only tags that don't contain the commit")),
482 OPT_MERGED(&filter
, N_("print only tags that are merged")),
483 OPT_NO_MERGED(&filter
, N_("print only tags that are not merged")),
484 OPT_BOOL(0, "omit-empty", &omit_empty
,
485 N_("do not output a newline after empty formatted refs")),
486 OPT_REF_SORT(&sorting_options
),
488 OPTION_CALLBACK
, 0, "points-at", &filter
.points_at
, N_("object"),
489 N_("print only tags of the object"), PARSE_OPT_LASTARG_DEFAULT
,
490 parse_opt_object_name
, (intptr_t) "HEAD"
492 OPT_STRING( 0 , "format", &format
.format
, N_("format"),
493 N_("format to use for the output")),
494 OPT__COLOR(&format
.use_color
, N_("respect format colors")),
495 OPT_BOOL('i', "ignore-case", &icase
, N_("sorting and filtering are case insensitive")),
499 const char *only_in_list
= NULL
;
502 setup_ref_filter_porcelain_msg();
504 git_config(git_tag_config
, &sorting_options
);
506 memset(&opt
, 0, sizeof(opt
));
510 argc
= parse_options(argc
, argv
, prefix
, options
, git_tag_usage
, 0);
515 else if (filter
.with_commit
|| filter
.no_commit
||
516 filter
.reachable_from
|| filter
.unreachable_from
||
517 filter
.points_at
.nr
|| filter
.lines
!= -1)
522 setup_auto_pager("tag", 1);
525 opt
.sign
= cmdmode
? 0 : config_sign_tag
> 0;
529 set_signing_key(keyid
);
531 create_tag_object
= (opt
.sign
|| annotate
|| msg
.given
|| msgfile
);
533 if ((create_tag_object
|| force
) && (cmdmode
!= 0))
534 usage_with_options(git_tag_usage
, options
);
536 finalize_colopts(&colopts
, -1);
537 if (cmdmode
== 'l' && filter
.lines
!= -1) {
538 if (explicitly_enable_column(colopts
))
539 die(_("options '%s' and '%s' cannot be used together"), "--column", "-n");
542 sorting
= ref_sorting_options(&sorting_options
);
543 ref_sorting_set_sort_flags_all(sorting
, REF_SORTING_ICASE
, icase
);
544 filter
.ignore_case
= icase
;
545 if (cmdmode
== 'l') {
546 if (column_active(colopts
)) {
547 struct column_options copts
;
548 memset(&copts
, 0, sizeof(copts
));
550 run_column_filter(colopts
, &copts
);
552 filter
.name_patterns
= argv
;
553 ret
= list_tags(&filter
, sorting
, &format
);
554 if (column_active(colopts
))
555 stop_column_filter();
558 if (filter
.lines
!= -1)
560 else if (filter
.with_commit
)
561 only_in_list
= "--contains";
562 else if (filter
.no_commit
)
563 only_in_list
= "--no-contains";
564 else if (filter
.points_at
.nr
)
565 only_in_list
= "--points-at";
566 else if (filter
.reachable_from
)
567 only_in_list
= "--merged";
568 else if (filter
.unreachable_from
)
569 only_in_list
= "--no-merged";
571 die(_("the '%s' option is only allowed in list mode"), only_in_list
);
572 if (cmdmode
== 'd') {
573 ret
= delete_tags(argv
);
576 if (cmdmode
== 'v') {
577 if (format
.format
&& verify_ref_format(&format
))
578 usage_with_options(git_tag_usage
, options
);
579 ret
= for_each_tag_name(argv
, verify_tag
, &format
);
583 if (msg
.given
|| msgfile
) {
584 if (msg
.given
&& msgfile
)
585 die(_("options '%s' and '%s' cannot be used together"), "-F", "-m");
587 strbuf_addbuf(&buf
, &(msg
.buf
));
589 if (!strcmp(msgfile
, "-")) {
590 if (strbuf_read(&buf
, 0, 1024) < 0)
591 die_errno(_("cannot read '%s'"), msgfile
);
593 if (strbuf_read_file(&buf
, msgfile
, 1024) < 0)
594 die_errno(_("could not open or read '%s'"),
602 object_ref
= argc
== 2 ? argv
[1] : "HEAD";
604 die(_("too many arguments"));
606 if (repo_get_oid(the_repository
, object_ref
, &object
))
607 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
609 if (strbuf_check_tag_ref(&ref
, tag
))
610 die(_("'%s' is not a valid tag name."), tag
);
612 if (read_ref(ref
.buf
, &prev
))
615 die(_("tag '%s' already exists"), tag
);
617 opt
.message_given
= msg
.given
|| msgfile
;
618 opt
.use_editor
= edit_flag
;
620 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "strip"))
621 opt
.cleanup_mode
= CLEANUP_ALL
;
622 else if (!strcmp(cleanup_arg
, "verbatim"))
623 opt
.cleanup_mode
= CLEANUP_NONE
;
624 else if (!strcmp(cleanup_arg
, "whitespace"))
625 opt
.cleanup_mode
= CLEANUP_SPACE
;
627 die(_("Invalid cleanup mode %s"), cleanup_arg
);
629 create_reflog_msg(&object
, &reflog_msg
);
631 if (create_tag_object
) {
632 if (force_sign_annotate
&& !annotate
)
634 path
= git_pathdup("TAG_EDITMSG");
635 create_tag(&object
, object_ref
, tag
, &buf
, &opt
, &prev
, &object
,
639 transaction
= ref_transaction_begin(&err
);
641 ref_transaction_update(transaction
, ref
.buf
, &object
, &prev
,
642 create_reflog
? REF_FORCE_CREATE_REFLOG
: 0,
643 reflog_msg
.buf
, &err
) ||
644 ref_transaction_commit(transaction
, &err
)) {
647 _("The tag message has been left in %s\n"),
652 unlink_or_warn(path
);
655 ref_transaction_free(transaction
);
656 if (force
&& !is_null_oid(&prev
) && !oideq(&prev
, &object
))
657 printf(_("Updated tag '%s' (was %s)\n"), tag
,
658 repo_find_unique_abbrev(the_repository
, &prev
, DEFAULT_ABBREV
));
661 ref_sorting_release(sorting
);
662 ref_filter_clear(&filter
);
663 strbuf_release(&buf
);
664 strbuf_release(&ref
);
665 strbuf_release(&reflog_msg
);
666 strbuf_release(&msg
.buf
);
667 strbuf_release(&err
);