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