prune: do not show error from pack-redundant when no packs are found.
[git/git.git] / tag.c
CommitLineData
2636f614
DB
1#include "tag.h"
2#include "cache.h"
3
4const char *tag_type = "tag";
5
9534f40b 6struct object *deref_tag(struct object *o, const char *warn, int warnlen)
37fde874
JH
7{
8 while (o && o->type == tag_type)
9 o = parse_object(((struct tag *)o)->tagged->sha1);
9534f40b
JH
10 if (!o && warn) {
11 if (!warnlen)
12 warnlen = strlen(warn);
13 error("missing object referenced by '%.*s'", warnlen, warn);
14 }
37fde874
JH
15 return o;
16}
17
5d6ccf5c 18struct tag *lookup_tag(const unsigned char *sha1)
2636f614
DB
19{
20 struct object *obj = lookup_object(sha1);
21 if (!obj) {
22 struct tag *ret = xmalloc(sizeof(struct tag));
23 memset(ret, 0, sizeof(struct tag));
24 created_object(sha1, &ret->object);
25 ret->object.type = tag_type;
26 return ret;
27 }
d1af002d
NP
28 if (!obj->type)
29 obj->type = tag_type;
2636f614
DB
30 if (obj->type != tag_type) {
31 error("Object %s is a %s, not a tree",
32 sha1_to_hex(sha1), obj->type);
33 return NULL;
34 }
35 return (struct tag *) obj;
36}
37
bd2c39f5 38int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
2636f614 39{
ae200ee5
ET
40 int typelen, taglen;
41 unsigned char object[20];
42 const char *type_line, *tag_line, *sig_line;
89e4202f 43 char type[20];
ae200ee5 44
2636f614
DB
45 if (item->object.parsed)
46 return 0;
47 item->object.parsed = 1;
2636f614 48
2636f614 49 if (size < 64)
bd2c39f5 50 return -1;
2636f614 51 if (memcmp("object ", data, 7) || get_sha1_hex(data + 7, object))
bd2c39f5 52 return -1;
2636f614 53
2636f614
DB
54 type_line = data + 48;
55 if (memcmp("\ntype ", type_line-1, 6))
bd2c39f5 56 return -1;
2636f614
DB
57
58 tag_line = strchr(type_line, '\n');
59 if (!tag_line || memcmp("tag ", ++tag_line, 4))
bd2c39f5 60 return -1;
2636f614
DB
61
62 sig_line = strchr(tag_line, '\n');
63 if (!sig_line)
bd2c39f5 64 return -1;
2636f614
DB
65 sig_line++;
66
67 typelen = tag_line - type_line - strlen("type \n");
68 if (typelen >= 20)
bd2c39f5 69 return -1;
89e4202f
DB
70 memcpy(type, type_line + 5, typelen);
71 type[typelen] = '\0';
2636f614
DB
72 taglen = sig_line - tag_line - strlen("tag \n");
73 item->tag = xmalloc(taglen + 1);
74 memcpy(item->tag, tag_line + 4, taglen);
75 item->tag[taglen] = '\0';
76
89e4202f 77 item->tagged = lookup_object_type(object, type);
27dedf0c
JH
78 if (item->tagged && track_object_refs) {
79 struct object_refs *refs = alloc_object_refs(1);
80 refs->ref[0] = item->tagged;
81 set_object_refs(&item->object, refs);
82 }
89e4202f 83
2636f614 84 return 0;
bd2c39f5 85}
13019d41 86
bd2c39f5
NP
87int parse_tag(struct tag *item)
88{
89 char type[20];
90 void *data;
91 unsigned long size;
92 int ret;
93
94 if (item->object.parsed)
95 return 0;
96 data = read_sha1_file(item->object.sha1, type, &size);
97 if (!data)
98 return error("Could not read %s",
99 sha1_to_hex(item->object.sha1));
100 if (strcmp(type, tag_type)) {
101 free(data);
102 return error("Object %s not a tag",
103 sha1_to_hex(item->object.sha1));
104 }
105 ret = parse_tag_buffer(item, data, size);
13019d41 106 free(data);
bd2c39f5 107 return ret;
2636f614 108}