]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/tag.c
Merge branch 'js/azure-pipelines-msvc'
[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 "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 "sha1-array.h"
21 #include "column.h"
22 #include "ref-filter.h"
23
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
32 };
33
34 static unsigned int colopts;
35 static int force_sign_annotate;
36 static int config_sign_tag = -1; /* unspecified */
37
38 static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
39 struct ref_format *format)
40 {
41 struct ref_array array;
42 char *to_free = NULL;
43 int i;
44
45 memset(&array, 0, sizeof(array));
46
47 if (filter->lines == -1)
48 filter->lines = 0;
49
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)";
58 }
59
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);
65
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);
70
71 return 0;
72 }
73
74 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
75 const struct object_id *oid, const void *cb_data);
76
77 static int for_each_tag_name(const char **argv, each_tag_name_fn fn,
78 const void *cb_data)
79 {
80 const char **p;
81 struct strbuf ref = STRBUF_INIT;
82 int had_error = 0;
83 struct object_id oid;
84
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;
92 }
93 if (fn(*p, ref.buf, &oid, cb_data))
94 had_error = 1;
95 }
96 strbuf_release(&ref);
97 return had_error;
98 }
99
100 static int delete_tag(const char *name, const char *ref,
101 const struct object_id *oid, const void *cb_data)
102 {
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;
108 }
109
110 static int verify_tag(const char *name, const char *ref,
111 const struct object_id *oid, const void *cb_data)
112 {
113 int flags;
114 const struct ref_format *format = cb_data;
115 flags = GPG_VERIFY_VERBOSE;
116
117 if (format->format)
118 flags = GPG_VERIFY_OMIT_STATUS;
119
120 if (gpg_verify_tag(oid, name, flags))
121 return -1;
122
123 if (format->format)
124 pretty_print_ref(name, oid, format);
125
126 return 0;
127 }
128
129 static int do_sign(struct strbuf *buffer)
130 {
131 return sign_buffer(buffer, buffer, get_signing_key());
132 }
133
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");
137
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");
142
143 static int git_tag_config(const char *var, const char *value, void *cb)
144 {
145 int status;
146 struct ref_sorting **sorting_tail = (struct ref_sorting **)cb;
147
148 if (!strcmp(var, "tag.gpgsign")) {
149 config_sign_tag = git_config_bool(var, value);
150 return 0;
151 }
152
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;
158 }
159
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;
166 }
167
168 if (starts_with(var, "column."))
169 return git_column_config(var, value, "tag", &colopts);
170 return git_color_default_config(var, value, cb);
171 }
172
173 static void write_tag_body(int fd, const struct object_id *oid)
174 {
175 unsigned long size;
176 enum object_type type;
177 char *buf, *sp;
178
179 buf = read_object_file(oid, &type, &size);
180 if (!buf)
181 return;
182 /* skip header */
183 sp = strstr(buf, "\n\n");
184
185 if (!sp || !size || type != OBJ_TAG) {
186 free(buf);
187 return;
188 }
189 sp += 2; /* skip the 2 LFs */
190 write_or_die(fd, sp, parse_signature(sp, buf + size - sp));
191
192 free(buf);
193 }
194
195 static int build_tag_object(struct strbuf *buf, int sign, struct object_id *result)
196 {
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;
202 }
203
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;
213 };
214
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^{}");
220
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)
225 {
226 enum object_type type;
227 struct strbuf header = STRBUF_INIT;
228 char *path = NULL;
229
230 type = oid_object_info(the_repository, object, NULL);
231 if (type <= OBJ_NONE)
232 die(_("bad object type."));
233
234 if (type == OBJ_TAG && advice_nested_tag)
235 advise(_(message_advice_nested_tag), tag, object_ref);
236
237 strbuf_addf(&header,
238 "object %s\n"
239 "type %s\n"
240 "tag %s\n"
241 "tagger %s\n\n",
242 oid_to_hex(object),
243 type_name(type),
244 tag,
245 git_committer_info(IDENT_STRICT));
246
247 if (!opt->message_given || opt->use_editor) {
248 int fd;
249
250 /* write the template message before editing: */
251 path = git_pathdup("TAG_EDITMSG");
252 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
253 if (fd < 0)
254 die_errno(_("could not create file '%s'"), path);
255
256 if (opt->message_given) {
257 write_or_die(fd, buf->buf, buf->len);
258 strbuf_reset(buf);
259 } else if (!is_null_oid(prev)) {
260 write_tag_body(fd, prev);
261 } else {
262 struct strbuf buf = STRBUF_INIT;
263 strbuf_addch(&buf, '\n');
264 if (opt->cleanup_mode == CLEANUP_ALL)
265 strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
266 else
267 strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char);
268 write_or_die(fd, buf.buf, buf.len);
269 strbuf_release(&buf);
270 }
271 close(fd);
272
273 if (launch_editor(path, buf, NULL)) {
274 fprintf(stderr,
275 _("Please supply the message using either -m or -F option.\n"));
276 exit(1);
277 }
278 }
279
280 if (opt->cleanup_mode != CLEANUP_NONE)
281 strbuf_stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
282
283 if (!opt->message_given && !buf->len)
284 die(_("no tag message?"));
285
286 strbuf_insert(buf, 0, header.buf, header.len);
287 strbuf_release(&header);
288
289 if (build_tag_object(buf, opt->sign, result) < 0) {
290 if (path)
291 fprintf(stderr, _("The tag message has been left in %s\n"),
292 path);
293 exit(128);
294 }
295 if (path) {
296 unlink_or_warn(path);
297 free(path);
298 }
299 }
300
301 static void create_reflog_msg(const struct object_id *oid, struct strbuf *sb)
302 {
303 enum object_type type;
304 struct commit *c;
305 char *buf;
306 unsigned long size;
307 int subject_len = 0;
308 const char *subject_start;
309
310 char *rla = getenv("GIT_REFLOG_ACTION");
311 if (rla) {
312 strbuf_addstr(sb, rla);
313 } else {
314 strbuf_addstr(sb, "tag: tagging ");
315 strbuf_add_unique_abbrev(sb, oid, DEFAULT_ABBREV);
316 }
317
318 strbuf_addstr(sb, " (");
319 type = oid_object_info(the_repository, oid, NULL);
320 switch (type) {
321 default:
322 strbuf_addstr(sb, "object of unknown type");
323 break;
324 case OBJ_COMMIT:
325 if ((buf = read_object_file(oid, &type, &size)) != NULL) {
326 subject_len = find_commit_subject(buf, &subject_start);
327 strbuf_insert(sb, sb->len, subject_start, subject_len);
328 } else {
329 strbuf_addstr(sb, "commit object");
330 }
331 free(buf);
332
333 if ((c = lookup_commit_reference(the_repository, oid)) != NULL)
334 strbuf_addf(sb, ", %s", show_date(c->date, 0, DATE_MODE(SHORT)));
335 break;
336 case OBJ_TREE:
337 strbuf_addstr(sb, "tree object");
338 break;
339 case OBJ_BLOB:
340 strbuf_addstr(sb, "blob object");
341 break;
342 case OBJ_TAG:
343 strbuf_addstr(sb, "other tag object");
344 break;
345 }
346 strbuf_addch(sb, ')');
347 }
348
349 struct msg_arg {
350 int given;
351 struct strbuf buf;
352 };
353
354 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
355 {
356 struct msg_arg *msg = opt->value;
357
358 BUG_ON_OPT_NEG(unset);
359
360 if (!arg)
361 return -1;
362 if (msg->buf.len)
363 strbuf_addstr(&(msg->buf), "\n\n");
364 strbuf_addstr(&(msg->buf), arg);
365 msg->given = 1;
366 return 0;
367 }
368
369 static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
370 {
371 if (name[0] == '-')
372 return -1;
373
374 strbuf_reset(sb);
375 strbuf_addf(sb, "refs/tags/%s", name);
376
377 return check_refname_format(sb->buf, 0);
378 }
379
380 int cmd_tag(int argc, const char **argv, const char *prefix)
381 {
382 struct strbuf buf = STRBUF_INIT;
383 struct strbuf ref = STRBUF_INIT;
384 struct strbuf reflog_msg = STRBUF_INIT;
385 struct object_id object, prev;
386 const char *object_ref, *tag;
387 struct create_tag_options opt;
388 char *cleanup_arg = NULL;
389 int create_reflog = 0;
390 int annotate = 0, force = 0;
391 int cmdmode = 0, create_tag_object = 0;
392 const char *msgfile = NULL, *keyid = NULL;
393 struct msg_arg msg = { 0, STRBUF_INIT };
394 struct ref_transaction *transaction;
395 struct strbuf err = STRBUF_INIT;
396 struct ref_filter filter;
397 static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
398 struct ref_format format = REF_FORMAT_INIT;
399 int icase = 0;
400 int edit_flag = 0;
401 struct option options[] = {
402 OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
403 { OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"),
404 N_("print <n> lines of each tag message"),
405 PARSE_OPT_OPTARG, NULL, 1 },
406 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'),
407 OPT_CMDMODE('v', "verify", &cmdmode, N_("verify tags"), 'v'),
408
409 OPT_GROUP(N_("Tag creation options")),
410 OPT_BOOL('a', "annotate", &annotate,
411 N_("annotated tag, needs a message")),
412 { OPTION_CALLBACK, 'm', "message", &msg, N_("message"),
413 N_("tag message"), PARSE_OPT_NONEG, parse_msg_arg },
414 OPT_FILENAME('F', "file", &msgfile, N_("read message from file")),
415 OPT_BOOL('e', "edit", &edit_flag, N_("force edit of tag message")),
416 OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")),
417 OPT_CLEANUP(&cleanup_arg),
418 OPT_STRING('u', "local-user", &keyid, N_("key-id"),
419 N_("use another key to sign the tag")),
420 OPT__FORCE(&force, N_("replace the tag if exists"), 0),
421 OPT_BOOL(0, "create-reflog", &create_reflog, N_("create a reflog")),
422
423 OPT_GROUP(N_("Tag listing options")),
424 OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
425 OPT_CONTAINS(&filter.with_commit, N_("print only tags that contain the commit")),
426 OPT_NO_CONTAINS(&filter.no_commit, N_("print only tags that don't contain the commit")),
427 OPT_WITH(&filter.with_commit, N_("print only tags that contain the commit")),
428 OPT_WITHOUT(&filter.no_commit, N_("print only tags that don't contain the commit")),
429 OPT_MERGED(&filter, N_("print only tags that are merged")),
430 OPT_NO_MERGED(&filter, N_("print only tags that are not merged")),
431 OPT_REF_SORT(sorting_tail),
432 {
433 OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
434 N_("print only tags of the object"), PARSE_OPT_LASTARG_DEFAULT,
435 parse_opt_object_name, (intptr_t) "HEAD"
436 },
437 OPT_STRING( 0 , "format", &format.format, N_("format"),
438 N_("format to use for the output")),
439 OPT__COLOR(&format.use_color, N_("respect format colors")),
440 OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
441 OPT_END()
442 };
443
444 setup_ref_filter_porcelain_msg();
445
446 git_config(git_tag_config, sorting_tail);
447
448 memset(&opt, 0, sizeof(opt));
449 memset(&filter, 0, sizeof(filter));
450 filter.lines = -1;
451 opt.sign = -1;
452
453 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
454
455 if (!cmdmode) {
456 if (argc == 0)
457 cmdmode = 'l';
458 else if (filter.with_commit || filter.no_commit ||
459 filter.points_at.nr || filter.merge_commit ||
460 filter.lines != -1)
461 cmdmode = 'l';
462 }
463
464 if (cmdmode == 'l')
465 setup_auto_pager("tag", 1);
466
467 if (opt.sign == -1)
468 opt.sign = cmdmode ? 0 : config_sign_tag > 0;
469
470 if (keyid) {
471 opt.sign = 1;
472 set_signing_key(keyid);
473 }
474 create_tag_object = (opt.sign || annotate || msg.given || msgfile);
475
476 if ((create_tag_object || force) && (cmdmode != 0))
477 usage_with_options(git_tag_usage, options);
478
479 finalize_colopts(&colopts, -1);
480 if (cmdmode == 'l' && filter.lines != -1) {
481 if (explicitly_enable_column(colopts))
482 die(_("--column and -n are incompatible"));
483 colopts = 0;
484 }
485 if (!sorting)
486 sorting = ref_default_sorting();
487 sorting->ignore_case = icase;
488 filter.ignore_case = icase;
489 if (cmdmode == 'l') {
490 int ret;
491 if (column_active(colopts)) {
492 struct column_options copts;
493 memset(&copts, 0, sizeof(copts));
494 copts.padding = 2;
495 run_column_filter(colopts, &copts);
496 }
497 filter.name_patterns = argv;
498 ret = list_tags(&filter, sorting, &format);
499 if (column_active(colopts))
500 stop_column_filter();
501 return ret;
502 }
503 if (filter.lines != -1)
504 die(_("-n option is only allowed in list mode"));
505 if (filter.with_commit)
506 die(_("--contains option is only allowed in list mode"));
507 if (filter.no_commit)
508 die(_("--no-contains option is only allowed in list mode"));
509 if (filter.points_at.nr)
510 die(_("--points-at option is only allowed in list mode"));
511 if (filter.merge_commit)
512 die(_("--merged and --no-merged options are only allowed in list mode"));
513 if (cmdmode == 'd')
514 return for_each_tag_name(argv, delete_tag, NULL);
515 if (cmdmode == 'v') {
516 if (format.format && verify_ref_format(&format))
517 usage_with_options(git_tag_usage, options);
518 return for_each_tag_name(argv, verify_tag, &format);
519 }
520
521 if (msg.given || msgfile) {
522 if (msg.given && msgfile)
523 die(_("only one -F or -m option is allowed."));
524 if (msg.given)
525 strbuf_addbuf(&buf, &(msg.buf));
526 else {
527 if (!strcmp(msgfile, "-")) {
528 if (strbuf_read(&buf, 0, 1024) < 0)
529 die_errno(_("cannot read '%s'"), msgfile);
530 } else {
531 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
532 die_errno(_("could not open or read '%s'"),
533 msgfile);
534 }
535 }
536 }
537
538 tag = argv[0];
539
540 object_ref = argc == 2 ? argv[1] : "HEAD";
541 if (argc > 2)
542 die(_("too many params"));
543
544 if (get_oid(object_ref, &object))
545 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
546
547 if (strbuf_check_tag_ref(&ref, tag))
548 die(_("'%s' is not a valid tag name."), tag);
549
550 if (read_ref(ref.buf, &prev))
551 oidclr(&prev);
552 else if (!force)
553 die(_("tag '%s' already exists"), tag);
554
555 opt.message_given = msg.given || msgfile;
556 opt.use_editor = edit_flag;
557
558 if (!cleanup_arg || !strcmp(cleanup_arg, "strip"))
559 opt.cleanup_mode = CLEANUP_ALL;
560 else if (!strcmp(cleanup_arg, "verbatim"))
561 opt.cleanup_mode = CLEANUP_NONE;
562 else if (!strcmp(cleanup_arg, "whitespace"))
563 opt.cleanup_mode = CLEANUP_SPACE;
564 else
565 die(_("Invalid cleanup mode %s"), cleanup_arg);
566
567 create_reflog_msg(&object, &reflog_msg);
568
569 if (create_tag_object) {
570 if (force_sign_annotate && !annotate)
571 opt.sign = 1;
572 create_tag(&object, object_ref, tag, &buf, &opt, &prev, &object);
573 }
574
575 transaction = ref_transaction_begin(&err);
576 if (!transaction ||
577 ref_transaction_update(transaction, ref.buf, &object, &prev,
578 create_reflog ? REF_FORCE_CREATE_REFLOG : 0,
579 reflog_msg.buf, &err) ||
580 ref_transaction_commit(transaction, &err))
581 die("%s", err.buf);
582 ref_transaction_free(transaction);
583 if (force && !is_null_oid(&prev) && !oideq(&prev, &object))
584 printf(_("Updated tag '%s' (was %s)\n"), tag,
585 find_unique_abbrev(&prev, DEFAULT_ABBREV));
586
587 UNLEAK(buf);
588 UNLEAK(ref);
589 UNLEAK(reflog_msg);
590 UNLEAK(msg);
591 UNLEAK(err);
592 return 0;
593 }