commit-graph tests: fix unportable "dd" invocation
[git.git] / builtin / mktag.c
blob6fb7dc8578d68526c64179d7fc43aaa5a72445f6
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(repl, buffer, size, expected_type);
34 free(buffer);
36 return ret;
39 static int verify_tag(char *buffer, unsigned long size)
41 int typelen;
42 char type[20];
43 struct object_id oid;
44 const char *object, *type_line, *tag_line, *tagger_line, *lb, *rb, *p;
45 size_t len;
47 if (size < 84)
48 return error("wanna fool me ? you obviously got the size wrong !");
50 buffer[size] = 0;
52 /* Verify object line */
53 object = buffer;
54 if (memcmp(object, "object ", 7))
55 return error("char%d: does not start with \"object \"", 0);
57 if (parse_oid_hex(object + 7, &oid, &p))
58 return error("char%d: could not get SHA1 hash", 7);
60 /* Verify type line */
61 type_line = p + 1;
62 if (memcmp(type_line - 1, "\ntype ", 6))
63 return error("char%d: could not find \"\\ntype \"", 47);
65 /* Verify tag-line */
66 tag_line = strchr(type_line, '\n');
67 if (!tag_line)
68 return error("char%"PRIuMAX": could not find next \"\\n\"",
69 (uintmax_t) (type_line - buffer));
70 tag_line++;
71 if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n')
72 return error("char%"PRIuMAX": no \"tag \" found",
73 (uintmax_t) (tag_line - buffer));
75 /* Get the actual type */
76 typelen = tag_line - type_line - strlen("type \n");
77 if (typelen >= sizeof(type))
78 return error("char%"PRIuMAX": type too long",
79 (uintmax_t) (type_line+5 - buffer));
81 memcpy(type, type_line+5, typelen);
82 type[typelen] = 0;
84 /* Verify that the object matches */
85 if (verify_object(&oid, type))
86 return error("char%d: could not verify object %s", 7, oid_to_hex(&oid));
88 /* Verify the tag-name: we don't allow control characters or spaces in it */
89 tag_line += 4;
90 for (;;) {
91 unsigned char c = *tag_line++;
92 if (c == '\n')
93 break;
94 if (c > ' ')
95 continue;
96 return error("char%"PRIuMAX": could not verify tag name",
97 (uintmax_t) (tag_line - buffer));
100 /* Verify the tagger line */
101 tagger_line = tag_line;
103 if (memcmp(tagger_line, "tagger ", 7))
104 return error("char%"PRIuMAX": could not find \"tagger \"",
105 (uintmax_t) (tagger_line - buffer));
108 * Check for correct form for name and email
109 * i.e. " <" followed by "> " on _this_ line
110 * No angle brackets within the name or email address fields.
111 * No spaces within the email address field.
113 tagger_line += 7;
114 if (!(lb = strstr(tagger_line, " <")) || !(rb = strstr(lb+2, "> ")) ||
115 strpbrk(tagger_line, "<>\n") != lb+1 ||
116 strpbrk(lb+2, "><\n ") != rb)
117 return error("char%"PRIuMAX": malformed tagger field",
118 (uintmax_t) (tagger_line - buffer));
120 /* Check for author name, at least one character, space is acceptable */
121 if (lb == tagger_line)
122 return error("char%"PRIuMAX": missing tagger name",
123 (uintmax_t) (tagger_line - buffer));
125 /* timestamp, 1 or more digits followed by space */
126 tagger_line = rb + 2;
127 if (!(len = strspn(tagger_line, "0123456789")))
128 return error("char%"PRIuMAX": missing tag timestamp",
129 (uintmax_t) (tagger_line - buffer));
130 tagger_line += len;
131 if (*tagger_line != ' ')
132 return error("char%"PRIuMAX": malformed tag timestamp",
133 (uintmax_t) (tagger_line - buffer));
134 tagger_line++;
136 /* timezone, 5 digits [+-]hhmm, max. 1400 */
137 if (!((tagger_line[0] == '+' || tagger_line[0] == '-') &&
138 strspn(tagger_line+1, "0123456789") == 4 &&
139 tagger_line[5] == '\n' && atoi(tagger_line+1) <= 1400))
140 return error("char%"PRIuMAX": malformed tag timezone",
141 (uintmax_t) (tagger_line - buffer));
142 tagger_line += 6;
144 /* Verify the blank line separating the header from the body */
145 if (*tagger_line != '\n')
146 return error("char%"PRIuMAX": trailing garbage in tag header",
147 (uintmax_t) (tagger_line - buffer));
149 /* The actual stuff afterwards we don't care about.. */
150 return 0;
153 int cmd_mktag(int argc, const char **argv, const char *prefix)
155 struct strbuf buf = STRBUF_INIT;
156 struct object_id result;
158 if (argc != 1)
159 usage("git mktag");
161 if (strbuf_read(&buf, 0, 4096) < 0) {
162 die_errno("could not read from stdin");
165 /* Verify it for some basic sanity: it needs to start with
166 "object <sha1>\ntype\ntagger " */
167 if (verify_tag(buf.buf, buf.len) < 0)
168 die("invalid tag signature file");
170 if (write_object_file(buf.buf, buf.len, tag_type, &result) < 0)
171 die("unable to write tag file");
173 strbuf_release(&buf);
174 printf("%s\n", oid_to_hex(&result));
175 return 0;