]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/tag.c
config: inline git_color_default_config
[thirdparty/git.git] / builtin / tag.c
1 /*
2 * Builtin "git tag"
3 *
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.
7 */
8
9 #include "cache.h"
10 #include "advice.h"
11 #include "config.h"
12 #include "builtin.h"
13 #include "editor.h"
14 #include "environment.h"
15 #include "gettext.h"
16 #include "hex.h"
17 #include "refs.h"
18 #include "object-name.h"
19 #include "object-store.h"
20 #include "tag.h"
21 #include "run-command.h"
22 #include "parse-options.h"
23 #include "diff.h"
24 #include "revision.h"
25 #include "gpg-interface.h"
26 #include "oid-array.h"
27 #include "column.h"
28 #include "ref-filter.h"
29 #include "date.h"
30 #include "write-or-die.h"
31
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>..."),
41 NULL
42 };
43
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;
48
49 static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
50 struct ref_format *format)
51 {
52 struct ref_array array;
53 struct strbuf output = STRBUF_INIT;
54 struct strbuf err = STRBUF_INIT;
55 char *to_free = NULL;
56 int i;
57
58 memset(&array, 0, sizeof(array));
59
60 if (filter->lines == -1)
61 filter->lines = 0;
62
63 if (!format->format) {
64 if (filter->lines) {
65 to_free = xstrfmt("%s %%(contents:lines=%d)",
66 "%(align:15)%(refname:lstrip=2)%(end)",
67 filter->lines);
68 format->format = to_free;
69 } else
70 format->format = "%(refname:lstrip=2)";
71 }
72
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);
79
80 for (i = 0; i < array.nr; i++) {
81 strbuf_reset(&output);
82 strbuf_reset(&err);
83 if (format_ref_array_item(array.items[i], format, &output, &err))
84 die("%s", err.buf);
85 fwrite(output.buf, 1, output.len, stdout);
86 if (output.len || !omit_empty)
87 putchar('\n');
88 }
89
90 strbuf_release(&err);
91 strbuf_release(&output);
92 ref_array_clear(&array);
93 free(to_free);
94
95 return 0;
96 }
97
98 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
99 const struct object_id *oid, void *cb_data);
100
101 static int for_each_tag_name(const char **argv, each_tag_name_fn fn,
102 void *cb_data)
103 {
104 const char **p;
105 struct strbuf ref = STRBUF_INIT;
106 int had_error = 0;
107 struct object_id oid;
108
109 for (p = argv; *p; p++) {
110 strbuf_reset(&ref);
111 strbuf_addf(&ref, "refs/tags/%s", *p);
112 if (read_ref(ref.buf, &oid)) {
113 error(_("tag '%s' not found."), *p);
114 had_error = 1;
115 continue;
116 }
117 if (fn(*p, ref.buf, &oid, cb_data))
118 had_error = 1;
119 }
120 strbuf_release(&ref);
121 return had_error;
122 }
123
124 static int collect_tags(const char *name, const char *ref,
125 const struct object_id *oid, void *cb_data)
126 {
127 struct string_list *ref_list = cb_data;
128
129 string_list_append(ref_list, ref);
130 ref_list->items[ref_list->nr - 1].util = oiddup(oid);
131 return 0;
132 }
133
134 static int delete_tags(const char **argv)
135 {
136 int result;
137 struct string_list refs_to_delete = STRING_LIST_INIT_DUP;
138 struct string_list_item *item;
139
140 result = for_each_tag_name(argv, collect_tags, (void *)&refs_to_delete);
141 if (delete_refs(NULL, &refs_to_delete, REF_NO_DEREF))
142 result = 1;
143
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"),
149 item->string + 10,
150 repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV));
151
152 free(oid);
153 }
154 string_list_clear(&refs_to_delete, 0);
155 return result;
156 }
157
158 static int verify_tag(const char *name, const char *ref,
159 const struct object_id *oid, void *cb_data)
160 {
161 int flags;
162 struct ref_format *format = cb_data;
163 flags = GPG_VERIFY_VERBOSE;
164
165 if (format->format)
166 flags = GPG_VERIFY_OMIT_STATUS;
167
168 if (gpg_verify_tag(oid, name, flags))
169 return -1;
170
171 if (format->format)
172 pretty_print_ref(name, oid, format);
173
174 return 0;
175 }
176
177 static int do_sign(struct strbuf *buffer)
178 {
179 return sign_buffer(buffer, buffer, get_signing_key());
180 }
181
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");
185
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");
190
191 static int git_tag_config(const char *var, const char *value, void *cb)
192 {
193 if (!strcmp(var, "tag.gpgsign")) {
194 config_sign_tag = git_config_bool(var, value);
195 return 0;
196 }
197
198 if (!strcmp(var, "tag.sort")) {
199 if (!value)
200 return config_error_nonbool(var);
201 string_list_append(cb, value);
202 return 0;
203 }
204
205 if (!strcmp(var, "tag.forcesignannotated")) {
206 force_sign_annotate = git_config_bool(var, value);
207 return 0;
208 }
209
210 if (starts_with(var, "column."))
211 return git_column_config(var, value, "tag", &colopts);
212
213 if (git_color_config(var, value, cb) < 0)
214 return -1;
215
216 return git_default_config(var, value, cb);
217 }
218
219 static void write_tag_body(int fd, const struct object_id *oid)
220 {
221 unsigned long size;
222 enum object_type type;
223 char *buf, *sp, *orig;
224 struct strbuf payload = STRBUF_INIT;
225 struct strbuf signature = STRBUF_INIT;
226
227 orig = buf = repo_read_object_file(the_repository, oid, &type, &size);
228 if (!buf)
229 return;
230 if (parse_signature(buf, size, &payload, &signature)) {
231 buf = payload.buf;
232 size = payload.len;
233 }
234 /* skip header */
235 sp = strstr(buf, "\n\n");
236
237 if (!sp || !size || type != OBJ_TAG) {
238 free(buf);
239 return;
240 }
241 sp += 2; /* skip the 2 LFs */
242 write_or_die(fd, sp, buf + size - sp);
243
244 free(orig);
245 strbuf_release(&payload);
246 strbuf_release(&signature);
247 }
248
249 static int build_tag_object(struct strbuf *buf, int sign, struct object_id *result)
250 {
251 if (sign && do_sign(buf) < 0)
252 return error(_("unable to sign the tag"));
253 if (write_object_file(buf->buf, buf->len, OBJ_TAG, result) < 0)
254 return error(_("unable to write tag file"));
255 return 0;
256 }
257
258 struct create_tag_options {
259 unsigned int message_given:1;
260 unsigned int use_editor:1;
261 unsigned int sign;
262 enum {
263 CLEANUP_NONE,
264 CLEANUP_SPACE,
265 CLEANUP_ALL
266 } cleanup_mode;
267 };
268
269 static const char message_advice_nested_tag[] =
270 N_("You have created a nested tag. The object referred to by your new tag is\n"
271 "already a tag. If you meant to tag the object that it points to, use:\n"
272 "\n"
273 "\tgit tag -f %s %s^{}");
274
275 static void create_tag(const struct object_id *object, const char *object_ref,
276 const char *tag,
277 struct strbuf *buf, struct create_tag_options *opt,
278 struct object_id *prev, struct object_id *result, char *path)
279 {
280 enum object_type type;
281 struct strbuf header = STRBUF_INIT;
282
283 type = oid_object_info(the_repository, object, NULL);
284 if (type <= OBJ_NONE)
285 die(_("bad object type."));
286
287 if (type == OBJ_TAG)
288 advise_if_enabled(ADVICE_NESTED_TAG, _(message_advice_nested_tag),
289 tag, object_ref);
290
291 strbuf_addf(&header,
292 "object %s\n"
293 "type %s\n"
294 "tag %s\n"
295 "tagger %s\n\n",
296 oid_to_hex(object),
297 type_name(type),
298 tag,
299 git_committer_info(IDENT_STRICT));
300
301 if (!opt->message_given || opt->use_editor) {
302 int fd;
303
304 /* write the template message before editing: */
305 fd = xopen(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
306
307 if (opt->message_given) {
308 write_or_die(fd, buf->buf, buf->len);
309 strbuf_reset(buf);
310 } else if (!is_null_oid(prev)) {
311 write_tag_body(fd, prev);
312 } else {
313 struct strbuf buf = STRBUF_INIT;
314 strbuf_addch(&buf, '\n');
315 if (opt->cleanup_mode == CLEANUP_ALL)
316 strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
317 else
318 strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char);
319 write_or_die(fd, buf.buf, buf.len);
320 strbuf_release(&buf);
321 }
322 close(fd);
323
324 if (launch_editor(path, buf, NULL)) {
325 fprintf(stderr,
326 _("Please supply the message using either -m or -F option.\n"));
327 exit(1);
328 }
329 }
330
331 if (opt->cleanup_mode != CLEANUP_NONE)
332 strbuf_stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
333
334 if (!opt->message_given && !buf->len)
335 die(_("no tag message?"));
336
337 strbuf_insert(buf, 0, header.buf, header.len);
338 strbuf_release(&header);
339
340 if (build_tag_object(buf, opt->sign, result) < 0) {
341 if (path)
342 fprintf(stderr, _("The tag message has been left in %s\n"),
343 path);
344 exit(128);
345 }
346 }
347
348 static void create_reflog_msg(const struct object_id *oid, struct strbuf *sb)
349 {
350 enum object_type type;
351 struct commit *c;
352 char *buf;
353 unsigned long size;
354 int subject_len = 0;
355 const char *subject_start;
356
357 char *rla = getenv("GIT_REFLOG_ACTION");
358 if (rla) {
359 strbuf_addstr(sb, rla);
360 } else {
361 strbuf_addstr(sb, "tag: tagging ");
362 strbuf_add_unique_abbrev(sb, oid, DEFAULT_ABBREV);
363 }
364
365 strbuf_addstr(sb, " (");
366 type = oid_object_info(the_repository, oid, NULL);
367 switch (type) {
368 default:
369 strbuf_addstr(sb, "object of unknown type");
370 break;
371 case OBJ_COMMIT:
372 if ((buf = repo_read_object_file(the_repository, oid, &type, &size))) {
373 subject_len = find_commit_subject(buf, &subject_start);
374 strbuf_insert(sb, sb->len, subject_start, subject_len);
375 } else {
376 strbuf_addstr(sb, "commit object");
377 }
378 free(buf);
379
380 if ((c = lookup_commit_reference(the_repository, oid)))
381 strbuf_addf(sb, ", %s", show_date(c->date, 0, DATE_MODE(SHORT)));
382 break;
383 case OBJ_TREE:
384 strbuf_addstr(sb, "tree object");
385 break;
386 case OBJ_BLOB:
387 strbuf_addstr(sb, "blob object");
388 break;
389 case OBJ_TAG:
390 strbuf_addstr(sb, "other tag object");
391 break;
392 }
393 strbuf_addch(sb, ')');
394 }
395
396 struct msg_arg {
397 int given;
398 struct strbuf buf;
399 };
400
401 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
402 {
403 struct msg_arg *msg = opt->value;
404
405 BUG_ON_OPT_NEG(unset);
406
407 if (!arg)
408 return -1;
409 if (msg->buf.len)
410 strbuf_addstr(&(msg->buf), "\n\n");
411 strbuf_addstr(&(msg->buf), arg);
412 msg->given = 1;
413 return 0;
414 }
415
416 static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
417 {
418 if (name[0] == '-')
419 return -1;
420
421 strbuf_reset(sb);
422 strbuf_addf(sb, "refs/tags/%s", name);
423
424 return check_refname_format(sb->buf, 0);
425 }
426
427 int cmd_tag(int argc, const char **argv, const char *prefix)
428 {
429 struct strbuf buf = STRBUF_INIT;
430 struct strbuf ref = STRBUF_INIT;
431 struct strbuf reflog_msg = STRBUF_INIT;
432 struct object_id object, prev;
433 const char *object_ref, *tag;
434 struct create_tag_options opt;
435 char *cleanup_arg = NULL;
436 int create_reflog = 0;
437 int annotate = 0, force = 0;
438 int cmdmode = 0, create_tag_object = 0;
439 char *msgfile = NULL;
440 const char *keyid = NULL;
441 struct msg_arg msg = { .buf = STRBUF_INIT };
442 struct ref_transaction *transaction;
443 struct strbuf err = STRBUF_INIT;
444 struct ref_filter filter;
445 struct ref_sorting *sorting;
446 struct string_list sorting_options = STRING_LIST_INIT_DUP;
447 struct ref_format format = REF_FORMAT_INIT;
448 int icase = 0;
449 int edit_flag = 0;
450 struct option options[] = {
451 OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
452 { OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"),
453 N_("print <n> lines of each tag message"),
454 PARSE_OPT_OPTARG, NULL, 1 },
455 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'),
456 OPT_CMDMODE('v', "verify", &cmdmode, N_("verify tags"), 'v'),
457
458 OPT_GROUP(N_("Tag creation options")),
459 OPT_BOOL('a', "annotate", &annotate,
460 N_("annotated tag, needs a message")),
461 OPT_CALLBACK_F('m', "message", &msg, N_("message"),
462 N_("tag message"), PARSE_OPT_NONEG, parse_msg_arg),
463 OPT_FILENAME('F', "file", &msgfile, N_("read message from file")),
464 OPT_BOOL('e', "edit", &edit_flag, N_("force edit of tag message")),
465 OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")),
466 OPT_CLEANUP(&cleanup_arg),
467 OPT_STRING('u', "local-user", &keyid, N_("key-id"),
468 N_("use another key to sign the tag")),
469 OPT__FORCE(&force, N_("replace the tag if exists"), 0),
470 OPT_BOOL(0, "create-reflog", &create_reflog, N_("create a reflog")),
471
472 OPT_GROUP(N_("Tag listing options")),
473 OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
474 OPT_CONTAINS(&filter.with_commit, N_("print only tags that contain the commit")),
475 OPT_NO_CONTAINS(&filter.no_commit, N_("print only tags that don't contain the commit")),
476 OPT_WITH(&filter.with_commit, N_("print only tags that contain the commit")),
477 OPT_WITHOUT(&filter.no_commit, N_("print only tags that don't contain the commit")),
478 OPT_MERGED(&filter, N_("print only tags that are merged")),
479 OPT_NO_MERGED(&filter, N_("print only tags that are not merged")),
480 OPT_BOOL(0, "omit-empty", &omit_empty,
481 N_("do not output a newline after empty formatted refs")),
482 OPT_REF_SORT(&sorting_options),
483 {
484 OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
485 N_("print only tags of the object"), PARSE_OPT_LASTARG_DEFAULT,
486 parse_opt_object_name, (intptr_t) "HEAD"
487 },
488 OPT_STRING( 0 , "format", &format.format, N_("format"),
489 N_("format to use for the output")),
490 OPT__COLOR(&format.use_color, N_("respect format colors")),
491 OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
492 OPT_END()
493 };
494 int ret = 0;
495 const char *only_in_list = NULL;
496 char *path = NULL;
497
498 setup_ref_filter_porcelain_msg();
499
500 git_config(git_tag_config, &sorting_options);
501
502 memset(&opt, 0, sizeof(opt));
503 memset(&filter, 0, sizeof(filter));
504 filter.lines = -1;
505 opt.sign = -1;
506
507 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
508
509 if (!cmdmode) {
510 if (argc == 0)
511 cmdmode = 'l';
512 else if (filter.with_commit || filter.no_commit ||
513 filter.reachable_from || filter.unreachable_from ||
514 filter.points_at.nr || filter.lines != -1)
515 cmdmode = 'l';
516 }
517
518 if (cmdmode == 'l')
519 setup_auto_pager("tag", 1);
520
521 if (opt.sign == -1)
522 opt.sign = cmdmode ? 0 : config_sign_tag > 0;
523
524 if (keyid) {
525 opt.sign = 1;
526 set_signing_key(keyid);
527 }
528 create_tag_object = (opt.sign || annotate || msg.given || msgfile);
529
530 if ((create_tag_object || force) && (cmdmode != 0))
531 usage_with_options(git_tag_usage, options);
532
533 finalize_colopts(&colopts, -1);
534 if (cmdmode == 'l' && filter.lines != -1) {
535 if (explicitly_enable_column(colopts))
536 die(_("options '%s' and '%s' cannot be used together"), "--column", "-n");
537 colopts = 0;
538 }
539 sorting = ref_sorting_options(&sorting_options);
540 ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
541 filter.ignore_case = icase;
542 if (cmdmode == 'l') {
543 if (column_active(colopts)) {
544 struct column_options copts;
545 memset(&copts, 0, sizeof(copts));
546 copts.padding = 2;
547 run_column_filter(colopts, &copts);
548 }
549 filter.name_patterns = argv;
550 ret = list_tags(&filter, sorting, &format);
551 if (column_active(colopts))
552 stop_column_filter();
553 goto cleanup;
554 }
555 if (filter.lines != -1)
556 only_in_list = "-n";
557 else if (filter.with_commit)
558 only_in_list = "--contains";
559 else if (filter.no_commit)
560 only_in_list = "--no-contains";
561 else if (filter.points_at.nr)
562 only_in_list = "--points-at";
563 else if (filter.reachable_from)
564 only_in_list = "--merged";
565 else if (filter.unreachable_from)
566 only_in_list = "--no-merged";
567 if (only_in_list)
568 die(_("the '%s' option is only allowed in list mode"), only_in_list);
569 if (cmdmode == 'd') {
570 ret = delete_tags(argv);
571 goto cleanup;
572 }
573 if (cmdmode == 'v') {
574 if (format.format && verify_ref_format(&format))
575 usage_with_options(git_tag_usage, options);
576 ret = for_each_tag_name(argv, verify_tag, &format);
577 goto cleanup;
578 }
579
580 if (msg.given || msgfile) {
581 if (msg.given && msgfile)
582 die(_("options '%s' and '%s' cannot be used together"), "-F", "-m");
583 if (msg.given)
584 strbuf_addbuf(&buf, &(msg.buf));
585 else {
586 if (!strcmp(msgfile, "-")) {
587 if (strbuf_read(&buf, 0, 1024) < 0)
588 die_errno(_("cannot read '%s'"), msgfile);
589 } else {
590 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
591 die_errno(_("could not open or read '%s'"),
592 msgfile);
593 }
594 }
595 }
596
597 tag = argv[0];
598
599 object_ref = argc == 2 ? argv[1] : "HEAD";
600 if (argc > 2)
601 die(_("too many arguments"));
602
603 if (repo_get_oid(the_repository, object_ref, &object))
604 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
605
606 if (strbuf_check_tag_ref(&ref, tag))
607 die(_("'%s' is not a valid tag name."), tag);
608
609 if (read_ref(ref.buf, &prev))
610 oidclr(&prev);
611 else if (!force)
612 die(_("tag '%s' already exists"), tag);
613
614 opt.message_given = msg.given || msgfile;
615 opt.use_editor = edit_flag;
616
617 if (!cleanup_arg || !strcmp(cleanup_arg, "strip"))
618 opt.cleanup_mode = CLEANUP_ALL;
619 else if (!strcmp(cleanup_arg, "verbatim"))
620 opt.cleanup_mode = CLEANUP_NONE;
621 else if (!strcmp(cleanup_arg, "whitespace"))
622 opt.cleanup_mode = CLEANUP_SPACE;
623 else
624 die(_("Invalid cleanup mode %s"), cleanup_arg);
625
626 create_reflog_msg(&object, &reflog_msg);
627
628 if (create_tag_object) {
629 if (force_sign_annotate && !annotate)
630 opt.sign = 1;
631 path = git_pathdup("TAG_EDITMSG");
632 create_tag(&object, object_ref, tag, &buf, &opt, &prev, &object,
633 path);
634 }
635
636 transaction = ref_transaction_begin(&err);
637 if (!transaction ||
638 ref_transaction_update(transaction, ref.buf, &object, &prev,
639 create_reflog ? REF_FORCE_CREATE_REFLOG : 0,
640 reflog_msg.buf, &err) ||
641 ref_transaction_commit(transaction, &err)) {
642 if (path)
643 fprintf(stderr,
644 _("The tag message has been left in %s\n"),
645 path);
646 die("%s", err.buf);
647 }
648 if (path) {
649 unlink_or_warn(path);
650 free(path);
651 }
652 ref_transaction_free(transaction);
653 if (force && !is_null_oid(&prev) && !oideq(&prev, &object))
654 printf(_("Updated tag '%s' (was %s)\n"), tag,
655 repo_find_unique_abbrev(the_repository, &prev, DEFAULT_ABBREV));
656
657 cleanup:
658 ref_sorting_release(sorting);
659 strbuf_release(&buf);
660 strbuf_release(&ref);
661 strbuf_release(&reflog_msg);
662 strbuf_release(&msg.buf);
663 strbuf_release(&err);
664 free(msgfile);
665 return ret;
666 }