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
, const char *format
)
37 struct ref_array array
;
41 memset(&array
, 0, sizeof(array
));
43 if (filter
->lines
== -1)
48 to_free
= xstrfmt("%s %%(contents:lines=%d)",
49 "%(align:15)%(refname:lstrip=2)%(end)",
53 format
= "%(refname:lstrip=2)";
56 verify_ref_format(format
);
57 filter
->with_commit_tag_algo
= 1;
58 filter_refs(&array
, filter
, FILTER_REFS_TAGS
);
59 ref_array_sort(sorting
, &array
);
61 for (i
= 0; i
< array
.nr
; i
++)
62 show_ref_array_item(array
.items
[i
], format
, 0);
63 ref_array_clear(&array
);
69 typedef int (*each_tag_name_fn
)(const char *name
, const char *ref
,
70 const struct object_id
*oid
, const void *cb_data
);
72 static int for_each_tag_name(const char **argv
, each_tag_name_fn fn
,
76 struct strbuf ref
= STRBUF_INIT
;
80 for (p
= argv
; *p
; p
++) {
82 strbuf_addf(&ref
, "refs/tags/%s", *p
);
83 if (read_ref(ref
.buf
, oid
.hash
)) {
84 error(_("tag '%s' not found."), *p
);
88 if (fn(*p
, ref
.buf
, &oid
, cb_data
))
95 static int delete_tag(const char *name
, const char *ref
,
96 const struct object_id
*oid
, const void *cb_data
)
98 if (delete_ref(NULL
, ref
, oid
->hash
, 0))
100 printf(_("Deleted tag '%s' (was %s)\n"), name
, find_unique_abbrev(oid
->hash
, DEFAULT_ABBREV
));
104 static int verify_tag(const char *name
, const char *ref
,
105 const struct object_id
*oid
, const void *cb_data
)
108 const char *fmt_pretty
= cb_data
;
109 flags
= GPG_VERIFY_VERBOSE
;
112 flags
= GPG_VERIFY_OMIT_STATUS
;
114 if (gpg_verify_tag(oid
->hash
, name
, flags
))
118 pretty_print_ref(name
, oid
->hash
, fmt_pretty
);
123 static int do_sign(struct strbuf
*buffer
)
125 return sign_buffer(buffer
, buffer
, get_signing_key());
128 static const char tag_template
[] =
129 N_("\nWrite a message for tag:\n %s\n"
130 "Lines starting with '%c' will be ignored.\n");
132 static const char tag_template_nocleanup
[] =
133 N_("\nWrite a message for tag:\n %s\n"
134 "Lines starting with '%c' will be kept; you may remove them"
135 " yourself if you want to.\n");
137 /* Parse arg given and add it the ref_sorting array */
138 static int parse_sorting_string(const char *arg
, struct ref_sorting
**sorting_tail
)
140 struct ref_sorting
*s
;
143 s
= xcalloc(1, sizeof(*s
));
144 s
->next
= *sorting_tail
;
151 if (skip_prefix(arg
, "version:", &arg
) ||
152 skip_prefix(arg
, "v:", &arg
))
156 s
->atom
= parse_ref_filter_atom(arg
, arg
+len
);
161 static int git_tag_config(const char *var
, const char *value
, void *cb
)
164 struct ref_sorting
**sorting_tail
= (struct ref_sorting
**)cb
;
166 if (!strcmp(var
, "tag.sort")) {
168 return config_error_nonbool(var
);
169 parse_sorting_string(value
, sorting_tail
);
173 status
= git_gpg_config(var
, value
, cb
);
176 if (!strcmp(var
, "tag.forcesignannotated")) {
177 force_sign_annotate
= git_config_bool(var
, value
);
181 if (starts_with(var
, "column."))
182 return git_column_config(var
, value
, "tag", &colopts
);
183 return git_default_config(var
, value
, cb
);
186 static void write_tag_body(int fd
, const struct object_id
*oid
)
189 enum object_type type
;
192 buf
= read_sha1_file(oid
->hash
, &type
, &size
);
196 sp
= strstr(buf
, "\n\n");
198 if (!sp
|| !size
|| type
!= OBJ_TAG
) {
202 sp
+= 2; /* skip the 2 LFs */
203 write_or_die(fd
, sp
, parse_signature(sp
, buf
+ size
- sp
));
208 static int build_tag_object(struct strbuf
*buf
, int sign
, struct object_id
*result
)
210 if (sign
&& do_sign(buf
) < 0)
211 return error(_("unable to sign the tag"));
212 if (write_sha1_file(buf
->buf
, buf
->len
, tag_type
, result
->hash
) < 0)
213 return error(_("unable to write tag file"));
217 struct create_tag_options
{
218 unsigned int message_given
:1;
227 static void create_tag(const struct object_id
*object
, const char *tag
,
228 struct strbuf
*buf
, struct create_tag_options
*opt
,
229 struct object_id
*prev
, struct object_id
*result
)
231 enum object_type type
;
232 struct strbuf header
= STRBUF_INIT
;
235 type
= sha1_object_info(object
->hash
, NULL
);
236 if (type
<= OBJ_NONE
)
237 die(_("bad object type."));
247 git_committer_info(IDENT_STRICT
));
249 if (!opt
->message_given
) {
252 /* write the template message before editing: */
253 path
= git_pathdup("TAG_EDITMSG");
254 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
256 die_errno(_("could not create file '%s'"), path
);
258 if (!is_null_oid(prev
)) {
259 write_tag_body(fd
, prev
);
261 struct strbuf buf
= STRBUF_INIT
;
262 strbuf_addch(&buf
, '\n');
263 if (opt
->cleanup_mode
== CLEANUP_ALL
)
264 strbuf_commented_addf(&buf
, _(tag_template
), tag
, comment_line_char
);
266 strbuf_commented_addf(&buf
, _(tag_template_nocleanup
), tag
, comment_line_char
);
267 write_or_die(fd
, buf
.buf
, buf
.len
);
268 strbuf_release(&buf
);
272 if (launch_editor(path
, buf
, NULL
)) {
274 _("Please supply the message using either -m or -F option.\n"));
279 if (opt
->cleanup_mode
!= CLEANUP_NONE
)
280 strbuf_stripspace(buf
, opt
->cleanup_mode
== CLEANUP_ALL
);
282 if (!opt
->message_given
&& !buf
->len
)
283 die(_("no tag message?"));
285 strbuf_insert(buf
, 0, header
.buf
, header
.len
);
286 strbuf_release(&header
);
288 if (build_tag_object(buf
, opt
->sign
, result
) < 0) {
290 fprintf(stderr
, _("The tag message has been left in %s\n"),
295 unlink_or_warn(path
);
300 static void create_reflog_msg(const struct object_id
*oid
, struct strbuf
*sb
)
302 enum object_type type
;
307 const char *subject_start
;
309 char *rla
= getenv("GIT_REFLOG_ACTION");
311 strbuf_addstr(sb
, rla
);
313 strbuf_addstr(sb
, "tag: tagging ");
314 strbuf_add_unique_abbrev(sb
, oid
->hash
, DEFAULT_ABBREV
);
317 strbuf_addstr(sb
, " (");
318 type
= sha1_object_info(oid
->hash
, NULL
);
321 strbuf_addstr(sb
, "object of unknown type");
324 if ((buf
= read_sha1_file(oid
->hash
, &type
, &size
)) != NULL
) {
325 subject_len
= find_commit_subject(buf
, &subject_start
);
326 strbuf_insert(sb
, sb
->len
, subject_start
, subject_len
);
328 strbuf_addstr(sb
, "commit object");
332 if ((c
= lookup_commit_reference(oid
)) != NULL
)
333 strbuf_addf(sb
, ", %s", show_date(c
->date
, 0, DATE_MODE(SHORT
)));
336 strbuf_addstr(sb
, "tree object");
339 strbuf_addstr(sb
, "blob object");
342 strbuf_addstr(sb
, "other tag object");
345 strbuf_addch(sb
, ')');
353 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
355 struct msg_arg
*msg
= opt
->value
;
360 strbuf_addstr(&(msg
->buf
), "\n\n");
361 strbuf_addstr(&(msg
->buf
), arg
);
366 static int strbuf_check_tag_ref(struct strbuf
*sb
, const char *name
)
372 strbuf_addf(sb
, "refs/tags/%s", name
);
374 return check_refname_format(sb
->buf
, 0);
377 int cmd_tag(int argc
, const char **argv
, const char *prefix
)
379 struct strbuf buf
= STRBUF_INIT
;
380 struct strbuf ref
= STRBUF_INIT
;
381 struct strbuf reflog_msg
= STRBUF_INIT
;
382 struct object_id object
, prev
;
383 const char *object_ref
, *tag
;
384 struct create_tag_options opt
;
385 char *cleanup_arg
= NULL
;
386 int create_reflog
= 0;
387 int annotate
= 0, force
= 0;
388 int cmdmode
= 0, create_tag_object
= 0;
389 const char *msgfile
= NULL
, *keyid
= NULL
;
390 struct msg_arg msg
= { 0, STRBUF_INIT
};
391 struct ref_transaction
*transaction
;
392 struct strbuf err
= STRBUF_INIT
;
393 struct ref_filter filter
;
394 static struct ref_sorting
*sorting
= NULL
, **sorting_tail
= &sorting
;
395 const char *format
= NULL
;
397 struct option options
[] = {
398 OPT_CMDMODE('l', "list", &cmdmode
, N_("list tag names"), 'l'),
399 { OPTION_INTEGER
, 'n', NULL
, &filter
.lines
, N_("n"),
400 N_("print <n> lines of each tag message"),
401 PARSE_OPT_OPTARG
, NULL
, 1 },
402 OPT_CMDMODE('d', "delete", &cmdmode
, N_("delete tags"), 'd'),
403 OPT_CMDMODE('v', "verify", &cmdmode
, N_("verify tags"), 'v'),
405 OPT_GROUP(N_("Tag creation options")),
406 OPT_BOOL('a', "annotate", &annotate
,
407 N_("annotated tag, needs a message")),
408 OPT_CALLBACK('m', "message", &msg
, N_("message"),
409 N_("tag message"), parse_msg_arg
),
410 OPT_FILENAME('F', "file", &msgfile
, N_("read message from file")),
411 OPT_BOOL('s', "sign", &opt
.sign
, N_("annotated and GPG-signed tag")),
412 OPT_STRING(0, "cleanup", &cleanup_arg
, N_("mode"),
413 N_("how to strip spaces and #comments from message")),
414 OPT_STRING('u', "local-user", &keyid
, N_("key-id"),
415 N_("use another key to sign the tag")),
416 OPT__FORCE(&force
, N_("replace the tag if exists")),
417 OPT_BOOL(0, "create-reflog", &create_reflog
, N_("create a reflog")),
419 OPT_GROUP(N_("Tag listing options")),
420 OPT_COLUMN(0, "column", &colopts
, N_("show tag list in columns")),
421 OPT_CONTAINS(&filter
.with_commit
, N_("print only tags that contain the commit")),
422 OPT_NO_CONTAINS(&filter
.no_commit
, N_("print only tags that don't contain the commit")),
423 OPT_WITH(&filter
.with_commit
, N_("print only tags that contain the commit")),
424 OPT_WITHOUT(&filter
.no_commit
, N_("print only tags that don't contain the commit")),
425 OPT_MERGED(&filter
, N_("print only tags that are merged")),
426 OPT_NO_MERGED(&filter
, N_("print only tags that are not merged")),
427 OPT_CALLBACK(0 , "sort", sorting_tail
, N_("key"),
428 N_("field name to sort on"), &parse_opt_ref_sorting
),
430 OPTION_CALLBACK
, 0, "points-at", &filter
.points_at
, N_("object"),
431 N_("print only tags of the object"), PARSE_OPT_LASTARG_DEFAULT
,
432 parse_opt_object_name
, (intptr_t) "HEAD"
434 OPT_STRING( 0 , "format", &format
, N_("format"), N_("format to use for the output")),
435 OPT_BOOL('i', "ignore-case", &icase
, N_("sorting and filtering are case insensitive")),
439 setup_ref_filter_porcelain_msg();
441 git_config(git_tag_config
, sorting_tail
);
443 memset(&opt
, 0, sizeof(opt
));
444 memset(&filter
, 0, sizeof(filter
));
447 argc
= parse_options(argc
, argv
, prefix
, options
, git_tag_usage
, 0);
451 set_signing_key(keyid
);
453 create_tag_object
= (opt
.sign
|| annotate
|| msg
.given
|| msgfile
);
458 else if (filter
.with_commit
|| filter
.no_commit
||
459 filter
.points_at
.nr
|| filter
.merge_commit
||
464 if ((create_tag_object
|| force
) && (cmdmode
!= 0))
465 usage_with_options(git_tag_usage
, options
);
467 finalize_colopts(&colopts
, -1);
468 if (cmdmode
== 'l' && filter
.lines
!= -1) {
469 if (explicitly_enable_column(colopts
))
470 die(_("--column and -n are incompatible"));
474 sorting
= ref_default_sorting();
475 sorting
->ignore_case
= icase
;
476 filter
.ignore_case
= icase
;
477 if (cmdmode
== 'l') {
479 if (column_active(colopts
)) {
480 struct column_options copts
;
481 memset(&copts
, 0, sizeof(copts
));
483 run_column_filter(colopts
, &copts
);
485 filter
.name_patterns
= argv
;
486 ret
= list_tags(&filter
, sorting
, format
);
487 if (column_active(colopts
))
488 stop_column_filter();
491 if (filter
.lines
!= -1)
492 die(_("-n option is only allowed in list mode"));
493 if (filter
.with_commit
)
494 die(_("--contains option is only allowed in list mode"));
495 if (filter
.no_commit
)
496 die(_("--no-contains option is only allowed in list mode"));
497 if (filter
.points_at
.nr
)
498 die(_("--points-at option is only allowed in list mode"));
499 if (filter
.merge_commit
)
500 die(_("--merged and --no-merged options are only allowed in list mode"));
502 return for_each_tag_name(argv
, delete_tag
, NULL
);
503 if (cmdmode
== 'v') {
505 verify_ref_format(format
);
506 return for_each_tag_name(argv
, verify_tag
, format
);
509 if (msg
.given
|| msgfile
) {
510 if (msg
.given
&& msgfile
)
511 die(_("only one -F or -m option is allowed."));
513 strbuf_addbuf(&buf
, &(msg
.buf
));
515 if (!strcmp(msgfile
, "-")) {
516 if (strbuf_read(&buf
, 0, 1024) < 0)
517 die_errno(_("cannot read '%s'"), msgfile
);
519 if (strbuf_read_file(&buf
, msgfile
, 1024) < 0)
520 die_errno(_("could not open or read '%s'"),
528 object_ref
= argc
== 2 ? argv
[1] : "HEAD";
530 die(_("too many params"));
532 if (get_oid(object_ref
, &object
))
533 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
535 if (strbuf_check_tag_ref(&ref
, tag
))
536 die(_("'%s' is not a valid tag name."), tag
);
538 if (read_ref(ref
.buf
, prev
.hash
))
541 die(_("tag '%s' already exists"), tag
);
543 opt
.message_given
= msg
.given
|| msgfile
;
545 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "strip"))
546 opt
.cleanup_mode
= CLEANUP_ALL
;
547 else if (!strcmp(cleanup_arg
, "verbatim"))
548 opt
.cleanup_mode
= CLEANUP_NONE
;
549 else if (!strcmp(cleanup_arg
, "whitespace"))
550 opt
.cleanup_mode
= CLEANUP_SPACE
;
552 die(_("Invalid cleanup mode %s"), cleanup_arg
);
554 create_reflog_msg(&object
, &reflog_msg
);
556 if (create_tag_object
) {
557 if (force_sign_annotate
&& !annotate
)
559 create_tag(&object
, tag
, &buf
, &opt
, &prev
, &object
);
562 transaction
= ref_transaction_begin(&err
);
564 ref_transaction_update(transaction
, ref
.buf
, object
.hash
, prev
.hash
,
565 create_reflog
? REF_FORCE_CREATE_REFLOG
: 0,
566 reflog_msg
.buf
, &err
) ||
567 ref_transaction_commit(transaction
, &err
))
569 ref_transaction_free(transaction
);
570 if (force
&& !is_null_oid(&prev
) && oidcmp(&prev
, &object
))
571 printf(_("Updated tag '%s' (was %s)\n"), tag
, find_unique_abbrev(prev
.hash
, DEFAULT_ABBREV
));
573 strbuf_release(&err
);
574 strbuf_release(&buf
);
575 strbuf_release(&ref
);
576 strbuf_release(&reflog_msg
);