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