Merge branch 'jn/gitweb-blame' into pu
[git/spearce.git] / commit.c
blob1ae69352365bec86d7a1e0da5f12fdde5120b0b9
1 #include "cache.h"
2 #include "tag.h"
3 #include "commit.h"
4 #include "pkt-line.h"
5 #include "utf8.h"
6 #include "diff.h"
7 #include "revision.h"
8 #include "notes.h"
10 int save_commit_buffer = 1;
12 const char *commit_type = "commit";
14 static struct commit *check_commit(struct object *obj,
15 const unsigned char *sha1,
16 int quiet)
18 if (obj->type != OBJ_COMMIT) {
19 if (!quiet)
20 error("Object %s is a %s, not a commit",
21 sha1_to_hex(sha1), typename(obj->type));
22 return NULL;
24 return (struct commit *) obj;
27 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
28 int quiet)
30 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
32 if (!obj)
33 return NULL;
34 return check_commit(obj, sha1, quiet);
37 struct commit *lookup_commit_reference(const unsigned char *sha1)
39 return lookup_commit_reference_gently(sha1, 0);
42 struct commit *lookup_commit(const unsigned char *sha1)
44 struct object *obj = lookup_object(sha1);
45 if (!obj)
46 return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
47 if (!obj->type)
48 obj->type = OBJ_COMMIT;
49 return check_commit(obj, sha1, 0);
52 static unsigned long parse_commit_date(const char *buf, const char *tail)
54 const char *dateptr;
56 if (buf + 6 >= tail)
57 return 0;
58 if (memcmp(buf, "author", 6))
59 return 0;
60 while (buf < tail && *buf++ != '\n')
61 /* nada */;
62 if (buf + 9 >= tail)
63 return 0;
64 if (memcmp(buf, "committer", 9))
65 return 0;
66 while (buf < tail && *buf++ != '>')
67 /* nada */;
68 if (buf >= tail)
69 return 0;
70 dateptr = buf;
71 while (buf < tail && *buf++ != '\n')
72 /* nada */;
73 if (buf >= tail)
74 return 0;
75 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
76 return strtoul(dateptr, NULL, 10);
79 static struct commit_graft **commit_graft;
80 static int commit_graft_alloc, commit_graft_nr;
82 static int commit_graft_pos(const unsigned char *sha1)
84 int lo, hi;
85 lo = 0;
86 hi = commit_graft_nr;
87 while (lo < hi) {
88 int mi = (lo + hi) / 2;
89 struct commit_graft *graft = commit_graft[mi];
90 int cmp = hashcmp(sha1, graft->sha1);
91 if (!cmp)
92 return mi;
93 if (cmp < 0)
94 hi = mi;
95 else
96 lo = mi + 1;
98 return -lo - 1;
101 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
103 int pos = commit_graft_pos(graft->sha1);
105 if (0 <= pos) {
106 if (ignore_dups)
107 free(graft);
108 else {
109 free(commit_graft[pos]);
110 commit_graft[pos] = graft;
112 return 1;
114 pos = -pos - 1;
115 if (commit_graft_alloc <= ++commit_graft_nr) {
116 commit_graft_alloc = alloc_nr(commit_graft_alloc);
117 commit_graft = xrealloc(commit_graft,
118 sizeof(*commit_graft) *
119 commit_graft_alloc);
121 if (pos < commit_graft_nr)
122 memmove(commit_graft + pos + 1,
123 commit_graft + pos,
124 (commit_graft_nr - pos - 1) *
125 sizeof(*commit_graft));
126 commit_graft[pos] = graft;
127 return 0;
130 struct commit_graft *read_graft_line(char *buf, int len)
132 /* The format is just "Commit Parent1 Parent2 ...\n" */
133 int i;
134 struct commit_graft *graft = NULL;
136 if (buf[len-1] == '\n')
137 buf[--len] = 0;
138 if (buf[0] == '#' || buf[0] == '\0')
139 return NULL;
140 if ((len + 1) % 41) {
141 bad_graft_data:
142 error("bad graft data: %s", buf);
143 free(graft);
144 return NULL;
146 i = (len + 1) / 41 - 1;
147 graft = xmalloc(sizeof(*graft) + 20 * i);
148 graft->nr_parent = i;
149 if (get_sha1_hex(buf, graft->sha1))
150 goto bad_graft_data;
151 for (i = 40; i < len; i += 41) {
152 if (buf[i] != ' ')
153 goto bad_graft_data;
154 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
155 goto bad_graft_data;
157 return graft;
160 static int read_graft_file(const char *graft_file)
162 FILE *fp = fopen(graft_file, "r");
163 char buf[1024];
164 if (!fp)
165 return -1;
166 while (fgets(buf, sizeof(buf), fp)) {
167 /* The format is just "Commit Parent1 Parent2 ...\n" */
168 int len = strlen(buf);
169 struct commit_graft *graft = read_graft_line(buf, len);
170 if (!graft)
171 continue;
172 if (register_commit_graft(graft, 1))
173 error("duplicate graft data: %s", buf);
175 fclose(fp);
176 return 0;
179 static void prepare_commit_graft(void)
181 static int commit_graft_prepared;
182 char *graft_file;
184 if (commit_graft_prepared)
185 return;
186 graft_file = get_graft_file();
187 read_graft_file(graft_file);
188 /* make sure shallows are read */
189 is_repository_shallow();
190 commit_graft_prepared = 1;
193 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
195 int pos;
196 prepare_commit_graft();
197 pos = commit_graft_pos(sha1);
198 if (pos < 0)
199 return NULL;
200 return commit_graft[pos];
203 int write_shallow_commits(int fd, int use_pack_protocol)
205 int i, count = 0;
206 for (i = 0; i < commit_graft_nr; i++)
207 if (commit_graft[i]->nr_parent < 0) {
208 const char *hex =
209 sha1_to_hex(commit_graft[i]->sha1);
210 count++;
211 if (use_pack_protocol)
212 packet_write(fd, "shallow %s", hex);
213 else {
214 if (write_in_full(fd, hex, 40) != 40)
215 break;
216 if (write_str_in_full(fd, "\n") != 1)
217 break;
220 return count;
223 int unregister_shallow(const unsigned char *sha1)
225 int pos = commit_graft_pos(sha1);
226 if (pos < 0)
227 return -1;
228 if (pos + 1 < commit_graft_nr)
229 memcpy(commit_graft + pos, commit_graft + pos + 1,
230 sizeof(struct commit_graft *)
231 * (commit_graft_nr - pos - 1));
232 commit_graft_nr--;
233 return 0;
236 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
238 char *tail = buffer;
239 char *bufptr = buffer;
240 unsigned char parent[20];
241 struct commit_list **pptr;
242 struct commit_graft *graft;
244 if (item->object.parsed)
245 return 0;
246 item->object.parsed = 1;
247 tail += size;
248 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
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 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
255 pptr = &item->parents;
256 while (pop_commit(pptr))
257 ; /* clear anything from cache */
259 graft = lookup_commit_graft(item->object.sha1);
260 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
261 struct commit *new_parent;
263 if (tail <= bufptr + 48 ||
264 get_sha1_hex(bufptr + 7, parent) ||
265 bufptr[47] != '\n')
266 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
267 bufptr += 48;
269 * The clone is shallow if nr_parent < 0, and we must
270 * not traverse its real parents even when we unhide them.
272 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
273 continue;
274 new_parent = lookup_commit(parent);
275 if (new_parent)
276 pptr = &commit_list_insert(new_parent, pptr)->next;
278 if (graft) {
279 int i;
280 struct commit *new_parent;
281 for (i = 0; i < graft->nr_parent; i++) {
282 new_parent = lookup_commit(graft->parent[i]);
283 if (!new_parent)
284 continue;
285 pptr = &commit_list_insert(new_parent, pptr)->next;
288 item->date = parse_commit_date(bufptr, tail);
290 return 0;
293 int parse_commit(struct commit *item)
295 enum object_type type;
296 void *buffer;
297 unsigned long size;
298 int ret;
300 if (!item)
301 return -1;
302 if (item->object.parsed)
303 return 0;
304 buffer = read_sha1_file(item->object.sha1, &type, &size);
305 if (!buffer)
306 return error("Could not read %s",
307 sha1_to_hex(item->object.sha1));
308 if (type != OBJ_COMMIT) {
309 free(buffer);
310 return error("Object %s not a commit",
311 sha1_to_hex(item->object.sha1));
313 ret = parse_commit_buffer(item, buffer, size);
314 if (save_commit_buffer && !ret) {
315 item->buffer = buffer;
316 return 0;
318 free(buffer);
319 return ret;
322 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
324 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
325 new_list->item = item;
326 new_list->next = *list_p;
327 *list_p = new_list;
328 return new_list;
331 unsigned commit_list_count(const struct commit_list *l)
333 unsigned c = 0;
334 for (; l; l = l->next )
335 c++;
336 return c;
339 void free_commit_list(struct commit_list *list)
341 while (list) {
342 struct commit_list *temp = list;
343 list = temp->next;
344 free(temp);
348 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
350 struct commit_list **pp = list;
351 struct commit_list *p;
352 while ((p = *pp) != NULL) {
353 if (p->item->date < item->date) {
354 break;
356 pp = &p->next;
358 return commit_list_insert(item, pp);
362 void sort_by_date(struct commit_list **list)
364 struct commit_list *ret = NULL;
365 while (*list) {
366 insert_by_date((*list)->item, &ret);
367 *list = (*list)->next;
369 *list = ret;
372 struct commit *pop_most_recent_commit(struct commit_list **list,
373 unsigned int mark)
375 struct commit *ret = (*list)->item;
376 struct commit_list *parents = ret->parents;
377 struct commit_list *old = *list;
379 *list = (*list)->next;
380 free(old);
382 while (parents) {
383 struct commit *commit = parents->item;
384 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
385 commit->object.flags |= mark;
386 insert_by_date(commit, list);
388 parents = parents->next;
390 return ret;
393 void clear_commit_marks(struct commit *commit, unsigned int mark)
395 while (commit) {
396 struct commit_list *parents;
398 if (!(mark & commit->object.flags))
399 return;
401 commit->object.flags &= ~mark;
403 parents = commit->parents;
404 if (!parents)
405 return;
407 while ((parents = parents->next))
408 clear_commit_marks(parents->item, mark);
410 commit = commit->parents->item;
414 struct commit *pop_commit(struct commit_list **stack)
416 struct commit_list *top = *stack;
417 struct commit *item = top ? top->item : NULL;
419 if (top) {
420 *stack = top->next;
421 free(top);
423 return item;
427 * Performs an in-place topological sort on the list supplied.
429 void sort_in_topological_order(struct commit_list ** list, int lifo)
431 struct commit_list *next, *orig = *list;
432 struct commit_list *work, **insert;
433 struct commit_list **pptr;
435 if (!orig)
436 return;
437 *list = NULL;
439 /* Mark them and clear the indegree */
440 for (next = orig; next; next = next->next) {
441 struct commit *commit = next->item;
442 commit->indegree = 1;
445 /* update the indegree */
446 for (next = orig; next; next = next->next) {
447 struct commit_list * parents = next->item->parents;
448 while (parents) {
449 struct commit *parent = parents->item;
451 if (parent->indegree)
452 parent->indegree++;
453 parents = parents->next;
458 * find the tips
460 * tips are nodes not reachable from any other node in the list
462 * the tips serve as a starting set for the work queue.
464 work = NULL;
465 insert = &work;
466 for (next = orig; next; next = next->next) {
467 struct commit *commit = next->item;
469 if (commit->indegree == 1)
470 insert = &commit_list_insert(commit, insert)->next;
473 /* process the list in topological order */
474 if (!lifo)
475 sort_by_date(&work);
477 pptr = list;
478 *list = NULL;
479 while (work) {
480 struct commit *commit;
481 struct commit_list *parents, *work_item;
483 work_item = work;
484 work = work_item->next;
485 work_item->next = NULL;
487 commit = work_item->item;
488 for (parents = commit->parents; parents ; parents = parents->next) {
489 struct commit *parent=parents->item;
491 if (!parent->indegree)
492 continue;
495 * parents are only enqueued for emission
496 * when all their children have been emitted thereby
497 * guaranteeing topological order.
499 if (--parent->indegree == 1) {
500 if (!lifo)
501 insert_by_date(parent, &work);
502 else
503 commit_list_insert(parent, &work);
507 * work_item is a commit all of whose children
508 * have already been emitted. we can emit it now.
510 commit->indegree = 0;
511 *pptr = work_item;
512 pptr = &work_item->next;
516 /* merge-base stuff */
518 /* bits #0..15 in revision.h */
519 #define PARENT1 (1u<<16)
520 #define PARENT2 (1u<<17)
521 #define STALE (1u<<18)
522 #define RESULT (1u<<19)
524 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
526 static struct commit *interesting(struct commit_list *list)
528 while (list) {
529 struct commit *commit = list->item;
530 list = list->next;
531 if (commit->object.flags & STALE)
532 continue;
533 return commit;
535 return NULL;
538 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
540 struct commit_list *list = NULL;
541 struct commit_list *result = NULL;
542 int i;
544 for (i = 0; i < n; i++) {
545 if (one == twos[i])
547 * We do not mark this even with RESULT so we do not
548 * have to clean it up.
550 return commit_list_insert(one, &result);
553 if (parse_commit(one))
554 return NULL;
555 for (i = 0; i < n; i++) {
556 if (parse_commit(twos[i]))
557 return NULL;
560 one->object.flags |= PARENT1;
561 insert_by_date(one, &list);
562 for (i = 0; i < n; i++) {
563 twos[i]->object.flags |= PARENT2;
564 insert_by_date(twos[i], &list);
567 while (interesting(list)) {
568 struct commit *commit;
569 struct commit_list *parents;
570 struct commit_list *next;
571 int flags;
573 commit = list->item;
574 next = list->next;
575 free(list);
576 list = next;
578 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
579 if (flags == (PARENT1 | PARENT2)) {
580 if (!(commit->object.flags & RESULT)) {
581 commit->object.flags |= RESULT;
582 insert_by_date(commit, &result);
584 /* Mark parents of a found merge stale */
585 flags |= STALE;
587 parents = commit->parents;
588 while (parents) {
589 struct commit *p = parents->item;
590 parents = parents->next;
591 if ((p->object.flags & flags) == flags)
592 continue;
593 if (parse_commit(p))
594 return NULL;
595 p->object.flags |= flags;
596 insert_by_date(p, &list);
600 /* Clean up the result to remove stale ones */
601 free_commit_list(list);
602 list = result; result = NULL;
603 while (list) {
604 struct commit_list *next = list->next;
605 if (!(list->item->object.flags & STALE))
606 insert_by_date(list->item, &result);
607 free(list);
608 list = next;
610 return result;
613 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
615 struct commit_list *i, *j, *k, *ret = NULL;
616 struct commit_list **pptr = &ret;
618 for (i = in; i; i = i->next) {
619 if (!ret)
620 pptr = &commit_list_insert(i->item, pptr)->next;
621 else {
622 struct commit_list *new = NULL, *end = NULL;
624 for (j = ret; j; j = j->next) {
625 struct commit_list *bases;
626 bases = get_merge_bases(i->item, j->item, 1);
627 if (!new)
628 new = bases;
629 else
630 end->next = bases;
631 for (k = bases; k; k = k->next)
632 end = k;
634 ret = new;
637 return ret;
640 struct commit_list *get_merge_bases_many(struct commit *one,
641 int n,
642 struct commit **twos,
643 int cleanup)
645 struct commit_list *list;
646 struct commit **rslt;
647 struct commit_list *result;
648 int cnt, i, j;
650 result = merge_bases_many(one, n, twos);
651 for (i = 0; i < n; i++) {
652 if (one == twos[i])
653 return result;
655 if (!result || !result->next) {
656 if (cleanup) {
657 clear_commit_marks(one, all_flags);
658 for (i = 0; i < n; i++)
659 clear_commit_marks(twos[i], all_flags);
661 return result;
664 /* There are more than one */
665 cnt = 0;
666 list = result;
667 while (list) {
668 list = list->next;
669 cnt++;
671 rslt = xcalloc(cnt, sizeof(*rslt));
672 for (list = result, i = 0; list; list = list->next)
673 rslt[i++] = list->item;
674 free_commit_list(result);
676 clear_commit_marks(one, all_flags);
677 for (i = 0; i < n; i++)
678 clear_commit_marks(twos[i], all_flags);
679 for (i = 0; i < cnt - 1; i++) {
680 for (j = i+1; j < cnt; j++) {
681 if (!rslt[i] || !rslt[j])
682 continue;
683 result = merge_bases_many(rslt[i], 1, &rslt[j]);
684 clear_commit_marks(rslt[i], all_flags);
685 clear_commit_marks(rslt[j], all_flags);
686 for (list = result; list; list = list->next) {
687 if (rslt[i] == list->item)
688 rslt[i] = NULL;
689 if (rslt[j] == list->item)
690 rslt[j] = NULL;
695 /* Surviving ones in rslt[] are the independent results */
696 result = NULL;
697 for (i = 0; i < cnt; i++) {
698 if (rslt[i])
699 insert_by_date(rslt[i], &result);
701 free(rslt);
702 return result;
705 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
706 int cleanup)
708 return get_merge_bases_many(one, 1, &two, cleanup);
711 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
713 if (!with_commit)
714 return 1;
715 while (with_commit) {
716 struct commit *other;
718 other = with_commit->item;
719 with_commit = with_commit->next;
720 if (in_merge_bases(other, &commit, 1))
721 return 1;
723 return 0;
726 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
728 struct commit_list *bases, *b;
729 int ret = 0;
731 if (num == 1)
732 bases = get_merge_bases(commit, *reference, 1);
733 else
734 die("not yet");
735 for (b = bases; b; b = b->next) {
736 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
737 ret = 1;
738 break;
742 free_commit_list(bases);
743 return ret;
746 struct commit_list *reduce_heads(struct commit_list *heads)
748 struct commit_list *p;
749 struct commit_list *result = NULL, **tail = &result;
750 struct commit **other;
751 size_t num_head, num_other;
753 if (!heads)
754 return NULL;
756 /* Avoid unnecessary reallocations */
757 for (p = heads, num_head = 0; p; p = p->next)
758 num_head++;
759 other = xcalloc(sizeof(*other), num_head);
761 /* For each commit, see if it can be reached by others */
762 for (p = heads; p; p = p->next) {
763 struct commit_list *q, *base;
765 /* Do we already have this in the result? */
766 for (q = result; q; q = q->next)
767 if (p->item == q->item)
768 break;
769 if (q)
770 continue;
772 num_other = 0;
773 for (q = heads; q; q = q->next) {
774 if (p->item == q->item)
775 continue;
776 other[num_other++] = q->item;
778 if (num_other)
779 base = get_merge_bases_many(p->item, num_other, other, 1);
780 else
781 base = NULL;
783 * If p->item does not have anything common with other
784 * commits, there won't be any merge base. If it is
785 * reachable from some of the others, p->item will be
786 * the merge base. If its history is connected with
787 * others, but p->item is not reachable by others, we
788 * will get something other than p->item back.
790 if (!base || (base->item != p->item))
791 tail = &(commit_list_insert(p->item, tail)->next);
792 free_commit_list(base);
794 free(other);
795 return result;