Git 1.7.2-rc2
[git/mingw/4msysgit/kblees.git] / tag.c
blob85607c219e25d63b0d1f3344649104c60f5b96e2
1 #include "cache.h"
2 #include "tag.h"
3 #include "commit.h"
4 #include "tree.h"
5 #include "blob.h"
7 const char *tag_type = "tag";
9 struct object *deref_tag(struct object *o, const char *warn, int warnlen)
11 while (o && o->type == OBJ_TAG)
12 if (((struct tag *)o)->tagged)
13 o = parse_object(((struct tag *)o)->tagged->sha1);
14 else
15 o = NULL;
16 if (!o && warn) {
17 if (!warnlen)
18 warnlen = strlen(warn);
19 error("missing object referenced by '%.*s'", warnlen, warn);
21 return o;
24 struct tag *lookup_tag(const unsigned char *sha1)
26 struct object *obj = lookup_object(sha1);
27 if (!obj)
28 return create_object(sha1, OBJ_TAG, alloc_tag_node());
29 if (!obj->type)
30 obj->type = OBJ_TAG;
31 if (obj->type != OBJ_TAG) {
32 error("Object %s is a %s, not a tag",
33 sha1_to_hex(sha1), typename(obj->type));
34 return NULL;
36 return (struct tag *) obj;
39 static unsigned long parse_tag_date(const char *buf, const char *tail)
41 const char *dateptr;
43 while (buf < tail && *buf++ != '>')
44 /* nada */;
45 if (buf >= tail)
46 return 0;
47 dateptr = buf;
48 while (buf < tail && *buf++ != '\n')
49 /* nada */;
50 if (buf >= tail)
51 return 0;
52 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
53 return strtoul(dateptr, NULL, 10);
56 int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
58 unsigned char sha1[20];
59 char type[20];
60 const char *bufptr = data;
61 const char *tail = bufptr + size;
62 const char *nl;
64 if (item->object.parsed)
65 return 0;
66 item->object.parsed = 1;
68 if (size < 64)
69 return -1;
70 if (memcmp("object ", bufptr, 7) || get_sha1_hex(bufptr + 7, sha1) || bufptr[47] != '\n')
71 return -1;
72 bufptr += 48; /* "object " + sha1 + "\n" */
74 if (prefixcmp(bufptr, "type "))
75 return -1;
76 bufptr += 5;
77 nl = memchr(bufptr, '\n', tail - bufptr);
78 if (!nl || sizeof(type) <= (nl - bufptr))
79 return -1;
80 strncpy(type, bufptr, nl - bufptr);
81 type[nl - bufptr] = '\0';
82 bufptr = nl + 1;
84 if (!strcmp(type, blob_type)) {
85 item->tagged = &lookup_blob(sha1)->object;
86 } else if (!strcmp(type, tree_type)) {
87 item->tagged = &lookup_tree(sha1)->object;
88 } else if (!strcmp(type, commit_type)) {
89 item->tagged = &lookup_commit(sha1)->object;
90 } else if (!strcmp(type, tag_type)) {
91 item->tagged = &lookup_tag(sha1)->object;
92 } else {
93 error("Unknown type %s", type);
94 item->tagged = NULL;
97 if (prefixcmp(bufptr, "tag "))
98 return -1;
99 bufptr += 4;
100 nl = memchr(bufptr, '\n', tail - bufptr);
101 if (!nl)
102 return -1;
103 item->tag = xmemdupz(bufptr, nl - bufptr);
104 bufptr = nl + 1;
106 if (!prefixcmp(bufptr, "tagger "))
107 item->date = parse_tag_date(bufptr, tail);
108 else
109 item->date = 0;
111 return 0;
114 int parse_tag(struct tag *item)
116 enum object_type type;
117 void *data;
118 unsigned long size;
119 int ret;
121 if (item->object.parsed)
122 return 0;
123 data = read_sha1_file(item->object.sha1, &type, &size);
124 if (!data)
125 return error("Could not read %s",
126 sha1_to_hex(item->object.sha1));
127 if (type != OBJ_TAG) {
128 free(data);
129 return error("Object %s not a tag",
130 sha1_to_hex(item->object.sha1));
132 ret = parse_tag_buffer(item, data, size);
133 free(data);
134 return ret;