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