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