Merge branch 'sn/diff-doc' into next
[git/dscho.git] / commit.c
blob5ed9ccd723b9992e9d331df8d7ad6122fd661a5f
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(const unsigned char *sha1)
44 struct object *obj = lookup_object(sha1);
45 if (!obj)
46 return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
47 if (!obj->type)
48 obj->type = OBJ_COMMIT;
49 return check_commit(obj, sha1, 0);
52 struct commit *lookup_commit_reference_by_name(const char *name)
54 unsigned char sha1[20];
55 struct commit *commit;
57 if (get_sha1(name, sha1))
58 return NULL;
59 commit = lookup_commit_reference(sha1);
60 if (!commit || parse_commit(commit))
61 return NULL;
62 return commit;
65 static unsigned long parse_commit_date(const char *buf, const char *tail)
67 const char *dateptr;
69 if (buf + 6 >= tail)
70 return 0;
71 if (memcmp(buf, "author", 6))
72 return 0;
73 while (buf < tail && *buf++ != '\n')
74 /* nada */;
75 if (buf + 9 >= tail)
76 return 0;
77 if (memcmp(buf, "committer", 9))
78 return 0;
79 while (buf < tail && *buf++ != '>')
80 /* nada */;
81 if (buf >= tail)
82 return 0;
83 dateptr = buf;
84 while (buf < tail && *buf++ != '\n')
85 /* nada */;
86 if (buf >= tail)
87 return 0;
88 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
89 return strtoul(dateptr, NULL, 10);
92 static struct commit_graft **commit_graft;
93 static int commit_graft_alloc, commit_graft_nr;
95 static int commit_graft_pos(const unsigned char *sha1)
97 int lo, hi;
98 lo = 0;
99 hi = commit_graft_nr;
100 while (lo < hi) {
101 int mi = (lo + hi) / 2;
102 struct commit_graft *graft = commit_graft[mi];
103 int cmp = hashcmp(sha1, graft->sha1);
104 if (!cmp)
105 return mi;
106 if (cmp < 0)
107 hi = mi;
108 else
109 lo = mi + 1;
111 return -lo - 1;
114 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
116 int pos = commit_graft_pos(graft->sha1);
118 if (0 <= pos) {
119 if (ignore_dups)
120 free(graft);
121 else {
122 free(commit_graft[pos]);
123 commit_graft[pos] = graft;
125 return 1;
127 pos = -pos - 1;
128 if (commit_graft_alloc <= ++commit_graft_nr) {
129 commit_graft_alloc = alloc_nr(commit_graft_alloc);
130 commit_graft = xrealloc(commit_graft,
131 sizeof(*commit_graft) *
132 commit_graft_alloc);
134 if (pos < commit_graft_nr)
135 memmove(commit_graft + pos + 1,
136 commit_graft + pos,
137 (commit_graft_nr - pos - 1) *
138 sizeof(*commit_graft));
139 commit_graft[pos] = graft;
140 return 0;
143 struct commit_graft *read_graft_line(char *buf, int len)
145 /* The format is just "Commit Parent1 Parent2 ...\n" */
146 int i;
147 struct commit_graft *graft = NULL;
149 while (len && isspace(buf[len-1]))
150 buf[--len] = '\0';
151 if (buf[0] == '#' || buf[0] == '\0')
152 return NULL;
153 if ((len + 1) % 41) {
154 bad_graft_data:
155 error("bad graft data: %s", buf);
156 free(graft);
157 return NULL;
159 i = (len + 1) / 41 - 1;
160 graft = xmalloc(sizeof(*graft) + 20 * i);
161 graft->nr_parent = i;
162 if (get_sha1_hex(buf, graft->sha1))
163 goto bad_graft_data;
164 for (i = 40; i < len; i += 41) {
165 if (buf[i] != ' ')
166 goto bad_graft_data;
167 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
168 goto bad_graft_data;
170 return graft;
173 static int read_graft_file(const char *graft_file)
175 FILE *fp = fopen(graft_file, "r");
176 char buf[1024];
177 if (!fp)
178 return -1;
179 while (fgets(buf, sizeof(buf), fp)) {
180 /* The format is just "Commit Parent1 Parent2 ...\n" */
181 int len = strlen(buf);
182 struct commit_graft *graft = read_graft_line(buf, len);
183 if (!graft)
184 continue;
185 if (register_commit_graft(graft, 1))
186 error("duplicate graft data: %s", buf);
188 fclose(fp);
189 return 0;
192 static void prepare_commit_graft(void)
194 static int commit_graft_prepared;
195 char *graft_file;
197 if (commit_graft_prepared)
198 return;
199 graft_file = get_graft_file();
200 read_graft_file(graft_file);
201 /* make sure shallows are read */
202 is_repository_shallow();
203 commit_graft_prepared = 1;
206 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
208 int pos;
209 prepare_commit_graft();
210 pos = commit_graft_pos(sha1);
211 if (pos < 0)
212 return NULL;
213 return commit_graft[pos];
216 int write_shallow_commits(struct strbuf *out, int use_pack_protocol)
218 int i, count = 0;
219 for (i = 0; i < commit_graft_nr; i++)
220 if (commit_graft[i]->nr_parent < 0) {
221 const char *hex =
222 sha1_to_hex(commit_graft[i]->sha1);
223 count++;
224 if (use_pack_protocol)
225 packet_buf_write(out, "shallow %s", hex);
226 else {
227 strbuf_addstr(out, hex);
228 strbuf_addch(out, '\n');
231 return count;
234 int unregister_shallow(const unsigned char *sha1)
236 int pos = commit_graft_pos(sha1);
237 if (pos < 0)
238 return -1;
239 if (pos + 1 < commit_graft_nr)
240 memmove(commit_graft + pos, commit_graft + pos + 1,
241 sizeof(struct commit_graft *)
242 * (commit_graft_nr - pos - 1));
243 commit_graft_nr--;
244 return 0;
247 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
249 char *tail = buffer;
250 char *bufptr = buffer;
251 unsigned char parent[20];
252 struct commit_list **pptr;
253 struct commit_graft *graft;
255 if (item->object.parsed)
256 return 0;
257 item->object.parsed = 1;
258 tail += size;
259 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
260 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
261 if (get_sha1_hex(bufptr + 5, parent) < 0)
262 return error("bad tree pointer in commit %s",
263 sha1_to_hex(item->object.sha1));
264 item->tree = lookup_tree(parent);
265 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
266 pptr = &item->parents;
268 graft = lookup_commit_graft(item->object.sha1);
269 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
270 struct commit *new_parent;
272 if (tail <= bufptr + 48 ||
273 get_sha1_hex(bufptr + 7, parent) ||
274 bufptr[47] != '\n')
275 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
276 bufptr += 48;
278 * The clone is shallow if nr_parent < 0, and we must
279 * not traverse its real parents even when we unhide them.
281 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
282 continue;
283 new_parent = lookup_commit(parent);
284 if (new_parent)
285 pptr = &commit_list_insert(new_parent, pptr)->next;
287 if (graft) {
288 int i;
289 struct commit *new_parent;
290 for (i = 0; i < graft->nr_parent; i++) {
291 new_parent = lookup_commit(graft->parent[i]);
292 if (!new_parent)
293 continue;
294 pptr = &commit_list_insert(new_parent, pptr)->next;
297 item->date = parse_commit_date(bufptr, tail);
299 return 0;
302 int parse_commit(struct commit *item)
304 enum object_type type;
305 void *buffer;
306 unsigned long size;
307 int ret;
309 if (!item)
310 return -1;
311 if (item->object.parsed)
312 return 0;
313 buffer = read_sha1_file(item->object.sha1, &type, &size);
314 if (!buffer)
315 return error("Could not read %s",
316 sha1_to_hex(item->object.sha1));
317 if (type != OBJ_COMMIT) {
318 free(buffer);
319 return error("Object %s not a commit",
320 sha1_to_hex(item->object.sha1));
322 ret = parse_commit_buffer(item, buffer, size);
323 if (save_commit_buffer && !ret) {
324 item->buffer = buffer;
325 return 0;
327 free(buffer);
328 return ret;
331 int find_commit_subject(const char *commit_buffer, const char **subject)
333 const char *eol;
334 const char *p = commit_buffer;
336 while (*p && (*p != '\n' || p[1] != '\n'))
337 p++;
338 if (*p) {
339 p += 2;
340 for (eol = p; *eol && *eol != '\n'; eol++)
341 ; /* do nothing */
342 } else
343 eol = p;
345 *subject = p;
347 return eol - p;
350 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
352 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
353 new_list->item = item;
354 new_list->next = *list_p;
355 *list_p = new_list;
356 return new_list;
359 unsigned commit_list_count(const struct commit_list *l)
361 unsigned c = 0;
362 for (; l; l = l->next )
363 c++;
364 return c;
367 void free_commit_list(struct commit_list *list)
369 while (list) {
370 struct commit_list *temp = list;
371 list = temp->next;
372 free(temp);
376 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
378 struct commit_list **pp = list;
379 struct commit_list *p;
380 while ((p = *pp) != NULL) {
381 if (p->item->date < item->date) {
382 break;
384 pp = &p->next;
386 return commit_list_insert(item, pp);
390 void sort_by_date(struct commit_list **list)
392 struct commit_list *ret = NULL;
393 while (*list) {
394 insert_by_date((*list)->item, &ret);
395 *list = (*list)->next;
397 *list = ret;
400 struct commit *pop_most_recent_commit(struct commit_list **list,
401 unsigned int mark)
403 struct commit *ret = (*list)->item;
404 struct commit_list *parents = ret->parents;
405 struct commit_list *old = *list;
407 *list = (*list)->next;
408 free(old);
410 while (parents) {
411 struct commit *commit = parents->item;
412 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
413 commit->object.flags |= mark;
414 insert_by_date(commit, list);
416 parents = parents->next;
418 return ret;
421 void clear_commit_marks(struct commit *commit, unsigned int mark)
423 while (commit) {
424 struct commit_list *parents;
426 if (!(mark & commit->object.flags))
427 return;
429 commit->object.flags &= ~mark;
431 parents = commit->parents;
432 if (!parents)
433 return;
435 while ((parents = parents->next))
436 clear_commit_marks(parents->item, mark);
438 commit = commit->parents->item;
442 struct commit *pop_commit(struct commit_list **stack)
444 struct commit_list *top = *stack;
445 struct commit *item = top ? top->item : NULL;
447 if (top) {
448 *stack = top->next;
449 free(top);
451 return item;
455 * Performs an in-place topological sort on the list supplied.
457 void sort_in_topological_order(struct commit_list ** list, int lifo)
459 struct commit_list *next, *orig = *list;
460 struct commit_list *work, **insert;
461 struct commit_list **pptr;
463 if (!orig)
464 return;
465 *list = NULL;
467 /* Mark them and clear the indegree */
468 for (next = orig; next; next = next->next) {
469 struct commit *commit = next->item;
470 commit->indegree = 1;
473 /* update the indegree */
474 for (next = orig; next; next = next->next) {
475 struct commit_list * parents = next->item->parents;
476 while (parents) {
477 struct commit *parent = parents->item;
479 if (parent->indegree)
480 parent->indegree++;
481 parents = parents->next;
486 * find the tips
488 * tips are nodes not reachable from any other node in the list
490 * the tips serve as a starting set for the work queue.
492 work = NULL;
493 insert = &work;
494 for (next = orig; next; next = next->next) {
495 struct commit *commit = next->item;
497 if (commit->indegree == 1)
498 insert = &commit_list_insert(commit, insert)->next;
501 /* process the list in topological order */
502 if (!lifo)
503 sort_by_date(&work);
505 pptr = list;
506 *list = NULL;
507 while (work) {
508 struct commit *commit;
509 struct commit_list *parents, *work_item;
511 work_item = work;
512 work = work_item->next;
513 work_item->next = NULL;
515 commit = work_item->item;
516 for (parents = commit->parents; parents ; parents = parents->next) {
517 struct commit *parent=parents->item;
519 if (!parent->indegree)
520 continue;
523 * parents are only enqueued for emission
524 * when all their children have been emitted thereby
525 * guaranteeing topological order.
527 if (--parent->indegree == 1) {
528 if (!lifo)
529 insert_by_date(parent, &work);
530 else
531 commit_list_insert(parent, &work);
535 * work_item is a commit all of whose children
536 * have already been emitted. we can emit it now.
538 commit->indegree = 0;
539 *pptr = work_item;
540 pptr = &work_item->next;
544 /* merge-base stuff */
546 /* bits #0..15 in revision.h */
547 #define PARENT1 (1u<<16)
548 #define PARENT2 (1u<<17)
549 #define STALE (1u<<18)
550 #define RESULT (1u<<19)
552 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
554 static struct commit *interesting(struct commit_list *list)
556 while (list) {
557 struct commit *commit = list->item;
558 list = list->next;
559 if (commit->object.flags & STALE)
560 continue;
561 return commit;
563 return NULL;
566 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
568 struct commit_list *list = NULL;
569 struct commit_list *result = NULL;
570 int i;
572 for (i = 0; i < n; i++) {
573 if (one == twos[i])
575 * We do not mark this even with RESULT so we do not
576 * have to clean it up.
578 return commit_list_insert(one, &result);
581 if (parse_commit(one))
582 return NULL;
583 for (i = 0; i < n; i++) {
584 if (parse_commit(twos[i]))
585 return NULL;
588 one->object.flags |= PARENT1;
589 insert_by_date(one, &list);
590 for (i = 0; i < n; i++) {
591 twos[i]->object.flags |= PARENT2;
592 insert_by_date(twos[i], &list);
595 while (interesting(list)) {
596 struct commit *commit;
597 struct commit_list *parents;
598 struct commit_list *next;
599 int flags;
601 commit = list->item;
602 next = list->next;
603 free(list);
604 list = next;
606 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
607 if (flags == (PARENT1 | PARENT2)) {
608 if (!(commit->object.flags & RESULT)) {
609 commit->object.flags |= RESULT;
610 insert_by_date(commit, &result);
612 /* Mark parents of a found merge stale */
613 flags |= STALE;
615 parents = commit->parents;
616 while (parents) {
617 struct commit *p = parents->item;
618 parents = parents->next;
619 if ((p->object.flags & flags) == flags)
620 continue;
621 if (parse_commit(p))
622 return NULL;
623 p->object.flags |= flags;
624 insert_by_date(p, &list);
628 /* Clean up the result to remove stale ones */
629 free_commit_list(list);
630 list = result; result = NULL;
631 while (list) {
632 struct commit_list *next = list->next;
633 if (!(list->item->object.flags & STALE))
634 insert_by_date(list->item, &result);
635 free(list);
636 list = next;
638 return result;
641 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
643 struct commit_list *i, *j, *k, *ret = NULL;
644 struct commit_list **pptr = &ret;
646 for (i = in; i; i = i->next) {
647 if (!ret)
648 pptr = &commit_list_insert(i->item, pptr)->next;
649 else {
650 struct commit_list *new = NULL, *end = NULL;
652 for (j = ret; j; j = j->next) {
653 struct commit_list *bases;
654 bases = get_merge_bases(i->item, j->item, 1);
655 if (!new)
656 new = bases;
657 else
658 end->next = bases;
659 for (k = bases; k; k = k->next)
660 end = k;
662 ret = new;
665 return ret;
668 struct commit_list *get_merge_bases_many(struct commit *one,
669 int n,
670 struct commit **twos,
671 int cleanup)
673 struct commit_list *list;
674 struct commit **rslt;
675 struct commit_list *result;
676 int cnt, i, j;
678 result = merge_bases_many(one, n, twos);
679 for (i = 0; i < n; i++) {
680 if (one == twos[i])
681 return result;
683 if (!result || !result->next) {
684 if (cleanup) {
685 clear_commit_marks(one, all_flags);
686 for (i = 0; i < n; i++)
687 clear_commit_marks(twos[i], all_flags);
689 return result;
692 /* There are more than one */
693 cnt = 0;
694 list = result;
695 while (list) {
696 list = list->next;
697 cnt++;
699 rslt = xcalloc(cnt, sizeof(*rslt));
700 for (list = result, i = 0; list; list = list->next)
701 rslt[i++] = list->item;
702 free_commit_list(result);
704 clear_commit_marks(one, all_flags);
705 for (i = 0; i < n; i++)
706 clear_commit_marks(twos[i], all_flags);
707 for (i = 0; i < cnt - 1; i++) {
708 for (j = i+1; j < cnt; j++) {
709 if (!rslt[i] || !rslt[j])
710 continue;
711 result = merge_bases_many(rslt[i], 1, &rslt[j]);
712 clear_commit_marks(rslt[i], all_flags);
713 clear_commit_marks(rslt[j], all_flags);
714 for (list = result; list; list = list->next) {
715 if (rslt[i] == list->item)
716 rslt[i] = NULL;
717 if (rslt[j] == list->item)
718 rslt[j] = NULL;
723 /* Surviving ones in rslt[] are the independent results */
724 result = NULL;
725 for (i = 0; i < cnt; i++) {
726 if (rslt[i])
727 insert_by_date(rslt[i], &result);
729 free(rslt);
730 return result;
733 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
734 int cleanup)
736 return get_merge_bases_many(one, 1, &two, cleanup);
739 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
741 if (!with_commit)
742 return 1;
743 while (with_commit) {
744 struct commit *other;
746 other = with_commit->item;
747 with_commit = with_commit->next;
748 if (in_merge_bases(other, &commit, 1))
749 return 1;
751 return 0;
754 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
756 struct commit_list *bases, *b;
757 int ret = 0;
759 if (num == 1)
760 bases = get_merge_bases(commit, *reference, 1);
761 else
762 die("not yet");
763 for (b = bases; b; b = b->next) {
764 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
765 ret = 1;
766 break;
770 free_commit_list(bases);
771 return ret;
774 struct commit_list *reduce_heads(struct commit_list *heads)
776 struct commit_list *p;
777 struct commit_list *result = NULL, **tail = &result;
778 struct commit **other;
779 size_t num_head, num_other;
781 if (!heads)
782 return NULL;
784 /* Avoid unnecessary reallocations */
785 for (p = heads, num_head = 0; p; p = p->next)
786 num_head++;
787 other = xcalloc(sizeof(*other), num_head);
789 /* For each commit, see if it can be reached by others */
790 for (p = heads; p; p = p->next) {
791 struct commit_list *q, *base;
793 /* Do we already have this in the result? */
794 for (q = result; q; q = q->next)
795 if (p->item == q->item)
796 break;
797 if (q)
798 continue;
800 num_other = 0;
801 for (q = heads; q; q = q->next) {
802 if (p->item == q->item)
803 continue;
804 other[num_other++] = q->item;
806 if (num_other)
807 base = get_merge_bases_many(p->item, num_other, other, 1);
808 else
809 base = NULL;
811 * If p->item does not have anything common with other
812 * commits, there won't be any merge base. If it is
813 * reachable from some of the others, p->item will be
814 * the merge base. If its history is connected with
815 * others, but p->item is not reachable by others, we
816 * will get something other than p->item back.
818 if (!base || (base->item != p->item))
819 tail = &(commit_list_insert(p->item, tail)->next);
820 free_commit_list(base);
822 free(other);
823 return result;
826 static const char commit_utf8_warn[] =
827 "Warning: commit message does not conform to UTF-8.\n"
828 "You may want to amend it after fixing the message, or set the config\n"
829 "variable i18n.commitencoding to the encoding your project uses.\n";
831 int commit_tree(const char *msg, unsigned char *tree,
832 struct commit_list *parents, unsigned char *ret,
833 const char *author)
835 int result;
836 int encoding_is_utf8;
837 struct strbuf buffer;
839 assert_sha1_type(tree, OBJ_TREE);
841 /* Not having i18n.commitencoding is the same as having utf-8 */
842 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
844 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
845 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
848 * NOTE! This ordering means that the same exact tree merged with a
849 * different order of parents will be a _different_ changeset even
850 * if everything else stays the same.
852 while (parents) {
853 struct commit_list *next = parents->next;
854 strbuf_addf(&buffer, "parent %s\n",
855 sha1_to_hex(parents->item->object.sha1));
856 free(parents);
857 parents = next;
860 /* Person/date information */
861 if (!author)
862 author = git_author_info(IDENT_ERROR_ON_NO_NAME);
863 strbuf_addf(&buffer, "author %s\n", author);
864 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
865 if (!encoding_is_utf8)
866 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
867 strbuf_addch(&buffer, '\n');
869 /* And add the comment */
870 strbuf_addstr(&buffer, msg);
872 /* And check the encoding */
873 if (encoding_is_utf8 && !is_utf8(buffer.buf))
874 fprintf(stderr, commit_utf8_warn);
876 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
877 strbuf_release(&buffer);
878 return result;