tag.c 3.19 KB
Newer Older
1
#include "cache.h"
2
#include "tag.h"
3 4 5
#include "commit.h"
#include "tree.h"
#include "blob.h"
6 7 8

const char *tag_type = "tag";

9
struct object *deref_tag(struct object *o, const char *warn, int warnlen)
10
{
11
	while (o && o->type == OBJ_TAG)
12 13 14 15
		if (((struct tag *)o)->tagged)
			o = parse_object(((struct tag *)o)->tagged->sha1);
		else
			o = NULL;
16 17 18 19 20
	if (!o && warn) {
		if (!warnlen)
			warnlen = strlen(warn);
		error("missing object referenced by '%.*s'", warnlen, warn);
	}
21 22 23
	return o;
}

24 25 26 27 28 29 30 31 32 33 34 35
struct object *deref_tag_noverify(struct object *o)
{
	while (o && o->type == OBJ_TAG) {
		o = parse_object(o->sha1);
		if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
			o = ((struct tag *)o)->tagged;
		else
			o = NULL;
	}
	return o;
}

36
struct tag *lookup_tag(const unsigned char *sha1)
37
{
38 39
	struct object *obj = lookup_object(sha1);
	if (!obj)
40
		return create_object(sha1, alloc_tag_node());
41
	return object_as_type(obj, OBJ_TAG, 0);
42 43
}

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
static unsigned long parse_tag_date(const char *buf, const char *tail)
{
	const char *dateptr;

	while (buf < tail && *buf++ != '>')
		/* nada */;
	if (buf >= tail)
		return 0;
	dateptr = buf;
	while (buf < tail && *buf++ != '\n')
		/* nada */;
	if (buf >= tail)
		return 0;
	/* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
	return strtoul(dateptr, NULL, 10);
}

61
int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
62
{
63
	unsigned char sha1[20];
64
	char type[20];
65 66 67
	const char *bufptr = data;
	const char *tail = bufptr + size;
	const char *nl;
68

Shawn O. Pearce's avatar
Shawn O. Pearce committed
69 70 71
	if (item->object.parsed)
		return 0;
	item->object.parsed = 1;
72 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" */
78

79
	if (!starts_with(bufptr, "type "))
80
		return -1;
81 82 83
	bufptr += 5;
	nl = memchr(bufptr, '\n', tail - bufptr);
	if (!nl || sizeof(type) <= (nl - bufptr))
84
		return -1;
85 86 87
	strncpy(type, bufptr, nl - bufptr);
	type[nl - bufptr] = '\0';
	bufptr = nl + 1;
88

89 90 91 92 93 94 95 96 97 98 99 100 101
	if (!strcmp(type, blob_type)) {
		item->tagged = &lookup_blob(sha1)->object;
	} else if (!strcmp(type, tree_type)) {
		item->tagged = &lookup_tree(sha1)->object;
	} else if (!strcmp(type, commit_type)) {
		item->tagged = &lookup_commit(sha1)->object;
	} else if (!strcmp(type, tag_type)) {
		item->tagged = &lookup_tag(sha1)->object;
	} else {
		error("Unknown type %s", type);
		item->tagged = NULL;
	}

102
	if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
103 104
		; 		/* good */
	else
105 106 107 108 109 110 111 112
		return -1;
	bufptr += 4;
	nl = memchr(bufptr, '\n', tail - bufptr);
	if (!nl)
		return -1;
	item->tag = xmemdupz(bufptr, nl - bufptr);
	bufptr = nl + 1;

113
	if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
114 115 116 117
		item->date = parse_tag_date(bufptr, tail);
	else
		item->date = 0;

118
	return 0;
119
}
120

121 122
int parse_tag(struct tag *item)
{
123
	enum object_type type;
124 125 126 127 128 129
	void *data;
	unsigned long size;
	int ret;

	if (item->object.parsed)
		return 0;
130
	data = read_sha1_file(item->object.sha1, &type, &size);
131 132 133
	if (!data)
		return error("Could not read %s",
			     sha1_to_hex(item->object.sha1));
134
	if (type != OBJ_TAG) {
135 136 137 138 139
		free(data);
		return error("Object %s not a tag",
			     sha1_to_hex(item->object.sha1));
	}
	ret = parse_tag_buffer(item, data, size);
140
	free(data);
141
	return ret;
142
}