Code

commit: factor out clear_commit_marks_for_object_array
[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 write_shallow_commits(struct strbuf *out, int use_pack_protocol)
219         int i, count = 0;
220         for (i = 0; i < commit_graft_nr; i++)
221                 if (commit_graft[i]->nr_parent < 0) {
222                         const char *hex =
223                                 sha1_to_hex(commit_graft[i]->sha1);
224                         count++;
225                         if (use_pack_protocol)
226                                 packet_buf_write(out, "shallow %s", hex);
227                         else {
228                                 strbuf_addstr(out, hex);
229                                 strbuf_addch(out, '\n');
230                         }
231                 }
232         return count;
235 int unregister_shallow(const unsigned char *sha1)
237         int pos = commit_graft_pos(sha1);
238         if (pos < 0)
239                 return -1;
240         if (pos + 1 < commit_graft_nr)
241                 memmove(commit_graft + pos, commit_graft + pos + 1,
242                                 sizeof(struct commit_graft *)
243                                 * (commit_graft_nr - pos - 1));
244         commit_graft_nr--;
245         return 0;
248 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
250         const char *tail = buffer;
251         const char *bufptr = buffer;
252         unsigned char parent[20];
253         struct commit_list **pptr;
254         struct commit_graft *graft;
256         if (item->object.parsed)
257                 return 0;
258         item->object.parsed = 1;
259         tail += size;
260         if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
261                 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
262         if (get_sha1_hex(bufptr + 5, parent) < 0)
263                 return error("bad tree pointer in commit %s",
264                              sha1_to_hex(item->object.sha1));
265         item->tree = lookup_tree(parent);
266         bufptr += 46; /* "tree " + "hex sha1" + "\n" */
267         pptr = &item->parents;
269         graft = lookup_commit_graft(item->object.sha1);
270         while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
271                 struct commit *new_parent;
273                 if (tail <= bufptr + 48 ||
274                     get_sha1_hex(bufptr + 7, parent) ||
275                     bufptr[47] != '\n')
276                         return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
277                 bufptr += 48;
278                 /*
279                  * The clone is shallow if nr_parent < 0, and we must
280                  * not traverse its real parents even when we unhide them.
281                  */
282                 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
283                         continue;
284                 new_parent = lookup_commit(parent);
285                 if (new_parent)
286                         pptr = &commit_list_insert(new_parent, pptr)->next;
287         }
288         if (graft) {
289                 int i;
290                 struct commit *new_parent;
291                 for (i = 0; i < graft->nr_parent; i++) {
292                         new_parent = lookup_commit(graft->parent[i]);
293                         if (!new_parent)
294                                 continue;
295                         pptr = &commit_list_insert(new_parent, pptr)->next;
296                 }
297         }
298         item->date = parse_commit_date(bufptr, tail);
300         return 0;
303 int parse_commit(struct commit *item)
305         enum object_type type;
306         void *buffer;
307         unsigned long size;
308         int ret;
310         if (!item)
311                 return -1;
312         if (item->object.parsed)
313                 return 0;
314         buffer = read_sha1_file(item->object.sha1, &type, &size);
315         if (!buffer)
316                 return error("Could not read %s",
317                              sha1_to_hex(item->object.sha1));
318         if (type != OBJ_COMMIT) {
319                 free(buffer);
320                 return error("Object %s not a commit",
321                              sha1_to_hex(item->object.sha1));
322         }
323         ret = parse_commit_buffer(item, buffer, size);
324         if (save_commit_buffer && !ret) {
325                 item->buffer = buffer;
326                 return 0;
327         }
328         free(buffer);
329         return ret;
332 int find_commit_subject(const char *commit_buffer, const char **subject)
334         const char *eol;
335         const char *p = commit_buffer;
337         while (*p && (*p != '\n' || p[1] != '\n'))
338                 p++;
339         if (*p) {
340                 p += 2;
341                 for (eol = p; *eol && *eol != '\n'; eol++)
342                         ; /* do nothing */
343         } else
344                 eol = p;
346         *subject = p;
348         return eol - p;
351 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
353         struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
354         new_list->item = item;
355         new_list->next = *list_p;
356         *list_p = new_list;
357         return new_list;
360 unsigned commit_list_count(const struct commit_list *l)
362         unsigned c = 0;
363         for (; l; l = l->next )
364                 c++;
365         return c;
368 void free_commit_list(struct commit_list *list)
370         while (list) {
371                 struct commit_list *temp = list;
372                 list = temp->next;
373                 free(temp);
374         }
377 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
379         struct commit_list **pp = list;
380         struct commit_list *p;
381         while ((p = *pp) != NULL) {
382                 if (p->item->date < item->date) {
383                         break;
384                 }
385                 pp = &p->next;
386         }
387         return commit_list_insert(item, pp);
391 void commit_list_sort_by_date(struct commit_list **list)
393         struct commit_list *ret = NULL;
394         while (*list) {
395                 commit_list_insert_by_date((*list)->item, &ret);
396                 *list = (*list)->next;
397         }
398         *list = ret;
401 struct commit *pop_most_recent_commit(struct commit_list **list,
402                                       unsigned int mark)
404         struct commit *ret = (*list)->item;
405         struct commit_list *parents = ret->parents;
406         struct commit_list *old = *list;
408         *list = (*list)->next;
409         free(old);
411         while (parents) {
412                 struct commit *commit = parents->item;
413                 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
414                         commit->object.flags |= mark;
415                         commit_list_insert_by_date(commit, list);
416                 }
417                 parents = parents->next;
418         }
419         return ret;
422 void clear_commit_marks(struct commit *commit, unsigned int mark)
424         while (commit) {
425                 struct commit_list *parents;
427                 if (!(mark & commit->object.flags))
428                         return;
430                 commit->object.flags &= ~mark;
432                 parents = commit->parents;
433                 if (!parents)
434                         return;
436                 while ((parents = parents->next))
437                         clear_commit_marks(parents->item, mark);
439                 commit = commit->parents->item;
440         }
443 void clear_commit_marks_for_object_array(struct object_array *a, unsigned mark)
445         struct object *object;
446         struct commit *commit;
447         unsigned int i;
449         for (i = 0; i < a->nr; i++) {
450                 object = a->objects[i].item;
451                 commit = lookup_commit_reference_gently(object->sha1, 1);
452                 if (commit)
453                         clear_commit_marks(commit, mark);
454         }
457 struct commit *pop_commit(struct commit_list **stack)
459         struct commit_list *top = *stack;
460         struct commit *item = top ? top->item : NULL;
462         if (top) {
463                 *stack = top->next;
464                 free(top);
465         }
466         return item;
469 /*
470  * Performs an in-place topological sort on the list supplied.
471  */
472 void sort_in_topological_order(struct commit_list ** list, int lifo)
474         struct commit_list *next, *orig = *list;
475         struct commit_list *work, **insert;
476         struct commit_list **pptr;
478         if (!orig)
479                 return;
480         *list = NULL;
482         /* Mark them and clear the indegree */
483         for (next = orig; next; next = next->next) {
484                 struct commit *commit = next->item;
485                 commit->indegree = 1;
486         }
488         /* update the indegree */
489         for (next = orig; next; next = next->next) {
490                 struct commit_list * parents = next->item->parents;
491                 while (parents) {
492                         struct commit *parent = parents->item;
494                         if (parent->indegree)
495                                 parent->indegree++;
496                         parents = parents->next;
497                 }
498         }
500         /*
501          * find the tips
502          *
503          * tips are nodes not reachable from any other node in the list
504          *
505          * the tips serve as a starting set for the work queue.
506          */
507         work = NULL;
508         insert = &work;
509         for (next = orig; next; next = next->next) {
510                 struct commit *commit = next->item;
512                 if (commit->indegree == 1)
513                         insert = &commit_list_insert(commit, insert)->next;
514         }
516         /* process the list in topological order */
517         if (!lifo)
518                 commit_list_sort_by_date(&work);
520         pptr = list;
521         *list = NULL;
522         while (work) {
523                 struct commit *commit;
524                 struct commit_list *parents, *work_item;
526                 work_item = work;
527                 work = work_item->next;
528                 work_item->next = NULL;
530                 commit = work_item->item;
531                 for (parents = commit->parents; parents ; parents = parents->next) {
532                         struct commit *parent=parents->item;
534                         if (!parent->indegree)
535                                 continue;
537                         /*
538                          * parents are only enqueued for emission
539                          * when all their children have been emitted thereby
540                          * guaranteeing topological order.
541                          */
542                         if (--parent->indegree == 1) {
543                                 if (!lifo)
544                                         commit_list_insert_by_date(parent, &work);
545                                 else
546                                         commit_list_insert(parent, &work);
547                         }
548                 }
549                 /*
550                  * work_item is a commit all of whose children
551                  * have already been emitted. we can emit it now.
552                  */
553                 commit->indegree = 0;
554                 *pptr = work_item;
555                 pptr = &work_item->next;
556         }
559 /* merge-base stuff */
561 /* bits #0..15 in revision.h */
562 #define PARENT1         (1u<<16)
563 #define PARENT2         (1u<<17)
564 #define STALE           (1u<<18)
565 #define RESULT          (1u<<19)
567 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
569 static struct commit *interesting(struct commit_list *list)
571         while (list) {
572                 struct commit *commit = list->item;
573                 list = list->next;
574                 if (commit->object.flags & STALE)
575                         continue;
576                 return commit;
577         }
578         return NULL;
581 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
583         struct commit_list *list = NULL;
584         struct commit_list *result = NULL;
585         int i;
587         for (i = 0; i < n; i++) {
588                 if (one == twos[i])
589                         /*
590                          * We do not mark this even with RESULT so we do not
591                          * have to clean it up.
592                          */
593                         return commit_list_insert(one, &result);
594         }
596         if (parse_commit(one))
597                 return NULL;
598         for (i = 0; i < n; i++) {
599                 if (parse_commit(twos[i]))
600                         return NULL;
601         }
603         one->object.flags |= PARENT1;
604         commit_list_insert_by_date(one, &list);
605         for (i = 0; i < n; i++) {
606                 twos[i]->object.flags |= PARENT2;
607                 commit_list_insert_by_date(twos[i], &list);
608         }
610         while (interesting(list)) {
611                 struct commit *commit;
612                 struct commit_list *parents;
613                 struct commit_list *next;
614                 int flags;
616                 commit = list->item;
617                 next = list->next;
618                 free(list);
619                 list = next;
621                 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
622                 if (flags == (PARENT1 | PARENT2)) {
623                         if (!(commit->object.flags & RESULT)) {
624                                 commit->object.flags |= RESULT;
625                                 commit_list_insert_by_date(commit, &result);
626                         }
627                         /* Mark parents of a found merge stale */
628                         flags |= STALE;
629                 }
630                 parents = commit->parents;
631                 while (parents) {
632                         struct commit *p = parents->item;
633                         parents = parents->next;
634                         if ((p->object.flags & flags) == flags)
635                                 continue;
636                         if (parse_commit(p))
637                                 return NULL;
638                         p->object.flags |= flags;
639                         commit_list_insert_by_date(p, &list);
640                 }
641         }
643         /* Clean up the result to remove stale ones */
644         free_commit_list(list);
645         list = result; result = NULL;
646         while (list) {
647                 struct commit_list *next = list->next;
648                 if (!(list->item->object.flags & STALE))
649                         commit_list_insert_by_date(list->item, &result);
650                 free(list);
651                 list = next;
652         }
653         return result;
656 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
658         struct commit_list *i, *j, *k, *ret = NULL;
659         struct commit_list **pptr = &ret;
661         for (i = in; i; i = i->next) {
662                 if (!ret)
663                         pptr = &commit_list_insert(i->item, pptr)->next;
664                 else {
665                         struct commit_list *new = NULL, *end = NULL;
667                         for (j = ret; j; j = j->next) {
668                                 struct commit_list *bases;
669                                 bases = get_merge_bases(i->item, j->item, 1);
670                                 if (!new)
671                                         new = bases;
672                                 else
673                                         end->next = bases;
674                                 for (k = bases; k; k = k->next)
675                                         end = k;
676                         }
677                         ret = new;
678                 }
679         }
680         return ret;
683 struct commit_list *get_merge_bases_many(struct commit *one,
684                                          int n,
685                                          struct commit **twos,
686                                          int cleanup)
688         struct commit_list *list;
689         struct commit **rslt;
690         struct commit_list *result;
691         int cnt, i, j;
693         result = merge_bases_many(one, n, twos);
694         for (i = 0; i < n; i++) {
695                 if (one == twos[i])
696                         return result;
697         }
698         if (!result || !result->next) {
699                 if (cleanup) {
700                         clear_commit_marks(one, all_flags);
701                         for (i = 0; i < n; i++)
702                                 clear_commit_marks(twos[i], all_flags);
703                 }
704                 return result;
705         }
707         /* There are more than one */
708         cnt = 0;
709         list = result;
710         while (list) {
711                 list = list->next;
712                 cnt++;
713         }
714         rslt = xcalloc(cnt, sizeof(*rslt));
715         for (list = result, i = 0; list; list = list->next)
716                 rslt[i++] = list->item;
717         free_commit_list(result);
719         clear_commit_marks(one, all_flags);
720         for (i = 0; i < n; i++)
721                 clear_commit_marks(twos[i], all_flags);
722         for (i = 0; i < cnt - 1; i++) {
723                 for (j = i+1; j < cnt; j++) {
724                         if (!rslt[i] || !rslt[j])
725                                 continue;
726                         result = merge_bases_many(rslt[i], 1, &rslt[j]);
727                         clear_commit_marks(rslt[i], all_flags);
728                         clear_commit_marks(rslt[j], all_flags);
729                         for (list = result; list; list = list->next) {
730                                 if (rslt[i] == list->item)
731                                         rslt[i] = NULL;
732                                 if (rslt[j] == list->item)
733                                         rslt[j] = NULL;
734                         }
735                 }
736         }
738         /* Surviving ones in rslt[] are the independent results */
739         result = NULL;
740         for (i = 0; i < cnt; i++) {
741                 if (rslt[i])
742                         commit_list_insert_by_date(rslt[i], &result);
743         }
744         free(rslt);
745         return result;
748 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
749                                     int cleanup)
751         return get_merge_bases_many(one, 1, &two, cleanup);
754 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
756         if (!with_commit)
757                 return 1;
758         while (with_commit) {
759                 struct commit *other;
761                 other = with_commit->item;
762                 with_commit = with_commit->next;
763                 if (in_merge_bases(other, &commit, 1))
764                         return 1;
765         }
766         return 0;
769 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
771         struct commit_list *bases, *b;
772         int ret = 0;
774         if (num == 1)
775                 bases = get_merge_bases(commit, *reference, 1);
776         else
777                 die("not yet");
778         for (b = bases; b; b = b->next) {
779                 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
780                         ret = 1;
781                         break;
782                 }
783         }
785         free_commit_list(bases);
786         return ret;
789 struct commit_list *reduce_heads(struct commit_list *heads)
791         struct commit_list *p;
792         struct commit_list *result = NULL, **tail = &result;
793         struct commit **other;
794         size_t num_head, num_other;
796         if (!heads)
797                 return NULL;
799         /* Avoid unnecessary reallocations */
800         for (p = heads, num_head = 0; p; p = p->next)
801                 num_head++;
802         other = xcalloc(sizeof(*other), num_head);
804         /* For each commit, see if it can be reached by others */
805         for (p = heads; p; p = p->next) {
806                 struct commit_list *q, *base;
808                 /* Do we already have this in the result? */
809                 for (q = result; q; q = q->next)
810                         if (p->item == q->item)
811                                 break;
812                 if (q)
813                         continue;
815                 num_other = 0;
816                 for (q = heads; q; q = q->next) {
817                         if (p->item == q->item)
818                                 continue;
819                         other[num_other++] = q->item;
820                 }
821                 if (num_other)
822                         base = get_merge_bases_many(p->item, num_other, other, 1);
823                 else
824                         base = NULL;
825                 /*
826                  * If p->item does not have anything common with other
827                  * commits, there won't be any merge base.  If it is
828                  * reachable from some of the others, p->item will be
829                  * the merge base.  If its history is connected with
830                  * others, but p->item is not reachable by others, we
831                  * will get something other than p->item back.
832                  */
833                 if (!base || (base->item != p->item))
834                         tail = &(commit_list_insert(p->item, tail)->next);
835                 free_commit_list(base);
836         }
837         free(other);
838         return result;
841 static const char commit_utf8_warn[] =
842 "Warning: commit message does not conform to UTF-8.\n"
843 "You may want to amend it after fixing the message, or set the config\n"
844 "variable i18n.commitencoding to the encoding your project uses.\n";
846 int commit_tree(const char *msg, unsigned char *tree,
847                 struct commit_list *parents, unsigned char *ret,
848                 const char *author)
850         int result;
851         int encoding_is_utf8;
852         struct strbuf buffer;
854         assert_sha1_type(tree, OBJ_TREE);
856         /* Not having i18n.commitencoding is the same as having utf-8 */
857         encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
859         strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
860         strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
862         /*
863          * NOTE! This ordering means that the same exact tree merged with a
864          * different order of parents will be a _different_ changeset even
865          * if everything else stays the same.
866          */
867         while (parents) {
868                 struct commit_list *next = parents->next;
869                 strbuf_addf(&buffer, "parent %s\n",
870                         sha1_to_hex(parents->item->object.sha1));
871                 free(parents);
872                 parents = next;
873         }
875         /* Person/date information */
876         if (!author)
877                 author = git_author_info(IDENT_ERROR_ON_NO_NAME);
878         strbuf_addf(&buffer, "author %s\n", author);
879         strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
880         if (!encoding_is_utf8)
881                 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
882         strbuf_addch(&buffer, '\n');
884         /* And add the comment */
885         strbuf_addstr(&buffer, msg);
887         /* And check the encoding */
888         if (encoding_is_utf8 && !is_utf8(buffer.buf))
889                 fprintf(stderr, commit_utf8_warn);
891         result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
892         strbuf_release(&buffer);
893         return result;