administrative functions for rev-cache, start of integration into git
[git/spearce.git] / commit.c
blob61d83c6cc7693d4e883bf354ba104e09d1f6dcdd
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"
9 int save_commit_buffer = 1;
11 const char *commit_type = "commit";
13 static struct commit *check_commit(struct object *obj,
14 const unsigned char *sha1,
15 int quiet)
17 if (obj->type != OBJ_COMMIT) {
18 if (!quiet)
19 error("Object %s is a %s, not a commit",
20 sha1_to_hex(sha1), typename(obj->type));
21 return NULL;
23 return (struct commit *) obj;
26 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
27 int quiet)
29 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
31 if (!obj)
32 return NULL;
33 return check_commit(obj, sha1, quiet);
36 struct commit *lookup_commit_reference(const unsigned char *sha1)
38 return lookup_commit_reference_gently(sha1, 0);
41 struct commit *lookup_commit(const unsigned char *sha1)
43 struct object *obj = lookup_object(sha1);
44 if (!obj)
45 return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
46 if (!obj->type)
47 obj->type = OBJ_COMMIT;
48 return check_commit(obj, sha1, 0);
51 static unsigned long parse_commit_date(const char *buf, const char *tail)
53 const char *dateptr;
55 if (buf + 6 >= tail)
56 return 0;
57 if (memcmp(buf, "author", 6))
58 return 0;
59 while (buf < tail && *buf++ != '\n')
60 /* nada */;
61 if (buf + 9 >= tail)
62 return 0;
63 if (memcmp(buf, "committer", 9))
64 return 0;
65 while (buf < tail && *buf++ != '>')
66 /* nada */;
67 if (buf >= tail)
68 return 0;
69 dateptr = buf;
70 while (buf < tail && *buf++ != '\n')
71 /* nada */;
72 if (buf >= tail)
73 return 0;
74 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
75 return strtoul(dateptr, NULL, 10);
78 static struct commit_graft **commit_graft;
79 static int commit_graft_alloc, commit_graft_nr;
81 static int commit_graft_pos(const unsigned char *sha1)
83 int lo, hi;
84 lo = 0;
85 hi = commit_graft_nr;
86 while (lo < hi) {
87 int mi = (lo + hi) / 2;
88 struct commit_graft *graft = commit_graft[mi];
89 int cmp = hashcmp(sha1, graft->sha1);
90 if (!cmp)
91 return mi;
92 if (cmp < 0)
93 hi = mi;
94 else
95 lo = mi + 1;
97 return -lo - 1;
100 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
102 int pos = commit_graft_pos(graft->sha1);
104 if (0 <= pos) {
105 if (ignore_dups)
106 free(graft);
107 else {
108 free(commit_graft[pos]);
109 commit_graft[pos] = graft;
111 return 1;
113 pos = -pos - 1;
114 if (commit_graft_alloc <= ++commit_graft_nr) {
115 commit_graft_alloc = alloc_nr(commit_graft_alloc);
116 commit_graft = xrealloc(commit_graft,
117 sizeof(*commit_graft) *
118 commit_graft_alloc);
120 if (pos < commit_graft_nr)
121 memmove(commit_graft + pos + 1,
122 commit_graft + pos,
123 (commit_graft_nr - pos - 1) *
124 sizeof(*commit_graft));
125 commit_graft[pos] = graft;
126 return 0;
129 struct commit_graft *read_graft_line(char *buf, int len)
131 /* The format is just "Commit Parent1 Parent2 ...\n" */
132 int i;
133 struct commit_graft *graft = NULL;
135 if (buf[len-1] == '\n')
136 buf[--len] = 0;
137 if (buf[0] == '#' || buf[0] == '\0')
138 return NULL;
139 if ((len + 1) % 41) {
140 bad_graft_data:
141 error("bad graft data: %s", buf);
142 free(graft);
143 return NULL;
145 i = (len + 1) / 41 - 1;
146 graft = xmalloc(sizeof(*graft) + 20 * i);
147 graft->nr_parent = i;
148 if (get_sha1_hex(buf, graft->sha1))
149 goto bad_graft_data;
150 for (i = 40; i < len; i += 41) {
151 if (buf[i] != ' ')
152 goto bad_graft_data;
153 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
154 goto bad_graft_data;
156 return graft;
159 static int read_graft_file(const char *graft_file)
161 FILE *fp = fopen(graft_file, "r");
162 char buf[1024];
163 if (!fp)
164 return -1;
165 while (fgets(buf, sizeof(buf), fp)) {
166 /* The format is just "Commit Parent1 Parent2 ...\n" */
167 int len = strlen(buf);
168 struct commit_graft *graft = read_graft_line(buf, len);
169 if (!graft)
170 continue;
171 if (register_commit_graft(graft, 1))
172 error("duplicate graft data: %s", buf);
174 fclose(fp);
175 return 0;
178 static void prepare_commit_graft(void)
180 static int commit_graft_prepared;
181 char *graft_file;
183 if (commit_graft_prepared)
184 return;
185 graft_file = get_graft_file();
186 read_graft_file(graft_file);
187 /* make sure shallows are read */
188 is_repository_shallow();
189 commit_graft_prepared = 1;
192 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
194 int pos;
195 prepare_commit_graft();
196 pos = commit_graft_pos(sha1);
197 if (pos < 0)
198 return NULL;
199 return commit_graft[pos];
202 int write_shallow_commits(int fd, int use_pack_protocol)
204 int i, count = 0;
205 for (i = 0; i < commit_graft_nr; i++)
206 if (commit_graft[i]->nr_parent < 0) {
207 const char *hex =
208 sha1_to_hex(commit_graft[i]->sha1);
209 count++;
210 if (use_pack_protocol)
211 packet_write(fd, "shallow %s", hex);
212 else {
213 if (write_in_full(fd, hex, 40) != 40)
214 break;
215 if (write_str_in_full(fd, "\n") != 1)
216 break;
219 return count;
222 int unregister_shallow(const unsigned char *sha1)
224 int pos = commit_graft_pos(sha1);
225 if (pos < 0)
226 return -1;
227 if (pos + 1 < commit_graft_nr)
228 memcpy(commit_graft + pos, commit_graft + pos + 1,
229 sizeof(struct commit_graft *)
230 * (commit_graft_nr - pos - 1));
231 commit_graft_nr--;
232 return 0;
235 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
237 char *tail = buffer;
238 char *bufptr = buffer;
239 unsigned char parent[20];
240 struct commit_list **pptr;
241 struct commit_graft *graft;
243 if (item->object.parsed)
244 return 0;
245 item->object.parsed = 1;
246 tail += size;
247 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
248 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
249 if (get_sha1_hex(bufptr + 5, parent) < 0)
250 return error("bad tree pointer in commit %s",
251 sha1_to_hex(item->object.sha1));
252 item->tree = lookup_tree(parent);
253 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
254 pptr = &item->parents;
255 while (pop_commit(pptr))
256 ; /* clear anything from cache */
258 graft = lookup_commit_graft(item->object.sha1);
259 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
260 struct commit *new_parent;
262 if (tail <= bufptr + 48 ||
263 get_sha1_hex(bufptr + 7, parent) ||
264 bufptr[47] != '\n')
265 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
266 bufptr += 48;
268 * The clone is shallow if nr_parent < 0, and we must
269 * not traverse its real parents even when we unhide them.
271 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
272 continue;
273 new_parent = lookup_commit(parent);
274 if (new_parent)
275 pptr = &commit_list_insert(new_parent, pptr)->next;
277 if (graft) {
278 int i;
279 struct commit *new_parent;
280 for (i = 0; i < graft->nr_parent; i++) {
281 new_parent = lookup_commit(graft->parent[i]);
282 if (!new_parent)
283 continue;
284 pptr = &commit_list_insert(new_parent, pptr)->next;
287 item->date = parse_commit_date(bufptr, tail);
289 return 0;
292 int parse_commit(struct commit *item)
294 enum object_type type;
295 void *buffer;
296 unsigned long size;
297 int ret;
299 if (!item)
300 return -1;
301 if (item->object.parsed)
302 return 0;
303 buffer = read_sha1_file(item->object.sha1, &type, &size);
304 if (!buffer)
305 return error("Could not read %s",
306 sha1_to_hex(item->object.sha1));
307 if (type != OBJ_COMMIT) {
308 free(buffer);
309 return error("Object %s not a commit",
310 sha1_to_hex(item->object.sha1));
312 ret = parse_commit_buffer(item, buffer, size);
313 if (save_commit_buffer && !ret) {
314 item->buffer = buffer;
315 return 0;
317 free(buffer);
318 return ret;
321 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
323 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
324 new_list->item = item;
325 new_list->next = *list_p;
326 *list_p = new_list;
327 return new_list;
330 unsigned commit_list_count(const struct commit_list *l)
332 unsigned c = 0;
333 for (; l; l = l->next )
334 c++;
335 return c;
338 void free_commit_list(struct commit_list *list)
340 while (list) {
341 struct commit_list *temp = list;
342 list = temp->next;
343 free(temp);
347 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
349 struct commit_list **pp = list;
350 struct commit_list *p;
351 while ((p = *pp) != NULL) {
352 if (p->item->date < item->date) {
353 break;
355 pp = &p->next;
357 return commit_list_insert(item, pp);
361 void sort_by_date(struct commit_list **list)
363 struct commit_list *ret = NULL;
364 while (*list) {
365 insert_by_date((*list)->item, &ret);
366 *list = (*list)->next;
368 *list = ret;
371 struct commit *pop_most_recent_commit(struct commit_list **list,
372 unsigned int mark)
374 struct commit *ret = (*list)->item;
375 struct commit_list *parents = ret->parents;
376 struct commit_list *old = *list;
378 *list = (*list)->next;
379 free(old);
381 while (parents) {
382 struct commit *commit = parents->item;
383 if (!parse_commit(commit) && !(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 while (commit) {
395 struct commit_list *parents;
397 if (!(mark & commit->object.flags))
398 return;
400 commit->object.flags &= ~mark;
402 parents = commit->parents;
403 if (!parents)
404 return;
406 while ((parents = parents->next))
407 clear_commit_marks(parents->item, mark);
409 commit = commit->parents->item;
413 struct commit *pop_commit(struct commit_list **stack)
415 struct commit_list *top = *stack;
416 struct commit *item = top ? top->item : NULL;
418 if (top) {
419 *stack = top->next;
420 free(top);
422 return item;
426 * Performs an in-place topological sort on the list supplied.
428 void sort_in_topological_order(struct commit_list ** list, int lifo)
430 struct commit_list *next, *orig = *list;
431 struct commit_list *work, **insert;
432 struct commit_list **pptr;
434 if (!orig)
435 return;
436 *list = NULL;
438 /* Mark them and clear the indegree */
439 for (next = orig; next; next = next->next) {
440 struct commit *commit = next->item;
441 commit->indegree = 1;
444 /* update the indegree */
445 for (next = orig; next; next = next->next) {
446 struct commit_list * parents = next->item->parents;
447 while (parents) {
448 struct commit *parent = parents->item;
450 if (parent->indegree)
451 parent->indegree++;
452 parents = parents->next;
457 * find the tips
459 * tips are nodes not reachable from any other node in the list
461 * the tips serve as a starting set for the work queue.
463 work = NULL;
464 insert = &work;
465 for (next = orig; next; next = next->next) {
466 struct commit *commit = next->item;
468 if (commit->indegree == 1)
469 insert = &commit_list_insert(commit, insert)->next;
472 /* process the list in topological order */
473 if (!lifo)
474 sort_by_date(&work);
476 pptr = list;
477 *list = NULL;
478 while (work) {
479 struct commit *commit;
480 struct commit_list *parents, *work_item;
482 work_item = work;
483 work = work_item->next;
484 work_item->next = NULL;
486 commit = work_item->item;
487 for (parents = commit->parents; parents ; parents = parents->next) {
488 struct commit *parent=parents->item;
490 if (!parent->indegree)
491 continue;
494 * parents are only enqueued for emission
495 * when all their children have been emitted thereby
496 * guaranteeing topological order.
498 if (--parent->indegree == 1) {
499 if (!lifo)
500 insert_by_date(parent, &work);
501 else
502 commit_list_insert(parent, &work);
506 * work_item is a commit all of whose children
507 * have already been emitted. we can emit it now.
509 commit->indegree = 0;
510 *pptr = work_item;
511 pptr = &work_item->next;
515 /* merge-base stuff */
517 /* bits #0..15 in revision.h */
518 #define PARENT1 (1u<<16)
519 #define PARENT2 (1u<<17)
520 #define STALE (1u<<18)
521 #define RESULT (1u<<19)
523 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
525 static struct commit *interesting(struct commit_list *list)
527 while (list) {
528 struct commit *commit = list->item;
529 list = list->next;
530 if (commit->object.flags & STALE)
531 continue;
532 return commit;
534 return NULL;
537 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
539 struct commit_list *list = NULL;
540 struct commit_list *result = NULL;
541 int i;
543 for (i = 0; i < n; i++) {
544 if (one == twos[i])
546 * We do not mark this even with RESULT so we do not
547 * have to clean it up.
549 return commit_list_insert(one, &result);
552 if (parse_commit(one))
553 return NULL;
554 for (i = 0; i < n; i++) {
555 if (parse_commit(twos[i]))
556 return NULL;
559 one->object.flags |= PARENT1;
560 insert_by_date(one, &list);
561 for (i = 0; i < n; i++) {
562 twos[i]->object.flags |= PARENT2;
563 insert_by_date(twos[i], &list);
566 while (interesting(list)) {
567 struct commit *commit;
568 struct commit_list *parents;
569 struct commit_list *next;
570 int flags;
572 commit = list->item;
573 next = list->next;
574 free(list);
575 list = next;
577 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
578 if (flags == (PARENT1 | PARENT2)) {
579 if (!(commit->object.flags & RESULT)) {
580 commit->object.flags |= RESULT;
581 insert_by_date(commit, &result);
583 /* Mark parents of a found merge stale */
584 flags |= STALE;
586 parents = commit->parents;
587 while (parents) {
588 struct commit *p = parents->item;
589 parents = parents->next;
590 if ((p->object.flags & flags) == flags)
591 continue;
592 if (parse_commit(p))
593 return NULL;
594 p->object.flags |= flags;
595 insert_by_date(p, &list);
599 /* Clean up the result to remove stale ones */
600 free_commit_list(list);
601 list = result; result = NULL;
602 while (list) {
603 struct commit_list *next = list->next;
604 if (!(list->item->object.flags & STALE))
605 insert_by_date(list->item, &result);
606 free(list);
607 list = next;
609 return result;
612 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
614 struct commit_list *i, *j, *k, *ret = NULL;
615 struct commit_list **pptr = &ret;
617 for (i = in; i; i = i->next) {
618 if (!ret)
619 pptr = &commit_list_insert(i->item, pptr)->next;
620 else {
621 struct commit_list *new = NULL, *end = NULL;
623 for (j = ret; j; j = j->next) {
624 struct commit_list *bases;
625 bases = get_merge_bases(i->item, j->item, 1);
626 if (!new)
627 new = bases;
628 else
629 end->next = bases;
630 for (k = bases; k; k = k->next)
631 end = k;
633 ret = new;
636 return ret;
639 struct commit_list *get_merge_bases_many(struct commit *one,
640 int n,
641 struct commit **twos,
642 int cleanup)
644 struct commit_list *list;
645 struct commit **rslt;
646 struct commit_list *result;
647 int cnt, i, j;
649 result = merge_bases_many(one, n, twos);
650 for (i = 0; i < n; i++) {
651 if (one == twos[i])
652 return result;
654 if (!result || !result->next) {
655 if (cleanup) {
656 clear_commit_marks(one, all_flags);
657 for (i = 0; i < n; i++)
658 clear_commit_marks(twos[i], all_flags);
660 return result;
663 /* There are more than one */
664 cnt = 0;
665 list = result;
666 while (list) {
667 list = list->next;
668 cnt++;
670 rslt = xcalloc(cnt, sizeof(*rslt));
671 for (list = result, i = 0; list; list = list->next)
672 rslt[i++] = list->item;
673 free_commit_list(result);
675 clear_commit_marks(one, all_flags);
676 for (i = 0; i < n; i++)
677 clear_commit_marks(twos[i], all_flags);
678 for (i = 0; i < cnt - 1; i++) {
679 for (j = i+1; j < cnt; j++) {
680 if (!rslt[i] || !rslt[j])
681 continue;
682 result = merge_bases_many(rslt[i], 1, &rslt[j]);
683 clear_commit_marks(rslt[i], all_flags);
684 clear_commit_marks(rslt[j], all_flags);
685 for (list = result; list; list = list->next) {
686 if (rslt[i] == list->item)
687 rslt[i] = NULL;
688 if (rslt[j] == list->item)
689 rslt[j] = NULL;
694 /* Surviving ones in rslt[] are the independent results */
695 result = NULL;
696 for (i = 0; i < cnt; i++) {
697 if (rslt[i])
698 insert_by_date(rslt[i], &result);
700 free(rslt);
701 return result;
704 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
705 int cleanup)
707 return get_merge_bases_many(one, 1, &two, cleanup);
710 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
712 if (!with_commit)
713 return 1;
714 while (with_commit) {
715 struct commit *other;
717 other = with_commit->item;
718 with_commit = with_commit->next;
719 if (in_merge_bases(other, &commit, 1))
720 return 1;
722 return 0;
725 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
727 struct commit_list *bases, *b;
728 int ret = 0;
730 if (num == 1)
731 bases = get_merge_bases(commit, *reference, 1);
732 else
733 die("not yet");
734 for (b = bases; b; b = b->next) {
735 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
736 ret = 1;
737 break;
741 free_commit_list(bases);
742 return ret;
745 struct commit_list *reduce_heads(struct commit_list *heads)
747 struct commit_list *p;
748 struct commit_list *result = NULL, **tail = &result;
749 struct commit **other;
750 size_t num_head, num_other;
752 if (!heads)
753 return NULL;
755 /* Avoid unnecessary reallocations */
756 for (p = heads, num_head = 0; p; p = p->next)
757 num_head++;
758 other = xcalloc(sizeof(*other), num_head);
760 /* For each commit, see if it can be reached by others */
761 for (p = heads; p; p = p->next) {
762 struct commit_list *q, *base;
764 /* Do we already have this in the result? */
765 for (q = result; q; q = q->next)
766 if (p->item == q->item)
767 break;
768 if (q)
769 continue;
771 num_other = 0;
772 for (q = heads; q; q = q->next) {
773 if (p->item == q->item)
774 continue;
775 other[num_other++] = q->item;
777 if (num_other)
778 base = get_merge_bases_many(p->item, num_other, other, 1);
779 else
780 base = NULL;
782 * If p->item does not have anything common with other
783 * commits, there won't be any merge base. If it is
784 * reachable from some of the others, p->item will be
785 * the merge base. If its history is connected with
786 * others, but p->item is not reachable by others, we
787 * will get something other than p->item back.
789 if (!base || (base->item != p->item))
790 tail = &(commit_list_insert(p->item, tail)->next);
791 free_commit_list(base);
793 free(other);
794 return result;