blob: d72f742af9a4a6a76c8b0f6fd314473dea244b2a [file] [log] [blame]
Daniel Barkalow2636f612005-04-28 07:46:33 -07001#include "cache.h"
Junio C Hamano8f1d2e62006-01-07 01:33:54 -08002#include "tag.h"
Nicolas Pitre0ab17952007-02-26 14:56:00 -05003#include "commit.h"
4#include "tree.h"
5#include "blob.h"
Daniel Barkalow2636f612005-04-28 07:46:33 -07006
7const char *tag_type = "tag";
8
Junio C Hamano9534f402005-11-02 15:19:13 -08009struct object *deref_tag(struct object *o, const char *warn, int warnlen)
Junio C Hamano37fde872005-08-05 00:47:56 -070010{
Linus Torvalds19746322006-07-11 20:45:31 -070011 while (o && o->type == OBJ_TAG)
Martin Koegler24e8a3c2008-02-18 08:31:55 +010012 if (((struct tag *)o)->tagged)
brian m. carlsoned1c9972015-11-10 02:22:29 +000013 o = parse_object(((struct tag *)o)->tagged->oid.hash);
Martin Koegler24e8a3c2008-02-18 08:31:55 +010014 else
15 o = NULL;
Junio C Hamano9534f402005-11-02 15:19:13 -080016 if (!o && warn) {
17 if (!warnlen)
18 warnlen = strlen(warn);
19 error("missing object referenced by '%.*s'", warnlen, warn);
20 }
Junio C Hamano37fde872005-08-05 00:47:56 -070021 return o;
22}
23
Jeff King90108a22012-01-06 14:18:01 -050024struct object *deref_tag_noverify(struct object *o)
25{
26 while (o && o->type == OBJ_TAG) {
brian m. carlsoned1c9972015-11-10 02:22:29 +000027 o = parse_object(o->oid.hash);
Jeff King90108a22012-01-06 14:18:01 -050028 if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
29 o = ((struct tag *)o)->tagged;
30 else
31 o = NULL;
32 }
33 return o;
34}
35
Jason McMullan5d6ccf52005-06-03 11:05:39 -040036struct tag *lookup_tag(const unsigned char *sha1)
Daniel Barkalow2636f612005-04-28 07:46:33 -070037{
Linus Torvalds100c5f32007-04-16 22:11:43 -070038 struct object *obj = lookup_object(sha1);
39 if (!obj)
Jeff Kingd36f51c2014-07-13 02:41:55 -040040 return create_object(sha1, alloc_tag_node());
Jeff King8ff226a2014-07-13 02:42:03 -040041 return object_as_type(obj, OBJ_TAG, 0);
Daniel Barkalow2636f612005-04-28 07:46:33 -070042}
43
Shawn O. Pearcee451d062010-04-12 16:25:28 -070044static unsigned long parse_tag_date(const char *buf, const char *tail)
45{
46 const char *dateptr;
47
48 while (buf < tail && *buf++ != '>')
49 /* nada */;
50 if (buf >= tail)
51 return 0;
52 dateptr = buf;
53 while (buf < tail && *buf++ != '\n')
54 /* nada */;
55 if (buf >= tail)
56 return 0;
57 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
58 return strtoul(dateptr, NULL, 10);
59}
60
Nguyễn Thái Ngọc Duycf7b1ca2011-02-05 17:52:20 +070061int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
Daniel Barkalow2636f612005-04-28 07:46:33 -070062{
Nicolas Pitre0ab17952007-02-26 14:56:00 -050063 unsigned char sha1[20];
Daniel Barkalow89e42022005-06-21 20:35:10 -040064 char type[20];
Shawn O. Pearce28de5b62010-04-12 16:25:27 -070065 const char *bufptr = data;
66 const char *tail = bufptr + size;
67 const char *nl;
Edgar Toernigae200ee2005-04-30 09:51:03 -070068
Shawn O. Pearce2e0052a2010-04-12 16:25:25 -070069 if (item->object.parsed)
70 return 0;
71 item->object.parsed = 1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070072
Daniel Barkalow2636f612005-04-28 07:46:33 -070073 if (size < 64)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040074 return -1;
Shawn O. Pearce28de5b62010-04-12 16:25:27 -070075 if (memcmp("object ", bufptr, 7) || get_sha1_hex(bufptr + 7, sha1) || bufptr[47] != '\n')
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040076 return -1;
Shawn O. Pearce28de5b62010-04-12 16:25:27 -070077 bufptr += 48; /* "object " + sha1 + "\n" */
Daniel Barkalow2636f612005-04-28 07:46:33 -070078
Christian Couder59556542013-11-30 21:55:40 +010079 if (!starts_with(bufptr, "type "))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040080 return -1;
Shawn O. Pearce28de5b62010-04-12 16:25:27 -070081 bufptr += 5;
82 nl = memchr(bufptr, '\n', tail - bufptr);
83 if (!nl || sizeof(type) <= (nl - bufptr))
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -040084 return -1;
Jeff Kingeddda372015-09-24 17:08:26 -040085 memcpy(type, bufptr, nl - bufptr);
Shawn O. Pearce28de5b62010-04-12 16:25:27 -070086 type[nl - bufptr] = '\0';
87 bufptr = nl + 1;
Daniel Barkalow2636f612005-04-28 07:46:33 -070088
Nicolas Pitre0ab17952007-02-26 14:56:00 -050089 if (!strcmp(type, blob_type)) {
90 item->tagged = &lookup_blob(sha1)->object;
91 } else if (!strcmp(type, tree_type)) {
92 item->tagged = &lookup_tree(sha1)->object;
93 } else if (!strcmp(type, commit_type)) {
94 item->tagged = &lookup_commit(sha1)->object;
95 } else if (!strcmp(type, tag_type)) {
96 item->tagged = &lookup_tag(sha1)->object;
97 } else {
98 error("Unknown type %s", type);
99 item->tagged = NULL;
100 }
101
Christian Couder59556542013-11-30 21:55:40 +0100102 if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
Nguyễn Thái Ngọc Duy85594252011-02-14 20:02:51 +0700103 ; /* good */
104 else
Shawn O. Pearce28de5b62010-04-12 16:25:27 -0700105 return -1;
106 bufptr += 4;
107 nl = memchr(bufptr, '\n', tail - bufptr);
108 if (!nl)
109 return -1;
110 item->tag = xmemdupz(bufptr, nl - bufptr);
111 bufptr = nl + 1;
112
Christian Couder59556542013-11-30 21:55:40 +0100113 if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
Shawn O. Pearcee451d062010-04-12 16:25:28 -0700114 item->date = parse_tag_date(bufptr, tail);
115 else
116 item->date = 0;
117
Daniel Barkalow2636f612005-04-28 07:46:33 -0700118 return 0;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400119}
Sergey Vlasov13019d42005-05-04 21:44:15 +0400120
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400121int parse_tag(struct tag *item)
122{
Nicolas Pitre21666f12007-02-26 14:55:59 -0500123 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400124 void *data;
125 unsigned long size;
126 int ret;
127
128 if (item->object.parsed)
129 return 0;
brian m. carlsoned1c9972015-11-10 02:22:29 +0000130 data = read_sha1_file(item->object.oid.hash, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400131 if (!data)
132 return error("Could not read %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000133 oid_to_hex(&item->object.oid));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500134 if (type != OBJ_TAG) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400135 free(data);
136 return error("Object %s not a tag",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000137 oid_to_hex(&item->object.oid));
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400138 }
139 ret = parse_tag_buffer(item, data, size);
Sergey Vlasov13019d42005-05-04 21:44:15 +0400140 free(data);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400141 return ret;
Daniel Barkalow2636f612005-04-28 07:46:33 -0700142}