]>
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 | 58 | { |
885a86ab | 59 | if (obj->type != TYPE_COMMIT) { |
f76412ed JH |
60 | if (!quiet) |
61 | error("Object %s is a %s, not a commit", | |
885a86ab | 62 | sha1_to_hex(sha1), typename(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) { | |
855419f7 | 87 | struct commit *ret = alloc_commit_node(); |
175785e5 | 88 | created_object(sha1, &ret->object); |
885a86ab | 89 | ret->object.type = TYPE_COMMIT; |
175785e5 DB |
90 | return ret; |
91 | } | |
d1af002d | 92 | if (!obj->type) |
885a86ab | 93 | obj->type = TYPE_COMMIT; |
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 | { |
3b44f15a | 239 | char *tail = buffer; |
f7cc77d7 | 240 | char *bufptr = buffer; |
175785e5 | 241 | unsigned char parent[20]; |
6c88be16 | 242 | struct commit_list **pptr; |
5da5c8f4 | 243 | struct commit_graft *graft; |
27dedf0c | 244 | unsigned n_refs = 0; |
bd2c39f5 | 245 | |
175785e5 DB |
246 | if (item->object.parsed) |
247 | return 0; | |
248 | item->object.parsed = 1; | |
3b44f15a JH |
249 | tail += size; |
250 | if (tail <= bufptr + 5 || memcmp(bufptr, "tree ", 5)) | |
f7cc77d7 | 251 | return error("bogus commit object %s", sha1_to_hex(item->object.sha1)); |
3b44f15a | 252 | if (tail <= bufptr + 45 || get_sha1_hex(bufptr + 5, parent) < 0) |
bd2afde8 JH |
253 | return error("bad tree pointer in commit %s", |
254 | sha1_to_hex(item->object.sha1)); | |
175785e5 | 255 | item->tree = lookup_tree(parent); |
235ac407 | 256 | if (item->tree) |
27dedf0c | 257 | n_refs++; |
175785e5 | 258 | bufptr += 46; /* "tree " + "hex sha1" + "\n" */ |
6c88be16 | 259 | pptr = &item->parents; |
5da5c8f4 JH |
260 | |
261 | graft = lookup_commit_graft(item->object.sha1); | |
3b44f15a | 262 | while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) { |
f7cc77d7 LT |
263 | struct commit *new_parent; |
264 | ||
3b44f15a JH |
265 | if (tail <= bufptr + 48 || |
266 | get_sha1_hex(bufptr + 7, parent) || | |
267 | bufptr[47] != '\n') | |
f7cc77d7 | 268 | return error("bad parents in commit %s", sha1_to_hex(item->object.sha1)); |
5da5c8f4 JH |
269 | bufptr += 48; |
270 | if (graft) | |
271 | continue; | |
f7cc77d7 | 272 | new_parent = lookup_commit(parent); |
235ac407 | 273 | if (new_parent) { |
6c88be16 | 274 | pptr = &commit_list_insert(new_parent, pptr)->next; |
27dedf0c | 275 | n_refs++; |
235ac407 | 276 | } |
5da5c8f4 JH |
277 | } |
278 | if (graft) { | |
279 | int i; | |
280 | struct commit *new_parent; | |
281 | for (i = 0; i < graft->nr_parent; i++) { | |
282 | new_parent = lookup_commit(graft->parent[i]); | |
283 | if (!new_parent) | |
284 | continue; | |
285 | pptr = &commit_list_insert(new_parent, pptr)->next; | |
27dedf0c | 286 | n_refs++; |
5da5c8f4 | 287 | } |
175785e5 DB |
288 | } |
289 | item->date = parse_commit_date(bufptr); | |
27dedf0c JH |
290 | |
291 | if (track_object_refs) { | |
292 | unsigned i = 0; | |
293 | struct commit_list *p; | |
294 | struct object_refs *refs = alloc_object_refs(n_refs); | |
295 | if (item->tree) | |
296 | refs->ref[i++] = &item->tree->object; | |
297 | for (p = item->parents; p; p = p->next) | |
298 | refs->ref[i++] = &p->item->object; | |
299 | set_object_refs(&item->object, refs); | |
300 | } | |
301 | ||
175785e5 DB |
302 | return 0; |
303 | } | |
304 | ||
bd2c39f5 NP |
305 | int parse_commit(struct commit *item) |
306 | { | |
307 | char type[20]; | |
308 | void *buffer; | |
309 | unsigned long size; | |
310 | int ret; | |
311 | ||
312 | if (item->object.parsed) | |
313 | return 0; | |
314 | buffer = read_sha1_file(item->object.sha1, type, &size); | |
315 | if (!buffer) | |
316 | return error("Could not read %s", | |
317 | sha1_to_hex(item->object.sha1)); | |
318 | if (strcmp(type, commit_type)) { | |
319 | free(buffer); | |
320 | return error("Object %s not a commit", | |
321 | sha1_to_hex(item->object.sha1)); | |
322 | } | |
323 | ret = parse_commit_buffer(item, buffer, size); | |
60ab26de | 324 | if (save_commit_buffer && !ret) { |
3ff1fbbb LT |
325 | item->buffer = buffer; |
326 | return 0; | |
327 | } | |
bd2c39f5 NP |
328 | free(buffer); |
329 | return ret; | |
330 | } | |
331 | ||
ac5155ef | 332 | struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p) |
dd97f850 | 333 | { |
812666c8 | 334 | struct commit_list *new_list = xmalloc(sizeof(struct commit_list)); |
dd97f850 DB |
335 | new_list->item = item; |
336 | new_list->next = *list_p; | |
337 | *list_p = new_list; | |
ac5155ef | 338 | return new_list; |
dd97f850 DB |
339 | } |
340 | ||
175785e5 DB |
341 | void free_commit_list(struct commit_list *list) |
342 | { | |
343 | while (list) { | |
344 | struct commit_list *temp = list; | |
345 | list = temp->next; | |
346 | free(temp); | |
347 | } | |
348 | } | |
dd97f850 | 349 | |
f755494c | 350 | struct commit_list * insert_by_date(struct commit *item, struct commit_list **list) |
dd97f850 DB |
351 | { |
352 | struct commit_list **pp = list; | |
353 | struct commit_list *p; | |
354 | while ((p = *pp) != NULL) { | |
355 | if (p->item->date < item->date) { | |
356 | break; | |
357 | } | |
358 | pp = &p->next; | |
359 | } | |
f755494c | 360 | return commit_list_insert(item, pp); |
dd97f850 DB |
361 | } |
362 | ||
363 | ||
364 | void sort_by_date(struct commit_list **list) | |
365 | { | |
366 | struct commit_list *ret = NULL; | |
367 | while (*list) { | |
f755494c | 368 | insert_by_date((*list)->item, &ret); |
dd97f850 DB |
369 | *list = (*list)->next; |
370 | } | |
371 | *list = ret; | |
372 | } | |
373 | ||
58e28af6 DB |
374 | struct commit *pop_most_recent_commit(struct commit_list **list, |
375 | unsigned int mark) | |
dd97f850 DB |
376 | { |
377 | struct commit *ret = (*list)->item; | |
378 | struct commit_list *parents = ret->parents; | |
379 | struct commit_list *old = *list; | |
380 | ||
381 | *list = (*list)->next; | |
382 | free(old); | |
383 | ||
384 | while (parents) { | |
4056c091 | 385 | struct commit *commit = parents->item; |
58e28af6 DB |
386 | parse_commit(commit); |
387 | if (!(commit->object.flags & mark)) { | |
388 | commit->object.flags |= mark; | |
f755494c | 389 | insert_by_date(commit, list); |
4056c091 | 390 | } |
dd97f850 DB |
391 | parents = parents->next; |
392 | } | |
393 | return ret; | |
394 | } | |
e3bc7a3b | 395 | |
f8f9c73c JH |
396 | void clear_commit_marks(struct commit *commit, unsigned int mark) |
397 | { | |
398 | struct commit_list *parents; | |
399 | ||
f8f9c73c | 400 | commit->object.flags &= ~mark; |
58ecf5c1 | 401 | parents = commit->parents; |
f8f9c73c | 402 | while (parents) { |
160b7983 | 403 | struct commit *parent = parents->item; |
58ecf5c1 JH |
404 | |
405 | /* Have we already cleared this? */ | |
406 | if (mark & parent->object.flags) | |
160b7983 | 407 | clear_commit_marks(parent, mark); |
f8f9c73c JH |
408 | parents = parents->next; |
409 | } | |
410 | } | |
411 | ||
e3bc7a3b LT |
412 | /* |
413 | * Generic support for pretty-printing the header | |
414 | */ | |
415 | static int get_one_line(const char *msg, unsigned long len) | |
416 | { | |
417 | int ret = 0; | |
418 | ||
419 | while (len--) { | |
420 | char c = *msg++; | |
684958ae LT |
421 | if (!c) |
422 | break; | |
e3bc7a3b LT |
423 | ret++; |
424 | if (c == '\n') | |
425 | break; | |
e3bc7a3b LT |
426 | } |
427 | return ret; | |
428 | } | |
429 | ||
cdd406e3 JH |
430 | static int is_rfc2047_special(char ch) |
431 | { | |
432 | return ((ch & 0x80) || (ch == '=') || (ch == '?') || (ch == '_')); | |
433 | } | |
434 | ||
435 | static int add_rfc2047(char *buf, const char *line, int len) | |
436 | { | |
437 | char *bp = buf; | |
438 | int i, needquote; | |
439 | static const char q_utf8[] = "=?utf-8?q?"; | |
440 | ||
441 | for (i = needquote = 0; !needquote && i < len; i++) { | |
442 | unsigned ch = line[i]; | |
443 | if (ch & 0x80) | |
444 | needquote++; | |
445 | if ((i + 1 < len) && | |
446 | (ch == '=' && line[i+1] == '?')) | |
447 | needquote++; | |
448 | } | |
449 | if (!needquote) | |
450 | return sprintf(buf, "%.*s", len, line); | |
451 | ||
452 | memcpy(bp, q_utf8, sizeof(q_utf8)-1); | |
453 | bp += sizeof(q_utf8)-1; | |
454 | for (i = 0; i < len; i++) { | |
0da46771 | 455 | unsigned ch = line[i] & 0xFF; |
cdd406e3 JH |
456 | if (is_rfc2047_special(ch)) { |
457 | sprintf(bp, "=%02X", ch); | |
458 | bp += 3; | |
459 | } | |
460 | else if (ch == ' ') | |
461 | *bp++ = '_'; | |
462 | else | |
463 | *bp++ = ch; | |
464 | } | |
465 | memcpy(bp, "?=", 2); | |
466 | bp += 2; | |
467 | return bp - buf; | |
468 | } | |
469 | ||
9b66ec04 | 470 | static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line) |
e3bc7a3b LT |
471 | { |
472 | char *date; | |
5de36bfe | 473 | int namelen; |
e3bc7a3b | 474 | unsigned long time; |
000182ea | 475 | int tz, ret; |
ff56fe1c | 476 | const char *filler = " "; |
e3bc7a3b | 477 | |
d87449c5 JH |
478 | if (fmt == CMIT_FMT_ONELINE) |
479 | return 0; | |
e3bc7a3b LT |
480 | date = strchr(line, '>'); |
481 | if (!date) | |
482 | return 0; | |
483 | namelen = ++date - line; | |
484 | time = strtoul(date, &date, 10); | |
485 | tz = strtol(date, NULL, 10); | |
486 | ||
3eefc189 | 487 | if (fmt == CMIT_FMT_EMAIL) { |
cdd406e3 JH |
488 | char *name_tail = strchr(line, '<'); |
489 | int display_name_length; | |
490 | if (!name_tail) | |
491 | return 0; | |
492 | while (line < name_tail && isspace(name_tail[-1])) | |
493 | name_tail--; | |
494 | display_name_length = name_tail - line; | |
3eefc189 | 495 | filler = ""; |
cdd406e3 JH |
496 | strcpy(buf, "From: "); |
497 | ret = strlen(buf); | |
498 | ret += add_rfc2047(buf + ret, line, display_name_length); | |
499 | memcpy(buf + ret, name_tail, namelen - display_name_length); | |
500 | ret += namelen - display_name_length; | |
501 | buf[ret++] = '\n'; | |
502 | } | |
503 | else { | |
504 | ret = sprintf(buf, "%s: %.*s%.*s\n", what, | |
505 | (fmt == CMIT_FMT_FULLER) ? 4 : 0, | |
506 | filler, namelen, line); | |
3eefc189 | 507 | } |
ff56fe1c JH |
508 | switch (fmt) { |
509 | case CMIT_FMT_MEDIUM: | |
000182ea | 510 | ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz)); |
ff56fe1c | 511 | break; |
3eefc189 | 512 | case CMIT_FMT_EMAIL: |
2a387043 JH |
513 | ret += sprintf(buf + ret, "Date: %s\n", |
514 | show_rfc2822_date(time, tz)); | |
3eefc189 | 515 | break; |
ff56fe1c JH |
516 | case CMIT_FMT_FULLER: |
517 | ret += sprintf(buf + ret, "%sDate: %s\n", what, show_date(time, tz)); | |
518 | break; | |
519 | default: | |
520 | /* notin' */ | |
521 | break; | |
522 | } | |
000182ea LT |
523 | return ret; |
524 | } | |
525 | ||
3eefc189 | 526 | static int is_empty_line(const char *line, int *len_p) |
000182ea | 527 | { |
3eefc189 | 528 | int len = *len_p; |
000182ea LT |
529 | while (len && isspace(line[len-1])) |
530 | len--; | |
3eefc189 | 531 | *len_p = len; |
000182ea | 532 | return !len; |
e3bc7a3b LT |
533 | } |
534 | ||
f2d42275 | 535 | static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *commit, int abbrev) |
28342a5d | 536 | { |
f2d42275 JH |
537 | struct commit_list *parent = commit->parents; |
538 | int offset; | |
d87449c5 | 539 | |
3eefc189 JH |
540 | if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) || |
541 | !parent || !parent->next) | |
f2d42275 JH |
542 | return 0; |
543 | ||
544 | offset = sprintf(buf, "Merge:"); | |
545 | ||
546 | while (parent) { | |
547 | struct commit *p = parent->item; | |
6bfb27a0 JH |
548 | const char *hex = abbrev |
549 | ? find_unique_abbrev(p->object.sha1, abbrev) | |
550 | : sha1_to_hex(p->object.sha1); | |
554fe20d | 551 | const char *dots = (abbrev && strlen(hex) != 40) ? "..." : ""; |
f2d42275 JH |
552 | parent = parent->next; |
553 | ||
6bfb27a0 | 554 | offset += sprintf(buf + offset, " %s%s", hex, dots); |
28342a5d | 555 | } |
f2d42275 | 556 | buf[offset++] = '\n'; |
28342a5d LT |
557 | return offset; |
558 | } | |
559 | ||
698ce6f8 | 560 | 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 | 561 | { |
000182ea | 562 | int hdr = 1, body = 0; |
e3bc7a3b | 563 | unsigned long offset = 0; |
3eefc189 | 564 | int indent = 4; |
f2d42275 | 565 | int parents_shown = 0; |
3815f423 | 566 | const char *msg = commit->buffer; |
c831da66 | 567 | int plain_non_ascii = 0; |
3eefc189 | 568 | |
3eefc189 JH |
569 | if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL) |
570 | indent = 0; | |
e3bc7a3b | 571 | |
c831da66 JH |
572 | /* After-subject is used to pass in Content-Type: multipart |
573 | * MIME header; in that case we do not have to do the | |
574 | * plaintext content type even if the commit message has | |
575 | * non 7-bit ASCII character. Otherwise, check if we need | |
576 | * to say this is not a 7-bit ASCII. | |
577 | */ | |
578 | if (fmt == CMIT_FMT_EMAIL && !after_subject) { | |
0da46771 JH |
579 | int i, ch, in_body; |
580 | ||
581 | for (in_body = i = 0; (ch = msg[i]) && i < len; i++) { | |
582 | if (!in_body) { | |
583 | /* author could be non 7-bit ASCII but | |
584 | * the log may so; skip over the | |
585 | * header part first. | |
586 | */ | |
587 | if (ch == '\n' && | |
588 | i + 1 < len && msg[i+1] == '\n') | |
589 | in_body = 1; | |
590 | } | |
591 | else if (ch & 0x80) { | |
c831da66 | 592 | plain_non_ascii = 1; |
0da46771 JH |
593 | break; |
594 | } | |
595 | } | |
c831da66 JH |
596 | } |
597 | ||
e3bc7a3b LT |
598 | for (;;) { |
599 | const char *line = msg; | |
600 | int linelen = get_one_line(msg, len); | |
601 | ||
602 | if (!linelen) | |
603 | break; | |
604 | ||
605 | /* | |
606 | * We want some slop for indentation and a possible | |
607 | * final "...". Thus the "+ 20". | |
608 | */ | |
609 | if (offset + linelen + 20 > space) { | |
610 | memcpy(buf + offset, " ...\n", 8); | |
611 | offset += 8; | |
612 | break; | |
613 | } | |
614 | ||
615 | msg += linelen; | |
616 | len -= linelen; | |
e3bc7a3b | 617 | if (hdr) { |
000182ea LT |
618 | if (linelen == 1) { |
619 | hdr = 0; | |
3eefc189 | 620 | if ((fmt != CMIT_FMT_ONELINE) && !subject) |
d87449c5 | 621 | buf[offset++] = '\n'; |
000182ea LT |
622 | continue; |
623 | } | |
624 | if (fmt == CMIT_FMT_RAW) { | |
625 | memcpy(buf + offset, line, linelen); | |
626 | offset += linelen; | |
627 | continue; | |
628 | } | |
28342a5d LT |
629 | if (!memcmp(line, "parent ", 7)) { |
630 | if (linelen != 48) | |
631 | die("bad parent line in commit"); | |
f2d42275 | 632 | continue; |
28342a5d | 633 | } |
ff56fe1c | 634 | |
f2d42275 JH |
635 | if (!parents_shown) { |
636 | offset += add_merge_info(fmt, buf + offset, | |
637 | commit, abbrev); | |
638 | parents_shown = 1; | |
639 | continue; | |
640 | } | |
ff56fe1c JH |
641 | /* |
642 | * MEDIUM == DEFAULT shows only author with dates. | |
643 | * FULL shows both authors but not dates. | |
644 | * FULLER shows both authors and dates. | |
645 | */ | |
e3bc7a3b | 646 | if (!memcmp(line, "author ", 7)) |
ff56fe1c JH |
647 | offset += add_user_info("Author", fmt, |
648 | buf + offset, | |
649 | line + 7); | |
650 | if (!memcmp(line, "committer ", 10) && | |
651 | (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) | |
652 | offset += add_user_info("Commit", fmt, | |
653 | buf + offset, | |
654 | line + 10); | |
e3bc7a3b LT |
655 | continue; |
656 | } | |
000182ea | 657 | |
3eefc189 | 658 | if (is_empty_line(line, &linelen)) { |
000182ea LT |
659 | if (!body) |
660 | continue; | |
3eefc189 JH |
661 | if (subject) |
662 | continue; | |
000182ea LT |
663 | if (fmt == CMIT_FMT_SHORT) |
664 | break; | |
665 | } else { | |
666 | body = 1; | |
667 | } | |
d87449c5 | 668 | |
3eefc189 | 669 | if (subject) { |
53f420ef JH |
670 | int slen = strlen(subject); |
671 | memcpy(buf + offset, subject, slen); | |
672 | offset += slen; | |
cdd406e3 JH |
673 | offset += add_rfc2047(buf + offset, line, linelen); |
674 | } | |
675 | else { | |
676 | memset(buf + offset, ' ', indent); | |
677 | memcpy(buf + offset + indent, line, linelen); | |
678 | offset += linelen + indent; | |
3eefc189 | 679 | } |
3eefc189 | 680 | buf[offset++] = '\n'; |
d87449c5 JH |
681 | if (fmt == CMIT_FMT_ONELINE) |
682 | break; | |
c831da66 | 683 | if (subject && plain_non_ascii) { |
cdd406e3 JH |
684 | static const char header[] = |
685 | "Content-Type: text/plain; charset=UTF-8\n" | |
686 | "Content-Transfer-Encoding: 8bit\n"; | |
687 | memcpy(buf + offset, header, sizeof(header)-1); | |
688 | offset += sizeof(header)-1; | |
cdd406e3 | 689 | } |
698ce6f8 JS |
690 | if (after_subject) { |
691 | int slen = strlen(after_subject); | |
692 | if (slen > space - offset - 1) | |
693 | slen = space - offset - 1; | |
694 | memcpy(buf + offset, after_subject, slen); | |
695 | offset += slen; | |
696 | after_subject = NULL; | |
697 | } | |
3eefc189 | 698 | subject = NULL; |
d87449c5 | 699 | } |
40c2fe00 LT |
700 | while (offset && isspace(buf[offset-1])) |
701 | offset--; | |
702 | /* Make sure there is an EOLN for the non-oneline case */ | |
703 | if (fmt != CMIT_FMT_ONELINE) | |
704 | buf[offset++] = '\n'; | |
e3bc7a3b LT |
705 | buf[offset] = '\0'; |
706 | return offset; | |
707 | } | |
a3437b8c JS |
708 | |
709 | struct commit *pop_commit(struct commit_list **stack) | |
710 | { | |
711 | struct commit_list *top = *stack; | |
712 | struct commit *item = top ? top->item : NULL; | |
713 | ||
714 | if (top) { | |
715 | *stack = top->next; | |
716 | free(top); | |
717 | } | |
718 | return item; | |
719 | } | |
720 | ||
721 | int count_parents(struct commit * commit) | |
722 | { | |
723 | int count = 0; | |
724 | struct commit_list * parents = commit->parents; | |
725 | for (count=0;parents; parents=parents->next,count++) | |
726 | ; | |
727 | return count; | |
728 | } | |
729 | ||
6b6dcfc2 FK |
730 | void topo_sort_default_setter(struct commit *c, void *data) |
731 | { | |
d3ff6f55 | 732 | c->util = data; |
6b6dcfc2 FK |
733 | } |
734 | ||
735 | void *topo_sort_default_getter(struct commit *c) | |
736 | { | |
d3ff6f55 | 737 | return c->util; |
6b6dcfc2 FK |
738 | } |
739 | ||
ab580ace JS |
740 | /* |
741 | * Performs an in-place topological sort on the list supplied. | |
742 | */ | |
4c8725f1 | 743 | void sort_in_topological_order(struct commit_list ** list, int lifo) |
6b6dcfc2 FK |
744 | { |
745 | sort_in_topological_order_fn(list, lifo, topo_sort_default_setter, | |
746 | topo_sort_default_getter); | |
747 | } | |
748 | ||
749 | void sort_in_topological_order_fn(struct commit_list ** list, int lifo, | |
750 | topo_sort_set_fn_t setter, | |
751 | topo_sort_get_fn_t getter) | |
ab580ace JS |
752 | { |
753 | struct commit_list * next = *list; | |
2ed02887 | 754 | struct commit_list * work = NULL, **insert; |
ab580ace JS |
755 | struct commit_list ** pptr = list; |
756 | struct sort_node * nodes; | |
757 | struct sort_node * next_nodes; | |
758 | int count = 0; | |
759 | ||
760 | /* determine the size of the list */ | |
761 | while (next) { | |
762 | next = next->next; | |
763 | count++; | |
764 | } | |
7d6fb370 EW |
765 | |
766 | if (!count) | |
767 | return; | |
ab580ace JS |
768 | /* allocate an array to help sort the list */ |
769 | nodes = xcalloc(count, sizeof(*nodes)); | |
770 | /* link the list to the array */ | |
771 | next_nodes = nodes; | |
772 | next=*list; | |
773 | while (next) { | |
774 | next_nodes->list_item = next; | |
6b6dcfc2 | 775 | setter(next->item, next_nodes); |
ab580ace JS |
776 | next_nodes++; |
777 | next = next->next; | |
778 | } | |
779 | /* update the indegree */ | |
780 | next=*list; | |
781 | while (next) { | |
782 | struct commit_list * parents = next->item->parents; | |
783 | while (parents) { | |
784 | struct commit * parent=parents->item; | |
6b6dcfc2 FK |
785 | struct sort_node * pn = (struct sort_node *) getter(parent); |
786 | ||
ab580ace JS |
787 | if (pn) |
788 | pn->indegree++; | |
789 | parents=parents->next; | |
790 | } | |
791 | next=next->next; | |
792 | } | |
793 | /* | |
794 | * find the tips | |
795 | * | |
796 | * tips are nodes not reachable from any other node in the list | |
797 | * | |
798 | * the tips serve as a starting set for the work queue. | |
799 | */ | |
800 | next=*list; | |
2ed02887 | 801 | insert = &work; |
ab580ace | 802 | while (next) { |
6b6dcfc2 | 803 | struct sort_node * node = (struct sort_node *) getter(next->item); |
ab580ace JS |
804 | |
805 | if (node->indegree == 0) { | |
2ed02887 | 806 | insert = &commit_list_insert(next->item, insert)->next; |
ab580ace JS |
807 | } |
808 | next=next->next; | |
809 | } | |
4c8725f1 | 810 | |
ab580ace | 811 | /* process the list in topological order */ |
4c8725f1 JH |
812 | if (!lifo) |
813 | sort_by_date(&work); | |
ab580ace JS |
814 | while (work) { |
815 | struct commit * work_item = pop_commit(&work); | |
6b6dcfc2 | 816 | struct sort_node * work_node = (struct sort_node *) getter(work_item); |
ab580ace JS |
817 | struct commit_list * parents = work_item->parents; |
818 | ||
819 | while (parents) { | |
820 | struct commit * parent=parents->item; | |
6b6dcfc2 FK |
821 | struct sort_node * pn = (struct sort_node *) getter(parent); |
822 | ||
ab580ace | 823 | if (pn) { |
6b6dcfc2 | 824 | /* |
ab580ace JS |
825 | * parents are only enqueued for emission |
826 | * when all their children have been emitted thereby | |
827 | * guaranteeing topological order. | |
828 | */ | |
829 | pn->indegree--; | |
4c8725f1 JH |
830 | if (!pn->indegree) { |
831 | if (!lifo) | |
832 | insert_by_date(parent, &work); | |
833 | else | |
834 | commit_list_insert(parent, &work); | |
835 | } | |
ab580ace JS |
836 | } |
837 | parents=parents->next; | |
838 | } | |
839 | /* | |
840 | * work_item is a commit all of whose children | |
841 | * have already been emitted. we can emit it now. | |
842 | */ | |
843 | *pptr = work_node->list_item; | |
844 | pptr = &(*pptr)->next; | |
845 | *pptr = NULL; | |
6b6dcfc2 | 846 | setter(work_item, NULL); |
ab580ace JS |
847 | } |
848 | free(nodes); | |
849 | } | |
7c6f8aaf JS |
850 | |
851 | /* merge-rebase stuff */ | |
852 | ||
31609c17 RS |
853 | /* bits #0..7 in revision.h */ |
854 | #define PARENT1 (1u<< 8) | |
855 | #define PARENT2 (1u<< 9) | |
542ccefe | 856 | #define STALE (1u<<10) |
f3249438 | 857 | #define RESULT (1u<<11) |
7c6f8aaf JS |
858 | |
859 | static struct commit *interesting(struct commit_list *list) | |
860 | { | |
861 | while (list) { | |
862 | struct commit *commit = list->item; | |
863 | list = list->next; | |
542ccefe | 864 | if (commit->object.flags & STALE) |
7c6f8aaf JS |
865 | continue; |
866 | return commit; | |
867 | } | |
868 | return NULL; | |
869 | } | |
870 | ||
f3249438 | 871 | static struct commit_list *merge_bases(struct commit *one, struct commit *two) |
7c6f8aaf JS |
872 | { |
873 | struct commit_list *list = NULL; | |
874 | struct commit_list *result = NULL; | |
7c6f8aaf | 875 | |
f3249438 JH |
876 | if (one == two) |
877 | /* We do not mark this even with RESULT so we do not | |
878 | * have to clean it up. | |
879 | */ | |
880 | return commit_list_insert(one, &result); | |
7c6f8aaf | 881 | |
f3249438 JH |
882 | parse_commit(one); |
883 | parse_commit(two); | |
7c6f8aaf | 884 | |
f3249438 JH |
885 | one->object.flags |= PARENT1; |
886 | two->object.flags |= PARENT2; | |
887 | insert_by_date(one, &list); | |
888 | insert_by_date(two, &list); | |
7c6f8aaf JS |
889 | |
890 | while (interesting(list)) { | |
f3249438 | 891 | struct commit *commit; |
7c6f8aaf | 892 | struct commit_list *parents; |
f3249438 JH |
893 | struct commit_list *n; |
894 | int flags; | |
7c6f8aaf | 895 | |
f3249438 JH |
896 | commit = list->item; |
897 | n = list->next; | |
898 | free(list); | |
899 | list = n; | |
7c6f8aaf | 900 | |
f3249438 JH |
901 | flags = commit->object.flags & (PARENT1 | PARENT2 | STALE); |
902 | if (flags == (PARENT1 | PARENT2)) { | |
903 | if (!(commit->object.flags & RESULT)) { | |
904 | commit->object.flags |= RESULT; | |
905 | insert_by_date(commit, &result); | |
906 | } | |
542ccefe JH |
907 | /* Mark parents of a found merge stale */ |
908 | flags |= STALE; | |
7c6f8aaf JS |
909 | } |
910 | parents = commit->parents; | |
911 | while (parents) { | |
912 | struct commit *p = parents->item; | |
913 | parents = parents->next; | |
914 | if ((p->object.flags & flags) == flags) | |
915 | continue; | |
916 | parse_commit(p); | |
917 | p->object.flags |= flags; | |
918 | insert_by_date(p, &list); | |
919 | } | |
920 | } | |
921 | ||
f3249438 JH |
922 | /* Clean up the result to remove stale ones */ |
923 | list = result; result = NULL; | |
924 | while (list) { | |
925 | struct commit_list *n = list->next; | |
926 | if (!(list->item->object.flags & STALE)) | |
927 | insert_by_date(list->item, &result); | |
928 | free(list); | |
929 | list = n; | |
930 | } | |
931 | return result; | |
932 | } | |
7c6f8aaf | 933 | |
f3249438 JH |
934 | struct commit_list *get_merge_bases(struct commit *one, |
935 | struct commit *two, | |
936 | int cleanup) | |
937 | { | |
938 | const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT); | |
939 | struct commit_list *list; | |
940 | struct commit **rslt; | |
941 | struct commit_list *result; | |
942 | int cnt, i, j; | |
943 | ||
944 | result = merge_bases(one, two); | |
945 | if (one == two) | |
946 | return result; | |
947 | if (!result || !result->next) { | |
948 | if (cleanup) { | |
949 | clear_commit_marks(one, all_flags); | |
950 | clear_commit_marks(two, all_flags); | |
7c6f8aaf | 951 | } |
f3249438 | 952 | return result; |
7c6f8aaf JS |
953 | } |
954 | ||
f3249438 JH |
955 | /* There are more than one */ |
956 | cnt = 0; | |
957 | list = result; | |
958 | while (list) { | |
959 | list = list->next; | |
960 | cnt++; | |
961 | } | |
962 | rslt = xcalloc(cnt, sizeof(*rslt)); | |
963 | for (list = result, i = 0; list; list = list->next) | |
964 | rslt[i++] = list->item; | |
965 | free_commit_list(result); | |
966 | ||
967 | clear_commit_marks(one, all_flags); | |
968 | clear_commit_marks(two, all_flags); | |
969 | for (i = 0; i < cnt - 1; i++) { | |
970 | for (j = i+1; j < cnt; j++) { | |
971 | if (!rslt[i] || !rslt[j]) | |
972 | continue; | |
973 | result = merge_bases(rslt[i], rslt[j]); | |
974 | clear_commit_marks(rslt[i], all_flags); | |
975 | clear_commit_marks(rslt[j], all_flags); | |
976 | for (list = result; list; list = list->next) { | |
977 | if (rslt[i] == list->item) | |
978 | rslt[i] = NULL; | |
979 | if (rslt[j] == list->item) | |
980 | rslt[j] = NULL; | |
981 | } | |
982 | } | |
58ecf5c1 | 983 | } |
7c6f8aaf | 984 | |
f3249438 JH |
985 | /* Surviving ones in rslt[] are the independent results */ |
986 | result = NULL; | |
987 | for (i = 0; i < cnt; i++) { | |
988 | if (rslt[i]) | |
989 | insert_by_date(rslt[i], &result); | |
990 | } | |
991 | free(rslt); | |
7c6f8aaf JS |
992 | return result; |
993 | } |