rev-list --boundary: show boundary commits even when limited otherwise.
[alt-git.git] / mktag.c
blob23288781cf6caad62253130f75112e18eb4905aa
1 #include "cache.h"
2 #include "tag.h"
4 /*
5 * A signature file has a very simple fixed format: three lines
6 * of "object <sha1>" + "type <typename>" + "tag <tagname>",
7 * followed by some free-form signature that git itself doesn't
8 * care about, but that can be verified with gpg or similar.
10 * The first three lines are guaranteed to be at least 63 bytes:
11 * "object <sha1>\n" is 48 bytes, "type tag\n" at 9 bytes is the
12 * shortest possible type-line, and "tag .\n" at 6 bytes is the
13 * shortest single-character-tag line.
15 * We also artificially limit the size of the full object to 8kB.
16 * Just because I'm a lazy bastard, and if you can't fit a signature
17 * in that size, you're doing something wrong.
20 // Some random size
21 #define MAXSIZE (8192)
24 * We refuse to tag something we can't verify. Just because.
26 static int verify_object(unsigned char *sha1, const char *expected_type)
28 int ret = -1;
29 char type[100];
30 unsigned long size;
31 void *buffer = read_sha1_file(sha1, type, &size);
33 if (buffer) {
34 if (!strcmp(type, expected_type))
35 ret = check_sha1_signature(sha1, buffer, size, type);
36 free(buffer);
38 return ret;
41 static int verify_tag(char *buffer, unsigned long size)
43 int typelen;
44 char type[20];
45 unsigned char sha1[20];
46 const char *object, *type_line, *tag_line, *tagger_line;
48 if (size < 64 || size > MAXSIZE-1)
49 return -1;
50 buffer[size] = 0;
52 /* Verify object line */
53 object = buffer;
54 if (memcmp(object, "object ", 7))
55 return -1;
56 if (get_sha1_hex(object + 7, sha1))
57 return -1;
59 /* Verify type line */
60 type_line = object + 48;
61 if (memcmp(type_line - 1, "\ntype ", 6))
62 return -1;
64 /* Verify tag-line */
65 tag_line = strchr(type_line, '\n');
66 if (!tag_line)
67 return -1;
68 tag_line++;
69 if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n')
70 return -1;
72 /* Get the actual type */
73 typelen = tag_line - type_line - strlen("type \n");
74 if (typelen >= sizeof(type))
75 return -1;
76 memcpy(type, type_line+5, typelen);
77 type[typelen] = 0;
79 /* Verify that the object matches */
80 if (get_sha1_hex(object + 7, sha1))
81 return -1;
82 if (verify_object(sha1, type))
83 return -1;
85 /* Verify the tag-name: we don't allow control characters or spaces in it */
86 tag_line += 4;
87 for (;;) {
88 unsigned char c = *tag_line++;
89 if (c == '\n')
90 break;
91 if (c > ' ')
92 continue;
93 return -1;
96 /* Verify the tagger line */
97 tagger_line = tag_line;
99 if (memcmp(tagger_line, "tagger", 6) || (tagger_line[6] == '\n'))
100 return -1;
102 /* The actual stuff afterwards we don't care about.. */
103 return 0;
106 int main(int argc, char **argv)
108 unsigned long size;
109 char buffer[MAXSIZE];
110 unsigned char result_sha1[20];
112 if (argc != 1)
113 usage("cat <signaturefile> | git-mktag");
115 setup_git_directory();
117 // Read the signature
118 size = 0;
119 for (;;) {
120 int ret = xread(0, buffer + size, MAXSIZE - size);
121 if (ret <= 0)
122 break;
123 size += ret;
126 // Verify it for some basic sanity: it needs to start with "object <sha1>\ntype\ntagger "
127 if (verify_tag(buffer, size) < 0)
128 die("invalid tag signature file");
130 if (write_sha1_file(buffer, size, tag_type, result_sha1) < 0)
131 die("unable to write tag file");
132 printf("%s\n", sha1_to_hex(result_sha1));
133 return 0;