THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
5.0KB

  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(r, 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. item->object.parsed = 1;
  124. if (size < the_hash_algo->hexsz + 24)
  125. return -1;
  126. if (memcmp("object ", bufptr, 7) || parse_oid_hex(bufptr + 7, &oid, &bufptr) || *bufptr++ != '\n')
  127. return -1;
  128. if (!starts_with(bufptr, "type "))
  129. return -1;
  130. bufptr += 5;
  131. nl = memchr(bufptr, '\n', tail - bufptr);
  132. if (!nl || sizeof(type) <= (nl - bufptr))
  133. return -1;
  134. memcpy(type, bufptr, nl - bufptr);
  135. type[nl - bufptr] = '\0';
  136. bufptr = nl + 1;
  137. if (!strcmp(type, blob_type)) {
  138. item->tagged = (struct object *)lookup_blob(r, &oid);
  139. } else if (!strcmp(type, tree_type)) {
  140. item->tagged = (struct object *)lookup_tree(r, &oid);
  141. } else if (!strcmp(type, commit_type)) {
  142. item->tagged = (struct object *)lookup_commit(r, &oid);
  143. } else if (!strcmp(type, tag_type)) {
  144. item->tagged = (struct object *)lookup_tag(r, &oid);
  145. } else {
  146. error("Unknown type %s", type);
  147. item->tagged = NULL;
  148. }
  149. if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
  150. ; /* good */
  151. else
  152. return -1;
  153. bufptr += 4;
  154. nl = memchr(bufptr, '\n', tail - bufptr);
  155. if (!nl)
  156. return -1;
  157. item->tag = xmemdupz(bufptr, nl - bufptr);
  158. bufptr = nl + 1;
  159. if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
  160. item->date = parse_tag_date(bufptr, tail);
  161. else
  162. item->date = 0;
  163. return 0;
  164. }
  165. int parse_tag(struct tag *item)
  166. {
  167. enum object_type type;
  168. void *data;
  169. unsigned long size;
  170. int ret;
  171. if (item->object.parsed)
  172. return 0;
  173. data = read_object_file(&item->object.oid, &type, &size);
  174. if (!data)
  175. return error("Could not read %s",
  176. oid_to_hex(&item->object.oid));
  177. if (type != OBJ_TAG) {
  178. free(data);
  179. return error("Object %s not a tag",
  180. oid_to_hex(&item->object.oid));
  181. }
  182. ret = parse_tag_buffer(the_repository, item, data, size);
  183. free(data);
  184. return ret;
  185. }
  186. struct object_id *get_tagged_oid(struct tag *tag)
  187. {
  188. if (!tag->tagged)
  189. die("bad tag");
  190. return &tag->tagged->oid;
  191. }