tag: do not show non-tag contents with "-n"
[git/mingw.git] / builtin / tag.c
blob6ca53e3310c3c0fd9fadebb83e2442368e53bab5
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"
18 static const char * const git_tag_usage[] = {
19 "git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]",
20 "git tag -d <tagname>...",
21 "git tag -l [-n[<num>]] [<pattern>...]",
22 "git tag -v <tagname>...",
23 NULL
26 static char signingkey[1000];
28 struct tag_filter {
29 const char **patterns;
30 int lines;
31 struct commit_list *with_commit;
34 static int match_pattern(const char **patterns, const char *ref)
36 /* no pattern means match everything */
37 if (!*patterns)
38 return 1;
39 for (; *patterns; patterns++)
40 if (!fnmatch(*patterns, ref, 0))
41 return 1;
42 return 0;
45 static int in_commit_list(const struct commit_list *want, struct commit *c)
47 for (; want; want = want->next)
48 if (!hashcmp(want->item->object.sha1, c->object.sha1))
49 return 1;
50 return 0;
53 static int contains_recurse(struct commit *candidate,
54 const struct commit_list *want)
56 struct commit_list *p;
58 /* was it previously marked as containing a want commit? */
59 if (candidate->object.flags & TMP_MARK)
60 return 1;
61 /* or marked as not possibly containing a want commit? */
62 if (candidate->object.flags & UNINTERESTING)
63 return 0;
64 /* or are we it? */
65 if (in_commit_list(want, candidate))
66 return 1;
68 if (parse_commit(candidate) < 0)
69 return 0;
71 /* Otherwise recurse and mark ourselves for future traversals. */
72 for (p = candidate->parents; p; p = p->next) {
73 if (contains_recurse(p->item, want)) {
74 candidate->object.flags |= TMP_MARK;
75 return 1;
78 candidate->object.flags |= UNINTERESTING;
79 return 0;
82 static int contains(struct commit *candidate, const struct commit_list *want)
84 return contains_recurse(candidate, want);
87 static void show_tag_lines(const unsigned char *sha1, int lines)
89 int i;
90 unsigned long size;
91 enum object_type type;
92 char *buf, *sp, *eol;
93 size_t len;
95 buf = read_sha1_file(sha1, &type, &size);
96 if (!buf)
97 die_errno("unable to read object %s", sha1_to_hex(sha1));
98 if (type != OBJ_COMMIT && type != OBJ_TAG)
99 goto free_return;
100 if (!size)
101 die("an empty %s object %s?",
102 typename(type), sha1_to_hex(sha1));
104 /* skip header */
105 sp = strstr(buf, "\n\n");
106 if (!sp)
107 goto free_return;
109 /* only take up to "lines" lines, and strip the signature from a tag */
110 if (type == OBJ_TAG)
111 size = parse_signature(buf, size);
112 for (i = 0, sp += 2; i < lines && sp < buf + size; i++) {
113 if (i)
114 printf("\n ");
115 eol = memchr(sp, '\n', size - (sp - buf));
116 len = eol ? eol - sp : size - (sp - buf);
117 fwrite(sp, len, 1, stdout);
118 if (!eol)
119 break;
120 sp = eol + 1;
122 free_return:
123 free(buf);
126 static int show_reference(const char *refname, const unsigned char *sha1,
127 int flag, void *cb_data)
129 struct tag_filter *filter = cb_data;
131 if (match_pattern(filter->patterns, refname)) {
132 if (filter->with_commit) {
133 struct commit *commit;
135 commit = lookup_commit_reference_gently(sha1, 1);
136 if (!commit)
137 return 0;
138 if (!contains(commit, filter->with_commit))
139 return 0;
142 if (!filter->lines) {
143 printf("%s\n", refname);
144 return 0;
146 printf("%-15s ", refname);
147 show_tag_lines(sha1, filter->lines);
148 putchar('\n');
151 return 0;
154 static int list_tags(const char **patterns, int lines,
155 struct commit_list *with_commit)
157 struct tag_filter filter;
159 filter.patterns = patterns;
160 filter.lines = lines;
161 filter.with_commit = with_commit;
163 for_each_tag_ref(show_reference, (void *) &filter);
165 return 0;
168 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
169 const unsigned char *sha1);
171 static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
173 const char **p;
174 char ref[PATH_MAX];
175 int had_error = 0;
176 unsigned char sha1[20];
178 for (p = argv; *p; p++) {
179 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
180 >= sizeof(ref)) {
181 error(_("tag name too long: %.*s..."), 50, *p);
182 had_error = 1;
183 continue;
185 if (!resolve_ref(ref, sha1, 1, NULL)) {
186 error(_("tag '%s' not found."), *p);
187 had_error = 1;
188 continue;
190 if (fn(*p, ref, sha1))
191 had_error = 1;
193 return had_error;
196 static int delete_tag(const char *name, const char *ref,
197 const unsigned char *sha1)
199 if (delete_ref(ref, sha1, 0))
200 return 1;
201 printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(sha1, DEFAULT_ABBREV));
202 return 0;
205 static int verify_tag(const char *name, const char *ref,
206 const unsigned char *sha1)
208 const char *argv_verify_tag[] = {"verify-tag",
209 "-v", "SHA1_HEX", NULL};
210 argv_verify_tag[2] = sha1_to_hex(sha1);
212 if (run_command_v_opt(argv_verify_tag, RUN_GIT_CMD))
213 return error(_("could not verify the tag '%s'"), name);
214 return 0;
217 static int do_sign(struct strbuf *buffer)
219 struct child_process gpg;
220 const char *args[4];
221 char *bracket;
222 int len;
223 int i, j;
225 if (!*signingkey) {
226 if (strlcpy(signingkey, git_committer_info(IDENT_ERROR_ON_NO_NAME),
227 sizeof(signingkey)) > sizeof(signingkey) - 1)
228 return error(_("committer info too long."));
229 bracket = strchr(signingkey, '>');
230 if (bracket)
231 bracket[1] = '\0';
234 /* When the username signingkey is bad, program could be terminated
235 * because gpg exits without reading and then write gets SIGPIPE. */
236 signal(SIGPIPE, SIG_IGN);
238 memset(&gpg, 0, sizeof(gpg));
239 gpg.argv = args;
240 gpg.in = -1;
241 gpg.out = -1;
242 args[0] = "gpg";
243 args[1] = "-bsau";
244 args[2] = signingkey;
245 args[3] = NULL;
247 if (start_command(&gpg))
248 return error(_("could not run gpg."));
250 if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
251 close(gpg.in);
252 close(gpg.out);
253 finish_command(&gpg);
254 return error(_("gpg did not accept the tag data"));
256 close(gpg.in);
257 len = strbuf_read(buffer, gpg.out, 1024);
258 close(gpg.out);
260 if (finish_command(&gpg) || !len || len < 0)
261 return error(_("gpg failed to sign the tag"));
263 /* Strip CR from the line endings, in case we are on Windows. */
264 for (i = j = 0; i < buffer->len; i++)
265 if (buffer->buf[i] != '\r') {
266 if (i != j)
267 buffer->buf[j] = buffer->buf[i];
268 j++;
270 strbuf_setlen(buffer, j);
272 return 0;
275 static const char tag_template[] =
276 N_("\n"
277 "#\n"
278 "# Write a tag message\n"
279 "#\n");
281 static void set_signingkey(const char *value)
283 if (strlcpy(signingkey, value, sizeof(signingkey)) >= sizeof(signingkey))
284 die(_("signing key value too long (%.10s...)"), value);
287 static int git_tag_config(const char *var, const char *value, void *cb)
289 if (!strcmp(var, "user.signingkey")) {
290 if (!value)
291 return config_error_nonbool(var);
292 set_signingkey(value);
293 return 0;
296 return git_default_config(var, value, cb);
299 static void write_tag_body(int fd, const unsigned char *sha1)
301 unsigned long size;
302 enum object_type type;
303 char *buf, *sp;
305 buf = read_sha1_file(sha1, &type, &size);
306 if (!buf)
307 return;
308 /* skip header */
309 sp = strstr(buf, "\n\n");
311 if (!sp || !size || type != OBJ_TAG) {
312 free(buf);
313 return;
315 sp += 2; /* skip the 2 LFs */
316 write_or_die(fd, sp, parse_signature(sp, buf + size - sp));
318 free(buf);
321 static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
323 if (sign && do_sign(buf) < 0)
324 return error(_("unable to sign the tag"));
325 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
326 return error(_("unable to write tag file"));
327 return 0;
330 static void create_tag(const unsigned char *object, const char *tag,
331 struct strbuf *buf, int message, int sign,
332 unsigned char *prev, unsigned char *result)
334 enum object_type type;
335 char header_buf[1024];
336 int header_len;
337 char *path = NULL;
339 type = sha1_object_info(object, NULL);
340 if (type <= OBJ_NONE)
341 die(_("bad object type."));
343 header_len = snprintf(header_buf, sizeof(header_buf),
344 "object %s\n"
345 "type %s\n"
346 "tag %s\n"
347 "tagger %s\n\n",
348 sha1_to_hex(object),
349 typename(type),
350 tag,
351 git_committer_info(IDENT_ERROR_ON_NO_NAME));
353 if (header_len > sizeof(header_buf) - 1)
354 die(_("tag header too big."));
356 if (!message) {
357 int fd;
359 /* write the template message before editing: */
360 path = git_pathdup("TAG_EDITMSG");
361 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
362 if (fd < 0)
363 die_errno(_("could not create file '%s'"), path);
365 if (!is_null_sha1(prev))
366 write_tag_body(fd, prev);
367 else
368 write_or_die(fd, _(tag_template), strlen(_(tag_template)));
369 close(fd);
371 if (launch_editor(path, buf, NULL)) {
372 fprintf(stderr,
373 _("Please supply the message using either -m or -F option.\n"));
374 exit(1);
378 stripspace(buf, 1);
380 if (!message && !buf->len)
381 die(_("no tag message?"));
383 strbuf_insert(buf, 0, header_buf, header_len);
385 if (build_tag_object(buf, sign, result) < 0) {
386 if (path)
387 fprintf(stderr, _("The tag message has been left in %s\n"),
388 path);
389 exit(128);
391 if (path) {
392 unlink_or_warn(path);
393 free(path);
397 struct msg_arg {
398 int given;
399 struct strbuf buf;
402 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
404 struct msg_arg *msg = opt->value;
406 if (!arg)
407 return -1;
408 if (msg->buf.len)
409 strbuf_addstr(&(msg->buf), "\n\n");
410 strbuf_addstr(&(msg->buf), arg);
411 msg->given = 1;
412 return 0;
415 static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
417 if (name[0] == '-')
418 return CHECK_REF_FORMAT_ERROR;
420 strbuf_reset(sb);
421 strbuf_addf(sb, "refs/tags/%s", name);
423 return check_ref_format(sb->buf);
426 int cmd_tag(int argc, const char **argv, const char *prefix)
428 struct strbuf buf = STRBUF_INIT;
429 struct strbuf ref = STRBUF_INIT;
430 unsigned char object[20], prev[20];
431 const char *object_ref, *tag;
432 struct ref_lock *lock;
434 int annotate = 0, sign = 0, force = 0, lines = -1,
435 list = 0, delete = 0, verify = 0;
436 const char *msgfile = NULL, *keyid = NULL;
437 struct msg_arg msg = { 0, STRBUF_INIT };
438 struct commit_list *with_commit = NULL;
439 struct option options[] = {
440 OPT_BOOLEAN('l', NULL, &list, "list tag names"),
441 { OPTION_INTEGER, 'n', NULL, &lines, "n",
442 "print <n> lines of each tag message",
443 PARSE_OPT_OPTARG, NULL, 1 },
444 OPT_BOOLEAN('d', NULL, &delete, "delete tags"),
445 OPT_BOOLEAN('v', NULL, &verify, "verify tags"),
447 OPT_GROUP("Tag creation options"),
448 OPT_BOOLEAN('a', NULL, &annotate,
449 "annotated tag, needs a message"),
450 OPT_CALLBACK('m', NULL, &msg, "message",
451 "tag message", parse_msg_arg),
452 OPT_FILENAME('F', NULL, &msgfile, "read message from file"),
453 OPT_BOOLEAN('s', NULL, &sign, "annotated and GPG-signed tag"),
454 OPT_STRING('u', NULL, &keyid, "key-id",
455 "use another key to sign the tag"),
456 OPT__FORCE(&force, "replace the tag if exists"),
458 OPT_GROUP("Tag listing options"),
460 OPTION_CALLBACK, 0, "contains", &with_commit, "commit",
461 "print only tags that contain the commit",
462 PARSE_OPT_LASTARG_DEFAULT,
463 parse_opt_with_commit, (intptr_t)"HEAD",
465 OPT_END()
468 git_config(git_tag_config, NULL);
470 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
472 if (keyid) {
473 sign = 1;
474 set_signingkey(keyid);
476 if (sign)
477 annotate = 1;
478 if (argc == 0 && !(delete || verify))
479 list = 1;
481 if ((annotate || msg.given || msgfile || force) &&
482 (list || delete || verify))
483 usage_with_options(git_tag_usage, options);
485 if (list + delete + verify > 1)
486 usage_with_options(git_tag_usage, options);
487 if (list)
488 return list_tags(argv, lines == -1 ? 0 : lines,
489 with_commit);
490 if (lines != -1)
491 die(_("-n option is only allowed with -l."));
492 if (with_commit)
493 die(_("--contains option is only allowed with -l."));
494 if (delete)
495 return for_each_tag_name(argv, delete_tag);
496 if (verify)
497 return for_each_tag_name(argv, verify_tag);
499 if (msg.given || msgfile) {
500 if (msg.given && msgfile)
501 die(_("only one -F or -m option is allowed."));
502 annotate = 1;
503 if (msg.given)
504 strbuf_addbuf(&buf, &(msg.buf));
505 else {
506 if (!strcmp(msgfile, "-")) {
507 if (strbuf_read(&buf, 0, 1024) < 0)
508 die_errno(_("cannot read '%s'"), msgfile);
509 } else {
510 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
511 die_errno(_("could not open or read '%s'"),
512 msgfile);
517 tag = argv[0];
519 object_ref = argc == 2 ? argv[1] : "HEAD";
520 if (argc > 2)
521 die(_("too many params"));
523 if (get_sha1(object_ref, object))
524 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
526 if (strbuf_check_tag_ref(&ref, tag))
527 die(_("'%s' is not a valid tag name."), tag);
529 if (!resolve_ref(ref.buf, prev, 1, NULL))
530 hashclr(prev);
531 else if (!force)
532 die(_("tag '%s' already exists"), tag);
534 if (annotate)
535 create_tag(object, tag, &buf, msg.given || msgfile,
536 sign, prev, object);
538 lock = lock_any_ref_for_update(ref.buf, prev, 0);
539 if (!lock)
540 die(_("%s: cannot lock the ref"), ref.buf);
541 if (write_ref_sha1(lock, object, NULL) < 0)
542 die(_("%s: cannot update the ref"), ref.buf);
543 if (force && hashcmp(prev, object))
544 printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
546 strbuf_release(&buf);
547 strbuf_release(&ref);
548 return 0;