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