GIT 0.99.9i aka 1.0rc2
[git/git.git] / commit.c
CommitLineData
961784ee 1#include "tag.h"
175785e5
DB
2#include "commit.h"
3#include "cache.h"
175785e5 4
60ab26de
LT
5int save_commit_buffer = 1;
6
ab580ace
JS
7struct 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
23const char *commit_type = "commit";
24
9b66ec04
LT
25enum cmit_fmt get_commit_format(const char *arg)
26{
27 if (!*arg)
28 return CMIT_FMT_DEFAULT;
29 if (!strcmp(arg, "=raw"))
30 return CMIT_FMT_RAW;
31 if (!strcmp(arg, "=medium"))
32 return CMIT_FMT_MEDIUM;
33 if (!strcmp(arg, "=short"))
34 return CMIT_FMT_SHORT;
35 if (!strcmp(arg, "=full"))
36 return CMIT_FMT_FULL;
ff56fe1c
JH
37 if (!strcmp(arg, "=fuller"))
38 return CMIT_FMT_FULLER;
d87449c5
JH
39 if (!strcmp(arg, "=oneline"))
40 return CMIT_FMT_ONELINE;
9b66ec04
LT
41 die("invalid --pretty format");
42}
43
f76412ed
JH
44static struct commit *check_commit(struct object *obj,
45 const unsigned char *sha1,
46 int quiet)
961784ee
LT
47{
48 if (obj->type != commit_type) {
f76412ed
JH
49 if (!quiet)
50 error("Object %s is a %s, not a commit",
51 sha1_to_hex(sha1), obj->type);
961784ee
LT
52 return NULL;
53 }
54 return (struct commit *) obj;
55}
56
f76412ed
JH
57struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
58 int quiet)
961784ee 59{
9534f40b 60 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
961784ee
LT
61
62 if (!obj)
63 return NULL;
f76412ed
JH
64 return check_commit(obj, sha1, quiet);
65}
66
67struct commit *lookup_commit_reference(const unsigned char *sha1)
68{
69 return lookup_commit_reference_gently(sha1, 0);
961784ee
LT
70}
71
5d6ccf5c 72struct commit *lookup_commit(const unsigned char *sha1)
175785e5
DB
73{
74 struct object *obj = lookup_object(sha1);
75 if (!obj) {
812666c8 76 struct commit *ret = xmalloc(sizeof(struct commit));
175785e5
DB
77 memset(ret, 0, sizeof(struct commit));
78 created_object(sha1, &ret->object);
d32987be 79 ret->object.type = commit_type;
175785e5
DB
80 return ret;
81 }
d1af002d
NP
82 if (!obj->type)
83 obj->type = commit_type;
f76412ed 84 return check_commit(obj, sha1, 0);
175785e5
DB
85}
86
87static unsigned long parse_commit_date(const char *buf)
88{
89 unsigned long date;
90
91 if (memcmp(buf, "author", 6))
92 return 0;
93 while (*buf++ != '\n')
94 /* nada */;
95 if (memcmp(buf, "committer", 9))
96 return 0;
97 while (*buf++ != '>')
98 /* nada */;
99 date = strtoul(buf, NULL, 10);
100 if (date == ULONG_MAX)
101 date = 0;
102 return date;
103}
104
5da5c8f4
JH
105static struct commit_graft {
106 unsigned char sha1[20];
107 int nr_parent;
2c04662d 108 unsigned char parent[0][20]; /* more */
5da5c8f4
JH
109} **commit_graft;
110static int commit_graft_alloc, commit_graft_nr;
111
112static int commit_graft_pos(const unsigned char *sha1)
113{
114 int lo, hi;
115 lo = 0;
116 hi = commit_graft_nr;
117 while (lo < hi) {
118 int mi = (lo + hi) / 2;
119 struct commit_graft *graft = commit_graft[mi];
120 int cmp = memcmp(sha1, graft->sha1, 20);
121 if (!cmp)
122 return mi;
123 if (cmp < 0)
124 hi = mi;
125 else
126 lo = mi + 1;
127 }
128 return -lo - 1;
129}
130
131static void prepare_commit_graft(void)
132{
133 char *graft_file = get_graft_file();
134 FILE *fp = fopen(graft_file, "r");
135 char buf[1024];
136 if (!fp) {
137 commit_graft = (struct commit_graft **) "hack";
138 return;
139 }
140 while (fgets(buf, sizeof(buf), fp)) {
141 /* The format is just "Commit Parent1 Parent2 ...\n" */
142 int len = strlen(buf);
143 int i;
144 struct commit_graft *graft = NULL;
145
146 if (buf[len-1] == '\n')
147 buf[--len] = 0;
148 if (buf[0] == '#')
149 continue;
150 if ((len + 1) % 41) {
151 bad_graft_data:
152 error("bad graft data: %s", buf);
153 free(graft);
154 continue;
155 }
156 i = (len + 1) / 41 - 1;
157 graft = xmalloc(sizeof(*graft) + 20 * i);
158 graft->nr_parent = i;
159 if (get_sha1_hex(buf, graft->sha1))
160 goto bad_graft_data;
161 for (i = 40; i < len; i += 41) {
162 if (buf[i] != ' ')
163 goto bad_graft_data;
164 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
165 goto bad_graft_data;
166 }
167 i = commit_graft_pos(graft->sha1);
168 if (0 <= i) {
169 error("duplicate graft data: %s", buf);
170 free(graft);
171 continue;
172 }
173 i = -i - 1;
174 if (commit_graft_alloc <= ++commit_graft_nr) {
175 commit_graft_alloc = alloc_nr(commit_graft_alloc);
176 commit_graft = xrealloc(commit_graft,
177 sizeof(*commit_graft) *
178 commit_graft_alloc);
179 }
180 if (i < commit_graft_nr)
181 memmove(commit_graft + i + 1,
182 commit_graft + i,
183 (commit_graft_nr - i - 1) *
184 sizeof(*commit_graft));
185 commit_graft[i] = graft;
186 }
187 fclose(fp);
188}
189
190static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
191{
192 int pos;
193 if (!commit_graft)
194 prepare_commit_graft();
195 pos = commit_graft_pos(sha1);
196 if (pos < 0)
197 return NULL;
198 return commit_graft[pos];
199}
200
bd2c39f5 201int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
175785e5 202{
f7cc77d7 203 char *bufptr = buffer;
175785e5 204 unsigned char parent[20];
6c88be16 205 struct commit_list **pptr;
5da5c8f4 206 struct commit_graft *graft;
bd2c39f5 207
175785e5
DB
208 if (item->object.parsed)
209 return 0;
210 item->object.parsed = 1;
f7cc77d7
LT
211 if (memcmp(bufptr, "tree ", 5))
212 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
213 if (get_sha1_hex(bufptr + 5, parent) < 0)
214 return error("bad tree pointer in commit %s\n", sha1_to_hex(item->object.sha1));
175785e5 215 item->tree = lookup_tree(parent);
235ac407
LT
216 if (item->tree)
217 add_ref(&item->object, &item->tree->object);
175785e5 218 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
6c88be16 219 pptr = &item->parents;
5da5c8f4
JH
220
221 graft = lookup_commit_graft(item->object.sha1);
f7cc77d7
LT
222 while (!memcmp(bufptr, "parent ", 7)) {
223 struct commit *new_parent;
224
225 if (get_sha1_hex(bufptr + 7, parent) || bufptr[47] != '\n')
226 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
5da5c8f4
JH
227 bufptr += 48;
228 if (graft)
229 continue;
f7cc77d7 230 new_parent = lookup_commit(parent);
235ac407 231 if (new_parent) {
6c88be16 232 pptr = &commit_list_insert(new_parent, pptr)->next;
235ac407
LT
233 add_ref(&item->object, &new_parent->object);
234 }
5da5c8f4
JH
235 }
236 if (graft) {
237 int i;
238 struct commit *new_parent;
239 for (i = 0; i < graft->nr_parent; i++) {
240 new_parent = lookup_commit(graft->parent[i]);
241 if (!new_parent)
242 continue;
243 pptr = &commit_list_insert(new_parent, pptr)->next;
244 add_ref(&item->object, &new_parent->object);
245 }
175785e5
DB
246 }
247 item->date = parse_commit_date(bufptr);
175785e5
DB
248 return 0;
249}
250
bd2c39f5
NP
251int parse_commit(struct commit *item)
252{
253 char type[20];
254 void *buffer;
255 unsigned long size;
256 int ret;
257
258 if (item->object.parsed)
259 return 0;
260 buffer = read_sha1_file(item->object.sha1, type, &size);
261 if (!buffer)
262 return error("Could not read %s",
263 sha1_to_hex(item->object.sha1));
264 if (strcmp(type, commit_type)) {
265 free(buffer);
266 return error("Object %s not a commit",
267 sha1_to_hex(item->object.sha1));
268 }
269 ret = parse_commit_buffer(item, buffer, size);
60ab26de 270 if (save_commit_buffer && !ret) {
3ff1fbbb
LT
271 item->buffer = buffer;
272 return 0;
273 }
bd2c39f5
NP
274 free(buffer);
275 return ret;
276}
277
ac5155ef 278struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
dd97f850 279{
812666c8 280 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
dd97f850
DB
281 new_list->item = item;
282 new_list->next = *list_p;
283 *list_p = new_list;
ac5155ef 284 return new_list;
dd97f850
DB
285}
286
175785e5
DB
287void free_commit_list(struct commit_list *list)
288{
289 while (list) {
290 struct commit_list *temp = list;
291 list = temp->next;
292 free(temp);
293 }
294}
dd97f850 295
f755494c 296struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
dd97f850
DB
297{
298 struct commit_list **pp = list;
299 struct commit_list *p;
300 while ((p = *pp) != NULL) {
301 if (p->item->date < item->date) {
302 break;
303 }
304 pp = &p->next;
305 }
f755494c 306 return commit_list_insert(item, pp);
dd97f850
DB
307}
308
309
310void sort_by_date(struct commit_list **list)
311{
312 struct commit_list *ret = NULL;
313 while (*list) {
f755494c 314 insert_by_date((*list)->item, &ret);
dd97f850
DB
315 *list = (*list)->next;
316 }
317 *list = ret;
318}
319
58e28af6
DB
320struct commit *pop_most_recent_commit(struct commit_list **list,
321 unsigned int mark)
dd97f850
DB
322{
323 struct commit *ret = (*list)->item;
324 struct commit_list *parents = ret->parents;
325 struct commit_list *old = *list;
326
327 *list = (*list)->next;
328 free(old);
329
330 while (parents) {
4056c091 331 struct commit *commit = parents->item;
58e28af6
DB
332 parse_commit(commit);
333 if (!(commit->object.flags & mark)) {
334 commit->object.flags |= mark;
f755494c 335 insert_by_date(commit, list);
4056c091 336 }
dd97f850
DB
337 parents = parents->next;
338 }
339 return ret;
340}
e3bc7a3b
LT
341
342/*
343 * Generic support for pretty-printing the header
344 */
345static int get_one_line(const char *msg, unsigned long len)
346{
347 int ret = 0;
348
349 while (len--) {
350 char c = *msg++;
351 ret++;
352 if (c == '\n')
353 break;
354 if (!c)
355 return 0;
356 }
357 return ret;
358}
359
9b66ec04 360static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
e3bc7a3b
LT
361{
362 char *date;
5de36bfe 363 int namelen;
e3bc7a3b 364 unsigned long time;
000182ea 365 int tz, ret;
ff56fe1c 366 const char *filler = " ";
e3bc7a3b 367
d87449c5
JH
368 if (fmt == CMIT_FMT_ONELINE)
369 return 0;
e3bc7a3b
LT
370 date = strchr(line, '>');
371 if (!date)
372 return 0;
373 namelen = ++date - line;
374 time = strtoul(date, &date, 10);
375 tz = strtol(date, NULL, 10);
376
ff56fe1c
JH
377 ret = sprintf(buf, "%s: %.*s%.*s\n", what,
378 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
379 filler, namelen, line);
380 switch (fmt) {
381 case CMIT_FMT_MEDIUM:
000182ea 382 ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
ff56fe1c
JH
383 break;
384 case CMIT_FMT_FULLER:
385 ret += sprintf(buf + ret, "%sDate: %s\n", what, show_date(time, tz));
386 break;
387 default:
388 /* notin' */
389 break;
390 }
000182ea
LT
391 return ret;
392}
393
394static int is_empty_line(const char *line, int len)
395{
396 while (len && isspace(line[len-1]))
397 len--;
398 return !len;
e3bc7a3b
LT
399}
400
28342a5d
LT
401static int add_parent_info(enum cmit_fmt fmt, char *buf, const char *line, int parents)
402{
403 int offset = 0;
d87449c5
JH
404
405 if (fmt == CMIT_FMT_ONELINE)
406 return offset;
28342a5d
LT
407 switch (parents) {
408 case 1:
409 break;
410 case 2:
411 /* Go back to the previous line: 40 characters of previous parent, and one '\n' */
412 offset = sprintf(buf, "Merge: %.40s\n", line-41);
413 /* Fallthrough */
414 default:
415 /* Replace the previous '\n' with a space */
416 buf[offset-1] = ' ';
417 offset += sprintf(buf + offset, "%.40s\n", line+7);
418 }
419 return offset;
420}
421
000182ea 422unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned long len, char *buf, unsigned long space)
e3bc7a3b 423{
000182ea 424 int hdr = 1, body = 0;
e3bc7a3b 425 unsigned long offset = 0;
28342a5d 426 int parents = 0;
d87449c5 427 int indent = (fmt == CMIT_FMT_ONELINE) ? 0 : 4;
e3bc7a3b
LT
428
429 for (;;) {
430 const char *line = msg;
431 int linelen = get_one_line(msg, len);
432
433 if (!linelen)
434 break;
435
436 /*
437 * We want some slop for indentation and a possible
438 * final "...". Thus the "+ 20".
439 */
440 if (offset + linelen + 20 > space) {
441 memcpy(buf + offset, " ...\n", 8);
442 offset += 8;
443 break;
444 }
445
446 msg += linelen;
447 len -= linelen;
e3bc7a3b 448 if (hdr) {
000182ea
LT
449 if (linelen == 1) {
450 hdr = 0;
d87449c5
JH
451 if (fmt != CMIT_FMT_ONELINE)
452 buf[offset++] = '\n';
000182ea
LT
453 continue;
454 }
455 if (fmt == CMIT_FMT_RAW) {
456 memcpy(buf + offset, line, linelen);
457 offset += linelen;
458 continue;
459 }
28342a5d
LT
460 if (!memcmp(line, "parent ", 7)) {
461 if (linelen != 48)
462 die("bad parent line in commit");
463 offset += add_parent_info(fmt, buf + offset, line, ++parents);
464 }
ff56fe1c
JH
465
466 /*
467 * MEDIUM == DEFAULT shows only author with dates.
468 * FULL shows both authors but not dates.
469 * FULLER shows both authors and dates.
470 */
e3bc7a3b 471 if (!memcmp(line, "author ", 7))
ff56fe1c
JH
472 offset += add_user_info("Author", fmt,
473 buf + offset,
474 line + 7);
475 if (!memcmp(line, "committer ", 10) &&
476 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
477 offset += add_user_info("Commit", fmt,
478 buf + offset,
479 line + 10);
e3bc7a3b
LT
480 continue;
481 }
000182ea
LT
482
483 if (is_empty_line(line, linelen)) {
484 if (!body)
485 continue;
486 if (fmt == CMIT_FMT_SHORT)
487 break;
488 } else {
489 body = 1;
490 }
d87449c5
JH
491
492 memset(buf + offset, ' ', indent);
493 memcpy(buf + offset + indent, line, linelen);
494 offset += linelen + indent;
495 if (fmt == CMIT_FMT_ONELINE)
496 break;
497 }
498 if (fmt == CMIT_FMT_ONELINE) {
499 /* We do not want the terminating newline */
500 if (buf[offset - 1] == '\n')
501 offset--;
502 }
503 else {
504 /* Make sure there is an EOLN */
505 if (buf[offset - 1] != '\n')
506 buf[offset++] = '\n';
e3bc7a3b 507 }
e3bc7a3b
LT
508 buf[offset] = '\0';
509 return offset;
510}
a3437b8c
JS
511
512struct commit *pop_commit(struct commit_list **stack)
513{
514 struct commit_list *top = *stack;
515 struct commit *item = top ? top->item : NULL;
516
517 if (top) {
518 *stack = top->next;
519 free(top);
520 }
521 return item;
522}
523
524int count_parents(struct commit * commit)
525{
526 int count = 0;
527 struct commit_list * parents = commit->parents;
528 for (count=0;parents; parents=parents->next,count++)
529 ;
530 return count;
531}
532
ab580ace
JS
533/*
534 * Performs an in-place topological sort on the list supplied.
535 */
536void sort_in_topological_order(struct commit_list ** list)
537{
538 struct commit_list * next = *list;
2ed02887 539 struct commit_list * work = NULL, **insert;
ab580ace
JS
540 struct commit_list ** pptr = list;
541 struct sort_node * nodes;
542 struct sort_node * next_nodes;
543 int count = 0;
544
545 /* determine the size of the list */
546 while (next) {
547 next = next->next;
548 count++;
549 }
550 /* allocate an array to help sort the list */
551 nodes = xcalloc(count, sizeof(*nodes));
552 /* link the list to the array */
553 next_nodes = nodes;
554 next=*list;
555 while (next) {
556 next_nodes->list_item = next;
557 next->item->object.util = next_nodes;
558 next_nodes++;
559 next = next->next;
560 }
561 /* update the indegree */
562 next=*list;
563 while (next) {
564 struct commit_list * parents = next->item->parents;
565 while (parents) {
566 struct commit * parent=parents->item;
567 struct sort_node * pn = (struct sort_node *)parent->object.util;
568
569 if (pn)
570 pn->indegree++;
571 parents=parents->next;
572 }
573 next=next->next;
574 }
575 /*
576 * find the tips
577 *
578 * tips are nodes not reachable from any other node in the list
579 *
580 * the tips serve as a starting set for the work queue.
581 */
582 next=*list;
2ed02887 583 insert = &work;
ab580ace
JS
584 while (next) {
585 struct sort_node * node = (struct sort_node *)next->item->object.util;
586
587 if (node->indegree == 0) {
2ed02887 588 insert = &commit_list_insert(next->item, insert)->next;
ab580ace
JS
589 }
590 next=next->next;
591 }
592 /* process the list in topological order */
593 while (work) {
594 struct commit * work_item = pop_commit(&work);
595 struct sort_node * work_node = (struct sort_node *)work_item->object.util;
596 struct commit_list * parents = work_item->parents;
597
598 while (parents) {
599 struct commit * parent=parents->item;
600 struct sort_node * pn = (struct sort_node *)parent->object.util;
601
602 if (pn) {
603 /*
604 * parents are only enqueued for emission
605 * when all their children have been emitted thereby
606 * guaranteeing topological order.
607 */
608 pn->indegree--;
609 if (!pn->indegree)
610 commit_list_insert(parent, &work);
611 }
612 parents=parents->next;
613 }
614 /*
615 * work_item is a commit all of whose children
616 * have already been emitted. we can emit it now.
617 */
618 *pptr = work_node->list_item;
619 pptr = &(*pptr)->next;
620 *pptr = NULL;
621 work_item->object.util = NULL;
622 }
623 free(nodes);
624}