tag.c 4.47 KB
Newer Older
1
#include "cache.h"
2
#include "tag.h"
3 4 5
#include "commit.h"
#include "tree.h"
#include "blob.h"
6
#include "gpg-interface.h"
7 8 9

const char *tag_type = "tag";

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
static int run_gpg_verify(const char *buf, unsigned long size, unsigned flags)
{
	struct signature_check sigc;
	size_t payload_size;
	int ret;

	memset(&sigc, 0, sizeof(sigc));

	payload_size = parse_signature(buf, size);

	if (size == payload_size) {
		if (flags & GPG_VERIFY_VERBOSE)
			write_in_full(1, buf, payload_size);
		return error("no signature found");
	}

	ret = check_signature(buf, payload_size, buf + payload_size,
				size - payload_size, &sigc);
28 29 30

	if (!(flags & GPG_VERIFY_OMIT_STATUS))
		print_signature_buffer(&sigc, flags);
31 32 33 34 35

	signature_check_clear(&sigc);
	return ret;
}

36
int gpg_verify_tag(const struct object_id *oid, const char *name_to_report,
37 38 39 40 41 42 43
		unsigned flags)
{
	enum object_type type;
	char *buf;
	unsigned long size;
	int ret;

44
	type = oid_object_info(the_repository, oid, NULL);
45 46 47 48
	if (type != OBJ_TAG)
		return error("%s: cannot verify a non-tag object of type %s.",
				name_to_report ?
				name_to_report :
49
				find_unique_abbrev(oid, DEFAULT_ABBREV),
50
				type_name(type));
51

52
	buf = read_object_file(oid, &type, &size);
53 54 55 56
	if (!buf)
		return error("%s: unable to read file.",
				name_to_report ?
				name_to_report :
57
				find_unique_abbrev(oid, DEFAULT_ABBREV));
58 59 60 61 62 63 64

	ret = run_gpg_verify(buf, size, flags);

	free(buf);
	return ret;
}

65
struct object *deref_tag(struct object *o, const char *warn, int warnlen)
66
{
67
	while (o && o->type == OBJ_TAG)
68
		if (((struct tag *)o)->tagged)
69
			o = parse_object(&((struct tag *)o)->tagged->oid);
70 71
		else
			o = NULL;
72 73 74 75 76
	if (!o && warn) {
		if (!warnlen)
			warnlen = strlen(warn);
		error("missing object referenced by '%.*s'", warnlen, warn);
	}
77 78 79
	return o;
}

80 81 82
struct object *deref_tag_noverify(struct object *o)
{
	while (o && o->type == OBJ_TAG) {
83
		o = parse_object(&o->oid);
84 85 86 87 88 89 90 91
		if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
			o = ((struct tag *)o)->tagged;
		else
			o = NULL;
	}
	return o;
}

92
struct tag *lookup_tag(const struct object_id *oid)
93
{
94
	struct object *obj = lookup_object(oid->hash);
95
	if (!obj)
96
		return create_object(oid->hash, alloc_tag_node());
97
	return object_as_type(obj, OBJ_TAG, 0);
98 99
}

100
static timestamp_t parse_tag_date(const char *buf, const char *tail)
101 102 103 104 105 106 107 108 109 110 111 112
{
	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;
113 114
	/* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
	return parse_timestamp(dateptr, NULL, 10);
115 116
}

117
int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
118
{
119
	struct object_id oid;
120
	char type[20];
121 122 123
	const char *bufptr = data;
	const char *tail = bufptr + size;
	const char *nl;
124

125 126 127
	if (item->object.parsed)
		return 0;
	item->object.parsed = 1;
128

129
	if (size < GIT_SHA1_HEXSZ + 24)
130
		return -1;
131
	if (memcmp("object ", bufptr, 7) || parse_oid_hex(bufptr + 7, &oid, &bufptr) || *bufptr++ != '\n')
132
		return -1;
133

134
	if (!starts_with(bufptr, "type "))
135
		return -1;
136 137 138
	bufptr += 5;
	nl = memchr(bufptr, '\n', tail - bufptr);
	if (!nl || sizeof(type) <= (nl - bufptr))
139
		return -1;
Jeff King's avatar
Jeff King committed
140
	memcpy(type, bufptr, nl - bufptr);
141 142
	type[nl - bufptr] = '\0';
	bufptr = nl + 1;
143

144
	if (!strcmp(type, blob_type)) {
145
		item->tagged = (struct object *)lookup_blob(&oid);
146
	} else if (!strcmp(type, tree_type)) {
147
		item->tagged = (struct object *)lookup_tree(&oid);
148
	} else if (!strcmp(type, commit_type)) {
149
		item->tagged = (struct object *)lookup_commit(&oid);
150
	} else if (!strcmp(type, tag_type)) {
151
		item->tagged = (struct object *)lookup_tag(&oid);
152 153 154 155 156
	} else {
		error("Unknown type %s", type);
		item->tagged = NULL;
	}

157
	if (bufptr + 4 < tail && starts_with(bufptr, "tag "))
158 159
		; 		/* good */
	else
160 161 162 163 164 165 166 167
		return -1;
	bufptr += 4;
	nl = memchr(bufptr, '\n', tail - bufptr);
	if (!nl)
		return -1;
	item->tag = xmemdupz(bufptr, nl - bufptr);
	bufptr = nl + 1;

168
	if (bufptr + 7 < tail && starts_with(bufptr, "tagger "))
169 170 171 172
		item->date = parse_tag_date(bufptr, tail);
	else
		item->date = 0;

173
	return 0;
174
}
175

176 177
int parse_tag(struct tag *item)
{
178
	enum object_type type;
179 180 181 182 183 184
	void *data;
	unsigned long size;
	int ret;

	if (item->object.parsed)
		return 0;
185
	data = read_object_file(&item->object.oid, &type, &size);
186 187
	if (!data)
		return error("Could not read %s",
188
			     oid_to_hex(&item->object.oid));
189
	if (type != OBJ_TAG) {
190 191
		free(data);
		return error("Object %s not a tag",
192
			     oid_to_hex(&item->object.oid));
193 194
	}
	ret = parse_tag_buffer(item, data, size);
195
	free(data);
196
	return ret;
197
}