2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
14 #define BLOCKING (1ul << 14)
17 * FIXME! Share the code with "write-tree.c"
19 static void init_buffer(char **bufp
, unsigned int *sizep
)
21 char *buf
= xmalloc(BLOCKING
);
26 static void add_buffer(char **bufp
, unsigned int *sizep
, const char *fmt
, ...)
31 unsigned long alloc
, size
, newsize
;
35 len
= vsnprintf(one_line
, sizeof(one_line
), fmt
, args
);
39 alloc
= (size
+ 32767) & ~32767;
41 if (newsize
> alloc
) {
42 alloc
= (newsize
+ 32767) & ~32767;
43 buf
= xrealloc(buf
, alloc
);
47 memcpy(buf
+ size
, one_line
, len
);
50 static void remove_special(char *p
)
53 char *dst
= p
, *src
= p
;
59 case '\n': case '<': case '>':
68 * Go back, and remove crud from the end: some people
69 * have commas etc in their gecos field
73 unsigned char c
= *dst
;
75 case ',': case ';': case '.':
83 static const char *month_names
[] = {
84 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
85 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
88 static const char *weekday_names
[] = {
89 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
93 static char *skipfws(char *str
)
101 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
102 (i.e. English) day/month names, and it doesn't work correctly with %z. */
103 static void parse_rfc2822_date(char *date
, char *result
, int maxlen
)
110 memset(&tm
, 0, sizeof(tm
));
115 for (i
=0; i
<7; i
++) {
116 if (!strncmp(p
,weekday_names
[i
],3) && p
[3] == ',') {
126 tm
.tm_mday
= strtoul(p
, &p
, 10);
128 if (tm
.tm_mday
< 1 || tm
.tm_mday
> 31)
138 for (i
=0; i
<12; i
++) {
139 if (!strncmp(p
, month_names
[i
], 3) && isspace(p
[3])) {
141 p
= skipfws(p
+strlen(month_names
[i
]));
145 return; /* Error -- bad month */
149 tm
.tm_year
= strtoul(p
, &p
, 10);
151 if (!tm
.tm_year
&& !isspace(*p
))
154 if (tm
.tm_year
> 1900)
162 tm
.tm_hour
= strtoul(p
, &p
, 10);
164 if (!tm
.tm_hour
> 23)
168 return; /* Error -- bad time */
174 tm
.tm_min
= strtoul(p
, &p
, 10);
183 return; /* Error -- bad time */
189 tm
.tm_sec
= strtoul(p
, &p
, 10);
207 if (!isdigit(p
[1]) || !isdigit(p
[2]) || !isdigit(p
[3]) || !isdigit(p
[4]))
210 i
= strtoul(p
+1, NULL
, 10);
211 offset
*= ((i
% 100) + ((i
/ 100) * 60));
213 if (*(skipfws(p
+ 5)))
216 then
= mktime(&tm
); /* mktime appears to ignore the GMT offset, stupidly */
222 snprintf(result
, maxlen
, "%lu %5.5s", then
, p
);
225 static void check_valid(unsigned char *sha1
, const char *expect
)
231 buf
= read_sha1_file(sha1
, type
, &size
);
232 if (!buf
|| strcmp(type
, expect
))
233 die("%s is not a valid '%s' object", sha1_to_hex(sha1
), expect
);
238 * Having more than two parents is not strange at all, and this is
239 * how multi-way merges are represented.
241 #define MAXPARENT (16)
243 static char *commit_tree_usage
= "commit-tree <sha1> [-p <sha1>]* < changelog";
245 int main(int argc
, char **argv
)
249 unsigned char tree_sha1
[20];
250 unsigned char parent_sha1
[MAXPARENT
][20];
251 unsigned char commit_sha1
[20];
252 char *gecos
, *realgecos
, *commitgecos
;
253 char *email
, *commitemail
, realemail
[1000];
254 char date
[20], realdate
[20];
263 if (argc
< 2 || get_sha1_hex(argv
[1], tree_sha1
) < 0)
264 usage(commit_tree_usage
);
266 check_valid(tree_sha1
, "tree");
267 for (i
= 2; i
< argc
; i
+= 2) {
269 a
= argv
[i
]; b
= argv
[i
+1];
270 if (!b
|| strcmp(a
, "-p") || get_sha1_hex(b
, parent_sha1
[parents
]))
271 usage(commit_tree_usage
);
272 check_valid(parent_sha1
[parents
], "commit");
276 fprintf(stderr
, "Committing initial tree %s\n", argv
[1]);
277 pw
= getpwuid(getuid());
279 die("You don't exist. Go away!");
280 realgecos
= pw
->pw_gecos
;
281 len
= strlen(pw
->pw_name
);
282 memcpy(realemail
, pw
->pw_name
, len
);
283 realemail
[len
] = '@';
284 gethostname(realemail
+len
+1, sizeof(realemail
)-len
-1);
285 if (!strchr(realemail
+len
+1, '.')) {
286 strcat(realemail
, ".");
287 getdomainname(realemail
+strlen(realemail
), sizeof(realemail
)-strlen(realemail
)-1);
290 tm
= localtime(&now
);
292 strftime(realdate
, sizeof(realdate
), "%s %z", tm
);
293 strcpy(date
, realdate
);
295 commitgecos
= getenv("COMMIT_AUTHOR_NAME") ? : realgecos
;
296 commitemail
= getenv("COMMIT_AUTHOR_EMAIL") ? : realemail
;
297 gecos
= getenv("AUTHOR_NAME") ? : realgecos
;
298 email
= getenv("AUTHOR_EMAIL") ? : realemail
;
299 audate
= getenv("AUTHOR_DATE");
301 parse_rfc2822_date(audate
, date
, sizeof(date
));
303 remove_special(gecos
); remove_special(realgecos
); remove_special(commitgecos
);
304 remove_special(email
); remove_special(realemail
); remove_special(commitemail
);
306 init_buffer(&buffer
, &size
);
307 add_buffer(&buffer
, &size
, "tree %s\n", sha1_to_hex(tree_sha1
));
310 * NOTE! This ordering means that the same exact tree merged with a
311 * different order of parents will be a _different_ changeset even
312 * if everything else stays the same.
314 for (i
= 0; i
< parents
; i
++)
315 add_buffer(&buffer
, &size
, "parent %s\n", sha1_to_hex(parent_sha1
[i
]));
317 /* Person/date information */
318 add_buffer(&buffer
, &size
, "author %s <%s> %s\n", gecos
, email
, date
);
319 add_buffer(&buffer
, &size
, "committer %s <%s> %s\n\n", commitgecos
, commitemail
, realdate
);
321 /* And add the comment */
322 while (fgets(comment
, sizeof(comment
), stdin
) != NULL
)
323 add_buffer(&buffer
, &size
, "%s", comment
);
325 write_sha1_file(buffer
, size
, "commit", commit_sha1
);
326 printf("%s\n", sha1_to_hex(commit_sha1
));