"needs update" considered harmful
[git/jnareb-git/bp-gitweb.git] / commit.c
blob5148ec552708bb6c98cc64a84647ec7715ad1c29
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 unsigned long date;
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 date = strtoul(dateptr, NULL, 10);
77 if (date == ULONG_MAX)
78 date = 0;
79 return date;
82 static struct commit_graft **commit_graft;
83 static int commit_graft_alloc, commit_graft_nr;
85 static int commit_graft_pos(const unsigned char *sha1)
87 int lo, hi;
88 lo = 0;
89 hi = commit_graft_nr;
90 while (lo < hi) {
91 int mi = (lo + hi) / 2;
92 struct commit_graft *graft = commit_graft[mi];
93 int cmp = hashcmp(sha1, graft->sha1);
94 if (!cmp)
95 return mi;
96 if (cmp < 0)
97 hi = mi;
98 else
99 lo = mi + 1;
101 return -lo - 1;
104 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
106 int pos = commit_graft_pos(graft->sha1);
108 if (0 <= pos) {
109 if (ignore_dups)
110 free(graft);
111 else {
112 free(commit_graft[pos]);
113 commit_graft[pos] = graft;
115 return 1;
117 pos = -pos - 1;
118 if (commit_graft_alloc <= ++commit_graft_nr) {
119 commit_graft_alloc = alloc_nr(commit_graft_alloc);
120 commit_graft = xrealloc(commit_graft,
121 sizeof(*commit_graft) *
122 commit_graft_alloc);
124 if (pos < commit_graft_nr)
125 memmove(commit_graft + pos + 1,
126 commit_graft + pos,
127 (commit_graft_nr - pos - 1) *
128 sizeof(*commit_graft));
129 commit_graft[pos] = graft;
130 return 0;
133 struct commit_graft *read_graft_line(char *buf, int len)
135 /* The format is just "Commit Parent1 Parent2 ...\n" */
136 int i;
137 struct commit_graft *graft = NULL;
139 if (buf[len-1] == '\n')
140 buf[--len] = 0;
141 if (buf[0] == '#' || buf[0] == '\0')
142 return NULL;
143 if ((len + 1) % 41) {
144 bad_graft_data:
145 error("bad graft data: %s", buf);
146 free(graft);
147 return NULL;
149 i = (len + 1) / 41 - 1;
150 graft = xmalloc(sizeof(*graft) + 20 * i);
151 graft->nr_parent = i;
152 if (get_sha1_hex(buf, graft->sha1))
153 goto bad_graft_data;
154 for (i = 40; i < len; i += 41) {
155 if (buf[i] != ' ')
156 goto bad_graft_data;
157 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
158 goto bad_graft_data;
160 return graft;
163 int read_graft_file(const char *graft_file)
165 FILE *fp = fopen(graft_file, "r");
166 char buf[1024];
167 if (!fp)
168 return -1;
169 while (fgets(buf, sizeof(buf), fp)) {
170 /* The format is just "Commit Parent1 Parent2 ...\n" */
171 int len = strlen(buf);
172 struct commit_graft *graft = read_graft_line(buf, len);
173 if (!graft)
174 continue;
175 if (register_commit_graft(graft, 1))
176 error("duplicate graft data: %s", buf);
178 fclose(fp);
179 return 0;
182 static void prepare_commit_graft(void)
184 static int commit_graft_prepared;
185 char *graft_file;
187 if (commit_graft_prepared)
188 return;
189 graft_file = get_graft_file();
190 read_graft_file(graft_file);
191 /* make sure shallows are read */
192 is_repository_shallow();
193 commit_graft_prepared = 1;
196 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
198 int pos;
199 prepare_commit_graft();
200 pos = commit_graft_pos(sha1);
201 if (pos < 0)
202 return NULL;
203 return commit_graft[pos];
206 int write_shallow_commits(int fd, int use_pack_protocol)
208 int i, count = 0;
209 for (i = 0; i < commit_graft_nr; i++)
210 if (commit_graft[i]->nr_parent < 0) {
211 const char *hex =
212 sha1_to_hex(commit_graft[i]->sha1);
213 count++;
214 if (use_pack_protocol)
215 packet_write(fd, "shallow %s", hex);
216 else {
217 if (write_in_full(fd, hex, 40) != 40)
218 break;
219 if (write_in_full(fd, "\n", 1) != 1)
220 break;
223 return count;
226 int unregister_shallow(const unsigned char *sha1)
228 int pos = commit_graft_pos(sha1);
229 if (pos < 0)
230 return -1;
231 if (pos + 1 < commit_graft_nr)
232 memcpy(commit_graft + pos, commit_graft + pos + 1,
233 sizeof(struct commit_graft *)
234 * (commit_graft_nr - pos - 1));
235 commit_graft_nr--;
236 return 0;
239 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
241 char *tail = buffer;
242 char *bufptr = buffer;
243 unsigned char parent[20];
244 struct commit_list **pptr;
245 struct commit_graft *graft;
247 if (item->object.parsed)
248 return 0;
249 item->object.parsed = 1;
250 tail += size;
251 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
252 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
253 if (get_sha1_hex(bufptr + 5, parent) < 0)
254 return error("bad tree pointer in commit %s",
255 sha1_to_hex(item->object.sha1));
256 item->tree = lookup_tree(parent);
257 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
258 pptr = &item->parents;
260 graft = lookup_commit_graft(item->object.sha1);
261 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
262 struct commit *new_parent;
264 if (tail <= bufptr + 48 ||
265 get_sha1_hex(bufptr + 7, parent) ||
266 bufptr[47] != '\n')
267 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
268 bufptr += 48;
269 if (graft)
270 continue;
271 new_parent = lookup_commit(parent);
272 if (new_parent)
273 pptr = &commit_list_insert(new_parent, pptr)->next;
275 if (graft) {
276 int i;
277 struct commit *new_parent;
278 for (i = 0; i < graft->nr_parent; i++) {
279 new_parent = lookup_commit(graft->parent[i]);
280 if (!new_parent)
281 continue;
282 pptr = &commit_list_insert(new_parent, pptr)->next;
285 item->date = parse_commit_date(bufptr, tail);
287 return 0;
290 int parse_commit(struct commit *item)
292 enum object_type type;
293 void *buffer;
294 unsigned long size;
295 int ret;
297 if (!item)
298 return -1;
299 if (item->object.parsed)
300 return 0;
301 buffer = read_sha1_file(item->object.sha1, &type, &size);
302 if (!buffer)
303 return error("Could not read %s",
304 sha1_to_hex(item->object.sha1));
305 if (type != OBJ_COMMIT) {
306 free(buffer);
307 return error("Object %s not a commit",
308 sha1_to_hex(item->object.sha1));
310 ret = parse_commit_buffer(item, buffer, size);
311 if (save_commit_buffer && !ret) {
312 item->buffer = buffer;
313 return 0;
315 free(buffer);
316 return ret;
319 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
321 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
322 new_list->item = item;
323 new_list->next = *list_p;
324 *list_p = new_list;
325 return new_list;
328 unsigned commit_list_count(const struct commit_list *l)
330 unsigned c = 0;
331 for (; l; l = l->next )
332 c++;
333 return c;
336 void free_commit_list(struct commit_list *list)
338 while (list) {
339 struct commit_list *temp = list;
340 list = temp->next;
341 free(temp);
345 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
347 struct commit_list **pp = list;
348 struct commit_list *p;
349 while ((p = *pp) != NULL) {
350 if (p->item->date < item->date) {
351 break;
353 pp = &p->next;
355 return commit_list_insert(item, pp);
359 void sort_by_date(struct commit_list **list)
361 struct commit_list *ret = NULL;
362 while (*list) {
363 insert_by_date((*list)->item, &ret);
364 *list = (*list)->next;
366 *list = ret;
369 struct commit *pop_most_recent_commit(struct commit_list **list,
370 unsigned int mark)
372 struct commit *ret = (*list)->item;
373 struct commit_list *parents = ret->parents;
374 struct commit_list *old = *list;
376 *list = (*list)->next;
377 free(old);
379 while (parents) {
380 struct commit *commit = parents->item;
381 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
382 commit->object.flags |= mark;
383 insert_by_date(commit, list);
385 parents = parents->next;
387 return ret;
390 void clear_commit_marks(struct commit *commit, unsigned int mark)
392 while (commit) {
393 struct commit_list *parents;
395 if (!(mark & commit->object.flags))
396 return;
398 commit->object.flags &= ~mark;
400 parents = commit->parents;
401 if (!parents)
402 return;
404 while ((parents = parents->next))
405 clear_commit_marks(parents->item, mark);
407 commit = commit->parents->item;
411 struct commit *pop_commit(struct commit_list **stack)
413 struct commit_list *top = *stack;
414 struct commit *item = top ? top->item : NULL;
416 if (top) {
417 *stack = top->next;
418 free(top);
420 return item;
424 * Performs an in-place topological sort on the list supplied.
426 void sort_in_topological_order(struct commit_list ** list, int lifo)
428 struct commit_list *next, *orig = *list;
429 struct commit_list *work, **insert;
430 struct commit_list **pptr;
432 if (!orig)
433 return;
434 *list = NULL;
436 /* Mark them and clear the indegree */
437 for (next = orig; next; next = next->next) {
438 struct commit *commit = next->item;
439 commit->object.flags |= TOPOSORT;
440 commit->indegree = 0;
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->object.flags & TOPOSORT)
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)
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->object.flags & TOPOSORT))
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) {
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->object.flags &= ~TOPOSORT;
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 in_merge_bases(struct commit *commit, struct commit **reference, int num)
711 struct commit_list *bases, *b;
712 int ret = 0;
714 if (num == 1)
715 bases = get_merge_bases(commit, *reference, 1);
716 else
717 die("not yet");
718 for (b = bases; b; b = b->next) {
719 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
720 ret = 1;
721 break;
725 free_commit_list(bases);
726 return ret;
729 struct commit_list *reduce_heads(struct commit_list *heads)
731 struct commit_list *p;
732 struct commit_list *result = NULL, **tail = &result;
733 struct commit **other;
734 size_t num_head, num_other;
736 if (!heads)
737 return NULL;
739 /* Avoid unnecessary reallocations */
740 for (p = heads, num_head = 0; p; p = p->next)
741 num_head++;
742 other = xcalloc(sizeof(*other), num_head);
744 /* For each commit, see if it can be reached by others */
745 for (p = heads; p; p = p->next) {
746 struct commit_list *q, *base;
748 /* Do we already have this in the result? */
749 for (q = result; q; q = q->next)
750 if (p->item == q->item)
751 break;
752 if (q)
753 continue;
755 num_other = 0;
756 for (q = heads; q; q = q->next) {
757 if (p->item == q->item)
758 continue;
759 other[num_other++] = q->item;
761 if (num_other)
762 base = get_merge_bases_many(p->item, num_other, other, 1);
763 else
764 base = NULL;
766 * If p->item does not have anything common with other
767 * commits, there won't be any merge base. If it is
768 * reachable from some of the others, p->item will be
769 * the merge base. If its history is connected with
770 * others, but p->item is not reachable by others, we
771 * will get something other than p->item back.
773 if (!base || (base->item != p->item))
774 tail = &(commit_list_insert(p->item, tail)->next);
775 free_commit_list(base);
777 free(other);
778 return result;