limit "contains" traversals based on commit timestamp
[git/mingw.git] / builtin / tag.c
blob72140b3986098593185d43d3014d0fa9c3ca60c1
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 static int core_clock_skew = -1;
30 struct tag_filter {
31 const char *pattern;
32 int lines;
33 struct commit_list *with_commit;
36 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
38 static int in_commit_list(const struct commit_list *want, struct commit *c)
40 for (; want; want = want->next)
41 if (!hashcmp(want->item->object.sha1, c->object.sha1))
42 return 1;
43 return 0;
46 static int contains_recurse(struct commit *candidate,
47 const struct commit_list *want,
48 unsigned long cutoff)
50 struct commit_list *p;
52 /* was it previously marked as containing a want commit? */
53 if (candidate->object.flags & TMP_MARK)
54 return 1;
55 /* or marked as not possibly containing a want commit? */
56 if (candidate->object.flags & UNINTERESTING)
57 return 0;
58 /* or are we it? */
59 if (in_commit_list(want, candidate))
60 return 1;
62 if (parse_commit(candidate) < 0)
63 return 0;
65 /* stop searching if we go too far back in time */
66 if (candidate->date < cutoff)
67 return 0;
69 /* Otherwise recurse and mark ourselves for future traversals. */
70 for (p = candidate->parents; p; p = p->next) {
71 if (contains_recurse(p->item, want, cutoff)) {
72 candidate->object.flags |= TMP_MARK;
73 return 1;
76 candidate->object.flags |= UNINTERESTING;
77 return 0;
80 static int contains(struct commit *candidate, const struct commit_list *want)
82 unsigned long cutoff = 0;
84 if (core_clock_skew >= 0) {
85 const struct commit_list *c;
86 unsigned long min_date = ULONG_MAX;
87 for (c = want; c; c = c->next) {
88 if (parse_commit(c->item) < 0)
89 continue;
90 if (c->item->date < min_date)
91 min_date = c->item->date;
93 if (min_date > core_clock_skew)
94 cutoff = min_date - core_clock_skew;
97 return contains_recurse(candidate, want, cutoff);
100 static int show_reference(const char *refname, const unsigned char *sha1,
101 int flag, void *cb_data)
103 struct tag_filter *filter = cb_data;
105 if (!fnmatch(filter->pattern, refname, 0)) {
106 int i;
107 unsigned long size;
108 enum object_type type;
109 char *buf, *sp, *eol;
110 size_t len;
112 if (filter->with_commit) {
113 struct commit *commit;
115 commit = lookup_commit_reference_gently(sha1, 1);
116 if (!commit)
117 return 0;
118 if (!contains(commit, filter->with_commit))
119 return 0;
122 if (!filter->lines) {
123 printf("%s\n", refname);
124 return 0;
126 printf("%-15s ", refname);
128 buf = read_sha1_file(sha1, &type, &size);
129 if (!buf || !size)
130 return 0;
132 /* skip header */
133 sp = strstr(buf, "\n\n");
134 if (!sp) {
135 free(buf);
136 return 0;
138 /* only take up to "lines" lines, and strip the signature */
139 for (i = 0, sp += 2;
140 i < filter->lines && sp < buf + size &&
141 prefixcmp(sp, PGP_SIGNATURE "\n");
142 i++) {
143 if (i)
144 printf("\n ");
145 eol = memchr(sp, '\n', size - (sp - buf));
146 len = eol ? eol - sp : size - (sp - buf);
147 fwrite(sp, len, 1, stdout);
148 if (!eol)
149 break;
150 sp = eol + 1;
152 putchar('\n');
153 free(buf);
156 return 0;
159 static int list_tags(const char *pattern, int lines,
160 struct commit_list *with_commit)
162 struct tag_filter filter;
164 if (pattern == NULL)
165 pattern = "*";
167 filter.pattern = pattern;
168 filter.lines = lines;
169 filter.with_commit = with_commit;
171 for_each_tag_ref(show_reference, (void *) &filter);
173 return 0;
176 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
177 const unsigned char *sha1);
179 static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
181 const char **p;
182 char ref[PATH_MAX];
183 int had_error = 0;
184 unsigned char sha1[20];
186 for (p = argv; *p; p++) {
187 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
188 >= sizeof(ref)) {
189 error("tag name too long: %.*s...", 50, *p);
190 had_error = 1;
191 continue;
193 if (!resolve_ref(ref, sha1, 1, NULL)) {
194 error("tag '%s' not found.", *p);
195 had_error = 1;
196 continue;
198 if (fn(*p, ref, sha1))
199 had_error = 1;
201 return had_error;
204 static int delete_tag(const char *name, const char *ref,
205 const unsigned char *sha1)
207 if (delete_ref(ref, sha1, 0))
208 return 1;
209 printf("Deleted tag '%s' (was %s)\n", name, find_unique_abbrev(sha1, DEFAULT_ABBREV));
210 return 0;
213 static int verify_tag(const char *name, const char *ref,
214 const unsigned char *sha1)
216 const char *argv_verify_tag[] = {"verify-tag",
217 "-v", "SHA1_HEX", NULL};
218 argv_verify_tag[2] = sha1_to_hex(sha1);
220 if (run_command_v_opt(argv_verify_tag, RUN_GIT_CMD))
221 return error("could not verify the tag '%s'", name);
222 return 0;
225 static int do_sign(struct strbuf *buffer)
227 struct child_process gpg;
228 const char *args[4];
229 char *bracket;
230 int len;
231 int i, j;
233 if (!*signingkey) {
234 if (strlcpy(signingkey, git_committer_info(IDENT_ERROR_ON_NO_NAME),
235 sizeof(signingkey)) > sizeof(signingkey) - 1)
236 return error("committer info too long.");
237 bracket = strchr(signingkey, '>');
238 if (bracket)
239 bracket[1] = '\0';
242 /* When the username signingkey is bad, program could be terminated
243 * because gpg exits without reading and then write gets SIGPIPE. */
244 signal(SIGPIPE, SIG_IGN);
246 memset(&gpg, 0, sizeof(gpg));
247 gpg.argv = args;
248 gpg.in = -1;
249 gpg.out = -1;
250 args[0] = "gpg";
251 args[1] = "-bsau";
252 args[2] = signingkey;
253 args[3] = NULL;
255 if (start_command(&gpg))
256 return error("could not run gpg.");
258 if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
259 close(gpg.in);
260 close(gpg.out);
261 finish_command(&gpg);
262 return error("gpg did not accept the tag data");
264 close(gpg.in);
265 len = strbuf_read(buffer, gpg.out, 1024);
266 close(gpg.out);
268 if (finish_command(&gpg) || !len || len < 0)
269 return error("gpg failed to sign the tag");
271 /* Strip CR from the line endings, in case we are on Windows. */
272 for (i = j = 0; i < buffer->len; i++)
273 if (buffer->buf[i] != '\r') {
274 if (i != j)
275 buffer->buf[j] = buffer->buf[i];
276 j++;
278 strbuf_setlen(buffer, j);
280 return 0;
283 static const char tag_template[] =
284 "\n"
285 "#\n"
286 "# Write a tag message\n"
287 "#\n";
289 static void set_signingkey(const char *value)
291 if (strlcpy(signingkey, value, sizeof(signingkey)) >= sizeof(signingkey))
292 die("signing key value too long (%.10s...)", value);
295 static int git_tag_config(const char *var, const char *value, void *cb)
297 if (!strcmp(var, "user.signingkey")) {
298 if (!value)
299 return config_error_nonbool(var);
300 set_signingkey(value);
301 return 0;
304 if (!strcmp(var, "core.clockskew")) {
305 if (!value || !strcmp(value, "none"))
306 core_clock_skew = -1;
307 else
308 core_clock_skew = git_config_int(var, value);
309 return 0;
312 return git_default_config(var, value, cb);
315 static void write_tag_body(int fd, const unsigned char *sha1)
317 unsigned long size;
318 enum object_type type;
319 char *buf, *sp, *eob;
320 size_t len;
322 buf = read_sha1_file(sha1, &type, &size);
323 if (!buf)
324 return;
325 /* skip header */
326 sp = strstr(buf, "\n\n");
328 if (!sp || !size || type != OBJ_TAG) {
329 free(buf);
330 return;
332 sp += 2; /* skip the 2 LFs */
333 eob = strstr(sp, "\n" PGP_SIGNATURE "\n");
334 if (eob)
335 len = eob - sp;
336 else
337 len = buf + size - sp;
338 write_or_die(fd, sp, len);
340 free(buf);
343 static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
345 if (sign && do_sign(buf) < 0)
346 return error("unable to sign the tag");
347 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
348 return error("unable to write tag file");
349 return 0;
352 static void create_tag(const unsigned char *object, const char *tag,
353 struct strbuf *buf, int message, int sign,
354 unsigned char *prev, unsigned char *result)
356 enum object_type type;
357 char header_buf[1024];
358 int header_len;
359 char *path = NULL;
361 type = sha1_object_info(object, NULL);
362 if (type <= OBJ_NONE)
363 die("bad object type.");
365 header_len = snprintf(header_buf, sizeof(header_buf),
366 "object %s\n"
367 "type %s\n"
368 "tag %s\n"
369 "tagger %s\n\n",
370 sha1_to_hex(object),
371 typename(type),
372 tag,
373 git_committer_info(IDENT_ERROR_ON_NO_NAME));
375 if (header_len > sizeof(header_buf) - 1)
376 die("tag header too big.");
378 if (!message) {
379 int fd;
381 /* write the template message before editing: */
382 path = git_pathdup("TAG_EDITMSG");
383 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
384 if (fd < 0)
385 die_errno("could not create file '%s'", path);
387 if (!is_null_sha1(prev))
388 write_tag_body(fd, prev);
389 else
390 write_or_die(fd, tag_template, strlen(tag_template));
391 close(fd);
393 if (launch_editor(path, buf, NULL)) {
394 fprintf(stderr,
395 "Please supply the message using either -m or -F option.\n");
396 exit(1);
400 stripspace(buf, 1);
402 if (!message && !buf->len)
403 die("no tag message?");
405 strbuf_insert(buf, 0, header_buf, header_len);
407 if (build_tag_object(buf, sign, result) < 0) {
408 if (path)
409 fprintf(stderr, "The tag message has been left in %s\n",
410 path);
411 exit(128);
413 if (path) {
414 unlink_or_warn(path);
415 free(path);
419 struct msg_arg {
420 int given;
421 struct strbuf buf;
424 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
426 struct msg_arg *msg = opt->value;
428 if (!arg)
429 return -1;
430 if (msg->buf.len)
431 strbuf_addstr(&(msg->buf), "\n\n");
432 strbuf_addstr(&(msg->buf), arg);
433 msg->given = 1;
434 return 0;
437 int cmd_tag(int argc, const char **argv, const char *prefix)
439 struct strbuf buf = STRBUF_INIT;
440 unsigned char object[20], prev[20];
441 char ref[PATH_MAX];
442 const char *object_ref, *tag;
443 struct ref_lock *lock;
445 int annotate = 0, sign = 0, force = 0, lines = -1,
446 list = 0, delete = 0, verify = 0;
447 const char *msgfile = NULL, *keyid = NULL;
448 struct msg_arg msg = { 0, STRBUF_INIT };
449 struct commit_list *with_commit = NULL;
450 struct option options[] = {
451 OPT_BOOLEAN('l', NULL, &list, "list tag names"),
452 { OPTION_INTEGER, 'n', NULL, &lines, "n",
453 "print <n> lines of each tag message",
454 PARSE_OPT_OPTARG, NULL, 1 },
455 OPT_BOOLEAN('d', NULL, &delete, "delete tags"),
456 OPT_BOOLEAN('v', NULL, &verify, "verify tags"),
458 OPT_GROUP("Tag creation options"),
459 OPT_BOOLEAN('a', NULL, &annotate,
460 "annotated tag, needs a message"),
461 OPT_CALLBACK('m', NULL, &msg, "msg",
462 "message for the tag", parse_msg_arg),
463 OPT_FILENAME('F', NULL, &msgfile, "message in a file"),
464 OPT_BOOLEAN('s', NULL, &sign, "annotated and GPG-signed tag"),
465 OPT_STRING('u', NULL, &keyid, "key-id",
466 "use another key to sign the tag"),
467 OPT_BOOLEAN('f', "force", &force, "replace the tag if exists"),
469 OPT_GROUP("Tag listing options"),
471 OPTION_CALLBACK, 0, "contains", &with_commit, "commit",
472 "print only tags that contain the commit",
473 PARSE_OPT_LASTARG_DEFAULT,
474 parse_opt_with_commit, (intptr_t)"HEAD",
476 OPT_END()
479 git_config(git_tag_config, NULL);
481 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
483 if (keyid) {
484 sign = 1;
485 set_signingkey(keyid);
487 if (sign)
488 annotate = 1;
489 if (argc == 0 && !(delete || verify))
490 list = 1;
492 if ((annotate || msg.given || msgfile || force) &&
493 (list || delete || verify))
494 usage_with_options(git_tag_usage, options);
496 if (list + delete + verify > 1)
497 usage_with_options(git_tag_usage, options);
498 if (list)
499 return list_tags(argv[0], lines == -1 ? 0 : lines,
500 with_commit);
501 if (lines != -1)
502 die("-n option is only allowed with -l.");
503 if (with_commit)
504 die("--contains option is only allowed with -l.");
505 if (delete)
506 return for_each_tag_name(argv, delete_tag);
507 if (verify)
508 return for_each_tag_name(argv, verify_tag);
510 if (msg.given || msgfile) {
511 if (msg.given && msgfile)
512 die("only one -F or -m option is allowed.");
513 annotate = 1;
514 if (msg.given)
515 strbuf_addbuf(&buf, &(msg.buf));
516 else {
517 if (!strcmp(msgfile, "-")) {
518 if (strbuf_read(&buf, 0, 1024) < 0)
519 die_errno("cannot read '%s'", msgfile);
520 } else {
521 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
522 die_errno("could not open or read '%s'",
523 msgfile);
528 tag = argv[0];
530 object_ref = argc == 2 ? argv[1] : "HEAD";
531 if (argc > 2)
532 die("too many params");
534 if (get_sha1(object_ref, object))
535 die("Failed to resolve '%s' as a valid ref.", object_ref);
537 if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) > sizeof(ref) - 1)
538 die("tag name too long: %.*s...", 50, tag);
539 if (check_ref_format(ref))
540 die("'%s' is not a valid tag name.", tag);
542 if (!resolve_ref(ref, prev, 1, NULL))
543 hashclr(prev);
544 else if (!force)
545 die("tag '%s' already exists", tag);
547 if (annotate)
548 create_tag(object, tag, &buf, msg.given || msgfile,
549 sign, prev, object);
551 lock = lock_any_ref_for_update(ref, prev, 0);
552 if (!lock)
553 die("%s: cannot lock the ref", ref);
554 if (write_ref_sha1(lock, object, NULL) < 0)
555 die("%s: cannot update the ref", ref);
556 if (force && hashcmp(prev, object))
557 printf("Updated tag '%s' (was %s)\n", tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
559 strbuf_release(&buf);
560 return 0;