ci: use upload-artifacts v1 for dockerized jobs
[git/debian.git] / builtin / tag.c
blob82fcfc0982423f3c7ee90fe43d30295023cc7873
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 struct strbuf output = STRBUF_INIT;
43 struct strbuf err = STRBUF_INIT;
44 char *to_free = NULL;
45 int i;
47 memset(&array, 0, sizeof(array));
49 if (filter->lines == -1)
50 filter->lines = 0;
52 if (!format->format) {
53 if (filter->lines) {
54 to_free = xstrfmt("%s %%(contents:lines=%d)",
55 "%(align:15)%(refname:lstrip=2)%(end)",
56 filter->lines);
57 format->format = to_free;
58 } else
59 format->format = "%(refname:lstrip=2)";
62 if (verify_ref_format(format))
63 die(_("unable to parse format string"));
64 filter->with_commit_tag_algo = 1;
65 filter_refs(&array, filter, FILTER_REFS_TAGS);
66 ref_array_sort(sorting, &array);
68 for (i = 0; i < array.nr; i++) {
69 strbuf_reset(&output);
70 strbuf_reset(&err);
71 if (format_ref_array_item(array.items[i], format, &output, &err))
72 die("%s", err.buf);
73 fwrite(output.buf, 1, output.len, stdout);
74 putchar('\n');
77 strbuf_release(&err);
78 strbuf_release(&output);
79 ref_array_clear(&array);
80 free(to_free);
82 return 0;
85 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
86 const struct object_id *oid, void *cb_data);
88 static int for_each_tag_name(const char **argv, each_tag_name_fn fn,
89 void *cb_data)
91 const char **p;
92 struct strbuf ref = STRBUF_INIT;
93 int had_error = 0;
94 struct object_id oid;
96 for (p = argv; *p; p++) {
97 strbuf_reset(&ref);
98 strbuf_addf(&ref, "refs/tags/%s", *p);
99 if (read_ref(ref.buf, &oid)) {
100 error(_("tag '%s' not found."), *p);
101 had_error = 1;
102 continue;
104 if (fn(*p, ref.buf, &oid, cb_data))
105 had_error = 1;
107 strbuf_release(&ref);
108 return had_error;
111 static int collect_tags(const char *name, const char *ref,
112 const struct object_id *oid, void *cb_data)
114 struct string_list *ref_list = cb_data;
116 string_list_append(ref_list, ref);
117 ref_list->items[ref_list->nr - 1].util = oiddup(oid);
118 return 0;
121 static int delete_tags(const char **argv)
123 int result;
124 struct string_list refs_to_delete = STRING_LIST_INIT_DUP;
125 struct string_list_item *item;
127 result = for_each_tag_name(argv, collect_tags, (void *)&refs_to_delete);
128 if (delete_refs(NULL, &refs_to_delete, REF_NO_DEREF))
129 result = 1;
131 for_each_string_list_item(item, &refs_to_delete) {
132 const char *name = item->string;
133 struct object_id *oid = item->util;
134 if (!ref_exists(name))
135 printf(_("Deleted tag '%s' (was %s)\n"),
136 item->string + 10,
137 find_unique_abbrev(oid, DEFAULT_ABBREV));
139 free(oid);
141 string_list_clear(&refs_to_delete, 0);
142 return result;
145 static int verify_tag(const char *name, const char *ref,
146 const struct object_id *oid, void *cb_data)
148 int flags;
149 const struct ref_format *format = cb_data;
150 flags = GPG_VERIFY_VERBOSE;
152 if (format->format)
153 flags = GPG_VERIFY_OMIT_STATUS;
155 if (gpg_verify_tag(oid, name, flags))
156 return -1;
158 if (format->format)
159 pretty_print_ref(name, oid, format);
161 return 0;
164 static int do_sign(struct strbuf *buffer)
166 return sign_buffer(buffer, buffer, get_signing_key());
169 static const char tag_template[] =
170 N_("\nWrite a message for tag:\n %s\n"
171 "Lines starting with '%c' will be ignored.\n");
173 static const char tag_template_nocleanup[] =
174 N_("\nWrite a message for tag:\n %s\n"
175 "Lines starting with '%c' will be kept; you may remove them"
176 " yourself if you want to.\n");
178 static int git_tag_config(const char *var, const char *value, void *cb)
180 int status;
181 struct ref_sorting **sorting_tail = (struct ref_sorting **)cb;
183 if (!strcmp(var, "tag.gpgsign")) {
184 config_sign_tag = git_config_bool(var, value);
185 return 0;
188 if (!strcmp(var, "tag.sort")) {
189 if (!value)
190 return config_error_nonbool(var);
191 parse_ref_sorting(sorting_tail, value);
192 return 0;
195 status = git_gpg_config(var, value, cb);
196 if (status)
197 return status;
198 if (!strcmp(var, "tag.forcesignannotated")) {
199 force_sign_annotate = git_config_bool(var, value);
200 return 0;
203 if (starts_with(var, "column."))
204 return git_column_config(var, value, "tag", &colopts);
205 return git_color_default_config(var, value, cb);
208 static void write_tag_body(int fd, const struct object_id *oid)
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;
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;
223 /* skip header */
224 sp = strstr(buf, "\n\n");
226 if (!sp || !size || type != OBJ_TAG) {
227 free(buf);
228 return;
230 sp += 2; /* skip the 2 LFs */
231 write_or_die(fd, sp, buf + size - sp);
233 free(orig);
234 strbuf_release(&payload);
235 strbuf_release(&signature);
238 static int build_tag_object(struct strbuf *buf, int sign, struct object_id *result)
240 if (sign && do_sign(buf) < 0)
241 return error(_("unable to sign the tag"));
242 if (write_object_file(buf->buf, buf->len, tag_type, result) < 0)
243 return error(_("unable to write tag file"));
244 return 0;
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;
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^{}");
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)
269 enum object_type type;
270 struct strbuf header = STRBUF_INIT;
271 char *path = NULL;
273 type = oid_object_info(the_repository, object, NULL);
274 if (type <= OBJ_NONE)
275 die(_("bad object type."));
277 if (type == OBJ_TAG)
278 advise_if_enabled(ADVICE_NESTED_TAG, _(message_advice_nested_tag),
279 tag, object_ref);
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));
291 if (!opt->message_given || opt->use_editor) {
292 int fd;
294 /* write the template message before editing: */
295 path = git_pathdup("TAG_EDITMSG");
296 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
297 if (fd < 0)
298 die_errno(_("could not create file '%s'"), path);
300 if (opt->message_given) {
301 write_or_die(fd, buf->buf, buf->len);
302 strbuf_reset(buf);
303 } else if (!is_null_oid(prev)) {
304 write_tag_body(fd, prev);
305 } else {
306 struct strbuf buf = STRBUF_INIT;
307 strbuf_addch(&buf, '\n');
308 if (opt->cleanup_mode == CLEANUP_ALL)
309 strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
310 else
311 strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char);
312 write_or_die(fd, buf.buf, buf.len);
313 strbuf_release(&buf);
315 close(fd);
317 if (launch_editor(path, buf, NULL)) {
318 fprintf(stderr,
319 _("Please supply the message using either -m or -F option.\n"));
320 exit(1);
324 if (opt->cleanup_mode != CLEANUP_NONE)
325 strbuf_stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
327 if (!opt->message_given && !buf->len)
328 die(_("no tag message?"));
330 strbuf_insert(buf, 0, header.buf, header.len);
331 strbuf_release(&header);
333 if (build_tag_object(buf, opt->sign, result) < 0) {
334 if (path)
335 fprintf(stderr, _("The tag message has been left in %s\n"),
336 path);
337 exit(128);
339 if (path) {
340 unlink_or_warn(path);
341 free(path);
345 static void create_reflog_msg(const struct object_id *oid, struct strbuf *sb)
347 enum object_type type;
348 struct commit *c;
349 char *buf;
350 unsigned long size;
351 int subject_len = 0;
352 const char *subject_start;
354 char *rla = getenv("GIT_REFLOG_ACTION");
355 if (rla) {
356 strbuf_addstr(sb, rla);
357 } else {
358 strbuf_addstr(sb, "tag: tagging ");
359 strbuf_add_unique_abbrev(sb, oid, DEFAULT_ABBREV);
362 strbuf_addstr(sb, " (");
363 type = oid_object_info(the_repository, oid, NULL);
364 switch (type) {
365 default:
366 strbuf_addstr(sb, "object of unknown type");
367 break;
368 case OBJ_COMMIT:
369 if ((buf = read_object_file(oid, &type, &size)) != NULL) {
370 subject_len = find_commit_subject(buf, &subject_start);
371 strbuf_insert(sb, sb->len, subject_start, subject_len);
372 } else {
373 strbuf_addstr(sb, "commit object");
375 free(buf);
377 if ((c = lookup_commit_reference(the_repository, oid)) != NULL)
378 strbuf_addf(sb, ", %s", show_date(c->date, 0, DATE_MODE(SHORT)));
379 break;
380 case OBJ_TREE:
381 strbuf_addstr(sb, "tree object");
382 break;
383 case OBJ_BLOB:
384 strbuf_addstr(sb, "blob object");
385 break;
386 case OBJ_TAG:
387 strbuf_addstr(sb, "other tag object");
388 break;
390 strbuf_addch(sb, ')');
393 struct msg_arg {
394 int given;
395 struct strbuf buf;
398 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
400 struct msg_arg *msg = opt->value;
402 BUG_ON_OPT_NEG(unset);
404 if (!arg)
405 return -1;
406 if (msg->buf.len)
407 strbuf_addstr(&(msg->buf), "\n\n");
408 strbuf_addstr(&(msg->buf), arg);
409 msg->given = 1;
410 return 0;
413 static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
415 if (name[0] == '-')
416 return -1;
418 strbuf_reset(sb);
419 strbuf_addf(sb, "refs/tags/%s", name);
421 return check_refname_format(sb->buf, 0);
424 int cmd_tag(int argc, const char **argv, const char *prefix)
426 struct strbuf buf = STRBUF_INIT;
427 struct strbuf ref = STRBUF_INIT;
428 struct strbuf reflog_msg = STRBUF_INIT;
429 struct object_id object, prev;
430 const char *object_ref, *tag;
431 struct create_tag_options opt;
432 char *cleanup_arg = NULL;
433 int create_reflog = 0;
434 int annotate = 0, force = 0;
435 int cmdmode = 0, create_tag_object = 0;
436 const char *msgfile = NULL, *keyid = NULL;
437 struct msg_arg msg = { 0, STRBUF_INIT };
438 struct ref_transaction *transaction;
439 struct strbuf err = STRBUF_INIT;
440 struct ref_filter filter;
441 static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
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'),
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")),
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_tail),
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"
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()
488 setup_ref_filter_porcelain_msg();
490 git_config(git_tag_config, sorting_tail);
492 memset(&opt, 0, sizeof(opt));
493 memset(&filter, 0, sizeof(filter));
494 filter.lines = -1;
495 opt.sign = -1;
497 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
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';
508 if (cmdmode == 'l')
509 setup_auto_pager("tag", 1);
511 if (opt.sign == -1)
512 opt.sign = cmdmode ? 0 : config_sign_tag > 0;
514 if (keyid) {
515 opt.sign = 1;
516 set_signing_key(keyid);
518 create_tag_object = (opt.sign || annotate || msg.given || msgfile);
520 if ((create_tag_object || force) && (cmdmode != 0))
521 usage_with_options(git_tag_usage, options);
523 finalize_colopts(&colopts, -1);
524 if (cmdmode == 'l' && filter.lines != -1) {
525 if (explicitly_enable_column(colopts))
526 die(_("--column and -n are incompatible"));
527 colopts = 0;
529 if (!sorting)
530 sorting = ref_default_sorting();
531 ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
532 filter.ignore_case = icase;
533 if (cmdmode == 'l') {
534 int ret;
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);
541 filter.name_patterns = argv;
542 ret = list_tags(&filter, sorting, &format);
543 if (column_active(colopts))
544 stop_column_filter();
545 return ret;
547 if (filter.lines != -1)
548 die(_("-n option is only allowed in list mode"));
549 if (filter.with_commit)
550 die(_("--contains option is only allowed in list mode"));
551 if (filter.no_commit)
552 die(_("--no-contains option is only allowed in list mode"));
553 if (filter.points_at.nr)
554 die(_("--points-at option is only allowed in list mode"));
555 if (filter.reachable_from || filter.unreachable_from)
556 die(_("--merged and --no-merged options are only allowed in list mode"));
557 if (cmdmode == 'd')
558 return delete_tags(argv);
559 if (cmdmode == 'v') {
560 if (format.format && verify_ref_format(&format))
561 usage_with_options(git_tag_usage, options);
562 return for_each_tag_name(argv, verify_tag, &format);
565 if (msg.given || msgfile) {
566 if (msg.given && msgfile)
567 die(_("only one -F or -m option is allowed."));
568 if (msg.given)
569 strbuf_addbuf(&buf, &(msg.buf));
570 else {
571 if (!strcmp(msgfile, "-")) {
572 if (strbuf_read(&buf, 0, 1024) < 0)
573 die_errno(_("cannot read '%s'"), msgfile);
574 } else {
575 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
576 die_errno(_("could not open or read '%s'"),
577 msgfile);
582 tag = argv[0];
584 object_ref = argc == 2 ? argv[1] : "HEAD";
585 if (argc > 2)
586 die(_("too many arguments"));
588 if (get_oid(object_ref, &object))
589 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
591 if (strbuf_check_tag_ref(&ref, tag))
592 die(_("'%s' is not a valid tag name."), tag);
594 if (read_ref(ref.buf, &prev))
595 oidclr(&prev);
596 else if (!force)
597 die(_("tag '%s' already exists"), tag);
599 opt.message_given = msg.given || msgfile;
600 opt.use_editor = edit_flag;
602 if (!cleanup_arg || !strcmp(cleanup_arg, "strip"))
603 opt.cleanup_mode = CLEANUP_ALL;
604 else if (!strcmp(cleanup_arg, "verbatim"))
605 opt.cleanup_mode = CLEANUP_NONE;
606 else if (!strcmp(cleanup_arg, "whitespace"))
607 opt.cleanup_mode = CLEANUP_SPACE;
608 else
609 die(_("Invalid cleanup mode %s"), cleanup_arg);
611 create_reflog_msg(&object, &reflog_msg);
613 if (create_tag_object) {
614 if (force_sign_annotate && !annotate)
615 opt.sign = 1;
616 create_tag(&object, object_ref, tag, &buf, &opt, &prev, &object);
619 transaction = ref_transaction_begin(&err);
620 if (!transaction ||
621 ref_transaction_update(transaction, ref.buf, &object, &prev,
622 create_reflog ? REF_FORCE_CREATE_REFLOG : 0,
623 reflog_msg.buf, &err) ||
624 ref_transaction_commit(transaction, &err))
625 die("%s", err.buf);
626 ref_transaction_free(transaction);
627 if (force && !is_null_oid(&prev) && !oideq(&prev, &object))
628 printf(_("Updated tag '%s' (was %s)\n"), tag,
629 find_unique_abbrev(&prev, DEFAULT_ABBREV));
631 UNLEAK(buf);
632 UNLEAK(ref);
633 UNLEAK(reflog_msg);
634 UNLEAK(msg);
635 UNLEAK(err);
636 return 0;