]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/tag.c
Merge branch 'tb/pack-bitmap-drop-unused-struct-member' into maint-2.43
[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 "builtin.h"
10 #include "advice.h"
11 #include "config.h"
12 #include "editor.h"
13 #include "environment.h"
14 #include "gettext.h"
15 #include "hex.h"
16 #include "refs.h"
17 #include "object-name.h"
18 #include "object-store-ll.h"
19 #include "path.h"
20 #include "tag.h"
21 #include "parse-options.h"
22 #include "diff.h"
23 #include "revision.h"
24 #include "gpg-interface.h"
25 #include "oid-array.h"
26 #include "column.h"
27 #include "ref-filter.h"
28 #include "date.h"
29 #include "write-or-die.h"
30
31 static const char * const git_tag_usage[] = {
32 N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] [-e]\n"
33 " <tagname> [<commit> | <object>]"),
34 N_("git tag -d <tagname>..."),
35 N_("git tag [-n[<num>]] -l [--contains <commit>] [--no-contains <commit>]\n"
36 " [--points-at <object>] [--column[=<options>] | --no-column]\n"
37 " [--create-reflog] [--sort=<key>] [--format=<format>]\n"
38 " [--merged <commit>] [--no-merged <commit>] [<pattern>...]"),
39 N_("git tag -v [--format=<format>] <tagname>..."),
40 NULL
41 };
42
43 static unsigned int colopts;
44 static int force_sign_annotate;
45 static int config_sign_tag = -1; /* unspecified */
46 static int omit_empty = 0;
47
48 static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
49 struct ref_format *format)
50 {
51 struct ref_array array;
52 struct strbuf output = STRBUF_INIT;
53 struct strbuf err = STRBUF_INIT;
54 char *to_free = NULL;
55 int i;
56
57 memset(&array, 0, sizeof(array));
58
59 if (filter->lines == -1)
60 filter->lines = 0;
61
62 if (!format->format) {
63 if (filter->lines) {
64 to_free = xstrfmt("%s %%(contents:lines=%d)",
65 "%(align:15)%(refname:lstrip=2)%(end)",
66 filter->lines);
67 format->format = to_free;
68 } else
69 format->format = "%(refname:lstrip=2)";
70 }
71
72 if (verify_ref_format(format))
73 die(_("unable to parse format string"));
74 filter->with_commit_tag_algo = 1;
75 filter_refs(&array, filter, FILTER_REFS_TAGS);
76 filter_ahead_behind(the_repository, format, &array);
77 ref_array_sort(sorting, &array);
78
79 for (i = 0; i < array.nr; i++) {
80 strbuf_reset(&output);
81 strbuf_reset(&err);
82 if (format_ref_array_item(array.items[i], format, &output, &err))
83 die("%s", err.buf);
84 fwrite(output.buf, 1, output.len, stdout);
85 if (output.len || !omit_empty)
86 putchar('\n');
87 }
88
89 strbuf_release(&err);
90 strbuf_release(&output);
91 ref_array_clear(&array);
92 free(to_free);
93
94 return 0;
95 }
96
97 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
98 const struct object_id *oid, void *cb_data);
99
100 static int for_each_tag_name(const char **argv, each_tag_name_fn fn,
101 void *cb_data)
102 {
103 const char **p;
104 struct strbuf ref = STRBUF_INIT;
105 int had_error = 0;
106 struct object_id oid;
107
108 for (p = argv; *p; p++) {
109 strbuf_reset(&ref);
110 strbuf_addf(&ref, "refs/tags/%s", *p);
111 if (read_ref(ref.buf, &oid)) {
112 error(_("tag '%s' not found."), *p);
113 had_error = 1;
114 continue;
115 }
116 if (fn(*p, ref.buf, &oid, cb_data))
117 had_error = 1;
118 }
119 strbuf_release(&ref);
120 return had_error;
121 }
122
123 static int collect_tags(const char *name UNUSED, const char *ref,
124 const struct object_id *oid, void *cb_data)
125 {
126 struct string_list *ref_list = cb_data;
127
128 string_list_append(ref_list, ref);
129 ref_list->items[ref_list->nr - 1].util = oiddup(oid);
130 return 0;
131 }
132
133 static int delete_tags(const char **argv)
134 {
135 int result;
136 struct string_list refs_to_delete = STRING_LIST_INIT_DUP;
137 struct string_list_item *item;
138
139 result = for_each_tag_name(argv, collect_tags, (void *)&refs_to_delete);
140 if (delete_refs(NULL, &refs_to_delete, REF_NO_DEREF))
141 result = 1;
142
143 for_each_string_list_item(item, &refs_to_delete) {
144 const char *name = item->string;
145 struct object_id *oid = item->util;
146 if (!ref_exists(name))
147 printf(_("Deleted tag '%s' (was %s)\n"),
148 item->string + 10,
149 repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV));
150
151 free(oid);
152 }
153 string_list_clear(&refs_to_delete, 0);
154 return result;
155 }
156
157 static int verify_tag(const char *name, const char *ref UNUSED,
158 const struct object_id *oid, void *cb_data)
159 {
160 int flags;
161 struct ref_format *format = cb_data;
162 flags = GPG_VERIFY_VERBOSE;
163
164 if (format->format)
165 flags = GPG_VERIFY_OMIT_STATUS;
166
167 if (gpg_verify_tag(oid, name, flags))
168 return -1;
169
170 if (format->format)
171 pretty_print_ref(name, oid, format);
172
173 return 0;
174 }
175
176 static int do_sign(struct strbuf *buffer)
177 {
178 return sign_buffer(buffer, buffer, get_signing_key()) ? -1 : 0;
179 }
180
181 static const char tag_template[] =
182 N_("\nWrite a message for tag:\n %s\n"
183 "Lines starting with '%c' will be ignored.\n");
184
185 static const char tag_template_nocleanup[] =
186 N_("\nWrite a message for tag:\n %s\n"
187 "Lines starting with '%c' will be kept; you may remove them"
188 " yourself if you want to.\n");
189
190 static int git_tag_config(const char *var, const char *value,
191 const struct config_context *ctx, 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, ctx, 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, comment_line_char,
317 _(tag_template), tag, comment_line_char);
318 else
319 strbuf_commented_addf(&buf, comment_line_char,
320 _(tag_template_nocleanup), tag, comment_line_char);
321 write_or_die(fd, buf.buf, buf.len);
322 strbuf_release(&buf);
323 }
324 close(fd);
325
326 if (launch_editor(path, buf, NULL)) {
327 fprintf(stderr,
328 _("Please supply the message using either -m or -F option.\n"));
329 exit(1);
330 }
331 }
332
333 if (opt->cleanup_mode != CLEANUP_NONE)
334 strbuf_stripspace(buf,
335 opt->cleanup_mode == CLEANUP_ALL ? comment_line_char : '\0');
336
337 if (!opt->message_given && !buf->len)
338 die(_("no tag message?"));
339
340 strbuf_insert(buf, 0, header.buf, header.len);
341 strbuf_release(&header);
342
343 if (build_tag_object(buf, opt->sign, result) < 0) {
344 if (path)
345 fprintf(stderr, _("The tag message has been left in %s\n"),
346 path);
347 exit(128);
348 }
349 }
350
351 static void create_reflog_msg(const struct object_id *oid, struct strbuf *sb)
352 {
353 enum object_type type;
354 struct commit *c;
355 char *buf;
356 unsigned long size;
357 int subject_len = 0;
358 const char *subject_start;
359
360 char *rla = getenv("GIT_REFLOG_ACTION");
361 if (rla) {
362 strbuf_addstr(sb, rla);
363 } else {
364 strbuf_addstr(sb, "tag: tagging ");
365 strbuf_add_unique_abbrev(sb, oid, DEFAULT_ABBREV);
366 }
367
368 strbuf_addstr(sb, " (");
369 type = oid_object_info(the_repository, oid, NULL);
370 switch (type) {
371 default:
372 strbuf_addstr(sb, "object of unknown type");
373 break;
374 case OBJ_COMMIT:
375 if ((buf = repo_read_object_file(the_repository, oid, &type, &size))) {
376 subject_len = find_commit_subject(buf, &subject_start);
377 strbuf_insert(sb, sb->len, subject_start, subject_len);
378 } else {
379 strbuf_addstr(sb, "commit object");
380 }
381 free(buf);
382
383 if ((c = lookup_commit_reference(the_repository, oid)))
384 strbuf_addf(sb, ", %s", show_date(c->date, 0, DATE_MODE(SHORT)));
385 break;
386 case OBJ_TREE:
387 strbuf_addstr(sb, "tree object");
388 break;
389 case OBJ_BLOB:
390 strbuf_addstr(sb, "blob object");
391 break;
392 case OBJ_TAG:
393 strbuf_addstr(sb, "other tag object");
394 break;
395 }
396 strbuf_addch(sb, ')');
397 }
398
399 struct msg_arg {
400 int given;
401 struct strbuf buf;
402 };
403
404 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
405 {
406 struct msg_arg *msg = opt->value;
407
408 BUG_ON_OPT_NEG(unset);
409
410 if (!arg)
411 return -1;
412 if (msg->buf.len)
413 strbuf_addstr(&(msg->buf), "\n\n");
414 strbuf_addstr(&(msg->buf), arg);
415 msg->given = 1;
416 return 0;
417 }
418
419 static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
420 {
421 if (name[0] == '-')
422 return -1;
423
424 strbuf_reset(sb);
425 strbuf_addf(sb, "refs/tags/%s", name);
426
427 return check_refname_format(sb->buf, 0);
428 }
429
430 int cmd_tag(int argc, const char **argv, const char *prefix)
431 {
432 struct strbuf buf = STRBUF_INIT;
433 struct strbuf ref = STRBUF_INIT;
434 struct strbuf reflog_msg = STRBUF_INIT;
435 struct object_id object, prev;
436 const char *object_ref, *tag;
437 struct create_tag_options opt;
438 char *cleanup_arg = NULL;
439 int create_reflog = 0;
440 int annotate = 0, force = 0;
441 int cmdmode = 0, create_tag_object = 0;
442 char *msgfile = NULL;
443 const char *keyid = NULL;
444 struct msg_arg msg = { .buf = STRBUF_INIT };
445 struct ref_transaction *transaction;
446 struct strbuf err = STRBUF_INIT;
447 struct ref_filter filter = REF_FILTER_INIT;
448 struct ref_sorting *sorting;
449 struct string_list sorting_options = STRING_LIST_INIT_DUP;
450 struct ref_format format = REF_FORMAT_INIT;
451 int icase = 0;
452 int edit_flag = 0;
453 struct option options[] = {
454 OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
455 { OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"),
456 N_("print <n> lines of each tag message"),
457 PARSE_OPT_OPTARG, NULL, 1 },
458 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'),
459 OPT_CMDMODE('v', "verify", &cmdmode, N_("verify tags"), 'v'),
460
461 OPT_GROUP(N_("Tag creation options")),
462 OPT_BOOL('a', "annotate", &annotate,
463 N_("annotated tag, needs a message")),
464 OPT_CALLBACK_F('m', "message", &msg, N_("message"),
465 N_("tag message"), PARSE_OPT_NONEG, parse_msg_arg),
466 OPT_FILENAME('F', "file", &msgfile, N_("read message from file")),
467 OPT_BOOL('e', "edit", &edit_flag, N_("force edit of tag message")),
468 OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")),
469 OPT_CLEANUP(&cleanup_arg),
470 OPT_STRING('u', "local-user", &keyid, N_("key-id"),
471 N_("use another key to sign the tag")),
472 OPT__FORCE(&force, N_("replace the tag if exists"), 0),
473 OPT_BOOL(0, "create-reflog", &create_reflog, N_("create a reflog")),
474
475 OPT_GROUP(N_("Tag listing options")),
476 OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
477 OPT_CONTAINS(&filter.with_commit, N_("print only tags that contain the commit")),
478 OPT_NO_CONTAINS(&filter.no_commit, N_("print only tags that don't contain the commit")),
479 OPT_WITH(&filter.with_commit, N_("print only tags that contain the commit")),
480 OPT_WITHOUT(&filter.no_commit, N_("print only tags that don't contain the commit")),
481 OPT_MERGED(&filter, N_("print only tags that are merged")),
482 OPT_NO_MERGED(&filter, N_("print only tags that are not merged")),
483 OPT_BOOL(0, "omit-empty", &omit_empty,
484 N_("do not output a newline after empty formatted refs")),
485 OPT_REF_SORT(&sorting_options),
486 {
487 OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
488 N_("print only tags of the object"), PARSE_OPT_LASTARG_DEFAULT,
489 parse_opt_object_name, (intptr_t) "HEAD"
490 },
491 OPT_STRING( 0 , "format", &format.format, N_("format"),
492 N_("format to use for the output")),
493 OPT__COLOR(&format.use_color, N_("respect format colors")),
494 OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
495 OPT_END()
496 };
497 int ret = 0;
498 const char *only_in_list = NULL;
499 char *path = NULL;
500
501 setup_ref_filter_porcelain_msg();
502
503 git_config(git_tag_config, &sorting_options);
504
505 memset(&opt, 0, sizeof(opt));
506 filter.lines = -1;
507 opt.sign = -1;
508
509 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
510
511 if (!cmdmode) {
512 if (argc == 0)
513 cmdmode = 'l';
514 else if (filter.with_commit || filter.no_commit ||
515 filter.reachable_from || filter.unreachable_from ||
516 filter.points_at.nr || filter.lines != -1)
517 cmdmode = 'l';
518 }
519
520 if (cmdmode == 'l')
521 setup_auto_pager("tag", 1);
522
523 if (opt.sign == -1)
524 opt.sign = cmdmode ? 0 : config_sign_tag > 0;
525
526 if (keyid) {
527 opt.sign = 1;
528 set_signing_key(keyid);
529 }
530 create_tag_object = (opt.sign || annotate || msg.given || msgfile);
531
532 if ((create_tag_object || force) && (cmdmode != 0))
533 usage_with_options(git_tag_usage, options);
534
535 finalize_colopts(&colopts, -1);
536 if (cmdmode == 'l' && filter.lines != -1) {
537 if (explicitly_enable_column(colopts))
538 die(_("options '%s' and '%s' cannot be used together"), "--column", "-n");
539 colopts = 0;
540 }
541 sorting = ref_sorting_options(&sorting_options);
542 ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
543 filter.ignore_case = icase;
544 if (cmdmode == 'l') {
545 if (column_active(colopts)) {
546 struct column_options copts;
547 memset(&copts, 0, sizeof(copts));
548 copts.padding = 2;
549 run_column_filter(colopts, &copts);
550 }
551 filter.name_patterns = argv;
552 ret = list_tags(&filter, sorting, &format);
553 if (column_active(colopts))
554 stop_column_filter();
555 goto cleanup;
556 }
557 if (filter.lines != -1)
558 only_in_list = "-n";
559 else if (filter.with_commit)
560 only_in_list = "--contains";
561 else if (filter.no_commit)
562 only_in_list = "--no-contains";
563 else if (filter.points_at.nr)
564 only_in_list = "--points-at";
565 else if (filter.reachable_from)
566 only_in_list = "--merged";
567 else if (filter.unreachable_from)
568 only_in_list = "--no-merged";
569 if (only_in_list)
570 die(_("the '%s' option is only allowed in list mode"), only_in_list);
571 if (cmdmode == 'd') {
572 ret = delete_tags(argv);
573 goto cleanup;
574 }
575 if (cmdmode == 'v') {
576 if (format.format && verify_ref_format(&format))
577 usage_with_options(git_tag_usage, options);
578 ret = for_each_tag_name(argv, verify_tag, &format);
579 goto cleanup;
580 }
581
582 if (msg.given || msgfile) {
583 if (msg.given && msgfile)
584 die(_("options '%s' and '%s' cannot be used together"), "-F", "-m");
585 if (msg.given)
586 strbuf_addbuf(&buf, &(msg.buf));
587 else {
588 if (!strcmp(msgfile, "-")) {
589 if (strbuf_read(&buf, 0, 1024) < 0)
590 die_errno(_("cannot read '%s'"), msgfile);
591 } else {
592 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
593 die_errno(_("could not open or read '%s'"),
594 msgfile);
595 }
596 }
597 }
598
599 tag = argv[0];
600
601 object_ref = argc == 2 ? argv[1] : "HEAD";
602 if (argc > 2)
603 die(_("too many arguments"));
604
605 if (repo_get_oid(the_repository, object_ref, &object))
606 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
607
608 if (strbuf_check_tag_ref(&ref, tag))
609 die(_("'%s' is not a valid tag name."), tag);
610
611 if (read_ref(ref.buf, &prev))
612 oidclr(&prev);
613 else if (!force)
614 die(_("tag '%s' already exists"), tag);
615
616 opt.message_given = msg.given || msgfile;
617 opt.use_editor = edit_flag;
618
619 if (!cleanup_arg || !strcmp(cleanup_arg, "strip"))
620 opt.cleanup_mode = CLEANUP_ALL;
621 else if (!strcmp(cleanup_arg, "verbatim"))
622 opt.cleanup_mode = CLEANUP_NONE;
623 else if (!strcmp(cleanup_arg, "whitespace"))
624 opt.cleanup_mode = CLEANUP_SPACE;
625 else
626 die(_("Invalid cleanup mode %s"), cleanup_arg);
627
628 create_reflog_msg(&object, &reflog_msg);
629
630 if (create_tag_object) {
631 if (force_sign_annotate && !annotate)
632 opt.sign = 1;
633 path = git_pathdup("TAG_EDITMSG");
634 create_tag(&object, object_ref, tag, &buf, &opt, &prev, &object,
635 path);
636 }
637
638 transaction = ref_transaction_begin(&err);
639 if (!transaction ||
640 ref_transaction_update(transaction, ref.buf, &object, &prev,
641 create_reflog ? REF_FORCE_CREATE_REFLOG : 0,
642 reflog_msg.buf, &err) ||
643 ref_transaction_commit(transaction, &err)) {
644 if (path)
645 fprintf(stderr,
646 _("The tag message has been left in %s\n"),
647 path);
648 die("%s", err.buf);
649 }
650 if (path) {
651 unlink_or_warn(path);
652 free(path);
653 }
654 ref_transaction_free(transaction);
655 if (force && !is_null_oid(&prev) && !oideq(&prev, &object))
656 printf(_("Updated tag '%s' (was %s)\n"), tag,
657 repo_find_unique_abbrev(the_repository, &prev, DEFAULT_ABBREV));
658
659 cleanup:
660 ref_sorting_release(sorting);
661 ref_filter_clear(&filter);
662 strbuf_release(&buf);
663 strbuf_release(&ref);
664 strbuf_release(&reflog_msg);
665 strbuf_release(&msg.buf);
666 strbuf_release(&err);
667 free(msgfile);
668 return ret;
669 }