Merge pull request #188 from kasal/revert-install-wincred
[git/mingw/4msysgit.git] / builtin / tag.c
blob40758d40a5e7e6f483912369d4cc371a3fb1c481
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 "builtin.h"
11 #include "refs.h"
12 #include "tag.h"
13 #include "run-command.h"
14 #include "parse-options.h"
15 #include "diff.h"
16 #include "revision.h"
17 #include "gpg-interface.h"
18 #include "sha1-array.h"
19 #include "column.h"
21 static const char * const git_tag_usage[] = {
22 N_("git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]"),
23 N_("git tag -d <tagname>..."),
24 N_("git tag -l [-n[<num>]] [--contains <commit>] [--points-at <object>] "
25 "\n\t\t[<pattern>...]"),
26 N_("git tag -v <tagname>..."),
27 NULL
30 struct tag_filter {
31 const char **patterns;
32 int lines;
33 struct commit_list *with_commit;
36 static struct sha1_array points_at;
37 static unsigned int colopts;
39 static int match_pattern(const char **patterns, const char *ref)
41 /* no pattern means match everything */
42 if (!*patterns)
43 return 1;
44 for (; *patterns; patterns++)
45 if (!fnmatch(*patterns, ref, 0))
46 return 1;
47 return 0;
50 static const unsigned char *match_points_at(const char *refname,
51 const unsigned char *sha1)
53 const unsigned char *tagged_sha1 = NULL;
54 struct object *obj;
56 if (sha1_array_lookup(&points_at, sha1) >= 0)
57 return sha1;
58 obj = parse_object(sha1);
59 if (!obj)
60 die(_("malformed object at '%s'"), refname);
61 if (obj->type == OBJ_TAG)
62 tagged_sha1 = ((struct tag *)obj)->tagged->sha1;
63 if (tagged_sha1 && sha1_array_lookup(&points_at, tagged_sha1) >= 0)
64 return tagged_sha1;
65 return NULL;
68 static int in_commit_list(const struct commit_list *want, struct commit *c)
70 for (; want; want = want->next)
71 if (!hashcmp(want->item->object.sha1, c->object.sha1))
72 return 1;
73 return 0;
76 enum contains_result {
77 CONTAINS_UNKNOWN = -1,
78 CONTAINS_NO = 0,
79 CONTAINS_YES = 1,
83 * Test whether the candidate or one of its parents is contained in the list.
84 * Do not recurse to find out, though, but return -1 if inconclusive.
86 static enum contains_result contains_test(struct commit *candidate,
87 const struct commit_list *want)
89 /* was it previously marked as containing a want commit? */
90 if (candidate->object.flags & TMP_MARK)
91 return 1;
92 /* or marked as not possibly containing a want commit? */
93 if (candidate->object.flags & UNINTERESTING)
94 return 0;
95 /* or are we it? */
96 if (in_commit_list(want, candidate)) {
97 candidate->object.flags |= TMP_MARK;
98 return 1;
101 if (parse_commit(candidate) < 0)
102 return 0;
104 return -1;
108 * Mimicking the real stack, this stack lives on the heap, avoiding stack
109 * overflows.
111 * At each recursion step, the stack items points to the commits whose
112 * ancestors are to be inspected.
114 struct stack {
115 int nr, alloc;
116 struct stack_entry {
117 struct commit *commit;
118 struct commit_list *parents;
119 } *stack;
122 static void push_to_stack(struct commit *candidate, struct stack *stack)
124 int index = stack->nr++;
125 ALLOC_GROW(stack->stack, stack->nr, stack->alloc);
126 stack->stack[index].commit = candidate;
127 stack->stack[index].parents = candidate->parents;
130 static enum contains_result contains(struct commit *candidate,
131 const struct commit_list *want)
133 struct stack stack = { 0, 0, NULL };
134 int result = contains_test(candidate, want);
136 if (result != CONTAINS_UNKNOWN)
137 return result;
139 push_to_stack(candidate, &stack);
140 while (stack.nr) {
141 struct stack_entry *entry = &stack.stack[stack.nr - 1];
142 struct commit *commit = entry->commit;
143 struct commit_list *parents = entry->parents;
145 if (!parents) {
146 commit->object.flags |= UNINTERESTING;
147 stack.nr--;
150 * If we just popped the stack, parents->item has been marked,
151 * therefore contains_test will return a meaningful 0 or 1.
153 else switch (contains_test(parents->item, want)) {
154 case CONTAINS_YES:
155 commit->object.flags |= TMP_MARK;
156 stack.nr--;
157 break;
158 case CONTAINS_NO:
159 entry->parents = parents->next;
160 break;
161 case CONTAINS_UNKNOWN:
162 push_to_stack(parents->item, &stack);
163 break;
166 free(stack.stack);
167 return contains_test(candidate, want);
170 static void show_tag_lines(const unsigned char *sha1, int lines)
172 int i;
173 unsigned long size;
174 enum object_type type;
175 char *buf, *sp, *eol;
176 size_t len;
178 buf = read_sha1_file(sha1, &type, &size);
179 if (!buf)
180 die_errno("unable to read object %s", sha1_to_hex(sha1));
181 if (type != OBJ_COMMIT && type != OBJ_TAG)
182 goto free_return;
183 if (!size)
184 die("an empty %s object %s?",
185 typename(type), sha1_to_hex(sha1));
187 /* skip header */
188 sp = strstr(buf, "\n\n");
189 if (!sp)
190 goto free_return;
192 /* only take up to "lines" lines, and strip the signature from a tag */
193 if (type == OBJ_TAG)
194 size = parse_signature(buf, size);
195 for (i = 0, sp += 2; i < lines && sp < buf + size; i++) {
196 if (i)
197 printf("\n ");
198 eol = memchr(sp, '\n', size - (sp - buf));
199 len = eol ? eol - sp : size - (sp - buf);
200 fwrite(sp, len, 1, stdout);
201 if (!eol)
202 break;
203 sp = eol + 1;
205 free_return:
206 free(buf);
209 static int show_reference(const char *refname, const unsigned char *sha1,
210 int flag, void *cb_data)
212 struct tag_filter *filter = cb_data;
214 if (match_pattern(filter->patterns, refname)) {
215 if (filter->with_commit) {
216 struct commit *commit;
218 commit = lookup_commit_reference_gently(sha1, 1);
219 if (!commit)
220 return 0;
221 if (!contains(commit, filter->with_commit))
222 return 0;
225 if (points_at.nr && !match_points_at(refname, sha1))
226 return 0;
228 if (!filter->lines) {
229 printf("%s\n", refname);
230 return 0;
232 printf("%-15s ", refname);
233 show_tag_lines(sha1, filter->lines);
234 putchar('\n');
237 return 0;
240 static int list_tags(const char **patterns, int lines,
241 struct commit_list *with_commit)
243 struct tag_filter filter;
245 filter.patterns = patterns;
246 filter.lines = lines;
247 filter.with_commit = with_commit;
249 for_each_tag_ref(show_reference, (void *) &filter);
251 return 0;
254 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
255 const unsigned char *sha1);
257 static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
259 const char **p;
260 char ref[PATH_MAX];
261 int had_error = 0;
262 unsigned char sha1[20];
264 for (p = argv; *p; p++) {
265 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
266 >= sizeof(ref)) {
267 error(_("tag name too long: %.*s..."), 50, *p);
268 had_error = 1;
269 continue;
271 if (read_ref(ref, sha1)) {
272 error(_("tag '%s' not found."), *p);
273 had_error = 1;
274 continue;
276 if (fn(*p, ref, sha1))
277 had_error = 1;
279 return had_error;
282 static int delete_tag(const char *name, const char *ref,
283 const unsigned char *sha1)
285 if (delete_ref(ref, sha1, 0))
286 return 1;
287 printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(sha1, DEFAULT_ABBREV));
288 return 0;
291 static int verify_tag(const char *name, const char *ref,
292 const unsigned char *sha1)
294 const char *argv_verify_tag[] = {"verify-tag",
295 "-v", "SHA1_HEX", NULL};
296 argv_verify_tag[2] = sha1_to_hex(sha1);
298 if (run_command_v_opt(argv_verify_tag, RUN_GIT_CMD))
299 return error(_("could not verify the tag '%s'"), name);
300 return 0;
303 static int do_sign(struct strbuf *buffer)
305 return sign_buffer(buffer, buffer, get_signing_key());
308 static const char tag_template[] =
309 N_("\nWrite a tag message\n"
310 "Lines starting with '%c' will be ignored.\n");
312 static const char tag_template_nocleanup[] =
313 N_("\nWrite a tag message\n"
314 "Lines starting with '%c' will be kept; you may remove them"
315 " yourself if you want to.\n");
317 static int git_tag_config(const char *var, const char *value, void *cb)
319 int status = git_gpg_config(var, value, cb);
320 if (status)
321 return status;
322 if (starts_with(var, "column."))
323 return git_column_config(var, value, "tag", &colopts);
324 return git_default_config(var, value, cb);
327 static void write_tag_body(int fd, const unsigned char *sha1)
329 unsigned long size;
330 enum object_type type;
331 char *buf, *sp;
333 buf = read_sha1_file(sha1, &type, &size);
334 if (!buf)
335 return;
336 /* skip header */
337 sp = strstr(buf, "\n\n");
339 if (!sp || !size || type != OBJ_TAG) {
340 free(buf);
341 return;
343 sp += 2; /* skip the 2 LFs */
344 write_or_die(fd, sp, parse_signature(sp, buf + size - sp));
346 free(buf);
349 static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
351 if (sign && do_sign(buf) < 0)
352 return error(_("unable to sign the tag"));
353 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
354 return error(_("unable to write tag file"));
355 return 0;
358 struct create_tag_options {
359 unsigned int message_given:1;
360 unsigned int sign;
361 enum {
362 CLEANUP_NONE,
363 CLEANUP_SPACE,
364 CLEANUP_ALL
365 } cleanup_mode;
368 static void create_tag(const unsigned char *object, const char *tag,
369 struct strbuf *buf, struct create_tag_options *opt,
370 unsigned char *prev, unsigned char *result)
372 enum object_type type;
373 char header_buf[1024];
374 int header_len;
375 char *path = NULL;
377 type = sha1_object_info(object, NULL);
378 if (type <= OBJ_NONE)
379 die(_("bad object type."));
381 header_len = snprintf(header_buf, sizeof(header_buf),
382 "object %s\n"
383 "type %s\n"
384 "tag %s\n"
385 "tagger %s\n\n",
386 sha1_to_hex(object),
387 typename(type),
388 tag,
389 git_committer_info(IDENT_STRICT));
391 if (header_len > sizeof(header_buf) - 1)
392 die(_("tag header too big."));
394 if (!opt->message_given) {
395 int fd;
397 /* write the template message before editing: */
398 path = git_pathdup("TAG_EDITMSG");
399 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
400 if (fd < 0)
401 die_errno(_("could not create file '%s'"), path);
403 if (!is_null_sha1(prev)) {
404 write_tag_body(fd, prev);
405 } else {
406 struct strbuf buf = STRBUF_INIT;
407 strbuf_addch(&buf, '\n');
408 if (opt->cleanup_mode == CLEANUP_ALL)
409 strbuf_commented_addf(&buf, _(tag_template), comment_line_char);
410 else
411 strbuf_commented_addf(&buf, _(tag_template_nocleanup), comment_line_char);
412 write_or_die(fd, buf.buf, buf.len);
413 strbuf_release(&buf);
415 close(fd);
417 if (launch_editor(path, buf, NULL)) {
418 fprintf(stderr,
419 _("Please supply the message using either -m or -F option.\n"));
420 exit(1);
424 if (opt->cleanup_mode != CLEANUP_NONE)
425 stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
427 if (!opt->message_given && !buf->len)
428 die(_("no tag message?"));
430 strbuf_insert(buf, 0, header_buf, header_len);
432 if (build_tag_object(buf, opt->sign, result) < 0) {
433 if (path)
434 fprintf(stderr, _("The tag message has been left in %s\n"),
435 path);
436 exit(128);
438 if (path) {
439 unlink_or_warn(path);
440 free(path);
444 struct msg_arg {
445 int given;
446 struct strbuf buf;
449 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
451 struct msg_arg *msg = opt->value;
453 if (!arg)
454 return -1;
455 if (msg->buf.len)
456 strbuf_addstr(&(msg->buf), "\n\n");
457 strbuf_addstr(&(msg->buf), arg);
458 msg->given = 1;
459 return 0;
462 static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
464 if (name[0] == '-')
465 return -1;
467 strbuf_reset(sb);
468 strbuf_addf(sb, "refs/tags/%s", name);
470 return check_refname_format(sb->buf, 0);
473 static int parse_opt_points_at(const struct option *opt __attribute__((unused)),
474 const char *arg, int unset)
476 unsigned char sha1[20];
478 if (unset) {
479 sha1_array_clear(&points_at);
480 return 0;
482 if (!arg)
483 return error(_("switch 'points-at' requires an object"));
484 if (get_sha1(arg, sha1))
485 return error(_("malformed object name '%s'"), arg);
486 sha1_array_append(&points_at, sha1);
487 return 0;
490 int cmd_tag(int argc, const char **argv, const char *prefix)
492 struct strbuf buf = STRBUF_INIT;
493 struct strbuf ref = STRBUF_INIT;
494 unsigned char object[20], prev[20];
495 const char *object_ref, *tag;
496 struct ref_lock *lock;
497 struct create_tag_options opt;
498 char *cleanup_arg = NULL;
499 int annotate = 0, force = 0, lines = -1;
500 int cmdmode = 0;
501 const char *msgfile = NULL, *keyid = NULL;
502 struct msg_arg msg = { 0, STRBUF_INIT };
503 struct commit_list *with_commit = NULL;
504 struct option options[] = {
505 OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
506 { OPTION_INTEGER, 'n', NULL, &lines, N_("n"),
507 N_("print <n> lines of each tag message"),
508 PARSE_OPT_OPTARG, NULL, 1 },
509 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'),
510 OPT_CMDMODE('v', "verify", &cmdmode, N_("verify tags"), 'v'),
512 OPT_GROUP(N_("Tag creation options")),
513 OPT_BOOL('a', "annotate", &annotate,
514 N_("annotated tag, needs a message")),
515 OPT_CALLBACK('m', "message", &msg, N_("message"),
516 N_("tag message"), parse_msg_arg),
517 OPT_FILENAME('F', "file", &msgfile, N_("read message from file")),
518 OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")),
519 OPT_STRING(0, "cleanup", &cleanup_arg, N_("mode"),
520 N_("how to strip spaces and #comments from message")),
521 OPT_STRING('u', "local-user", &keyid, N_("key id"),
522 N_("use another key to sign the tag")),
523 OPT__FORCE(&force, N_("replace the tag if exists")),
524 OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
526 OPT_GROUP(N_("Tag listing options")),
528 OPTION_CALLBACK, 0, "contains", &with_commit, N_("commit"),
529 N_("print only tags that contain the commit"),
530 PARSE_OPT_LASTARG_DEFAULT,
531 parse_opt_with_commit, (intptr_t)"HEAD",
534 OPTION_CALLBACK, 0, "points-at", NULL, N_("object"),
535 N_("print only tags of the object"), 0, parse_opt_points_at
537 OPT_END()
540 git_config(git_tag_config, NULL);
542 memset(&opt, 0, sizeof(opt));
544 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
546 if (keyid) {
547 opt.sign = 1;
548 set_signing_key(keyid);
550 if (opt.sign)
551 annotate = 1;
552 if (argc == 0 && !cmdmode)
553 cmdmode = 'l';
555 if ((annotate || msg.given || msgfile || force) && (cmdmode != 0))
556 usage_with_options(git_tag_usage, options);
558 finalize_colopts(&colopts, -1);
559 if (cmdmode == 'l' && lines != -1) {
560 if (explicitly_enable_column(colopts))
561 die(_("--column and -n are incompatible"));
562 colopts = 0;
564 if (cmdmode == 'l') {
565 int ret;
566 if (column_active(colopts)) {
567 struct column_options copts;
568 memset(&copts, 0, sizeof(copts));
569 copts.padding = 2;
570 run_column_filter(colopts, &copts);
572 ret = list_tags(argv, lines == -1 ? 0 : lines, with_commit);
573 if (column_active(colopts))
574 stop_column_filter();
575 return ret;
577 if (lines != -1)
578 die(_("-n option is only allowed with -l."));
579 if (with_commit)
580 die(_("--contains option is only allowed with -l."));
581 if (points_at.nr)
582 die(_("--points-at option is only allowed with -l."));
583 if (cmdmode == 'd')
584 return for_each_tag_name(argv, delete_tag);
585 if (cmdmode == 'v')
586 return for_each_tag_name(argv, verify_tag);
588 if (msg.given || msgfile) {
589 if (msg.given && msgfile)
590 die(_("only one -F or -m option is allowed."));
591 annotate = 1;
592 if (msg.given)
593 strbuf_addbuf(&buf, &(msg.buf));
594 else {
595 if (!strcmp(msgfile, "-")) {
596 if (strbuf_read(&buf, 0, 1024) < 0)
597 die_errno(_("cannot read '%s'"), msgfile);
598 } else {
599 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
600 die_errno(_("could not open or read '%s'"),
601 msgfile);
606 tag = argv[0];
608 object_ref = argc == 2 ? argv[1] : "HEAD";
609 if (argc > 2)
610 die(_("too many params"));
612 if (get_sha1(object_ref, object))
613 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
615 if (strbuf_check_tag_ref(&ref, tag))
616 die(_("'%s' is not a valid tag name."), tag);
618 if (read_ref(ref.buf, prev))
619 hashclr(prev);
620 else if (!force)
621 die(_("tag '%s' already exists"), tag);
623 opt.message_given = msg.given || msgfile;
625 if (!cleanup_arg || !strcmp(cleanup_arg, "strip"))
626 opt.cleanup_mode = CLEANUP_ALL;
627 else if (!strcmp(cleanup_arg, "verbatim"))
628 opt.cleanup_mode = CLEANUP_NONE;
629 else if (!strcmp(cleanup_arg, "whitespace"))
630 opt.cleanup_mode = CLEANUP_SPACE;
631 else
632 die(_("Invalid cleanup mode %s"), cleanup_arg);
634 if (annotate)
635 create_tag(object, tag, &buf, &opt, prev, object);
637 lock = lock_any_ref_for_update(ref.buf, prev, 0, NULL);
638 if (!lock)
639 die(_("%s: cannot lock the ref"), ref.buf);
640 if (write_ref_sha1(lock, object, NULL) < 0)
641 die(_("%s: cannot update the ref"), ref.buf);
642 if (force && !is_null_sha1(prev) && hashcmp(prev, object))
643 printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
645 strbuf_release(&buf);
646 strbuf_release(&ref);
647 return 0;