deref_tag: handle return value NULL
[git/git.git] / tag.c
CommitLineData
2636f614 1#include "cache.h"
8f1d2e6f 2#include "tag.h"
0ab17950
NP
3#include "commit.h"
4#include "tree.h"
5#include "blob.h"
2636f614
DB
6
7const char *tag_type = "tag";
8
9534f40b 9struct object *deref_tag(struct object *o, const char *warn, int warnlen)
37fde874 10{
1974632c 11 while (o && o->type == OBJ_TAG)
37fde874 12 o = parse_object(((struct tag *)o)->tagged->sha1);
9534f40b
JH
13 if (!o && warn) {
14 if (!warnlen)
15 warnlen = strlen(warn);
16 error("missing object referenced by '%.*s'", warnlen, warn);
17 }
37fde874
JH
18 return o;
19}
20
5d6ccf5c 21struct tag *lookup_tag(const unsigned char *sha1)
2636f614 22{
100c5f3b
LT
23 struct object *obj = lookup_object(sha1);
24 if (!obj)
25 return create_object(sha1, OBJ_TAG, alloc_tag_node());
d1af002d 26 if (!obj->type)
1974632c
LT
27 obj->type = OBJ_TAG;
28 if (obj->type != OBJ_TAG) {
eb09626b 29 error("Object %s is a %s, not a tag",
885a86ab 30 sha1_to_hex(sha1), typename(obj->type));
2636f614
DB
31 return NULL;
32 }
33 return (struct tag *) obj;
34}
35
bd2c39f5 36int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
2636f614 37{
ae200ee5 38 int typelen, taglen;
0ab17950 39 unsigned char sha1[20];
ae200ee5 40 const char *type_line, *tag_line, *sig_line;
89e4202f 41 char type[20];
a0393ef6 42 const char *start = data;
ae200ee5 43
2636f614
DB
44 if (item->object.parsed)
45 return 0;
46 item->object.parsed = 1;
2636f614 47
2636f614 48 if (size < 64)
bd2c39f5 49 return -1;
0ab17950 50 if (memcmp("object ", data, 7) || get_sha1_hex((char *) data + 7, sha1))
bd2c39f5 51 return -1;
2636f614 52
1d7f171c 53 type_line = (char *) data + 48;
2636f614 54 if (memcmp("\ntype ", type_line-1, 6))
bd2c39f5 55 return -1;
2636f614 56
a0393ef6 57 tag_line = memchr(type_line, '\n', size - (type_line - start));
2636f614 58 if (!tag_line || memcmp("tag ", ++tag_line, 4))
bd2c39f5 59 return -1;
2636f614 60
a0393ef6 61 sig_line = memchr(tag_line, '\n', size - (tag_line - start));
2636f614 62 if (!sig_line)
bd2c39f5 63 return -1;
2636f614
DB
64 sig_line++;
65
66 typelen = tag_line - type_line - strlen("type \n");
67 if (typelen >= 20)
bd2c39f5 68 return -1;
89e4202f
DB
69 memcpy(type, type_line + 5, typelen);
70 type[typelen] = '\0';
2636f614 71 taglen = sig_line - tag_line - strlen("tag \n");
182af834 72 item->tag = xmemdupz(tag_line + 4, taglen);
2636f614 73
0ab17950
NP
74 if (!strcmp(type, blob_type)) {
75 item->tagged = &lookup_blob(sha1)->object;
76 } else if (!strcmp(type, tree_type)) {
77 item->tagged = &lookup_tree(sha1)->object;
78 } else if (!strcmp(type, commit_type)) {
79 item->tagged = &lookup_commit(sha1)->object;
80 } else if (!strcmp(type, tag_type)) {
81 item->tagged = &lookup_tag(sha1)->object;
82 } else {
83 error("Unknown type %s", type);
84 item->tagged = NULL;
85 }
86
27dedf0c
JH
87 if (item->tagged && track_object_refs) {
88 struct object_refs *refs = alloc_object_refs(1);
89 refs->ref[0] = item->tagged;
90 set_object_refs(&item->object, refs);
91 }
89e4202f 92
2636f614 93 return 0;
bd2c39f5 94}
13019d41 95
bd2c39f5
NP
96int parse_tag(struct tag *item)
97{
21666f1a 98 enum object_type type;
bd2c39f5
NP
99 void *data;
100 unsigned long size;
101 int ret;
102
103 if (item->object.parsed)
104 return 0;
21666f1a 105 data = read_sha1_file(item->object.sha1, &type, &size);
bd2c39f5
NP
106 if (!data)
107 return error("Could not read %s",
108 sha1_to_hex(item->object.sha1));
21666f1a 109 if (type != OBJ_TAG) {
bd2c39f5
NP
110 free(data);
111 return error("Object %s not a tag",
112 sha1_to_hex(item->object.sha1));
113 }
114 ret = parse_tag_buffer(item, data, size);
13019d41 115 free(data);
bd2c39f5 116 return ret;
2636f614 117}