2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
14 #define BLOCKING (1ul << 14)
15 #define ORIG_OFFSET (40)
18 * Leave space at the beginning to insert the tag
19 * once we know how big things are.
21 * FIXME! Share the code with "write-tree.c"
23 static void init_buffer(char **bufp
, unsigned int *sizep
)
25 char *buf
= malloc(BLOCKING
);
26 memset(buf
, 0, ORIG_OFFSET
);
31 static void add_buffer(char **bufp
, unsigned int *sizep
, const char *fmt
, ...)
36 unsigned long alloc
, size
, newsize
;
40 len
= vsnprintf(one_line
, sizeof(one_line
), fmt
, args
);
44 alloc
= (size
+ 32767) & ~32767;
46 if (newsize
> alloc
) {
47 alloc
= (newsize
+ 32767) & ~32767;
48 buf
= realloc(buf
, alloc
);
52 memcpy(buf
+ size
, one_line
, len
);
55 static int prepend_integer(char *buffer
, unsigned val
, int i
)
59 buffer
[--i
] = '0' + (val
% 10);
65 static void finish_buffer(char *tag
, char **bufp
, unsigned int *sizep
)
70 unsigned int size
= *sizep
;
72 offset
= prepend_integer(buf
, size
- ORIG_OFFSET
, ORIG_OFFSET
);
77 memcpy(buf
, tag
, taglen
);
83 static void remove_special(char *p
)
86 char *dst
= p
, *src
= p
;
92 case '\n': case '<': case '>':
101 * Go back, and remove crud from the end: some people
102 * have commas etc in their gecos field
106 unsigned char c
= *dst
;
108 case ',': case ';': case '.':
116 static const char *month_names
[] = {
117 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
118 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
121 static const char *weekday_names
[] = {
122 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
126 static char *skipfws(char *str
)
128 while (isspace(*str
))
134 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
135 (i.e. English) day/month names, and it doesn't work correctly with %z. */
136 static void parse_rfc2822_date(char *date
, char *result
, int maxlen
)
143 memset(&tm
, 0, sizeof(tm
));
148 for (i
=0; i
<7; i
++) {
149 if (!strncmp(p
,weekday_names
[i
],3) && p
[3] == ',') {
159 tm
.tm_mday
= strtoul(p
, &p
, 10);
161 if (tm
.tm_mday
< 1 || tm
.tm_mday
> 31)
171 for (i
=0; i
<12; i
++) {
172 if (!strncmp(p
, month_names
[i
], 3) && isspace(p
[3])) {
174 p
= skipfws(p
+strlen(month_names
[i
]));
178 return; /* Error -- bad month */
182 tm
.tm_year
= strtoul(p
, &p
, 10);
184 if (!tm
.tm_year
&& !isspace(*p
))
187 if (tm
.tm_year
> 1900)
195 tm
.tm_hour
= strtoul(p
, &p
, 10);
197 if (!tm
.tm_hour
> 23)
201 return; /* Error -- bad time */
207 tm
.tm_min
= strtoul(p
, &p
, 10);
216 return; /* Error -- bad time */
222 tm
.tm_sec
= strtoul(p
, &p
, 10);
240 if (!isdigit(p
[1]) || !isdigit(p
[2]) || !isdigit(p
[3]) || !isdigit(p
[4]))
243 i
= strtoul(p
+1, NULL
, 10);
244 offset
*= ((i
% 100) + ((i
/ 100) * 60));
246 if (*(skipfws(p
+ 5)))
249 then
= mktime(&tm
); /* mktime appears to ignore the GMT offset, stupidly */
255 snprintf(result
, maxlen
, "%lu %5.5s", then
, p
);
258 static void check_valid(unsigned char *sha1
, const char *expect
)
264 buf
= read_sha1_file(sha1
, type
, &size
);
265 if (!buf
|| strcmp(type
, expect
))
266 die("%s is not a valid '%s' object", sha1_to_hex(sha1
), expect
);
271 * Having more than two parents may be strange, but hey, there's
272 * no conceptual reason why the file format couldn't accept multi-way
273 * merges. It might be the "union" of several packages, for example.
275 * I don't really expect that to happen, but this is here to make
276 * it clear that _conceptually_ it's ok..
278 #define MAXPARENT (16)
280 int main(int argc
, char **argv
)
284 unsigned char tree_sha1
[20];
285 unsigned char parent_sha1
[MAXPARENT
][20];
286 unsigned char commit_sha1
[20];
287 char *gecos
, *realgecos
, *commitgecos
;
288 char *email
, *commitemail
, realemail
[1000];
289 char date
[20], realdate
[20];
298 if (argc
< 2 || get_sha1_hex(argv
[1], tree_sha1
) < 0)
299 usage("commit-tree <sha1> [-p <sha1>]* < changelog");
301 check_valid(tree_sha1
, "tree");
302 for (i
= 2; i
< argc
; i
+= 2) {
304 a
= argv
[i
]; b
= argv
[i
+1];
305 if (!b
|| strcmp(a
, "-p") || get_sha1_hex(b
, parent_sha1
[parents
]))
306 usage("commit-tree <sha1> [-p <sha1>]* < changelog");
307 check_valid(parent_sha1
[parents
], "commit");
311 fprintf(stderr
, "Committing initial tree %s\n", argv
[1]);
312 pw
= getpwuid(getuid());
314 die("You don't exist. Go away!");
315 realgecos
= pw
->pw_gecos
;
316 len
= strlen(pw
->pw_name
);
317 memcpy(realemail
, pw
->pw_name
, len
);
318 realemail
[len
] = '@';
319 gethostname(realemail
+len
+1, sizeof(realemail
)-len
-1);
320 strcat(realemail
, ".");
321 getdomainname(realemail
+strlen(realemail
), sizeof(realemail
)-strlen(realemail
)-1);
323 tm
= localtime(&now
);
325 strftime(realdate
, sizeof(realdate
), "%s %z", tm
);
326 strcpy(date
, realdate
);
328 commitgecos
= getenv("COMMIT_AUTHOR_NAME") ? : realgecos
;
329 commitemail
= getenv("COMMIT_AUTHOR_EMAIL") ? : realemail
;
330 gecos
= getenv("AUTHOR_NAME") ? : realgecos
;
331 email
= getenv("AUTHOR_EMAIL") ? : realemail
;
332 audate
= getenv("AUTHOR_DATE");
334 parse_rfc2822_date(audate
, date
, sizeof(date
));
336 remove_special(gecos
); remove_special(realgecos
); remove_special(commitgecos
);
337 remove_special(email
); remove_special(realemail
); remove_special(commitemail
);
339 init_buffer(&buffer
, &size
);
340 add_buffer(&buffer
, &size
, "tree %s\n", sha1_to_hex(tree_sha1
));
343 * NOTE! This ordering means that the same exact tree merged with a
344 * different order of parents will be a _different_ changeset even
345 * if everything else stays the same.
347 for (i
= 0; i
< parents
; i
++)
348 add_buffer(&buffer
, &size
, "parent %s\n", sha1_to_hex(parent_sha1
[i
]));
350 /* Person/date information */
351 add_buffer(&buffer
, &size
, "author %s <%s> %s\n", gecos
, email
, date
);
352 add_buffer(&buffer
, &size
, "committer %s <%s> %s\n\n", commitgecos
, commitemail
, realdate
);
354 /* And add the comment */
355 while (fgets(comment
, sizeof(comment
), stdin
) != NULL
)
356 add_buffer(&buffer
, &size
, "%s", comment
);
358 finish_buffer("commit ", &buffer
, &size
);
360 write_sha1_file(buffer
, size
, commit_sha1
);
361 printf("%s\n", sha1_to_hex(commit_sha1
));