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