Merge branch 'mm/detach-at-HEAD-reflog' into maint
[git/debian.git] / tag.c
blob5b0ac62ed846188ad44ceac41d278ff37ec8bd9d
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 object *deref_tag_noverify(struct object *o)
26 while (o && o->type == OBJ_TAG) {
27 o = parse_object(o->sha1);
28 if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
29 o = ((struct tag *)o)->tagged;
30 else
31 o = NULL;
33 return o;
36 struct tag *lookup_tag(const unsigned char *sha1)
38 struct object *obj = lookup_object(sha1);
39 if (!obj)
40 return create_object(sha1, alloc_tag_node());
41 return object_as_type(obj, OBJ_TAG, 0);
44 static unsigned long parse_tag_date(const char *buf, const char *tail)
46 const char *dateptr;
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);
61 int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
63 unsigned char sha1[20];
64 char type[20];
65 const char *bufptr = data;
66 const char *tail = bufptr + size;
67 const char *nl;
69 if (item->object.parsed)
70 return 0;
71 item->object.parsed = 1;
73 if (size < 64)
74 return -1;
75 if (memcmp("object ", bufptr, 7) || get_sha1_hex(bufptr + 7, sha1) || bufptr[47] != '\n')
76 return -1;
77 bufptr += 48; /* "object " + sha1 + "\n" */
79 if (!starts_with(bufptr, "type "))
80 return -1;
81 bufptr += 5;
82 nl = memchr(bufptr, '\n', tail - bufptr);
83 if (!nl || sizeof(type) <= (nl - bufptr))
84 return -1;
85 strncpy(type, bufptr, nl - bufptr);
86 type[nl - bufptr] = '\0';
87 bufptr = nl + 1;
89 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;
102 if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
103 ; /* good */
104 else
105 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;
113 if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
114 item->date = parse_tag_date(bufptr, tail);
115 else
116 item->date = 0;
118 return 0;
121 int parse_tag(struct tag *item)
123 enum object_type type;
124 void *data;
125 unsigned long size;
126 int ret;
128 if (item->object.parsed)
129 return 0;
130 data = read_sha1_file(item->object.sha1, &type, &size);
131 if (!data)
132 return error("Could not read %s",
133 sha1_to_hex(item->object.sha1));
134 if (type != OBJ_TAG) {
135 free(data);
136 return error("Object %s not a tag",
137 sha1_to_hex(item->object.sha1));
139 ret = parse_tag_buffer(item, data, size);
140 free(data);
141 return ret;