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
);
18 warnlen
= strlen(warn
);
19 error("missing object referenced by '%.*s'", warnlen
, warn
);
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
;
36 struct tag
*lookup_tag(const unsigned char *sha1
)
38 struct object
*obj
= lookup_object(sha1
);
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
)
48 while (buf
< tail
&& *buf
++ != '>')
53 while (buf
< tail
&& *buf
++ != '\n')
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];
65 const char *bufptr
= data
;
66 const char *tail
= bufptr
+ size
;
69 if (item
->object
.parsed
)
71 item
->object
.parsed
= 1;
75 if (memcmp("object ", bufptr
, 7) || get_sha1_hex(bufptr
+ 7, sha1
) || bufptr
[47] != '\n')
77 bufptr
+= 48; /* "object " + sha1 + "\n" */
79 if (!starts_with(bufptr
, "type "))
82 nl
= memchr(bufptr
, '\n', tail
- bufptr
);
83 if (!nl
|| sizeof(type
) <= (nl
- bufptr
))
85 strncpy(type
, bufptr
, nl
- bufptr
);
86 type
[nl
- bufptr
] = '\0';
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
;
98 error("Unknown type %s", type
);
102 if (bufptr
+ 4 < tail
&& starts_with(bufptr
, "tag "))
107 nl
= memchr(bufptr
, '\n', tail
- bufptr
);
110 item
->tag
= xmemdupz(bufptr
, nl
- bufptr
);
113 if (bufptr
+ 7 < tail
&& starts_with(bufptr
, "tagger "))
114 item
->date
= parse_tag_date(bufptr
, tail
);
121 int parse_tag(struct tag
*item
)
123 enum object_type type
;
128 if (item
->object
.parsed
)
130 data
= read_sha1_file(item
->object
.sha1
, &type
, &size
);
132 return error("Could not read %s",
133 sha1_to_hex(item
->object
.sha1
));
134 if (type
!= OBJ_TAG
) {
136 return error("Object %s not a tag",
137 sha1_to_hex(item
->object
.sha1
));
139 ret
= parse_tag_buffer(item
, data
, size
);