tag.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include "cache.h"
  2. #include "tag.h"
  3. #include "object-store.h"
  4. #include "commit.h"
  5. #include "tree.h"
  6. #include "blob.h"
  7. #include "alloc.h"
  8. #include "gpg-interface.h"
  9. #include "packfile.h"
  10. const char *tag_type = "tag";
  11. static int run_gpg_verify(const char *buf, unsigned long size, unsigned flags)
  12. {
  13. struct signature_check sigc;
  14. size_t payload_size;
  15. int ret;
  16. memset(&sigc, 0, sizeof(sigc));
  17. payload_size = parse_signature(buf, size);
  18. if (size == payload_size) {
  19. if (flags & GPG_VERIFY_VERBOSE)
  20. write_in_full(1, buf, payload_size);
  21. return error("no signature found");
  22. }
  23. ret = check_signature(buf, payload_size, buf + payload_size,
  24. size - payload_size, &sigc);
  25. if (!(flags & GPG_VERIFY_OMIT_STATUS))
  26. print_signature_buffer(&sigc, flags);
  27. signature_check_clear(&sigc);
  28. return ret;
  29. }
  30. int gpg_verify_tag(const struct object_id *oid, const char *name_to_report,
  31. unsigned flags)
  32. {
  33. enum object_type type;
  34. char *buf;
  35. unsigned long size;
  36. int ret;
  37. type = oid_object_info(the_repository, oid, NULL);
  38. if (type != OBJ_TAG)
  39. return error("%s: cannot verify a non-tag object of type %s.",
  40. name_to_report ?
  41. name_to_report :
  42. find_unique_abbrev(oid, DEFAULT_ABBREV),
  43. type_name(type));
  44. buf = read_object_file(oid, &type, &size);
  45. if (!buf)
  46. return error("%s: unable to read file.",
  47. name_to_report ?
  48. name_to_report :
  49. find_unique_abbrev(oid, DEFAULT_ABBREV));
  50. ret = run_gpg_verify(buf, size, flags);
  51. free(buf);
  52. return ret;
  53. }
  54. struct object *deref_tag(struct repository *r, struct object *o, const char *warn, int warnlen)
  55. {
  56. struct object_id *last_oid = NULL;
  57. while (o && o->type == OBJ_TAG)
  58. if (((struct tag *)o)->tagged) {
  59. last_oid = &((struct tag *)o)->tagged->oid;
  60. o = parse_object(r, last_oid);
  61. } else {
  62. last_oid = NULL;
  63. o = NULL;
  64. }
  65. if (!o && warn) {
  66. if (last_oid && is_promisor_object(last_oid))
  67. return NULL;
  68. if (!warnlen)
  69. warnlen = strlen(warn);
  70. error("missing object referenced by '%.*s'", warnlen, warn);
  71. }
  72. return o;
  73. }
  74. struct object *deref_tag_noverify(struct object *o)
  75. {
  76. while (o && o->type == OBJ_TAG) {
  77. o = parse_object(the_repository, &o->oid);
  78. if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
  79. o = ((struct tag *)o)->tagged;
  80. else
  81. o = NULL;
  82. }
  83. return o;
  84. }
  85. struct tag *lookup_tag(struct repository *r, const struct object_id *oid)
  86. {
  87. struct object *obj = lookup_object(r, oid);
  88. if (!obj)
  89. return create_object(r, oid, alloc_tag_node(r));
  90. return object_as_type(obj, OBJ_TAG, 0);
  91. }
  92. static timestamp_t parse_tag_date(const char *buf, const char *tail)
  93. {
  94. const char *dateptr;
  95. while (buf < tail && *buf++ != '>')
  96. /* nada */;
  97. if (buf >= tail)
  98. return 0;
  99. dateptr = buf;
  100. while (buf < tail && *buf++ != '\n')
  101. /* nada */;
  102. if (buf >= tail)
  103. return 0;
  104. /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
  105. return parse_timestamp(dateptr, NULL, 10);
  106. }
  107. void release_tag_memory(struct tag *t)
  108. {
  109. free(t->tag);
  110. t->tagged = NULL;
  111. t->object.parsed = 0;
  112. t->date = 0;
  113. }
  114. int parse_tag_buffer(struct repository *r, struct tag *item, const void *data, unsigned long size)
  115. {
  116. struct object_id oid;
  117. char type[20];
  118. const char *bufptr = data;
  119. const char *tail = bufptr + size;
  120. const char *nl;
  121. if (item->object.parsed)
  122. return 0;
  123. if (item->tag) {
  124. /*
  125. * Presumably left over from a previous failed parse;
  126. * clear it out in preparation for re-parsing (we'll probably
  127. * hit the same error, which lets us tell our current caller
  128. * about the problem).
  129. */
  130. FREE_AND_NULL(item->tag);
  131. }
  132. if (size < the_hash_algo->hexsz + 24)
  133. return -1;
  134. if (memcmp("object ", bufptr, 7) || parse_oid_hex(bufptr + 7, &oid, &bufptr) || *bufptr++ != '\n')
  135. return -1;
  136. if (!starts_with(bufptr, "type "))
  137. return -1;
  138. bufptr += 5;
  139. nl = memchr(bufptr, '\n', tail - bufptr);
  140. if (!nl || sizeof(type) <= (nl - bufptr))
  141. return -1;
  142. memcpy(type, bufptr, nl - bufptr);
  143. type[nl - bufptr] = '\0';
  144. bufptr = nl + 1;
  145. if (!strcmp(type, blob_type)) {
  146. item->tagged = (struct object *)lookup_blob(r, &oid);
  147. } else if (!strcmp(type, tree_type)) {
  148. item->tagged = (struct object *)lookup_tree(r, &oid);
  149. } else if (!strcmp(type, commit_type)) {
  150. item->tagged = (struct object *)lookup_commit(r, &oid);
  151. } else if (!strcmp(type, tag_type)) {
  152. item->tagged = (struct object *)lookup_tag(r, &oid);
  153. } else {
  154. return error("unknown tag type '%s' in %s",
  155. type, oid_to_hex(&item->object.oid));
  156. }
  157. if (!item->tagged)
  158. return error("bad tag pointer to %s in %s",
  159. oid_to_hex(&oid),
  160. oid_to_hex(&item->object.oid));
  161. if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
  162. ; /* good */
  163. else
  164. return -1;
  165. bufptr += 4;
  166. nl = memchr(bufptr, '\n', tail - bufptr);
  167. if (!nl)
  168. return -1;
  169. item->tag = xmemdupz(bufptr, nl - bufptr);
  170. bufptr = nl + 1;
  171. if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
  172. item->date = parse_tag_date(bufptr, tail);
  173. else
  174. item->date = 0;
  175. item->object.parsed = 1;
  176. return 0;
  177. }
  178. int parse_tag(struct tag *item)
  179. {
  180. enum object_type type;
  181. void *data;
  182. unsigned long size;
  183. int ret;
  184. if (item->object.parsed)
  185. return 0;
  186. data = read_object_file(&item->object.oid, &type, &size);
  187. if (!data)
  188. return error("Could not read %s",
  189. oid_to_hex(&item->object.oid));
  190. if (type != OBJ_TAG) {
  191. free(data);
  192. return error("Object %s not a tag",
  193. oid_to_hex(&item->object.oid));
  194. }
  195. ret = parse_tag_buffer(the_repository, item, data, size);
  196. free(data);
  197. return ret;
  198. }
  199. struct object_id *get_tagged_oid(struct tag *tag)
  200. {
  201. if (!tag->tagged)
  202. die("bad tag");
  203. return &tag->tagged->oid;
  204. }