6 #define BLOCKING (1ul << 14)
7 #define ORIG_OFFSET (40)
10 * Leave space at the beginning to insert the tag
11 * once we know how big things are.
13 * FIXME! Share the code with "write-tree.c"
15 static void init_buffer(char **bufp, unsigned int *sizep)
17 char *buf = malloc(BLOCKING);
18 memset(buf, 0, ORIG_OFFSET);
23 static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...)
28 unsigned long alloc, size, newsize;
32 len = vsnprintf(one_line, sizeof(one_line), fmt, args);
36 alloc = (size + 32767) & ~32767;
38 if (newsize > alloc) {
39 alloc = (newsize + 32767) & ~32767;
40 buf = realloc(buf, alloc);
44 memcpy(buf + size, one_line, len);
47 static int prepend_integer(char *buffer, unsigned val, int i)
51 buffer[--i] = '0' + (val % 10);
57 static void finish_buffer(char *tag, char **bufp, unsigned int *sizep)
62 unsigned int size = *sizep;
64 offset = prepend_integer(buf, size - ORIG_OFFSET, ORIG_OFFSET);
69 memcpy(buf, tag, taglen);
75 static void remove_special(char *p)
84 case '\n': case '<': case '>':
94 * Having more than two parents may be strange, but hey, there's
95 * no conceptual reason why the file format couldn't accept multi-way
96 * merges. It might be the "union" of several packages, for example.
98 * I don't really expect that to happen, but this is here to make
99 * it clear that _conceptually_ it's ok..
101 #define MAXPARENT (16)
103 int main(int argc, char **argv)
107 unsigned char tree_sha1[20];
108 unsigned char parent_sha1[MAXPARENT][20];
109 char *gecos, *realgecos;
110 char *email, realemail[1000];
111 char *date, *realdate;
118 if (argc < 2 || get_sha1_hex(argv[1], tree_sha1) < 0)
119 usage("commit-tree <sha1> [-p <sha1>]* < changelog");
121 for (i = 2; i < argc; i += 2) {
123 a = argv[i]; b = argv[i+1];
124 if (!b || strcmp(a, "-p") || get_sha1_hex(b, parent_sha1[parents]))
125 usage("commit-tree <sha1> [-p <sha1>]* < changelog");
129 fprintf(stderr, "Committing initial tree %s\n", argv[1]);
130 pw = getpwuid(getuid());
132 usage("You don't exist. Go away!");
133 realgecos = pw->pw_gecos;
134 len = strlen(pw->pw_name);
135 memcpy(realemail, pw->pw_name, len);
136 realemail[len] = '@';
137 gethostname(realemail+len+1, sizeof(realemail)-len-1);
139 realdate = ctime(&now);
141 gecos = getenv("COMMITTER_NAME") ? : realgecos;
142 email = getenv("COMMITTER_EMAIL") ? : realemail;
143 date = getenv("COMMITTER_DATE") ? : realdate;
145 remove_special(gecos); remove_special(realgecos);
146 remove_special(email); remove_special(realemail);
147 remove_special(date); remove_special(realdate);
149 init_buffer(&buffer, &size);
150 add_buffer(&buffer, &size, "tree %s\n", sha1_to_hex(tree_sha1));
153 * NOTE! This ordering means that the same exact tree merged with a
154 * different order of parents will be a _different_ changeset even
155 * if everything else stays the same.
157 for (i = 0; i < parents; i++)
158 add_buffer(&buffer, &size, "parent %s\n", sha1_to_hex(parent_sha1[i]));
160 /* Person/date information */
161 add_buffer(&buffer, &size, "author %s <%s> %s\n", gecos, email, date);
162 add_buffer(&buffer, &size, "committer %s <%s> %s\n\n", realgecos, realemail, realdate);
164 /* And add the comment */
165 while (fgets(comment, sizeof(comment), stdin) != NULL)
166 add_buffer(&buffer, &size, "%s", comment);
168 finish_buffer("commit ", &buffer, &size);
170 write_sha1_file(buffer, size);