tree.c: allow read_tree_recursive() to traverse gitlink entries
[git/jnareb-git.git] / commit.c
blobcf72143f58346c93879a98709d5d6fa8a0981b40
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 unsigned long date;
55 const char *dateptr;
57 if (buf + 6 >= tail)
58 return 0;
59 if (memcmp(buf, "author", 6))
60 return 0;
61 while (buf < tail && *buf++ != '\n')
62 /* nada */;
63 if (buf + 9 >= tail)
64 return 0;
65 if (memcmp(buf, "committer", 9))
66 return 0;
67 while (buf < tail && *buf++ != '>')
68 /* nada */;
69 if (buf >= tail)
70 return 0;
71 dateptr = buf;
72 while (buf < tail && *buf++ != '\n')
73 /* nada */;
74 if (buf >= tail)
75 return 0;
76 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
77 date = strtoul(dateptr, NULL, 10);
78 if (date == ULONG_MAX)
79 date = 0;
80 return date;
83 static struct commit_graft **commit_graft;
84 static int commit_graft_alloc, commit_graft_nr;
86 static int commit_graft_pos(const unsigned char *sha1)
88 int lo, hi;
89 lo = 0;
90 hi = commit_graft_nr;
91 while (lo < hi) {
92 int mi = (lo + hi) / 2;
93 struct commit_graft *graft = commit_graft[mi];
94 int cmp = hashcmp(sha1, graft->sha1);
95 if (!cmp)
96 return mi;
97 if (cmp < 0)
98 hi = mi;
99 else
100 lo = mi + 1;
102 return -lo - 1;
105 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
107 int pos = commit_graft_pos(graft->sha1);
109 if (0 <= pos) {
110 if (ignore_dups)
111 free(graft);
112 else {
113 free(commit_graft[pos]);
114 commit_graft[pos] = graft;
116 return 1;
118 pos = -pos - 1;
119 if (commit_graft_alloc <= ++commit_graft_nr) {
120 commit_graft_alloc = alloc_nr(commit_graft_alloc);
121 commit_graft = xrealloc(commit_graft,
122 sizeof(*commit_graft) *
123 commit_graft_alloc);
125 if (pos < commit_graft_nr)
126 memmove(commit_graft + pos + 1,
127 commit_graft + pos,
128 (commit_graft_nr - pos - 1) *
129 sizeof(*commit_graft));
130 commit_graft[pos] = graft;
131 return 0;
134 struct commit_graft *read_graft_line(char *buf, int len)
136 /* The format is just "Commit Parent1 Parent2 ...\n" */
137 int i;
138 struct commit_graft *graft = NULL;
140 if (buf[len-1] == '\n')
141 buf[--len] = 0;
142 if (buf[0] == '#' || buf[0] == '\0')
143 return NULL;
144 if ((len + 1) % 41) {
145 bad_graft_data:
146 error("bad graft data: %s", buf);
147 free(graft);
148 return NULL;
150 i = (len + 1) / 41 - 1;
151 graft = xmalloc(sizeof(*graft) + 20 * i);
152 graft->nr_parent = i;
153 if (get_sha1_hex(buf, graft->sha1))
154 goto bad_graft_data;
155 for (i = 40; i < len; i += 41) {
156 if (buf[i] != ' ')
157 goto bad_graft_data;
158 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
159 goto bad_graft_data;
161 return graft;
164 static int read_graft_file(const char *graft_file)
166 FILE *fp = fopen(graft_file, "r");
167 char buf[1024];
168 if (!fp)
169 return -1;
170 while (fgets(buf, sizeof(buf), fp)) {
171 /* The format is just "Commit Parent1 Parent2 ...\n" */
172 int len = strlen(buf);
173 struct commit_graft *graft = read_graft_line(buf, len);
174 if (!graft)
175 continue;
176 if (register_commit_graft(graft, 1))
177 error("duplicate graft data: %s", buf);
179 fclose(fp);
180 return 0;
183 static void prepare_commit_graft(void)
185 static int commit_graft_prepared;
186 char *graft_file;
188 if (commit_graft_prepared)
189 return;
190 graft_file = get_graft_file();
191 read_graft_file(graft_file);
192 /* make sure shallows are read */
193 is_repository_shallow();
194 commit_graft_prepared = 1;
197 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
199 int pos;
200 prepare_commit_graft();
201 pos = commit_graft_pos(sha1);
202 if (pos < 0)
203 return NULL;
204 return commit_graft[pos];
207 int write_shallow_commits(int fd, int use_pack_protocol)
209 int i, count = 0;
210 for (i = 0; i < commit_graft_nr; i++)
211 if (commit_graft[i]->nr_parent < 0) {
212 const char *hex =
213 sha1_to_hex(commit_graft[i]->sha1);
214 count++;
215 if (use_pack_protocol)
216 packet_write(fd, "shallow %s", hex);
217 else {
218 if (write_in_full(fd, hex, 40) != 40)
219 break;
220 if (write_in_full(fd, "\n", 1) != 1)
221 break;
224 return count;
227 int unregister_shallow(const unsigned char *sha1)
229 int pos = commit_graft_pos(sha1);
230 if (pos < 0)
231 return -1;
232 if (pos + 1 < commit_graft_nr)
233 memcpy(commit_graft + pos, commit_graft + pos + 1,
234 sizeof(struct commit_graft *)
235 * (commit_graft_nr - pos - 1));
236 commit_graft_nr--;
237 return 0;
240 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
242 char *tail = buffer;
243 char *bufptr = buffer;
244 unsigned char parent[20];
245 struct commit_list **pptr;
246 struct commit_graft *graft;
248 if (item->object.parsed)
249 return 0;
250 item->object.parsed = 1;
251 tail += size;
252 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
253 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
254 if (get_sha1_hex(bufptr + 5, parent) < 0)
255 return error("bad tree pointer in commit %s",
256 sha1_to_hex(item->object.sha1));
257 item->tree = lookup_tree(parent);
258 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
259 pptr = &item->parents;
261 graft = lookup_commit_graft(item->object.sha1);
262 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
263 struct commit *new_parent;
265 if (tail <= bufptr + 48 ||
266 get_sha1_hex(bufptr + 7, parent) ||
267 bufptr[47] != '\n')
268 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
269 bufptr += 48;
270 if (graft)
271 continue;
272 new_parent = lookup_commit(parent);
273 if (new_parent)
274 pptr = &commit_list_insert(new_parent, pptr)->next;
276 if (graft) {
277 int i;
278 struct commit *new_parent;
279 for (i = 0; i < graft->nr_parent; i++) {
280 new_parent = lookup_commit(graft->parent[i]);
281 if (!new_parent)
282 continue;
283 pptr = &commit_list_insert(new_parent, pptr)->next;
286 item->date = parse_commit_date(bufptr, tail);
288 return 0;
291 int parse_commit(struct commit *item)
293 enum object_type type;
294 void *buffer;
295 unsigned long size;
296 int ret;
298 if (!item)
299 return -1;
300 if (item->object.parsed)
301 return 0;
302 buffer = read_sha1_file(item->object.sha1, &type, &size);
303 if (!buffer)
304 return error("Could not read %s",
305 sha1_to_hex(item->object.sha1));
306 if (type != OBJ_COMMIT) {
307 free(buffer);
308 return error("Object %s not a commit",
309 sha1_to_hex(item->object.sha1));
311 ret = parse_commit_buffer(item, buffer, size);
312 if (save_commit_buffer && !ret) {
313 item->buffer = buffer;
314 return 0;
316 free(buffer);
317 return ret;
320 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
322 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
323 new_list->item = item;
324 new_list->next = *list_p;
325 *list_p = new_list;
326 return new_list;
329 unsigned commit_list_count(const struct commit_list *l)
331 unsigned c = 0;
332 for (; l; l = l->next )
333 c++;
334 return c;
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 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
383 commit->object.flags |= mark;
384 insert_by_date(commit, list);
386 parents = parents->next;
388 return ret;
391 void clear_commit_marks(struct commit *commit, unsigned int mark)
393 while (commit) {
394 struct commit_list *parents;
396 if (!(mark & commit->object.flags))
397 return;
399 commit->object.flags &= ~mark;
401 parents = commit->parents;
402 if (!parents)
403 return;
405 while ((parents = parents->next))
406 clear_commit_marks(parents->item, mark);
408 commit = commit->parents->item;
412 struct commit *pop_commit(struct commit_list **stack)
414 struct commit_list *top = *stack;
415 struct commit *item = top ? top->item : NULL;
417 if (top) {
418 *stack = top->next;
419 free(top);
421 return item;
425 * Performs an in-place topological sort on the list supplied.
427 void sort_in_topological_order(struct commit_list ** list, int lifo)
429 struct commit_list *next, *orig = *list;
430 struct commit_list *work, **insert;
431 struct commit_list **pptr;
433 if (!orig)
434 return;
435 *list = NULL;
437 /* Mark them and clear the indegree */
438 for (next = orig; next; next = next->next) {
439 struct commit *commit = next->item;
440 commit->indegree = 1;
443 /* update the indegree */
444 for (next = orig; next; next = next->next) {
445 struct commit_list * parents = next->item->parents;
446 while (parents) {
447 struct commit *parent = parents->item;
449 if (parent->indegree)
450 parent->indegree++;
451 parents = parents->next;
456 * find the tips
458 * tips are nodes not reachable from any other node in the list
460 * the tips serve as a starting set for the work queue.
462 work = NULL;
463 insert = &work;
464 for (next = orig; next; next = next->next) {
465 struct commit *commit = next->item;
467 if (commit->indegree == 1)
468 insert = &commit_list_insert(commit, insert)->next;
471 /* process the list in topological order */
472 if (!lifo)
473 sort_by_date(&work);
475 pptr = list;
476 *list = NULL;
477 while (work) {
478 struct commit *commit;
479 struct commit_list *parents, *work_item;
481 work_item = work;
482 work = work_item->next;
483 work_item->next = NULL;
485 commit = work_item->item;
486 for (parents = commit->parents; parents ; parents = parents->next) {
487 struct commit *parent=parents->item;
489 if (!parent->indegree)
490 continue;
493 * parents are only enqueued for emission
494 * when all their children have been emitted thereby
495 * guaranteeing topological order.
497 if (--parent->indegree == 1) {
498 if (!lifo)
499 insert_by_date(parent, &work);
500 else
501 commit_list_insert(parent, &work);
505 * work_item is a commit all of whose children
506 * have already been emitted. we can emit it now.
508 commit->indegree = 0;
509 *pptr = work_item;
510 pptr = &work_item->next;
514 /* merge-base stuff */
516 /* bits #0..15 in revision.h */
517 #define PARENT1 (1u<<16)
518 #define PARENT2 (1u<<17)
519 #define STALE (1u<<18)
520 #define RESULT (1u<<19)
522 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
524 static struct commit *interesting(struct commit_list *list)
526 while (list) {
527 struct commit *commit = list->item;
528 list = list->next;
529 if (commit->object.flags & STALE)
530 continue;
531 return commit;
533 return NULL;
536 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
538 struct commit_list *list = NULL;
539 struct commit_list *result = NULL;
540 int i;
542 for (i = 0; i < n; i++) {
543 if (one == twos[i])
545 * We do not mark this even with RESULT so we do not
546 * have to clean it up.
548 return commit_list_insert(one, &result);
551 if (parse_commit(one))
552 return NULL;
553 for (i = 0; i < n; i++) {
554 if (parse_commit(twos[i]))
555 return NULL;
558 one->object.flags |= PARENT1;
559 insert_by_date(one, &list);
560 for (i = 0; i < n; i++) {
561 twos[i]->object.flags |= PARENT2;
562 insert_by_date(twos[i], &list);
565 while (interesting(list)) {
566 struct commit *commit;
567 struct commit_list *parents;
568 struct commit_list *n;
569 int flags;
571 commit = list->item;
572 n = list->next;
573 free(list);
574 list = n;
576 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
577 if (flags == (PARENT1 | PARENT2)) {
578 if (!(commit->object.flags & RESULT)) {
579 commit->object.flags |= RESULT;
580 insert_by_date(commit, &result);
582 /* Mark parents of a found merge stale */
583 flags |= STALE;
585 parents = commit->parents;
586 while (parents) {
587 struct commit *p = parents->item;
588 parents = parents->next;
589 if ((p->object.flags & flags) == flags)
590 continue;
591 if (parse_commit(p))
592 return NULL;
593 p->object.flags |= flags;
594 insert_by_date(p, &list);
598 /* Clean up the result to remove stale ones */
599 free_commit_list(list);
600 list = result; result = NULL;
601 while (list) {
602 struct commit_list *n = list->next;
603 if (!(list->item->object.flags & STALE))
604 insert_by_date(list->item, &result);
605 free(list);
606 list = n;
608 return result;
611 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
613 struct commit_list *i, *j, *k, *ret = NULL;
614 struct commit_list **pptr = &ret;
616 for (i = in; i; i = i->next) {
617 if (!ret)
618 pptr = &commit_list_insert(i->item, pptr)->next;
619 else {
620 struct commit_list *new = NULL, *end = NULL;
622 for (j = ret; j; j = j->next) {
623 struct commit_list *bases;
624 bases = get_merge_bases(i->item, j->item, 1);
625 if (!new)
626 new = bases;
627 else
628 end->next = bases;
629 for (k = bases; k; k = k->next)
630 end = k;
632 ret = new;
635 return ret;
638 struct commit_list *get_merge_bases_many(struct commit *one,
639 int n,
640 struct commit **twos,
641 int cleanup)
643 struct commit_list *list;
644 struct commit **rslt;
645 struct commit_list *result;
646 int cnt, i, j;
648 result = merge_bases_many(one, n, twos);
649 for (i = 0; i < n; i++) {
650 if (one == twos[i])
651 return result;
653 if (!result || !result->next) {
654 if (cleanup) {
655 clear_commit_marks(one, all_flags);
656 for (i = 0; i < n; i++)
657 clear_commit_marks(twos[i], all_flags);
659 return result;
662 /* There are more than one */
663 cnt = 0;
664 list = result;
665 while (list) {
666 list = list->next;
667 cnt++;
669 rslt = xcalloc(cnt, sizeof(*rslt));
670 for (list = result, i = 0; list; list = list->next)
671 rslt[i++] = list->item;
672 free_commit_list(result);
674 clear_commit_marks(one, all_flags);
675 for (i = 0; i < n; i++)
676 clear_commit_marks(twos[i], all_flags);
677 for (i = 0; i < cnt - 1; i++) {
678 for (j = i+1; j < cnt; j++) {
679 if (!rslt[i] || !rslt[j])
680 continue;
681 result = merge_bases_many(rslt[i], 1, &rslt[j]);
682 clear_commit_marks(rslt[i], all_flags);
683 clear_commit_marks(rslt[j], all_flags);
684 for (list = result; list; list = list->next) {
685 if (rslt[i] == list->item)
686 rslt[i] = NULL;
687 if (rslt[j] == list->item)
688 rslt[j] = NULL;
693 /* Surviving ones in rslt[] are the independent results */
694 result = NULL;
695 for (i = 0; i < cnt; i++) {
696 if (rslt[i])
697 insert_by_date(rslt[i], &result);
699 free(rslt);
700 return result;
703 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
704 int cleanup)
706 return get_merge_bases_many(one, 1, &two, cleanup);
709 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
711 if (!with_commit)
712 return 1;
713 while (with_commit) {
714 struct commit *other;
716 other = with_commit->item;
717 with_commit = with_commit->next;
718 if (in_merge_bases(other, &commit, 1))
719 return 1;
721 return 0;
724 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
726 struct commit_list *bases, *b;
727 int ret = 0;
729 if (num == 1)
730 bases = get_merge_bases(commit, *reference, 1);
731 else
732 die("not yet");
733 for (b = bases; b; b = b->next) {
734 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
735 ret = 1;
736 break;
740 free_commit_list(bases);
741 return ret;
744 struct commit_list *reduce_heads(struct commit_list *heads)
746 struct commit_list *p;
747 struct commit_list *result = NULL, **tail = &result;
748 struct commit **other;
749 size_t num_head, num_other;
751 if (!heads)
752 return NULL;
754 /* Avoid unnecessary reallocations */
755 for (p = heads, num_head = 0; p; p = p->next)
756 num_head++;
757 other = xcalloc(sizeof(*other), num_head);
759 /* For each commit, see if it can be reached by others */
760 for (p = heads; p; p = p->next) {
761 struct commit_list *q, *base;
763 /* Do we already have this in the result? */
764 for (q = result; q; q = q->next)
765 if (p->item == q->item)
766 break;
767 if (q)
768 continue;
770 num_other = 0;
771 for (q = heads; q; q = q->next) {
772 if (p->item == q->item)
773 continue;
774 other[num_other++] = q->item;
776 if (num_other)
777 base = get_merge_bases_many(p->item, num_other, other, 1);
778 else
779 base = NULL;
781 * If p->item does not have anything common with other
782 * commits, there won't be any merge base. If it is
783 * reachable from some of the others, p->item will be
784 * the merge base. If its history is connected with
785 * others, but p->item is not reachable by others, we
786 * will get something other than p->item back.
788 if (!base || (base->item != p->item))
789 tail = &(commit_list_insert(p->item, tail)->next);
790 free_commit_list(base);
792 free(other);
793 return result;