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 core_clock_skew = 86400;
11 int save_commit_buffer = 1;
13 const char *commit_type = "commit";
15 static struct commit *check_commit(struct object *obj,
16 const unsigned char *sha1,
17 int quiet)
18 {
19 if (obj->type != OBJ_COMMIT) {
20 if (!quiet)
21 error("Object %s is a %s, not a commit",
22 sha1_to_hex(sha1), typename(obj->type));
23 return NULL;
24 }
25 return (struct commit *) obj;
26 }
28 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
29 int quiet)
30 {
31 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
33 if (!obj)
34 return NULL;
35 return check_commit(obj, sha1, quiet);
36 }
38 struct commit *lookup_commit_reference(const unsigned char *sha1)
39 {
40 return lookup_commit_reference_gently(sha1, 0);
41 }
43 struct commit *lookup_commit(const unsigned char *sha1)
44 {
45 struct object *obj = lookup_object(sha1);
46 if (!obj)
47 return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
48 if (!obj->type)
49 obj->type = OBJ_COMMIT;
50 return check_commit(obj, sha1, 0);
51 }
53 struct commit *lookup_commit_reference_by_name(const char *name)
54 {
55 unsigned char sha1[20];
56 struct commit *commit;
58 if (get_sha1(name, sha1))
59 return NULL;
60 commit = lookup_commit_reference(sha1);
61 if (!commit || parse_commit(commit))
62 return NULL;
63 return commit;
64 }
66 static unsigned long parse_commit_date(const char *buf, const char *tail)
67 {
68 const char *dateptr;
70 if (buf + 6 >= tail)
71 return 0;
72 if (memcmp(buf, "author", 6))
73 return 0;
74 while (buf < tail && *buf++ != '\n')
75 /* nada */;
76 if (buf + 9 >= tail)
77 return 0;
78 if (memcmp(buf, "committer", 9))
79 return 0;
80 while (buf < tail && *buf++ != '>')
81 /* nada */;
82 if (buf >= tail)
83 return 0;
84 dateptr = buf;
85 while (buf < tail && *buf++ != '\n')
86 /* nada */;
87 if (buf >= tail)
88 return 0;
89 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
90 return strtoul(dateptr, NULL, 10);
91 }
93 static struct commit_graft **commit_graft;
94 static int commit_graft_alloc, commit_graft_nr;
96 static int commit_graft_pos(const unsigned char *sha1)
97 {
98 int lo, hi;
99 lo = 0;
100 hi = commit_graft_nr;
101 while (lo < hi) {
102 int mi = (lo + hi) / 2;
103 struct commit_graft *graft = commit_graft[mi];
104 int cmp = hashcmp(sha1, graft->sha1);
105 if (!cmp)
106 return mi;
107 if (cmp < 0)
108 hi = mi;
109 else
110 lo = mi + 1;
111 }
112 return -lo - 1;
113 }
115 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
116 {
117 int pos = commit_graft_pos(graft->sha1);
119 if (0 <= pos) {
120 if (ignore_dups)
121 free(graft);
122 else {
123 free(commit_graft[pos]);
124 commit_graft[pos] = graft;
125 }
126 return 1;
127 }
128 pos = -pos - 1;
129 if (commit_graft_alloc <= ++commit_graft_nr) {
130 commit_graft_alloc = alloc_nr(commit_graft_alloc);
131 commit_graft = xrealloc(commit_graft,
132 sizeof(*commit_graft) *
133 commit_graft_alloc);
134 }
135 if (pos < commit_graft_nr)
136 memmove(commit_graft + pos + 1,
137 commit_graft + pos,
138 (commit_graft_nr - pos - 1) *
139 sizeof(*commit_graft));
140 commit_graft[pos] = graft;
141 return 0;
142 }
144 struct commit_graft *read_graft_line(char *buf, int len)
145 {
146 /* The format is just "Commit Parent1 Parent2 ...\n" */
147 int i;
148 struct commit_graft *graft = NULL;
150 while (len && isspace(buf[len-1]))
151 buf[--len] = '\0';
152 if (buf[0] == '#' || buf[0] == '\0')
153 return NULL;
154 if ((len + 1) % 41)
155 goto bad_graft_data;
156 i = (len + 1) / 41 - 1;
157 graft = xmalloc(sizeof(*graft) + 20 * i);
158 graft->nr_parent = i;
159 if (get_sha1_hex(buf, graft->sha1))
160 goto bad_graft_data;
161 for (i = 40; i < len; i += 41) {
162 if (buf[i] != ' ')
163 goto bad_graft_data;
164 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
165 goto bad_graft_data;
166 }
167 return graft;
169 bad_graft_data:
170 error("bad graft data: %s", buf);
171 free(graft);
172 return NULL;
173 }
175 static int read_graft_file(const char *graft_file)
176 {
177 FILE *fp = fopen(graft_file, "r");
178 char buf[1024];
179 if (!fp)
180 return -1;
181 while (fgets(buf, sizeof(buf), fp)) {
182 /* The format is just "Commit Parent1 Parent2 ...\n" */
183 int len = strlen(buf);
184 struct commit_graft *graft = read_graft_line(buf, len);
185 if (!graft)
186 continue;
187 if (register_commit_graft(graft, 1))
188 error("duplicate graft data: %s", buf);
189 }
190 fclose(fp);
191 return 0;
192 }
194 static void prepare_commit_graft(void)
195 {
196 static int commit_graft_prepared;
197 char *graft_file;
199 if (commit_graft_prepared)
200 return;
201 graft_file = get_graft_file();
202 read_graft_file(graft_file);
203 /* make sure shallows are read */
204 is_repository_shallow();
205 commit_graft_prepared = 1;
206 }
208 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
209 {
210 int pos;
211 prepare_commit_graft();
212 pos = commit_graft_pos(sha1);
213 if (pos < 0)
214 return NULL;
215 return commit_graft[pos];
216 }
218 int write_shallow_commits(struct strbuf *out, int use_pack_protocol)
219 {
220 int i, count = 0;
221 for (i = 0; i < commit_graft_nr; i++)
222 if (commit_graft[i]->nr_parent < 0) {
223 const char *hex =
224 sha1_to_hex(commit_graft[i]->sha1);
225 count++;
226 if (use_pack_protocol)
227 packet_buf_write(out, "shallow %s", hex);
228 else {
229 strbuf_addstr(out, hex);
230 strbuf_addch(out, '\n');
231 }
232 }
233 return count;
234 }
236 int unregister_shallow(const unsigned char *sha1)
237 {
238 int pos = commit_graft_pos(sha1);
239 if (pos < 0)
240 return -1;
241 if (pos + 1 < commit_graft_nr)
242 memmove(commit_graft + pos, commit_graft + pos + 1,
243 sizeof(struct commit_graft *)
244 * (commit_graft_nr - pos - 1));
245 commit_graft_nr--;
246 return 0;
247 }
249 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
250 {
251 char *tail = buffer;
252 char *bufptr = buffer;
253 unsigned char parent[20];
254 struct commit_list **pptr;
255 struct commit_graft *graft;
257 if (item->object.parsed)
258 return 0;
259 item->object.parsed = 1;
260 tail += size;
261 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
262 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
263 if (get_sha1_hex(bufptr + 5, parent) < 0)
264 return error("bad tree pointer in commit %s",
265 sha1_to_hex(item->object.sha1));
266 item->tree = lookup_tree(parent);
267 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
268 pptr = &item->parents;
270 graft = lookup_commit_graft(item->object.sha1);
271 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
272 struct commit *new_parent;
274 if (tail <= bufptr + 48 ||
275 get_sha1_hex(bufptr + 7, parent) ||
276 bufptr[47] != '\n')
277 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
278 bufptr += 48;
279 /*
280 * The clone is shallow if nr_parent < 0, and we must
281 * not traverse its real parents even when we unhide them.
282 */
283 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
284 continue;
285 new_parent = lookup_commit(parent);
286 if (new_parent)
287 pptr = &commit_list_insert(new_parent, pptr)->next;
288 }
289 if (graft) {
290 int i;
291 struct commit *new_parent;
292 for (i = 0; i < graft->nr_parent; i++) {
293 new_parent = lookup_commit(graft->parent[i]);
294 if (!new_parent)
295 continue;
296 pptr = &commit_list_insert(new_parent, pptr)->next;
297 }
298 }
299 item->date = parse_commit_date(bufptr, tail);
301 return 0;
302 }
304 int parse_commit(struct commit *item)
305 {
306 enum object_type type;
307 void *buffer;
308 unsigned long size;
309 int ret;
311 if (!item)
312 return -1;
313 if (item->object.parsed)
314 return 0;
315 buffer = read_sha1_file(item->object.sha1, &type, &size);
316 if (!buffer)
317 return error("Could not read %s",
318 sha1_to_hex(item->object.sha1));
319 if (type != OBJ_COMMIT) {
320 free(buffer);
321 return error("Object %s not a commit",
322 sha1_to_hex(item->object.sha1));
323 }
324 ret = parse_commit_buffer(item, buffer, size);
325 if (save_commit_buffer && !ret) {
326 item->buffer = buffer;
327 return 0;
328 }
329 free(buffer);
330 return ret;
331 }
333 int find_commit_subject(const char *commit_buffer, const char **subject)
334 {
335 const char *eol;
336 const char *p = commit_buffer;
338 while (*p && (*p != '\n' || p[1] != '\n'))
339 p++;
340 if (*p) {
341 p += 2;
342 for (eol = p; *eol && *eol != '\n'; eol++)
343 ; /* do nothing */
344 } else
345 eol = p;
347 *subject = p;
349 return eol - p;
350 }
352 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
353 {
354 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
355 new_list->item = item;
356 new_list->next = *list_p;
357 *list_p = new_list;
358 return new_list;
359 }
361 unsigned commit_list_count(const struct commit_list *l)
362 {
363 unsigned c = 0;
364 for (; l; l = l->next )
365 c++;
366 return c;
367 }
369 void free_commit_list(struct commit_list *list)
370 {
371 while (list) {
372 struct commit_list *temp = list;
373 list = temp->next;
374 free(temp);
375 }
376 }
378 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
379 {
380 struct commit_list **pp = list;
381 struct commit_list *p;
382 while ((p = *pp) != NULL) {
383 if (p->item->date < item->date) {
384 break;
385 }
386 pp = &p->next;
387 }
388 return commit_list_insert(item, pp);
389 }
392 void commit_list_sort_by_date(struct commit_list **list)
393 {
394 struct commit_list *ret = NULL;
395 while (*list) {
396 commit_list_insert_by_date((*list)->item, &ret);
397 *list = (*list)->next;
398 }
399 *list = ret;
400 }
402 struct commit *pop_most_recent_commit(struct commit_list **list,
403 unsigned int mark)
404 {
405 struct commit *ret = (*list)->item;
406 struct commit_list *parents = ret->parents;
407 struct commit_list *old = *list;
409 *list = (*list)->next;
410 free(old);
412 while (parents) {
413 struct commit *commit = parents->item;
414 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
415 commit->object.flags |= mark;
416 commit_list_insert_by_date(commit, list);
417 }
418 parents = parents->next;
419 }
420 return ret;
421 }
423 void clear_commit_marks(struct commit *commit, unsigned int mark)
424 {
425 while (commit) {
426 struct commit_list *parents;
428 if (!(mark & commit->object.flags))
429 return;
431 commit->object.flags &= ~mark;
433 parents = commit->parents;
434 if (!parents)
435 return;
437 while ((parents = parents->next))
438 clear_commit_marks(parents->item, mark);
440 commit = commit->parents->item;
441 }
442 }
444 struct commit *pop_commit(struct commit_list **stack)
445 {
446 struct commit_list *top = *stack;
447 struct commit *item = top ? top->item : NULL;
449 if (top) {
450 *stack = top->next;
451 free(top);
452 }
453 return item;
454 }
456 /*
457 * Performs an in-place topological sort on the list supplied.
458 */
459 void sort_in_topological_order(struct commit_list ** list, int lifo)
460 {
461 struct commit_list *next, *orig = *list;
462 struct commit_list *work, **insert;
463 struct commit_list **pptr;
465 if (!orig)
466 return;
467 *list = NULL;
469 /* Mark them and clear the indegree */
470 for (next = orig; next; next = next->next) {
471 struct commit *commit = next->item;
472 commit->indegree = 1;
473 }
475 /* update the indegree */
476 for (next = orig; next; next = next->next) {
477 struct commit_list * parents = next->item->parents;
478 while (parents) {
479 struct commit *parent = parents->item;
481 if (parent->indegree)
482 parent->indegree++;
483 parents = parents->next;
484 }
485 }
487 /*
488 * find the tips
489 *
490 * tips are nodes not reachable from any other node in the list
491 *
492 * the tips serve as a starting set for the work queue.
493 */
494 work = NULL;
495 insert = &work;
496 for (next = orig; next; next = next->next) {
497 struct commit *commit = next->item;
499 if (commit->indegree == 1)
500 insert = &commit_list_insert(commit, insert)->next;
501 }
503 /* process the list in topological order */
504 if (!lifo)
505 commit_list_sort_by_date(&work);
507 pptr = list;
508 *list = NULL;
509 while (work) {
510 struct commit *commit;
511 struct commit_list *parents, *work_item;
513 work_item = work;
514 work = work_item->next;
515 work_item->next = NULL;
517 commit = work_item->item;
518 for (parents = commit->parents; parents ; parents = parents->next) {
519 struct commit *parent=parents->item;
521 if (!parent->indegree)
522 continue;
524 /*
525 * parents are only enqueued for emission
526 * when all their children have been emitted thereby
527 * guaranteeing topological order.
528 */
529 if (--parent->indegree == 1) {
530 if (!lifo)
531 commit_list_insert_by_date(parent, &work);
532 else
533 commit_list_insert(parent, &work);
534 }
535 }
536 /*
537 * work_item is a commit all of whose children
538 * have already been emitted. we can emit it now.
539 */
540 commit->indegree = 0;
541 *pptr = work_item;
542 pptr = &work_item->next;
543 }
544 }
546 /* merge-base stuff */
548 /* bits #0..15 in revision.h */
549 #define PARENT1 (1u<<16)
550 #define PARENT2 (1u<<17)
551 #define STALE (1u<<18)
552 #define RESULT (1u<<19)
554 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
556 static struct commit *interesting(struct commit_list *list)
557 {
558 while (list) {
559 struct commit *commit = list->item;
560 list = list->next;
561 if (commit->object.flags & STALE)
562 continue;
563 return commit;
564 }
565 return NULL;
566 }
568 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
569 {
570 struct commit_list *list = NULL;
571 struct commit_list *result = NULL;
572 int i;
574 for (i = 0; i < n; i++) {
575 if (one == twos[i])
576 /*
577 * We do not mark this even with RESULT so we do not
578 * have to clean it up.
579 */
580 return commit_list_insert(one, &result);
581 }
583 if (parse_commit(one))
584 return NULL;
585 for (i = 0; i < n; i++) {
586 if (parse_commit(twos[i]))
587 return NULL;
588 }
590 one->object.flags |= PARENT1;
591 commit_list_insert_by_date(one, &list);
592 for (i = 0; i < n; i++) {
593 twos[i]->object.flags |= PARENT2;
594 commit_list_insert_by_date(twos[i], &list);
595 }
597 while (interesting(list)) {
598 struct commit *commit;
599 struct commit_list *parents;
600 struct commit_list *next;
601 int flags;
603 commit = list->item;
604 next = list->next;
605 free(list);
606 list = next;
608 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
609 if (flags == (PARENT1 | PARENT2)) {
610 if (!(commit->object.flags & RESULT)) {
611 commit->object.flags |= RESULT;
612 commit_list_insert_by_date(commit, &result);
613 }
614 /* Mark parents of a found merge stale */
615 flags |= STALE;
616 }
617 parents = commit->parents;
618 while (parents) {
619 struct commit *p = parents->item;
620 parents = parents->next;
621 if ((p->object.flags & flags) == flags)
622 continue;
623 if (parse_commit(p))
624 return NULL;
625 p->object.flags |= flags;
626 commit_list_insert_by_date(p, &list);
627 }
628 }
630 /* Clean up the result to remove stale ones */
631 free_commit_list(list);
632 list = result; result = NULL;
633 while (list) {
634 struct commit_list *next = list->next;
635 if (!(list->item->object.flags & STALE))
636 commit_list_insert_by_date(list->item, &result);
637 free(list);
638 list = next;
639 }
640 return result;
641 }
643 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
644 {
645 struct commit_list *i, *j, *k, *ret = NULL;
646 struct commit_list **pptr = &ret;
648 for (i = in; i; i = i->next) {
649 if (!ret)
650 pptr = &commit_list_insert(i->item, pptr)->next;
651 else {
652 struct commit_list *new = NULL, *end = NULL;
654 for (j = ret; j; j = j->next) {
655 struct commit_list *bases;
656 bases = get_merge_bases(i->item, j->item, 1);
657 if (!new)
658 new = bases;
659 else
660 end->next = bases;
661 for (k = bases; k; k = k->next)
662 end = k;
663 }
664 ret = new;
665 }
666 }
667 return ret;
668 }
670 struct commit_list *get_merge_bases_many(struct commit *one,
671 int n,
672 struct commit **twos,
673 int cleanup)
674 {
675 struct commit_list *list;
676 struct commit **rslt;
677 struct commit_list *result;
678 int cnt, i, j;
680 result = merge_bases_many(one, n, twos);
681 for (i = 0; i < n; i++) {
682 if (one == twos[i])
683 return result;
684 }
685 if (!result || !result->next) {
686 if (cleanup) {
687 clear_commit_marks(one, all_flags);
688 for (i = 0; i < n; i++)
689 clear_commit_marks(twos[i], all_flags);
690 }
691 return result;
692 }
694 /* There are more than one */
695 cnt = 0;
696 list = result;
697 while (list) {
698 list = list->next;
699 cnt++;
700 }
701 rslt = xcalloc(cnt, sizeof(*rslt));
702 for (list = result, i = 0; list; list = list->next)
703 rslt[i++] = list->item;
704 free_commit_list(result);
706 clear_commit_marks(one, all_flags);
707 for (i = 0; i < n; i++)
708 clear_commit_marks(twos[i], all_flags);
709 for (i = 0; i < cnt - 1; i++) {
710 for (j = i+1; j < cnt; j++) {
711 if (!rslt[i] || !rslt[j])
712 continue;
713 result = merge_bases_many(rslt[i], 1, &rslt[j]);
714 clear_commit_marks(rslt[i], all_flags);
715 clear_commit_marks(rslt[j], all_flags);
716 for (list = result; list; list = list->next) {
717 if (rslt[i] == list->item)
718 rslt[i] = NULL;
719 if (rslt[j] == list->item)
720 rslt[j] = NULL;
721 }
722 }
723 }
725 /* Surviving ones in rslt[] are the independent results */
726 result = NULL;
727 for (i = 0; i < cnt; i++) {
728 if (rslt[i])
729 commit_list_insert_by_date(rslt[i], &result);
730 }
731 free(rslt);
732 return result;
733 }
735 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
736 int cleanup)
737 {
738 return get_merge_bases_many(one, 1, &two, cleanup);
739 }
741 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
742 {
743 if (!with_commit)
744 return 1;
745 while (with_commit) {
746 struct commit *other;
748 other = with_commit->item;
749 with_commit = with_commit->next;
750 if (in_merge_bases(other, &commit, 1))
751 return 1;
752 }
753 return 0;
754 }
756 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
757 {
758 struct commit_list *bases, *b;
759 int ret = 0;
761 if (num == 1)
762 bases = get_merge_bases(commit, *reference, 1);
763 else
764 die("not yet");
765 for (b = bases; b; b = b->next) {
766 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
767 ret = 1;
768 break;
769 }
770 }
772 free_commit_list(bases);
773 return ret;
774 }
776 struct commit_list *reduce_heads(struct commit_list *heads)
777 {
778 struct commit_list *p;
779 struct commit_list *result = NULL, **tail = &result;
780 struct commit **other;
781 size_t num_head, num_other;
783 if (!heads)
784 return NULL;
786 /* Avoid unnecessary reallocations */
787 for (p = heads, num_head = 0; p; p = p->next)
788 num_head++;
789 other = xcalloc(sizeof(*other), num_head);
791 /* For each commit, see if it can be reached by others */
792 for (p = heads; p; p = p->next) {
793 struct commit_list *q, *base;
795 /* Do we already have this in the result? */
796 for (q = result; q; q = q->next)
797 if (p->item == q->item)
798 break;
799 if (q)
800 continue;
802 num_other = 0;
803 for (q = heads; q; q = q->next) {
804 if (p->item == q->item)
805 continue;
806 other[num_other++] = q->item;
807 }
808 if (num_other)
809 base = get_merge_bases_many(p->item, num_other, other, 1);
810 else
811 base = NULL;
812 /*
813 * If p->item does not have anything common with other
814 * commits, there won't be any merge base. If it is
815 * reachable from some of the others, p->item will be
816 * the merge base. If its history is connected with
817 * others, but p->item is not reachable by others, we
818 * will get something other than p->item back.
819 */
820 if (!base || (base->item != p->item))
821 tail = &(commit_list_insert(p->item, tail)->next);
822 free_commit_list(base);
823 }
824 free(other);
825 return result;
826 }
828 static const char commit_utf8_warn[] =
829 "Warning: commit message does not conform to UTF-8.\n"
830 "You may want to amend it after fixing the message, or set the config\n"
831 "variable i18n.commitencoding to the encoding your project uses.\n";
833 int commit_tree(const char *msg, unsigned char *tree,
834 struct commit_list *parents, unsigned char *ret,
835 const char *author)
836 {
837 int result;
838 int encoding_is_utf8;
839 struct strbuf buffer;
841 assert_sha1_type(tree, OBJ_TREE);
843 /* Not having i18n.commitencoding is the same as having utf-8 */
844 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
846 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
847 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
849 /*
850 * NOTE! This ordering means that the same exact tree merged with a
851 * different order of parents will be a _different_ changeset even
852 * if everything else stays the same.
853 */
854 while (parents) {
855 struct commit_list *next = parents->next;
856 strbuf_addf(&buffer, "parent %s\n",
857 sha1_to_hex(parents->item->object.sha1));
858 free(parents);
859 parents = next;
860 }
862 /* Person/date information */
863 if (!author)
864 author = git_author_info(IDENT_ERROR_ON_NO_NAME);
865 strbuf_addf(&buffer, "author %s\n", author);
866 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
867 if (!encoding_is_utf8)
868 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
869 strbuf_addch(&buffer, '\n');
871 /* And add the comment */
872 strbuf_addstr(&buffer, msg);
874 /* And check the encoding */
875 if (encoding_is_utf8 && !is_utf8(buffer.buf))
876 fprintf(stderr, commit_utf8_warn);
878 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
879 strbuf_release(&buffer);
880 return result;
881 }
883 static int in_commit_list(const struct commit_list *want, struct commit *c)
884 {
885 for (; want; want = want->next)
886 if (!hashcmp(want->item->object.sha1, c->object.sha1))
887 return 1;
888 return 0;
889 }
891 static int contains_recurse(struct commit *candidate,
892 const struct commit_list *want,
893 unsigned long cutoff)
894 {
895 struct commit_list *p;
897 /* was it previously marked as containing a want commit? */
898 if (candidate->object.flags & TMP_MARK)
899 return 1;
900 /* or marked as not possibly containing a want commit? */
901 if (candidate->object.flags & UNINTERESTING)
902 return 0;
903 /* or are we it? */
904 if (in_commit_list(want, candidate))
905 return 1;
907 if (parse_commit(candidate) < 0)
908 return 0;
910 /* stop searching if we go too far back in time */
911 if (candidate->date < cutoff)
912 return 0;
914 /* Otherwise recurse and mark ourselves for future traversals. */
915 for (p = candidate->parents; p; p = p->next) {
916 if (contains_recurse(p->item, want, cutoff)) {
917 candidate->object.flags |= TMP_MARK;
918 return 1;
919 }
920 }
921 candidate->object.flags |= UNINTERESTING;
922 return 0;
923 }
925 int contains(struct commit *candidate, const struct commit_list *want)
926 {
927 unsigned long cutoff = 0;
929 if (core_clock_skew >= 0) {
930 const struct commit_list *c;
931 unsigned long min_date = ULONG_MAX;
932 for (c = want; c; c = c->next) {
933 if (parse_commit(c->item) < 0)
934 continue;
935 if (c->item->date < min_date)
936 min_date = c->item->date;
937 }
938 if (min_date > core_clock_skew)
939 cutoff = min_date - core_clock_skew;
940 }
942 return contains_recurse(candidate, want, cutoff);
943 }