Code

246cd18b0eb6306e8142ae349ddfd753c2c656a1
[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"
10 int save_commit_buffer = 1;
12 const char *commit_type = "commit";
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 }
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);
32         if (!obj)
33                 return NULL;
34         return check_commit(obj, sha1, quiet);
35 }
37 struct commit *lookup_commit_reference(const unsigned char *sha1)
38 {
39         return lookup_commit_reference_gently(sha1, 0);
40 }
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 }
52 struct commit *lookup_commit_reference_by_name(const char *name)
53 {
54         unsigned char sha1[20];
55         struct commit *commit;
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 }
65 static unsigned long parse_commit_date(const char *buf, const char *tail)
66 {
67         const char *dateptr;
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 }
92 static struct commit_graft **commit_graft;
93 static int commit_graft_alloc, commit_graft_nr;
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;
114 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
116         int pos = commit_graft_pos(graft->sha1);
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;
143 struct commit_graft *read_graft_line(char *buf, int len)
145         /* The format is just "Commit Parent1 Parent2 ...\n" */
146         int i;
147         struct commit_graft *graft = NULL;
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                 goto bad_graft_data;
155         i = (len + 1) / 41 - 1;
156         graft = xmalloc(sizeof(*graft) + 20 * i);
157         graft->nr_parent = i;
158         if (get_sha1_hex(buf, graft->sha1))
159                 goto bad_graft_data;
160         for (i = 40; i < len; i += 41) {
161                 if (buf[i] != ' ')
162                         goto bad_graft_data;
163                 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
164                         goto bad_graft_data;
165         }
166         return graft;
168 bad_graft_data:
169         error("bad graft data: %s", buf);
170         free(graft);
171         return NULL;
174 static int read_graft_file(const char *graft_file)
176         FILE *fp = fopen(graft_file, "r");
177         char buf[1024];
178         if (!fp)
179                 return -1;
180         while (fgets(buf, sizeof(buf), fp)) {
181                 /* The format is just "Commit Parent1 Parent2 ...\n" */
182                 int len = strlen(buf);
183                 struct commit_graft *graft = read_graft_line(buf, len);
184                 if (!graft)
185                         continue;
186                 if (register_commit_graft(graft, 1))
187                         error("duplicate graft data: %s", buf);
188         }
189         fclose(fp);
190         return 0;
193 static void prepare_commit_graft(void)
195         static int commit_graft_prepared;
196         char *graft_file;
198         if (commit_graft_prepared)
199                 return;
200         graft_file = get_graft_file();
201         read_graft_file(graft_file);
202         /* make sure shallows are read */
203         is_repository_shallow();
204         commit_graft_prepared = 1;
207 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
209         int pos;
210         prepare_commit_graft();
211         pos = commit_graft_pos(sha1);
212         if (pos < 0)
213                 return NULL;
214         return commit_graft[pos];
217 int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
219         int i, ret;
220         for (i = ret = 0; i < commit_graft_nr && !ret; i++)
221                 ret = fn(commit_graft[i], cb_data);
222         return ret;
225 int write_shallow_commits(struct strbuf *out, int use_pack_protocol)
227         int i, count = 0;
228         for (i = 0; i < commit_graft_nr; i++)
229                 if (commit_graft[i]->nr_parent < 0) {
230                         const char *hex =
231                                 sha1_to_hex(commit_graft[i]->sha1);
232                         count++;
233                         if (use_pack_protocol)
234                                 packet_buf_write(out, "shallow %s", hex);
235                         else {
236                                 strbuf_addstr(out, hex);
237                                 strbuf_addch(out, '\n');
238                         }
239                 }
240         return count;
243 int unregister_shallow(const unsigned char *sha1)
245         int pos = commit_graft_pos(sha1);
246         if (pos < 0)
247                 return -1;
248         if (pos + 1 < commit_graft_nr)
249                 memmove(commit_graft + pos, commit_graft + pos + 1,
250                                 sizeof(struct commit_graft *)
251                                 * (commit_graft_nr - pos - 1));
252         commit_graft_nr--;
253         return 0;
256 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
258         const char *tail = buffer;
259         const char *bufptr = buffer;
260         unsigned char parent[20];
261         struct commit_list **pptr;
262         struct commit_graft *graft;
264         if (item->object.parsed)
265                 return 0;
266         item->object.parsed = 1;
267         tail += size;
268         if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
269                 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
270         if (get_sha1_hex(bufptr + 5, parent) < 0)
271                 return error("bad tree pointer in commit %s",
272                              sha1_to_hex(item->object.sha1));
273         item->tree = lookup_tree(parent);
274         bufptr += 46; /* "tree " + "hex sha1" + "\n" */
275         pptr = &item->parents;
277         graft = lookup_commit_graft(item->object.sha1);
278         while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
279                 struct commit *new_parent;
281                 if (tail <= bufptr + 48 ||
282                     get_sha1_hex(bufptr + 7, parent) ||
283                     bufptr[47] != '\n')
284                         return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
285                 bufptr += 48;
286                 /*
287                  * The clone is shallow if nr_parent < 0, and we must
288                  * not traverse its real parents even when we unhide them.
289                  */
290                 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
291                         continue;
292                 new_parent = lookup_commit(parent);
293                 if (new_parent)
294                         pptr = &commit_list_insert(new_parent, pptr)->next;
295         }
296         if (graft) {
297                 int i;
298                 struct commit *new_parent;
299                 for (i = 0; i < graft->nr_parent; i++) {
300                         new_parent = lookup_commit(graft->parent[i]);
301                         if (!new_parent)
302                                 continue;
303                         pptr = &commit_list_insert(new_parent, pptr)->next;
304                 }
305         }
306         item->date = parse_commit_date(bufptr, tail);
308         return 0;
311 int parse_commit(struct commit *item)
313         enum object_type type;
314         void *buffer;
315         unsigned long size;
316         int ret;
318         if (!item)
319                 return -1;
320         if (item->object.parsed)
321                 return 0;
322         buffer = read_sha1_file(item->object.sha1, &type, &size);
323         if (!buffer)
324                 return error("Could not read %s",
325                              sha1_to_hex(item->object.sha1));
326         if (type != OBJ_COMMIT) {
327                 free(buffer);
328                 return error("Object %s not a commit",
329                              sha1_to_hex(item->object.sha1));
330         }
331         ret = parse_commit_buffer(item, buffer, size);
332         if (save_commit_buffer && !ret) {
333                 item->buffer = buffer;
334                 return 0;
335         }
336         free(buffer);
337         return ret;
340 int find_commit_subject(const char *commit_buffer, const char **subject)
342         const char *eol;
343         const char *p = commit_buffer;
345         while (*p && (*p != '\n' || p[1] != '\n'))
346                 p++;
347         if (*p) {
348                 p += 2;
349                 for (eol = p; *eol && *eol != '\n'; eol++)
350                         ; /* do nothing */
351         } else
352                 eol = p;
354         *subject = p;
356         return eol - p;
359 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
361         struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
362         new_list->item = item;
363         new_list->next = *list_p;
364         *list_p = new_list;
365         return new_list;
368 unsigned commit_list_count(const struct commit_list *l)
370         unsigned c = 0;
371         for (; l; l = l->next )
372                 c++;
373         return c;
376 void free_commit_list(struct commit_list *list)
378         while (list) {
379                 struct commit_list *temp = list;
380                 list = temp->next;
381                 free(temp);
382         }
385 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
387         struct commit_list **pp = list;
388         struct commit_list *p;
389         while ((p = *pp) != NULL) {
390                 if (p->item->date < item->date) {
391                         break;
392                 }
393                 pp = &p->next;
394         }
395         return commit_list_insert(item, pp);
399 void commit_list_sort_by_date(struct commit_list **list)
401         struct commit_list *ret = NULL;
402         while (*list) {
403                 commit_list_insert_by_date((*list)->item, &ret);
404                 *list = (*list)->next;
405         }
406         *list = ret;
409 struct commit *pop_most_recent_commit(struct commit_list **list,
410                                       unsigned int mark)
412         struct commit *ret = (*list)->item;
413         struct commit_list *parents = ret->parents;
414         struct commit_list *old = *list;
416         *list = (*list)->next;
417         free(old);
419         while (parents) {
420                 struct commit *commit = parents->item;
421                 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
422                         commit->object.flags |= mark;
423                         commit_list_insert_by_date(commit, list);
424                 }
425                 parents = parents->next;
426         }
427         return ret;
430 void clear_commit_marks(struct commit *commit, unsigned int mark)
432         while (commit) {
433                 struct commit_list *parents;
435                 if (!(mark & commit->object.flags))
436                         return;
438                 commit->object.flags &= ~mark;
440                 parents = commit->parents;
441                 if (!parents)
442                         return;
444                 while ((parents = parents->next))
445                         clear_commit_marks(parents->item, mark);
447                 commit = commit->parents->item;
448         }
451 struct commit *pop_commit(struct commit_list **stack)
453         struct commit_list *top = *stack;
454         struct commit *item = top ? top->item : NULL;
456         if (top) {
457                 *stack = top->next;
458                 free(top);
459         }
460         return item;
463 /*
464  * Performs an in-place topological sort on the list supplied.
465  */
466 void sort_in_topological_order(struct commit_list ** list, int lifo)
468         struct commit_list *next, *orig = *list;
469         struct commit_list *work, **insert;
470         struct commit_list **pptr;
472         if (!orig)
473                 return;
474         *list = NULL;
476         /* Mark them and clear the indegree */
477         for (next = orig; next; next = next->next) {
478                 struct commit *commit = next->item;
479                 commit->indegree = 1;
480         }
482         /* update the indegree */
483         for (next = orig; next; next = next->next) {
484                 struct commit_list * parents = next->item->parents;
485                 while (parents) {
486                         struct commit *parent = parents->item;
488                         if (parent->indegree)
489                                 parent->indegree++;
490                         parents = parents->next;
491                 }
492         }
494         /*
495          * find the tips
496          *
497          * tips are nodes not reachable from any other node in the list
498          *
499          * the tips serve as a starting set for the work queue.
500          */
501         work = NULL;
502         insert = &work;
503         for (next = orig; next; next = next->next) {
504                 struct commit *commit = next->item;
506                 if (commit->indegree == 1)
507                         insert = &commit_list_insert(commit, insert)->next;
508         }
510         /* process the list in topological order */
511         if (!lifo)
512                 commit_list_sort_by_date(&work);
514         pptr = list;
515         *list = NULL;
516         while (work) {
517                 struct commit *commit;
518                 struct commit_list *parents, *work_item;
520                 work_item = work;
521                 work = work_item->next;
522                 work_item->next = NULL;
524                 commit = work_item->item;
525                 for (parents = commit->parents; parents ; parents = parents->next) {
526                         struct commit *parent=parents->item;
528                         if (!parent->indegree)
529                                 continue;
531                         /*
532                          * parents are only enqueued for emission
533                          * when all their children have been emitted thereby
534                          * guaranteeing topological order.
535                          */
536                         if (--parent->indegree == 1) {
537                                 if (!lifo)
538                                         commit_list_insert_by_date(parent, &work);
539                                 else
540                                         commit_list_insert(parent, &work);
541                         }
542                 }
543                 /*
544                  * work_item is a commit all of whose children
545                  * have already been emitted. we can emit it now.
546                  */
547                 commit->indegree = 0;
548                 *pptr = work_item;
549                 pptr = &work_item->next;
550         }
553 /* merge-base stuff */
555 /* bits #0..15 in revision.h */
556 #define PARENT1         (1u<<16)
557 #define PARENT2         (1u<<17)
558 #define STALE           (1u<<18)
559 #define RESULT          (1u<<19)
561 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
563 static struct commit *interesting(struct commit_list *list)
565         while (list) {
566                 struct commit *commit = list->item;
567                 list = list->next;
568                 if (commit->object.flags & STALE)
569                         continue;
570                 return commit;
571         }
572         return NULL;
575 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
577         struct commit_list *list = NULL;
578         struct commit_list *result = NULL;
579         int i;
581         for (i = 0; i < n; i++) {
582                 if (one == twos[i])
583                         /*
584                          * We do not mark this even with RESULT so we do not
585                          * have to clean it up.
586                          */
587                         return commit_list_insert(one, &result);
588         }
590         if (parse_commit(one))
591                 return NULL;
592         for (i = 0; i < n; i++) {
593                 if (parse_commit(twos[i]))
594                         return NULL;
595         }
597         one->object.flags |= PARENT1;
598         commit_list_insert_by_date(one, &list);
599         for (i = 0; i < n; i++) {
600                 twos[i]->object.flags |= PARENT2;
601                 commit_list_insert_by_date(twos[i], &list);
602         }
604         while (interesting(list)) {
605                 struct commit *commit;
606                 struct commit_list *parents;
607                 struct commit_list *next;
608                 int flags;
610                 commit = list->item;
611                 next = list->next;
612                 free(list);
613                 list = next;
615                 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
616                 if (flags == (PARENT1 | PARENT2)) {
617                         if (!(commit->object.flags & RESULT)) {
618                                 commit->object.flags |= RESULT;
619                                 commit_list_insert_by_date(commit, &result);
620                         }
621                         /* Mark parents of a found merge stale */
622                         flags |= STALE;
623                 }
624                 parents = commit->parents;
625                 while (parents) {
626                         struct commit *p = parents->item;
627                         parents = parents->next;
628                         if ((p->object.flags & flags) == flags)
629                                 continue;
630                         if (parse_commit(p))
631                                 return NULL;
632                         p->object.flags |= flags;
633                         commit_list_insert_by_date(p, &list);
634                 }
635         }
637         /* Clean up the result to remove stale ones */
638         free_commit_list(list);
639         list = result; result = NULL;
640         while (list) {
641                 struct commit_list *next = list->next;
642                 if (!(list->item->object.flags & STALE))
643                         commit_list_insert_by_date(list->item, &result);
644                 free(list);
645                 list = next;
646         }
647         return result;
650 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
652         struct commit_list *i, *j, *k, *ret = NULL;
653         struct commit_list **pptr = &ret;
655         for (i = in; i; i = i->next) {
656                 if (!ret)
657                         pptr = &commit_list_insert(i->item, pptr)->next;
658                 else {
659                         struct commit_list *new = NULL, *end = NULL;
661                         for (j = ret; j; j = j->next) {
662                                 struct commit_list *bases;
663                                 bases = get_merge_bases(i->item, j->item, 1);
664                                 if (!new)
665                                         new = bases;
666                                 else
667                                         end->next = bases;
668                                 for (k = bases; k; k = k->next)
669                                         end = k;
670                         }
671                         ret = new;
672                 }
673         }
674         return ret;
677 struct commit_list *get_merge_bases_many(struct commit *one,
678                                          int n,
679                                          struct commit **twos,
680                                          int cleanup)
682         struct commit_list *list;
683         struct commit **rslt;
684         struct commit_list *result;
685         int cnt, i, j;
687         result = merge_bases_many(one, n, twos);
688         for (i = 0; i < n; i++) {
689                 if (one == twos[i])
690                         return result;
691         }
692         if (!result || !result->next) {
693                 if (cleanup) {
694                         clear_commit_marks(one, all_flags);
695                         for (i = 0; i < n; i++)
696                                 clear_commit_marks(twos[i], all_flags);
697                 }
698                 return result;
699         }
701         /* There are more than one */
702         cnt = 0;
703         list = result;
704         while (list) {
705                 list = list->next;
706                 cnt++;
707         }
708         rslt = xcalloc(cnt, sizeof(*rslt));
709         for (list = result, i = 0; list; list = list->next)
710                 rslt[i++] = list->item;
711         free_commit_list(result);
713         clear_commit_marks(one, all_flags);
714         for (i = 0; i < n; i++)
715                 clear_commit_marks(twos[i], all_flags);
716         for (i = 0; i < cnt - 1; i++) {
717                 for (j = i+1; j < cnt; j++) {
718                         if (!rslt[i] || !rslt[j])
719                                 continue;
720                         result = merge_bases_many(rslt[i], 1, &rslt[j]);
721                         clear_commit_marks(rslt[i], all_flags);
722                         clear_commit_marks(rslt[j], all_flags);
723                         for (list = result; list; list = list->next) {
724                                 if (rslt[i] == list->item)
725                                         rslt[i] = NULL;
726                                 if (rslt[j] == list->item)
727                                         rslt[j] = NULL;
728                         }
729                 }
730         }
732         /* Surviving ones in rslt[] are the independent results */
733         result = NULL;
734         for (i = 0; i < cnt; i++) {
735                 if (rslt[i])
736                         commit_list_insert_by_date(rslt[i], &result);
737         }
738         free(rslt);
739         return result;
742 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
743                                     int cleanup)
745         return get_merge_bases_many(one, 1, &two, cleanup);
748 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
750         if (!with_commit)
751                 return 1;
752         while (with_commit) {
753                 struct commit *other;
755                 other = with_commit->item;
756                 with_commit = with_commit->next;
757                 if (in_merge_bases(other, &commit, 1))
758                         return 1;
759         }
760         return 0;
763 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
765         struct commit_list *bases, *b;
766         int ret = 0;
768         if (num == 1)
769                 bases = get_merge_bases(commit, *reference, 1);
770         else
771                 die("not yet");
772         for (b = bases; b; b = b->next) {
773                 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
774                         ret = 1;
775                         break;
776                 }
777         }
779         free_commit_list(bases);
780         return ret;
783 struct commit_list *reduce_heads(struct commit_list *heads)
785         struct commit_list *p;
786         struct commit_list *result = NULL, **tail = &result;
787         struct commit **other;
788         size_t num_head, num_other;
790         if (!heads)
791                 return NULL;
793         /* Avoid unnecessary reallocations */
794         for (p = heads, num_head = 0; p; p = p->next)
795                 num_head++;
796         other = xcalloc(sizeof(*other), num_head);
798         /* For each commit, see if it can be reached by others */
799         for (p = heads; p; p = p->next) {
800                 struct commit_list *q, *base;
802                 /* Do we already have this in the result? */
803                 for (q = result; q; q = q->next)
804                         if (p->item == q->item)
805                                 break;
806                 if (q)
807                         continue;
809                 num_other = 0;
810                 for (q = heads; q; q = q->next) {
811                         if (p->item == q->item)
812                                 continue;
813                         other[num_other++] = q->item;
814                 }
815                 if (num_other)
816                         base = get_merge_bases_many(p->item, num_other, other, 1);
817                 else
818                         base = NULL;
819                 /*
820                  * If p->item does not have anything common with other
821                  * commits, there won't be any merge base.  If it is
822                  * reachable from some of the others, p->item will be
823                  * the merge base.  If its history is connected with
824                  * others, but p->item is not reachable by others, we
825                  * will get something other than p->item back.
826                  */
827                 if (!base || (base->item != p->item))
828                         tail = &(commit_list_insert(p->item, tail)->next);
829                 free_commit_list(base);
830         }
831         free(other);
832         return result;
835 static const char commit_utf8_warn[] =
836 "Warning: commit message does not conform to UTF-8.\n"
837 "You may want to amend it after fixing the message, or set the config\n"
838 "variable i18n.commitencoding to the encoding your project uses.\n";
840 int commit_tree(const char *msg, unsigned char *tree,
841                 struct commit_list *parents, unsigned char *ret,
842                 const char *author)
844         int result;
845         int encoding_is_utf8;
846         struct strbuf buffer;
848         assert_sha1_type(tree, OBJ_TREE);
850         /* Not having i18n.commitencoding is the same as having utf-8 */
851         encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
853         strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
854         strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
856         /*
857          * NOTE! This ordering means that the same exact tree merged with a
858          * different order of parents will be a _different_ changeset even
859          * if everything else stays the same.
860          */
861         while (parents) {
862                 struct commit_list *next = parents->next;
863                 strbuf_addf(&buffer, "parent %s\n",
864                         sha1_to_hex(parents->item->object.sha1));
865                 free(parents);
866                 parents = next;
867         }
869         /* Person/date information */
870         if (!author)
871                 author = git_author_info(IDENT_ERROR_ON_NO_NAME);
872         strbuf_addf(&buffer, "author %s\n", author);
873         strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
874         if (!encoding_is_utf8)
875                 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
876         strbuf_addch(&buffer, '\n');
878         /* And add the comment */
879         strbuf_addstr(&buffer, msg);
881         /* And check the encoding */
882         if (encoding_is_utf8 && !is_utf8(buffer.buf))
883                 fprintf(stderr, commit_utf8_warn);
885         result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
886         strbuf_release(&buffer);
887         return result;