make "mktag" a built-in
[git/git.git] / builtin-mktag.c
CommitLineData
ec4465ad 1#include "cache.h"
8e440259 2#include "tag.h"
2fb3f6db 3#include "exec_cmd.h"
ec4465ad
LT
4
5/*
446c6fae
RJ
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.
ec4465ad 11 *
e0aaf781 12 * The first four lines are guaranteed to be at least 83 bytes:
ec4465ad 13 * "object <sha1>\n" is 48 bytes, "type tag\n" at 9 bytes is the
e0aaf781
BC
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.
ec4465ad
LT
17 */
18
ec4465ad
LT
19/*
20 * We refuse to tag something we can't verify. Just because.
21 */
cc400f50 22static int verify_object(const unsigned char *sha1, const char *expected_type)
ec4465ad
LT
23{
24 int ret = -1;
21666f1a 25 enum object_type type;
91d7b8af 26 unsigned long size;
cc400f50
CC
27 const unsigned char *repl;
28 void *buffer = read_sha1_file_repl(sha1, &type, &size, &repl);
ec4465ad 29
91d7b8af 30 if (buffer) {
21666f1a 31 if (type == type_from_string(expected_type))
cc400f50 32 ret = check_sha1_signature(repl, buffer, size, expected_type);
91d7b8af 33 free(buffer);
ec4465ad
LT
34 }
35 return ret;
36}
37
579d1fbf
RJ
38#ifdef NO_C99_FORMAT
39#define PD_FMT "%d"
40#else
41#define PD_FMT "%td"
42#endif
43
ec4465ad
LT
44static int verify_tag(char *buffer, unsigned long size)
45{
46 int typelen;
47 char type[20];
48 unsigned char sha1[20];
e0aaf781 49 const char *object, *type_line, *tag_line, *tagger_line, *lb, *rb;
ba26ab99 50 size_t len;
ec4465ad 51
e0aaf781 52 if (size < 84)
446c6fae 53 return error("wanna fool me ? you obviously got the size wrong !");
cfba0459 54
ec4465ad
LT
55 buffer[size] = 0;
56
57 /* Verify object line */
58 object = buffer;
59 if (memcmp(object, "object ", 7))
446c6fae 60 return error("char%d: does not start with \"object \"", 0);
cfba0459 61
ec4465ad 62 if (get_sha1_hex(object + 7, sha1))
446c6fae 63 return error("char%d: could not get SHA1 hash", 7);
ec4465ad
LT
64
65 /* Verify type line */
66 type_line = object + 48;
67 if (memcmp(type_line - 1, "\ntype ", 6))
446c6fae 68 return error("char%d: could not find \"\\ntype \"", 47);
ec4465ad
LT
69
70 /* Verify tag-line */
71 tag_line = strchr(type_line, '\n');
72 if (!tag_line)
579d1fbf 73 return error("char" PD_FMT ": could not find next \"\\n\"", type_line - buffer);
ec4465ad
LT
74 tag_line++;
75 if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n')
579d1fbf 76 return error("char" PD_FMT ": no \"tag \" found", tag_line - buffer);
ec4465ad
LT
77
78 /* Get the actual type */
79 typelen = tag_line - type_line - strlen("type \n");
80 if (typelen >= sizeof(type))
579d1fbf 81 return error("char" PD_FMT ": type too long", type_line+5 - buffer);
cfba0459 82
ec4465ad
LT
83 memcpy(type, type_line+5, typelen);
84 type[typelen] = 0;
85
86 /* Verify that the object matches */
ec4465ad 87 if (verify_object(sha1, type))
446c6fae 88 return error("char%d: could not verify object %s", 7, sha1_to_hex(sha1));
ec4465ad
LT
89
90 /* Verify the tag-name: we don't allow control characters or spaces in it */
91 tag_line += 4;
92 for (;;) {
93 unsigned char c = *tag_line++;
94 if (c == '\n')
95 break;
96 if (c > ' ')
97 continue;
579d1fbf 98 return error("char" PD_FMT ": could not verify tag name", tag_line - buffer);
ec4465ad
LT
99 }
100
c818566d
EB
101 /* Verify the tagger line */
102 tagger_line = tag_line;
103
ba26ab99 104 if (memcmp(tagger_line, "tagger ", 7))
e0aaf781
BC
105 return error("char" PD_FMT ": could not find \"tagger \"",
106 tagger_line - buffer);
107
108 /*
109 * Check for correct form for name and email
110 * i.e. " <" followed by "> " on _this_ line
ba26ab99
BC
111 * No angle brackets within the name or email address fields.
112 * No spaces within the email address field.
e0aaf781
BC
113 */
114 tagger_line += 7;
115 if (!(lb = strstr(tagger_line, " <")) || !(rb = strstr(lb+2, "> ")) ||
ba26ab99
BC
116 strpbrk(tagger_line, "<>\n") != lb+1 ||
117 strpbrk(lb+2, "><\n ") != rb)
118 return error("char" PD_FMT ": malformed tagger field",
e0aaf781
BC
119 tagger_line - buffer);
120
121 /* Check for author name, at least one character, space is acceptable */
122 if (lb == tagger_line)
123 return error("char" PD_FMT ": missing tagger name",
124 tagger_line - buffer);
125
ba26ab99 126 /* timestamp, 1 or more digits followed by space */
e0aaf781 127 tagger_line = rb + 2;
ba26ab99
BC
128 if (!(len = strspn(tagger_line, "0123456789")))
129 return error("char" PD_FMT ": missing tag timestamp",
e0aaf781 130 tagger_line - buffer);
ba26ab99
BC
131 tagger_line += len;
132 if (*tagger_line != ' ')
e0aaf781
BC
133 return error("char" PD_FMT ": malformed tag timestamp",
134 tagger_line - buffer);
ba26ab99 135 tagger_line++;
446c6fae 136
e0aaf781
BC
137 /* timezone, 5 digits [+-]hhmm, max. 1400 */
138 if (!((tagger_line[0] == '+' || tagger_line[0] == '-') &&
ba26ab99 139 strspn(tagger_line+1, "0123456789") == 4 &&
e0aaf781
BC
140 tagger_line[5] == '\n' && atoi(tagger_line+1) <= 1400))
141 return error("char" PD_FMT ": malformed tag timezone",
142 tagger_line - buffer);
143 tagger_line += 6;
144
145 /* Verify the blank line separating the header from the body */
146 if (*tagger_line != '\n')
147 return error("char" PD_FMT ": trailing garbage in tag header",
148 tagger_line - buffer);
c818566d 149
ec4465ad
LT
150 /* The actual stuff afterwards we don't care about.. */
151 return 0;
152}
153
579d1fbf
RJ
154#undef PD_FMT
155
112dd514 156int cmd_mktag(int argc, const char **argv, const char *prefix)
ec4465ad 157{
f285a2d7 158 struct strbuf buf = STRBUF_INIT;
ec4465ad
LT
159 unsigned char result_sha1[20];
160
161 if (argc != 1)
34263de0 162 usage("git mktag < signaturefile");
ec4465ad 163
fd17f5b5 164 if (strbuf_read(&buf, 0, 4096) < 0) {
0721c314 165 die_errno("could not read from stdin");
b97e3dfa 166 }
ec4465ad 167
a9486b02
PR
168 /* Verify it for some basic sanity: it needs to start with
169 "object <sha1>\ntype\ntagger " */
fd17f5b5 170 if (verify_tag(buf.buf, buf.len) < 0)
ec4465ad
LT
171 die("invalid tag signature file");
172
fd17f5b5 173 if (write_sha1_file(buf.buf, buf.len, tag_type, result_sha1) < 0)
ec4465ad 174 die("unable to write tag file");
e7332f96 175
fd17f5b5 176 strbuf_release(&buf);
ec4465ad
LT
177 printf("%s\n", sha1_to_hex(result_sha1));
178 return 0;
179}