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 three lines are guaranteed to be at least 63 bytes:
13 * "object <sha1>\n" is 48 bytes, "type tag\n" at 9 bytes is the
14 * shortest possible type-line, and "tag .\n" at 6 bytes is the
15 * shortest single-character-tag line.
19 * We refuse to tag something we can't verify. Just because.
21 static int verify_object(unsigned char *sha1
, const char *expected_type
)
24 enum object_type type
;
26 void *buffer
= read_sha1_file(sha1
, &type
, &size
);
29 if (type
== type_from_string(expected_type
))
30 ret
= check_sha1_signature(sha1
, buffer
, size
, expected_type
);
42 static int verify_tag(char *buffer
, unsigned long size
)
46 unsigned char sha1
[20];
47 const char *object
, *type_line
, *tag_line
, *tagger_line
;
50 return error("wanna fool me ? you obviously got the size wrong !");
54 /* Verify object line */
56 if (memcmp(object
, "object ", 7))
57 return error("char%d: does not start with \"object \"", 0);
59 if (get_sha1_hex(object
+ 7, sha1
))
60 return error("char%d: could not get SHA1 hash", 7);
62 /* Verify type line */
63 type_line
= object
+ 48;
64 if (memcmp(type_line
- 1, "\ntype ", 6))
65 return error("char%d: could not find \"\\ntype \"", 47);
68 tag_line
= strchr(type_line
, '\n');
70 return error("char" PD_FMT
": could not find next \"\\n\"", type_line
- buffer
);
72 if (memcmp(tag_line
, "tag ", 4) || tag_line
[4] == '\n')
73 return error("char" PD_FMT
": no \"tag \" found", 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" PD_FMT
": type too long", type_line
+5 - buffer
);
80 memcpy(type
, type_line
+5, typelen
);
83 /* Verify that the object matches */
84 if (verify_object(sha1
, type
))
85 return error("char%d: could not verify object %s", 7, sha1_to_hex(sha1
));
87 /* Verify the tag-name: we don't allow control characters or spaces in it */
90 unsigned char c
= *tag_line
++;
95 return error("char" PD_FMT
": could not verify tag name", tag_line
- buffer
);
98 /* Verify the tagger line */
99 tagger_line
= tag_line
;
101 if (memcmp(tagger_line
, "tagger", 6) || (tagger_line
[6] == '\n'))
102 return error("char" PD_FMT
": could not find \"tagger\"", tagger_line
- buffer
);
104 /* TODO: check for committer info + blank line? */
105 /* Also, the minimum length is probably + "tagger .", or 63+8=71 */
107 /* The actual stuff afterwards we don't care about.. */
113 int main(int argc
, char **argv
)
116 unsigned char result_sha1
[20];
119 usage("git-mktag < signaturefile");
121 setup_git_directory();
123 strbuf_init(&buf
, 0);
124 if (strbuf_read(&buf
, 0, 4096) < 0) {
125 die("could not read from stdin");
128 /* Verify it for some basic sanity: it needs to start with
129 "object <sha1>\ntype\ntagger " */
130 if (verify_tag(buf
.buf
, buf
.len
) < 0)
131 die("invalid tag signature file");
133 if (write_sha1_file(buf
.buf
, buf
.len
, tag_type
, result_sha1
) < 0)
134 die("unable to write tag file");
136 strbuf_release(&buf
);
137 printf("%s\n", sha1_to_hex(result_sha1
));