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 "object-store.h"
15 #include "run-command.h"
16 #include "parse-options.h"
19 #include "gpg-interface.h"
20 #include "oid-array.h"
22 #include "ref-filter.h"
24 static const char * const git_tag_usage
[] = {
25 N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>]\n"
26 " <tagname> [<head>]"),
27 N_("git tag -d <tagname>..."),
28 N_("git tag -l [-n[<num>]] [--contains <commit>] [--no-contains <commit>] [--points-at <object>]\n"
29 " [--format=<format>] [--merged <commit>] [--no-merged <commit>] [<pattern>...]"),
30 N_("git tag -v [--format=<format>] <tagname>..."),
34 static unsigned int colopts
;
35 static int force_sign_annotate
;
36 static int config_sign_tag
= -1; /* unspecified */
38 static int list_tags(struct ref_filter
*filter
, struct ref_sorting
*sorting
,
39 struct ref_format
*format
)
41 struct ref_array array
;
42 struct strbuf output
= STRBUF_INIT
;
43 struct strbuf err
= STRBUF_INIT
;
47 memset(&array
, 0, sizeof(array
));
49 if (filter
->lines
== -1)
52 if (!format
->format
) {
54 to_free
= xstrfmt("%s %%(contents:lines=%d)",
55 "%(align:15)%(refname:lstrip=2)%(end)",
57 format
->format
= to_free
;
59 format
->format
= "%(refname:lstrip=2)";
62 if (verify_ref_format(format
))
63 die(_("unable to parse format string"));
64 filter
->with_commit_tag_algo
= 1;
65 filter_refs(&array
, filter
, FILTER_REFS_TAGS
);
66 ref_array_sort(sorting
, &array
);
68 for (i
= 0; i
< array
.nr
; i
++) {
69 strbuf_reset(&output
);
71 if (format_ref_array_item(array
.items
[i
], format
, &output
, &err
))
73 fwrite(output
.buf
, 1, output
.len
, stdout
);
78 strbuf_release(&output
);
79 ref_array_clear(&array
);
85 typedef int (*each_tag_name_fn
)(const char *name
, const char *ref
,
86 const struct object_id
*oid
, void *cb_data
);
88 static int for_each_tag_name(const char **argv
, each_tag_name_fn fn
,
92 struct strbuf ref
= STRBUF_INIT
;
96 for (p
= argv
; *p
; p
++) {
98 strbuf_addf(&ref
, "refs/tags/%s", *p
);
99 if (read_ref(ref
.buf
, &oid
)) {
100 error(_("tag '%s' not found."), *p
);
104 if (fn(*p
, ref
.buf
, &oid
, cb_data
))
107 strbuf_release(&ref
);
111 static int collect_tags(const char *name
, const char *ref
,
112 const struct object_id
*oid
, void *cb_data
)
114 struct string_list
*ref_list
= cb_data
;
116 string_list_append(ref_list
, ref
);
117 ref_list
->items
[ref_list
->nr
- 1].util
= oiddup(oid
);
121 static int delete_tags(const char **argv
)
124 struct string_list refs_to_delete
= STRING_LIST_INIT_DUP
;
125 struct string_list_item
*item
;
127 result
= for_each_tag_name(argv
, collect_tags
, (void *)&refs_to_delete
);
128 if (delete_refs(NULL
, &refs_to_delete
, REF_NO_DEREF
))
131 for_each_string_list_item(item
, &refs_to_delete
) {
132 const char *name
= item
->string
;
133 struct object_id
*oid
= item
->util
;
134 if (!ref_exists(name
))
135 printf(_("Deleted tag '%s' (was %s)\n"),
137 find_unique_abbrev(oid
, DEFAULT_ABBREV
));
141 string_list_clear(&refs_to_delete
, 0);
145 static int verify_tag(const char *name
, const char *ref
,
146 const struct object_id
*oid
, void *cb_data
)
149 struct ref_format
*format
= cb_data
;
150 flags
= GPG_VERIFY_VERBOSE
;
153 flags
= GPG_VERIFY_OMIT_STATUS
;
155 if (gpg_verify_tag(oid
, name
, flags
))
159 pretty_print_ref(name
, oid
, format
);
164 static int do_sign(struct strbuf
*buffer
)
166 return sign_buffer(buffer
, buffer
, get_signing_key());
169 static const char tag_template
[] =
170 N_("\nWrite a message for tag:\n %s\n"
171 "Lines starting with '%c' will be ignored.\n");
173 static const char tag_template_nocleanup
[] =
174 N_("\nWrite a message for tag:\n %s\n"
175 "Lines starting with '%c' will be kept; you may remove them"
176 " yourself if you want to.\n");
178 static int git_tag_config(const char *var
, const char *value
, void *cb
)
182 if (!strcmp(var
, "tag.gpgsign")) {
183 config_sign_tag
= git_config_bool(var
, value
);
187 if (!strcmp(var
, "tag.sort")) {
189 return config_error_nonbool(var
);
190 string_list_append(cb
, value
);
194 status
= git_gpg_config(var
, value
, cb
);
197 if (!strcmp(var
, "tag.forcesignannotated")) {
198 force_sign_annotate
= git_config_bool(var
, value
);
202 if (starts_with(var
, "column."))
203 return git_column_config(var
, value
, "tag", &colopts
);
204 return git_color_default_config(var
, value
, cb
);
207 static void write_tag_body(int fd
, const struct object_id
*oid
)
210 enum object_type type
;
211 char *buf
, *sp
, *orig
;
212 struct strbuf payload
= STRBUF_INIT
;
213 struct strbuf signature
= STRBUF_INIT
;
215 orig
= buf
= read_object_file(oid
, &type
, &size
);
218 if (parse_signature(buf
, size
, &payload
, &signature
)) {
223 sp
= strstr(buf
, "\n\n");
225 if (!sp
|| !size
|| type
!= OBJ_TAG
) {
229 sp
+= 2; /* skip the 2 LFs */
230 write_or_die(fd
, sp
, buf
+ size
- sp
);
233 strbuf_release(&payload
);
234 strbuf_release(&signature
);
237 static int build_tag_object(struct strbuf
*buf
, int sign
, struct object_id
*result
)
239 if (sign
&& do_sign(buf
) < 0)
240 return error(_("unable to sign the tag"));
241 if (write_object_file(buf
->buf
, buf
->len
, tag_type
, result
) < 0)
242 return error(_("unable to write tag file"));
246 struct create_tag_options
{
247 unsigned int message_given
:1;
248 unsigned int use_editor
:1;
257 static const char message_advice_nested_tag
[] =
258 N_("You have created a nested tag. The object referred to by your new tag is\n"
259 "already a tag. If you meant to tag the object that it points to, use:\n"
261 "\tgit tag -f %s %s^{}");
263 static void create_tag(const struct object_id
*object
, const char *object_ref
,
265 struct strbuf
*buf
, struct create_tag_options
*opt
,
266 struct object_id
*prev
, struct object_id
*result
)
268 enum object_type type
;
269 struct strbuf header
= STRBUF_INIT
;
272 type
= oid_object_info(the_repository
, object
, NULL
);
273 if (type
<= OBJ_NONE
)
274 die(_("bad object type."));
277 advise_if_enabled(ADVICE_NESTED_TAG
, _(message_advice_nested_tag
),
288 git_committer_info(IDENT_STRICT
));
290 if (!opt
->message_given
|| opt
->use_editor
) {
293 /* write the template message before editing: */
294 path
= git_pathdup("TAG_EDITMSG");
295 fd
= xopen(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
297 if (opt
->message_given
) {
298 write_or_die(fd
, buf
->buf
, buf
->len
);
300 } else if (!is_null_oid(prev
)) {
301 write_tag_body(fd
, prev
);
303 struct strbuf buf
= STRBUF_INIT
;
304 strbuf_addch(&buf
, '\n');
305 if (opt
->cleanup_mode
== CLEANUP_ALL
)
306 strbuf_commented_addf(&buf
, _(tag_template
), tag
, comment_line_char
);
308 strbuf_commented_addf(&buf
, _(tag_template_nocleanup
), tag
, comment_line_char
);
309 write_or_die(fd
, buf
.buf
, buf
.len
);
310 strbuf_release(&buf
);
314 if (launch_editor(path
, buf
, NULL
)) {
316 _("Please supply the message using either -m or -F option.\n"));
321 if (opt
->cleanup_mode
!= CLEANUP_NONE
)
322 strbuf_stripspace(buf
, opt
->cleanup_mode
== CLEANUP_ALL
);
324 if (!opt
->message_given
&& !buf
->len
)
325 die(_("no tag message?"));
327 strbuf_insert(buf
, 0, header
.buf
, header
.len
);
328 strbuf_release(&header
);
330 if (build_tag_object(buf
, opt
->sign
, result
) < 0) {
332 fprintf(stderr
, _("The tag message has been left in %s\n"),
337 unlink_or_warn(path
);
342 static void create_reflog_msg(const struct object_id
*oid
, struct strbuf
*sb
)
344 enum object_type type
;
349 const char *subject_start
;
351 char *rla
= getenv("GIT_REFLOG_ACTION");
353 strbuf_addstr(sb
, rla
);
355 strbuf_addstr(sb
, "tag: tagging ");
356 strbuf_add_unique_abbrev(sb
, oid
, DEFAULT_ABBREV
);
359 strbuf_addstr(sb
, " (");
360 type
= oid_object_info(the_repository
, oid
, NULL
);
363 strbuf_addstr(sb
, "object of unknown type");
366 if ((buf
= read_object_file(oid
, &type
, &size
)) != NULL
) {
367 subject_len
= find_commit_subject(buf
, &subject_start
);
368 strbuf_insert(sb
, sb
->len
, subject_start
, subject_len
);
370 strbuf_addstr(sb
, "commit object");
374 if ((c
= lookup_commit_reference(the_repository
, oid
)) != NULL
)
375 strbuf_addf(sb
, ", %s", show_date(c
->date
, 0, DATE_MODE(SHORT
)));
378 strbuf_addstr(sb
, "tree object");
381 strbuf_addstr(sb
, "blob object");
384 strbuf_addstr(sb
, "other tag object");
387 strbuf_addch(sb
, ')');
395 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
397 struct msg_arg
*msg
= opt
->value
;
399 BUG_ON_OPT_NEG(unset
);
404 strbuf_addstr(&(msg
->buf
), "\n\n");
405 strbuf_addstr(&(msg
->buf
), arg
);
410 static int strbuf_check_tag_ref(struct strbuf
*sb
, const char *name
)
416 strbuf_addf(sb
, "refs/tags/%s", name
);
418 return check_refname_format(sb
->buf
, 0);
421 int cmd_tag(int argc
, const char **argv
, const char *prefix
)
423 struct strbuf buf
= STRBUF_INIT
;
424 struct strbuf ref
= STRBUF_INIT
;
425 struct strbuf reflog_msg
= STRBUF_INIT
;
426 struct object_id object
, prev
;
427 const char *object_ref
, *tag
;
428 struct create_tag_options opt
;
429 char *cleanup_arg
= NULL
;
430 int create_reflog
= 0;
431 int annotate
= 0, force
= 0;
432 int cmdmode
= 0, create_tag_object
= 0;
433 const char *msgfile
= NULL
, *keyid
= NULL
;
434 struct msg_arg msg
= { .buf
= STRBUF_INIT
};
435 struct ref_transaction
*transaction
;
436 struct strbuf err
= STRBUF_INIT
;
437 struct ref_filter filter
;
438 struct ref_sorting
*sorting
;
439 struct string_list sorting_options
= STRING_LIST_INIT_DUP
;
440 struct ref_format format
= REF_FORMAT_INIT
;
443 struct option options
[] = {
444 OPT_CMDMODE('l', "list", &cmdmode
, N_("list tag names"), 'l'),
445 { OPTION_INTEGER
, 'n', NULL
, &filter
.lines
, N_("n"),
446 N_("print <n> lines of each tag message"),
447 PARSE_OPT_OPTARG
, NULL
, 1 },
448 OPT_CMDMODE('d', "delete", &cmdmode
, N_("delete tags"), 'd'),
449 OPT_CMDMODE('v', "verify", &cmdmode
, N_("verify tags"), 'v'),
451 OPT_GROUP(N_("Tag creation options")),
452 OPT_BOOL('a', "annotate", &annotate
,
453 N_("annotated tag, needs a message")),
454 OPT_CALLBACK_F('m', "message", &msg
, N_("message"),
455 N_("tag message"), PARSE_OPT_NONEG
, parse_msg_arg
),
456 OPT_FILENAME('F', "file", &msgfile
, N_("read message from file")),
457 OPT_BOOL('e', "edit", &edit_flag
, N_("force edit of tag message")),
458 OPT_BOOL('s', "sign", &opt
.sign
, N_("annotated and GPG-signed tag")),
459 OPT_CLEANUP(&cleanup_arg
),
460 OPT_STRING('u', "local-user", &keyid
, N_("key-id"),
461 N_("use another key to sign the tag")),
462 OPT__FORCE(&force
, N_("replace the tag if exists"), 0),
463 OPT_BOOL(0, "create-reflog", &create_reflog
, N_("create a reflog")),
465 OPT_GROUP(N_("Tag listing options")),
466 OPT_COLUMN(0, "column", &colopts
, N_("show tag list in columns")),
467 OPT_CONTAINS(&filter
.with_commit
, N_("print only tags that contain the commit")),
468 OPT_NO_CONTAINS(&filter
.no_commit
, N_("print only tags that don't contain the commit")),
469 OPT_WITH(&filter
.with_commit
, N_("print only tags that contain the commit")),
470 OPT_WITHOUT(&filter
.no_commit
, N_("print only tags that don't contain the commit")),
471 OPT_MERGED(&filter
, N_("print only tags that are merged")),
472 OPT_NO_MERGED(&filter
, N_("print only tags that are not merged")),
473 OPT_REF_SORT(&sorting_options
),
475 OPTION_CALLBACK
, 0, "points-at", &filter
.points_at
, N_("object"),
476 N_("print only tags of the object"), PARSE_OPT_LASTARG_DEFAULT
,
477 parse_opt_object_name
, (intptr_t) "HEAD"
479 OPT_STRING( 0 , "format", &format
.format
, N_("format"),
480 N_("format to use for the output")),
481 OPT__COLOR(&format
.use_color
, N_("respect format colors")),
482 OPT_BOOL('i', "ignore-case", &icase
, N_("sorting and filtering are case insensitive")),
487 setup_ref_filter_porcelain_msg();
489 git_config(git_tag_config
, &sorting_options
);
491 memset(&opt
, 0, sizeof(opt
));
492 memset(&filter
, 0, sizeof(filter
));
496 argc
= parse_options(argc
, argv
, prefix
, options
, git_tag_usage
, 0);
501 else if (filter
.with_commit
|| filter
.no_commit
||
502 filter
.reachable_from
|| filter
.unreachable_from
||
503 filter
.points_at
.nr
|| filter
.lines
!= -1)
508 setup_auto_pager("tag", 1);
511 opt
.sign
= cmdmode
? 0 : config_sign_tag
> 0;
515 set_signing_key(keyid
);
517 create_tag_object
= (opt
.sign
|| annotate
|| msg
.given
|| msgfile
);
519 if ((create_tag_object
|| force
) && (cmdmode
!= 0))
520 usage_with_options(git_tag_usage
, options
);
522 finalize_colopts(&colopts
, -1);
523 if (cmdmode
== 'l' && filter
.lines
!= -1) {
524 if (explicitly_enable_column(colopts
))
525 die(_("--column and -n are incompatible"));
528 sorting
= ref_sorting_options(&sorting_options
);
529 ref_sorting_set_sort_flags_all(sorting
, REF_SORTING_ICASE
, icase
);
530 filter
.ignore_case
= icase
;
531 if (cmdmode
== 'l') {
532 if (column_active(colopts
)) {
533 struct column_options copts
;
534 memset(&copts
, 0, sizeof(copts
));
536 run_column_filter(colopts
, &copts
);
538 filter
.name_patterns
= argv
;
539 ret
= list_tags(&filter
, sorting
, &format
);
540 if (column_active(colopts
))
541 stop_column_filter();
544 if (filter
.lines
!= -1)
545 die(_("-n option is only allowed in list mode"));
546 if (filter
.with_commit
)
547 die(_("--contains option is only allowed in list mode"));
548 if (filter
.no_commit
)
549 die(_("--no-contains option is only allowed in list mode"));
550 if (filter
.points_at
.nr
)
551 die(_("--points-at option is only allowed in list mode"));
552 if (filter
.reachable_from
|| filter
.unreachable_from
)
553 die(_("--merged and --no-merged options are only allowed in list mode"));
554 if (cmdmode
== 'd') {
555 ret
= delete_tags(argv
);
558 if (cmdmode
== 'v') {
559 if (format
.format
&& verify_ref_format(&format
))
560 usage_with_options(git_tag_usage
, options
);
561 ret
= for_each_tag_name(argv
, verify_tag
, &format
);
565 if (msg
.given
|| msgfile
) {
566 if (msg
.given
&& msgfile
)
567 die(_("only one -F or -m option is allowed."));
569 strbuf_addbuf(&buf
, &(msg
.buf
));
571 if (!strcmp(msgfile
, "-")) {
572 if (strbuf_read(&buf
, 0, 1024) < 0)
573 die_errno(_("cannot read '%s'"), msgfile
);
575 if (strbuf_read_file(&buf
, msgfile
, 1024) < 0)
576 die_errno(_("could not open or read '%s'"),
584 object_ref
= argc
== 2 ? argv
[1] : "HEAD";
586 die(_("too many arguments"));
588 if (get_oid(object_ref
, &object
))
589 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
591 if (strbuf_check_tag_ref(&ref
, tag
))
592 die(_("'%s' is not a valid tag name."), tag
);
594 if (read_ref(ref
.buf
, &prev
))
597 die(_("tag '%s' already exists"), tag
);
599 opt
.message_given
= msg
.given
|| msgfile
;
600 opt
.use_editor
= edit_flag
;
602 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "strip"))
603 opt
.cleanup_mode
= CLEANUP_ALL
;
604 else if (!strcmp(cleanup_arg
, "verbatim"))
605 opt
.cleanup_mode
= CLEANUP_NONE
;
606 else if (!strcmp(cleanup_arg
, "whitespace"))
607 opt
.cleanup_mode
= CLEANUP_SPACE
;
609 die(_("Invalid cleanup mode %s"), cleanup_arg
);
611 create_reflog_msg(&object
, &reflog_msg
);
613 if (create_tag_object
) {
614 if (force_sign_annotate
&& !annotate
)
616 create_tag(&object
, object_ref
, tag
, &buf
, &opt
, &prev
, &object
);
619 transaction
= ref_transaction_begin(&err
);
621 ref_transaction_update(transaction
, ref
.buf
, &object
, &prev
,
622 create_reflog
? REF_FORCE_CREATE_REFLOG
: 0,
623 reflog_msg
.buf
, &err
) ||
624 ref_transaction_commit(transaction
, &err
))
626 ref_transaction_free(transaction
);
627 if (force
&& !is_null_oid(&prev
) && !oideq(&prev
, &object
))
628 printf(_("Updated tag '%s' (was %s)\n"), tag
,
629 find_unique_abbrev(&prev
, DEFAULT_ABBREV
));
632 ref_sorting_release(sorting
);
633 strbuf_release(&buf
);
634 strbuf_release(&ref
);
635 strbuf_release(&reflog_msg
);
636 strbuf_release(&msg
.buf
);
637 strbuf_release(&err
);