5 * A signature file has a very simple fixed format: four lines
6 * of "object <sha1>" + "type <typename>" + "tag <tagname>" +
7 * "tagger <committer>", followed by a blank line, a free-form tag
8 * message and a signature block that git itself doesn't care about,
9 * but that can be verified with gpg or similar.
11 * The first four lines are guaranteed to be at least 83 bytes:
12 * "object <sha1>\n" is 48 bytes, "type tag\n" at 9 bytes is the
13 * shortest possible type-line, "tag .\n" at 6 bytes is the shortest
14 * single-character-tag line, and "tagger . <> 0 +0000\n" at 20 bytes is
15 * the shortest possible tagger-line.
19 * We refuse to tag something we can't verify. Just because.
21 static int verify_object(const struct object_id
*oid
, const char *expected_type
)
24 enum object_type type
;
26 void *buffer
= read_object_file(oid
, &type
, &size
);
27 const struct object_id
*repl
= lookup_replace_object(oid
);
30 if (type
== type_from_string(expected_type
))
31 ret
= check_object_signature(repl
, buffer
, size
, expected_type
);
37 static int verify_tag(char *buffer
, unsigned long size
)
42 const char *object
, *type_line
, *tag_line
, *tagger_line
, *lb
, *rb
, *p
;
46 return error("wanna fool me ? you obviously got the size wrong !");
50 /* Verify object line */
52 if (memcmp(object
, "object ", 7))
53 return error("char%d: does not start with \"object \"", 0);
55 if (parse_oid_hex(object
+ 7, &oid
, &p
))
56 return error("char%d: could not get SHA1 hash", 7);
58 /* Verify type line */
60 if (memcmp(type_line
- 1, "\ntype ", 6))
61 return error("char%d: could not find \"\\ntype \"", 47);
64 tag_line
= strchr(type_line
, '\n');
66 return error("char%"PRIuMAX
": could not find next \"\\n\"",
67 (uintmax_t) (type_line
- buffer
));
69 if (memcmp(tag_line
, "tag ", 4) || tag_line
[4] == '\n')
70 return error("char%"PRIuMAX
": no \"tag \" found",
71 (uintmax_t) (tag_line
- buffer
));
73 /* Get the actual type */
74 typelen
= tag_line
- type_line
- strlen("type \n");
75 if (typelen
>= sizeof(type
))
76 return error("char%"PRIuMAX
": type too long",
77 (uintmax_t) (type_line
+5 - buffer
));
79 memcpy(type
, type_line
+5, typelen
);
82 /* Verify that the object matches */
83 if (verify_object(&oid
, type
))
84 return error("char%d: could not verify object %s", 7, oid_to_hex(&oid
));
86 /* Verify the tag-name: we don't allow control characters or spaces in it */
89 unsigned char c
= *tag_line
++;
94 return error("char%"PRIuMAX
": could not verify tag name",
95 (uintmax_t) (tag_line
- buffer
));
98 /* Verify the tagger line */
99 tagger_line
= tag_line
;
101 if (memcmp(tagger_line
, "tagger ", 7))
102 return error("char%"PRIuMAX
": could not find \"tagger \"",
103 (uintmax_t) (tagger_line
- buffer
));
106 * Check for correct form for name and email
107 * i.e. " <" followed by "> " on _this_ line
108 * No angle brackets within the name or email address fields.
109 * No spaces within the email address field.
112 if (!(lb
= strstr(tagger_line
, " <")) || !(rb
= strstr(lb
+2, "> ")) ||
113 strpbrk(tagger_line
, "<>\n") != lb
+1 ||
114 strpbrk(lb
+2, "><\n ") != rb
)
115 return error("char%"PRIuMAX
": malformed tagger field",
116 (uintmax_t) (tagger_line
- buffer
));
118 /* Check for author name, at least one character, space is acceptable */
119 if (lb
== tagger_line
)
120 return error("char%"PRIuMAX
": missing tagger name",
121 (uintmax_t) (tagger_line
- buffer
));
123 /* timestamp, 1 or more digits followed by space */
124 tagger_line
= rb
+ 2;
125 if (!(len
= strspn(tagger_line
, "0123456789")))
126 return error("char%"PRIuMAX
": missing tag timestamp",
127 (uintmax_t) (tagger_line
- buffer
));
129 if (*tagger_line
!= ' ')
130 return error("char%"PRIuMAX
": malformed tag timestamp",
131 (uintmax_t) (tagger_line
- buffer
));
134 /* timezone, 5 digits [+-]hhmm, max. 1400 */
135 if (!((tagger_line
[0] == '+' || tagger_line
[0] == '-') &&
136 strspn(tagger_line
+1, "0123456789") == 4 &&
137 tagger_line
[5] == '\n' && atoi(tagger_line
+1) <= 1400))
138 return error("char%"PRIuMAX
": malformed tag timezone",
139 (uintmax_t) (tagger_line
- buffer
));
142 /* Verify the blank line separating the header from the body */
143 if (*tagger_line
!= '\n')
144 return error("char%"PRIuMAX
": trailing garbage in tag header",
145 (uintmax_t) (tagger_line
- buffer
));
147 /* The actual stuff afterwards we don't care about.. */
151 int cmd_mktag(int argc
, const char **argv
, const char *prefix
)
153 struct strbuf buf
= STRBUF_INIT
;
154 struct object_id result
;
159 if (strbuf_read(&buf
, 0, 4096) < 0) {
160 die_errno("could not read from stdin");
163 /* Verify it for some basic sanity: it needs to start with
164 "object <sha1>\ntype\ntagger " */
165 if (verify_tag(buf
.buf
, buf
.len
) < 0)
166 die("invalid tag signature file");
168 if (write_object_file(buf
.buf
, buf
.len
, tag_type
, &result
) < 0)
169 die("unable to write tag file");
171 strbuf_release(&buf
);
172 printf("%s\n", oid_to_hex(&result
));