]>
Commit | Line | Data |
---|---|---|
8f1d2e6f | 1 | #include "cache.h" |
961784ee | 2 | #include "tag.h" |
175785e5 | 3 | #include "commit.h" |
175785e5 | 4 | |
60ab26de LT |
5 | int save_commit_buffer = 1; |
6 | ||
ab580ace JS |
7 | struct sort_node |
8 | { | |
9 | /* | |
10 | * the number of children of the associated commit | |
11 | * that also occur in the list being sorted. | |
12 | */ | |
13 | unsigned int indegree; | |
14 | ||
15 | /* | |
16 | * reference to original list item that we will re-use | |
17 | * on output. | |
18 | */ | |
19 | struct commit_list * list_item; | |
20 | ||
21 | }; | |
22 | ||
175785e5 DB |
23 | const char *commit_type = "commit"; |
24 | ||
6cdfd179 EW |
25 | struct cmt_fmt_map { |
26 | const char *n; | |
27 | size_t cmp_len; | |
28 | enum cmit_fmt v; | |
29 | } cmt_fmts[] = { | |
30 | { "raw", 1, CMIT_FMT_RAW }, | |
31 | { "medium", 1, CMIT_FMT_MEDIUM }, | |
32 | { "short", 1, CMIT_FMT_SHORT }, | |
328b710d | 33 | { "email", 1, CMIT_FMT_EMAIL }, |
6cdfd179 EW |
34 | { "full", 5, CMIT_FMT_FULL }, |
35 | { "fuller", 5, CMIT_FMT_FULLER }, | |
36 | { "oneline", 1, CMIT_FMT_ONELINE }, | |
37 | }; | |
38 | ||
9b66ec04 LT |
39 | enum cmit_fmt get_commit_format(const char *arg) |
40 | { | |
6cdfd179 EW |
41 | int i; |
42 | ||
43 | if (!arg || !*arg) | |
9b66ec04 | 44 | return CMIT_FMT_DEFAULT; |
6cdfd179 EW |
45 | if (*arg == '=') |
46 | arg++; | |
47 | for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) { | |
48 | if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len)) | |
49 | return cmt_fmts[i].v; | |
50 | } | |
51 | ||
52 | die("invalid --pretty format: %s", arg); | |
9b66ec04 LT |
53 | } |
54 | ||
f76412ed JH |
55 | static struct commit *check_commit(struct object *obj, |
56 | const unsigned char *sha1, | |
57 | int quiet) | |
961784ee LT |
58 | { |
59 | if (obj->type != commit_type) { | |
f76412ed JH |
60 | if (!quiet) |
61 | error("Object %s is a %s, not a commit", | |
62 | sha1_to_hex(sha1), obj->type); | |
961784ee LT |
63 | return NULL; |
64 | } | |
65 | return (struct commit *) obj; | |
66 | } | |
67 | ||
f76412ed JH |
68 | struct commit *lookup_commit_reference_gently(const unsigned char *sha1, |
69 | int quiet) | |
961784ee | 70 | { |
9534f40b | 71 | struct object *obj = deref_tag(parse_object(sha1), NULL, 0); |
961784ee LT |
72 | |
73 | if (!obj) | |
74 | return NULL; | |
f76412ed JH |
75 | return check_commit(obj, sha1, quiet); |
76 | } | |
77 | ||
78 | struct commit *lookup_commit_reference(const unsigned char *sha1) | |
79 | { | |
80 | return lookup_commit_reference_gently(sha1, 0); | |
961784ee LT |
81 | } |
82 | ||
5d6ccf5c | 83 | struct commit *lookup_commit(const unsigned char *sha1) |
175785e5 DB |
84 | { |
85 | struct object *obj = lookup_object(sha1); | |
86 | if (!obj) { | |
90321c10 | 87 | struct commit *ret = xcalloc(1, sizeof(struct commit)); |
175785e5 | 88 | created_object(sha1, &ret->object); |
d32987be | 89 | ret->object.type = commit_type; |
175785e5 DB |
90 | return ret; |
91 | } | |
d1af002d NP |
92 | if (!obj->type) |
93 | obj->type = commit_type; | |
f76412ed | 94 | return check_commit(obj, sha1, 0); |
175785e5 DB |
95 | } |
96 | ||
97 | static unsigned long parse_commit_date(const char *buf) | |
98 | { | |
99 | unsigned long date; | |
100 | ||
101 | if (memcmp(buf, "author", 6)) | |
102 | return 0; | |
103 | while (*buf++ != '\n') | |
104 | /* nada */; | |
105 | if (memcmp(buf, "committer", 9)) | |
106 | return 0; | |
107 | while (*buf++ != '>') | |
108 | /* nada */; | |
109 | date = strtoul(buf, NULL, 10); | |
110 | if (date == ULONG_MAX) | |
111 | date = 0; | |
112 | return date; | |
113 | } | |
114 | ||
5040f17e | 115 | static struct commit_graft **commit_graft; |
5da5c8f4 JH |
116 | static int commit_graft_alloc, commit_graft_nr; |
117 | ||
118 | static int commit_graft_pos(const unsigned char *sha1) | |
119 | { | |
120 | int lo, hi; | |
121 | lo = 0; | |
122 | hi = commit_graft_nr; | |
123 | while (lo < hi) { | |
124 | int mi = (lo + hi) / 2; | |
125 | struct commit_graft *graft = commit_graft[mi]; | |
126 | int cmp = memcmp(sha1, graft->sha1, 20); | |
127 | if (!cmp) | |
128 | return mi; | |
129 | if (cmp < 0) | |
130 | hi = mi; | |
131 | else | |
132 | lo = mi + 1; | |
133 | } | |
134 | return -lo - 1; | |
135 | } | |
136 | ||
5040f17e JH |
137 | int register_commit_graft(struct commit_graft *graft, int ignore_dups) |
138 | { | |
139 | int pos = commit_graft_pos(graft->sha1); | |
140 | ||
141 | if (0 <= pos) { | |
142 | if (ignore_dups) | |
143 | free(graft); | |
144 | else { | |
145 | free(commit_graft[pos]); | |
146 | commit_graft[pos] = graft; | |
147 | } | |
148 | return 1; | |
149 | } | |
150 | pos = -pos - 1; | |
151 | if (commit_graft_alloc <= ++commit_graft_nr) { | |
152 | commit_graft_alloc = alloc_nr(commit_graft_alloc); | |
153 | commit_graft = xrealloc(commit_graft, | |
154 | sizeof(*commit_graft) * | |
155 | commit_graft_alloc); | |
156 | } | |
157 | if (pos < commit_graft_nr) | |
158 | memmove(commit_graft + pos + 1, | |
159 | commit_graft + pos, | |
160 | (commit_graft_nr - pos - 1) * | |
161 | sizeof(*commit_graft)); | |
162 | commit_graft[pos] = graft; | |
163 | return 0; | |
164 | } | |
165 | ||
166 | struct commit_graft *read_graft_line(char *buf, int len) | |
167 | { | |
168 | /* The format is just "Commit Parent1 Parent2 ...\n" */ | |
169 | int i; | |
170 | struct commit_graft *graft = NULL; | |
171 | ||
172 | if (buf[len-1] == '\n') | |
173 | buf[--len] = 0; | |
360204c3 | 174 | if (buf[0] == '#' || buf[0] == '\0') |
5bc4ce58 | 175 | return NULL; |
5040f17e JH |
176 | if ((len + 1) % 41) { |
177 | bad_graft_data: | |
178 | error("bad graft data: %s", buf); | |
179 | free(graft); | |
180 | return NULL; | |
181 | } | |
182 | i = (len + 1) / 41 - 1; | |
183 | graft = xmalloc(sizeof(*graft) + 20 * i); | |
184 | graft->nr_parent = i; | |
185 | if (get_sha1_hex(buf, graft->sha1)) | |
186 | goto bad_graft_data; | |
187 | for (i = 40; i < len; i += 41) { | |
188 | if (buf[i] != ' ') | |
189 | goto bad_graft_data; | |
190 | if (get_sha1_hex(buf + i + 1, graft->parent[i/41])) | |
191 | goto bad_graft_data; | |
192 | } | |
193 | return graft; | |
194 | } | |
195 | ||
196 | int read_graft_file(const char *graft_file) | |
5da5c8f4 | 197 | { |
5da5c8f4 JH |
198 | FILE *fp = fopen(graft_file, "r"); |
199 | char buf[1024]; | |
5040f17e JH |
200 | if (!fp) |
201 | return -1; | |
5da5c8f4 JH |
202 | while (fgets(buf, sizeof(buf), fp)) { |
203 | /* The format is just "Commit Parent1 Parent2 ...\n" */ | |
204 | int len = strlen(buf); | |
5040f17e | 205 | struct commit_graft *graft = read_graft_line(buf, len); |
5bc4ce58 JH |
206 | if (!graft) |
207 | continue; | |
5040f17e | 208 | if (register_commit_graft(graft, 1)) |
5da5c8f4 | 209 | error("duplicate graft data: %s", buf); |
5da5c8f4 JH |
210 | } |
211 | fclose(fp); | |
5040f17e JH |
212 | return 0; |
213 | } | |
214 | ||
215 | static void prepare_commit_graft(void) | |
216 | { | |
217 | static int commit_graft_prepared; | |
218 | char *graft_file; | |
219 | ||
220 | if (commit_graft_prepared) | |
221 | return; | |
222 | graft_file = get_graft_file(); | |
223 | read_graft_file(graft_file); | |
224 | commit_graft_prepared = 1; | |
5da5c8f4 JH |
225 | } |
226 | ||
227 | static struct commit_graft *lookup_commit_graft(const unsigned char *sha1) | |
228 | { | |
229 | int pos; | |
5040f17e | 230 | prepare_commit_graft(); |
5da5c8f4 JH |
231 | pos = commit_graft_pos(sha1); |
232 | if (pos < 0) | |
233 | return NULL; | |
234 | return commit_graft[pos]; | |
235 | } | |
236 | ||
bd2c39f5 | 237 | int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size) |
175785e5 | 238 | { |
f7cc77d7 | 239 | char *bufptr = buffer; |
175785e5 | 240 | unsigned char parent[20]; |
6c88be16 | 241 | struct commit_list **pptr; |
5da5c8f4 | 242 | struct commit_graft *graft; |
27dedf0c | 243 | unsigned n_refs = 0; |
bd2c39f5 | 244 | |
175785e5 DB |
245 | if (item->object.parsed) |
246 | return 0; | |
247 | item->object.parsed = 1; | |
f7cc77d7 LT |
248 | if (memcmp(bufptr, "tree ", 5)) |
249 | return error("bogus commit object %s", sha1_to_hex(item->object.sha1)); | |
250 | if (get_sha1_hex(bufptr + 5, parent) < 0) | |
bd2afde8 JH |
251 | return error("bad tree pointer in commit %s", |
252 | sha1_to_hex(item->object.sha1)); | |
175785e5 | 253 | item->tree = lookup_tree(parent); |
235ac407 | 254 | if (item->tree) |
27dedf0c | 255 | n_refs++; |
175785e5 | 256 | bufptr += 46; /* "tree " + "hex sha1" + "\n" */ |
6c88be16 | 257 | pptr = &item->parents; |
5da5c8f4 JH |
258 | |
259 | graft = lookup_commit_graft(item->object.sha1); | |
f7cc77d7 LT |
260 | while (!memcmp(bufptr, "parent ", 7)) { |
261 | struct commit *new_parent; | |
262 | ||
263 | if (get_sha1_hex(bufptr + 7, parent) || bufptr[47] != '\n') | |
264 | return error("bad parents in commit %s", sha1_to_hex(item->object.sha1)); | |
5da5c8f4 JH |
265 | bufptr += 48; |
266 | if (graft) | |
267 | continue; | |
f7cc77d7 | 268 | new_parent = lookup_commit(parent); |
235ac407 | 269 | if (new_parent) { |
6c88be16 | 270 | pptr = &commit_list_insert(new_parent, pptr)->next; |
27dedf0c | 271 | n_refs++; |
235ac407 | 272 | } |
5da5c8f4 JH |
273 | } |
274 | if (graft) { | |
275 | int i; | |
276 | struct commit *new_parent; | |
277 | for (i = 0; i < graft->nr_parent; i++) { | |
278 | new_parent = lookup_commit(graft->parent[i]); | |
279 | if (!new_parent) | |
280 | continue; | |
281 | pptr = &commit_list_insert(new_parent, pptr)->next; | |
27dedf0c | 282 | n_refs++; |
5da5c8f4 | 283 | } |
175785e5 DB |
284 | } |
285 | item->date = parse_commit_date(bufptr); | |
27dedf0c JH |
286 | |
287 | if (track_object_refs) { | |
288 | unsigned i = 0; | |
289 | struct commit_list *p; | |
290 | struct object_refs *refs = alloc_object_refs(n_refs); | |
291 | if (item->tree) | |
292 | refs->ref[i++] = &item->tree->object; | |
293 | for (p = item->parents; p; p = p->next) | |
294 | refs->ref[i++] = &p->item->object; | |
295 | set_object_refs(&item->object, refs); | |
296 | } | |
297 | ||
175785e5 DB |
298 | return 0; |
299 | } | |
300 | ||
bd2c39f5 NP |
301 | int parse_commit(struct commit *item) |
302 | { | |
303 | char type[20]; | |
304 | void *buffer; | |
305 | unsigned long size; | |
306 | int ret; | |
307 | ||
308 | if (item->object.parsed) | |
309 | return 0; | |
310 | buffer = read_sha1_file(item->object.sha1, type, &size); | |
311 | if (!buffer) | |
312 | return error("Could not read %s", | |
313 | sha1_to_hex(item->object.sha1)); | |
314 | if (strcmp(type, commit_type)) { | |
315 | free(buffer); | |
316 | return error("Object %s not a commit", | |
317 | sha1_to_hex(item->object.sha1)); | |
318 | } | |
319 | ret = parse_commit_buffer(item, buffer, size); | |
60ab26de | 320 | if (save_commit_buffer && !ret) { |
3ff1fbbb LT |
321 | item->buffer = buffer; |
322 | return 0; | |
323 | } | |
bd2c39f5 NP |
324 | free(buffer); |
325 | return ret; | |
326 | } | |
327 | ||
ac5155ef | 328 | struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p) |
dd97f850 | 329 | { |
812666c8 | 330 | struct commit_list *new_list = xmalloc(sizeof(struct commit_list)); |
dd97f850 DB |
331 | new_list->item = item; |
332 | new_list->next = *list_p; | |
333 | *list_p = new_list; | |
ac5155ef | 334 | return new_list; |
dd97f850 DB |
335 | } |
336 | ||
175785e5 DB |
337 | void free_commit_list(struct commit_list *list) |
338 | { | |
339 | while (list) { | |
340 | struct commit_list *temp = list; | |
341 | list = temp->next; | |
342 | free(temp); | |
343 | } | |
344 | } | |
dd97f850 | 345 | |
f755494c | 346 | struct commit_list * insert_by_date(struct commit *item, struct commit_list **list) |
dd97f850 DB |
347 | { |
348 | struct commit_list **pp = list; | |
349 | struct commit_list *p; | |
350 | while ((p = *pp) != NULL) { | |
351 | if (p->item->date < item->date) { | |
352 | break; | |
353 | } | |
354 | pp = &p->next; | |
355 | } | |
f755494c | 356 | return commit_list_insert(item, pp); |
dd97f850 DB |
357 | } |
358 | ||
359 | ||
360 | void sort_by_date(struct commit_list **list) | |
361 | { | |
362 | struct commit_list *ret = NULL; | |
363 | while (*list) { | |
f755494c | 364 | insert_by_date((*list)->item, &ret); |
dd97f850 DB |
365 | *list = (*list)->next; |
366 | } | |
367 | *list = ret; | |
368 | } | |
369 | ||
58e28af6 DB |
370 | struct commit *pop_most_recent_commit(struct commit_list **list, |
371 | unsigned int mark) | |
dd97f850 DB |
372 | { |
373 | struct commit *ret = (*list)->item; | |
374 | struct commit_list *parents = ret->parents; | |
375 | struct commit_list *old = *list; | |
376 | ||
377 | *list = (*list)->next; | |
378 | free(old); | |
379 | ||
380 | while (parents) { | |
4056c091 | 381 | struct commit *commit = parents->item; |
58e28af6 DB |
382 | parse_commit(commit); |
383 | if (!(commit->object.flags & mark)) { | |
384 | commit->object.flags |= mark; | |
f755494c | 385 | insert_by_date(commit, list); |
4056c091 | 386 | } |
dd97f850 DB |
387 | parents = parents->next; |
388 | } | |
389 | return ret; | |
390 | } | |
e3bc7a3b | 391 | |
f8f9c73c JH |
392 | void clear_commit_marks(struct commit *commit, unsigned int mark) |
393 | { | |
394 | struct commit_list *parents; | |
395 | ||
396 | parents = commit->parents; | |
397 | commit->object.flags &= ~mark; | |
398 | while (parents) { | |
181dc776 JH |
399 | struct commit *parent = parents->item; |
400 | if (parent && parent->object.parsed && | |
401 | (parent->object.flags & mark)) | |
402 | clear_commit_marks(parent, mark); | |
f8f9c73c JH |
403 | parents = parents->next; |
404 | } | |
405 | } | |
406 | ||
e3bc7a3b LT |
407 | /* |
408 | * Generic support for pretty-printing the header | |
409 | */ | |
410 | static int get_one_line(const char *msg, unsigned long len) | |
411 | { | |
412 | int ret = 0; | |
413 | ||
414 | while (len--) { | |
415 | char c = *msg++; | |
684958ae LT |
416 | if (!c) |
417 | break; | |
e3bc7a3b LT |
418 | ret++; |
419 | if (c == '\n') | |
420 | break; | |
e3bc7a3b LT |
421 | } |
422 | return ret; | |
423 | } | |
424 | ||
cdd406e3 JH |
425 | static int is_rfc2047_special(char ch) |
426 | { | |
427 | return ((ch & 0x80) || (ch == '=') || (ch == '?') || (ch == '_')); | |
428 | } | |
429 | ||
430 | static int add_rfc2047(char *buf, const char *line, int len) | |
431 | { | |
432 | char *bp = buf; | |
433 | int i, needquote; | |
434 | static const char q_utf8[] = "=?utf-8?q?"; | |
435 | ||
436 | for (i = needquote = 0; !needquote && i < len; i++) { | |
437 | unsigned ch = line[i]; | |
438 | if (ch & 0x80) | |
439 | needquote++; | |
440 | if ((i + 1 < len) && | |
441 | (ch == '=' && line[i+1] == '?')) | |
442 | needquote++; | |
443 | } | |
444 | if (!needquote) | |
445 | return sprintf(buf, "%.*s", len, line); | |
446 | ||
447 | memcpy(bp, q_utf8, sizeof(q_utf8)-1); | |
448 | bp += sizeof(q_utf8)-1; | |
449 | for (i = 0; i < len; i++) { | |
450 | unsigned ch = line[i]; | |
451 | if (is_rfc2047_special(ch)) { | |
452 | sprintf(bp, "=%02X", ch); | |
453 | bp += 3; | |
454 | } | |
455 | else if (ch == ' ') | |
456 | *bp++ = '_'; | |
457 | else | |
458 | *bp++ = ch; | |
459 | } | |
460 | memcpy(bp, "?=", 2); | |
461 | bp += 2; | |
462 | return bp - buf; | |
463 | } | |
464 | ||
9b66ec04 | 465 | static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line) |
e3bc7a3b LT |
466 | { |
467 | char *date; | |
5de36bfe | 468 | int namelen; |
e3bc7a3b | 469 | unsigned long time; |
000182ea | 470 | int tz, ret; |
ff56fe1c | 471 | const char *filler = " "; |
e3bc7a3b | 472 | |
d87449c5 JH |
473 | if (fmt == CMIT_FMT_ONELINE) |
474 | return 0; | |
e3bc7a3b LT |
475 | date = strchr(line, '>'); |
476 | if (!date) | |
477 | return 0; | |
478 | namelen = ++date - line; | |
479 | time = strtoul(date, &date, 10); | |
480 | tz = strtol(date, NULL, 10); | |
481 | ||
3eefc189 | 482 | if (fmt == CMIT_FMT_EMAIL) { |
cdd406e3 JH |
483 | char *name_tail = strchr(line, '<'); |
484 | int display_name_length; | |
485 | if (!name_tail) | |
486 | return 0; | |
487 | while (line < name_tail && isspace(name_tail[-1])) | |
488 | name_tail--; | |
489 | display_name_length = name_tail - line; | |
3eefc189 | 490 | filler = ""; |
cdd406e3 JH |
491 | strcpy(buf, "From: "); |
492 | ret = strlen(buf); | |
493 | ret += add_rfc2047(buf + ret, line, display_name_length); | |
494 | memcpy(buf + ret, name_tail, namelen - display_name_length); | |
495 | ret += namelen - display_name_length; | |
496 | buf[ret++] = '\n'; | |
497 | } | |
498 | else { | |
499 | ret = sprintf(buf, "%s: %.*s%.*s\n", what, | |
500 | (fmt == CMIT_FMT_FULLER) ? 4 : 0, | |
501 | filler, namelen, line); | |
3eefc189 | 502 | } |
ff56fe1c JH |
503 | switch (fmt) { |
504 | case CMIT_FMT_MEDIUM: | |
000182ea | 505 | ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz)); |
ff56fe1c | 506 | break; |
3eefc189 | 507 | case CMIT_FMT_EMAIL: |
2a387043 JH |
508 | ret += sprintf(buf + ret, "Date: %s\n", |
509 | show_rfc2822_date(time, tz)); | |
3eefc189 | 510 | break; |
ff56fe1c JH |
511 | case CMIT_FMT_FULLER: |
512 | ret += sprintf(buf + ret, "%sDate: %s\n", what, show_date(time, tz)); | |
513 | break; | |
514 | default: | |
515 | /* notin' */ | |
516 | break; | |
517 | } | |
000182ea LT |
518 | return ret; |
519 | } | |
520 | ||
3eefc189 | 521 | static int is_empty_line(const char *line, int *len_p) |
000182ea | 522 | { |
3eefc189 | 523 | int len = *len_p; |
000182ea LT |
524 | while (len && isspace(line[len-1])) |
525 | len--; | |
3eefc189 | 526 | *len_p = len; |
000182ea | 527 | return !len; |
e3bc7a3b LT |
528 | } |
529 | ||
f2d42275 | 530 | static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *commit, int abbrev) |
28342a5d | 531 | { |
f2d42275 JH |
532 | struct commit_list *parent = commit->parents; |
533 | int offset; | |
d87449c5 | 534 | |
3eefc189 JH |
535 | if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) || |
536 | !parent || !parent->next) | |
f2d42275 JH |
537 | return 0; |
538 | ||
539 | offset = sprintf(buf, "Merge:"); | |
540 | ||
541 | while (parent) { | |
542 | struct commit *p = parent->item; | |
6bfb27a0 JH |
543 | const char *hex = abbrev |
544 | ? find_unique_abbrev(p->object.sha1, abbrev) | |
545 | : sha1_to_hex(p->object.sha1); | |
546 | char *dots = (abbrev && strlen(hex) != 40) ? "..." : ""; | |
f2d42275 JH |
547 | parent = parent->next; |
548 | ||
6bfb27a0 | 549 | offset += sprintf(buf + offset, " %s%s", hex, dots); |
28342a5d | 550 | } |
f2d42275 | 551 | buf[offset++] = '\n'; |
28342a5d LT |
552 | return offset; |
553 | } | |
554 | ||
698ce6f8 | 555 | unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, unsigned long len, char *buf, unsigned long space, int abbrev, const char *subject, const char *after_subject) |
e3bc7a3b | 556 | { |
000182ea | 557 | int hdr = 1, body = 0; |
e3bc7a3b | 558 | unsigned long offset = 0; |
3eefc189 | 559 | int indent = 4; |
f2d42275 | 560 | int parents_shown = 0; |
3815f423 | 561 | const char *msg = commit->buffer; |
c831da66 | 562 | int plain_non_ascii = 0; |
3eefc189 | 563 | |
3eefc189 JH |
564 | if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL) |
565 | indent = 0; | |
e3bc7a3b | 566 | |
c831da66 JH |
567 | /* After-subject is used to pass in Content-Type: multipart |
568 | * MIME header; in that case we do not have to do the | |
569 | * plaintext content type even if the commit message has | |
570 | * non 7-bit ASCII character. Otherwise, check if we need | |
571 | * to say this is not a 7-bit ASCII. | |
572 | */ | |
573 | if (fmt == CMIT_FMT_EMAIL && !after_subject) { | |
574 | int i; | |
575 | for (i = 0; !plain_non_ascii && msg[i] && i < len; i++) | |
576 | if (msg[i] & 0x80) | |
577 | plain_non_ascii = 1; | |
578 | } | |
579 | ||
e3bc7a3b LT |
580 | for (;;) { |
581 | const char *line = msg; | |
582 | int linelen = get_one_line(msg, len); | |
583 | ||
584 | if (!linelen) | |
585 | break; | |
586 | ||
587 | /* | |
588 | * We want some slop for indentation and a possible | |
589 | * final "...". Thus the "+ 20". | |
590 | */ | |
591 | if (offset + linelen + 20 > space) { | |
592 | memcpy(buf + offset, " ...\n", 8); | |
593 | offset += 8; | |
594 | break; | |
595 | } | |
596 | ||
597 | msg += linelen; | |
598 | len -= linelen; | |
e3bc7a3b | 599 | if (hdr) { |
000182ea LT |
600 | if (linelen == 1) { |
601 | hdr = 0; | |
3eefc189 | 602 | if ((fmt != CMIT_FMT_ONELINE) && !subject) |
d87449c5 | 603 | buf[offset++] = '\n'; |
000182ea LT |
604 | continue; |
605 | } | |
606 | if (fmt == CMIT_FMT_RAW) { | |
607 | memcpy(buf + offset, line, linelen); | |
608 | offset += linelen; | |
609 | continue; | |
610 | } | |
28342a5d LT |
611 | if (!memcmp(line, "parent ", 7)) { |
612 | if (linelen != 48) | |
613 | die("bad parent line in commit"); | |
f2d42275 | 614 | continue; |
28342a5d | 615 | } |
ff56fe1c | 616 | |
f2d42275 JH |
617 | if (!parents_shown) { |
618 | offset += add_merge_info(fmt, buf + offset, | |
619 | commit, abbrev); | |
620 | parents_shown = 1; | |
621 | continue; | |
622 | } | |
ff56fe1c JH |
623 | /* |
624 | * MEDIUM == DEFAULT shows only author with dates. | |
625 | * FULL shows both authors but not dates. | |
626 | * FULLER shows both authors and dates. | |
627 | */ | |
e3bc7a3b | 628 | if (!memcmp(line, "author ", 7)) |
ff56fe1c JH |
629 | offset += add_user_info("Author", fmt, |
630 | buf + offset, | |
631 | line + 7); | |
632 | if (!memcmp(line, "committer ", 10) && | |
633 | (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) | |
634 | offset += add_user_info("Commit", fmt, | |
635 | buf + offset, | |
636 | line + 10); | |
e3bc7a3b LT |
637 | continue; |
638 | } | |
000182ea | 639 | |
3eefc189 | 640 | if (is_empty_line(line, &linelen)) { |
000182ea LT |
641 | if (!body) |
642 | continue; | |
3eefc189 JH |
643 | if (subject) |
644 | continue; | |
000182ea LT |
645 | if (fmt == CMIT_FMT_SHORT) |
646 | break; | |
647 | } else { | |
648 | body = 1; | |
649 | } | |
d87449c5 | 650 | |
3eefc189 | 651 | if (subject) { |
53f420ef JH |
652 | int slen = strlen(subject); |
653 | memcpy(buf + offset, subject, slen); | |
654 | offset += slen; | |
cdd406e3 JH |
655 | offset += add_rfc2047(buf + offset, line, linelen); |
656 | } | |
657 | else { | |
658 | memset(buf + offset, ' ', indent); | |
659 | memcpy(buf + offset + indent, line, linelen); | |
660 | offset += linelen + indent; | |
3eefc189 | 661 | } |
3eefc189 | 662 | buf[offset++] = '\n'; |
d87449c5 JH |
663 | if (fmt == CMIT_FMT_ONELINE) |
664 | break; | |
c831da66 | 665 | if (subject && plain_non_ascii) { |
cdd406e3 JH |
666 | static const char header[] = |
667 | "Content-Type: text/plain; charset=UTF-8\n" | |
668 | "Content-Transfer-Encoding: 8bit\n"; | |
669 | memcpy(buf + offset, header, sizeof(header)-1); | |
670 | offset += sizeof(header)-1; | |
cdd406e3 | 671 | } |
698ce6f8 JS |
672 | if (after_subject) { |
673 | int slen = strlen(after_subject); | |
674 | if (slen > space - offset - 1) | |
675 | slen = space - offset - 1; | |
676 | memcpy(buf + offset, after_subject, slen); | |
677 | offset += slen; | |
678 | after_subject = NULL; | |
679 | } | |
3eefc189 | 680 | subject = NULL; |
d87449c5 | 681 | } |
40c2fe00 LT |
682 | while (offset && isspace(buf[offset-1])) |
683 | offset--; | |
684 | /* Make sure there is an EOLN for the non-oneline case */ | |
685 | if (fmt != CMIT_FMT_ONELINE) | |
686 | buf[offset++] = '\n'; | |
e3bc7a3b LT |
687 | buf[offset] = '\0'; |
688 | return offset; | |
689 | } | |
a3437b8c JS |
690 | |
691 | struct commit *pop_commit(struct commit_list **stack) | |
692 | { | |
693 | struct commit_list *top = *stack; | |
694 | struct commit *item = top ? top->item : NULL; | |
695 | ||
696 | if (top) { | |
697 | *stack = top->next; | |
698 | free(top); | |
699 | } | |
700 | return item; | |
701 | } | |
702 | ||
703 | int count_parents(struct commit * commit) | |
704 | { | |
705 | int count = 0; | |
706 | struct commit_list * parents = commit->parents; | |
707 | for (count=0;parents; parents=parents->next,count++) | |
708 | ; | |
709 | return count; | |
710 | } | |
711 | ||
6b6dcfc2 FK |
712 | void topo_sort_default_setter(struct commit *c, void *data) |
713 | { | |
714 | c->object.util = data; | |
715 | } | |
716 | ||
717 | void *topo_sort_default_getter(struct commit *c) | |
718 | { | |
719 | return c->object.util; | |
720 | } | |
721 | ||
ab580ace JS |
722 | /* |
723 | * Performs an in-place topological sort on the list supplied. | |
724 | */ | |
4c8725f1 | 725 | void sort_in_topological_order(struct commit_list ** list, int lifo) |
6b6dcfc2 FK |
726 | { |
727 | sort_in_topological_order_fn(list, lifo, topo_sort_default_setter, | |
728 | topo_sort_default_getter); | |
729 | } | |
730 | ||
731 | void sort_in_topological_order_fn(struct commit_list ** list, int lifo, | |
732 | topo_sort_set_fn_t setter, | |
733 | topo_sort_get_fn_t getter) | |
ab580ace JS |
734 | { |
735 | struct commit_list * next = *list; | |
2ed02887 | 736 | struct commit_list * work = NULL, **insert; |
ab580ace JS |
737 | struct commit_list ** pptr = list; |
738 | struct sort_node * nodes; | |
739 | struct sort_node * next_nodes; | |
740 | int count = 0; | |
741 | ||
742 | /* determine the size of the list */ | |
743 | while (next) { | |
744 | next = next->next; | |
745 | count++; | |
746 | } | |
7d6fb370 EW |
747 | |
748 | if (!count) | |
749 | return; | |
ab580ace JS |
750 | /* allocate an array to help sort the list */ |
751 | nodes = xcalloc(count, sizeof(*nodes)); | |
752 | /* link the list to the array */ | |
753 | next_nodes = nodes; | |
754 | next=*list; | |
755 | while (next) { | |
756 | next_nodes->list_item = next; | |
6b6dcfc2 | 757 | setter(next->item, next_nodes); |
ab580ace JS |
758 | next_nodes++; |
759 | next = next->next; | |
760 | } | |
761 | /* update the indegree */ | |
762 | next=*list; | |
763 | while (next) { | |
764 | struct commit_list * parents = next->item->parents; | |
765 | while (parents) { | |
766 | struct commit * parent=parents->item; | |
6b6dcfc2 FK |
767 | struct sort_node * pn = (struct sort_node *) getter(parent); |
768 | ||
ab580ace JS |
769 | if (pn) |
770 | pn->indegree++; | |
771 | parents=parents->next; | |
772 | } | |
773 | next=next->next; | |
774 | } | |
775 | /* | |
776 | * find the tips | |
777 | * | |
778 | * tips are nodes not reachable from any other node in the list | |
779 | * | |
780 | * the tips serve as a starting set for the work queue. | |
781 | */ | |
782 | next=*list; | |
2ed02887 | 783 | insert = &work; |
ab580ace | 784 | while (next) { |
6b6dcfc2 | 785 | struct sort_node * node = (struct sort_node *) getter(next->item); |
ab580ace JS |
786 | |
787 | if (node->indegree == 0) { | |
2ed02887 | 788 | insert = &commit_list_insert(next->item, insert)->next; |
ab580ace JS |
789 | } |
790 | next=next->next; | |
791 | } | |
4c8725f1 | 792 | |
ab580ace | 793 | /* process the list in topological order */ |
4c8725f1 JH |
794 | if (!lifo) |
795 | sort_by_date(&work); | |
ab580ace JS |
796 | while (work) { |
797 | struct commit * work_item = pop_commit(&work); | |
6b6dcfc2 | 798 | struct sort_node * work_node = (struct sort_node *) getter(work_item); |
ab580ace JS |
799 | struct commit_list * parents = work_item->parents; |
800 | ||
801 | while (parents) { | |
802 | struct commit * parent=parents->item; | |
6b6dcfc2 FK |
803 | struct sort_node * pn = (struct sort_node *) getter(parent); |
804 | ||
ab580ace | 805 | if (pn) { |
6b6dcfc2 | 806 | /* |
ab580ace JS |
807 | * parents are only enqueued for emission |
808 | * when all their children have been emitted thereby | |
809 | * guaranteeing topological order. | |
810 | */ | |
811 | pn->indegree--; | |
4c8725f1 JH |
812 | if (!pn->indegree) { |
813 | if (!lifo) | |
814 | insert_by_date(parent, &work); | |
815 | else | |
816 | commit_list_insert(parent, &work); | |
817 | } | |
ab580ace JS |
818 | } |
819 | parents=parents->next; | |
820 | } | |
821 | /* | |
822 | * work_item is a commit all of whose children | |
823 | * have already been emitted. we can emit it now. | |
824 | */ | |
825 | *pptr = work_node->list_item; | |
826 | pptr = &(*pptr)->next; | |
827 | *pptr = NULL; | |
6b6dcfc2 | 828 | setter(work_item, NULL); |
ab580ace JS |
829 | } |
830 | free(nodes); | |
831 | } |