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