2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
11 #define BLOCKING (1ul << 14)
12 #define ORIG_OFFSET (40)
15 * Leave space at the beginning to insert the tag
16 * once we know how big things are.
18 * FIXME! Share the code with "write-tree.c"
20 static void init_buffer(char **bufp, unsigned int *sizep)
22 char *buf = malloc(BLOCKING);
23 memset(buf, 0, ORIG_OFFSET);
28 static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...)
33 unsigned long alloc, size, newsize;
37 len = vsnprintf(one_line, sizeof(one_line), fmt, args);
41 alloc = (size + 32767) & ~32767;
43 if (newsize > alloc) {
44 alloc = (newsize + 32767) & ~32767;
45 buf = realloc(buf, alloc);
49 memcpy(buf + size, one_line, len);
52 static int prepend_integer(char *buffer, unsigned val, int i)
56 buffer[--i] = '0' + (val % 10);
62 static void finish_buffer(char *tag, char **bufp, unsigned int *sizep)
67 unsigned int size = *sizep;
69 offset = prepend_integer(buf, size - ORIG_OFFSET, ORIG_OFFSET);
74 memcpy(buf, tag, taglen);
80 static void remove_special(char *p)
89 case '\n': case '<': case '>':
99 * Having more than two parents may be strange, but hey, there's
100 * no conceptual reason why the file format couldn't accept multi-way
101 * merges. It might be the "union" of several packages, for example.
103 * I don't really expect that to happen, but this is here to make
104 * it clear that _conceptually_ it's ok..
106 #define MAXPARENT (16)
108 int main(int argc, char **argv)
112 unsigned char tree_sha1[20];
113 unsigned char parent_sha1[MAXPARENT][20];
114 unsigned char commit_sha1[20];
115 char *gecos, *realgecos;
116 char *email, realemail[1000];
117 char *date, *realdate;
124 if (argc < 2 || get_sha1_hex(argv[1], tree_sha1) < 0)
125 usage("commit-tree <sha1> [-p <sha1>]* < changelog");
127 for (i = 2; i < argc; i += 2) {
129 a = argv[i]; b = argv[i+1];
130 if (!b || strcmp(a, "-p") || get_sha1_hex(b, parent_sha1[parents]))
131 usage("commit-tree <sha1> [-p <sha1>]* < changelog");
135 fprintf(stderr, "Committing initial tree %s\n", argv[1]);
136 pw = getpwuid(getuid());
138 die("You don't exist. Go away!");
139 realgecos = pw->pw_gecos;
140 len = strlen(pw->pw_name);
141 memcpy(realemail, pw->pw_name, len);
142 realemail[len] = '@';
143 gethostname(realemail+len+1, sizeof(realemail)-len-1);
145 realdate = ctime(&now);
147 gecos = getenv("AUTHOR_NAME") ? : realgecos;
148 email = getenv("AUTHOR_EMAIL") ? : realemail;
149 date = getenv("AUTHOR_DATE") ? : realdate;
151 remove_special(gecos); remove_special(realgecos);
152 remove_special(email); remove_special(realemail);
153 remove_special(date); remove_special(realdate);
155 init_buffer(&buffer, &size);
156 add_buffer(&buffer, &size, "tree %s\n", sha1_to_hex(tree_sha1));
159 * NOTE! This ordering means that the same exact tree merged with a
160 * different order of parents will be a _different_ changeset even
161 * if everything else stays the same.
163 for (i = 0; i < parents; i++)
164 add_buffer(&buffer, &size, "parent %s\n", sha1_to_hex(parent_sha1[i]));
166 /* Person/date information */
167 add_buffer(&buffer, &size, "author %s <%s> %s\n", gecos, email, date);
168 add_buffer(&buffer, &size, "committer %s <%s> %s\n\n", realgecos, realemail, realdate);
170 /* And add the comment */
171 while (fgets(comment, sizeof(comment), stdin) != NULL)
172 add_buffer(&buffer, &size, "%s", comment);
174 finish_buffer("commit ", &buffer, &size);
176 write_sha1_file(buffer, size, commit_sha1);
177 printf("%s\n", sha1_to_hex(commit_sha1));