Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Junio C Hamano | 8f1d2e6 | 2006-01-07 01:33:54 -0800 | [diff] [blame] | 2 | #include "tag.h" |
Nicolas Pitre | 0ab1795 | 2007-02-26 14:56:00 -0500 | [diff] [blame] | 3 | #include "commit.h" |
| 4 | #include "tree.h" |
| 5 | #include "blob.h" |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 6 | |
Michael J Gruber | ac58c4c | 2010-11-10 12:17:27 +0100 | [diff] [blame^] | 7 | #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----" |
| 8 | |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 9 | const char *tag_type = "tag"; |
| 10 | |
Junio C Hamano | 9534f40 | 2005-11-02 15:19:13 -0800 | [diff] [blame] | 11 | struct object *deref_tag(struct object *o, const char *warn, int warnlen) |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 12 | { |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 13 | while (o && o->type == OBJ_TAG) |
Martin Koegler | 24e8a3c | 2008-02-18 08:31:55 +0100 | [diff] [blame] | 14 | if (((struct tag *)o)->tagged) |
| 15 | o = parse_object(((struct tag *)o)->tagged->sha1); |
| 16 | else |
| 17 | o = NULL; |
Junio C Hamano | 9534f40 | 2005-11-02 15:19:13 -0800 | [diff] [blame] | 18 | if (!o && warn) { |
| 19 | if (!warnlen) |
| 20 | warnlen = strlen(warn); |
| 21 | error("missing object referenced by '%.*s'", warnlen, warn); |
| 22 | } |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 23 | return o; |
| 24 | } |
| 25 | |
Jason McMullan | 5d6ccf5 | 2005-06-03 11:05:39 -0400 | [diff] [blame] | 26 | struct tag *lookup_tag(const unsigned char *sha1) |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 27 | { |
Linus Torvalds | 100c5f3 | 2007-04-16 22:11:43 -0700 | [diff] [blame] | 28 | struct object *obj = lookup_object(sha1); |
| 29 | if (!obj) |
| 30 | return create_object(sha1, OBJ_TAG, alloc_tag_node()); |
Nicolas Pitre | d1af002 | 2005-05-20 16:59:17 -0400 | [diff] [blame] | 31 | if (!obj->type) |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 32 | obj->type = OBJ_TAG; |
Junio C Hamano | d2c030d | 2010-09-05 22:32:05 -0700 | [diff] [blame] | 33 | if (obj->type != OBJ_TAG) { |
| 34 | error("Object %s is a %s, not a tag", |
| 35 | sha1_to_hex(sha1), typename(obj->type)); |
| 36 | return NULL; |
| 37 | } |
| 38 | return (struct tag *) obj; |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Shawn O. Pearce | e451d06 | 2010-04-12 16:25:28 -0700 | [diff] [blame] | 41 | static unsigned long parse_tag_date(const char *buf, const char *tail) |
| 42 | { |
| 43 | const char *dateptr; |
| 44 | |
| 45 | while (buf < tail && *buf++ != '>') |
| 46 | /* nada */; |
| 47 | if (buf >= tail) |
| 48 | return 0; |
| 49 | dateptr = buf; |
| 50 | while (buf < tail && *buf++ != '\n') |
| 51 | /* nada */; |
| 52 | if (buf >= tail) |
| 53 | return 0; |
| 54 | /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */ |
| 55 | return strtoul(dateptr, NULL, 10); |
| 56 | } |
| 57 | |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 58 | int parse_tag_buffer(struct tag *item, void *data, unsigned long size) |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 59 | { |
Nicolas Pitre | 0ab1795 | 2007-02-26 14:56:00 -0500 | [diff] [blame] | 60 | unsigned char sha1[20]; |
Daniel Barkalow | 89e4202 | 2005-06-21 20:35:10 -0400 | [diff] [blame] | 61 | char type[20]; |
Shawn O. Pearce | 28de5b6 | 2010-04-12 16:25:27 -0700 | [diff] [blame] | 62 | const char *bufptr = data; |
| 63 | const char *tail = bufptr + size; |
| 64 | const char *nl; |
Edgar Toernig | ae200ee | 2005-04-30 09:51:03 -0700 | [diff] [blame] | 65 | |
Shawn O. Pearce | 2e0052a | 2010-04-12 16:25:25 -0700 | [diff] [blame] | 66 | if (item->object.parsed) |
| 67 | return 0; |
| 68 | item->object.parsed = 1; |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 69 | |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 70 | if (size < 64) |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 71 | return -1; |
Shawn O. Pearce | 28de5b6 | 2010-04-12 16:25:27 -0700 | [diff] [blame] | 72 | if (memcmp("object ", bufptr, 7) || get_sha1_hex(bufptr + 7, sha1) || bufptr[47] != '\n') |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 73 | return -1; |
Shawn O. Pearce | 28de5b6 | 2010-04-12 16:25:27 -0700 | [diff] [blame] | 74 | bufptr += 48; /* "object " + sha1 + "\n" */ |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 75 | |
Shawn O. Pearce | 28de5b6 | 2010-04-12 16:25:27 -0700 | [diff] [blame] | 76 | if (prefixcmp(bufptr, "type ")) |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 77 | return -1; |
Shawn O. Pearce | 28de5b6 | 2010-04-12 16:25:27 -0700 | [diff] [blame] | 78 | bufptr += 5; |
| 79 | nl = memchr(bufptr, '\n', tail - bufptr); |
| 80 | if (!nl || sizeof(type) <= (nl - bufptr)) |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 81 | return -1; |
Shawn O. Pearce | 28de5b6 | 2010-04-12 16:25:27 -0700 | [diff] [blame] | 82 | strncpy(type, bufptr, nl - bufptr); |
| 83 | type[nl - bufptr] = '\0'; |
| 84 | bufptr = nl + 1; |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 85 | |
Nicolas Pitre | 0ab1795 | 2007-02-26 14:56:00 -0500 | [diff] [blame] | 86 | if (!strcmp(type, blob_type)) { |
| 87 | item->tagged = &lookup_blob(sha1)->object; |
| 88 | } else if (!strcmp(type, tree_type)) { |
| 89 | item->tagged = &lookup_tree(sha1)->object; |
| 90 | } else if (!strcmp(type, commit_type)) { |
| 91 | item->tagged = &lookup_commit(sha1)->object; |
| 92 | } else if (!strcmp(type, tag_type)) { |
| 93 | item->tagged = &lookup_tag(sha1)->object; |
| 94 | } else { |
| 95 | error("Unknown type %s", type); |
| 96 | item->tagged = NULL; |
| 97 | } |
| 98 | |
Shawn O. Pearce | 28de5b6 | 2010-04-12 16:25:27 -0700 | [diff] [blame] | 99 | if (prefixcmp(bufptr, "tag ")) |
| 100 | return -1; |
| 101 | bufptr += 4; |
| 102 | nl = memchr(bufptr, '\n', tail - bufptr); |
| 103 | if (!nl) |
| 104 | return -1; |
| 105 | item->tag = xmemdupz(bufptr, nl - bufptr); |
| 106 | bufptr = nl + 1; |
| 107 | |
Shawn O. Pearce | e451d06 | 2010-04-12 16:25:28 -0700 | [diff] [blame] | 108 | if (!prefixcmp(bufptr, "tagger ")) |
| 109 | item->date = parse_tag_date(bufptr, tail); |
| 110 | else |
| 111 | item->date = 0; |
| 112 | |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 113 | return 0; |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 114 | } |
Sergey Vlasov | 13019d4 | 2005-05-04 21:44:15 +0400 | [diff] [blame] | 115 | |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 116 | int parse_tag(struct tag *item) |
| 117 | { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 118 | enum object_type type; |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 119 | void *data; |
| 120 | unsigned long size; |
| 121 | int ret; |
| 122 | |
| 123 | if (item->object.parsed) |
| 124 | return 0; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 125 | data = read_sha1_file(item->object.sha1, &type, &size); |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 126 | if (!data) |
| 127 | return error("Could not read %s", |
| 128 | sha1_to_hex(item->object.sha1)); |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 129 | if (type != OBJ_TAG) { |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 130 | free(data); |
| 131 | return error("Object %s not a tag", |
| 132 | sha1_to_hex(item->object.sha1)); |
| 133 | } |
| 134 | ret = parse_tag_buffer(item, data, size); |
Sergey Vlasov | 13019d4 | 2005-05-04 21:44:15 +0400 | [diff] [blame] | 135 | free(data); |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 136 | return ret; |
Daniel Barkalow | 2636f61 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 137 | } |
Michael J Gruber | ac58c4c | 2010-11-10 12:17:27 +0100 | [diff] [blame^] | 138 | |
| 139 | size_t parse_signature(const char *buf, unsigned long size) |
| 140 | { |
| 141 | char *eol; |
| 142 | size_t len = 0; |
| 143 | while (len < size && prefixcmp(buf + len, PGP_SIGNATURE)) { |
| 144 | eol = memchr(buf + len, '\n', size - len); |
| 145 | len += eol ? eol - (buf + len) + 1 : size - len; |
| 146 | } |
| 147 | return len; |
| 148 | } |