criss cross rename failure workaround
[git/dscho.git] / commit.c
blob2792c47adc2f0c65c716b6f88a1195adc9657a4b
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 #include "gpg-interface.h"
11 int save_commit_buffer = 1;
13 const char *commit_type = "commit";
15 static struct commit *check_commit(struct object *obj,
16 const unsigned char *sha1,
17 int quiet)
19 if (obj->type != OBJ_COMMIT) {
20 if (!quiet)
21 error("Object %s is a %s, not a commit",
22 sha1_to_hex(sha1), typename(obj->type));
23 return NULL;
25 return (struct commit *) obj;
28 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
29 int quiet)
31 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
33 if (!obj)
34 return NULL;
35 return check_commit(obj, sha1, quiet);
38 struct commit *lookup_commit_reference(const unsigned char *sha1)
40 return lookup_commit_reference_gently(sha1, 0);
43 struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_name)
45 struct commit *c = lookup_commit_reference(sha1);
46 if (!c)
47 die(_("could not parse %s"), ref_name);
48 if (hashcmp(sha1, c->object.sha1)) {
49 warning(_("%s %s is not a commit!"),
50 ref_name, sha1_to_hex(sha1));
52 return c;
55 struct commit *lookup_commit(const unsigned char *sha1)
57 struct object *obj = lookup_object(sha1);
58 if (!obj)
59 return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
60 if (!obj->type)
61 obj->type = OBJ_COMMIT;
62 return check_commit(obj, sha1, 0);
65 struct commit *lookup_commit_reference_by_name(const char *name)
67 unsigned char sha1[20];
68 struct commit *commit;
70 if (get_sha1(name, sha1))
71 return NULL;
72 commit = lookup_commit_reference(sha1);
73 if (!commit || parse_commit(commit))
74 return NULL;
75 return commit;
78 static unsigned long parse_commit_date(const char *buf, const char *tail)
80 const char *dateptr;
82 if (buf + 6 >= tail)
83 return 0;
84 if (memcmp(buf, "author", 6))
85 return 0;
86 while (buf < tail && *buf++ != '\n')
87 /* nada */;
88 if (buf + 9 >= tail)
89 return 0;
90 if (memcmp(buf, "committer", 9))
91 return 0;
92 while (buf < tail && *buf++ != '>')
93 /* nada */;
94 if (buf >= tail)
95 return 0;
96 dateptr = buf;
97 while (buf < tail && *buf++ != '\n')
98 /* nada */;
99 if (buf >= tail)
100 return 0;
101 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
102 return strtoul(dateptr, NULL, 10);
105 static struct commit_graft **commit_graft;
106 static int commit_graft_alloc, commit_graft_nr;
108 static int commit_graft_pos(const unsigned char *sha1)
110 int lo, hi;
111 lo = 0;
112 hi = commit_graft_nr;
113 while (lo < hi) {
114 int mi = (lo + hi) / 2;
115 struct commit_graft *graft = commit_graft[mi];
116 int cmp = hashcmp(sha1, graft->sha1);
117 if (!cmp)
118 return mi;
119 if (cmp < 0)
120 hi = mi;
121 else
122 lo = mi + 1;
124 return -lo - 1;
127 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
129 int pos = commit_graft_pos(graft->sha1);
131 if (0 <= pos) {
132 if (ignore_dups)
133 free(graft);
134 else {
135 free(commit_graft[pos]);
136 commit_graft[pos] = graft;
138 return 1;
140 pos = -pos - 1;
141 if (commit_graft_alloc <= ++commit_graft_nr) {
142 commit_graft_alloc = alloc_nr(commit_graft_alloc);
143 commit_graft = xrealloc(commit_graft,
144 sizeof(*commit_graft) *
145 commit_graft_alloc);
147 if (pos < commit_graft_nr)
148 memmove(commit_graft + pos + 1,
149 commit_graft + pos,
150 (commit_graft_nr - pos - 1) *
151 sizeof(*commit_graft));
152 commit_graft[pos] = graft;
153 return 0;
156 struct commit_graft *read_graft_line(char *buf, int len)
158 /* The format is just "Commit Parent1 Parent2 ...\n" */
159 int i;
160 struct commit_graft *graft = NULL;
162 while (len && isspace(buf[len-1]))
163 buf[--len] = '\0';
164 if (buf[0] == '#' || buf[0] == '\0')
165 return NULL;
166 if ((len + 1) % 41)
167 goto bad_graft_data;
168 i = (len + 1) / 41 - 1;
169 graft = xmalloc(sizeof(*graft) + 20 * i);
170 graft->nr_parent = i;
171 if (get_sha1_hex(buf, graft->sha1))
172 goto bad_graft_data;
173 for (i = 40; i < len; i += 41) {
174 if (buf[i] != ' ')
175 goto bad_graft_data;
176 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
177 goto bad_graft_data;
179 return graft;
181 bad_graft_data:
182 error("bad graft data: %s", buf);
183 free(graft);
184 return NULL;
187 static int read_graft_file(const char *graft_file)
189 FILE *fp = fopen(graft_file, "r");
190 char buf[1024];
191 if (!fp)
192 return -1;
193 while (fgets(buf, sizeof(buf), fp)) {
194 /* The format is just "Commit Parent1 Parent2 ...\n" */
195 int len = strlen(buf);
196 struct commit_graft *graft = read_graft_line(buf, len);
197 if (!graft)
198 continue;
199 if (register_commit_graft(graft, 1))
200 error("duplicate graft data: %s", buf);
202 fclose(fp);
203 return 0;
206 static void prepare_commit_graft(void)
208 static int commit_graft_prepared;
209 char *graft_file;
211 if (commit_graft_prepared)
212 return;
213 graft_file = get_graft_file();
214 read_graft_file(graft_file);
215 /* make sure shallows are read */
216 is_repository_shallow();
217 commit_graft_prepared = 1;
220 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
222 int pos;
223 prepare_commit_graft();
224 pos = commit_graft_pos(sha1);
225 if (pos < 0)
226 return NULL;
227 return commit_graft[pos];
230 int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
232 int i, ret;
233 for (i = ret = 0; i < commit_graft_nr && !ret; i++)
234 ret = fn(commit_graft[i], cb_data);
235 return ret;
238 int unregister_shallow(const unsigned char *sha1)
240 int pos = commit_graft_pos(sha1);
241 if (pos < 0)
242 return -1;
243 if (pos + 1 < commit_graft_nr)
244 memmove(commit_graft + pos, commit_graft + pos + 1,
245 sizeof(struct commit_graft *)
246 * (commit_graft_nr - pos - 1));
247 commit_graft_nr--;
248 return 0;
251 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
253 const char *tail = buffer;
254 const char *bufptr = buffer;
255 unsigned char parent[20];
256 struct commit_list **pptr;
257 struct commit_graft *graft;
259 if (item->object.parsed)
260 return 0;
261 item->object.parsed = 1;
262 tail += size;
263 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
264 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
265 if (get_sha1_hex(bufptr + 5, parent) < 0)
266 return error("bad tree pointer in commit %s",
267 sha1_to_hex(item->object.sha1));
268 item->tree = lookup_tree(parent);
269 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
270 pptr = &item->parents;
272 graft = lookup_commit_graft(item->object.sha1);
273 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
274 struct commit *new_parent;
276 if (tail <= bufptr + 48 ||
277 get_sha1_hex(bufptr + 7, parent) ||
278 bufptr[47] != '\n')
279 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
280 bufptr += 48;
282 * The clone is shallow if nr_parent < 0, and we must
283 * not traverse its real parents even when we unhide them.
285 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
286 continue;
287 new_parent = lookup_commit(parent);
288 if (new_parent)
289 pptr = &commit_list_insert(new_parent, pptr)->next;
291 if (graft) {
292 int i;
293 struct commit *new_parent;
294 for (i = 0; i < graft->nr_parent; i++) {
295 new_parent = lookup_commit(graft->parent[i]);
296 if (!new_parent)
297 continue;
298 pptr = &commit_list_insert(new_parent, pptr)->next;
301 item->date = parse_commit_date(bufptr, tail);
303 return 0;
306 int parse_commit(struct commit *item)
308 enum object_type type;
309 void *buffer;
310 unsigned long size;
311 int ret;
313 if (!item)
314 return -1;
315 if (item->object.parsed)
316 return 0;
317 buffer = read_sha1_file(item->object.sha1, &type, &size);
318 if (!buffer)
319 return error("Could not read %s",
320 sha1_to_hex(item->object.sha1));
321 if (type != OBJ_COMMIT) {
322 free(buffer);
323 return error("Object %s not a commit",
324 sha1_to_hex(item->object.sha1));
326 ret = parse_commit_buffer(item, buffer, size);
327 if (save_commit_buffer && !ret) {
328 item->buffer = buffer;
329 return 0;
331 free(buffer);
332 return ret;
335 int find_commit_subject(const char *commit_buffer, const char **subject)
337 const char *eol;
338 const char *p = commit_buffer;
340 while (*p && (*p != '\n' || p[1] != '\n'))
341 p++;
342 if (*p) {
343 p += 2;
344 for (eol = p; *eol && *eol != '\n'; eol++)
345 ; /* do nothing */
346 } else
347 eol = p;
349 *subject = p;
351 return eol - p;
354 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
356 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
357 new_list->item = item;
358 new_list->next = *list_p;
359 *list_p = new_list;
360 return new_list;
363 unsigned commit_list_count(const struct commit_list *l)
365 unsigned c = 0;
366 for (; l; l = l->next )
367 c++;
368 return c;
371 void free_commit_list(struct commit_list *list)
373 while (list) {
374 struct commit_list *temp = list;
375 list = temp->next;
376 free(temp);
380 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
382 struct commit_list **pp = list;
383 struct commit_list *p;
384 while ((p = *pp) != NULL) {
385 if (p->item->date < item->date) {
386 break;
388 pp = &p->next;
390 return commit_list_insert(item, pp);
394 void commit_list_sort_by_date(struct commit_list **list)
396 struct commit_list *ret = NULL;
397 while (*list) {
398 commit_list_insert_by_date((*list)->item, &ret);
399 *list = (*list)->next;
401 *list = ret;
404 struct commit *pop_most_recent_commit(struct commit_list **list,
405 unsigned int mark)
407 struct commit *ret = (*list)->item;
408 struct commit_list *parents = ret->parents;
409 struct commit_list *old = *list;
411 *list = (*list)->next;
412 free(old);
414 while (parents) {
415 struct commit *commit = parents->item;
416 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
417 commit->object.flags |= mark;
418 commit_list_insert_by_date(commit, list);
420 parents = parents->next;
422 return ret;
425 void clear_commit_marks(struct commit *commit, unsigned int mark)
427 while (commit) {
428 struct commit_list *parents;
430 if (!(mark & commit->object.flags))
431 return;
433 commit->object.flags &= ~mark;
435 parents = commit->parents;
436 if (!parents)
437 return;
439 while ((parents = parents->next))
440 clear_commit_marks(parents->item, mark);
442 commit = commit->parents->item;
446 void clear_commit_marks_for_object_array(struct object_array *a, unsigned mark)
448 struct object *object;
449 struct commit *commit;
450 unsigned int i;
452 for (i = 0; i < a->nr; i++) {
453 object = a->objects[i].item;
454 commit = lookup_commit_reference_gently(object->sha1, 1);
455 if (commit)
456 clear_commit_marks(commit, mark);
460 struct commit *pop_commit(struct commit_list **stack)
462 struct commit_list *top = *stack;
463 struct commit *item = top ? top->item : NULL;
465 if (top) {
466 *stack = top->next;
467 free(top);
469 return item;
473 * Performs an in-place topological sort on the list supplied.
475 void sort_in_topological_order(struct commit_list ** list, int lifo)
477 struct commit_list *next, *orig = *list;
478 struct commit_list *work, **insert;
479 struct commit_list **pptr;
481 if (!orig)
482 return;
483 *list = NULL;
485 /* Mark them and clear the indegree */
486 for (next = orig; next; next = next->next) {
487 struct commit *commit = next->item;
488 commit->indegree = 1;
491 /* update the indegree */
492 for (next = orig; next; next = next->next) {
493 struct commit_list * parents = next->item->parents;
494 while (parents) {
495 struct commit *parent = parents->item;
497 if (parent->indegree)
498 parent->indegree++;
499 parents = parents->next;
504 * find the tips
506 * tips are nodes not reachable from any other node in the list
508 * the tips serve as a starting set for the work queue.
510 work = NULL;
511 insert = &work;
512 for (next = orig; next; next = next->next) {
513 struct commit *commit = next->item;
515 if (commit->indegree == 1)
516 insert = &commit_list_insert(commit, insert)->next;
519 /* process the list in topological order */
520 if (!lifo)
521 commit_list_sort_by_date(&work);
523 pptr = list;
524 *list = NULL;
525 while (work) {
526 struct commit *commit;
527 struct commit_list *parents, *work_item;
529 work_item = work;
530 work = work_item->next;
531 work_item->next = NULL;
533 commit = work_item->item;
534 for (parents = commit->parents; parents ; parents = parents->next) {
535 struct commit *parent = parents->item;
537 if (!parent->indegree)
538 continue;
541 * parents are only enqueued for emission
542 * when all their children have been emitted thereby
543 * guaranteeing topological order.
545 if (--parent->indegree == 1) {
546 if (!lifo)
547 commit_list_insert_by_date(parent, &work);
548 else
549 commit_list_insert(parent, &work);
553 * work_item is a commit all of whose children
554 * have already been emitted. we can emit it now.
556 commit->indegree = 0;
557 *pptr = work_item;
558 pptr = &work_item->next;
562 /* merge-base stuff */
564 /* bits #0..15 in revision.h */
565 #define PARENT1 (1u<<16)
566 #define PARENT2 (1u<<17)
567 #define STALE (1u<<18)
568 #define RESULT (1u<<19)
570 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
572 static struct commit *interesting(struct commit_list *list)
574 while (list) {
575 struct commit *commit = list->item;
576 list = list->next;
577 if (commit->object.flags & STALE)
578 continue;
579 return commit;
581 return NULL;
584 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
586 struct commit_list *list = NULL;
587 struct commit_list *result = NULL;
588 int i;
590 for (i = 0; i < n; i++) {
591 if (one == twos[i])
593 * We do not mark this even with RESULT so we do not
594 * have to clean it up.
596 return commit_list_insert(one, &result);
599 if (parse_commit(one))
600 return NULL;
601 for (i = 0; i < n; i++) {
602 if (parse_commit(twos[i]))
603 return NULL;
606 one->object.flags |= PARENT1;
607 commit_list_insert_by_date(one, &list);
608 for (i = 0; i < n; i++) {
609 twos[i]->object.flags |= PARENT2;
610 commit_list_insert_by_date(twos[i], &list);
613 while (interesting(list)) {
614 struct commit *commit;
615 struct commit_list *parents;
616 struct commit_list *next;
617 int flags;
619 commit = list->item;
620 next = list->next;
621 free(list);
622 list = next;
624 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
625 if (flags == (PARENT1 | PARENT2)) {
626 if (!(commit->object.flags & RESULT)) {
627 commit->object.flags |= RESULT;
628 commit_list_insert_by_date(commit, &result);
630 /* Mark parents of a found merge stale */
631 flags |= STALE;
633 parents = commit->parents;
634 while (parents) {
635 struct commit *p = parents->item;
636 parents = parents->next;
637 if ((p->object.flags & flags) == flags)
638 continue;
639 if (parse_commit(p))
640 return NULL;
641 p->object.flags |= flags;
642 commit_list_insert_by_date(p, &list);
646 /* Clean up the result to remove stale ones */
647 free_commit_list(list);
648 list = result; result = NULL;
649 while (list) {
650 struct commit_list *next = list->next;
651 if (!(list->item->object.flags & STALE))
652 commit_list_insert_by_date(list->item, &result);
653 free(list);
654 list = next;
656 return result;
659 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
661 struct commit_list *i, *j, *k, *ret = NULL;
662 struct commit_list **pptr = &ret;
664 for (i = in; i; i = i->next) {
665 if (!ret)
666 pptr = &commit_list_insert(i->item, pptr)->next;
667 else {
668 struct commit_list *new = NULL, *end = NULL;
670 for (j = ret; j; j = j->next) {
671 struct commit_list *bases;
672 bases = get_merge_bases(i->item, j->item, 1);
673 if (!new)
674 new = bases;
675 else
676 end->next = bases;
677 for (k = bases; k; k = k->next)
678 end = k;
680 ret = new;
683 return ret;
686 struct commit_list *get_merge_bases_many(struct commit *one,
687 int n,
688 struct commit **twos,
689 int cleanup)
691 struct commit_list *list;
692 struct commit **rslt;
693 struct commit_list *result;
694 int cnt, i, j;
696 result = merge_bases_many(one, n, twos);
697 for (i = 0; i < n; i++) {
698 if (one == twos[i])
699 return result;
701 if (!result || !result->next) {
702 if (cleanup) {
703 clear_commit_marks(one, all_flags);
704 for (i = 0; i < n; i++)
705 clear_commit_marks(twos[i], all_flags);
707 return result;
710 /* There are more than one */
711 cnt = 0;
712 list = result;
713 while (list) {
714 list = list->next;
715 cnt++;
717 rslt = xcalloc(cnt, sizeof(*rslt));
718 for (list = result, i = 0; list; list = list->next)
719 rslt[i++] = list->item;
720 free_commit_list(result);
722 clear_commit_marks(one, all_flags);
723 for (i = 0; i < n; i++)
724 clear_commit_marks(twos[i], all_flags);
725 for (i = 0; i < cnt - 1; i++) {
726 for (j = i+1; j < cnt; j++) {
727 if (!rslt[i] || !rslt[j])
728 continue;
729 result = merge_bases_many(rslt[i], 1, &rslt[j]);
730 clear_commit_marks(rslt[i], all_flags);
731 clear_commit_marks(rslt[j], all_flags);
732 for (list = result; list; list = list->next) {
733 if (rslt[i] == list->item)
734 rslt[i] = NULL;
735 if (rslt[j] == list->item)
736 rslt[j] = NULL;
741 /* Surviving ones in rslt[] are the independent results */
742 result = NULL;
743 for (i = 0; i < cnt; i++) {
744 if (rslt[i])
745 commit_list_insert_by_date(rslt[i], &result);
747 free(rslt);
748 return result;
751 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
752 int cleanup)
754 return get_merge_bases_many(one, 1, &two, cleanup);
757 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
759 if (!with_commit)
760 return 1;
761 while (with_commit) {
762 struct commit *other;
764 other = with_commit->item;
765 with_commit = with_commit->next;
766 if (in_merge_bases(other, &commit, 1))
767 return 1;
769 return 0;
772 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
774 struct commit_list *bases, *b;
775 int ret = 0;
777 if (num == 1)
778 bases = get_merge_bases(commit, *reference, 1);
779 else
780 die("not yet");
781 for (b = bases; b; b = b->next) {
782 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
783 ret = 1;
784 break;
788 free_commit_list(bases);
789 return ret;
792 struct commit_list *reduce_heads(struct commit_list *heads)
794 struct commit_list *p;
795 struct commit_list *result = NULL, **tail = &result;
796 struct commit **other;
797 size_t num_head, num_other;
799 if (!heads)
800 return NULL;
802 /* Avoid unnecessary reallocations */
803 for (p = heads, num_head = 0; p; p = p->next)
804 num_head++;
805 other = xcalloc(sizeof(*other), num_head);
807 /* For each commit, see if it can be reached by others */
808 for (p = heads; p; p = p->next) {
809 struct commit_list *q, *base;
811 /* Do we already have this in the result? */
812 for (q = result; q; q = q->next)
813 if (p->item == q->item)
814 break;
815 if (q)
816 continue;
818 num_other = 0;
819 for (q = heads; q; q = q->next) {
820 if (p->item == q->item)
821 continue;
822 other[num_other++] = q->item;
824 if (num_other)
825 base = get_merge_bases_many(p->item, num_other, other, 1);
826 else
827 base = NULL;
829 * If p->item does not have anything common with other
830 * commits, there won't be any merge base. If it is
831 * reachable from some of the others, p->item will be
832 * the merge base. If its history is connected with
833 * others, but p->item is not reachable by others, we
834 * will get something other than p->item back.
836 if (!base || (base->item != p->item))
837 tail = &(commit_list_insert(p->item, tail)->next);
838 free_commit_list(base);
840 free(other);
841 return result;
844 static const char gpg_sig_header[] = "gpgsig ";
845 static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
847 static int do_sign_commit(struct strbuf *buf, const char *keyid)
849 struct strbuf sig = STRBUF_INIT;
850 int inspos, copypos;
852 /* find the end of the header */
853 inspos = strstr(buf->buf, "\n\n") - buf->buf + 1;
855 if (!keyid || !*keyid)
856 keyid = get_signing_key();
857 if (sign_buffer(buf, &sig, keyid)) {
858 strbuf_release(&sig);
859 return -1;
862 for (copypos = 0; sig.buf[copypos]; ) {
863 const char *bol = sig.buf + copypos;
864 const char *eol = strchrnul(bol, '\n');
865 int len = (eol - bol) + !!*eol;
867 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
868 inspos += gpg_sig_header_len;
869 strbuf_insert(buf, inspos, bol, len);
870 inspos += len;
871 copypos += len;
873 strbuf_release(&sig);
874 return 0;
877 int parse_signed_commit(const unsigned char *sha1,
878 struct strbuf *payload, struct strbuf *signature)
880 unsigned long size;
881 enum object_type type;
882 char *buffer = read_sha1_file(sha1, &type, &size);
883 int saw_signature = -1;
884 char *line, *tail;
886 if (!buffer || type != OBJ_COMMIT)
887 goto cleanup;
889 line = buffer;
890 tail = buffer + size;
891 saw_signature = 0;
892 while (line < tail) {
893 char *next = memchr(line, '\n', tail - line);
894 if (!next)
895 next = tail;
896 else
897 next++;
898 if (!prefixcmp(line, gpg_sig_header)) {
899 const char *sig = line + gpg_sig_header_len;
900 strbuf_add(signature, sig, next - sig);
901 saw_signature = 1;
902 } else {
903 if (*line == '\n')
904 /* dump the whole remainder of the buffer */
905 next = tail;
906 strbuf_add(payload, line, next - line);
908 line = next;
910 cleanup:
911 free(buffer);
912 return saw_signature;
915 static const char commit_utf8_warn[] =
916 "Warning: commit message does not conform to UTF-8.\n"
917 "You may want to amend it after fixing the message, or set the config\n"
918 "variable i18n.commitencoding to the encoding your project uses.\n";
920 int commit_tree(const char *msg, unsigned char *tree,
921 struct commit_list *parents, unsigned char *ret,
922 const char *author, const char *sign_commit)
924 int result;
925 int encoding_is_utf8;
926 struct strbuf buffer;
928 assert_sha1_type(tree, OBJ_TREE);
930 /* Not having i18n.commitencoding is the same as having utf-8 */
931 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
933 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
934 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
937 * NOTE! This ordering means that the same exact tree merged with a
938 * different order of parents will be a _different_ changeset even
939 * if everything else stays the same.
941 while (parents) {
942 struct commit_list *next = parents->next;
943 strbuf_addf(&buffer, "parent %s\n",
944 sha1_to_hex(parents->item->object.sha1));
945 free(parents);
946 parents = next;
949 /* Person/date information */
950 if (!author)
951 author = git_author_info(IDENT_ERROR_ON_NO_NAME);
952 strbuf_addf(&buffer, "author %s\n", author);
953 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
954 if (!encoding_is_utf8)
955 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
956 strbuf_addch(&buffer, '\n');
958 /* And add the comment */
959 strbuf_addstr(&buffer, msg);
961 /* And check the encoding */
962 if (encoding_is_utf8 && !is_utf8(buffer.buf))
963 fprintf(stderr, commit_utf8_warn);
965 if (sign_commit && do_sign_commit(&buffer, sign_commit))
966 return -1;
968 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
969 strbuf_release(&buffer);
970 return result;