attr: "binary" attribute should choose built-in "binary" merge driver
[git.git] / builtin / tag.c
blob03df16ac6e0e492483bf3695e0f6b2aa0a95978b
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"
19 static const char * const git_tag_usage[] = {
20 "git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]",
21 "git tag -d <tagname>...",
22 "git tag -l [-n[<num>]] [<pattern>...]",
23 "git tag -v <tagname>...",
24 NULL
27 struct tag_filter {
28 const char **patterns;
29 int lines;
30 struct commit_list *with_commit;
33 static int match_pattern(const char **patterns, const char *ref)
35 /* no pattern means match everything */
36 if (!*patterns)
37 return 1;
38 for (; *patterns; patterns++)
39 if (!fnmatch(*patterns, ref, 0))
40 return 1;
41 return 0;
44 static int in_commit_list(const struct commit_list *want, struct commit *c)
46 for (; want; want = want->next)
47 if (!hashcmp(want->item->object.sha1, c->object.sha1))
48 return 1;
49 return 0;
52 static int contains_recurse(struct commit *candidate,
53 const struct commit_list *want)
55 struct commit_list *p;
57 /* was it previously marked as containing a want commit? */
58 if (candidate->object.flags & TMP_MARK)
59 return 1;
60 /* or marked as not possibly containing a want commit? */
61 if (candidate->object.flags & UNINTERESTING)
62 return 0;
63 /* or are we it? */
64 if (in_commit_list(want, candidate))
65 return 1;
67 if (parse_commit(candidate) < 0)
68 return 0;
70 /* Otherwise recurse and mark ourselves for future traversals. */
71 for (p = candidate->parents; p; p = p->next) {
72 if (contains_recurse(p->item, want)) {
73 candidate->object.flags |= TMP_MARK;
74 return 1;
77 candidate->object.flags |= UNINTERESTING;
78 return 0;
81 static int contains(struct commit *candidate, const struct commit_list *want)
83 return contains_recurse(candidate, want);
86 static void show_tag_lines(const unsigned char *sha1, int lines)
88 int i;
89 unsigned long size;
90 enum object_type type;
91 char *buf, *sp, *eol;
92 size_t len;
94 buf = read_sha1_file(sha1, &type, &size);
95 if (!buf)
96 die_errno("unable to read object %s", sha1_to_hex(sha1));
97 if (type != OBJ_COMMIT && type != OBJ_TAG)
98 goto free_return;
99 if (!size)
100 die("an empty %s object %s?",
101 typename(type), sha1_to_hex(sha1));
103 /* skip header */
104 sp = strstr(buf, "\n\n");
105 if (!sp)
106 goto free_return;
108 /* only take up to "lines" lines, and strip the signature from a tag */
109 if (type == OBJ_TAG)
110 size = parse_signature(buf, size);
111 for (i = 0, sp += 2; i < lines && sp < buf + size; i++) {
112 if (i)
113 printf("\n ");
114 eol = memchr(sp, '\n', size - (sp - buf));
115 len = eol ? eol - sp : size - (sp - buf);
116 fwrite(sp, len, 1, stdout);
117 if (!eol)
118 break;
119 sp = eol + 1;
121 free_return:
122 free(buf);
125 static int show_reference(const char *refname, const unsigned char *sha1,
126 int flag, void *cb_data)
128 struct tag_filter *filter = cb_data;
130 if (match_pattern(filter->patterns, refname)) {
131 if (filter->with_commit) {
132 struct commit *commit;
134 commit = lookup_commit_reference_gently(sha1, 1);
135 if (!commit)
136 return 0;
137 if (!contains(commit, filter->with_commit))
138 return 0;
141 if (!filter->lines) {
142 printf("%s\n", refname);
143 return 0;
145 printf("%-15s ", refname);
146 show_tag_lines(sha1, filter->lines);
147 putchar('\n');
150 return 0;
153 static int list_tags(const char **patterns, int lines,
154 struct commit_list *with_commit)
156 struct tag_filter filter;
158 filter.patterns = patterns;
159 filter.lines = lines;
160 filter.with_commit = with_commit;
162 for_each_tag_ref(show_reference, (void *) &filter);
164 return 0;
167 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
168 const unsigned char *sha1);
170 static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
172 const char **p;
173 char ref[PATH_MAX];
174 int had_error = 0;
175 unsigned char sha1[20];
177 for (p = argv; *p; p++) {
178 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
179 >= sizeof(ref)) {
180 error(_("tag name too long: %.*s..."), 50, *p);
181 had_error = 1;
182 continue;
184 if (read_ref(ref, sha1)) {
185 error(_("tag '%s' not found."), *p);
186 had_error = 1;
187 continue;
189 if (fn(*p, ref, sha1))
190 had_error = 1;
192 return had_error;
195 static int delete_tag(const char *name, const char *ref,
196 const unsigned char *sha1)
198 if (delete_ref(ref, sha1, 0))
199 return 1;
200 printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(sha1, DEFAULT_ABBREV));
201 return 0;
204 static int verify_tag(const char *name, const char *ref,
205 const unsigned char *sha1)
207 const char *argv_verify_tag[] = {"verify-tag",
208 "-v", "SHA1_HEX", NULL};
209 argv_verify_tag[2] = sha1_to_hex(sha1);
211 if (run_command_v_opt(argv_verify_tag, RUN_GIT_CMD))
212 return error(_("could not verify the tag '%s'"), name);
213 return 0;
216 static int do_sign(struct strbuf *buffer)
218 return sign_buffer(buffer, buffer, get_signing_key());
221 static const char tag_template[] =
222 N_("\n"
223 "#\n"
224 "# Write a tag message\n"
225 "# Lines starting with '#' will be ignored.\n"
226 "#\n");
228 static const char tag_template_nocleanup[] =
229 N_("\n"
230 "#\n"
231 "# Write a tag message\n"
232 "# Lines starting with '#' will be kept; you may remove them"
233 " yourself if you want to.\n"
234 "#\n");
236 static int git_tag_config(const char *var, const char *value, void *cb)
238 int status = git_gpg_config(var, value, cb);
239 if (status)
240 return status;
241 return git_default_config(var, value, cb);
244 static void write_tag_body(int fd, const unsigned char *sha1)
246 unsigned long size;
247 enum object_type type;
248 char *buf, *sp;
250 buf = read_sha1_file(sha1, &type, &size);
251 if (!buf)
252 return;
253 /* skip header */
254 sp = strstr(buf, "\n\n");
256 if (!sp || !size || type != OBJ_TAG) {
257 free(buf);
258 return;
260 sp += 2; /* skip the 2 LFs */
261 write_or_die(fd, sp, parse_signature(sp, buf + size - sp));
263 free(buf);
266 static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
268 if (sign && do_sign(buf) < 0)
269 return error(_("unable to sign the tag"));
270 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
271 return error(_("unable to write tag file"));
272 return 0;
275 struct create_tag_options {
276 unsigned int message_given:1;
277 unsigned int sign;
278 enum {
279 CLEANUP_NONE,
280 CLEANUP_SPACE,
281 CLEANUP_ALL
282 } cleanup_mode;
285 static void create_tag(const unsigned char *object, const char *tag,
286 struct strbuf *buf, struct create_tag_options *opt,
287 unsigned char *prev, unsigned char *result)
289 enum object_type type;
290 char header_buf[1024];
291 int header_len;
292 char *path = NULL;
294 type = sha1_object_info(object, NULL);
295 if (type <= OBJ_NONE)
296 die(_("bad object type."));
298 header_len = snprintf(header_buf, sizeof(header_buf),
299 "object %s\n"
300 "type %s\n"
301 "tag %s\n"
302 "tagger %s\n\n",
303 sha1_to_hex(object),
304 typename(type),
305 tag,
306 git_committer_info(IDENT_ERROR_ON_NO_NAME));
308 if (header_len > sizeof(header_buf) - 1)
309 die(_("tag header too big."));
311 if (!opt->message_given) {
312 int fd;
314 /* write the template message before editing: */
315 path = git_pathdup("TAG_EDITMSG");
316 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
317 if (fd < 0)
318 die_errno(_("could not create file '%s'"), path);
320 if (!is_null_sha1(prev))
321 write_tag_body(fd, prev);
322 else if (opt->cleanup_mode == CLEANUP_ALL)
323 write_or_die(fd, _(tag_template),
324 strlen(_(tag_template)));
325 else
326 write_or_die(fd, _(tag_template_nocleanup),
327 strlen(_(tag_template_nocleanup)));
328 close(fd);
330 if (launch_editor(path, buf, NULL)) {
331 fprintf(stderr,
332 _("Please supply the message using either -m or -F option.\n"));
333 exit(1);
337 if (opt->cleanup_mode != CLEANUP_NONE)
338 stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
340 if (!opt->message_given && !buf->len)
341 die(_("no tag message?"));
343 strbuf_insert(buf, 0, header_buf, header_len);
345 if (build_tag_object(buf, opt->sign, result) < 0) {
346 if (path)
347 fprintf(stderr, _("The tag message has been left in %s\n"),
348 path);
349 exit(128);
351 if (path) {
352 unlink_or_warn(path);
353 free(path);
357 struct msg_arg {
358 int given;
359 struct strbuf buf;
362 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
364 struct msg_arg *msg = opt->value;
366 if (!arg)
367 return -1;
368 if (msg->buf.len)
369 strbuf_addstr(&(msg->buf), "\n\n");
370 strbuf_addstr(&(msg->buf), arg);
371 msg->given = 1;
372 return 0;
375 static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
377 if (name[0] == '-')
378 return -1;
380 strbuf_reset(sb);
381 strbuf_addf(sb, "refs/tags/%s", name);
383 return check_refname_format(sb->buf, 0);
386 int cmd_tag(int argc, const char **argv, const char *prefix)
388 struct strbuf buf = STRBUF_INIT;
389 struct strbuf ref = STRBUF_INIT;
390 unsigned char object[20], prev[20];
391 const char *object_ref, *tag;
392 struct ref_lock *lock;
393 struct create_tag_options opt;
394 char *cleanup_arg = NULL;
395 int annotate = 0, force = 0, lines = -1, list = 0,
396 delete = 0, verify = 0;
397 const char *msgfile = NULL, *keyid = NULL;
398 struct msg_arg msg = { 0, STRBUF_INIT };
399 struct commit_list *with_commit = NULL;
400 struct option options[] = {
401 OPT_BOOLEAN('l', "list", &list, "list tag names"),
402 { OPTION_INTEGER, 'n', NULL, &lines, "n",
403 "print <n> lines of each tag message",
404 PARSE_OPT_OPTARG, NULL, 1 },
405 OPT_BOOLEAN('d', "delete", &delete, "delete tags"),
406 OPT_BOOLEAN('v', "verify", &verify, "verify tags"),
408 OPT_GROUP("Tag creation options"),
409 OPT_BOOLEAN('a', "annotate", &annotate,
410 "annotated tag, needs a message"),
411 OPT_CALLBACK('m', "message", &msg, "message",
412 "tag message", parse_msg_arg),
413 OPT_FILENAME('F', "file", &msgfile, "read message from file"),
414 OPT_BOOLEAN('s', "sign", &opt.sign, "annotated and GPG-signed tag"),
415 OPT_STRING(0, "cleanup", &cleanup_arg, "mode",
416 "how to strip spaces and #comments from message"),
417 OPT_STRING('u', "local-user", &keyid, "key-id",
418 "use another key to sign the tag"),
419 OPT__FORCE(&force, "replace the tag if exists"),
421 OPT_GROUP("Tag listing options"),
423 OPTION_CALLBACK, 0, "contains", &with_commit, "commit",
424 "print only tags that contain the commit",
425 PARSE_OPT_LASTARG_DEFAULT,
426 parse_opt_with_commit, (intptr_t)"HEAD",
428 OPT_END()
431 git_config(git_tag_config, NULL);
433 memset(&opt, 0, sizeof(opt));
435 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
437 if (keyid) {
438 opt.sign = 1;
439 set_signing_key(keyid);
441 if (opt.sign)
442 annotate = 1;
443 if (argc == 0 && !(delete || verify))
444 list = 1;
446 if ((annotate || msg.given || msgfile || force) &&
447 (list || delete || verify))
448 usage_with_options(git_tag_usage, options);
450 if (list + delete + verify > 1)
451 usage_with_options(git_tag_usage, options);
452 if (list)
453 return list_tags(argv, lines == -1 ? 0 : lines,
454 with_commit);
455 if (lines != -1)
456 die(_("-n option is only allowed with -l."));
457 if (with_commit)
458 die(_("--contains option is only allowed with -l."));
459 if (delete)
460 return for_each_tag_name(argv, delete_tag);
461 if (verify)
462 return for_each_tag_name(argv, verify_tag);
464 if (msg.given || msgfile) {
465 if (msg.given && msgfile)
466 die(_("only one -F or -m option is allowed."));
467 annotate = 1;
468 if (msg.given)
469 strbuf_addbuf(&buf, &(msg.buf));
470 else {
471 if (!strcmp(msgfile, "-")) {
472 if (strbuf_read(&buf, 0, 1024) < 0)
473 die_errno(_("cannot read '%s'"), msgfile);
474 } else {
475 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
476 die_errno(_("could not open or read '%s'"),
477 msgfile);
482 tag = argv[0];
484 object_ref = argc == 2 ? argv[1] : "HEAD";
485 if (argc > 2)
486 die(_("too many params"));
488 if (get_sha1(object_ref, object))
489 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
491 if (strbuf_check_tag_ref(&ref, tag))
492 die(_("'%s' is not a valid tag name."), tag);
494 if (read_ref(ref.buf, prev))
495 hashclr(prev);
496 else if (!force)
497 die(_("tag '%s' already exists"), tag);
499 opt.message_given = msg.given || msgfile;
501 if (!cleanup_arg || !strcmp(cleanup_arg, "strip"))
502 opt.cleanup_mode = CLEANUP_ALL;
503 else if (!strcmp(cleanup_arg, "verbatim"))
504 opt.cleanup_mode = CLEANUP_NONE;
505 else if (!strcmp(cleanup_arg, "whitespace"))
506 opt.cleanup_mode = CLEANUP_SPACE;
507 else
508 die(_("Invalid cleanup mode %s"), cleanup_arg);
510 if (annotate)
511 create_tag(object, tag, &buf, &opt, prev, object);
513 lock = lock_any_ref_for_update(ref.buf, prev, 0);
514 if (!lock)
515 die(_("%s: cannot lock the ref"), ref.buf);
516 if (write_ref_sha1(lock, object, NULL) < 0)
517 die(_("%s: cannot update the ref"), ref.buf);
518 if (force && hashcmp(prev, object))
519 printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
521 strbuf_release(&buf);
522 strbuf_release(&ref);
523 return 0;