]>
Commit | Line | Data |
---|---|---|
8f1d2e6f | 1 | #include "cache.h" |
961784ee | 2 | #include "tag.h" |
175785e5 | 3 | #include "commit.h" |
ed09aef0 | 4 | #include "pkt-line.h" |
52883fbd | 5 | #include "utf8.h" |
e52a5de4 | 6 | #include "interpolate.h" |
199c45bf JH |
7 | #include "diff.h" |
8 | #include "revision.h" | |
175785e5 | 9 | |
60ab26de LT |
10 | int save_commit_buffer = 1; |
11 | ||
ab580ace JS |
12 | struct sort_node |
13 | { | |
14 | /* | |
55c3eb43 TS |
15 | * the number of children of the associated commit |
16 | * that also occur in the list being sorted. | |
17 | */ | |
ab580ace JS |
18 | unsigned int indegree; |
19 | ||
20 | /* | |
55c3eb43 TS |
21 | * reference to original list item that we will re-use |
22 | * on output. | |
23 | */ | |
ab580ace JS |
24 | struct commit_list * list_item; |
25 | ||
26 | }; | |
27 | ||
175785e5 DB |
28 | const char *commit_type = "commit"; |
29 | ||
6cdfd179 EW |
30 | struct cmt_fmt_map { |
31 | const char *n; | |
32 | size_t cmp_len; | |
33 | enum cmit_fmt v; | |
34 | } cmt_fmts[] = { | |
35 | { "raw", 1, CMIT_FMT_RAW }, | |
36 | { "medium", 1, CMIT_FMT_MEDIUM }, | |
37 | { "short", 1, CMIT_FMT_SHORT }, | |
328b710d | 38 | { "email", 1, CMIT_FMT_EMAIL }, |
6cdfd179 EW |
39 | { "full", 5, CMIT_FMT_FULL }, |
40 | { "fuller", 5, CMIT_FMT_FULLER }, | |
41 | { "oneline", 1, CMIT_FMT_ONELINE }, | |
e52a5de4 | 42 | { "format:", 7, CMIT_FMT_USERFORMAT}, |
6cdfd179 EW |
43 | }; |
44 | ||
e52a5de4 JS |
45 | static char *user_format; |
46 | ||
9b66ec04 LT |
47 | enum cmit_fmt get_commit_format(const char *arg) |
48 | { | |
6cdfd179 EW |
49 | int i; |
50 | ||
51 | if (!arg || !*arg) | |
9b66ec04 | 52 | return CMIT_FMT_DEFAULT; |
6cdfd179 EW |
53 | if (*arg == '=') |
54 | arg++; | |
e52a5de4 JS |
55 | if (!prefixcmp(arg, "format:")) { |
56 | if (user_format) | |
57 | free(user_format); | |
58 | user_format = xstrdup(arg + 7); | |
59 | return CMIT_FMT_USERFORMAT; | |
60 | } | |
6cdfd179 | 61 | for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) { |
b6936205 EW |
62 | if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len) && |
63 | !strncmp(arg, cmt_fmts[i].n, strlen(arg))) | |
6cdfd179 EW |
64 | return cmt_fmts[i].v; |
65 | } | |
66 | ||
67 | die("invalid --pretty format: %s", arg); | |
9b66ec04 LT |
68 | } |
69 | ||
f76412ed JH |
70 | static struct commit *check_commit(struct object *obj, |
71 | const unsigned char *sha1, | |
72 | int quiet) | |
961784ee | 73 | { |
1974632c | 74 | if (obj->type != OBJ_COMMIT) { |
f76412ed JH |
75 | if (!quiet) |
76 | error("Object %s is a %s, not a commit", | |
885a86ab | 77 | sha1_to_hex(sha1), typename(obj->type)); |
961784ee LT |
78 | return NULL; |
79 | } | |
80 | return (struct commit *) obj; | |
81 | } | |
82 | ||
f76412ed JH |
83 | struct commit *lookup_commit_reference_gently(const unsigned char *sha1, |
84 | int quiet) | |
961784ee | 85 | { |
9534f40b | 86 | struct object *obj = deref_tag(parse_object(sha1), NULL, 0); |
961784ee LT |
87 | |
88 | if (!obj) | |
89 | return NULL; | |
f76412ed JH |
90 | return check_commit(obj, sha1, quiet); |
91 | } | |
92 | ||
93 | struct commit *lookup_commit_reference(const unsigned char *sha1) | |
94 | { | |
95 | return lookup_commit_reference_gently(sha1, 0); | |
961784ee LT |
96 | } |
97 | ||
5d6ccf5c | 98 | struct commit *lookup_commit(const unsigned char *sha1) |
175785e5 DB |
99 | { |
100 | struct object *obj = lookup_object(sha1); | |
101 | if (!obj) { | |
855419f7 | 102 | struct commit *ret = alloc_commit_node(); |
175785e5 | 103 | created_object(sha1, &ret->object); |
1974632c | 104 | ret->object.type = OBJ_COMMIT; |
175785e5 DB |
105 | return ret; |
106 | } | |
d1af002d | 107 | if (!obj->type) |
1974632c | 108 | obj->type = OBJ_COMMIT; |
f76412ed | 109 | return check_commit(obj, sha1, 0); |
175785e5 DB |
110 | } |
111 | ||
112 | static unsigned long parse_commit_date(const char *buf) | |
113 | { | |
114 | unsigned long date; | |
115 | ||
116 | if (memcmp(buf, "author", 6)) | |
117 | return 0; | |
118 | while (*buf++ != '\n') | |
119 | /* nada */; | |
120 | if (memcmp(buf, "committer", 9)) | |
121 | return 0; | |
122 | while (*buf++ != '>') | |
123 | /* nada */; | |
124 | date = strtoul(buf, NULL, 10); | |
125 | if (date == ULONG_MAX) | |
126 | date = 0; | |
127 | return date; | |
128 | } | |
129 | ||
5040f17e | 130 | static struct commit_graft **commit_graft; |
5da5c8f4 JH |
131 | static int commit_graft_alloc, commit_graft_nr; |
132 | ||
133 | static int commit_graft_pos(const unsigned char *sha1) | |
134 | { | |
135 | int lo, hi; | |
136 | lo = 0; | |
137 | hi = commit_graft_nr; | |
138 | while (lo < hi) { | |
139 | int mi = (lo + hi) / 2; | |
140 | struct commit_graft *graft = commit_graft[mi]; | |
a89fccd2 | 141 | int cmp = hashcmp(sha1, graft->sha1); |
5da5c8f4 JH |
142 | if (!cmp) |
143 | return mi; | |
144 | if (cmp < 0) | |
145 | hi = mi; | |
146 | else | |
147 | lo = mi + 1; | |
148 | } | |
149 | return -lo - 1; | |
150 | } | |
151 | ||
5040f17e JH |
152 | int register_commit_graft(struct commit_graft *graft, int ignore_dups) |
153 | { | |
154 | int pos = commit_graft_pos(graft->sha1); | |
155 | ||
156 | if (0 <= pos) { | |
157 | if (ignore_dups) | |
158 | free(graft); | |
159 | else { | |
160 | free(commit_graft[pos]); | |
161 | commit_graft[pos] = graft; | |
162 | } | |
163 | return 1; | |
164 | } | |
165 | pos = -pos - 1; | |
166 | if (commit_graft_alloc <= ++commit_graft_nr) { | |
167 | commit_graft_alloc = alloc_nr(commit_graft_alloc); | |
168 | commit_graft = xrealloc(commit_graft, | |
169 | sizeof(*commit_graft) * | |
170 | commit_graft_alloc); | |
171 | } | |
172 | if (pos < commit_graft_nr) | |
173 | memmove(commit_graft + pos + 1, | |
174 | commit_graft + pos, | |
175 | (commit_graft_nr - pos - 1) * | |
176 | sizeof(*commit_graft)); | |
177 | commit_graft[pos] = graft; | |
178 | return 0; | |
179 | } | |
180 | ||
181 | struct commit_graft *read_graft_line(char *buf, int len) | |
182 | { | |
183 | /* The format is just "Commit Parent1 Parent2 ...\n" */ | |
184 | int i; | |
185 | struct commit_graft *graft = NULL; | |
186 | ||
187 | if (buf[len-1] == '\n') | |
188 | buf[--len] = 0; | |
360204c3 | 189 | if (buf[0] == '#' || buf[0] == '\0') |
5bc4ce58 | 190 | return NULL; |
5040f17e JH |
191 | if ((len + 1) % 41) { |
192 | bad_graft_data: | |
193 | error("bad graft data: %s", buf); | |
194 | free(graft); | |
195 | return NULL; | |
196 | } | |
197 | i = (len + 1) / 41 - 1; | |
198 | graft = xmalloc(sizeof(*graft) + 20 * i); | |
199 | graft->nr_parent = i; | |
200 | if (get_sha1_hex(buf, graft->sha1)) | |
201 | goto bad_graft_data; | |
202 | for (i = 40; i < len; i += 41) { | |
203 | if (buf[i] != ' ') | |
204 | goto bad_graft_data; | |
205 | if (get_sha1_hex(buf + i + 1, graft->parent[i/41])) | |
206 | goto bad_graft_data; | |
207 | } | |
208 | return graft; | |
209 | } | |
210 | ||
211 | int read_graft_file(const char *graft_file) | |
5da5c8f4 | 212 | { |
5da5c8f4 JH |
213 | FILE *fp = fopen(graft_file, "r"); |
214 | char buf[1024]; | |
5040f17e JH |
215 | if (!fp) |
216 | return -1; | |
5da5c8f4 JH |
217 | while (fgets(buf, sizeof(buf), fp)) { |
218 | /* The format is just "Commit Parent1 Parent2 ...\n" */ | |
219 | int len = strlen(buf); | |
5040f17e | 220 | struct commit_graft *graft = read_graft_line(buf, len); |
5bc4ce58 JH |
221 | if (!graft) |
222 | continue; | |
5040f17e | 223 | if (register_commit_graft(graft, 1)) |
5da5c8f4 | 224 | error("duplicate graft data: %s", buf); |
5da5c8f4 JH |
225 | } |
226 | fclose(fp); | |
5040f17e JH |
227 | return 0; |
228 | } | |
229 | ||
230 | static void prepare_commit_graft(void) | |
231 | { | |
232 | static int commit_graft_prepared; | |
233 | char *graft_file; | |
234 | ||
235 | if (commit_graft_prepared) | |
236 | return; | |
237 | graft_file = get_graft_file(); | |
238 | read_graft_file(graft_file); | |
ed09aef0 JS |
239 | /* make sure shallows are read */ |
240 | is_repository_shallow(); | |
5040f17e | 241 | commit_graft_prepared = 1; |
5da5c8f4 JH |
242 | } |
243 | ||
244 | static struct commit_graft *lookup_commit_graft(const unsigned char *sha1) | |
245 | { | |
246 | int pos; | |
5040f17e | 247 | prepare_commit_graft(); |
5da5c8f4 JH |
248 | pos = commit_graft_pos(sha1); |
249 | if (pos < 0) | |
250 | return NULL; | |
251 | return commit_graft[pos]; | |
252 | } | |
253 | ||
ed09aef0 JS |
254 | int write_shallow_commits(int fd, int use_pack_protocol) |
255 | { | |
256 | int i, count = 0; | |
257 | for (i = 0; i < commit_graft_nr; i++) | |
258 | if (commit_graft[i]->nr_parent < 0) { | |
259 | const char *hex = | |
260 | sha1_to_hex(commit_graft[i]->sha1); | |
261 | count++; | |
262 | if (use_pack_protocol) | |
263 | packet_write(fd, "shallow %s", hex); | |
264 | else { | |
93822c22 AW |
265 | if (write_in_full(fd, hex, 40) != 40) |
266 | break; | |
267 | if (write_in_full(fd, "\n", 1) != 1) | |
268 | break; | |
ed09aef0 JS |
269 | } |
270 | } | |
271 | return count; | |
272 | } | |
273 | ||
f53514bc JS |
274 | int unregister_shallow(const unsigned char *sha1) |
275 | { | |
276 | int pos = commit_graft_pos(sha1); | |
277 | if (pos < 0) | |
278 | return -1; | |
279 | if (pos + 1 < commit_graft_nr) | |
280 | memcpy(commit_graft + pos, commit_graft + pos + 1, | |
281 | sizeof(struct commit_graft *) | |
282 | * (commit_graft_nr - pos - 1)); | |
283 | commit_graft_nr--; | |
284 | return 0; | |
285 | } | |
286 | ||
bd2c39f5 | 287 | int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size) |
175785e5 | 288 | { |
3b44f15a | 289 | char *tail = buffer; |
f7cc77d7 | 290 | char *bufptr = buffer; |
175785e5 | 291 | unsigned char parent[20]; |
6c88be16 | 292 | struct commit_list **pptr; |
5da5c8f4 | 293 | struct commit_graft *graft; |
27dedf0c | 294 | unsigned n_refs = 0; |
bd2c39f5 | 295 | |
175785e5 DB |
296 | if (item->object.parsed) |
297 | return 0; | |
298 | item->object.parsed = 1; | |
3b44f15a JH |
299 | tail += size; |
300 | if (tail <= bufptr + 5 || memcmp(bufptr, "tree ", 5)) | |
f7cc77d7 | 301 | return error("bogus commit object %s", sha1_to_hex(item->object.sha1)); |
3b44f15a | 302 | if (tail <= bufptr + 45 || get_sha1_hex(bufptr + 5, parent) < 0) |
bd2afde8 JH |
303 | return error("bad tree pointer in commit %s", |
304 | sha1_to_hex(item->object.sha1)); | |
175785e5 | 305 | item->tree = lookup_tree(parent); |
235ac407 | 306 | if (item->tree) |
27dedf0c | 307 | n_refs++; |
175785e5 | 308 | bufptr += 46; /* "tree " + "hex sha1" + "\n" */ |
6c88be16 | 309 | pptr = &item->parents; |
5da5c8f4 JH |
310 | |
311 | graft = lookup_commit_graft(item->object.sha1); | |
3b44f15a | 312 | while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) { |
f7cc77d7 LT |
313 | struct commit *new_parent; |
314 | ||
3b44f15a JH |
315 | if (tail <= bufptr + 48 || |
316 | get_sha1_hex(bufptr + 7, parent) || | |
317 | bufptr[47] != '\n') | |
f7cc77d7 | 318 | return error("bad parents in commit %s", sha1_to_hex(item->object.sha1)); |
5da5c8f4 JH |
319 | bufptr += 48; |
320 | if (graft) | |
321 | continue; | |
f7cc77d7 | 322 | new_parent = lookup_commit(parent); |
235ac407 | 323 | if (new_parent) { |
6c88be16 | 324 | pptr = &commit_list_insert(new_parent, pptr)->next; |
27dedf0c | 325 | n_refs++; |
235ac407 | 326 | } |
5da5c8f4 JH |
327 | } |
328 | if (graft) { | |
329 | int i; | |
330 | struct commit *new_parent; | |
331 | for (i = 0; i < graft->nr_parent; i++) { | |
332 | new_parent = lookup_commit(graft->parent[i]); | |
333 | if (!new_parent) | |
334 | continue; | |
335 | pptr = &commit_list_insert(new_parent, pptr)->next; | |
27dedf0c | 336 | n_refs++; |
5da5c8f4 | 337 | } |
175785e5 DB |
338 | } |
339 | item->date = parse_commit_date(bufptr); | |
27dedf0c JH |
340 | |
341 | if (track_object_refs) { | |
342 | unsigned i = 0; | |
343 | struct commit_list *p; | |
344 | struct object_refs *refs = alloc_object_refs(n_refs); | |
345 | if (item->tree) | |
346 | refs->ref[i++] = &item->tree->object; | |
347 | for (p = item->parents; p; p = p->next) | |
348 | refs->ref[i++] = &p->item->object; | |
349 | set_object_refs(&item->object, refs); | |
350 | } | |
351 | ||
175785e5 DB |
352 | return 0; |
353 | } | |
354 | ||
bd2c39f5 NP |
355 | int parse_commit(struct commit *item) |
356 | { | |
21666f1a | 357 | enum object_type type; |
bd2c39f5 NP |
358 | void *buffer; |
359 | unsigned long size; | |
360 | int ret; | |
361 | ||
362 | if (item->object.parsed) | |
363 | return 0; | |
21666f1a | 364 | buffer = read_sha1_file(item->object.sha1, &type, &size); |
bd2c39f5 NP |
365 | if (!buffer) |
366 | return error("Could not read %s", | |
367 | sha1_to_hex(item->object.sha1)); | |
21666f1a | 368 | if (type != OBJ_COMMIT) { |
bd2c39f5 NP |
369 | free(buffer); |
370 | return error("Object %s not a commit", | |
371 | sha1_to_hex(item->object.sha1)); | |
372 | } | |
373 | ret = parse_commit_buffer(item, buffer, size); | |
60ab26de | 374 | if (save_commit_buffer && !ret) { |
3ff1fbbb LT |
375 | item->buffer = buffer; |
376 | return 0; | |
377 | } | |
bd2c39f5 NP |
378 | free(buffer); |
379 | return ret; | |
380 | } | |
381 | ||
ac5155ef | 382 | struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p) |
dd97f850 | 383 | { |
812666c8 | 384 | struct commit_list *new_list = xmalloc(sizeof(struct commit_list)); |
dd97f850 DB |
385 | new_list->item = item; |
386 | new_list->next = *list_p; | |
387 | *list_p = new_list; | |
ac5155ef | 388 | return new_list; |
dd97f850 DB |
389 | } |
390 | ||
175785e5 DB |
391 | void free_commit_list(struct commit_list *list) |
392 | { | |
393 | while (list) { | |
394 | struct commit_list *temp = list; | |
395 | list = temp->next; | |
396 | free(temp); | |
397 | } | |
398 | } | |
dd97f850 | 399 | |
f755494c | 400 | struct commit_list * insert_by_date(struct commit *item, struct commit_list **list) |
dd97f850 DB |
401 | { |
402 | struct commit_list **pp = list; | |
403 | struct commit_list *p; | |
404 | while ((p = *pp) != NULL) { | |
405 | if (p->item->date < item->date) { | |
406 | break; | |
407 | } | |
408 | pp = &p->next; | |
409 | } | |
f755494c | 410 | return commit_list_insert(item, pp); |
dd97f850 DB |
411 | } |
412 | ||
413 | ||
414 | void sort_by_date(struct commit_list **list) | |
415 | { | |
416 | struct commit_list *ret = NULL; | |
417 | while (*list) { | |
f755494c | 418 | insert_by_date((*list)->item, &ret); |
dd97f850 DB |
419 | *list = (*list)->next; |
420 | } | |
421 | *list = ret; | |
422 | } | |
423 | ||
58e28af6 DB |
424 | struct commit *pop_most_recent_commit(struct commit_list **list, |
425 | unsigned int mark) | |
dd97f850 DB |
426 | { |
427 | struct commit *ret = (*list)->item; | |
428 | struct commit_list *parents = ret->parents; | |
429 | struct commit_list *old = *list; | |
430 | ||
431 | *list = (*list)->next; | |
432 | free(old); | |
433 | ||
434 | while (parents) { | |
4056c091 | 435 | struct commit *commit = parents->item; |
58e28af6 DB |
436 | parse_commit(commit); |
437 | if (!(commit->object.flags & mark)) { | |
438 | commit->object.flags |= mark; | |
f755494c | 439 | insert_by_date(commit, list); |
4056c091 | 440 | } |
dd97f850 DB |
441 | parents = parents->next; |
442 | } | |
443 | return ret; | |
444 | } | |
e3bc7a3b | 445 | |
f8f9c73c JH |
446 | void clear_commit_marks(struct commit *commit, unsigned int mark) |
447 | { | |
448 | struct commit_list *parents; | |
449 | ||
f8f9c73c | 450 | commit->object.flags &= ~mark; |
58ecf5c1 | 451 | parents = commit->parents; |
f8f9c73c | 452 | while (parents) { |
160b7983 | 453 | struct commit *parent = parents->item; |
58ecf5c1 JH |
454 | |
455 | /* Have we already cleared this? */ | |
456 | if (mark & parent->object.flags) | |
160b7983 | 457 | clear_commit_marks(parent, mark); |
f8f9c73c JH |
458 | parents = parents->next; |
459 | } | |
460 | } | |
461 | ||
e3bc7a3b LT |
462 | /* |
463 | * Generic support for pretty-printing the header | |
464 | */ | |
465 | static int get_one_line(const char *msg, unsigned long len) | |
466 | { | |
467 | int ret = 0; | |
468 | ||
469 | while (len--) { | |
470 | char c = *msg++; | |
684958ae LT |
471 | if (!c) |
472 | break; | |
e3bc7a3b LT |
473 | ret++; |
474 | if (c == '\n') | |
475 | break; | |
e3bc7a3b LT |
476 | } |
477 | return ret; | |
478 | } | |
479 | ||
f7e68b29 JH |
480 | /* High bit set, or ISO-2022-INT */ |
481 | static int non_ascii(int ch) | |
482 | { | |
483 | ch = (ch & 0xff); | |
484 | return ((ch & 0x80) || (ch == 0x1b)); | |
485 | } | |
486 | ||
cdd406e3 JH |
487 | static int is_rfc2047_special(char ch) |
488 | { | |
f7e68b29 | 489 | return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_')); |
cdd406e3 JH |
490 | } |
491 | ||
f7e68b29 JH |
492 | static int add_rfc2047(char *buf, const char *line, int len, |
493 | const char *encoding) | |
cdd406e3 JH |
494 | { |
495 | char *bp = buf; | |
496 | int i, needquote; | |
f7e68b29 JH |
497 | char q_encoding[128]; |
498 | const char *q_encoding_fmt = "=?%s?q?"; | |
cdd406e3 JH |
499 | |
500 | for (i = needquote = 0; !needquote && i < len; i++) { | |
f7e68b29 JH |
501 | int ch = line[i]; |
502 | if (non_ascii(ch)) | |
cdd406e3 JH |
503 | needquote++; |
504 | if ((i + 1 < len) && | |
505 | (ch == '=' && line[i+1] == '?')) | |
506 | needquote++; | |
507 | } | |
508 | if (!needquote) | |
509 | return sprintf(buf, "%.*s", len, line); | |
510 | ||
f7e68b29 JH |
511 | i = snprintf(q_encoding, sizeof(q_encoding), q_encoding_fmt, encoding); |
512 | if (sizeof(q_encoding) < i) | |
513 | die("Insanely long encoding name %s", encoding); | |
514 | memcpy(bp, q_encoding, i); | |
515 | bp += i; | |
cdd406e3 | 516 | for (i = 0; i < len; i++) { |
0da46771 | 517 | unsigned ch = line[i] & 0xFF; |
cdd406e3 JH |
518 | if (is_rfc2047_special(ch)) { |
519 | sprintf(bp, "=%02X", ch); | |
520 | bp += 3; | |
521 | } | |
522 | else if (ch == ' ') | |
523 | *bp++ = '_'; | |
524 | else | |
525 | *bp++ = ch; | |
526 | } | |
527 | memcpy(bp, "?=", 2); | |
528 | bp += 2; | |
529 | return bp - buf; | |
530 | } | |
531 | ||
3dfb9278 | 532 | static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, |
f7e68b29 JH |
533 | const char *line, int relative_date, |
534 | const char *encoding) | |
e3bc7a3b LT |
535 | { |
536 | char *date; | |
5de36bfe | 537 | int namelen; |
e3bc7a3b | 538 | unsigned long time; |
000182ea | 539 | int tz, ret; |
ff56fe1c | 540 | const char *filler = " "; |
e3bc7a3b | 541 | |
d87449c5 JH |
542 | if (fmt == CMIT_FMT_ONELINE) |
543 | return 0; | |
e3bc7a3b LT |
544 | date = strchr(line, '>'); |
545 | if (!date) | |
546 | return 0; | |
547 | namelen = ++date - line; | |
548 | time = strtoul(date, &date, 10); | |
549 | tz = strtol(date, NULL, 10); | |
550 | ||
3eefc189 | 551 | if (fmt == CMIT_FMT_EMAIL) { |
cdd406e3 JH |
552 | char *name_tail = strchr(line, '<'); |
553 | int display_name_length; | |
554 | if (!name_tail) | |
555 | return 0; | |
556 | while (line < name_tail && isspace(name_tail[-1])) | |
557 | name_tail--; | |
558 | display_name_length = name_tail - line; | |
3eefc189 | 559 | filler = ""; |
cdd406e3 JH |
560 | strcpy(buf, "From: "); |
561 | ret = strlen(buf); | |
f7e68b29 JH |
562 | ret += add_rfc2047(buf + ret, line, display_name_length, |
563 | encoding); | |
cdd406e3 JH |
564 | memcpy(buf + ret, name_tail, namelen - display_name_length); |
565 | ret += namelen - display_name_length; | |
566 | buf[ret++] = '\n'; | |
567 | } | |
568 | else { | |
569 | ret = sprintf(buf, "%s: %.*s%.*s\n", what, | |
570 | (fmt == CMIT_FMT_FULLER) ? 4 : 0, | |
571 | filler, namelen, line); | |
3eefc189 | 572 | } |
ff56fe1c JH |
573 | switch (fmt) { |
574 | case CMIT_FMT_MEDIUM: | |
3dfb9278 JF |
575 | ret += sprintf(buf + ret, "Date: %s\n", |
576 | show_date(time, tz, relative_date)); | |
ff56fe1c | 577 | break; |
3eefc189 | 578 | case CMIT_FMT_EMAIL: |
2a387043 JH |
579 | ret += sprintf(buf + ret, "Date: %s\n", |
580 | show_rfc2822_date(time, tz)); | |
3eefc189 | 581 | break; |
ff56fe1c | 582 | case CMIT_FMT_FULLER: |
3dfb9278 JF |
583 | ret += sprintf(buf + ret, "%sDate: %s\n", what, |
584 | show_date(time, tz, relative_date)); | |
ff56fe1c JH |
585 | break; |
586 | default: | |
587 | /* notin' */ | |
588 | break; | |
589 | } | |
000182ea LT |
590 | return ret; |
591 | } | |
592 | ||
3eefc189 | 593 | static int is_empty_line(const char *line, int *len_p) |
000182ea | 594 | { |
3eefc189 | 595 | int len = *len_p; |
000182ea LT |
596 | while (len && isspace(line[len-1])) |
597 | len--; | |
3eefc189 | 598 | *len_p = len; |
000182ea | 599 | return !len; |
e3bc7a3b LT |
600 | } |
601 | ||
f2d42275 | 602 | static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *commit, int abbrev) |
28342a5d | 603 | { |
f2d42275 JH |
604 | struct commit_list *parent = commit->parents; |
605 | int offset; | |
d87449c5 | 606 | |
3eefc189 JH |
607 | if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) || |
608 | !parent || !parent->next) | |
f2d42275 JH |
609 | return 0; |
610 | ||
611 | offset = sprintf(buf, "Merge:"); | |
612 | ||
613 | while (parent) { | |
614 | struct commit *p = parent->item; | |
14763e7b EW |
615 | const char *hex = NULL; |
616 | const char *dots; | |
617 | if (abbrev) | |
618 | hex = find_unique_abbrev(p->object.sha1, abbrev); | |
619 | if (!hex) | |
620 | hex = sha1_to_hex(p->object.sha1); | |
621 | dots = (abbrev && strlen(hex) != 40) ? "..." : ""; | |
f2d42275 JH |
622 | parent = parent->next; |
623 | ||
6bfb27a0 | 624 | offset += sprintf(buf + offset, " %s%s", hex, dots); |
28342a5d | 625 | } |
f2d42275 | 626 | buf[offset++] = '\n'; |
28342a5d LT |
627 | return offset; |
628 | } | |
629 | ||
52883fbd JH |
630 | static char *get_header(const struct commit *commit, const char *key) |
631 | { | |
632 | int key_len = strlen(key); | |
633 | const char *line = commit->buffer; | |
634 | ||
635 | for (;;) { | |
636 | const char *eol = strchr(line, '\n'), *next; | |
637 | ||
638 | if (line == eol) | |
639 | return NULL; | |
640 | if (!eol) { | |
641 | eol = line + strlen(line); | |
642 | next = NULL; | |
643 | } else | |
644 | next = eol + 1; | |
645 | if (!strncmp(line, key, key_len) && line[key_len] == ' ') { | |
646 | int len = eol - line - key_len; | |
647 | char *ret = xmalloc(len); | |
648 | memcpy(ret, line + key_len + 1, len - 1); | |
649 | ret[len - 1] = '\0'; | |
650 | return ret; | |
651 | } | |
652 | line = next; | |
653 | } | |
654 | } | |
655 | ||
3a55602e | 656 | static char *replace_encoding_header(char *buf, const char *encoding) |
53af9816 JH |
657 | { |
658 | char *encoding_header = strstr(buf, "\nencoding "); | |
d0e50cb4 | 659 | char *header_end = strstr(buf, "\n\n"); |
53af9816 JH |
660 | char *end_of_encoding_header; |
661 | int encoding_header_pos; | |
662 | int encoding_header_len; | |
663 | int new_len; | |
664 | int need_len; | |
665 | int buflen = strlen(buf) + 1; | |
666 | ||
d0e50cb4 JK |
667 | if (!header_end) |
668 | header_end = buf + buflen; | |
669 | if (!encoding_header || encoding_header >= header_end) | |
670 | return buf; | |
53af9816 JH |
671 | encoding_header++; |
672 | end_of_encoding_header = strchr(encoding_header, '\n'); | |
673 | if (!end_of_encoding_header) | |
674 | return buf; /* should not happen but be defensive */ | |
675 | end_of_encoding_header++; | |
676 | ||
677 | encoding_header_len = end_of_encoding_header - encoding_header; | |
678 | encoding_header_pos = encoding_header - buf; | |
679 | ||
680 | if (is_encoding_utf8(encoding)) { | |
681 | /* we have re-coded to UTF-8; drop the header */ | |
682 | memmove(encoding_header, end_of_encoding_header, | |
683 | buflen - (encoding_header_pos + encoding_header_len)); | |
684 | return buf; | |
685 | } | |
686 | new_len = strlen(encoding); | |
687 | need_len = new_len + strlen("encoding \n"); | |
688 | if (encoding_header_len < need_len) { | |
689 | buf = xrealloc(buf, buflen + (need_len - encoding_header_len)); | |
690 | encoding_header = buf + encoding_header_pos; | |
691 | end_of_encoding_header = encoding_header + encoding_header_len; | |
692 | } | |
693 | memmove(end_of_encoding_header + (need_len - encoding_header_len), | |
694 | end_of_encoding_header, | |
695 | buflen - (encoding_header_pos + encoding_header_len)); | |
696 | memcpy(encoding_header + 9, encoding, strlen(encoding)); | |
697 | encoding_header[9 + new_len] = '\n'; | |
698 | return buf; | |
699 | } | |
700 | ||
f7e68b29 | 701 | static char *logmsg_reencode(const struct commit *commit, |
3a55602e | 702 | const char *output_encoding) |
52883fbd | 703 | { |
3a55602e SP |
704 | static const char *utf8 = "utf-8"; |
705 | const char *use_encoding; | |
d2c11a38 | 706 | char *encoding; |
52883fbd JH |
707 | char *out; |
708 | ||
f7e68b29 | 709 | if (!*output_encoding) |
52883fbd | 710 | return NULL; |
d2c11a38 | 711 | encoding = get_header(commit, "encoding"); |
3a55602e SP |
712 | use_encoding = encoding ? encoding : utf8; |
713 | if (!strcmp(use_encoding, output_encoding)) | |
567fb65e | 714 | out = xstrdup(commit->buffer); |
e90068a9 JH |
715 | else |
716 | out = reencode_string(commit->buffer, | |
3a55602e | 717 | output_encoding, use_encoding); |
53af9816 JH |
718 | if (out) |
719 | out = replace_encoding_header(out, output_encoding); | |
720 | ||
3a55602e | 721 | free(encoding); |
52883fbd JH |
722 | return out; |
723 | } | |
724 | ||
e52a5de4 JS |
725 | static char *xstrndup(const char *text, int len) |
726 | { | |
727 | char *result = xmalloc(len + 1); | |
728 | memcpy(result, text, len); | |
729 | result[len] = '\0'; | |
730 | return result; | |
731 | } | |
732 | ||
733 | static void fill_person(struct interp *table, const char *msg, int len) | |
734 | { | |
735 | int start, end, tz = 0; | |
736 | unsigned long date; | |
737 | char *ep; | |
738 | ||
739 | /* parse name */ | |
740 | for (end = 0; end < len && msg[end] != '<'; end++) | |
741 | ; /* do nothing */ | |
742 | start = end + 1; | |
743 | while (end > 0 && isspace(msg[end - 1])) | |
744 | end--; | |
745 | table[0].value = xstrndup(msg, end); | |
746 | ||
747 | if (start >= len) | |
748 | return; | |
749 | ||
750 | /* parse email */ | |
751 | for (end = start + 1; end < len && msg[end] != '>'; end++) | |
752 | ; /* do nothing */ | |
753 | ||
754 | if (end >= len) | |
755 | return; | |
756 | ||
757 | table[1].value = xstrndup(msg + start, end - start); | |
758 | ||
759 | /* parse date */ | |
760 | for (start = end + 1; start < len && isspace(msg[start]); start++) | |
761 | ; /* do nothing */ | |
762 | if (start >= len) | |
763 | return; | |
764 | date = strtoul(msg + start, &ep, 10); | |
765 | if (msg + start == ep) | |
766 | return; | |
767 | ||
4621af37 | 768 | table[5].value = xstrndup(msg + start, ep - (msg + start)); |
e52a5de4 JS |
769 | |
770 | /* parse tz */ | |
771 | for (start = ep - msg + 1; start < len && isspace(msg[start]); start++) | |
772 | ; /* do nothing */ | |
773 | if (start + 1 < len) { | |
774 | tz = strtoul(msg + start + 1, NULL, 10); | |
775 | if (msg[start] == '-') | |
776 | tz = -tz; | |
777 | } | |
778 | ||
779 | interp_set_entry(table, 2, show_date(date, tz, 0)); | |
780 | interp_set_entry(table, 3, show_rfc2822_date(date, tz)); | |
781 | interp_set_entry(table, 4, show_date(date, tz, 1)); | |
782 | } | |
783 | ||
784 | static long format_commit_message(const struct commit *commit, | |
785 | const char *msg, char *buf, unsigned long space) | |
786 | { | |
787 | struct interp table[] = { | |
788 | { "%H" }, /* commit hash */ | |
789 | { "%h" }, /* abbreviated commit hash */ | |
790 | { "%T" }, /* tree hash */ | |
791 | { "%t" }, /* abbreviated tree hash */ | |
792 | { "%P" }, /* parent hashes */ | |
793 | { "%p" }, /* abbreviated parent hashes */ | |
794 | { "%an" }, /* author name */ | |
795 | { "%ae" }, /* author email */ | |
796 | { "%ad" }, /* author date */ | |
797 | { "%aD" }, /* author date, RFC2822 style */ | |
798 | { "%ar" }, /* author date, relative */ | |
799 | { "%at" }, /* author date, UNIX timestamp */ | |
800 | { "%cn" }, /* committer name */ | |
801 | { "%ce" }, /* committer email */ | |
802 | { "%cd" }, /* committer date */ | |
803 | { "%cD" }, /* committer date, RFC2822 style */ | |
804 | { "%cr" }, /* committer date, relative */ | |
805 | { "%ct" }, /* committer date, UNIX timestamp */ | |
806 | { "%e" }, /* encoding */ | |
807 | { "%s" }, /* subject */ | |
808 | { "%b" }, /* body */ | |
809 | { "%Cred" }, /* red */ | |
810 | { "%Cgreen" }, /* green */ | |
811 | { "%Cblue" }, /* blue */ | |
812 | { "%Creset" }, /* reset color */ | |
199c45bf JH |
813 | { "%n" }, /* newline */ |
814 | { "%m" }, /* left/right/bottom */ | |
e52a5de4 JS |
815 | }; |
816 | enum interp_index { | |
817 | IHASH = 0, IHASH_ABBREV, | |
818 | ITREE, ITREE_ABBREV, | |
819 | IPARENTS, IPARENTS_ABBREV, | |
820 | IAUTHOR_NAME, IAUTHOR_EMAIL, | |
821 | IAUTHOR_DATE, IAUTHOR_DATE_RFC2822, IAUTHOR_DATE_RELATIVE, | |
822 | IAUTHOR_TIMESTAMP, | |
823 | ICOMMITTER_NAME, ICOMMITTER_EMAIL, | |
824 | ICOMMITTER_DATE, ICOMMITTER_DATE_RFC2822, | |
825 | ICOMMITTER_DATE_RELATIVE, ICOMMITTER_TIMESTAMP, | |
826 | IENCODING, | |
827 | ISUBJECT, | |
828 | IBODY, | |
829 | IRED, IGREEN, IBLUE, IRESET_COLOR, | |
199c45bf JH |
830 | INEWLINE, |
831 | ILEFT_RIGHT, | |
e52a5de4 JS |
832 | }; |
833 | struct commit_list *p; | |
834 | char parents[1024]; | |
835 | int i; | |
836 | enum { HEADER, SUBJECT, BODY } state; | |
837 | ||
199c45bf | 838 | if (ILEFT_RIGHT + 1 != ARRAY_SIZE(table)) |
e52a5de4 JS |
839 | die("invalid interp table!"); |
840 | ||
841 | /* these are independent of the commit */ | |
842 | interp_set_entry(table, IRED, "\033[31m"); | |
843 | interp_set_entry(table, IGREEN, "\033[32m"); | |
844 | interp_set_entry(table, IBLUE, "\033[34m"); | |
845 | interp_set_entry(table, IRESET_COLOR, "\033[m"); | |
846 | interp_set_entry(table, INEWLINE, "\n"); | |
847 | ||
848 | /* these depend on the commit */ | |
849 | if (!commit->object.parsed) | |
850 | parse_object(commit->object.sha1); | |
851 | interp_set_entry(table, IHASH, sha1_to_hex(commit->object.sha1)); | |
852 | interp_set_entry(table, IHASH_ABBREV, | |
853 | find_unique_abbrev(commit->object.sha1, | |
854 | DEFAULT_ABBREV)); | |
855 | interp_set_entry(table, ITREE, sha1_to_hex(commit->tree->object.sha1)); | |
856 | interp_set_entry(table, ITREE_ABBREV, | |
857 | find_unique_abbrev(commit->tree->object.sha1, | |
858 | DEFAULT_ABBREV)); | |
199c45bf JH |
859 | interp_set_entry(table, ILEFT_RIGHT, |
860 | (commit->object.flags & BOUNDARY) | |
861 | ? "-" | |
862 | : (commit->object.flags & SYMMETRIC_LEFT) | |
863 | ? "<" | |
864 | : ">"); | |
542e165c JH |
865 | |
866 | parents[1] = 0; | |
e52a5de4 JS |
867 | for (i = 0, p = commit->parents; |
868 | p && i < sizeof(parents) - 1; | |
869 | p = p->next) | |
542e165c | 870 | i += snprintf(parents + i, sizeof(parents) - i - 1, " %s", |
e52a5de4 | 871 | sha1_to_hex(p->item->object.sha1)); |
542e165c JH |
872 | interp_set_entry(table, IPARENTS, parents + 1); |
873 | ||
874 | parents[1] = 0; | |
e52a5de4 JS |
875 | for (i = 0, p = commit->parents; |
876 | p && i < sizeof(parents) - 1; | |
877 | p = p->next) | |
542e165c | 878 | i += snprintf(parents + i, sizeof(parents) - i - 1, " %s", |
e52a5de4 JS |
879 | find_unique_abbrev(p->item->object.sha1, |
880 | DEFAULT_ABBREV)); | |
542e165c | 881 | interp_set_entry(table, IPARENTS_ABBREV, parents + 1); |
e52a5de4 JS |
882 | |
883 | for (i = 0, state = HEADER; msg[i] && state < BODY; i++) { | |
884 | int eol; | |
885 | for (eol = i; msg[eol] && msg[eol] != '\n'; eol++) | |
886 | ; /* do nothing */ | |
887 | ||
888 | if (state == SUBJECT) { | |
889 | table[ISUBJECT].value = xstrndup(msg + i, eol - i); | |
890 | i = eol; | |
891 | } | |
892 | if (i == eol) { | |
893 | state++; | |
894 | /* strip empty lines */ | |
895 | while (msg[eol + 1] == '\n') | |
896 | eol++; | |
897 | } else if (!prefixcmp(msg + i, "author ")) | |
898 | fill_person(table + IAUTHOR_NAME, | |
899 | msg + i + 7, eol - i - 7); | |
900 | else if (!prefixcmp(msg + i, "committer ")) | |
901 | fill_person(table + ICOMMITTER_NAME, | |
902 | msg + i + 10, eol - i - 10); | |
903 | else if (!prefixcmp(msg + i, "encoding ")) | |
6a5ea2d0 JK |
904 | table[IENCODING].value = |
905 | xstrndup(msg + i + 9, eol - i - 9); | |
e52a5de4 JS |
906 | i = eol; |
907 | } | |
908 | if (msg[i]) | |
909 | table[IBODY].value = xstrdup(msg + i); | |
910 | for (i = 0; i < ARRAY_SIZE(table); i++) | |
911 | if (!table[i].value) | |
912 | interp_set_entry(table, i, "<unknown>"); | |
913 | ||
914 | interpolate(buf, space, user_format, table, ARRAY_SIZE(table)); | |
915 | interp_clear_table(table, ARRAY_SIZE(table)); | |
916 | ||
917 | return strlen(buf); | |
918 | } | |
919 | ||
52883fbd JH |
920 | unsigned long pretty_print_commit(enum cmit_fmt fmt, |
921 | const struct commit *commit, | |
922 | unsigned long len, | |
923 | char *buf, unsigned long space, | |
3dfb9278 | 924 | int abbrev, const char *subject, |
52883fbd JH |
925 | const char *after_subject, |
926 | int relative_date) | |
e3bc7a3b | 927 | { |
f3a47405 | 928 | int hdr = 1, body = 0, seen_title = 0; |
e3bc7a3b | 929 | unsigned long offset = 0; |
3eefc189 | 930 | int indent = 4; |
f2d42275 | 931 | int parents_shown = 0; |
3815f423 | 932 | const char *msg = commit->buffer; |
c831da66 | 933 | int plain_non_ascii = 0; |
f7e68b29 | 934 | char *reencoded; |
3a55602e | 935 | const char *encoding; |
52883fbd | 936 | |
e52a5de4 JS |
937 | if (fmt == CMIT_FMT_USERFORMAT) |
938 | return format_commit_message(commit, msg, buf, space); | |
939 | ||
f7e68b29 JH |
940 | encoding = (git_log_output_encoding |
941 | ? git_log_output_encoding | |
942 | : git_commit_encoding); | |
943 | if (!encoding) | |
944 | encoding = "utf-8"; | |
945 | reencoded = logmsg_reencode(commit, encoding); | |
d2c11a38 JH |
946 | if (reencoded) |
947 | msg = reencoded; | |
3eefc189 | 948 | |
3eefc189 JH |
949 | if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL) |
950 | indent = 0; | |
e3bc7a3b | 951 | |
c831da66 JH |
952 | /* After-subject is used to pass in Content-Type: multipart |
953 | * MIME header; in that case we do not have to do the | |
954 | * plaintext content type even if the commit message has | |
955 | * non 7-bit ASCII character. Otherwise, check if we need | |
956 | * to say this is not a 7-bit ASCII. | |
957 | */ | |
958 | if (fmt == CMIT_FMT_EMAIL && !after_subject) { | |
0da46771 JH |
959 | int i, ch, in_body; |
960 | ||
961 | for (in_body = i = 0; (ch = msg[i]) && i < len; i++) { | |
962 | if (!in_body) { | |
963 | /* author could be non 7-bit ASCII but | |
d2c11a38 | 964 | * the log may be so; skip over the |
0da46771 JH |
965 | * header part first. |
966 | */ | |
967 | if (ch == '\n' && | |
968 | i + 1 < len && msg[i+1] == '\n') | |
969 | in_body = 1; | |
970 | } | |
f7e68b29 | 971 | else if (non_ascii(ch)) { |
c831da66 | 972 | plain_non_ascii = 1; |
0da46771 JH |
973 | break; |
974 | } | |
975 | } | |
c831da66 JH |
976 | } |
977 | ||
e3bc7a3b LT |
978 | for (;;) { |
979 | const char *line = msg; | |
980 | int linelen = get_one_line(msg, len); | |
981 | ||
982 | if (!linelen) | |
983 | break; | |
984 | ||
985 | /* | |
986 | * We want some slop for indentation and a possible | |
987 | * final "...". Thus the "+ 20". | |
988 | */ | |
989 | if (offset + linelen + 20 > space) { | |
990 | memcpy(buf + offset, " ...\n", 8); | |
991 | offset += 8; | |
992 | break; | |
993 | } | |
994 | ||
995 | msg += linelen; | |
996 | len -= linelen; | |
e3bc7a3b | 997 | if (hdr) { |
000182ea LT |
998 | if (linelen == 1) { |
999 | hdr = 0; | |
3eefc189 | 1000 | if ((fmt != CMIT_FMT_ONELINE) && !subject) |
d87449c5 | 1001 | buf[offset++] = '\n'; |
000182ea LT |
1002 | continue; |
1003 | } | |
1004 | if (fmt == CMIT_FMT_RAW) { | |
1005 | memcpy(buf + offset, line, linelen); | |
1006 | offset += linelen; | |
1007 | continue; | |
1008 | } | |
28342a5d LT |
1009 | if (!memcmp(line, "parent ", 7)) { |
1010 | if (linelen != 48) | |
1011 | die("bad parent line in commit"); | |
f2d42275 | 1012 | continue; |
28342a5d | 1013 | } |
ff56fe1c | 1014 | |
f2d42275 JH |
1015 | if (!parents_shown) { |
1016 | offset += add_merge_info(fmt, buf + offset, | |
1017 | commit, abbrev); | |
1018 | parents_shown = 1; | |
1019 | continue; | |
1020 | } | |
ff56fe1c JH |
1021 | /* |
1022 | * MEDIUM == DEFAULT shows only author with dates. | |
1023 | * FULL shows both authors but not dates. | |
1024 | * FULLER shows both authors and dates. | |
1025 | */ | |
e3bc7a3b | 1026 | if (!memcmp(line, "author ", 7)) |
ff56fe1c JH |
1027 | offset += add_user_info("Author", fmt, |
1028 | buf + offset, | |
3dfb9278 | 1029 | line + 7, |
f7e68b29 JH |
1030 | relative_date, |
1031 | encoding); | |
ff56fe1c JH |
1032 | if (!memcmp(line, "committer ", 10) && |
1033 | (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) | |
1034 | offset += add_user_info("Commit", fmt, | |
1035 | buf + offset, | |
3dfb9278 | 1036 | line + 10, |
f7e68b29 JH |
1037 | relative_date, |
1038 | encoding); | |
e3bc7a3b LT |
1039 | continue; |
1040 | } | |
000182ea | 1041 | |
19b3bd3e RS |
1042 | if (!subject) |
1043 | body = 1; | |
1044 | ||
3eefc189 | 1045 | if (is_empty_line(line, &linelen)) { |
f3a47405 LH |
1046 | if (!seen_title) |
1047 | continue; | |
000182ea LT |
1048 | if (!body) |
1049 | continue; | |
3eefc189 JH |
1050 | if (subject) |
1051 | continue; | |
000182ea LT |
1052 | if (fmt == CMIT_FMT_SHORT) |
1053 | break; | |
000182ea | 1054 | } |
d87449c5 | 1055 | |
f3a47405 | 1056 | seen_title = 1; |
3eefc189 | 1057 | if (subject) { |
53f420ef JH |
1058 | int slen = strlen(subject); |
1059 | memcpy(buf + offset, subject, slen); | |
1060 | offset += slen; | |
f7e68b29 JH |
1061 | offset += add_rfc2047(buf + offset, line, linelen, |
1062 | encoding); | |
cdd406e3 JH |
1063 | } |
1064 | else { | |
1065 | memset(buf + offset, ' ', indent); | |
1066 | memcpy(buf + offset + indent, line, linelen); | |
1067 | offset += linelen + indent; | |
3eefc189 | 1068 | } |
3eefc189 | 1069 | buf[offset++] = '\n'; |
d87449c5 JH |
1070 | if (fmt == CMIT_FMT_ONELINE) |
1071 | break; | |
c831da66 | 1072 | if (subject && plain_non_ascii) { |
f7e68b29 JH |
1073 | int sz; |
1074 | char header[512]; | |
1075 | const char *header_fmt = | |
1076 | "Content-Type: text/plain; charset=%s\n" | |
cdd406e3 | 1077 | "Content-Transfer-Encoding: 8bit\n"; |
f7e68b29 JH |
1078 | sz = snprintf(header, sizeof(header), header_fmt, |
1079 | encoding); | |
1080 | if (sizeof(header) < sz) | |
1081 | die("Encoding name %s too long", encoding); | |
1082 | memcpy(buf + offset, header, sz); | |
1083 | offset += sz; | |
cdd406e3 | 1084 | } |
698ce6f8 JS |
1085 | if (after_subject) { |
1086 | int slen = strlen(after_subject); | |
1087 | if (slen > space - offset - 1) | |
1088 | slen = space - offset - 1; | |
1089 | memcpy(buf + offset, after_subject, slen); | |
1090 | offset += slen; | |
1091 | after_subject = NULL; | |
1092 | } | |
3eefc189 | 1093 | subject = NULL; |
d87449c5 | 1094 | } |
40c2fe00 LT |
1095 | while (offset && isspace(buf[offset-1])) |
1096 | offset--; | |
1097 | /* Make sure there is an EOLN for the non-oneline case */ | |
1098 | if (fmt != CMIT_FMT_ONELINE) | |
1099 | buf[offset++] = '\n'; | |
19b3bd3e RS |
1100 | /* |
1101 | * make sure there is another EOLN to separate the headers from whatever | |
1102 | * body the caller appends if we haven't already written a body | |
1103 | */ | |
1104 | if (fmt == CMIT_FMT_EMAIL && !body) | |
1105 | buf[offset++] = '\n'; | |
e3bc7a3b | 1106 | buf[offset] = '\0'; |
52883fbd JH |
1107 | |
1108 | free(reencoded); | |
e3bc7a3b LT |
1109 | return offset; |
1110 | } | |
a3437b8c JS |
1111 | |
1112 | struct commit *pop_commit(struct commit_list **stack) | |
1113 | { | |
1114 | struct commit_list *top = *stack; | |
1115 | struct commit *item = top ? top->item : NULL; | |
1116 | ||
1117 | if (top) { | |
1118 | *stack = top->next; | |
1119 | free(top); | |
1120 | } | |
1121 | return item; | |
1122 | } | |
1123 | ||
1124 | int count_parents(struct commit * commit) | |
1125 | { | |
96f1e58f | 1126 | int count; |
a3437b8c | 1127 | struct commit_list * parents = commit->parents; |
96f1e58f DR |
1128 | for (count = 0; parents; parents = parents->next,count++) |
1129 | ; | |
a3437b8c JS |
1130 | return count; |
1131 | } | |
1132 | ||
6b6dcfc2 FK |
1133 | void topo_sort_default_setter(struct commit *c, void *data) |
1134 | { | |
d3ff6f55 | 1135 | c->util = data; |
6b6dcfc2 FK |
1136 | } |
1137 | ||
1138 | void *topo_sort_default_getter(struct commit *c) | |
1139 | { | |
d3ff6f55 | 1140 | return c->util; |
6b6dcfc2 FK |
1141 | } |
1142 | ||
ab580ace JS |
1143 | /* |
1144 | * Performs an in-place topological sort on the list supplied. | |
1145 | */ | |
4c8725f1 | 1146 | void sort_in_topological_order(struct commit_list ** list, int lifo) |
6b6dcfc2 FK |
1147 | { |
1148 | sort_in_topological_order_fn(list, lifo, topo_sort_default_setter, | |
1149 | topo_sort_default_getter); | |
1150 | } | |
1151 | ||
1152 | void sort_in_topological_order_fn(struct commit_list ** list, int lifo, | |
1153 | topo_sort_set_fn_t setter, | |
1154 | topo_sort_get_fn_t getter) | |
ab580ace JS |
1155 | { |
1156 | struct commit_list * next = *list; | |
2ed02887 | 1157 | struct commit_list * work = NULL, **insert; |
ab580ace JS |
1158 | struct commit_list ** pptr = list; |
1159 | struct sort_node * nodes; | |
1160 | struct sort_node * next_nodes; | |
1161 | int count = 0; | |
1162 | ||
1163 | /* determine the size of the list */ | |
1164 | while (next) { | |
1165 | next = next->next; | |
1166 | count++; | |
1167 | } | |
7d6fb370 EW |
1168 | |
1169 | if (!count) | |
1170 | return; | |
ab580ace JS |
1171 | /* allocate an array to help sort the list */ |
1172 | nodes = xcalloc(count, sizeof(*nodes)); | |
1173 | /* link the list to the array */ | |
1174 | next_nodes = nodes; | |
1175 | next=*list; | |
1176 | while (next) { | |
1177 | next_nodes->list_item = next; | |
6b6dcfc2 | 1178 | setter(next->item, next_nodes); |
ab580ace JS |
1179 | next_nodes++; |
1180 | next = next->next; | |
1181 | } | |
1182 | /* update the indegree */ | |
1183 | next=*list; | |
1184 | while (next) { | |
1185 | struct commit_list * parents = next->item->parents; | |
1186 | while (parents) { | |
1187 | struct commit * parent=parents->item; | |
6b6dcfc2 FK |
1188 | struct sort_node * pn = (struct sort_node *) getter(parent); |
1189 | ||
ab580ace JS |
1190 | if (pn) |
1191 | pn->indegree++; | |
1192 | parents=parents->next; | |
1193 | } | |
1194 | next=next->next; | |
1195 | } | |
1196 | /* | |
1197 | * find the tips | |
1198 | * | |
1199 | * tips are nodes not reachable from any other node in the list | |
1200 | * | |
1201 | * the tips serve as a starting set for the work queue. | |
1202 | */ | |
1203 | next=*list; | |
2ed02887 | 1204 | insert = &work; |
ab580ace | 1205 | while (next) { |
6b6dcfc2 | 1206 | struct sort_node * node = (struct sort_node *) getter(next->item); |
ab580ace JS |
1207 | |
1208 | if (node->indegree == 0) { | |
2ed02887 | 1209 | insert = &commit_list_insert(next->item, insert)->next; |
ab580ace JS |
1210 | } |
1211 | next=next->next; | |
1212 | } | |
4c8725f1 | 1213 | |
ab580ace | 1214 | /* process the list in topological order */ |
4c8725f1 JH |
1215 | if (!lifo) |
1216 | sort_by_date(&work); | |
ab580ace JS |
1217 | while (work) { |
1218 | struct commit * work_item = pop_commit(&work); | |
6b6dcfc2 | 1219 | struct sort_node * work_node = (struct sort_node *) getter(work_item); |
ab580ace JS |
1220 | struct commit_list * parents = work_item->parents; |
1221 | ||
1222 | while (parents) { | |
1223 | struct commit * parent=parents->item; | |
6b6dcfc2 FK |
1224 | struct sort_node * pn = (struct sort_node *) getter(parent); |
1225 | ||
ab580ace | 1226 | if (pn) { |
6b6dcfc2 | 1227 | /* |
ab580ace JS |
1228 | * parents are only enqueued for emission |
1229 | * when all their children have been emitted thereby | |
1230 | * guaranteeing topological order. | |
1231 | */ | |
1232 | pn->indegree--; | |
4c8725f1 JH |
1233 | if (!pn->indegree) { |
1234 | if (!lifo) | |
1235 | insert_by_date(parent, &work); | |
1236 | else | |
1237 | commit_list_insert(parent, &work); | |
1238 | } | |
ab580ace JS |
1239 | } |
1240 | parents=parents->next; | |
1241 | } | |
1242 | /* | |
1243 | * work_item is a commit all of whose children | |
1244 | * have already been emitted. we can emit it now. | |
1245 | */ | |
1246 | *pptr = work_node->list_item; | |
1247 | pptr = &(*pptr)->next; | |
1248 | *pptr = NULL; | |
6b6dcfc2 | 1249 | setter(work_item, NULL); |
ab580ace JS |
1250 | } |
1251 | free(nodes); | |
1252 | } | |
7c6f8aaf | 1253 | |
1295228d | 1254 | /* merge-base stuff */ |
7c6f8aaf | 1255 | |
577ed5c2 JH |
1256 | /* bits #0..15 in revision.h */ |
1257 | #define PARENT1 (1u<<16) | |
1258 | #define PARENT2 (1u<<17) | |
1259 | #define STALE (1u<<18) | |
1260 | #define RESULT (1u<<19) | |
7c6f8aaf | 1261 | |
1295228d JH |
1262 | static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT); |
1263 | ||
7c6f8aaf JS |
1264 | static struct commit *interesting(struct commit_list *list) |
1265 | { | |
1266 | while (list) { | |
1267 | struct commit *commit = list->item; | |
1268 | list = list->next; | |
542ccefe | 1269 | if (commit->object.flags & STALE) |
7c6f8aaf JS |
1270 | continue; |
1271 | return commit; | |
1272 | } | |
1273 | return NULL; | |
1274 | } | |
1275 | ||
f3249438 | 1276 | static struct commit_list *merge_bases(struct commit *one, struct commit *two) |
7c6f8aaf JS |
1277 | { |
1278 | struct commit_list *list = NULL; | |
1279 | struct commit_list *result = NULL; | |
7c6f8aaf | 1280 | |
f3249438 JH |
1281 | if (one == two) |
1282 | /* We do not mark this even with RESULT so we do not | |
1283 | * have to clean it up. | |
1284 | */ | |
1285 | return commit_list_insert(one, &result); | |
7c6f8aaf | 1286 | |
f3249438 JH |
1287 | parse_commit(one); |
1288 | parse_commit(two); | |
7c6f8aaf | 1289 | |
f3249438 JH |
1290 | one->object.flags |= PARENT1; |
1291 | two->object.flags |= PARENT2; | |
1292 | insert_by_date(one, &list); | |
1293 | insert_by_date(two, &list); | |
7c6f8aaf JS |
1294 | |
1295 | while (interesting(list)) { | |
f3249438 | 1296 | struct commit *commit; |
7c6f8aaf | 1297 | struct commit_list *parents; |
f3249438 JH |
1298 | struct commit_list *n; |
1299 | int flags; | |
7c6f8aaf | 1300 | |
f3249438 JH |
1301 | commit = list->item; |
1302 | n = list->next; | |
1303 | free(list); | |
1304 | list = n; | |
7c6f8aaf | 1305 | |
f3249438 JH |
1306 | flags = commit->object.flags & (PARENT1 | PARENT2 | STALE); |
1307 | if (flags == (PARENT1 | PARENT2)) { | |
1308 | if (!(commit->object.flags & RESULT)) { | |
1309 | commit->object.flags |= RESULT; | |
1310 | insert_by_date(commit, &result); | |
1311 | } | |
542ccefe JH |
1312 | /* Mark parents of a found merge stale */ |
1313 | flags |= STALE; | |
7c6f8aaf JS |
1314 | } |
1315 | parents = commit->parents; | |
1316 | while (parents) { | |
1317 | struct commit *p = parents->item; | |
1318 | parents = parents->next; | |
1319 | if ((p->object.flags & flags) == flags) | |
1320 | continue; | |
1321 | parse_commit(p); | |
1322 | p->object.flags |= flags; | |
1323 | insert_by_date(p, &list); | |
1324 | } | |
1325 | } | |
1326 | ||
f3249438 | 1327 | /* Clean up the result to remove stale ones */ |
1295228d | 1328 | free_commit_list(list); |
f3249438 JH |
1329 | list = result; result = NULL; |
1330 | while (list) { | |
1331 | struct commit_list *n = list->next; | |
1332 | if (!(list->item->object.flags & STALE)) | |
1333 | insert_by_date(list->item, &result); | |
1334 | free(list); | |
1335 | list = n; | |
1336 | } | |
1337 | return result; | |
1338 | } | |
7c6f8aaf | 1339 | |
f3249438 JH |
1340 | struct commit_list *get_merge_bases(struct commit *one, |
1341 | struct commit *two, | |
1342 | int cleanup) | |
1343 | { | |
f3249438 JH |
1344 | struct commit_list *list; |
1345 | struct commit **rslt; | |
1346 | struct commit_list *result; | |
1347 | int cnt, i, j; | |
1348 | ||
1349 | result = merge_bases(one, two); | |
1350 | if (one == two) | |
1351 | return result; | |
1352 | if (!result || !result->next) { | |
1353 | if (cleanup) { | |
1354 | clear_commit_marks(one, all_flags); | |
1355 | clear_commit_marks(two, all_flags); | |
7c6f8aaf | 1356 | } |
f3249438 | 1357 | return result; |
7c6f8aaf JS |
1358 | } |
1359 | ||
f3249438 JH |
1360 | /* There are more than one */ |
1361 | cnt = 0; | |
1362 | list = result; | |
1363 | while (list) { | |
1364 | list = list->next; | |
1365 | cnt++; | |
1366 | } | |
1367 | rslt = xcalloc(cnt, sizeof(*rslt)); | |
1368 | for (list = result, i = 0; list; list = list->next) | |
1369 | rslt[i++] = list->item; | |
1370 | free_commit_list(result); | |
1371 | ||
1372 | clear_commit_marks(one, all_flags); | |
1373 | clear_commit_marks(two, all_flags); | |
1374 | for (i = 0; i < cnt - 1; i++) { | |
1375 | for (j = i+1; j < cnt; j++) { | |
1376 | if (!rslt[i] || !rslt[j]) | |
1377 | continue; | |
1378 | result = merge_bases(rslt[i], rslt[j]); | |
1379 | clear_commit_marks(rslt[i], all_flags); | |
1380 | clear_commit_marks(rslt[j], all_flags); | |
1381 | for (list = result; list; list = list->next) { | |
1382 | if (rslt[i] == list->item) | |
1383 | rslt[i] = NULL; | |
1384 | if (rslt[j] == list->item) | |
1385 | rslt[j] = NULL; | |
1386 | } | |
1387 | } | |
58ecf5c1 | 1388 | } |
7c6f8aaf | 1389 | |
f3249438 JH |
1390 | /* Surviving ones in rslt[] are the independent results */ |
1391 | result = NULL; | |
1392 | for (i = 0; i < cnt; i++) { | |
1393 | if (rslt[i]) | |
1394 | insert_by_date(rslt[i], &result); | |
1395 | } | |
1396 | free(rslt); | |
7c6f8aaf JS |
1397 | return result; |
1398 | } | |
2ecd2bbc | 1399 | |
03840fc3 | 1400 | int in_merge_bases(struct commit *commit, struct commit **reference, int num) |
2ecd2bbc JH |
1401 | { |
1402 | struct commit_list *bases, *b; | |
1403 | int ret = 0; | |
1404 | ||
03840fc3 JH |
1405 | if (num == 1) |
1406 | bases = get_merge_bases(commit, *reference, 1); | |
1407 | else | |
1408 | die("not yet"); | |
2ecd2bbc | 1409 | for (b = bases; b; b = b->next) { |
03840fc3 | 1410 | if (!hashcmp(commit->object.sha1, b->item->object.sha1)) { |
2ecd2bbc JH |
1411 | ret = 1; |
1412 | break; | |
1413 | } | |
1414 | } | |
1415 | ||
1416 | free_commit_list(bases); | |
1417 | return ret; | |
1418 | } |