Merge part of 'js/fmt-patch' for RFC2822 dates into 'sp/reflog'
[git/gitweb.git] / commit.c
blob8317f6d26da8bc684c020a3116cb5685916f278d
1 #include "cache.h"
2 #include "tag.h"
3 #include "commit.h"
5 int save_commit_buffer = 1;
7 struct sort_node
9 /*
10 * the number of children of the associated commit
11 * that also occur in the list being sorted.
13 unsigned int indegree;
16 * reference to original list item that we will re-use
17 * on output.
19 struct commit_list * list_item;
23 const char *commit_type = "commit";
25 struct cmt_fmt_map {
26 const char *n;
27 size_t cmp_len;
28 enum cmit_fmt v;
29 } cmt_fmts[] = {
30 { "raw", 1, CMIT_FMT_RAW },
31 { "medium", 1, CMIT_FMT_MEDIUM },
32 { "short", 1, CMIT_FMT_SHORT },
33 { "email", 1, CMIT_FMT_EMAIL },
34 { "full", 5, CMIT_FMT_FULL },
35 { "fuller", 5, CMIT_FMT_FULLER },
36 { "oneline", 1, CMIT_FMT_ONELINE },
39 enum cmit_fmt get_commit_format(const char *arg)
41 int i;
43 if (!arg || !*arg)
44 return CMIT_FMT_DEFAULT;
45 if (*arg == '=')
46 arg++;
47 for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
48 if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len))
49 return cmt_fmts[i].v;
52 die("invalid --pretty format: %s", arg);
55 static struct commit *check_commit(struct object *obj,
56 const unsigned char *sha1,
57 int quiet)
59 if (obj->type != commit_type) {
60 if (!quiet)
61 error("Object %s is a %s, not a commit",
62 sha1_to_hex(sha1), obj->type);
63 return NULL;
65 return (struct commit *) obj;
68 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
69 int quiet)
71 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
73 if (!obj)
74 return NULL;
75 return check_commit(obj, sha1, quiet);
78 struct commit *lookup_commit_reference(const unsigned char *sha1)
80 return lookup_commit_reference_gently(sha1, 0);
83 struct commit *lookup_commit(const unsigned char *sha1)
85 struct object *obj = lookup_object(sha1);
86 if (!obj) {
87 struct commit *ret = xcalloc(1, sizeof(struct commit));
88 created_object(sha1, &ret->object);
89 ret->object.type = commit_type;
90 return ret;
92 if (!obj->type)
93 obj->type = commit_type;
94 return check_commit(obj, sha1, 0);
97 static unsigned long parse_commit_date(const char *buf)
99 unsigned long date;
101 if (memcmp(buf, "author", 6))
102 return 0;
103 while (*buf++ != '\n')
104 /* nada */;
105 if (memcmp(buf, "committer", 9))
106 return 0;
107 while (*buf++ != '>')
108 /* nada */;
109 date = strtoul(buf, NULL, 10);
110 if (date == ULONG_MAX)
111 date = 0;
112 return date;
115 static struct commit_graft **commit_graft;
116 static int commit_graft_alloc, commit_graft_nr;
118 static int commit_graft_pos(const unsigned char *sha1)
120 int lo, hi;
121 lo = 0;
122 hi = commit_graft_nr;
123 while (lo < hi) {
124 int mi = (lo + hi) / 2;
125 struct commit_graft *graft = commit_graft[mi];
126 int cmp = memcmp(sha1, graft->sha1, 20);
127 if (!cmp)
128 return mi;
129 if (cmp < 0)
130 hi = mi;
131 else
132 lo = mi + 1;
134 return -lo - 1;
137 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
139 int pos = commit_graft_pos(graft->sha1);
141 if (0 <= pos) {
142 if (ignore_dups)
143 free(graft);
144 else {
145 free(commit_graft[pos]);
146 commit_graft[pos] = graft;
148 return 1;
150 pos = -pos - 1;
151 if (commit_graft_alloc <= ++commit_graft_nr) {
152 commit_graft_alloc = alloc_nr(commit_graft_alloc);
153 commit_graft = xrealloc(commit_graft,
154 sizeof(*commit_graft) *
155 commit_graft_alloc);
157 if (pos < commit_graft_nr)
158 memmove(commit_graft + pos + 1,
159 commit_graft + pos,
160 (commit_graft_nr - pos - 1) *
161 sizeof(*commit_graft));
162 commit_graft[pos] = graft;
163 return 0;
166 struct commit_graft *read_graft_line(char *buf, int len)
168 /* The format is just "Commit Parent1 Parent2 ...\n" */
169 int i;
170 struct commit_graft *graft = NULL;
172 if (buf[len-1] == '\n')
173 buf[--len] = 0;
174 if (buf[0] == '#' || buf[0] == '\0')
175 return NULL;
176 if ((len + 1) % 41) {
177 bad_graft_data:
178 error("bad graft data: %s", buf);
179 free(graft);
180 return NULL;
182 i = (len + 1) / 41 - 1;
183 graft = xmalloc(sizeof(*graft) + 20 * i);
184 graft->nr_parent = i;
185 if (get_sha1_hex(buf, graft->sha1))
186 goto bad_graft_data;
187 for (i = 40; i < len; i += 41) {
188 if (buf[i] != ' ')
189 goto bad_graft_data;
190 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
191 goto bad_graft_data;
193 return graft;
196 int read_graft_file(const char *graft_file)
198 FILE *fp = fopen(graft_file, "r");
199 char buf[1024];
200 if (!fp)
201 return -1;
202 while (fgets(buf, sizeof(buf), fp)) {
203 /* The format is just "Commit Parent1 Parent2 ...\n" */
204 int len = strlen(buf);
205 struct commit_graft *graft = read_graft_line(buf, len);
206 if (!graft)
207 continue;
208 if (register_commit_graft(graft, 1))
209 error("duplicate graft data: %s", buf);
211 fclose(fp);
212 return 0;
215 static void prepare_commit_graft(void)
217 static int commit_graft_prepared;
218 char *graft_file;
220 if (commit_graft_prepared)
221 return;
222 graft_file = get_graft_file();
223 read_graft_file(graft_file);
224 commit_graft_prepared = 1;
227 static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
229 int pos;
230 prepare_commit_graft();
231 pos = commit_graft_pos(sha1);
232 if (pos < 0)
233 return NULL;
234 return commit_graft[pos];
237 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
239 char *bufptr = buffer;
240 unsigned char parent[20];
241 struct commit_list **pptr;
242 struct commit_graft *graft;
243 unsigned n_refs = 0;
245 if (item->object.parsed)
246 return 0;
247 item->object.parsed = 1;
248 if (memcmp(bufptr, "tree ", 5))
249 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
250 if (get_sha1_hex(bufptr + 5, parent) < 0)
251 return error("bad tree pointer in commit %s",
252 sha1_to_hex(item->object.sha1));
253 item->tree = lookup_tree(parent);
254 if (item->tree)
255 n_refs++;
256 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
257 pptr = &item->parents;
259 graft = lookup_commit_graft(item->object.sha1);
260 while (!memcmp(bufptr, "parent ", 7)) {
261 struct commit *new_parent;
263 if (get_sha1_hex(bufptr + 7, parent) || bufptr[47] != '\n')
264 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
265 bufptr += 48;
266 if (graft)
267 continue;
268 new_parent = lookup_commit(parent);
269 if (new_parent) {
270 pptr = &commit_list_insert(new_parent, pptr)->next;
271 n_refs++;
274 if (graft) {
275 int i;
276 struct commit *new_parent;
277 for (i = 0; i < graft->nr_parent; i++) {
278 new_parent = lookup_commit(graft->parent[i]);
279 if (!new_parent)
280 continue;
281 pptr = &commit_list_insert(new_parent, pptr)->next;
282 n_refs++;
285 item->date = parse_commit_date(bufptr);
287 if (track_object_refs) {
288 unsigned i = 0;
289 struct commit_list *p;
290 struct object_refs *refs = alloc_object_refs(n_refs);
291 if (item->tree)
292 refs->ref[i++] = &item->tree->object;
293 for (p = item->parents; p; p = p->next)
294 refs->ref[i++] = &p->item->object;
295 set_object_refs(&item->object, refs);
298 return 0;
301 int parse_commit(struct commit *item)
303 char type[20];
304 void *buffer;
305 unsigned long size;
306 int ret;
308 if (item->object.parsed)
309 return 0;
310 buffer = read_sha1_file(item->object.sha1, type, &size);
311 if (!buffer)
312 return error("Could not read %s",
313 sha1_to_hex(item->object.sha1));
314 if (strcmp(type, commit_type)) {
315 free(buffer);
316 return error("Object %s not a commit",
317 sha1_to_hex(item->object.sha1));
319 ret = parse_commit_buffer(item, buffer, size);
320 if (save_commit_buffer && !ret) {
321 item->buffer = buffer;
322 return 0;
324 free(buffer);
325 return ret;
328 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
330 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
331 new_list->item = item;
332 new_list->next = *list_p;
333 *list_p = new_list;
334 return new_list;
337 void free_commit_list(struct commit_list *list)
339 while (list) {
340 struct commit_list *temp = list;
341 list = temp->next;
342 free(temp);
346 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
348 struct commit_list **pp = list;
349 struct commit_list *p;
350 while ((p = *pp) != NULL) {
351 if (p->item->date < item->date) {
352 break;
354 pp = &p->next;
356 return commit_list_insert(item, pp);
360 void sort_by_date(struct commit_list **list)
362 struct commit_list *ret = NULL;
363 while (*list) {
364 insert_by_date((*list)->item, &ret);
365 *list = (*list)->next;
367 *list = ret;
370 struct commit *pop_most_recent_commit(struct commit_list **list,
371 unsigned int mark)
373 struct commit *ret = (*list)->item;
374 struct commit_list *parents = ret->parents;
375 struct commit_list *old = *list;
377 *list = (*list)->next;
378 free(old);
380 while (parents) {
381 struct commit *commit = parents->item;
382 parse_commit(commit);
383 if (!(commit->object.flags & mark)) {
384 commit->object.flags |= mark;
385 insert_by_date(commit, list);
387 parents = parents->next;
389 return ret;
392 void clear_commit_marks(struct commit *commit, unsigned int mark)
394 struct commit_list *parents;
396 parents = commit->parents;
397 commit->object.flags &= ~mark;
398 while (parents) {
399 struct commit *parent = parents->item;
400 if (parent && parent->object.parsed &&
401 (parent->object.flags & mark))
402 clear_commit_marks(parent, mark);
403 parents = parents->next;
408 * Generic support for pretty-printing the header
410 static int get_one_line(const char *msg, unsigned long len)
412 int ret = 0;
414 while (len--) {
415 char c = *msg++;
416 if (!c)
417 break;
418 ret++;
419 if (c == '\n')
420 break;
422 return ret;
425 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
427 char *date;
428 int namelen;
429 unsigned long time;
430 int tz, ret;
431 const char *filler = " ";
433 if (fmt == CMIT_FMT_ONELINE)
434 return 0;
435 date = strchr(line, '>');
436 if (!date)
437 return 0;
438 namelen = ++date - line;
439 time = strtoul(date, &date, 10);
440 tz = strtol(date, NULL, 10);
442 if (fmt == CMIT_FMT_EMAIL) {
443 what = "From";
444 filler = "";
446 ret = sprintf(buf, "%s: %.*s%.*s\n", what,
447 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
448 filler, namelen, line);
449 switch (fmt) {
450 case CMIT_FMT_MEDIUM:
451 ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
452 break;
453 case CMIT_FMT_EMAIL:
454 ret += sprintf(buf + ret, "Date: %s\n",
455 show_rfc2822_date(time, tz));
456 break;
457 case CMIT_FMT_FULLER:
458 ret += sprintf(buf + ret, "%sDate: %s\n", what, show_date(time, tz));
459 break;
460 default:
461 /* notin' */
462 break;
464 return ret;
467 static int is_empty_line(const char *line, int *len_p)
469 int len = *len_p;
470 while (len && isspace(line[len-1]))
471 len--;
472 *len_p = len;
473 return !len;
476 static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *commit, int abbrev)
478 struct commit_list *parent = commit->parents;
479 int offset;
481 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
482 !parent || !parent->next)
483 return 0;
485 offset = sprintf(buf, "Merge:");
487 while (parent) {
488 struct commit *p = parent->item;
489 const char *hex = abbrev
490 ? find_unique_abbrev(p->object.sha1, abbrev)
491 : sha1_to_hex(p->object.sha1);
492 char *dots = (abbrev && strlen(hex) != 40) ? "..." : "";
493 parent = parent->next;
495 offset += sprintf(buf + offset, " %s%s", hex, dots);
497 buf[offset++] = '\n';
498 return offset;
501 unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, unsigned long len, char *buf, unsigned long space, int abbrev)
503 int hdr = 1, body = 0;
504 unsigned long offset = 0;
505 int indent = 4;
506 int parents_shown = 0;
507 const char *msg = commit->buffer;
508 const char *subject = NULL;
510 if (fmt == CMIT_FMT_EMAIL)
511 subject = "Subject: [PATCH] ";
512 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
513 indent = 0;
515 for (;;) {
516 const char *line = msg;
517 int linelen = get_one_line(msg, len);
519 if (!linelen)
520 break;
523 * We want some slop for indentation and a possible
524 * final "...". Thus the "+ 20".
526 if (offset + linelen + 20 > space) {
527 memcpy(buf + offset, " ...\n", 8);
528 offset += 8;
529 break;
532 msg += linelen;
533 len -= linelen;
534 if (hdr) {
535 if (linelen == 1) {
536 hdr = 0;
537 if ((fmt != CMIT_FMT_ONELINE) && !subject)
538 buf[offset++] = '\n';
539 continue;
541 if (fmt == CMIT_FMT_RAW) {
542 memcpy(buf + offset, line, linelen);
543 offset += linelen;
544 continue;
546 if (!memcmp(line, "parent ", 7)) {
547 if (linelen != 48)
548 die("bad parent line in commit");
549 continue;
552 if (!parents_shown) {
553 offset += add_merge_info(fmt, buf + offset,
554 commit, abbrev);
555 parents_shown = 1;
556 continue;
559 * MEDIUM == DEFAULT shows only author with dates.
560 * FULL shows both authors but not dates.
561 * FULLER shows both authors and dates.
563 if (!memcmp(line, "author ", 7))
564 offset += add_user_info("Author", fmt,
565 buf + offset,
566 line + 7);
567 if (!memcmp(line, "committer ", 10) &&
568 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
569 offset += add_user_info("Commit", fmt,
570 buf + offset,
571 line + 10);
572 continue;
575 if (is_empty_line(line, &linelen)) {
576 if (!body)
577 continue;
578 if (subject)
579 continue;
580 if (fmt == CMIT_FMT_SHORT)
581 break;
582 } else {
583 body = 1;
586 if (subject) {
587 int slen = strlen(subject);
588 memcpy(buf + offset, subject, slen);
589 offset += slen;
591 memset(buf + offset, ' ', indent);
592 memcpy(buf + offset + indent, line, linelen);
593 offset += linelen + indent;
594 buf[offset++] = '\n';
595 if (fmt == CMIT_FMT_ONELINE)
596 break;
597 subject = NULL;
599 while (offset && isspace(buf[offset-1]))
600 offset--;
601 /* Make sure there is an EOLN for the non-oneline case */
602 if (fmt != CMIT_FMT_ONELINE)
603 buf[offset++] = '\n';
604 buf[offset] = '\0';
605 return offset;
608 struct commit *pop_commit(struct commit_list **stack)
610 struct commit_list *top = *stack;
611 struct commit *item = top ? top->item : NULL;
613 if (top) {
614 *stack = top->next;
615 free(top);
617 return item;
620 int count_parents(struct commit * commit)
622 int count = 0;
623 struct commit_list * parents = commit->parents;
624 for (count=0;parents; parents=parents->next,count++)
626 return count;
629 void topo_sort_default_setter(struct commit *c, void *data)
631 c->object.util = data;
634 void *topo_sort_default_getter(struct commit *c)
636 return c->object.util;
640 * Performs an in-place topological sort on the list supplied.
642 void sort_in_topological_order(struct commit_list ** list, int lifo)
644 sort_in_topological_order_fn(list, lifo, topo_sort_default_setter,
645 topo_sort_default_getter);
648 void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
649 topo_sort_set_fn_t setter,
650 topo_sort_get_fn_t getter)
652 struct commit_list * next = *list;
653 struct commit_list * work = NULL, **insert;
654 struct commit_list ** pptr = list;
655 struct sort_node * nodes;
656 struct sort_node * next_nodes;
657 int count = 0;
659 /* determine the size of the list */
660 while (next) {
661 next = next->next;
662 count++;
665 if (!count)
666 return;
667 /* allocate an array to help sort the list */
668 nodes = xcalloc(count, sizeof(*nodes));
669 /* link the list to the array */
670 next_nodes = nodes;
671 next=*list;
672 while (next) {
673 next_nodes->list_item = next;
674 setter(next->item, next_nodes);
675 next_nodes++;
676 next = next->next;
678 /* update the indegree */
679 next=*list;
680 while (next) {
681 struct commit_list * parents = next->item->parents;
682 while (parents) {
683 struct commit * parent=parents->item;
684 struct sort_node * pn = (struct sort_node *) getter(parent);
686 if (pn)
687 pn->indegree++;
688 parents=parents->next;
690 next=next->next;
693 * find the tips
695 * tips are nodes not reachable from any other node in the list
697 * the tips serve as a starting set for the work queue.
699 next=*list;
700 insert = &work;
701 while (next) {
702 struct sort_node * node = (struct sort_node *) getter(next->item);
704 if (node->indegree == 0) {
705 insert = &commit_list_insert(next->item, insert)->next;
707 next=next->next;
710 /* process the list in topological order */
711 if (!lifo)
712 sort_by_date(&work);
713 while (work) {
714 struct commit * work_item = pop_commit(&work);
715 struct sort_node * work_node = (struct sort_node *) getter(work_item);
716 struct commit_list * parents = work_item->parents;
718 while (parents) {
719 struct commit * parent=parents->item;
720 struct sort_node * pn = (struct sort_node *) getter(parent);
722 if (pn) {
724 * parents are only enqueued for emission
725 * when all their children have been emitted thereby
726 * guaranteeing topological order.
728 pn->indegree--;
729 if (!pn->indegree) {
730 if (!lifo)
731 insert_by_date(parent, &work);
732 else
733 commit_list_insert(parent, &work);
736 parents=parents->next;
739 * work_item is a commit all of whose children
740 * have already been emitted. we can emit it now.
742 *pptr = work_node->list_item;
743 pptr = &(*pptr)->next;
744 *pptr = NULL;
745 setter(work_item, NULL);
747 free(nodes);