]> git.ipfire.org Git - thirdparty/git.git/blame - tag.c
Refactor handling of error_string in receive-pack
[thirdparty/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
DB
22{
23 struct object *obj = lookup_object(sha1);
24 if (!obj) {
855419f7 25 struct tag *ret = alloc_tag_node();
2636f614 26 created_object(sha1, &ret->object);
1974632c 27 ret->object.type = OBJ_TAG;
2636f614
DB
28 return ret;
29 }
d1af002d 30 if (!obj->type)
1974632c
LT
31 obj->type = OBJ_TAG;
32 if (obj->type != OBJ_TAG) {
885a86ab
LT
33 error("Object %s is a %s, not a tree",
34 sha1_to_hex(sha1), typename(obj->type));
2636f614
DB
35 return NULL;
36 }
37 return (struct tag *) obj;
38}
39
bd2c39f5 40int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
2636f614 41{
ae200ee5 42 int typelen, taglen;
0ab17950 43 unsigned char sha1[20];
ae200ee5 44 const char *type_line, *tag_line, *sig_line;
89e4202f 45 char type[20];
ae200ee5 46
2636f614
DB
47 if (item->object.parsed)
48 return 0;
49 item->object.parsed = 1;
2636f614 50
2636f614 51 if (size < 64)
bd2c39f5 52 return -1;
0ab17950 53 if (memcmp("object ", data, 7) || get_sha1_hex((char *) data + 7, sha1))
bd2c39f5 54 return -1;
2636f614 55
1d7f171c 56 type_line = (char *) data + 48;
2636f614 57 if (memcmp("\ntype ", type_line-1, 6))
bd2c39f5 58 return -1;
2636f614
DB
59
60 tag_line = strchr(type_line, '\n');
61 if (!tag_line || memcmp("tag ", ++tag_line, 4))
bd2c39f5 62 return -1;
2636f614
DB
63
64 sig_line = strchr(tag_line, '\n');
65 if (!sig_line)
bd2c39f5 66 return -1;
2636f614
DB
67 sig_line++;
68
69 typelen = tag_line - type_line - strlen("type \n");
70 if (typelen >= 20)
bd2c39f5 71 return -1;
89e4202f
DB
72 memcpy(type, type_line + 5, typelen);
73 type[typelen] = '\0';
2636f614
DB
74 taglen = sig_line - tag_line - strlen("tag \n");
75 item->tag = xmalloc(taglen + 1);
76 memcpy(item->tag, tag_line + 4, taglen);
77 item->tag[taglen] = '\0';
78
0ab17950
NP
79 if (!strcmp(type, blob_type)) {
80 item->tagged = &lookup_blob(sha1)->object;
81 } else if (!strcmp(type, tree_type)) {
82 item->tagged = &lookup_tree(sha1)->object;
83 } else if (!strcmp(type, commit_type)) {
84 item->tagged = &lookup_commit(sha1)->object;
85 } else if (!strcmp(type, tag_type)) {
86 item->tagged = &lookup_tag(sha1)->object;
87 } else {
88 error("Unknown type %s", type);
89 item->tagged = NULL;
90 }
91
27dedf0c
JH
92 if (item->tagged && track_object_refs) {
93 struct object_refs *refs = alloc_object_refs(1);
94 refs->ref[0] = item->tagged;
95 set_object_refs(&item->object, refs);
96 }
89e4202f 97
2636f614 98 return 0;
bd2c39f5 99}
13019d41 100
bd2c39f5
NP
101int parse_tag(struct tag *item)
102{
21666f1a 103 enum object_type type;
bd2c39f5
NP
104 void *data;
105 unsigned long size;
106 int ret;
107
108 if (item->object.parsed)
109 return 0;
21666f1a 110 data = read_sha1_file(item->object.sha1, &type, &size);
bd2c39f5
NP
111 if (!data)
112 return error("Could not read %s",
113 sha1_to_hex(item->object.sha1));
21666f1a 114 if (type != OBJ_TAG) {
bd2c39f5
NP
115 free(data);
116 return error("Object %s not a tag",
117 sha1_to_hex(item->object.sha1));
118 }
119 ret = parse_tag_buffer(item, data, size);
13019d41 120 free(data);
bd2c39f5 121 return ret;
2636f614 122}