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