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.
14 #include "run-command.h"
15 #include "parse-options.h"
18 #include "gpg-interface.h"
19 #include "sha1-array.h"
21 #include "ref-filter.h"
23 static const char * const git_tag_usage
[] = {
24 N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>]"),
25 N_("git tag -d <tagname>..."),
26 N_("git tag -l [-n[<num>]] [--contains <commit>] [--no-contains <commit>] [--points-at <object>]"
27 "\n\t\t[--format=<format>] [--[no-]merged [<commit>]] [<pattern>...]"),
28 N_("git tag -v [--format=<format>] <tagname>..."),
32 static unsigned int colopts
;
33 static int force_sign_annotate
;
35 static int list_tags(struct ref_filter
*filter
, struct ref_sorting
*sorting
,
36 struct ref_format
*format
)
38 struct ref_array array
;
42 memset(&array
, 0, sizeof(array
));
44 if (filter
->lines
== -1)
47 if (!format
->format
) {
49 to_free
= xstrfmt("%s %%(contents:lines=%d)",
50 "%(align:15)%(refname:lstrip=2)%(end)",
52 format
->format
= to_free
;
54 format
->format
= "%(refname:lstrip=2)";
57 if (verify_ref_format(format
))
58 die(_("unable to parse format string"));
59 filter
->with_commit_tag_algo
= 1;
60 filter_refs(&array
, filter
, FILTER_REFS_TAGS
);
61 ref_array_sort(sorting
, &array
);
63 for (i
= 0; i
< array
.nr
; i
++)
64 show_ref_array_item(array
.items
[i
], format
);
65 ref_array_clear(&array
);
71 typedef int (*each_tag_name_fn
)(const char *name
, const char *ref
,
72 const struct object_id
*oid
, const void *cb_data
);
74 static int for_each_tag_name(const char **argv
, each_tag_name_fn fn
,
78 struct strbuf ref
= STRBUF_INIT
;
82 for (p
= argv
; *p
; p
++) {
84 strbuf_addf(&ref
, "refs/tags/%s", *p
);
85 if (read_ref(ref
.buf
, &oid
)) {
86 error(_("tag '%s' not found."), *p
);
90 if (fn(*p
, ref
.buf
, &oid
, cb_data
))
97 static int delete_tag(const char *name
, const char *ref
,
98 const struct object_id
*oid
, const void *cb_data
)
100 if (delete_ref(NULL
, ref
, oid
, 0))
102 printf(_("Deleted tag '%s' (was %s)\n"), name
,
103 find_unique_abbrev(oid
, DEFAULT_ABBREV
));
107 static int verify_tag(const char *name
, const char *ref
,
108 const struct object_id
*oid
, const void *cb_data
)
111 const struct ref_format
*format
= cb_data
;
112 flags
= GPG_VERIFY_VERBOSE
;
115 flags
= GPG_VERIFY_OMIT_STATUS
;
117 if (gpg_verify_tag(oid
, name
, flags
))
121 pretty_print_ref(name
, oid
, format
);
126 static int do_sign(struct strbuf
*buffer
)
128 return sign_buffer(buffer
, buffer
, get_signing_key());
131 static const char tag_template
[] =
132 N_("\nWrite a message for tag:\n %s\n"
133 "Lines starting with '%c' will be ignored.\n");
135 static const char tag_template_nocleanup
[] =
136 N_("\nWrite a message for tag:\n %s\n"
137 "Lines starting with '%c' will be kept; you may remove them"
138 " yourself if you want to.\n");
140 static int git_tag_config(const char *var
, const char *value
, void *cb
)
143 struct ref_sorting
**sorting_tail
= (struct ref_sorting
**)cb
;
145 if (!strcmp(var
, "tag.sort")) {
147 return config_error_nonbool(var
);
148 parse_ref_sorting(sorting_tail
, value
);
152 status
= git_gpg_config(var
, value
, cb
);
155 if (!strcmp(var
, "tag.forcesignannotated")) {
156 force_sign_annotate
= git_config_bool(var
, value
);
160 if (starts_with(var
, "column."))
161 return git_column_config(var
, value
, "tag", &colopts
);
162 return git_color_default_config(var
, value
, cb
);
165 static void write_tag_body(int fd
, const struct object_id
*oid
)
168 enum object_type type
;
171 buf
= read_object_file(oid
, &type
, &size
);
175 sp
= strstr(buf
, "\n\n");
177 if (!sp
|| !size
|| type
!= OBJ_TAG
) {
181 sp
+= 2; /* skip the 2 LFs */
182 write_or_die(fd
, sp
, parse_signature(sp
, buf
+ size
- sp
));
187 static int build_tag_object(struct strbuf
*buf
, int sign
, struct object_id
*result
)
189 if (sign
&& do_sign(buf
) < 0)
190 return error(_("unable to sign the tag"));
191 if (write_object_file(buf
->buf
, buf
->len
, tag_type
, result
) < 0)
192 return error(_("unable to write tag file"));
196 struct create_tag_options
{
197 unsigned int message_given
:1;
198 unsigned int use_editor
:1;
207 static void create_tag(const struct object_id
*object
, const char *tag
,
208 struct strbuf
*buf
, struct create_tag_options
*opt
,
209 struct object_id
*prev
, struct object_id
*result
)
211 enum object_type type
;
212 struct strbuf header
= STRBUF_INIT
;
215 type
= oid_object_info(the_repository
, object
, NULL
);
216 if (type
<= OBJ_NONE
)
217 die(_("bad object type."));
227 git_committer_info(IDENT_STRICT
));
229 if (!opt
->message_given
|| opt
->use_editor
) {
232 /* write the template message before editing: */
233 path
= git_pathdup("TAG_EDITMSG");
234 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
236 die_errno(_("could not create file '%s'"), path
);
238 if (opt
->message_given
) {
239 write_or_die(fd
, buf
->buf
, buf
->len
);
241 } else if (!is_null_oid(prev
)) {
242 write_tag_body(fd
, prev
);
244 struct strbuf buf
= STRBUF_INIT
;
245 strbuf_addch(&buf
, '\n');
246 if (opt
->cleanup_mode
== CLEANUP_ALL
)
247 strbuf_commented_addf(&buf
, _(tag_template
), tag
, comment_line_char
);
249 strbuf_commented_addf(&buf
, _(tag_template_nocleanup
), tag
, comment_line_char
);
250 write_or_die(fd
, buf
.buf
, buf
.len
);
251 strbuf_release(&buf
);
255 if (launch_editor(path
, buf
, NULL
)) {
257 _("Please supply the message using either -m or -F option.\n"));
262 if (opt
->cleanup_mode
!= CLEANUP_NONE
)
263 strbuf_stripspace(buf
, opt
->cleanup_mode
== CLEANUP_ALL
);
265 if (!opt
->message_given
&& !buf
->len
)
266 die(_("no tag message?"));
268 strbuf_insert(buf
, 0, header
.buf
, header
.len
);
269 strbuf_release(&header
);
271 if (build_tag_object(buf
, opt
->sign
, result
) < 0) {
273 fprintf(stderr
, _("The tag message has been left in %s\n"),
278 unlink_or_warn(path
);
283 static void create_reflog_msg(const struct object_id
*oid
, struct strbuf
*sb
)
285 enum object_type type
;
290 const char *subject_start
;
292 char *rla
= getenv("GIT_REFLOG_ACTION");
294 strbuf_addstr(sb
, rla
);
296 strbuf_addstr(sb
, "tag: tagging ");
297 strbuf_add_unique_abbrev(sb
, oid
, DEFAULT_ABBREV
);
300 strbuf_addstr(sb
, " (");
301 type
= oid_object_info(the_repository
, oid
, NULL
);
304 strbuf_addstr(sb
, "object of unknown type");
307 if ((buf
= read_object_file(oid
, &type
, &size
)) != NULL
) {
308 subject_len
= find_commit_subject(buf
, &subject_start
);
309 strbuf_insert(sb
, sb
->len
, subject_start
, subject_len
);
311 strbuf_addstr(sb
, "commit object");
315 if ((c
= lookup_commit_reference(oid
)) != NULL
)
316 strbuf_addf(sb
, ", %s", show_date(c
->date
, 0, DATE_MODE(SHORT
)));
319 strbuf_addstr(sb
, "tree object");
322 strbuf_addstr(sb
, "blob object");
325 strbuf_addstr(sb
, "other tag object");
328 strbuf_addch(sb
, ')');
336 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
338 struct msg_arg
*msg
= opt
->value
;
343 strbuf_addstr(&(msg
->buf
), "\n\n");
344 strbuf_addstr(&(msg
->buf
), arg
);
349 static int strbuf_check_tag_ref(struct strbuf
*sb
, const char *name
)
355 strbuf_addf(sb
, "refs/tags/%s", name
);
357 return check_refname_format(sb
->buf
, 0);
360 int cmd_tag(int argc
, const char **argv
, const char *prefix
)
362 struct strbuf buf
= STRBUF_INIT
;
363 struct strbuf ref
= STRBUF_INIT
;
364 struct strbuf reflog_msg
= STRBUF_INIT
;
365 struct object_id object
, prev
;
366 const char *object_ref
, *tag
;
367 struct create_tag_options opt
;
368 char *cleanup_arg
= NULL
;
369 int create_reflog
= 0;
370 int annotate
= 0, force
= 0;
371 int cmdmode
= 0, create_tag_object
= 0;
372 const char *msgfile
= NULL
, *keyid
= NULL
;
373 struct msg_arg msg
= { 0, STRBUF_INIT
};
374 struct ref_transaction
*transaction
;
375 struct strbuf err
= STRBUF_INIT
;
376 struct ref_filter filter
;
377 static struct ref_sorting
*sorting
= NULL
, **sorting_tail
= &sorting
;
378 struct ref_format format
= REF_FORMAT_INIT
;
381 struct option options
[] = {
382 OPT_CMDMODE('l', "list", &cmdmode
, N_("list tag names"), 'l'),
383 { OPTION_INTEGER
, 'n', NULL
, &filter
.lines
, N_("n"),
384 N_("print <n> lines of each tag message"),
385 PARSE_OPT_OPTARG
, NULL
, 1 },
386 OPT_CMDMODE('d', "delete", &cmdmode
, N_("delete tags"), 'd'),
387 OPT_CMDMODE('v', "verify", &cmdmode
, N_("verify tags"), 'v'),
389 OPT_GROUP(N_("Tag creation options")),
390 OPT_BOOL('a', "annotate", &annotate
,
391 N_("annotated tag, needs a message")),
392 OPT_CALLBACK('m', "message", &msg
, N_("message"),
393 N_("tag message"), parse_msg_arg
),
394 OPT_FILENAME('F', "file", &msgfile
, N_("read message from file")),
395 OPT_BOOL('e', "edit", &edit_flag
, N_("force edit of tag message")),
396 OPT_BOOL('s', "sign", &opt
.sign
, N_("annotated and GPG-signed tag")),
397 OPT_STRING(0, "cleanup", &cleanup_arg
, N_("mode"),
398 N_("how to strip spaces and #comments from message")),
399 OPT_STRING('u', "local-user", &keyid
, N_("key-id"),
400 N_("use another key to sign the tag")),
401 OPT__FORCE(&force
, N_("replace the tag if exists"), 0),
402 OPT_BOOL(0, "create-reflog", &create_reflog
, N_("create a reflog")),
404 OPT_GROUP(N_("Tag listing options")),
405 OPT_COLUMN(0, "column", &colopts
, N_("show tag list in columns")),
406 OPT_CONTAINS(&filter
.with_commit
, N_("print only tags that contain the commit")),
407 OPT_NO_CONTAINS(&filter
.no_commit
, N_("print only tags that don't contain the commit")),
408 OPT_WITH(&filter
.with_commit
, N_("print only tags that contain the commit")),
409 OPT_WITHOUT(&filter
.no_commit
, N_("print only tags that don't contain the commit")),
410 OPT_MERGED(&filter
, N_("print only tags that are merged")),
411 OPT_NO_MERGED(&filter
, N_("print only tags that are not merged")),
412 OPT_CALLBACK(0 , "sort", sorting_tail
, N_("key"),
413 N_("field name to sort on"), &parse_opt_ref_sorting
),
415 OPTION_CALLBACK
, 0, "points-at", &filter
.points_at
, N_("object"),
416 N_("print only tags of the object"), PARSE_OPT_LASTARG_DEFAULT
,
417 parse_opt_object_name
, (intptr_t) "HEAD"
419 OPT_STRING( 0 , "format", &format
.format
, N_("format"),
420 N_("format to use for the output")),
421 OPT__COLOR(&format
.use_color
, N_("respect format colors")),
422 OPT_BOOL('i', "ignore-case", &icase
, N_("sorting and filtering are case insensitive")),
426 setup_ref_filter_porcelain_msg();
428 git_config(git_tag_config
, sorting_tail
);
430 memset(&opt
, 0, sizeof(opt
));
431 memset(&filter
, 0, sizeof(filter
));
434 argc
= parse_options(argc
, argv
, prefix
, options
, git_tag_usage
, 0);
438 set_signing_key(keyid
);
440 create_tag_object
= (opt
.sign
|| annotate
|| msg
.given
|| msgfile
);
445 else if (filter
.with_commit
|| filter
.no_commit
||
446 filter
.points_at
.nr
|| filter
.merge_commit
||
452 setup_auto_pager("tag", 1);
454 if ((create_tag_object
|| force
) && (cmdmode
!= 0))
455 usage_with_options(git_tag_usage
, options
);
457 finalize_colopts(&colopts
, -1);
458 if (cmdmode
== 'l' && filter
.lines
!= -1) {
459 if (explicitly_enable_column(colopts
))
460 die(_("--column and -n are incompatible"));
464 sorting
= ref_default_sorting();
465 sorting
->ignore_case
= icase
;
466 filter
.ignore_case
= icase
;
467 if (cmdmode
== 'l') {
469 if (column_active(colopts
)) {
470 struct column_options copts
;
471 memset(&copts
, 0, sizeof(copts
));
473 run_column_filter(colopts
, &copts
);
475 filter
.name_patterns
= argv
;
476 ret
= list_tags(&filter
, sorting
, &format
);
477 if (column_active(colopts
))
478 stop_column_filter();
481 if (filter
.lines
!= -1)
482 die(_("-n option is only allowed in list mode"));
483 if (filter
.with_commit
)
484 die(_("--contains option is only allowed in list mode"));
485 if (filter
.no_commit
)
486 die(_("--no-contains option is only allowed in list mode"));
487 if (filter
.points_at
.nr
)
488 die(_("--points-at option is only allowed in list mode"));
489 if (filter
.merge_commit
)
490 die(_("--merged and --no-merged options are only allowed in list mode"));
492 return for_each_tag_name(argv
, delete_tag
, NULL
);
493 if (cmdmode
== 'v') {
494 if (format
.format
&& verify_ref_format(&format
))
495 usage_with_options(git_tag_usage
, options
);
496 return for_each_tag_name(argv
, verify_tag
, &format
);
499 if (msg
.given
|| msgfile
) {
500 if (msg
.given
&& msgfile
)
501 die(_("only one -F or -m option is allowed."));
503 strbuf_addbuf(&buf
, &(msg
.buf
));
505 if (!strcmp(msgfile
, "-")) {
506 if (strbuf_read(&buf
, 0, 1024) < 0)
507 die_errno(_("cannot read '%s'"), msgfile
);
509 if (strbuf_read_file(&buf
, msgfile
, 1024) < 0)
510 die_errno(_("could not open or read '%s'"),
518 object_ref
= argc
== 2 ? argv
[1] : "HEAD";
520 die(_("too many params"));
522 if (get_oid(object_ref
, &object
))
523 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
525 if (strbuf_check_tag_ref(&ref
, tag
))
526 die(_("'%s' is not a valid tag name."), tag
);
528 if (read_ref(ref
.buf
, &prev
))
531 die(_("tag '%s' already exists"), tag
);
533 opt
.message_given
= msg
.given
|| msgfile
;
534 opt
.use_editor
= edit_flag
;
536 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "strip"))
537 opt
.cleanup_mode
= CLEANUP_ALL
;
538 else if (!strcmp(cleanup_arg
, "verbatim"))
539 opt
.cleanup_mode
= CLEANUP_NONE
;
540 else if (!strcmp(cleanup_arg
, "whitespace"))
541 opt
.cleanup_mode
= CLEANUP_SPACE
;
543 die(_("Invalid cleanup mode %s"), cleanup_arg
);
545 create_reflog_msg(&object
, &reflog_msg
);
547 if (create_tag_object
) {
548 if (force_sign_annotate
&& !annotate
)
550 create_tag(&object
, tag
, &buf
, &opt
, &prev
, &object
);
553 transaction
= ref_transaction_begin(&err
);
555 ref_transaction_update(transaction
, ref
.buf
, &object
, &prev
,
556 create_reflog
? REF_FORCE_CREATE_REFLOG
: 0,
557 reflog_msg
.buf
, &err
) ||
558 ref_transaction_commit(transaction
, &err
))
560 ref_transaction_free(transaction
);
561 if (force
&& !is_null_oid(&prev
) && oidcmp(&prev
, &object
))
562 printf(_("Updated tag '%s' (was %s)\n"), tag
,
563 find_unique_abbrev(&prev
, DEFAULT_ABBREV
));