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