bisect: stop referring to sha1_array
[git/raj.git] / builtin / mktag.c
blob4982d3a93efb4b5fce3a2cb5ebc3199238f65fb0
1 #include "builtin.h"
2 #include "tag.h"
3 #include "replace-object.h"
4 #include "object-store.h"
6 /*
7 * A signature file has a very simple fixed format: four lines
8 * of "object <sha1>" + "type <typename>" + "tag <tagname>" +
9 * "tagger <committer>", followed by a blank line, a free-form tag
10 * message and a signature block that git itself doesn't care about,
11 * but that can be verified with gpg or similar.
13 * The first four lines are guaranteed to be at least 83 bytes:
14 * "object <sha1>\n" is 48 bytes, "type tag\n" at 9 bytes is the
15 * shortest possible type-line, "tag .\n" at 6 bytes is the shortest
16 * single-character-tag line, and "tagger . <> 0 +0000\n" at 20 bytes is
17 * the shortest possible tagger-line.
21 * We refuse to tag something we can't verify. Just because.
23 static int verify_object(const struct object_id *oid, const char *expected_type)
25 int ret = -1;
26 enum object_type type;
27 unsigned long size;
28 void *buffer = read_object_file(oid, &type, &size);
29 const struct object_id *repl = lookup_replace_object(the_repository, oid);
31 if (buffer) {
32 if (type == type_from_string(expected_type)) {
33 ret = check_object_signature(the_repository, repl,
34 buffer, size,
35 expected_type);
37 free(buffer);
39 return ret;
42 static int verify_tag(char *buffer, unsigned long size)
44 int typelen;
45 char type[20];
46 struct object_id oid;
47 const char *object, *type_line, *tag_line, *tagger_line, *lb, *rb, *p;
48 size_t len;
50 if (size < 84)
51 return error("wanna fool me ? you obviously got the size wrong !");
53 buffer[size] = 0;
55 /* Verify object line */
56 object = buffer;
57 if (memcmp(object, "object ", 7))
58 return error("char%d: does not start with \"object \"", 0);
60 if (parse_oid_hex(object + 7, &oid, &p))
61 return error("char%d: could not get SHA1 hash", 7);
63 /* Verify type line */
64 type_line = p + 1;
65 if (memcmp(type_line - 1, "\ntype ", 6))
66 return error("char%d: could not find \"\\ntype \"", 47);
68 /* Verify tag-line */
69 tag_line = strchr(type_line, '\n');
70 if (!tag_line)
71 return error("char%"PRIuMAX": could not find next \"\\n\"",
72 (uintmax_t) (type_line - buffer));
73 tag_line++;
74 if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n')
75 return error("char%"PRIuMAX": no \"tag \" found",
76 (uintmax_t) (tag_line - buffer));
78 /* Get the actual type */
79 typelen = tag_line - type_line - strlen("type \n");
80 if (typelen >= sizeof(type))
81 return error("char%"PRIuMAX": type too long",
82 (uintmax_t) (type_line+5 - buffer));
84 memcpy(type, type_line+5, typelen);
85 type[typelen] = 0;
87 /* Verify that the object matches */
88 if (verify_object(&oid, type))
89 return error("char%d: could not verify object %s", 7, oid_to_hex(&oid));
91 /* Verify the tag-name: we don't allow control characters or spaces in it */
92 tag_line += 4;
93 for (;;) {
94 unsigned char c = *tag_line++;
95 if (c == '\n')
96 break;
97 if (c > ' ')
98 continue;
99 return error("char%"PRIuMAX": could not verify tag name",
100 (uintmax_t) (tag_line - buffer));
103 /* Verify the tagger line */
104 tagger_line = tag_line;
106 if (memcmp(tagger_line, "tagger ", 7))
107 return error("char%"PRIuMAX": could not find \"tagger \"",
108 (uintmax_t) (tagger_line - buffer));
111 * Check for correct form for name and email
112 * i.e. " <" followed by "> " on _this_ line
113 * No angle brackets within the name or email address fields.
114 * No spaces within the email address field.
116 tagger_line += 7;
117 if (!(lb = strstr(tagger_line, " <")) || !(rb = strstr(lb+2, "> ")) ||
118 strpbrk(tagger_line, "<>\n") != lb+1 ||
119 strpbrk(lb+2, "><\n ") != rb)
120 return error("char%"PRIuMAX": malformed tagger field",
121 (uintmax_t) (tagger_line - buffer));
123 /* Check for author name, at least one character, space is acceptable */
124 if (lb == tagger_line)
125 return error("char%"PRIuMAX": missing tagger name",
126 (uintmax_t) (tagger_line - buffer));
128 /* timestamp, 1 or more digits followed by space */
129 tagger_line = rb + 2;
130 if (!(len = strspn(tagger_line, "0123456789")))
131 return error("char%"PRIuMAX": missing tag timestamp",
132 (uintmax_t) (tagger_line - buffer));
133 tagger_line += len;
134 if (*tagger_line != ' ')
135 return error("char%"PRIuMAX": malformed tag timestamp",
136 (uintmax_t) (tagger_line - buffer));
137 tagger_line++;
139 /* timezone, 5 digits [+-]hhmm, max. 1400 */
140 if (!((tagger_line[0] == '+' || tagger_line[0] == '-') &&
141 strspn(tagger_line+1, "0123456789") == 4 &&
142 tagger_line[5] == '\n' && atoi(tagger_line+1) <= 1400))
143 return error("char%"PRIuMAX": malformed tag timezone",
144 (uintmax_t) (tagger_line - buffer));
145 tagger_line += 6;
147 /* Verify the blank line separating the header from the body */
148 if (*tagger_line != '\n')
149 return error("char%"PRIuMAX": trailing garbage in tag header",
150 (uintmax_t) (tagger_line - buffer));
152 /* The actual stuff afterwards we don't care about.. */
153 return 0;
156 int cmd_mktag(int argc, const char **argv, const char *prefix)
158 struct strbuf buf = STRBUF_INIT;
159 struct object_id result;
161 if (argc != 1)
162 usage("git mktag");
164 if (strbuf_read(&buf, 0, 4096) < 0) {
165 die_errno("could not read from stdin");
168 /* Verify it for some basic sanity: it needs to start with
169 "object <sha1>\ntype\ntagger " */
170 if (verify_tag(buf.buf, buf.len) < 0)
171 die("invalid tag signature file");
173 if (write_object_file(buf.buf, buf.len, tag_type, &result) < 0)
174 die("unable to write tag file");
176 strbuf_release(&buf);
177 printf("%s\n", oid_to_hex(&result));
178 return 0;