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 "sha1-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>] <tagname> [<head>]"),
26 N_("git tag -d <tagname>..."),
27 N_("git tag -l [-n[<num>]] [--contains <commit>] [--no-contains <commit>] [--points-at <object>]"
28 "\n\t\t[--format=<format>] [--[no-]merged [<commit>]] [<pattern>...]"),
29 N_("git tag -v [--format=<format>] <tagname>..."),
33 static unsigned int colopts
;
34 static int force_sign_annotate
;
36 static int list_tags(struct ref_filter
*filter
, struct ref_sorting
*sorting
,
37 struct ref_format
*format
)
39 struct ref_array array
;
43 memset(&array
, 0, sizeof(array
));
45 if (filter
->lines
== -1)
48 if (!format
->format
) {
50 to_free
= xstrfmt("%s %%(contents:lines=%d)",
51 "%(align:15)%(refname:lstrip=2)%(end)",
53 format
->format
= to_free
;
55 format
->format
= "%(refname:lstrip=2)";
58 if (verify_ref_format(format
))
59 die(_("unable to parse format string"));
60 filter
->with_commit_tag_algo
= 1;
61 filter_refs(&array
, filter
, FILTER_REFS_TAGS
);
62 ref_array_sort(sorting
, &array
);
64 for (i
= 0; i
< array
.nr
; i
++)
65 show_ref_array_item(array
.items
[i
], format
);
66 ref_array_clear(&array
);
72 typedef int (*each_tag_name_fn
)(const char *name
, const char *ref
,
73 const struct object_id
*oid
, const void *cb_data
);
75 static int for_each_tag_name(const char **argv
, each_tag_name_fn fn
,
79 struct strbuf ref
= STRBUF_INIT
;
83 for (p
= argv
; *p
; p
++) {
85 strbuf_addf(&ref
, "refs/tags/%s", *p
);
86 if (read_ref(ref
.buf
, &oid
)) {
87 error(_("tag '%s' not found."), *p
);
91 if (fn(*p
, ref
.buf
, &oid
, cb_data
))
98 static int delete_tag(const char *name
, const char *ref
,
99 const struct object_id
*oid
, const void *cb_data
)
101 if (delete_ref(NULL
, ref
, oid
, 0))
103 printf(_("Deleted tag '%s' (was %s)\n"), name
,
104 find_unique_abbrev(oid
, DEFAULT_ABBREV
));
108 static int verify_tag(const char *name
, const char *ref
,
109 const struct object_id
*oid
, const void *cb_data
)
112 const struct ref_format
*format
= cb_data
;
113 flags
= GPG_VERIFY_VERBOSE
;
116 flags
= GPG_VERIFY_OMIT_STATUS
;
118 if (gpg_verify_tag(oid
, name
, flags
))
122 pretty_print_ref(name
, oid
, format
);
127 static int do_sign(struct strbuf
*buffer
)
129 return sign_buffer(buffer
, buffer
, get_signing_key());
132 static const char tag_template
[] =
133 N_("\nWrite a message for tag:\n %s\n"
134 "Lines starting with '%c' will be ignored.\n");
136 static const char tag_template_nocleanup
[] =
137 N_("\nWrite a message for tag:\n %s\n"
138 "Lines starting with '%c' will be kept; you may remove them"
139 " yourself if you want to.\n");
141 static int git_tag_config(const char *var
, const char *value
, void *cb
)
144 struct ref_sorting
**sorting_tail
= (struct ref_sorting
**)cb
;
146 if (!strcmp(var
, "tag.sort")) {
148 return config_error_nonbool(var
);
149 parse_ref_sorting(sorting_tail
, value
);
153 status
= git_gpg_config(var
, value
, cb
);
156 if (!strcmp(var
, "tag.forcesignannotated")) {
157 force_sign_annotate
= git_config_bool(var
, value
);
161 if (starts_with(var
, "column."))
162 return git_column_config(var
, value
, "tag", &colopts
);
163 return git_color_default_config(var
, value
, cb
);
166 static void write_tag_body(int fd
, const struct object_id
*oid
)
169 enum object_type type
;
172 buf
= read_object_file(oid
, &type
, &size
);
176 sp
= strstr(buf
, "\n\n");
178 if (!sp
|| !size
|| type
!= OBJ_TAG
) {
182 sp
+= 2; /* skip the 2 LFs */
183 write_or_die(fd
, sp
, parse_signature(sp
, buf
+ size
- sp
));
188 static int build_tag_object(struct strbuf
*buf
, int sign
, struct object_id
*result
)
190 if (sign
&& do_sign(buf
) < 0)
191 return error(_("unable to sign the tag"));
192 if (write_object_file(buf
->buf
, buf
->len
, tag_type
, result
) < 0)
193 return error(_("unable to write tag file"));
197 struct create_tag_options
{
198 unsigned int message_given
:1;
199 unsigned int use_editor
:1;
208 static void create_tag(const struct object_id
*object
, const char *tag
,
209 struct strbuf
*buf
, struct create_tag_options
*opt
,
210 struct object_id
*prev
, struct object_id
*result
)
212 enum object_type type
;
213 struct strbuf header
= STRBUF_INIT
;
216 type
= oid_object_info(the_repository
, object
, NULL
);
217 if (type
<= OBJ_NONE
)
218 die(_("bad object type."));
228 git_committer_info(IDENT_STRICT
));
230 if (!opt
->message_given
|| opt
->use_editor
) {
233 /* write the template message before editing: */
234 path
= git_pathdup("TAG_EDITMSG");
235 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
237 die_errno(_("could not create file '%s'"), path
);
239 if (opt
->message_given
) {
240 write_or_die(fd
, buf
->buf
, buf
->len
);
242 } else if (!is_null_oid(prev
)) {
243 write_tag_body(fd
, prev
);
245 struct strbuf buf
= STRBUF_INIT
;
246 strbuf_addch(&buf
, '\n');
247 if (opt
->cleanup_mode
== CLEANUP_ALL
)
248 strbuf_commented_addf(&buf
, _(tag_template
), tag
, comment_line_char
);
250 strbuf_commented_addf(&buf
, _(tag_template_nocleanup
), tag
, comment_line_char
);
251 write_or_die(fd
, buf
.buf
, buf
.len
);
252 strbuf_release(&buf
);
256 if (launch_editor(path
, buf
, NULL
)) {
258 _("Please supply the message using either -m or -F option.\n"));
263 if (opt
->cleanup_mode
!= CLEANUP_NONE
)
264 strbuf_stripspace(buf
, opt
->cleanup_mode
== CLEANUP_ALL
);
266 if (!opt
->message_given
&& !buf
->len
)
267 die(_("no tag message?"));
269 strbuf_insert(buf
, 0, header
.buf
, header
.len
);
270 strbuf_release(&header
);
272 if (build_tag_object(buf
, opt
->sign
, result
) < 0) {
274 fprintf(stderr
, _("The tag message has been left in %s\n"),
279 unlink_or_warn(path
);
284 static void create_reflog_msg(const struct object_id
*oid
, struct strbuf
*sb
)
286 enum object_type type
;
291 const char *subject_start
;
293 char *rla
= getenv("GIT_REFLOG_ACTION");
295 strbuf_addstr(sb
, rla
);
297 strbuf_addstr(sb
, "tag: tagging ");
298 strbuf_add_unique_abbrev(sb
, oid
, DEFAULT_ABBREV
);
301 strbuf_addstr(sb
, " (");
302 type
= oid_object_info(the_repository
, oid
, NULL
);
305 strbuf_addstr(sb
, "object of unknown type");
308 if ((buf
= read_object_file(oid
, &type
, &size
)) != NULL
) {
309 subject_len
= find_commit_subject(buf
, &subject_start
);
310 strbuf_insert(sb
, sb
->len
, subject_start
, subject_len
);
312 strbuf_addstr(sb
, "commit object");
316 if ((c
= lookup_commit_reference(the_repository
, oid
)) != NULL
)
317 strbuf_addf(sb
, ", %s", show_date(c
->date
, 0, DATE_MODE(SHORT
)));
320 strbuf_addstr(sb
, "tree object");
323 strbuf_addstr(sb
, "blob object");
326 strbuf_addstr(sb
, "other tag object");
329 strbuf_addch(sb
, ')');
337 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
339 struct msg_arg
*msg
= opt
->value
;
344 strbuf_addstr(&(msg
->buf
), "\n\n");
345 strbuf_addstr(&(msg
->buf
), arg
);
350 static int strbuf_check_tag_ref(struct strbuf
*sb
, const char *name
)
356 strbuf_addf(sb
, "refs/tags/%s", name
);
358 return check_refname_format(sb
->buf
, 0);
361 int cmd_tag(int argc
, const char **argv
, const char *prefix
)
363 struct strbuf buf
= STRBUF_INIT
;
364 struct strbuf ref
= STRBUF_INIT
;
365 struct strbuf reflog_msg
= STRBUF_INIT
;
366 struct object_id object
, prev
;
367 const char *object_ref
, *tag
;
368 struct create_tag_options opt
;
369 char *cleanup_arg
= NULL
;
370 int create_reflog
= 0;
371 int annotate
= 0, force
= 0;
372 int cmdmode
= 0, create_tag_object
= 0;
373 const char *msgfile
= NULL
, *keyid
= NULL
;
374 struct msg_arg msg
= { 0, STRBUF_INIT
};
375 struct ref_transaction
*transaction
;
376 struct strbuf err
= STRBUF_INIT
;
377 struct ref_filter filter
;
378 static struct ref_sorting
*sorting
= NULL
, **sorting_tail
= &sorting
;
379 struct ref_format format
= REF_FORMAT_INIT
;
382 struct option options
[] = {
383 OPT_CMDMODE('l', "list", &cmdmode
, N_("list tag names"), 'l'),
384 { OPTION_INTEGER
, 'n', NULL
, &filter
.lines
, N_("n"),
385 N_("print <n> lines of each tag message"),
386 PARSE_OPT_OPTARG
, NULL
, 1 },
387 OPT_CMDMODE('d', "delete", &cmdmode
, N_("delete tags"), 'd'),
388 OPT_CMDMODE('v', "verify", &cmdmode
, N_("verify tags"), 'v'),
390 OPT_GROUP(N_("Tag creation options")),
391 OPT_BOOL('a', "annotate", &annotate
,
392 N_("annotated tag, needs a message")),
393 OPT_CALLBACK('m', "message", &msg
, N_("message"),
394 N_("tag message"), parse_msg_arg
),
395 OPT_FILENAME('F', "file", &msgfile
, N_("read message from file")),
396 OPT_BOOL('e', "edit", &edit_flag
, N_("force edit of tag message")),
397 OPT_BOOL('s', "sign", &opt
.sign
, N_("annotated and GPG-signed tag")),
398 OPT_STRING(0, "cleanup", &cleanup_arg
, N_("mode"),
399 N_("how to strip spaces and #comments from message")),
400 OPT_STRING('u', "local-user", &keyid
, N_("key-id"),
401 N_("use another key to sign the tag")),
402 OPT__FORCE(&force
, N_("replace the tag if exists"), 0),
403 OPT_BOOL(0, "create-reflog", &create_reflog
, N_("create a reflog")),
405 OPT_GROUP(N_("Tag listing options")),
406 OPT_COLUMN(0, "column", &colopts
, N_("show tag list in columns")),
407 OPT_CONTAINS(&filter
.with_commit
, N_("print only tags that contain the commit")),
408 OPT_NO_CONTAINS(&filter
.no_commit
, N_("print only tags that don't contain the commit")),
409 OPT_WITH(&filter
.with_commit
, N_("print only tags that contain the commit")),
410 OPT_WITHOUT(&filter
.no_commit
, N_("print only tags that don't contain the commit")),
411 OPT_MERGED(&filter
, N_("print only tags that are merged")),
412 OPT_NO_MERGED(&filter
, N_("print only tags that are not merged")),
413 OPT_CALLBACK(0 , "sort", sorting_tail
, N_("key"),
414 N_("field name to sort on"), &parse_opt_ref_sorting
),
416 OPTION_CALLBACK
, 0, "points-at", &filter
.points_at
, N_("object"),
417 N_("print only tags of the object"), PARSE_OPT_LASTARG_DEFAULT
,
418 parse_opt_object_name
, (intptr_t) "HEAD"
420 OPT_STRING( 0 , "format", &format
.format
, N_("format"),
421 N_("format to use for the output")),
422 OPT__COLOR(&format
.use_color
, N_("respect format colors")),
423 OPT_BOOL('i', "ignore-case", &icase
, N_("sorting and filtering are case insensitive")),
427 setup_ref_filter_porcelain_msg();
429 git_config(git_tag_config
, sorting_tail
);
431 memset(&opt
, 0, sizeof(opt
));
432 memset(&filter
, 0, sizeof(filter
));
435 argc
= parse_options(argc
, argv
, prefix
, options
, git_tag_usage
, 0);
439 set_signing_key(keyid
);
441 create_tag_object
= (opt
.sign
|| annotate
|| msg
.given
|| msgfile
);
446 else if (filter
.with_commit
|| filter
.no_commit
||
447 filter
.points_at
.nr
|| filter
.merge_commit
||
453 setup_auto_pager("tag", 1);
455 if ((create_tag_object
|| force
) && (cmdmode
!= 0))
456 usage_with_options(git_tag_usage
, options
);
458 finalize_colopts(&colopts
, -1);
459 if (cmdmode
== 'l' && filter
.lines
!= -1) {
460 if (explicitly_enable_column(colopts
))
461 die(_("--column and -n are incompatible"));
465 sorting
= ref_default_sorting();
466 sorting
->ignore_case
= icase
;
467 filter
.ignore_case
= icase
;
468 if (cmdmode
== 'l') {
470 if (column_active(colopts
)) {
471 struct column_options copts
;
472 memset(&copts
, 0, sizeof(copts
));
474 run_column_filter(colopts
, &copts
);
476 filter
.name_patterns
= argv
;
477 ret
= list_tags(&filter
, sorting
, &format
);
478 if (column_active(colopts
))
479 stop_column_filter();
482 if (filter
.lines
!= -1)
483 die(_("-n option is only allowed in list mode"));
484 if (filter
.with_commit
)
485 die(_("--contains option is only allowed in list mode"));
486 if (filter
.no_commit
)
487 die(_("--no-contains option is only allowed in list mode"));
488 if (filter
.points_at
.nr
)
489 die(_("--points-at option is only allowed in list mode"));
490 if (filter
.merge_commit
)
491 die(_("--merged and --no-merged options are only allowed in list mode"));
493 return for_each_tag_name(argv
, delete_tag
, NULL
);
494 if (cmdmode
== 'v') {
495 if (format
.format
&& verify_ref_format(&format
))
496 usage_with_options(git_tag_usage
, options
);
497 return for_each_tag_name(argv
, verify_tag
, &format
);
500 if (msg
.given
|| msgfile
) {
501 if (msg
.given
&& msgfile
)
502 die(_("only one -F or -m option is allowed."));
504 strbuf_addbuf(&buf
, &(msg
.buf
));
506 if (!strcmp(msgfile
, "-")) {
507 if (strbuf_read(&buf
, 0, 1024) < 0)
508 die_errno(_("cannot read '%s'"), msgfile
);
510 if (strbuf_read_file(&buf
, msgfile
, 1024) < 0)
511 die_errno(_("could not open or read '%s'"),
519 object_ref
= argc
== 2 ? argv
[1] : "HEAD";
521 die(_("too many params"));
523 if (get_oid(object_ref
, &object
))
524 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
526 if (strbuf_check_tag_ref(&ref
, tag
))
527 die(_("'%s' is not a valid tag name."), tag
);
529 if (read_ref(ref
.buf
, &prev
))
532 die(_("tag '%s' already exists"), tag
);
534 opt
.message_given
= msg
.given
|| msgfile
;
535 opt
.use_editor
= edit_flag
;
537 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "strip"))
538 opt
.cleanup_mode
= CLEANUP_ALL
;
539 else if (!strcmp(cleanup_arg
, "verbatim"))
540 opt
.cleanup_mode
= CLEANUP_NONE
;
541 else if (!strcmp(cleanup_arg
, "whitespace"))
542 opt
.cleanup_mode
= CLEANUP_SPACE
;
544 die(_("Invalid cleanup mode %s"), cleanup_arg
);
546 create_reflog_msg(&object
, &reflog_msg
);
548 if (create_tag_object
) {
549 if (force_sign_annotate
&& !annotate
)
551 create_tag(&object
, tag
, &buf
, &opt
, &prev
, &object
);
554 transaction
= ref_transaction_begin(&err
);
556 ref_transaction_update(transaction
, ref
.buf
, &object
, &prev
,
557 create_reflog
? REF_FORCE_CREATE_REFLOG
: 0,
558 reflog_msg
.buf
, &err
) ||
559 ref_transaction_commit(transaction
, &err
))
561 ref_transaction_free(transaction
);
562 if (force
&& !is_null_oid(&prev
) && !oideq(&prev
, &object
))
563 printf(_("Updated tag '%s' (was %s)\n"), tag
,
564 find_unique_abbrev(&prev
, DEFAULT_ABBREV
));