9 #include "gpg-interface.h"
10 #include "mergesort.h"
12 int save_commit_buffer = 1;
14 const char *commit_type = "commit";
16 static struct commit *check_commit(struct object *obj,
17 const unsigned char *sha1,
20 if (obj->type != OBJ_COMMIT) {
22 error("Object %s is a %s, not a commit",
23 sha1_to_hex(sha1), typename(obj->type));
26 return (struct commit *) obj;
29 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
32 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
36 return check_commit(obj, sha1, quiet);
39 struct commit *lookup_commit_reference(const unsigned char *sha1)
41 return lookup_commit_reference_gently(sha1, 0);
44 struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_name)
46 struct commit *c = lookup_commit_reference(sha1);
48 die(_("could not parse %s"), ref_name);
49 if (hashcmp(sha1, c->object.sha1)) {
50 warning(_("%s %s is not a commit!"),
51 ref_name, sha1_to_hex(sha1));
56 struct commit *lookup_commit(const unsigned char *sha1)
58 struct object *obj = lookup_object(sha1);
60 return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
62 obj->type = OBJ_COMMIT;
63 return check_commit(obj, sha1, 0);
66 struct commit *lookup_commit_reference_by_name(const char *name)
68 unsigned char sha1[20];
69 struct commit *commit;
71 if (get_sha1_committish(name, sha1))
73 commit = lookup_commit_reference(sha1);
74 if (!commit || parse_commit(commit))
79 static unsigned long parse_commit_date(const char *buf, const char *tail)
85 if (memcmp(buf, "author", 6))
87 while (buf < tail && *buf++ != '\n')
91 if (memcmp(buf, "committer", 9))
93 while (buf < tail && *buf++ != '>')
98 while (buf < tail && *buf++ != '\n')
102 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
103 return strtoul(dateptr, NULL, 10);
106 static struct commit_graft **commit_graft;
107 static int commit_graft_alloc, commit_graft_nr;
109 static int commit_graft_pos(const unsigned char *sha1)
113 hi = commit_graft_nr;
115 int mi = (lo + hi) / 2;
116 struct commit_graft *graft = commit_graft[mi];
117 int cmp = hashcmp(sha1, graft->sha1);
128 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
130 int pos = commit_graft_pos(graft->sha1);
136 free(commit_graft[pos]);
137 commit_graft[pos] = graft;
142 if (commit_graft_alloc <= ++commit_graft_nr) {
143 commit_graft_alloc = alloc_nr(commit_graft_alloc);
144 commit_graft = xrealloc(commit_graft,
145 sizeof(*commit_graft) *
148 if (pos < commit_graft_nr)
149 memmove(commit_graft + pos + 1,
151 (commit_graft_nr - pos - 1) *
152 sizeof(*commit_graft));
153 commit_graft[pos] = graft;
157 struct commit_graft *read_graft_line(char *buf, int len)
159 /* The format is just "Commit Parent1 Parent2 ...\n" */
161 struct commit_graft *graft = NULL;
163 while (len && isspace(buf[len-1]))
165 if (buf[0] == '#' || buf[0] == '\0')
169 i = (len + 1) / 41 - 1;
170 graft = xmalloc(sizeof(*graft) + 20 * i);
171 graft->nr_parent = i;
172 if (get_sha1_hex(buf, graft->sha1))
174 for (i = 40; i < len; i += 41) {
177 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
183 error("bad graft data: %s", buf);
188 static int read_graft_file(const char *graft_file)
190 FILE *fp = fopen(graft_file, "r");
194 while (fgets(buf, sizeof(buf), fp)) {
195 /* The format is just "Commit Parent1 Parent2 ...\n" */
196 int len = strlen(buf);
197 struct commit_graft *graft = read_graft_line(buf, len);
200 if (register_commit_graft(graft, 1))
201 error("duplicate graft data: %s", buf);
207 static void prepare_commit_graft(void)
209 static int commit_graft_prepared;
212 if (commit_graft_prepared)
214 graft_file = get_graft_file();
215 read_graft_file(graft_file);
216 /* make sure shallows are read */
217 is_repository_shallow();
218 commit_graft_prepared = 1;
221 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
224 prepare_commit_graft();
225 pos = commit_graft_pos(sha1);
228 return commit_graft[pos];
231 int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
234 for (i = ret = 0; i < commit_graft_nr && !ret; i++)
235 ret = fn(commit_graft[i], cb_data);
239 int unregister_shallow(const unsigned char *sha1)
241 int pos = commit_graft_pos(sha1);
244 if (pos + 1 < commit_graft_nr)
245 memmove(commit_graft + pos, commit_graft + pos + 1,
246 sizeof(struct commit_graft *)
247 * (commit_graft_nr - pos - 1));
252 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
254 const char *tail = buffer;
255 const char *bufptr = buffer;
256 unsigned char parent[20];
257 struct commit_list **pptr;
258 struct commit_graft *graft;
260 if (item->object.parsed)
262 item->object.parsed = 1;
264 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
265 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
266 if (get_sha1_hex(bufptr + 5, parent) < 0)
267 return error("bad tree pointer in commit %s",
268 sha1_to_hex(item->object.sha1));
269 item->tree = lookup_tree(parent);
270 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
271 pptr = &item->parents;
273 graft = lookup_commit_graft(item->object.sha1);
274 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
275 struct commit *new_parent;
277 if (tail <= bufptr + 48 ||
278 get_sha1_hex(bufptr + 7, parent) ||
280 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
283 * The clone is shallow if nr_parent < 0, and we must
284 * not traverse its real parents even when we unhide them.
286 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
288 new_parent = lookup_commit(parent);
290 pptr = &commit_list_insert(new_parent, pptr)->next;
294 struct commit *new_parent;
295 for (i = 0; i < graft->nr_parent; i++) {
296 new_parent = lookup_commit(graft->parent[i]);
299 pptr = &commit_list_insert(new_parent, pptr)->next;
302 item->date = parse_commit_date(bufptr, tail);
307 int parse_commit(struct commit *item)
309 enum object_type type;
316 if (item->object.parsed)
318 buffer = read_sha1_file(item->object.sha1, &type, &size);
320 return error("Could not read %s",
321 sha1_to_hex(item->object.sha1));
322 if (type != OBJ_COMMIT) {
324 return error("Object %s not a commit",
325 sha1_to_hex(item->object.sha1));
327 ret = parse_commit_buffer(item, buffer, size);
328 if (save_commit_buffer && !ret) {
329 item->buffer = buffer;
336 int find_commit_subject(const char *commit_buffer, const char **subject)
339 const char *p = commit_buffer;
341 while (*p && (*p != '\n' || p[1] != '\n'))
345 for (eol = p; *eol && *eol != '\n'; eol++)
355 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
357 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
358 new_list->item = item;
359 new_list->next = *list_p;
364 unsigned commit_list_count(const struct commit_list *l)
367 for (; l; l = l->next )
372 void free_commit_list(struct commit_list *list)
375 struct commit_list *temp = list;
381 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
383 struct commit_list **pp = list;
384 struct commit_list *p;
385 while ((p = *pp) != NULL) {
386 if (p->item->date < item->date) {
391 return commit_list_insert(item, pp);
394 static int commit_list_compare_by_date(const void *a, const void *b)
396 unsigned long a_date = ((const struct commit_list *)a)->item->date;
397 unsigned long b_date = ((const struct commit_list *)b)->item->date;
405 static void *commit_list_get_next(const void *a)
407 return ((const struct commit_list *)a)->next;
410 static void commit_list_set_next(void *a, void *next)
412 ((struct commit_list *)a)->next = next;
415 void commit_list_sort_by_date(struct commit_list **list)
417 *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
418 commit_list_compare_by_date);
421 struct commit *pop_most_recent_commit(struct commit_list **list,
424 struct commit *ret = (*list)->item;
425 struct commit_list *parents = ret->parents;
426 struct commit_list *old = *list;
428 *list = (*list)->next;
432 struct commit *commit = parents->item;
433 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
434 commit->object.flags |= mark;
435 commit_list_insert_by_date(commit, list);
437 parents = parents->next;
442 static void clear_commit_marks_1(struct commit_list **plist,
443 struct commit *commit, unsigned int mark)
446 struct commit_list *parents;
448 if (!(mark & commit->object.flags))
451 commit->object.flags &= ~mark;
453 parents = commit->parents;
457 while ((parents = parents->next))
458 commit_list_insert(parents->item, plist);
460 commit = commit->parents->item;
464 void clear_commit_marks(struct commit *commit, unsigned int mark)
466 struct commit_list *list = NULL;
467 commit_list_insert(commit, &list);
469 clear_commit_marks_1(&list, pop_commit(&list), mark);
472 void clear_commit_marks_for_object_array(struct object_array *a, unsigned mark)
474 struct object *object;
475 struct commit *commit;
478 for (i = 0; i < a->nr; i++) {
479 object = a->objects[i].item;
480 commit = lookup_commit_reference_gently(object->sha1, 1);
482 clear_commit_marks(commit, mark);
486 struct commit *pop_commit(struct commit_list **stack)
488 struct commit_list *top = *stack;
489 struct commit *item = top ? top->item : NULL;
499 * Performs an in-place topological sort on the list supplied.
501 void sort_in_topological_order(struct commit_list ** list, int lifo)
503 struct commit_list *next, *orig = *list;
504 struct commit_list *work, **insert;
505 struct commit_list **pptr;
511 /* Mark them and clear the indegree */
512 for (next = orig; next; next = next->next) {
513 struct commit *commit = next->item;
514 commit->indegree = 1;
517 /* update the indegree */
518 for (next = orig; next; next = next->next) {
519 struct commit_list * parents = next->item->parents;
521 struct commit *parent = parents->item;
523 if (parent->indegree)
525 parents = parents->next;
532 * tips are nodes not reachable from any other node in the list
534 * the tips serve as a starting set for the work queue.
538 for (next = orig; next; next = next->next) {
539 struct commit *commit = next->item;
541 if (commit->indegree == 1)
542 insert = &commit_list_insert(commit, insert)->next;
545 /* process the list in topological order */
547 commit_list_sort_by_date(&work);
552 struct commit *commit;
553 struct commit_list *parents, *work_item;
556 work = work_item->next;
557 work_item->next = NULL;
559 commit = work_item->item;
560 for (parents = commit->parents; parents ; parents = parents->next) {
561 struct commit *parent = parents->item;
563 if (!parent->indegree)
567 * parents are only enqueued for emission
568 * when all their children have been emitted thereby
569 * guaranteeing topological order.
571 if (--parent->indegree == 1) {
573 commit_list_insert_by_date(parent, &work);
575 commit_list_insert(parent, &work);
579 * work_item is a commit all of whose children
580 * have already been emitted. we can emit it now.
582 commit->indegree = 0;
584 pptr = &work_item->next;
588 /* merge-base stuff */
590 /* bits #0..15 in revision.h */
591 #define PARENT1 (1u<<16)
592 #define PARENT2 (1u<<17)
593 #define STALE (1u<<18)
594 #define RESULT (1u<<19)
596 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
598 static struct commit *interesting(struct commit_list *list)
601 struct commit *commit = list->item;
603 if (commit->object.flags & STALE)
610 static struct commit_list *paint_down_to_common(struct commit *one, int n, struct commit **twos)
612 struct commit_list *list = NULL;
613 struct commit_list *result = NULL;
616 one->object.flags |= PARENT1;
617 commit_list_insert_by_date(one, &list);
618 for (i = 0; i < n; i++) {
619 twos[i]->object.flags |= PARENT2;
620 commit_list_insert_by_date(twos[i], &list);
623 while (interesting(list)) {
624 struct commit *commit;
625 struct commit_list *parents;
626 struct commit_list *next;
634 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
635 if (flags == (PARENT1 | PARENT2)) {
636 if (!(commit->object.flags & RESULT)) {
637 commit->object.flags |= RESULT;
638 commit_list_insert_by_date(commit, &result);
640 /* Mark parents of a found merge stale */
643 parents = commit->parents;
645 struct commit *p = parents->item;
646 parents = parents->next;
647 if ((p->object.flags & flags) == flags)
651 p->object.flags |= flags;
652 commit_list_insert_by_date(p, &list);
656 free_commit_list(list);
660 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
662 struct commit_list *list = NULL;
663 struct commit_list *result = NULL;
666 for (i = 0; i < n; i++) {
669 * We do not mark this even with RESULT so we do not
670 * have to clean it up.
672 return commit_list_insert(one, &result);
675 if (parse_commit(one))
677 for (i = 0; i < n; i++) {
678 if (parse_commit(twos[i]))
682 list = paint_down_to_common(one, n, twos);
685 struct commit_list *next = list->next;
686 if (!(list->item->object.flags & STALE))
687 commit_list_insert_by_date(list->item, &result);
694 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
696 struct commit_list *i, *j, *k, *ret = NULL;
697 struct commit_list **pptr = &ret;
699 for (i = in; i; i = i->next) {
701 pptr = &commit_list_insert(i->item, pptr)->next;
703 struct commit_list *new = NULL, *end = NULL;
705 for (j = ret; j; j = j->next) {
706 struct commit_list *bases;
707 bases = get_merge_bases(i->item, j->item, 1);
712 for (k = bases; k; k = k->next)
721 static int remove_redundant(struct commit **array, int cnt)
724 * Some commit in the array may be an ancestor of
725 * another commit. Move such commit to the end of
726 * the array, and return the number of commits that
727 * are independent from each other.
729 struct commit **work;
730 unsigned char *redundant;
734 work = xcalloc(cnt, sizeof(*work));
735 redundant = xcalloc(cnt, 1);
736 filled_index = xmalloc(sizeof(*filled_index) * (cnt - 1));
738 for (i = 0; i < cnt; i++) {
739 struct commit_list *common;
743 for (j = filled = 0; j < cnt; j++) {
744 if (i == j || redundant[j])
746 filled_index[filled] = j;
747 work[filled++] = array[j];
749 common = paint_down_to_common(array[i], filled, work);
750 if (array[i]->object.flags & PARENT2)
752 for (j = 0; j < filled; j++)
753 if (work[j]->object.flags & PARENT1)
754 redundant[filled_index[j]] = 1;
755 clear_commit_marks(array[i], all_flags);
756 for (j = 0; j < filled; j++)
757 clear_commit_marks(work[j], all_flags);
758 free_commit_list(common);
761 /* Now collect the result */
762 memcpy(work, array, sizeof(*array) * cnt);
763 for (i = filled = 0; i < cnt; i++)
765 array[filled++] = work[i];
766 for (j = filled, i = 0; i < cnt; i++)
768 array[j++] = work[i];
775 struct commit_list *get_merge_bases_many(struct commit *one,
777 struct commit **twos,
780 struct commit_list *list;
781 struct commit **rslt;
782 struct commit_list *result;
785 result = merge_bases_many(one, n, twos);
786 for (i = 0; i < n; i++) {
790 if (!result || !result->next) {
792 clear_commit_marks(one, all_flags);
793 for (i = 0; i < n; i++)
794 clear_commit_marks(twos[i], all_flags);
799 /* There are more than one */
806 rslt = xcalloc(cnt, sizeof(*rslt));
807 for (list = result, i = 0; list; list = list->next)
808 rslt[i++] = list->item;
809 free_commit_list(result);
811 clear_commit_marks(one, all_flags);
812 for (i = 0; i < n; i++)
813 clear_commit_marks(twos[i], all_flags);
815 cnt = remove_redundant(rslt, cnt);
817 for (i = 0; i < cnt; i++)
818 commit_list_insert_by_date(rslt[i], &result);
823 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
826 return get_merge_bases_many(one, 1, &two, cleanup);
830 * Is "commit" a decendant of one of the elements on the "with_commit" list?
832 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
836 while (with_commit) {
837 struct commit *other;
839 other = with_commit->item;
840 with_commit = with_commit->next;
841 if (in_merge_bases(other, commit))
848 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
850 int in_merge_bases(struct commit *commit, struct commit *reference)
852 struct commit_list *bases;
855 if (parse_commit(commit) || parse_commit(reference))
858 bases = paint_down_to_common(commit, 1, &reference);
859 if (commit->object.flags & PARENT2)
861 clear_commit_marks(commit, all_flags);
862 clear_commit_marks(reference, all_flags);
863 free_commit_list(bases);
867 struct commit_list *reduce_heads(struct commit_list *heads)
869 struct commit_list *p;
870 struct commit_list *result = NULL, **tail = &result;
871 struct commit **array;
878 for (p = heads; p; p = p->next)
879 p->item->object.flags &= ~STALE;
880 for (p = heads, num_head = 0; p; p = p->next) {
881 if (p->item->object.flags & STALE)
883 p->item->object.flags |= STALE;
886 array = xcalloc(sizeof(*array), num_head);
887 for (p = heads, i = 0; p; p = p->next) {
888 if (p->item->object.flags & STALE) {
889 array[i++] = p->item;
890 p->item->object.flags &= ~STALE;
893 num_head = remove_redundant(array, num_head);
894 for (i = 0; i < num_head; i++)
895 tail = &commit_list_insert(array[i], tail)->next;
899 static const char gpg_sig_header[] = "gpgsig";
900 static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
902 static int do_sign_commit(struct strbuf *buf, const char *keyid)
904 struct strbuf sig = STRBUF_INIT;
907 /* find the end of the header */
908 inspos = strstr(buf->buf, "\n\n") - buf->buf + 1;
910 if (!keyid || !*keyid)
911 keyid = get_signing_key();
912 if (sign_buffer(buf, &sig, keyid)) {
913 strbuf_release(&sig);
917 for (copypos = 0; sig.buf[copypos]; ) {
918 const char *bol = sig.buf + copypos;
919 const char *eol = strchrnul(bol, '\n');
920 int len = (eol - bol) + !!*eol;
923 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
924 inspos += gpg_sig_header_len;
926 strbuf_insert(buf, inspos++, " ", 1);
927 strbuf_insert(buf, inspos, bol, len);
931 strbuf_release(&sig);
935 int parse_signed_commit(const unsigned char *sha1,
936 struct strbuf *payload, struct strbuf *signature)
939 enum object_type type;
940 char *buffer = read_sha1_file(sha1, &type, &size);
941 int in_signature, saw_signature = -1;
944 if (!buffer || type != OBJ_COMMIT)
948 tail = buffer + size;
951 while (line < tail) {
952 const char *sig = NULL;
953 char *next = memchr(line, '\n', tail - line);
955 next = next ? next + 1 : tail;
956 if (in_signature && line[0] == ' ')
958 else if (!prefixcmp(line, gpg_sig_header) &&
959 line[gpg_sig_header_len] == ' ')
960 sig = line + gpg_sig_header_len + 1;
962 strbuf_add(signature, sig, next - sig);
967 /* dump the whole remainder of the buffer */
969 strbuf_add(payload, line, next - line);
976 return saw_signature;
979 static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
981 struct merge_remote_desc *desc;
982 struct commit_extra_header *mergetag;
984 unsigned long size, len;
985 enum object_type type;
987 desc = merge_remote_util(parent);
988 if (!desc || !desc->obj)
990 buf = read_sha1_file(desc->obj->sha1, &type, &size);
991 if (!buf || type != OBJ_TAG)
993 len = parse_signature(buf, size);
997 * We could verify this signature and either omit the tag when
998 * it does not validate, but the integrator may not have the
999 * public key of the signer of the tag he is merging, while a
1000 * later auditor may have it while auditing, so let's not run
1001 * verify-signed-buffer here for now...
1003 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1004 * warn("warning: signed tag unverified.");
1006 mergetag = xcalloc(1, sizeof(*mergetag));
1007 mergetag->key = xstrdup("mergetag");
1008 mergetag->value = buf;
1009 mergetag->len = size;
1012 *tail = &mergetag->next;
1019 void append_merge_tag_headers(struct commit_list *parents,
1020 struct commit_extra_header ***tail)
1023 struct commit *parent = parents->item;
1024 handle_signed_tag(parent, tail);
1025 parents = parents->next;
1029 static void add_extra_header(struct strbuf *buffer,
1030 struct commit_extra_header *extra)
1032 strbuf_addstr(buffer, extra->key);
1034 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1036 strbuf_addch(buffer, '\n');
1039 struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1040 const char **exclude)
1042 struct commit_extra_header *extra = NULL;
1044 enum object_type type;
1045 char *buffer = read_sha1_file(commit->object.sha1, &type, &size);
1046 if (buffer && type == OBJ_COMMIT)
1047 extra = read_commit_extra_header_lines(buffer, size, exclude);
1052 static inline int standard_header_field(const char *field, size_t len)
1054 return ((len == 4 && !memcmp(field, "tree ", 5)) ||
1055 (len == 6 && !memcmp(field, "parent ", 7)) ||
1056 (len == 6 && !memcmp(field, "author ", 7)) ||
1057 (len == 9 && !memcmp(field, "committer ", 10)) ||
1058 (len == 8 && !memcmp(field, "encoding ", 9)));
1061 static int excluded_header_field(const char *field, size_t len, const char **exclude)
1067 size_t xlen = strlen(*exclude);
1069 !memcmp(field, *exclude, xlen) && field[xlen] == ' ')
1076 struct commit_extra_header *read_commit_extra_header_lines(const char *buffer, size_t size,
1077 const char **exclude)
1079 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1080 const char *line, *next, *eof, *eob;
1081 struct strbuf buf = STRBUF_INIT;
1083 for (line = buffer, eob = line + size;
1084 line < eob && *line != '\n';
1086 next = memchr(line, '\n', eob - line);
1087 next = next ? next + 1 : eob;
1091 strbuf_add(&buf, line + 1, next - (line + 1));
1095 it->value = strbuf_detach(&buf, &it->len);
1099 eof = strchr(line, ' ');
1103 if (standard_header_field(line, eof - line) ||
1104 excluded_header_field(line, eof - line, exclude))
1107 it = xcalloc(1, sizeof(*it));
1108 it->key = xmemdupz(line, eof-line);
1112 strbuf_add(&buf, eof + 1, next - (eof + 1));
1115 it->value = strbuf_detach(&buf, &it->len);
1119 void free_commit_extra_headers(struct commit_extra_header *extra)
1122 struct commit_extra_header *next = extra->next;
1130 int commit_tree(const struct strbuf *msg, unsigned char *tree,
1131 struct commit_list *parents, unsigned char *ret,
1132 const char *author, const char *sign_commit)
1134 struct commit_extra_header *extra = NULL, **tail = &extra;
1137 append_merge_tag_headers(parents, &tail);
1138 result = commit_tree_extended(msg, tree, parents, ret,
1139 author, sign_commit, extra);
1140 free_commit_extra_headers(extra);
1144 static int find_invalid_utf8(const char *buf, int len)
1149 unsigned char c = *buf++;
1150 int bytes, bad_offset;
1155 /* Simple US-ASCII? No worries. */
1159 bad_offset = offset-1;
1162 * Count how many more high bits set: that's how
1163 * many more bytes this sequence should have.
1171 /* Must be between 1 and 5 more bytes */
1172 if (bytes < 1 || bytes > 5)
1175 /* Do we *have* that many bytes? */
1182 /* And verify that they are good continuation bytes */
1184 if ((*buf++ & 0xc0) != 0x80)
1188 /* We could/should check the value and length here too */
1194 * This verifies that the buffer is in proper utf8 format.
1196 * If it isn't, it assumes any non-utf8 characters are Latin1,
1197 * and does the conversion.
1199 * Fixme: we should probably also disallow overlong forms and
1200 * invalid characters. But we don't do that currently.
1202 static int verify_utf8(struct strbuf *buf)
1210 unsigned char replace[2];
1212 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1218 strbuf_remove(buf, pos, 1);
1220 /* We know 'c' must be in the range 128-255 */
1221 replace[0] = 0xc0 + (c >> 6);
1222 replace[1] = 0x80 + (c & 0x3f);
1223 strbuf_insert(buf, pos, replace, 2);
1228 static const char commit_utf8_warn[] =
1229 "Warning: commit message did not conform to UTF-8.\n"
1230 "You may want to amend it after fixing the message, or set the config\n"
1231 "variable i18n.commitencoding to the encoding your project uses.\n";
1233 int commit_tree_extended(const struct strbuf *msg, unsigned char *tree,
1234 struct commit_list *parents, unsigned char *ret,
1235 const char *author, const char *sign_commit,
1236 struct commit_extra_header *extra)
1239 int encoding_is_utf8;
1240 struct strbuf buffer;
1242 assert_sha1_type(tree, OBJ_TREE);
1244 if (memchr(msg->buf, '\0', msg->len))
1245 return error("a NUL byte in commit log message not allowed.");
1247 /* Not having i18n.commitencoding is the same as having utf-8 */
1248 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1250 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1251 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
1254 * NOTE! This ordering means that the same exact tree merged with a
1255 * different order of parents will be a _different_ changeset even
1256 * if everything else stays the same.
1259 struct commit_list *next = parents->next;
1260 struct commit *parent = parents->item;
1262 strbuf_addf(&buffer, "parent %s\n",
1263 sha1_to_hex(parent->object.sha1));
1268 /* Person/date information */
1270 author = git_author_info(IDENT_STRICT);
1271 strbuf_addf(&buffer, "author %s\n", author);
1272 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
1273 if (!encoding_is_utf8)
1274 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
1277 add_extra_header(&buffer, extra);
1278 extra = extra->next;
1280 strbuf_addch(&buffer, '\n');
1282 /* And add the comment */
1283 strbuf_addbuf(&buffer, msg);
1285 /* And check the encoding */
1286 if (encoding_is_utf8 && !verify_utf8(&buffer))
1287 fprintf(stderr, commit_utf8_warn);
1289 if (sign_commit && do_sign_commit(&buffer, sign_commit))
1292 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
1293 strbuf_release(&buffer);
1297 struct commit *get_merge_parent(const char *name)
1300 struct commit *commit;
1301 unsigned char sha1[20];
1302 if (get_sha1(name, sha1))
1304 obj = parse_object(sha1);
1305 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1306 if (commit && !commit->util) {
1307 struct merge_remote_desc *desc;
1308 desc = xmalloc(sizeof(*desc));
1310 desc->name = strdup(name);
1311 commit->util = desc;
1317 * Append a commit to the end of the commit_list.
1319 * next starts by pointing to the variable that holds the head of an
1320 * empty commit_list, and is updated to point to the "next" field of
1321 * the last item on the list as new commits are appended.
1325 * struct commit_list *list;
1326 * struct commit_list **next = &list;
1328 * next = commit_list_append(c1, next);
1329 * next = commit_list_append(c2, next);
1330 * assert(commit_list_count(list) == 2);
1333 struct commit_list **commit_list_append(struct commit *commit,
1334 struct commit_list **next)
1336 struct commit_list *new = xmalloc(sizeof(struct commit_list));