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