send-pack: detect when the server doesn't support our hash
[git/raj.git] / builtin / tag.c
blob5cbd80dc3e93f478eb0ff47465a1e4b287b897d2
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>] [--[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, const void *cb_data);
77 static int for_each_tag_name(const char **argv, each_tag_name_fn fn,
78 const 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 delete_tag(const char *name, const char *ref,
101 const struct object_id *oid, const void *cb_data)
103 if (delete_ref(NULL, ref, oid, 0))
104 return 1;
105 printf(_("Deleted tag '%s' (was %s)\n"), name,
106 find_unique_abbrev(oid, DEFAULT_ABBREV));
107 return 0;
110 static int verify_tag(const char *name, const char *ref,
111 const struct object_id *oid, const void *cb_data)
113 int flags;
114 const struct ref_format *format = cb_data;
115 flags = GPG_VERIFY_VERBOSE;
117 if (format->format)
118 flags = GPG_VERIFY_OMIT_STATUS;
120 if (gpg_verify_tag(oid, name, flags))
121 return -1;
123 if (format->format)
124 pretty_print_ref(name, oid, format);
126 return 0;
129 static int do_sign(struct strbuf *buffer)
131 return sign_buffer(buffer, buffer, get_signing_key());
134 static const char tag_template[] =
135 N_("\nWrite a message for tag:\n %s\n"
136 "Lines starting with '%c' will be ignored.\n");
138 static const char tag_template_nocleanup[] =
139 N_("\nWrite a message for tag:\n %s\n"
140 "Lines starting with '%c' will be kept; you may remove them"
141 " yourself if you want to.\n");
143 static int git_tag_config(const char *var, const char *value, void *cb)
145 int status;
146 struct ref_sorting **sorting_tail = (struct ref_sorting **)cb;
148 if (!strcmp(var, "tag.gpgsign")) {
149 config_sign_tag = git_config_bool(var, value);
150 return 0;
153 if (!strcmp(var, "tag.sort")) {
154 if (!value)
155 return config_error_nonbool(var);
156 parse_ref_sorting(sorting_tail, value);
157 return 0;
160 status = git_gpg_config(var, value, cb);
161 if (status)
162 return status;
163 if (!strcmp(var, "tag.forcesignannotated")) {
164 force_sign_annotate = git_config_bool(var, value);
165 return 0;
168 if (starts_with(var, "column."))
169 return git_column_config(var, value, "tag", &colopts);
170 return git_color_default_config(var, value, cb);
173 static void write_tag_body(int fd, const struct object_id *oid)
175 unsigned long size;
176 enum object_type type;
177 char *buf, *sp;
179 buf = read_object_file(oid, &type, &size);
180 if (!buf)
181 return;
182 /* skip header */
183 sp = strstr(buf, "\n\n");
185 if (!sp || !size || type != OBJ_TAG) {
186 free(buf);
187 return;
189 sp += 2; /* skip the 2 LFs */
190 write_or_die(fd, sp, parse_signature(sp, buf + size - sp));
192 free(buf);
195 static int build_tag_object(struct strbuf *buf, int sign, struct object_id *result)
197 if (sign && do_sign(buf) < 0)
198 return error(_("unable to sign the tag"));
199 if (write_object_file(buf->buf, buf->len, tag_type, result) < 0)
200 return error(_("unable to write tag file"));
201 return 0;
204 struct create_tag_options {
205 unsigned int message_given:1;
206 unsigned int use_editor:1;
207 unsigned int sign;
208 enum {
209 CLEANUP_NONE,
210 CLEANUP_SPACE,
211 CLEANUP_ALL
212 } cleanup_mode;
215 static const char message_advice_nested_tag[] =
216 N_("You have created a nested tag. The object referred to by your new tag is\n"
217 "already a tag. If you meant to tag the object that it points to, use:\n"
218 "\n"
219 "\tgit tag -f %s %s^{}");
221 static void create_tag(const struct object_id *object, const char *object_ref,
222 const char *tag,
223 struct strbuf *buf, struct create_tag_options *opt,
224 struct object_id *prev, struct object_id *result)
226 enum object_type type;
227 struct strbuf header = STRBUF_INIT;
228 char *path = NULL;
230 type = oid_object_info(the_repository, object, NULL);
231 if (type <= OBJ_NONE)
232 die(_("bad object type."));
234 if (type == OBJ_TAG)
235 advise_if_enabled(ADVICE_NESTED_TAG, _(message_advice_nested_tag),
236 tag, object_ref);
238 strbuf_addf(&header,
239 "object %s\n"
240 "type %s\n"
241 "tag %s\n"
242 "tagger %s\n\n",
243 oid_to_hex(object),
244 type_name(type),
245 tag,
246 git_committer_info(IDENT_STRICT));
248 if (!opt->message_given || opt->use_editor) {
249 int fd;
251 /* write the template message before editing: */
252 path = git_pathdup("TAG_EDITMSG");
253 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
254 if (fd < 0)
255 die_errno(_("could not create file '%s'"), path);
257 if (opt->message_given) {
258 write_or_die(fd, buf->buf, buf->len);
259 strbuf_reset(buf);
260 } else if (!is_null_oid(prev)) {
261 write_tag_body(fd, prev);
262 } else {
263 struct strbuf buf = STRBUF_INIT;
264 strbuf_addch(&buf, '\n');
265 if (opt->cleanup_mode == CLEANUP_ALL)
266 strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
267 else
268 strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char);
269 write_or_die(fd, buf.buf, buf.len);
270 strbuf_release(&buf);
272 close(fd);
274 if (launch_editor(path, buf, NULL)) {
275 fprintf(stderr,
276 _("Please supply the message using either -m or -F option.\n"));
277 exit(1);
281 if (opt->cleanup_mode != CLEANUP_NONE)
282 strbuf_stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
284 if (!opt->message_given && !buf->len)
285 die(_("no tag message?"));
287 strbuf_insert(buf, 0, header.buf, header.len);
288 strbuf_release(&header);
290 if (build_tag_object(buf, opt->sign, result) < 0) {
291 if (path)
292 fprintf(stderr, _("The tag message has been left in %s\n"),
293 path);
294 exit(128);
296 if (path) {
297 unlink_or_warn(path);
298 free(path);
302 static void create_reflog_msg(const struct object_id *oid, struct strbuf *sb)
304 enum object_type type;
305 struct commit *c;
306 char *buf;
307 unsigned long size;
308 int subject_len = 0;
309 const char *subject_start;
311 char *rla = getenv("GIT_REFLOG_ACTION");
312 if (rla) {
313 strbuf_addstr(sb, rla);
314 } else {
315 strbuf_addstr(sb, "tag: tagging ");
316 strbuf_add_unique_abbrev(sb, oid, DEFAULT_ABBREV);
319 strbuf_addstr(sb, " (");
320 type = oid_object_info(the_repository, oid, NULL);
321 switch (type) {
322 default:
323 strbuf_addstr(sb, "object of unknown type");
324 break;
325 case OBJ_COMMIT:
326 if ((buf = read_object_file(oid, &type, &size)) != NULL) {
327 subject_len = find_commit_subject(buf, &subject_start);
328 strbuf_insert(sb, sb->len, subject_start, subject_len);
329 } else {
330 strbuf_addstr(sb, "commit object");
332 free(buf);
334 if ((c = lookup_commit_reference(the_repository, oid)) != NULL)
335 strbuf_addf(sb, ", %s", show_date(c->date, 0, DATE_MODE(SHORT)));
336 break;
337 case OBJ_TREE:
338 strbuf_addstr(sb, "tree object");
339 break;
340 case OBJ_BLOB:
341 strbuf_addstr(sb, "blob object");
342 break;
343 case OBJ_TAG:
344 strbuf_addstr(sb, "other tag object");
345 break;
347 strbuf_addch(sb, ')');
350 struct msg_arg {
351 int given;
352 struct strbuf buf;
355 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
357 struct msg_arg *msg = opt->value;
359 BUG_ON_OPT_NEG(unset);
361 if (!arg)
362 return -1;
363 if (msg->buf.len)
364 strbuf_addstr(&(msg->buf), "\n\n");
365 strbuf_addstr(&(msg->buf), arg);
366 msg->given = 1;
367 return 0;
370 static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
372 if (name[0] == '-')
373 return -1;
375 strbuf_reset(sb);
376 strbuf_addf(sb, "refs/tags/%s", name);
378 return check_refname_format(sb->buf, 0);
381 int cmd_tag(int argc, const char **argv, const char *prefix)
383 struct strbuf buf = STRBUF_INIT;
384 struct strbuf ref = STRBUF_INIT;
385 struct strbuf reflog_msg = STRBUF_INIT;
386 struct object_id object, prev;
387 const char *object_ref, *tag;
388 struct create_tag_options opt;
389 char *cleanup_arg = NULL;
390 int create_reflog = 0;
391 int annotate = 0, force = 0;
392 int cmdmode = 0, create_tag_object = 0;
393 const char *msgfile = NULL, *keyid = NULL;
394 struct msg_arg msg = { 0, STRBUF_INIT };
395 struct ref_transaction *transaction;
396 struct strbuf err = STRBUF_INIT;
397 struct ref_filter filter;
398 static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
399 struct ref_format format = REF_FORMAT_INIT;
400 int icase = 0;
401 int edit_flag = 0;
402 struct option options[] = {
403 OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
404 { OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"),
405 N_("print <n> lines of each tag message"),
406 PARSE_OPT_OPTARG, NULL, 1 },
407 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'),
408 OPT_CMDMODE('v', "verify", &cmdmode, N_("verify tags"), 'v'),
410 OPT_GROUP(N_("Tag creation options")),
411 OPT_BOOL('a', "annotate", &annotate,
412 N_("annotated tag, needs a message")),
413 OPT_CALLBACK_F('m', "message", &msg, N_("message"),
414 N_("tag message"), PARSE_OPT_NONEG, parse_msg_arg),
415 OPT_FILENAME('F', "file", &msgfile, N_("read message from file")),
416 OPT_BOOL('e', "edit", &edit_flag, N_("force edit of tag message")),
417 OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")),
418 OPT_CLEANUP(&cleanup_arg),
419 OPT_STRING('u', "local-user", &keyid, N_("key-id"),
420 N_("use another key to sign the tag")),
421 OPT__FORCE(&force, N_("replace the tag if exists"), 0),
422 OPT_BOOL(0, "create-reflog", &create_reflog, N_("create a reflog")),
424 OPT_GROUP(N_("Tag listing options")),
425 OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
426 OPT_CONTAINS(&filter.with_commit, N_("print only tags that contain the commit")),
427 OPT_NO_CONTAINS(&filter.no_commit, N_("print only tags that don't contain the commit")),
428 OPT_WITH(&filter.with_commit, N_("print only tags that contain the commit")),
429 OPT_WITHOUT(&filter.no_commit, N_("print only tags that don't contain the commit")),
430 OPT_MERGED(&filter, N_("print only tags that are merged")),
431 OPT_NO_MERGED(&filter, N_("print only tags that are not merged")),
432 OPT_REF_SORT(sorting_tail),
434 OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
435 N_("print only tags of the object"), PARSE_OPT_LASTARG_DEFAULT,
436 parse_opt_object_name, (intptr_t) "HEAD"
438 OPT_STRING( 0 , "format", &format.format, N_("format"),
439 N_("format to use for the output")),
440 OPT__COLOR(&format.use_color, N_("respect format colors")),
441 OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
442 OPT_END()
445 setup_ref_filter_porcelain_msg();
447 git_config(git_tag_config, sorting_tail);
449 memset(&opt, 0, sizeof(opt));
450 memset(&filter, 0, sizeof(filter));
451 filter.lines = -1;
452 opt.sign = -1;
454 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
456 if (!cmdmode) {
457 if (argc == 0)
458 cmdmode = 'l';
459 else if (filter.with_commit || filter.no_commit ||
460 filter.points_at.nr || filter.merge_commit ||
461 filter.lines != -1)
462 cmdmode = 'l';
465 if (cmdmode == 'l')
466 setup_auto_pager("tag", 1);
468 if (opt.sign == -1)
469 opt.sign = cmdmode ? 0 : config_sign_tag > 0;
471 if (keyid) {
472 opt.sign = 1;
473 set_signing_key(keyid);
475 create_tag_object = (opt.sign || annotate || msg.given || msgfile);
477 if ((create_tag_object || force) && (cmdmode != 0))
478 usage_with_options(git_tag_usage, options);
480 finalize_colopts(&colopts, -1);
481 if (cmdmode == 'l' && filter.lines != -1) {
482 if (explicitly_enable_column(colopts))
483 die(_("--column and -n are incompatible"));
484 colopts = 0;
486 if (!sorting)
487 sorting = ref_default_sorting();
488 ref_sorting_icase_all(sorting, icase);
489 filter.ignore_case = icase;
490 if (cmdmode == 'l') {
491 int ret;
492 if (column_active(colopts)) {
493 struct column_options copts;
494 memset(&copts, 0, sizeof(copts));
495 copts.padding = 2;
496 run_column_filter(colopts, &copts);
498 filter.name_patterns = argv;
499 ret = list_tags(&filter, sorting, &format);
500 if (column_active(colopts))
501 stop_column_filter();
502 return ret;
504 if (filter.lines != -1)
505 die(_("-n option is only allowed in list mode"));
506 if (filter.with_commit)
507 die(_("--contains option is only allowed in list mode"));
508 if (filter.no_commit)
509 die(_("--no-contains option is only allowed in list mode"));
510 if (filter.points_at.nr)
511 die(_("--points-at option is only allowed in list mode"));
512 if (filter.merge_commit)
513 die(_("--merged and --no-merged options are only allowed in list mode"));
514 if (cmdmode == 'd')
515 return for_each_tag_name(argv, delete_tag, NULL);
516 if (cmdmode == 'v') {
517 if (format.format && verify_ref_format(&format))
518 usage_with_options(git_tag_usage, options);
519 return for_each_tag_name(argv, verify_tag, &format);
522 if (msg.given || msgfile) {
523 if (msg.given && msgfile)
524 die(_("only one -F or -m option is allowed."));
525 if (msg.given)
526 strbuf_addbuf(&buf, &(msg.buf));
527 else {
528 if (!strcmp(msgfile, "-")) {
529 if (strbuf_read(&buf, 0, 1024) < 0)
530 die_errno(_("cannot read '%s'"), msgfile);
531 } else {
532 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
533 die_errno(_("could not open or read '%s'"),
534 msgfile);
539 tag = argv[0];
541 object_ref = argc == 2 ? argv[1] : "HEAD";
542 if (argc > 2)
543 die(_("too many params"));
545 if (get_oid(object_ref, &object))
546 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
548 if (strbuf_check_tag_ref(&ref, tag))
549 die(_("'%s' is not a valid tag name."), tag);
551 if (read_ref(ref.buf, &prev))
552 oidclr(&prev);
553 else if (!force)
554 die(_("tag '%s' already exists"), tag);
556 opt.message_given = msg.given || msgfile;
557 opt.use_editor = edit_flag;
559 if (!cleanup_arg || !strcmp(cleanup_arg, "strip"))
560 opt.cleanup_mode = CLEANUP_ALL;
561 else if (!strcmp(cleanup_arg, "verbatim"))
562 opt.cleanup_mode = CLEANUP_NONE;
563 else if (!strcmp(cleanup_arg, "whitespace"))
564 opt.cleanup_mode = CLEANUP_SPACE;
565 else
566 die(_("Invalid cleanup mode %s"), cleanup_arg);
568 create_reflog_msg(&object, &reflog_msg);
570 if (create_tag_object) {
571 if (force_sign_annotate && !annotate)
572 opt.sign = 1;
573 create_tag(&object, object_ref, tag, &buf, &opt, &prev, &object);
576 transaction = ref_transaction_begin(&err);
577 if (!transaction ||
578 ref_transaction_update(transaction, ref.buf, &object, &prev,
579 create_reflog ? REF_FORCE_CREATE_REFLOG : 0,
580 reflog_msg.buf, &err) ||
581 ref_transaction_commit(transaction, &err))
582 die("%s", err.buf);
583 ref_transaction_free(transaction);
584 if (force && !is_null_oid(&prev) && !oideq(&prev, &object))
585 printf(_("Updated tag '%s' (was %s)\n"), tag,
586 find_unique_abbrev(&prev, DEFAULT_ABBREV));
588 UNLEAK(buf);
589 UNLEAK(ref);
590 UNLEAK(reflog_msg);
591 UNLEAK(msg);
592 UNLEAK(err);
593 return 0;