index-pack: eliminate recursion in find_unresolved_deltas
[git/jrn.git] / commit.c
blobc7aefbfd2868deddb1dc80a5a110f785b1289b7e
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)
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;
24 return (struct commit *) obj;
27 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
28 int quiet)
30 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
32 if (!obj)
33 return NULL;
34 return check_commit(obj, sha1, quiet);
37 struct commit *lookup_commit_reference(const unsigned char *sha1)
39 return lookup_commit_reference_gently(sha1, 0);
42 struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_name)
44 struct commit *c = lookup_commit_reference(sha1);
45 if (!c)
46 die(_("could not parse %s"), ref_name);
47 if (hashcmp(sha1, c->object.sha1)) {
48 warning(_("%s %s is not a commit!"),
49 ref_name, sha1_to_hex(sha1));
51 return c;
54 struct commit *lookup_commit(const unsigned char *sha1)
56 struct object *obj = lookup_object(sha1);
57 if (!obj)
58 return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
59 if (!obj->type)
60 obj->type = OBJ_COMMIT;
61 return check_commit(obj, sha1, 0);
64 struct commit *lookup_commit_reference_by_name(const char *name)
66 unsigned char sha1[20];
67 struct commit *commit;
69 if (get_sha1(name, sha1))
70 return NULL;
71 commit = lookup_commit_reference(sha1);
72 if (!commit || parse_commit(commit))
73 return NULL;
74 return commit;
77 static unsigned long parse_commit_date(const char *buf, const char *tail)
79 const char *dateptr;
81 if (buf + 6 >= tail)
82 return 0;
83 if (memcmp(buf, "author", 6))
84 return 0;
85 while (buf < tail && *buf++ != '\n')
86 /* nada */;
87 if (buf + 9 >= tail)
88 return 0;
89 if (memcmp(buf, "committer", 9))
90 return 0;
91 while (buf < tail && *buf++ != '>')
92 /* nada */;
93 if (buf >= tail)
94 return 0;
95 dateptr = buf;
96 while (buf < tail && *buf++ != '\n')
97 /* nada */;
98 if (buf >= tail)
99 return 0;
100 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
101 return strtoul(dateptr, NULL, 10);
104 static struct commit_graft **commit_graft;
105 static int commit_graft_alloc, commit_graft_nr;
107 static int commit_graft_pos(const unsigned char *sha1)
109 int lo, hi;
110 lo = 0;
111 hi = commit_graft_nr;
112 while (lo < hi) {
113 int mi = (lo + hi) / 2;
114 struct commit_graft *graft = commit_graft[mi];
115 int cmp = hashcmp(sha1, graft->sha1);
116 if (!cmp)
117 return mi;
118 if (cmp < 0)
119 hi = mi;
120 else
121 lo = mi + 1;
123 return -lo - 1;
126 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
128 int pos = commit_graft_pos(graft->sha1);
130 if (0 <= pos) {
131 if (ignore_dups)
132 free(graft);
133 else {
134 free(commit_graft[pos]);
135 commit_graft[pos] = graft;
137 return 1;
139 pos = -pos - 1;
140 if (commit_graft_alloc <= ++commit_graft_nr) {
141 commit_graft_alloc = alloc_nr(commit_graft_alloc);
142 commit_graft = xrealloc(commit_graft,
143 sizeof(*commit_graft) *
144 commit_graft_alloc);
146 if (pos < commit_graft_nr)
147 memmove(commit_graft + pos + 1,
148 commit_graft + pos,
149 (commit_graft_nr - pos - 1) *
150 sizeof(*commit_graft));
151 commit_graft[pos] = graft;
152 return 0;
155 struct commit_graft *read_graft_line(char *buf, int len)
157 /* The format is just "Commit Parent1 Parent2 ...\n" */
158 int i;
159 struct commit_graft *graft = NULL;
161 while (len && isspace(buf[len-1]))
162 buf[--len] = '\0';
163 if (buf[0] == '#' || buf[0] == '\0')
164 return NULL;
165 if ((len + 1) % 41)
166 goto bad_graft_data;
167 i = (len + 1) / 41 - 1;
168 graft = xmalloc(sizeof(*graft) + 20 * i);
169 graft->nr_parent = i;
170 if (get_sha1_hex(buf, graft->sha1))
171 goto bad_graft_data;
172 for (i = 40; i < len; i += 41) {
173 if (buf[i] != ' ')
174 goto bad_graft_data;
175 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
176 goto bad_graft_data;
178 return graft;
180 bad_graft_data:
181 error("bad graft data: %s", buf);
182 free(graft);
183 return NULL;
186 static int read_graft_file(const char *graft_file)
188 FILE *fp = fopen(graft_file, "r");
189 char buf[1024];
190 if (!fp)
191 return -1;
192 while (fgets(buf, sizeof(buf), fp)) {
193 /* The format is just "Commit Parent1 Parent2 ...\n" */
194 int len = strlen(buf);
195 struct commit_graft *graft = read_graft_line(buf, len);
196 if (!graft)
197 continue;
198 if (register_commit_graft(graft, 1))
199 error("duplicate graft data: %s", buf);
201 fclose(fp);
202 return 0;
205 static void prepare_commit_graft(void)
207 static int commit_graft_prepared;
208 char *graft_file;
210 if (commit_graft_prepared)
211 return;
212 graft_file = get_graft_file();
213 read_graft_file(graft_file);
214 /* make sure shallows are read */
215 is_repository_shallow();
216 commit_graft_prepared = 1;
219 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
221 int pos;
222 prepare_commit_graft();
223 pos = commit_graft_pos(sha1);
224 if (pos < 0)
225 return NULL;
226 return commit_graft[pos];
229 int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
231 int i, ret;
232 for (i = ret = 0; i < commit_graft_nr && !ret; i++)
233 ret = fn(commit_graft[i], cb_data);
234 return ret;
237 int unregister_shallow(const unsigned char *sha1)
239 int pos = commit_graft_pos(sha1);
240 if (pos < 0)
241 return -1;
242 if (pos + 1 < commit_graft_nr)
243 memmove(commit_graft + pos, commit_graft + pos + 1,
244 sizeof(struct commit_graft *)
245 * (commit_graft_nr - pos - 1));
246 commit_graft_nr--;
247 return 0;
250 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
252 const char *tail = buffer;
253 const char *bufptr = buffer;
254 unsigned char parent[20];
255 struct commit_list **pptr;
256 struct commit_graft *graft;
258 if (item->object.parsed)
259 return 0;
260 item->object.parsed = 1;
261 tail += size;
262 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
263 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
264 if (get_sha1_hex(bufptr + 5, parent) < 0)
265 return error("bad tree pointer in commit %s",
266 sha1_to_hex(item->object.sha1));
267 item->tree = lookup_tree(parent);
268 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
269 pptr = &item->parents;
271 graft = lookup_commit_graft(item->object.sha1);
272 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
273 struct commit *new_parent;
275 if (tail <= bufptr + 48 ||
276 get_sha1_hex(bufptr + 7, parent) ||
277 bufptr[47] != '\n')
278 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
279 bufptr += 48;
281 * The clone is shallow if nr_parent < 0, and we must
282 * not traverse its real parents even when we unhide them.
284 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
285 continue;
286 new_parent = lookup_commit(parent);
287 if (new_parent)
288 pptr = &commit_list_insert(new_parent, pptr)->next;
290 if (graft) {
291 int i;
292 struct commit *new_parent;
293 for (i = 0; i < graft->nr_parent; i++) {
294 new_parent = lookup_commit(graft->parent[i]);
295 if (!new_parent)
296 continue;
297 pptr = &commit_list_insert(new_parent, pptr)->next;
300 item->date = parse_commit_date(bufptr, tail);
302 return 0;
305 int parse_commit(struct commit *item)
307 enum object_type type;
308 void *buffer;
309 unsigned long size;
310 int ret;
312 if (!item)
313 return -1;
314 if (item->object.parsed)
315 return 0;
316 buffer = read_sha1_file(item->object.sha1, &type, &size);
317 if (!buffer)
318 return error("Could not read %s",
319 sha1_to_hex(item->object.sha1));
320 if (type != OBJ_COMMIT) {
321 free(buffer);
322 return error("Object %s not a commit",
323 sha1_to_hex(item->object.sha1));
325 ret = parse_commit_buffer(item, buffer, size);
326 if (save_commit_buffer && !ret) {
327 item->buffer = buffer;
328 return 0;
330 free(buffer);
331 return ret;
334 int find_commit_subject(const char *commit_buffer, const char **subject)
336 const char *eol;
337 const char *p = commit_buffer;
339 while (*p && (*p != '\n' || p[1] != '\n'))
340 p++;
341 if (*p) {
342 p += 2;
343 for (eol = p; *eol && *eol != '\n'; eol++)
344 ; /* do nothing */
345 } else
346 eol = p;
348 *subject = p;
350 return eol - p;
353 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
355 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
356 new_list->item = item;
357 new_list->next = *list_p;
358 *list_p = new_list;
359 return new_list;
362 unsigned commit_list_count(const struct commit_list *l)
364 unsigned c = 0;
365 for (; l; l = l->next )
366 c++;
367 return c;
370 void free_commit_list(struct commit_list *list)
372 while (list) {
373 struct commit_list *temp = list;
374 list = temp->next;
375 free(temp);
379 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
381 struct commit_list **pp = list;
382 struct commit_list *p;
383 while ((p = *pp) != NULL) {
384 if (p->item->date < item->date) {
385 break;
387 pp = &p->next;
389 return commit_list_insert(item, pp);
393 void commit_list_sort_by_date(struct commit_list **list)
395 struct commit_list *ret = NULL;
396 while (*list) {
397 commit_list_insert_by_date((*list)->item, &ret);
398 *list = (*list)->next;
400 *list = ret;
403 struct commit *pop_most_recent_commit(struct commit_list **list,
404 unsigned int mark)
406 struct commit *ret = (*list)->item;
407 struct commit_list *parents = ret->parents;
408 struct commit_list *old = *list;
410 *list = (*list)->next;
411 free(old);
413 while (parents) {
414 struct commit *commit = parents->item;
415 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
416 commit->object.flags |= mark;
417 commit_list_insert_by_date(commit, list);
419 parents = parents->next;
421 return ret;
424 static void clear_commit_marks_1(struct commit_list **plist,
425 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 commit_list_insert(parents->item, plist);
442 commit = commit->parents->item;
446 void clear_commit_marks(struct commit *commit, unsigned int mark)
448 struct commit_list *list = NULL;
449 commit_list_insert(commit, &list);
450 while (list)
451 clear_commit_marks_1(&list, pop_commit(&list), mark);
454 void clear_commit_marks_for_object_array(struct object_array *a, unsigned mark)
456 struct object *object;
457 struct commit *commit;
458 unsigned int i;
460 for (i = 0; i < a->nr; i++) {
461 object = a->objects[i].item;
462 commit = lookup_commit_reference_gently(object->sha1, 1);
463 if (commit)
464 clear_commit_marks(commit, mark);
468 struct commit *pop_commit(struct commit_list **stack)
470 struct commit_list *top = *stack;
471 struct commit *item = top ? top->item : NULL;
473 if (top) {
474 *stack = top->next;
475 free(top);
477 return item;
481 * Performs an in-place topological sort on the list supplied.
483 void sort_in_topological_order(struct commit_list ** list, int lifo)
485 struct commit_list *next, *orig = *list;
486 struct commit_list *work, **insert;
487 struct commit_list **pptr;
489 if (!orig)
490 return;
491 *list = NULL;
493 /* Mark them and clear the indegree */
494 for (next = orig; next; next = next->next) {
495 struct commit *commit = next->item;
496 commit->indegree = 1;
499 /* update the indegree */
500 for (next = orig; next; next = next->next) {
501 struct commit_list * parents = next->item->parents;
502 while (parents) {
503 struct commit *parent = parents->item;
505 if (parent->indegree)
506 parent->indegree++;
507 parents = parents->next;
512 * find the tips
514 * tips are nodes not reachable from any other node in the list
516 * the tips serve as a starting set for the work queue.
518 work = NULL;
519 insert = &work;
520 for (next = orig; next; next = next->next) {
521 struct commit *commit = next->item;
523 if (commit->indegree == 1)
524 insert = &commit_list_insert(commit, insert)->next;
527 /* process the list in topological order */
528 if (!lifo)
529 commit_list_sort_by_date(&work);
531 pptr = list;
532 *list = NULL;
533 while (work) {
534 struct commit *commit;
535 struct commit_list *parents, *work_item;
537 work_item = work;
538 work = work_item->next;
539 work_item->next = NULL;
541 commit = work_item->item;
542 for (parents = commit->parents; parents ; parents = parents->next) {
543 struct commit *parent = parents->item;
545 if (!parent->indegree)
546 continue;
549 * parents are only enqueued for emission
550 * when all their children have been emitted thereby
551 * guaranteeing topological order.
553 if (--parent->indegree == 1) {
554 if (!lifo)
555 commit_list_insert_by_date(parent, &work);
556 else
557 commit_list_insert(parent, &work);
561 * work_item is a commit all of whose children
562 * have already been emitted. we can emit it now.
564 commit->indegree = 0;
565 *pptr = work_item;
566 pptr = &work_item->next;
570 /* merge-base stuff */
572 /* bits #0..15 in revision.h */
573 #define PARENT1 (1u<<16)
574 #define PARENT2 (1u<<17)
575 #define STALE (1u<<18)
576 #define RESULT (1u<<19)
578 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
580 static struct commit *interesting(struct commit_list *list)
582 while (list) {
583 struct commit *commit = list->item;
584 list = list->next;
585 if (commit->object.flags & STALE)
586 continue;
587 return commit;
589 return NULL;
592 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
594 struct commit_list *list = NULL;
595 struct commit_list *result = NULL;
596 int i;
598 for (i = 0; i < n; i++) {
599 if (one == twos[i])
601 * We do not mark this even with RESULT so we do not
602 * have to clean it up.
604 return commit_list_insert(one, &result);
607 if (parse_commit(one))
608 return NULL;
609 for (i = 0; i < n; i++) {
610 if (parse_commit(twos[i]))
611 return NULL;
614 one->object.flags |= PARENT1;
615 commit_list_insert_by_date(one, &list);
616 for (i = 0; i < n; i++) {
617 twos[i]->object.flags |= PARENT2;
618 commit_list_insert_by_date(twos[i], &list);
621 while (interesting(list)) {
622 struct commit *commit;
623 struct commit_list *parents;
624 struct commit_list *next;
625 int flags;
627 commit = list->item;
628 next = list->next;
629 free(list);
630 list = next;
632 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
633 if (flags == (PARENT1 | PARENT2)) {
634 if (!(commit->object.flags & RESULT)) {
635 commit->object.flags |= RESULT;
636 commit_list_insert_by_date(commit, &result);
638 /* Mark parents of a found merge stale */
639 flags |= STALE;
641 parents = commit->parents;
642 while (parents) {
643 struct commit *p = parents->item;
644 parents = parents->next;
645 if ((p->object.flags & flags) == flags)
646 continue;
647 if (parse_commit(p))
648 return NULL;
649 p->object.flags |= flags;
650 commit_list_insert_by_date(p, &list);
654 /* Clean up the result to remove stale ones */
655 free_commit_list(list);
656 list = result; result = NULL;
657 while (list) {
658 struct commit_list *next = list->next;
659 if (!(list->item->object.flags & STALE))
660 commit_list_insert_by_date(list->item, &result);
661 free(list);
662 list = next;
664 return result;
667 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
669 struct commit_list *i, *j, *k, *ret = NULL;
670 struct commit_list **pptr = &ret;
672 for (i = in; i; i = i->next) {
673 if (!ret)
674 pptr = &commit_list_insert(i->item, pptr)->next;
675 else {
676 struct commit_list *new = NULL, *end = NULL;
678 for (j = ret; j; j = j->next) {
679 struct commit_list *bases;
680 bases = get_merge_bases(i->item, j->item, 1);
681 if (!new)
682 new = bases;
683 else
684 end->next = bases;
685 for (k = bases; k; k = k->next)
686 end = k;
688 ret = new;
691 return ret;
694 struct commit_list *get_merge_bases_many(struct commit *one,
695 int n,
696 struct commit **twos,
697 int cleanup)
699 struct commit_list *list;
700 struct commit **rslt;
701 struct commit_list *result;
702 int cnt, i, j;
704 result = merge_bases_many(one, n, twos);
705 for (i = 0; i < n; i++) {
706 if (one == twos[i])
707 return result;
709 if (!result || !result->next) {
710 if (cleanup) {
711 clear_commit_marks(one, all_flags);
712 for (i = 0; i < n; i++)
713 clear_commit_marks(twos[i], all_flags);
715 return result;
718 /* There are more than one */
719 cnt = 0;
720 list = result;
721 while (list) {
722 list = list->next;
723 cnt++;
725 rslt = xcalloc(cnt, sizeof(*rslt));
726 for (list = result, i = 0; list; list = list->next)
727 rslt[i++] = list->item;
728 free_commit_list(result);
730 clear_commit_marks(one, all_flags);
731 for (i = 0; i < n; i++)
732 clear_commit_marks(twos[i], all_flags);
733 for (i = 0; i < cnt - 1; i++) {
734 for (j = i+1; j < cnt; j++) {
735 if (!rslt[i] || !rslt[j])
736 continue;
737 result = merge_bases_many(rslt[i], 1, &rslt[j]);
738 clear_commit_marks(rslt[i], all_flags);
739 clear_commit_marks(rslt[j], all_flags);
740 for (list = result; list; list = list->next) {
741 if (rslt[i] == list->item)
742 rslt[i] = NULL;
743 if (rslt[j] == list->item)
744 rslt[j] = NULL;
749 /* Surviving ones in rslt[] are the independent results */
750 result = NULL;
751 for (i = 0; i < cnt; i++) {
752 if (rslt[i])
753 commit_list_insert_by_date(rslt[i], &result);
755 free(rslt);
756 return result;
759 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
760 int cleanup)
762 return get_merge_bases_many(one, 1, &two, cleanup);
765 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
767 if (!with_commit)
768 return 1;
769 while (with_commit) {
770 struct commit *other;
772 other = with_commit->item;
773 with_commit = with_commit->next;
774 if (in_merge_bases(other, &commit, 1))
775 return 1;
777 return 0;
780 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
782 struct commit_list *bases, *b;
783 int ret = 0;
785 if (num == 1)
786 bases = get_merge_bases(commit, *reference, 1);
787 else
788 die("not yet");
789 for (b = bases; b; b = b->next) {
790 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
791 ret = 1;
792 break;
796 free_commit_list(bases);
797 return ret;
800 struct commit_list *reduce_heads(struct commit_list *heads)
802 struct commit_list *p;
803 struct commit_list *result = NULL, **tail = &result;
804 struct commit **other;
805 size_t num_head, num_other;
807 if (!heads)
808 return NULL;
810 /* Avoid unnecessary reallocations */
811 for (p = heads, num_head = 0; p; p = p->next)
812 num_head++;
813 other = xcalloc(sizeof(*other), num_head);
815 /* For each commit, see if it can be reached by others */
816 for (p = heads; p; p = p->next) {
817 struct commit_list *q, *base;
819 /* Do we already have this in the result? */
820 for (q = result; q; q = q->next)
821 if (p->item == q->item)
822 break;
823 if (q)
824 continue;
826 num_other = 0;
827 for (q = heads; q; q = q->next) {
828 if (p->item == q->item)
829 continue;
830 other[num_other++] = q->item;
832 if (num_other)
833 base = get_merge_bases_many(p->item, num_other, other, 1);
834 else
835 base = NULL;
837 * If p->item does not have anything common with other
838 * commits, there won't be any merge base. If it is
839 * reachable from some of the others, p->item will be
840 * the merge base. If its history is connected with
841 * others, but p->item is not reachable by others, we
842 * will get something other than p->item back.
844 if (!base || (base->item != p->item))
845 tail = &(commit_list_insert(p->item, tail)->next);
846 free_commit_list(base);
848 free(other);
849 return result;
852 static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
854 struct merge_remote_desc *desc;
855 struct commit_extra_header *mergetag;
856 char *buf;
857 unsigned long size, len;
858 enum object_type type;
860 desc = merge_remote_util(parent);
861 if (!desc || !desc->obj)
862 return;
863 buf = read_sha1_file(desc->obj->sha1, &type, &size);
864 if (!buf || type != OBJ_TAG)
865 goto free_return;
866 len = parse_signature(buf, size);
867 if (size == len)
868 goto free_return;
870 * We could verify this signature and either omit the tag when
871 * it does not validate, but the integrator may not have the
872 * public key of the signer of the tag he is merging, while a
873 * later auditor may have it while auditing, so let's not run
874 * verify-signed-buffer here for now...
876 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
877 * warn("warning: signed tag unverified.");
879 mergetag = xcalloc(1, sizeof(*mergetag));
880 mergetag->key = xstrdup("mergetag");
881 mergetag->value = buf;
882 mergetag->len = size;
884 **tail = mergetag;
885 *tail = &mergetag->next;
886 return;
888 free_return:
889 free(buf);
892 void append_merge_tag_headers(struct commit_list *parents,
893 struct commit_extra_header ***tail)
895 while (parents) {
896 struct commit *parent = parents->item;
897 handle_signed_tag(parent, tail);
898 parents = parents->next;
902 static void add_extra_header(struct strbuf *buffer,
903 struct commit_extra_header *extra)
905 strbuf_addstr(buffer, extra->key);
906 if (extra->len)
907 strbuf_add_lines(buffer, " ", extra->value, extra->len);
908 else
909 strbuf_addch(buffer, '\n');
912 struct commit_extra_header *read_commit_extra_headers(struct commit *commit)
914 struct commit_extra_header *extra = NULL;
915 unsigned long size;
916 enum object_type type;
917 char *buffer = read_sha1_file(commit->object.sha1, &type, &size);
918 if (buffer && type == OBJ_COMMIT)
919 extra = read_commit_extra_header_lines(buffer, size);
920 free(buffer);
921 return extra;
924 static inline int standard_header_field(const char *field, size_t len)
926 return ((len == 4 && !memcmp(field, "tree ", 5)) ||
927 (len == 6 && !memcmp(field, "parent ", 7)) ||
928 (len == 6 && !memcmp(field, "author ", 7)) ||
929 (len == 9 && !memcmp(field, "committer ", 10)) ||
930 (len == 8 && !memcmp(field, "encoding ", 9)));
933 struct commit_extra_header *read_commit_extra_header_lines(const char *buffer, size_t size)
935 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
936 const char *line, *next, *eof, *eob;
937 struct strbuf buf = STRBUF_INIT;
939 for (line = buffer, eob = line + size;
940 line < eob && *line != '\n';
941 line = next) {
942 next = memchr(line, '\n', eob - line);
943 next = next ? next + 1 : eob;
944 if (*line == ' ') {
945 /* continuation */
946 if (it)
947 strbuf_add(&buf, line + 1, next - (line + 1));
948 continue;
950 if (it)
951 it->value = strbuf_detach(&buf, &it->len);
952 strbuf_reset(&buf);
953 it = NULL;
955 eof = strchr(line, ' ');
956 if (next <= eof)
957 eof = next;
959 if (standard_header_field(line, eof - line))
960 continue;
962 it = xcalloc(1, sizeof(*it));
963 it->key = xmemdupz(line, eof-line);
964 *tail = it;
965 tail = &it->next;
966 if (eof + 1 < next)
967 strbuf_add(&buf, eof + 1, next - (eof + 1));
969 if (it)
970 it->value = strbuf_detach(&buf, &it->len);
971 return extra;
974 void free_commit_extra_headers(struct commit_extra_header *extra)
976 while (extra) {
977 struct commit_extra_header *next = extra->next;
978 free(extra->key);
979 free(extra->value);
980 free(extra);
981 extra = next;
985 int commit_tree(const struct strbuf *msg, unsigned char *tree,
986 struct commit_list *parents, unsigned char *ret,
987 const char *author)
989 struct commit_extra_header *extra = NULL, **tail = &extra;
990 int result;
992 append_merge_tag_headers(parents, &tail);
993 result = commit_tree_extended(msg, tree, parents, ret, author, extra);
994 free_commit_extra_headers(extra);
995 return result;
998 static const char commit_utf8_warn[] =
999 "Warning: commit message does not conform to UTF-8.\n"
1000 "You may want to amend it after fixing the message, or set the config\n"
1001 "variable i18n.commitencoding to the encoding your project uses.\n";
1003 int commit_tree_extended(const struct strbuf *msg, unsigned char *tree,
1004 struct commit_list *parents, unsigned char *ret,
1005 const char *author, struct commit_extra_header *extra)
1007 int result;
1008 int encoding_is_utf8;
1009 struct strbuf buffer;
1011 assert_sha1_type(tree, OBJ_TREE);
1013 if (memchr(msg->buf, '\0', msg->len))
1014 return error("a NUL byte in commit log message not allowed.");
1016 /* Not having i18n.commitencoding is the same as having utf-8 */
1017 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1019 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1020 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
1023 * NOTE! This ordering means that the same exact tree merged with a
1024 * different order of parents will be a _different_ changeset even
1025 * if everything else stays the same.
1027 while (parents) {
1028 struct commit_list *next = parents->next;
1029 struct commit *parent = parents->item;
1031 strbuf_addf(&buffer, "parent %s\n",
1032 sha1_to_hex(parent->object.sha1));
1033 free(parents);
1034 parents = next;
1037 /* Person/date information */
1038 if (!author)
1039 author = git_author_info(IDENT_ERROR_ON_NO_NAME);
1040 strbuf_addf(&buffer, "author %s\n", author);
1041 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
1042 if (!encoding_is_utf8)
1043 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
1045 while (extra) {
1046 add_extra_header(&buffer, extra);
1047 extra = extra->next;
1049 strbuf_addch(&buffer, '\n');
1051 /* And add the comment */
1052 strbuf_addbuf(&buffer, msg);
1054 /* And check the encoding */
1055 if (encoding_is_utf8 && !is_utf8(buffer.buf))
1056 fprintf(stderr, commit_utf8_warn);
1058 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
1059 strbuf_release(&buffer);
1060 return result;
1063 struct commit *get_merge_parent(const char *name)
1065 struct object *obj;
1066 struct commit *commit;
1067 unsigned char sha1[20];
1068 if (get_sha1(name, sha1))
1069 return NULL;
1070 obj = parse_object(sha1);
1071 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1072 if (commit && !commit->util) {
1073 struct merge_remote_desc *desc;
1074 desc = xmalloc(sizeof(*desc));
1075 desc->obj = obj;
1076 desc->name = strdup(name);
1077 commit->util = desc;
1079 return commit;