2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
13 #include <curl/curl.h>
15 #define BLOCKING (1ul << 14)
18 * FIXME! Share the code with "write-tree.c"
20 static void init_buffer(char **bufp, unsigned int *sizep)
22 char *buf = xmalloc(BLOCKING);
27 static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...)
32 unsigned long alloc, size, newsize;
36 len = vsnprintf(one_line, sizeof(one_line), fmt, args);
40 alloc = (size + 32767) & ~32767;
42 if (newsize > alloc) {
43 alloc = (newsize + 32767) & ~32767;
44 buf = xrealloc(buf, alloc);
48 memcpy(buf + size, one_line, len);
51 static void remove_special(char *p)
54 char *dst = p, *src = p;
60 case '\n': case '<': case '>':
69 * Go back, and remove crud from the end: some people
70 * have commas etc in their gecos field
74 unsigned char c = *dst;
76 case ',': case ';': case '.':
84 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
85 (i.e. English) day/month names, and it doesn't work correctly with %z. */
86 static void parse_date(char *date, time_t *now, char *result, int maxlen)
91 if ((then = curl_getdate(date, now)) == 0)
94 /* find the timezone at the end */
95 p = date + strlen(date);
96 while (p > date && isdigit(*--p))
98 if ((*p == '+' || *p == '-') && strlen(p) == 5)
99 snprintf(result, maxlen, "%lu %5.5s", then, p);
102 static void check_valid(unsigned char *sha1, const char *expect)
108 buf = read_sha1_file(sha1, type, &size);
109 if (!buf || strcmp(type, expect))
110 die("%s is not a valid '%s' object", sha1_to_hex(sha1), expect);
115 * Having more than two parents is not strange at all, and this is
116 * how multi-way merges are represented.
118 #define MAXPARENT (16)
120 static char *commit_tree_usage = "commit-tree <sha1> [-p <sha1>]* < changelog";
122 int main(int argc, char **argv)
126 unsigned char tree_sha1[20];
127 unsigned char parent_sha1[MAXPARENT][20];
128 unsigned char commit_sha1[20];
129 char *gecos, *realgecos, *commitgecos;
130 char *email, *commitemail, realemail[1000];
131 char date[20], realdate[20];
140 if (argc < 2 || get_sha1_hex(argv[1], tree_sha1) < 0)
141 usage(commit_tree_usage);
143 check_valid(tree_sha1, "tree");
144 for (i = 2; i < argc; i += 2) {
146 a = argv[i]; b = argv[i+1];
147 if (!b || strcmp(a, "-p") || get_sha1_hex(b, parent_sha1[parents]))
148 usage(commit_tree_usage);
149 check_valid(parent_sha1[parents], "commit");
153 fprintf(stderr, "Committing initial tree %s\n", argv[1]);
154 pw = getpwuid(getuid());
156 die("You don't exist. Go away!");
157 realgecos = pw->pw_gecos;
158 len = strlen(pw->pw_name);
159 memcpy(realemail, pw->pw_name, len);
160 realemail[len] = '@';
161 gethostname(realemail+len+1, sizeof(realemail)-len-1);
162 if (!strchr(realemail+len+1, '.')) {
163 strcat(realemail, ".");
164 getdomainname(realemail+strlen(realemail), sizeof(realemail)-strlen(realemail)-1);
167 tm = localtime(&now);
169 strftime(realdate, sizeof(realdate), "%s %z", tm);
170 strcpy(date, realdate);
172 commitgecos = getenv("COMMIT_AUTHOR_NAME") ? : realgecos;
173 commitemail = getenv("COMMIT_AUTHOR_EMAIL") ? : realemail;
174 gecos = getenv("AUTHOR_NAME") ? : realgecos;
175 email = getenv("AUTHOR_EMAIL") ? : realemail;
176 audate = getenv("AUTHOR_DATE");
178 parse_date(audate, &now, date, sizeof(date));
180 remove_special(gecos); remove_special(realgecos); remove_special(commitgecos);
181 remove_special(email); remove_special(realemail); remove_special(commitemail);
183 init_buffer(&buffer, &size);
184 add_buffer(&buffer, &size, "tree %s\n", sha1_to_hex(tree_sha1));
187 * NOTE! This ordering means that the same exact tree merged with a
188 * different order of parents will be a _different_ changeset even
189 * if everything else stays the same.
191 for (i = 0; i < parents; i++)
192 add_buffer(&buffer, &size, "parent %s\n", sha1_to_hex(parent_sha1[i]));
194 /* Person/date information */
195 add_buffer(&buffer, &size, "author %s <%s> %s\n", gecos, email, date);
196 add_buffer(&buffer, &size, "committer %s <%s> %s\n\n", commitgecos, commitemail, realdate);
198 /* And add the comment */
199 while (fgets(comment, sizeof(comment), stdin) != NULL)
200 add_buffer(&buffer, &size, "%s", comment);
202 write_sha1_file(buffer, size, "commit", commit_sha1);
203 printf("%s\n", sha1_to_hex(commit_sha1));