9 #include "gpg-interface.h"
10 #include "mergesort.h"
11 #include "commit-slab.h"
13 static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
15 int save_commit_buffer = 1;
17 const char *commit_type = "commit";
18 static int commit_count;
20 static struct commit *check_commit(struct object *obj,
21 const unsigned char *sha1,
24 if (obj->type != OBJ_COMMIT) {
26 error("Object %s is a %s, not a commit",
27 sha1_to_hex(sha1), typename(obj->type));
30 return (struct commit *) obj;
33 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
36 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
40 return check_commit(obj, sha1, quiet);
43 struct commit *lookup_commit_reference(const unsigned char *sha1)
45 return lookup_commit_reference_gently(sha1, 0);
48 struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_name)
50 struct commit *c = lookup_commit_reference(sha1);
52 die(_("could not parse %s"), ref_name);
53 if (hashcmp(sha1, c->object.sha1)) {
54 warning(_("%s %s is not a commit!"),
55 ref_name, sha1_to_hex(sha1));
60 struct commit *lookup_commit(const unsigned char *sha1)
62 struct object *obj = lookup_object(sha1);
64 struct commit *c = alloc_commit_node();
65 c->index = commit_count++;
66 return create_object(sha1, OBJ_COMMIT, c);
69 obj->type = OBJ_COMMIT;
70 return check_commit(obj, sha1, 0);
73 struct commit *lookup_commit_reference_by_name(const char *name)
75 unsigned char sha1[20];
76 struct commit *commit;
78 if (get_sha1_committish(name, sha1))
80 commit = lookup_commit_reference(sha1);
81 if (!commit || parse_commit(commit))
86 static unsigned long parse_commit_date(const char *buf, const char *tail)
92 if (memcmp(buf, "author", 6))
94 while (buf < tail && *buf++ != '\n')
98 if (memcmp(buf, "committer", 9))
100 while (buf < tail && *buf++ != '>')
105 while (buf < tail && *buf++ != '\n')
109 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
110 return strtoul(dateptr, NULL, 10);
113 static struct commit_graft **commit_graft;
114 static int commit_graft_alloc, commit_graft_nr;
116 static int commit_graft_pos(const unsigned char *sha1)
120 hi = commit_graft_nr;
122 int mi = (lo + hi) / 2;
123 struct commit_graft *graft = commit_graft[mi];
124 int cmp = hashcmp(sha1, graft->sha1);
135 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
137 int pos = commit_graft_pos(graft->sha1);
143 free(commit_graft[pos]);
144 commit_graft[pos] = graft;
149 if (commit_graft_alloc <= ++commit_graft_nr) {
150 commit_graft_alloc = alloc_nr(commit_graft_alloc);
151 commit_graft = xrealloc(commit_graft,
152 sizeof(*commit_graft) *
155 if (pos < commit_graft_nr)
156 memmove(commit_graft + pos + 1,
158 (commit_graft_nr - pos - 1) *
159 sizeof(*commit_graft));
160 commit_graft[pos] = graft;
164 struct commit_graft *read_graft_line(char *buf, int len)
166 /* The format is just "Commit Parent1 Parent2 ...\n" */
168 struct commit_graft *graft = NULL;
170 while (len && isspace(buf[len-1]))
172 if (buf[0] == '#' || buf[0] == '\0')
176 i = (len + 1) / 41 - 1;
177 graft = xmalloc(sizeof(*graft) + 20 * i);
178 graft->nr_parent = i;
179 if (get_sha1_hex(buf, graft->sha1))
181 for (i = 40; i < len; i += 41) {
184 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
190 error("bad graft data: %s", buf);
195 static int read_graft_file(const char *graft_file)
197 FILE *fp = fopen(graft_file, "r");
201 while (fgets(buf, sizeof(buf), fp)) {
202 /* The format is just "Commit Parent1 Parent2 ...\n" */
203 int len = strlen(buf);
204 struct commit_graft *graft = read_graft_line(buf, len);
207 if (register_commit_graft(graft, 1))
208 error("duplicate graft data: %s", buf);
214 static void prepare_commit_graft(void)
216 static int commit_graft_prepared;
219 if (commit_graft_prepared)
221 graft_file = get_graft_file();
222 read_graft_file(graft_file);
223 /* make sure shallows are read */
224 is_repository_shallow();
225 commit_graft_prepared = 1;
228 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
231 prepare_commit_graft();
232 pos = commit_graft_pos(sha1);
235 return commit_graft[pos];
238 int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
241 for (i = ret = 0; i < commit_graft_nr && !ret; i++)
242 ret = fn(commit_graft[i], cb_data);
246 int unregister_shallow(const unsigned char *sha1)
248 int pos = commit_graft_pos(sha1);
251 if (pos + 1 < commit_graft_nr)
252 memmove(commit_graft + pos, commit_graft + pos + 1,
253 sizeof(struct commit_graft *)
254 * (commit_graft_nr - pos - 1));
259 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
261 const char *tail = buffer;
262 const char *bufptr = buffer;
263 unsigned char parent[20];
264 struct commit_list **pptr;
265 struct commit_graft *graft;
267 if (item->object.parsed)
269 item->object.parsed = 1;
271 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
272 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
273 if (get_sha1_hex(bufptr + 5, parent) < 0)
274 return error("bad tree pointer in commit %s",
275 sha1_to_hex(item->object.sha1));
276 item->tree = lookup_tree(parent);
277 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
278 pptr = &item->parents;
280 graft = lookup_commit_graft(item->object.sha1);
281 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
282 struct commit *new_parent;
284 if (tail <= bufptr + 48 ||
285 get_sha1_hex(bufptr + 7, parent) ||
287 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
290 * The clone is shallow if nr_parent < 0, and we must
291 * not traverse its real parents even when we unhide them.
293 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
295 new_parent = lookup_commit(parent);
297 pptr = &commit_list_insert(new_parent, pptr)->next;
301 struct commit *new_parent;
302 for (i = 0; i < graft->nr_parent; i++) {
303 new_parent = lookup_commit(graft->parent[i]);
306 pptr = &commit_list_insert(new_parent, pptr)->next;
309 item->date = parse_commit_date(bufptr, tail);
314 int parse_commit(struct commit *item)
316 enum object_type type;
323 if (item->object.parsed)
325 buffer = read_sha1_file(item->object.sha1, &type, &size);
327 return error("Could not read %s",
328 sha1_to_hex(item->object.sha1));
329 if (type != OBJ_COMMIT) {
331 return error("Object %s not a commit",
332 sha1_to_hex(item->object.sha1));
334 ret = parse_commit_buffer(item, buffer, size);
335 if (save_commit_buffer && !ret) {
336 item->buffer = buffer;
343 int find_commit_subject(const char *commit_buffer, const char **subject)
346 const char *p = commit_buffer;
348 while (*p && (*p != '\n' || p[1] != '\n'))
352 for (eol = p; *eol && *eol != '\n'; eol++)
362 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
364 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
365 new_list->item = item;
366 new_list->next = *list_p;
371 unsigned commit_list_count(const struct commit_list *l)
374 for (; l; l = l->next )
379 void free_commit_list(struct commit_list *list)
382 struct commit_list *temp = list;
388 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
390 struct commit_list **pp = list;
391 struct commit_list *p;
392 while ((p = *pp) != NULL) {
393 if (p->item->date < item->date) {
398 return commit_list_insert(item, pp);
401 static int commit_list_compare_by_date(const void *a, const void *b)
403 unsigned long a_date = ((const struct commit_list *)a)->item->date;
404 unsigned long b_date = ((const struct commit_list *)b)->item->date;
412 static void *commit_list_get_next(const void *a)
414 return ((const struct commit_list *)a)->next;
417 static void commit_list_set_next(void *a, void *next)
419 ((struct commit_list *)a)->next = next;
422 void commit_list_sort_by_date(struct commit_list **list)
424 *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
425 commit_list_compare_by_date);
428 struct commit *pop_most_recent_commit(struct commit_list **list,
431 struct commit *ret = (*list)->item;
432 struct commit_list *parents = ret->parents;
433 struct commit_list *old = *list;
435 *list = (*list)->next;
439 struct commit *commit = parents->item;
440 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
441 commit->object.flags |= mark;
442 commit_list_insert_by_date(commit, list);
444 parents = parents->next;
449 static void clear_commit_marks_1(struct commit_list **plist,
450 struct commit *commit, unsigned int mark)
453 struct commit_list *parents;
455 if (!(mark & commit->object.flags))
458 commit->object.flags &= ~mark;
460 parents = commit->parents;
464 while ((parents = parents->next))
465 commit_list_insert(parents->item, plist);
467 commit = commit->parents->item;
471 void clear_commit_marks(struct commit *commit, unsigned int mark)
473 struct commit_list *list = NULL;
474 commit_list_insert(commit, &list);
476 clear_commit_marks_1(&list, pop_commit(&list), mark);
479 void clear_commit_marks_for_object_array(struct object_array *a, unsigned mark)
481 struct object *object;
482 struct commit *commit;
485 for (i = 0; i < a->nr; i++) {
486 object = a->objects[i].item;
487 commit = lookup_commit_reference_gently(object->sha1, 1);
489 clear_commit_marks(commit, mark);
493 struct commit *pop_commit(struct commit_list **stack)
495 struct commit_list *top = *stack;
496 struct commit *item = top ? top->item : NULL;
506 * Topological sort support
509 /* count number of children that have not been emitted */
510 define_commit_slab(indegree_slab, int);
513 * Performs an in-place topological sort on the list supplied.
515 void sort_in_topological_order(struct commit_list ** list, int lifo)
517 struct commit_list *next, *orig = *list;
518 struct commit_list *work, **insert;
519 struct commit_list **pptr;
520 struct indegree_slab indegree;
526 init_indegree_slab(&indegree);
528 /* Mark them and clear the indegree */
529 for (next = orig; next; next = next->next) {
530 struct commit *commit = next->item;
531 *(indegree_slab_at(&indegree, commit)) = 1;
534 /* update the indegree */
535 for (next = orig; next; next = next->next) {
536 struct commit_list * parents = next->item->parents;
538 struct commit *parent = parents->item;
539 int *pi = indegree_slab_at(&indegree, parent);
543 parents = parents->next;
550 * tips are nodes not reachable from any other node in the list
552 * the tips serve as a starting set for the work queue.
556 for (next = orig; next; next = next->next) {
557 struct commit *commit = next->item;
559 if (*(indegree_slab_at(&indegree, commit)) == 1)
560 insert = &commit_list_insert(commit, insert)->next;
563 /* process the list in topological order */
565 commit_list_sort_by_date(&work);
570 struct commit *commit;
571 struct commit_list *parents, *work_item;
574 work = work_item->next;
575 work_item->next = NULL;
577 commit = work_item->item;
578 for (parents = commit->parents; parents ; parents = parents->next) {
579 struct commit *parent = parents->item;
580 int *pi = indegree_slab_at(&indegree, parent);
586 * parents are only enqueued for emission
587 * when all their children have been emitted thereby
588 * guaranteeing topological order.
592 commit_list_insert_by_date(parent, &work);
594 commit_list_insert(parent, &work);
598 * work_item is a commit all of whose children
599 * have already been emitted. we can emit it now.
601 *(indegree_slab_at(&indegree, commit)) = 0;
603 pptr = &work_item->next;
606 clear_indegree_slab(&indegree);
609 /* merge-base stuff */
611 /* bits #0..15 in revision.h */
612 #define PARENT1 (1u<<16)
613 #define PARENT2 (1u<<17)
614 #define STALE (1u<<18)
615 #define RESULT (1u<<19)
617 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
619 static struct commit *interesting(struct commit_list *list)
622 struct commit *commit = list->item;
624 if (commit->object.flags & STALE)
631 /* all input commits in one and twos[] must have been parsed! */
632 static struct commit_list *paint_down_to_common(struct commit *one, int n, struct commit **twos)
634 struct commit_list *list = NULL;
635 struct commit_list *result = NULL;
638 one->object.flags |= PARENT1;
639 commit_list_insert_by_date(one, &list);
642 for (i = 0; i < n; i++) {
643 twos[i]->object.flags |= PARENT2;
644 commit_list_insert_by_date(twos[i], &list);
647 while (interesting(list)) {
648 struct commit *commit;
649 struct commit_list *parents;
650 struct commit_list *next;
658 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
659 if (flags == (PARENT1 | PARENT2)) {
660 if (!(commit->object.flags & RESULT)) {
661 commit->object.flags |= RESULT;
662 commit_list_insert_by_date(commit, &result);
664 /* Mark parents of a found merge stale */
667 parents = commit->parents;
669 struct commit *p = parents->item;
670 parents = parents->next;
671 if ((p->object.flags & flags) == flags)
675 p->object.flags |= flags;
676 commit_list_insert_by_date(p, &list);
680 free_commit_list(list);
684 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
686 struct commit_list *list = NULL;
687 struct commit_list *result = NULL;
690 for (i = 0; i < n; i++) {
693 * We do not mark this even with RESULT so we do not
694 * have to clean it up.
696 return commit_list_insert(one, &result);
699 if (parse_commit(one))
701 for (i = 0; i < n; i++) {
702 if (parse_commit(twos[i]))
706 list = paint_down_to_common(one, n, twos);
709 struct commit_list *next = list->next;
710 if (!(list->item->object.flags & STALE))
711 commit_list_insert_by_date(list->item, &result);
718 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
720 struct commit_list *i, *j, *k, *ret = NULL;
721 struct commit_list **pptr = &ret;
723 for (i = in; i; i = i->next) {
725 pptr = &commit_list_insert(i->item, pptr)->next;
727 struct commit_list *new = NULL, *end = NULL;
729 for (j = ret; j; j = j->next) {
730 struct commit_list *bases;
731 bases = get_merge_bases(i->item, j->item, 1);
736 for (k = bases; k; k = k->next)
745 static int remove_redundant(struct commit **array, int cnt)
748 * Some commit in the array may be an ancestor of
749 * another commit. Move such commit to the end of
750 * the array, and return the number of commits that
751 * are independent from each other.
753 struct commit **work;
754 unsigned char *redundant;
758 work = xcalloc(cnt, sizeof(*work));
759 redundant = xcalloc(cnt, 1);
760 filled_index = xmalloc(sizeof(*filled_index) * (cnt - 1));
762 for (i = 0; i < cnt; i++)
763 parse_commit(array[i]);
764 for (i = 0; i < cnt; i++) {
765 struct commit_list *common;
769 for (j = filled = 0; j < cnt; j++) {
770 if (i == j || redundant[j])
772 filled_index[filled] = j;
773 work[filled++] = array[j];
775 common = paint_down_to_common(array[i], filled, work);
776 if (array[i]->object.flags & PARENT2)
778 for (j = 0; j < filled; j++)
779 if (work[j]->object.flags & PARENT1)
780 redundant[filled_index[j]] = 1;
781 clear_commit_marks(array[i], all_flags);
782 for (j = 0; j < filled; j++)
783 clear_commit_marks(work[j], all_flags);
784 free_commit_list(common);
787 /* Now collect the result */
788 memcpy(work, array, sizeof(*array) * cnt);
789 for (i = filled = 0; i < cnt; i++)
791 array[filled++] = work[i];
792 for (j = filled, i = 0; i < cnt; i++)
794 array[j++] = work[i];
801 struct commit_list *get_merge_bases_many(struct commit *one,
803 struct commit **twos,
806 struct commit_list *list;
807 struct commit **rslt;
808 struct commit_list *result;
811 result = merge_bases_many(one, n, twos);
812 for (i = 0; i < n; i++) {
816 if (!result || !result->next) {
818 clear_commit_marks(one, all_flags);
819 for (i = 0; i < n; i++)
820 clear_commit_marks(twos[i], all_flags);
825 /* There are more than one */
832 rslt = xcalloc(cnt, sizeof(*rslt));
833 for (list = result, i = 0; list; list = list->next)
834 rslt[i++] = list->item;
835 free_commit_list(result);
837 clear_commit_marks(one, all_flags);
838 for (i = 0; i < n; i++)
839 clear_commit_marks(twos[i], all_flags);
841 cnt = remove_redundant(rslt, cnt);
843 for (i = 0; i < cnt; i++)
844 commit_list_insert_by_date(rslt[i], &result);
849 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
852 return get_merge_bases_many(one, 1, &two, cleanup);
856 * Is "commit" a descendant of one of the elements on the "with_commit" list?
858 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
862 while (with_commit) {
863 struct commit *other;
865 other = with_commit->item;
866 with_commit = with_commit->next;
867 if (in_merge_bases(other, commit))
874 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
876 int in_merge_bases(struct commit *commit, struct commit *reference)
878 struct commit_list *bases;
881 if (parse_commit(commit) || parse_commit(reference))
884 bases = paint_down_to_common(commit, 1, &reference);
885 if (commit->object.flags & PARENT2)
887 clear_commit_marks(commit, all_flags);
888 clear_commit_marks(reference, all_flags);
889 free_commit_list(bases);
893 struct commit_list *reduce_heads(struct commit_list *heads)
895 struct commit_list *p;
896 struct commit_list *result = NULL, **tail = &result;
897 struct commit **array;
904 for (p = heads; p; p = p->next)
905 p->item->object.flags &= ~STALE;
906 for (p = heads, num_head = 0; p; p = p->next) {
907 if (p->item->object.flags & STALE)
909 p->item->object.flags |= STALE;
912 array = xcalloc(sizeof(*array), num_head);
913 for (p = heads, i = 0; p; p = p->next) {
914 if (p->item->object.flags & STALE) {
915 array[i++] = p->item;
916 p->item->object.flags &= ~STALE;
919 num_head = remove_redundant(array, num_head);
920 for (i = 0; i < num_head; i++)
921 tail = &commit_list_insert(array[i], tail)->next;
925 static const char gpg_sig_header[] = "gpgsig";
926 static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
928 static int do_sign_commit(struct strbuf *buf, const char *keyid)
930 struct strbuf sig = STRBUF_INIT;
933 /* find the end of the header */
934 inspos = strstr(buf->buf, "\n\n") - buf->buf + 1;
936 if (!keyid || !*keyid)
937 keyid = get_signing_key();
938 if (sign_buffer(buf, &sig, keyid)) {
939 strbuf_release(&sig);
943 for (copypos = 0; sig.buf[copypos]; ) {
944 const char *bol = sig.buf + copypos;
945 const char *eol = strchrnul(bol, '\n');
946 int len = (eol - bol) + !!*eol;
949 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
950 inspos += gpg_sig_header_len;
952 strbuf_insert(buf, inspos++, " ", 1);
953 strbuf_insert(buf, inspos, bol, len);
957 strbuf_release(&sig);
961 int parse_signed_commit(const unsigned char *sha1,
962 struct strbuf *payload, struct strbuf *signature)
965 enum object_type type;
966 char *buffer = read_sha1_file(sha1, &type, &size);
967 int in_signature, saw_signature = -1;
970 if (!buffer || type != OBJ_COMMIT)
974 tail = buffer + size;
977 while (line < tail) {
978 const char *sig = NULL;
979 char *next = memchr(line, '\n', tail - line);
981 next = next ? next + 1 : tail;
982 if (in_signature && line[0] == ' ')
984 else if (!prefixcmp(line, gpg_sig_header) &&
985 line[gpg_sig_header_len] == ' ')
986 sig = line + gpg_sig_header_len + 1;
988 strbuf_add(signature, sig, next - sig);
993 /* dump the whole remainder of the buffer */
995 strbuf_add(payload, line, next - line);
1002 return saw_signature;
1005 static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
1007 struct merge_remote_desc *desc;
1008 struct commit_extra_header *mergetag;
1010 unsigned long size, len;
1011 enum object_type type;
1013 desc = merge_remote_util(parent);
1014 if (!desc || !desc->obj)
1016 buf = read_sha1_file(desc->obj->sha1, &type, &size);
1017 if (!buf || type != OBJ_TAG)
1019 len = parse_signature(buf, size);
1023 * We could verify this signature and either omit the tag when
1024 * it does not validate, but the integrator may not have the
1025 * public key of the signer of the tag he is merging, while a
1026 * later auditor may have it while auditing, so let's not run
1027 * verify-signed-buffer here for now...
1029 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1030 * warn("warning: signed tag unverified.");
1032 mergetag = xcalloc(1, sizeof(*mergetag));
1033 mergetag->key = xstrdup("mergetag");
1034 mergetag->value = buf;
1035 mergetag->len = size;
1038 *tail = &mergetag->next;
1045 void append_merge_tag_headers(struct commit_list *parents,
1046 struct commit_extra_header ***tail)
1049 struct commit *parent = parents->item;
1050 handle_signed_tag(parent, tail);
1051 parents = parents->next;
1055 static void add_extra_header(struct strbuf *buffer,
1056 struct commit_extra_header *extra)
1058 strbuf_addstr(buffer, extra->key);
1060 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1062 strbuf_addch(buffer, '\n');
1065 struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1066 const char **exclude)
1068 struct commit_extra_header *extra = NULL;
1070 enum object_type type;
1071 char *buffer = read_sha1_file(commit->object.sha1, &type, &size);
1072 if (buffer && type == OBJ_COMMIT)
1073 extra = read_commit_extra_header_lines(buffer, size, exclude);
1078 static inline int standard_header_field(const char *field, size_t len)
1080 return ((len == 4 && !memcmp(field, "tree ", 5)) ||
1081 (len == 6 && !memcmp(field, "parent ", 7)) ||
1082 (len == 6 && !memcmp(field, "author ", 7)) ||
1083 (len == 9 && !memcmp(field, "committer ", 10)) ||
1084 (len == 8 && !memcmp(field, "encoding ", 9)));
1087 static int excluded_header_field(const char *field, size_t len, const char **exclude)
1093 size_t xlen = strlen(*exclude);
1095 !memcmp(field, *exclude, xlen) && field[xlen] == ' ')
1102 static struct commit_extra_header *read_commit_extra_header_lines(
1103 const char *buffer, size_t size,
1104 const char **exclude)
1106 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1107 const char *line, *next, *eof, *eob;
1108 struct strbuf buf = STRBUF_INIT;
1110 for (line = buffer, eob = line + size;
1111 line < eob && *line != '\n';
1113 next = memchr(line, '\n', eob - line);
1114 next = next ? next + 1 : eob;
1118 strbuf_add(&buf, line + 1, next - (line + 1));
1122 it->value = strbuf_detach(&buf, &it->len);
1126 eof = strchr(line, ' ');
1130 if (standard_header_field(line, eof - line) ||
1131 excluded_header_field(line, eof - line, exclude))
1134 it = xcalloc(1, sizeof(*it));
1135 it->key = xmemdupz(line, eof-line);
1139 strbuf_add(&buf, eof + 1, next - (eof + 1));
1142 it->value = strbuf_detach(&buf, &it->len);
1146 void free_commit_extra_headers(struct commit_extra_header *extra)
1149 struct commit_extra_header *next = extra->next;
1157 int commit_tree(const struct strbuf *msg, unsigned char *tree,
1158 struct commit_list *parents, unsigned char *ret,
1159 const char *author, const char *sign_commit)
1161 struct commit_extra_header *extra = NULL, **tail = &extra;
1164 append_merge_tag_headers(parents, &tail);
1165 result = commit_tree_extended(msg, tree, parents, ret,
1166 author, sign_commit, extra);
1167 free_commit_extra_headers(extra);
1171 static int find_invalid_utf8(const char *buf, int len)
1176 unsigned char c = *buf++;
1177 int bytes, bad_offset;
1182 /* Simple US-ASCII? No worries. */
1186 bad_offset = offset-1;
1189 * Count how many more high bits set: that's how
1190 * many more bytes this sequence should have.
1198 /* Must be between 1 and 5 more bytes */
1199 if (bytes < 1 || bytes > 5)
1202 /* Do we *have* that many bytes? */
1209 /* And verify that they are good continuation bytes */
1211 if ((*buf++ & 0xc0) != 0x80)
1215 /* We could/should check the value and length here too */
1221 * This verifies that the buffer is in proper utf8 format.
1223 * If it isn't, it assumes any non-utf8 characters are Latin1,
1224 * and does the conversion.
1226 * Fixme: we should probably also disallow overlong forms and
1227 * invalid characters. But we don't do that currently.
1229 static int verify_utf8(struct strbuf *buf)
1237 unsigned char replace[2];
1239 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1245 strbuf_remove(buf, pos, 1);
1247 /* We know 'c' must be in the range 128-255 */
1248 replace[0] = 0xc0 + (c >> 6);
1249 replace[1] = 0x80 + (c & 0x3f);
1250 strbuf_insert(buf, pos, replace, 2);
1255 static const char commit_utf8_warn[] =
1256 "Warning: commit message did not conform to UTF-8.\n"
1257 "You may want to amend it after fixing the message, or set the config\n"
1258 "variable i18n.commitencoding to the encoding your project uses.\n";
1260 int commit_tree_extended(const struct strbuf *msg, unsigned char *tree,
1261 struct commit_list *parents, unsigned char *ret,
1262 const char *author, const char *sign_commit,
1263 struct commit_extra_header *extra)
1266 int encoding_is_utf8;
1267 struct strbuf buffer;
1269 assert_sha1_type(tree, OBJ_TREE);
1271 if (memchr(msg->buf, '\0', msg->len))
1272 return error("a NUL byte in commit log message not allowed.");
1274 /* Not having i18n.commitencoding is the same as having utf-8 */
1275 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1277 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1278 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
1281 * NOTE! This ordering means that the same exact tree merged with a
1282 * different order of parents will be a _different_ changeset even
1283 * if everything else stays the same.
1286 struct commit_list *next = parents->next;
1287 struct commit *parent = parents->item;
1289 strbuf_addf(&buffer, "parent %s\n",
1290 sha1_to_hex(parent->object.sha1));
1295 /* Person/date information */
1297 author = git_author_info(IDENT_STRICT);
1298 strbuf_addf(&buffer, "author %s\n", author);
1299 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
1300 if (!encoding_is_utf8)
1301 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
1304 add_extra_header(&buffer, extra);
1305 extra = extra->next;
1307 strbuf_addch(&buffer, '\n');
1309 /* And add the comment */
1310 strbuf_addbuf(&buffer, msg);
1312 /* And check the encoding */
1313 if (encoding_is_utf8 && !verify_utf8(&buffer))
1314 fprintf(stderr, commit_utf8_warn);
1316 if (sign_commit && do_sign_commit(&buffer, sign_commit))
1319 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
1320 strbuf_release(&buffer);
1324 struct commit *get_merge_parent(const char *name)
1327 struct commit *commit;
1328 unsigned char sha1[20];
1329 if (get_sha1(name, sha1))
1331 obj = parse_object(sha1);
1332 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1333 if (commit && !commit->util) {
1334 struct merge_remote_desc *desc;
1335 desc = xmalloc(sizeof(*desc));
1337 desc->name = strdup(name);
1338 commit->util = desc;
1344 * Append a commit to the end of the commit_list.
1346 * next starts by pointing to the variable that holds the head of an
1347 * empty commit_list, and is updated to point to the "next" field of
1348 * the last item on the list as new commits are appended.
1352 * struct commit_list *list;
1353 * struct commit_list **next = &list;
1355 * next = commit_list_append(c1, next);
1356 * next = commit_list_append(c2, next);
1357 * assert(commit_list_count(list) == 2);
1360 struct commit_list **commit_list_append(struct commit *commit,
1361 struct commit_list **next)
1363 struct commit_list *new = xmalloc(sizeof(struct commit_list));
1370 void print_commit_list(struct commit_list *list,
1371 const char *format_cur,
1372 const char *format_last)
1374 for ( ; list; list = list->next) {
1375 const char *format = list->next ? format_cur : format_last;
1376 printf(format, sha1_to_hex(list->item->object.sha1));