pager: set LV=-c alongside LESS=FRSX
[alt-git.git] / tag.c
blob78d272b863f22285048cf54b9dcb03f80cb36f00
1 #include "cache.h"
2 #include "tag.h"
3 #include "commit.h"
4 #include "tree.h"
5 #include "blob.h"
7 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
8 #define PGP_MESSAGE "-----BEGIN PGP MESSAGE-----"
10 const char *tag_type = "tag";
12 struct object *deref_tag(struct object *o, const char *warn, int warnlen)
14 while (o && o->type == OBJ_TAG)
15 if (((struct tag *)o)->tagged)
16 o = parse_object(((struct tag *)o)->tagged->sha1);
17 else
18 o = NULL;
19 if (!o && warn) {
20 if (!warnlen)
21 warnlen = strlen(warn);
22 error("missing object referenced by '%.*s'", warnlen, warn);
24 return o;
27 struct object *deref_tag_noverify(struct object *o)
29 while (o && o->type == OBJ_TAG) {
30 o = parse_object(o->sha1);
31 if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
32 o = ((struct tag *)o)->tagged;
33 else
34 o = NULL;
36 return o;
39 struct tag *lookup_tag(const unsigned char *sha1)
41 struct object *obj = lookup_object(sha1);
42 if (!obj)
43 return create_object(sha1, OBJ_TAG, alloc_tag_node());
44 if (!obj->type)
45 obj->type = OBJ_TAG;
46 if (obj->type != OBJ_TAG) {
47 error("Object %s is a %s, not a tag",
48 sha1_to_hex(sha1), typename(obj->type));
49 return NULL;
51 return (struct tag *) obj;
54 static unsigned long parse_tag_date(const char *buf, const char *tail)
56 const char *dateptr;
58 while (buf < tail && *buf++ != '>')
59 /* nada */;
60 if (buf >= tail)
61 return 0;
62 dateptr = buf;
63 while (buf < tail && *buf++ != '\n')
64 /* nada */;
65 if (buf >= tail)
66 return 0;
67 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
68 return strtoul(dateptr, NULL, 10);
71 int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
73 unsigned char sha1[20];
74 char type[20];
75 const char *bufptr = data;
76 const char *tail = bufptr + size;
77 const char *nl;
79 if (item->object.parsed)
80 return 0;
81 item->object.parsed = 1;
83 if (size < 64)
84 return -1;
85 if (memcmp("object ", bufptr, 7) || get_sha1_hex(bufptr + 7, sha1) || bufptr[47] != '\n')
86 return -1;
87 bufptr += 48; /* "object " + sha1 + "\n" */
89 if (prefixcmp(bufptr, "type "))
90 return -1;
91 bufptr += 5;
92 nl = memchr(bufptr, '\n', tail - bufptr);
93 if (!nl || sizeof(type) <= (nl - bufptr))
94 return -1;
95 strncpy(type, bufptr, nl - bufptr);
96 type[nl - bufptr] = '\0';
97 bufptr = nl + 1;
99 if (!strcmp(type, blob_type)) {
100 item->tagged = &lookup_blob(sha1)->object;
101 } else if (!strcmp(type, tree_type)) {
102 item->tagged = &lookup_tree(sha1)->object;
103 } else if (!strcmp(type, commit_type)) {
104 item->tagged = &lookup_commit(sha1)->object;
105 } else if (!strcmp(type, tag_type)) {
106 item->tagged = &lookup_tag(sha1)->object;
107 } else {
108 error("Unknown type %s", type);
109 item->tagged = NULL;
112 if (bufptr + 4 < tail && !prefixcmp(bufptr, "tag "))
113 ; /* good */
114 else
115 return -1;
116 bufptr += 4;
117 nl = memchr(bufptr, '\n', tail - bufptr);
118 if (!nl)
119 return -1;
120 item->tag = xmemdupz(bufptr, nl - bufptr);
121 bufptr = nl + 1;
123 if (bufptr + 7 < tail && !prefixcmp(bufptr, "tagger "))
124 item->date = parse_tag_date(bufptr, tail);
125 else
126 item->date = 0;
128 return 0;
131 int parse_tag(struct tag *item)
133 enum object_type type;
134 void *data;
135 unsigned long size;
136 int ret;
138 if (item->object.parsed)
139 return 0;
140 data = read_sha1_file(item->object.sha1, &type, &size);
141 if (!data)
142 return error("Could not read %s",
143 sha1_to_hex(item->object.sha1));
144 if (type != OBJ_TAG) {
145 free(data);
146 return error("Object %s not a tag",
147 sha1_to_hex(item->object.sha1));
149 ret = parse_tag_buffer(item, data, size);
150 free(data);
151 return ret;
155 * Look at a signed tag object, and return the offset where
156 * the embedded detached signature begins, or the end of the
157 * data when there is no such signature.
159 size_t parse_signature(const char *buf, unsigned long size)
161 char *eol;
162 size_t len = 0;
163 while (len < size && prefixcmp(buf + len, PGP_SIGNATURE) &&
164 prefixcmp(buf + len, PGP_MESSAGE)) {
165 eol = memchr(buf + len, '\n', size - len);
166 len += eol ? eol - (buf + len) + 1 : size - len;
168 return len;