bisect: rework some rev related functions to make them more reusable
[git/dscho.git] / mktag.c
blob99a356e9ee75cb247d80ed6dc0b251ceb0bd9e46
1 #include "cache.h"
2 #include "tag.h"
3 #include "exec_cmd.h"
5 /*
6 * A signature file has a very simple fixed format: four lines
7 * of "object <sha1>" + "type <typename>" + "tag <tagname>" +
8 * "tagger <committer>", followed by a blank line, a free-form tag
9 * message and a signature block that git itself doesn't care about,
10 * but that can be verified with gpg or similar.
12 * The first four lines are guaranteed to be at least 83 bytes:
13 * "object <sha1>\n" is 48 bytes, "type tag\n" at 9 bytes is the
14 * shortest possible type-line, "tag .\n" at 6 bytes is the shortest
15 * single-character-tag line, and "tagger . <> 0 +0000\n" at 20 bytes is
16 * the shortest possible tagger-line.
20 * We refuse to tag something we can't verify. Just because.
22 static int verify_object(unsigned char *sha1, const char *expected_type)
24 int ret = -1;
25 enum object_type type;
26 unsigned long size;
27 void *buffer = read_sha1_file(sha1, &type, &size);
29 if (buffer) {
30 if (type == type_from_string(expected_type))
31 ret = check_sha1_signature(sha1, buffer, size, expected_type);
32 free(buffer);
34 return ret;
37 #ifdef NO_C99_FORMAT
38 #define PD_FMT "%d"
39 #else
40 #define PD_FMT "%td"
41 #endif
43 static int verify_tag(char *buffer, unsigned long size)
45 int typelen;
46 char type[20];
47 unsigned char sha1[20];
48 const char *object, *type_line, *tag_line, *tagger_line, *lb, *rb;
49 size_t len;
51 if (size < 84)
52 return error("wanna fool me ? you obviously got the size wrong !");
54 buffer[size] = 0;
56 /* Verify object line */
57 object = buffer;
58 if (memcmp(object, "object ", 7))
59 return error("char%d: does not start with \"object \"", 0);
61 if (get_sha1_hex(object + 7, sha1))
62 return error("char%d: could not get SHA1 hash", 7);
64 /* Verify type line */
65 type_line = object + 48;
66 if (memcmp(type_line - 1, "\ntype ", 6))
67 return error("char%d: could not find \"\\ntype \"", 47);
69 /* Verify tag-line */
70 tag_line = strchr(type_line, '\n');
71 if (!tag_line)
72 return error("char" PD_FMT ": could not find next \"\\n\"", type_line - buffer);
73 tag_line++;
74 if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n')
75 return error("char" PD_FMT ": no \"tag \" found", tag_line - buffer);
77 /* Get the actual type */
78 typelen = tag_line - type_line - strlen("type \n");
79 if (typelen >= sizeof(type))
80 return error("char" PD_FMT ": type too long", type_line+5 - buffer);
82 memcpy(type, type_line+5, typelen);
83 type[typelen] = 0;
85 /* Verify that the object matches */
86 if (verify_object(sha1, type))
87 return error("char%d: could not verify object %s", 7, sha1_to_hex(sha1));
89 /* Verify the tag-name: we don't allow control characters or spaces in it */
90 tag_line += 4;
91 for (;;) {
92 unsigned char c = *tag_line++;
93 if (c == '\n')
94 break;
95 if (c > ' ')
96 continue;
97 return error("char" PD_FMT ": could not verify tag name", tag_line - buffer);
100 /* Verify the tagger line */
101 tagger_line = tag_line;
103 if (memcmp(tagger_line, "tagger ", 7))
104 return error("char" PD_FMT ": could not find \"tagger \"",
105 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" PD_FMT ": malformed tagger field",
118 tagger_line - buffer);
120 /* Check for author name, at least one character, space is acceptable */
121 if (lb == tagger_line)
122 return error("char" PD_FMT ": missing tagger name",
123 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" PD_FMT ": missing tag timestamp",
129 tagger_line - buffer);
130 tagger_line += len;
131 if (*tagger_line != ' ')
132 return error("char" PD_FMT ": malformed tag timestamp",
133 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" PD_FMT ": malformed tag timezone",
141 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" PD_FMT ": trailing garbage in tag header",
147 tagger_line - buffer);
149 /* The actual stuff afterwards we don't care about.. */
150 return 0;
153 #undef PD_FMT
155 int main(int argc, char **argv)
157 struct strbuf buf = STRBUF_INIT;
158 unsigned char result_sha1[20];
160 if (argc != 1)
161 usage("git mktag < signaturefile");
163 git_extract_argv0_path(argv[0]);
165 setup_git_directory();
167 if (strbuf_read(&buf, 0, 4096) < 0) {
168 die("could not read from stdin");
171 /* Verify it for some basic sanity: it needs to start with
172 "object <sha1>\ntype\ntagger " */
173 if (verify_tag(buf.buf, buf.len) < 0)
174 die("invalid tag signature file");
176 if (write_sha1_file(buf.buf, buf.len, tag_type, result_sha1) < 0)
177 die("unable to write tag file");
179 strbuf_release(&buf);
180 printf("%s\n", sha1_to_hex(result_sha1));
181 return 0;