Merge branch 'sn/diff-doc'
[git/dkf.git] / commit.c
blob2d9265d9fe1e13c0295f5ff33c0cb26201f5fce0
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 static unsigned long parse_commit_date(const char *buf, const char *tail)
54 const char *dateptr;
56 if (buf + 6 >= tail)
57 return 0;
58 if (memcmp(buf, "author", 6))
59 return 0;
60 while (buf < tail && *buf++ != '\n')
61 /* nada */;
62 if (buf + 9 >= tail)
63 return 0;
64 if (memcmp(buf, "committer", 9))
65 return 0;
66 while (buf < tail && *buf++ != '>')
67 /* nada */;
68 if (buf >= tail)
69 return 0;
70 dateptr = buf;
71 while (buf < tail && *buf++ != '\n')
72 /* nada */;
73 if (buf >= tail)
74 return 0;
75 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
76 return strtoul(dateptr, NULL, 10);
79 static struct commit_graft **commit_graft;
80 static int commit_graft_alloc, commit_graft_nr;
82 static int commit_graft_pos(const unsigned char *sha1)
84 int lo, hi;
85 lo = 0;
86 hi = commit_graft_nr;
87 while (lo < hi) {
88 int mi = (lo + hi) / 2;
89 struct commit_graft *graft = commit_graft[mi];
90 int cmp = hashcmp(sha1, graft->sha1);
91 if (!cmp)
92 return mi;
93 if (cmp < 0)
94 hi = mi;
95 else
96 lo = mi + 1;
98 return -lo - 1;
101 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
103 int pos = commit_graft_pos(graft->sha1);
105 if (0 <= pos) {
106 if (ignore_dups)
107 free(graft);
108 else {
109 free(commit_graft[pos]);
110 commit_graft[pos] = graft;
112 return 1;
114 pos = -pos - 1;
115 if (commit_graft_alloc <= ++commit_graft_nr) {
116 commit_graft_alloc = alloc_nr(commit_graft_alloc);
117 commit_graft = xrealloc(commit_graft,
118 sizeof(*commit_graft) *
119 commit_graft_alloc);
121 if (pos < commit_graft_nr)
122 memmove(commit_graft + pos + 1,
123 commit_graft + pos,
124 (commit_graft_nr - pos - 1) *
125 sizeof(*commit_graft));
126 commit_graft[pos] = graft;
127 return 0;
130 struct commit_graft *read_graft_line(char *buf, int len)
132 /* The format is just "Commit Parent1 Parent2 ...\n" */
133 int i;
134 struct commit_graft *graft = NULL;
136 while (len && isspace(buf[len-1]))
137 buf[--len] = '\0';
138 if (buf[0] == '#' || buf[0] == '\0')
139 return NULL;
140 if ((len + 1) % 41)
141 goto bad_graft_data;
142 i = (len + 1) / 41 - 1;
143 graft = xmalloc(sizeof(*graft) + 20 * i);
144 graft->nr_parent = i;
145 if (get_sha1_hex(buf, graft->sha1))
146 goto bad_graft_data;
147 for (i = 40; i < len; i += 41) {
148 if (buf[i] != ' ')
149 goto bad_graft_data;
150 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
151 goto bad_graft_data;
153 return graft;
155 bad_graft_data:
156 error("bad graft data: %s", buf);
157 free(graft);
158 return NULL;
161 static int read_graft_file(const char *graft_file)
163 FILE *fp = fopen(graft_file, "r");
164 char buf[1024];
165 if (!fp)
166 return -1;
167 while (fgets(buf, sizeof(buf), fp)) {
168 /* The format is just "Commit Parent1 Parent2 ...\n" */
169 int len = strlen(buf);
170 struct commit_graft *graft = read_graft_line(buf, len);
171 if (!graft)
172 continue;
173 if (register_commit_graft(graft, 1))
174 error("duplicate graft data: %s", buf);
176 fclose(fp);
177 return 0;
180 static void prepare_commit_graft(void)
182 static int commit_graft_prepared;
183 char *graft_file;
185 if (commit_graft_prepared)
186 return;
187 graft_file = get_graft_file();
188 read_graft_file(graft_file);
189 /* make sure shallows are read */
190 is_repository_shallow();
191 commit_graft_prepared = 1;
194 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
196 int pos;
197 prepare_commit_graft();
198 pos = commit_graft_pos(sha1);
199 if (pos < 0)
200 return NULL;
201 return commit_graft[pos];
204 int write_shallow_commits(struct strbuf *out, int use_pack_protocol)
206 int i, count = 0;
207 for (i = 0; i < commit_graft_nr; i++)
208 if (commit_graft[i]->nr_parent < 0) {
209 const char *hex =
210 sha1_to_hex(commit_graft[i]->sha1);
211 count++;
212 if (use_pack_protocol)
213 packet_buf_write(out, "shallow %s", hex);
214 else {
215 strbuf_addstr(out, hex);
216 strbuf_addch(out, '\n');
219 return count;
222 int unregister_shallow(const unsigned char *sha1)
224 int pos = commit_graft_pos(sha1);
225 if (pos < 0)
226 return -1;
227 if (pos + 1 < commit_graft_nr)
228 memmove(commit_graft + pos, commit_graft + pos + 1,
229 sizeof(struct commit_graft *)
230 * (commit_graft_nr - pos - 1));
231 commit_graft_nr--;
232 return 0;
235 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
237 char *tail = buffer;
238 char *bufptr = buffer;
239 unsigned char parent[20];
240 struct commit_list **pptr;
241 struct commit_graft *graft;
243 if (item->object.parsed)
244 return 0;
245 item->object.parsed = 1;
246 tail += size;
247 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
248 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
249 if (get_sha1_hex(bufptr + 5, parent) < 0)
250 return error("bad tree pointer in commit %s",
251 sha1_to_hex(item->object.sha1));
252 item->tree = lookup_tree(parent);
253 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
254 pptr = &item->parents;
256 graft = lookup_commit_graft(item->object.sha1);
257 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
258 struct commit *new_parent;
260 if (tail <= bufptr + 48 ||
261 get_sha1_hex(bufptr + 7, parent) ||
262 bufptr[47] != '\n')
263 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
264 bufptr += 48;
266 * The clone is shallow if nr_parent < 0, and we must
267 * not traverse its real parents even when we unhide them.
269 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
270 continue;
271 new_parent = lookup_commit(parent);
272 if (new_parent)
273 pptr = &commit_list_insert(new_parent, pptr)->next;
275 if (graft) {
276 int i;
277 struct commit *new_parent;
278 for (i = 0; i < graft->nr_parent; i++) {
279 new_parent = lookup_commit(graft->parent[i]);
280 if (!new_parent)
281 continue;
282 pptr = &commit_list_insert(new_parent, pptr)->next;
285 item->date = parse_commit_date(bufptr, tail);
287 return 0;
290 int parse_commit(struct commit *item)
292 enum object_type type;
293 void *buffer;
294 unsigned long size;
295 int ret;
297 if (!item)
298 return -1;
299 if (item->object.parsed)
300 return 0;
301 buffer = read_sha1_file(item->object.sha1, &type, &size);
302 if (!buffer)
303 return error("Could not read %s",
304 sha1_to_hex(item->object.sha1));
305 if (type != OBJ_COMMIT) {
306 free(buffer);
307 return error("Object %s not a commit",
308 sha1_to_hex(item->object.sha1));
310 ret = parse_commit_buffer(item, buffer, size);
311 if (save_commit_buffer && !ret) {
312 item->buffer = buffer;
313 return 0;
315 free(buffer);
316 return ret;
319 int find_commit_subject(const char *commit_buffer, const char **subject)
321 const char *eol;
322 const char *p = commit_buffer;
324 while (*p && (*p != '\n' || p[1] != '\n'))
325 p++;
326 if (*p) {
327 p += 2;
328 for (eol = p; *eol && *eol != '\n'; eol++)
329 ; /* do nothing */
330 } else
331 eol = p;
333 *subject = p;
335 return eol - p;
338 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
340 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
341 new_list->item = item;
342 new_list->next = *list_p;
343 *list_p = new_list;
344 return new_list;
347 unsigned commit_list_count(const struct commit_list *l)
349 unsigned c = 0;
350 for (; l; l = l->next )
351 c++;
352 return c;
355 void free_commit_list(struct commit_list *list)
357 while (list) {
358 struct commit_list *temp = list;
359 list = temp->next;
360 free(temp);
364 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
366 struct commit_list **pp = list;
367 struct commit_list *p;
368 while ((p = *pp) != NULL) {
369 if (p->item->date < item->date) {
370 break;
372 pp = &p->next;
374 return commit_list_insert(item, pp);
378 void sort_by_date(struct commit_list **list)
380 struct commit_list *ret = NULL;
381 while (*list) {
382 insert_by_date((*list)->item, &ret);
383 *list = (*list)->next;
385 *list = ret;
388 struct commit *pop_most_recent_commit(struct commit_list **list,
389 unsigned int mark)
391 struct commit *ret = (*list)->item;
392 struct commit_list *parents = ret->parents;
393 struct commit_list *old = *list;
395 *list = (*list)->next;
396 free(old);
398 while (parents) {
399 struct commit *commit = parents->item;
400 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
401 commit->object.flags |= mark;
402 insert_by_date(commit, list);
404 parents = parents->next;
406 return ret;
409 void clear_commit_marks(struct commit *commit, unsigned int mark)
411 while (commit) {
412 struct commit_list *parents;
414 if (!(mark & commit->object.flags))
415 return;
417 commit->object.flags &= ~mark;
419 parents = commit->parents;
420 if (!parents)
421 return;
423 while ((parents = parents->next))
424 clear_commit_marks(parents->item, mark);
426 commit = commit->parents->item;
430 struct commit *pop_commit(struct commit_list **stack)
432 struct commit_list *top = *stack;
433 struct commit *item = top ? top->item : NULL;
435 if (top) {
436 *stack = top->next;
437 free(top);
439 return item;
443 * Performs an in-place topological sort on the list supplied.
445 void sort_in_topological_order(struct commit_list ** list, int lifo)
447 struct commit_list *next, *orig = *list;
448 struct commit_list *work, **insert;
449 struct commit_list **pptr;
451 if (!orig)
452 return;
453 *list = NULL;
455 /* Mark them and clear the indegree */
456 for (next = orig; next; next = next->next) {
457 struct commit *commit = next->item;
458 commit->indegree = 1;
461 /* update the indegree */
462 for (next = orig; next; next = next->next) {
463 struct commit_list * parents = next->item->parents;
464 while (parents) {
465 struct commit *parent = parents->item;
467 if (parent->indegree)
468 parent->indegree++;
469 parents = parents->next;
474 * find the tips
476 * tips are nodes not reachable from any other node in the list
478 * the tips serve as a starting set for the work queue.
480 work = NULL;
481 insert = &work;
482 for (next = orig; next; next = next->next) {
483 struct commit *commit = next->item;
485 if (commit->indegree == 1)
486 insert = &commit_list_insert(commit, insert)->next;
489 /* process the list in topological order */
490 if (!lifo)
491 sort_by_date(&work);
493 pptr = list;
494 *list = NULL;
495 while (work) {
496 struct commit *commit;
497 struct commit_list *parents, *work_item;
499 work_item = work;
500 work = work_item->next;
501 work_item->next = NULL;
503 commit = work_item->item;
504 for (parents = commit->parents; parents ; parents = parents->next) {
505 struct commit *parent=parents->item;
507 if (!parent->indegree)
508 continue;
511 * parents are only enqueued for emission
512 * when all their children have been emitted thereby
513 * guaranteeing topological order.
515 if (--parent->indegree == 1) {
516 if (!lifo)
517 insert_by_date(parent, &work);
518 else
519 commit_list_insert(parent, &work);
523 * work_item is a commit all of whose children
524 * have already been emitted. we can emit it now.
526 commit->indegree = 0;
527 *pptr = work_item;
528 pptr = &work_item->next;
532 /* merge-base stuff */
534 /* bits #0..15 in revision.h */
535 #define PARENT1 (1u<<16)
536 #define PARENT2 (1u<<17)
537 #define STALE (1u<<18)
538 #define RESULT (1u<<19)
540 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
542 static struct commit *interesting(struct commit_list *list)
544 while (list) {
545 struct commit *commit = list->item;
546 list = list->next;
547 if (commit->object.flags & STALE)
548 continue;
549 return commit;
551 return NULL;
554 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
556 struct commit_list *list = NULL;
557 struct commit_list *result = NULL;
558 int i;
560 for (i = 0; i < n; i++) {
561 if (one == twos[i])
563 * We do not mark this even with RESULT so we do not
564 * have to clean it up.
566 return commit_list_insert(one, &result);
569 if (parse_commit(one))
570 return NULL;
571 for (i = 0; i < n; i++) {
572 if (parse_commit(twos[i]))
573 return NULL;
576 one->object.flags |= PARENT1;
577 insert_by_date(one, &list);
578 for (i = 0; i < n; i++) {
579 twos[i]->object.flags |= PARENT2;
580 insert_by_date(twos[i], &list);
583 while (interesting(list)) {
584 struct commit *commit;
585 struct commit_list *parents;
586 struct commit_list *next;
587 int flags;
589 commit = list->item;
590 next = list->next;
591 free(list);
592 list = next;
594 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
595 if (flags == (PARENT1 | PARENT2)) {
596 if (!(commit->object.flags & RESULT)) {
597 commit->object.flags |= RESULT;
598 insert_by_date(commit, &result);
600 /* Mark parents of a found merge stale */
601 flags |= STALE;
603 parents = commit->parents;
604 while (parents) {
605 struct commit *p = parents->item;
606 parents = parents->next;
607 if ((p->object.flags & flags) == flags)
608 continue;
609 if (parse_commit(p))
610 return NULL;
611 p->object.flags |= flags;
612 insert_by_date(p, &list);
616 /* Clean up the result to remove stale ones */
617 free_commit_list(list);
618 list = result; result = NULL;
619 while (list) {
620 struct commit_list *next = list->next;
621 if (!(list->item->object.flags & STALE))
622 insert_by_date(list->item, &result);
623 free(list);
624 list = next;
626 return result;
629 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
631 struct commit_list *i, *j, *k, *ret = NULL;
632 struct commit_list **pptr = &ret;
634 for (i = in; i; i = i->next) {
635 if (!ret)
636 pptr = &commit_list_insert(i->item, pptr)->next;
637 else {
638 struct commit_list *new = NULL, *end = NULL;
640 for (j = ret; j; j = j->next) {
641 struct commit_list *bases;
642 bases = get_merge_bases(i->item, j->item, 1);
643 if (!new)
644 new = bases;
645 else
646 end->next = bases;
647 for (k = bases; k; k = k->next)
648 end = k;
650 ret = new;
653 return ret;
656 struct commit_list *get_merge_bases_many(struct commit *one,
657 int n,
658 struct commit **twos,
659 int cleanup)
661 struct commit_list *list;
662 struct commit **rslt;
663 struct commit_list *result;
664 int cnt, i, j;
666 result = merge_bases_many(one, n, twos);
667 for (i = 0; i < n; i++) {
668 if (one == twos[i])
669 return result;
671 if (!result || !result->next) {
672 if (cleanup) {
673 clear_commit_marks(one, all_flags);
674 for (i = 0; i < n; i++)
675 clear_commit_marks(twos[i], all_flags);
677 return result;
680 /* There are more than one */
681 cnt = 0;
682 list = result;
683 while (list) {
684 list = list->next;
685 cnt++;
687 rslt = xcalloc(cnt, sizeof(*rslt));
688 for (list = result, i = 0; list; list = list->next)
689 rslt[i++] = list->item;
690 free_commit_list(result);
692 clear_commit_marks(one, all_flags);
693 for (i = 0; i < n; i++)
694 clear_commit_marks(twos[i], all_flags);
695 for (i = 0; i < cnt - 1; i++) {
696 for (j = i+1; j < cnt; j++) {
697 if (!rslt[i] || !rslt[j])
698 continue;
699 result = merge_bases_many(rslt[i], 1, &rslt[j]);
700 clear_commit_marks(rslt[i], all_flags);
701 clear_commit_marks(rslt[j], all_flags);
702 for (list = result; list; list = list->next) {
703 if (rslt[i] == list->item)
704 rslt[i] = NULL;
705 if (rslt[j] == list->item)
706 rslt[j] = NULL;
711 /* Surviving ones in rslt[] are the independent results */
712 result = NULL;
713 for (i = 0; i < cnt; i++) {
714 if (rslt[i])
715 insert_by_date(rslt[i], &result);
717 free(rslt);
718 return result;
721 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
722 int cleanup)
724 return get_merge_bases_many(one, 1, &two, cleanup);
727 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
729 if (!with_commit)
730 return 1;
731 while (with_commit) {
732 struct commit *other;
734 other = with_commit->item;
735 with_commit = with_commit->next;
736 if (in_merge_bases(other, &commit, 1))
737 return 1;
739 return 0;
742 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
744 struct commit_list *bases, *b;
745 int ret = 0;
747 if (num == 1)
748 bases = get_merge_bases(commit, *reference, 1);
749 else
750 die("not yet");
751 for (b = bases; b; b = b->next) {
752 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
753 ret = 1;
754 break;
758 free_commit_list(bases);
759 return ret;
762 struct commit_list *reduce_heads(struct commit_list *heads)
764 struct commit_list *p;
765 struct commit_list *result = NULL, **tail = &result;
766 struct commit **other;
767 size_t num_head, num_other;
769 if (!heads)
770 return NULL;
772 /* Avoid unnecessary reallocations */
773 for (p = heads, num_head = 0; p; p = p->next)
774 num_head++;
775 other = xcalloc(sizeof(*other), num_head);
777 /* For each commit, see if it can be reached by others */
778 for (p = heads; p; p = p->next) {
779 struct commit_list *q, *base;
781 /* Do we already have this in the result? */
782 for (q = result; q; q = q->next)
783 if (p->item == q->item)
784 break;
785 if (q)
786 continue;
788 num_other = 0;
789 for (q = heads; q; q = q->next) {
790 if (p->item == q->item)
791 continue;
792 other[num_other++] = q->item;
794 if (num_other)
795 base = get_merge_bases_many(p->item, num_other, other, 1);
796 else
797 base = NULL;
799 * If p->item does not have anything common with other
800 * commits, there won't be any merge base. If it is
801 * reachable from some of the others, p->item will be
802 * the merge base. If its history is connected with
803 * others, but p->item is not reachable by others, we
804 * will get something other than p->item back.
806 if (!base || (base->item != p->item))
807 tail = &(commit_list_insert(p->item, tail)->next);
808 free_commit_list(base);
810 free(other);
811 return result;
814 static const char commit_utf8_warn[] =
815 "Warning: commit message does not conform to UTF-8.\n"
816 "You may want to amend it after fixing the message, or set the config\n"
817 "variable i18n.commitencoding to the encoding your project uses.\n";
819 int commit_tree(const char *msg, unsigned char *tree,
820 struct commit_list *parents, unsigned char *ret,
821 const char *author)
823 int result;
824 int encoding_is_utf8;
825 struct strbuf buffer;
827 assert_sha1_type(tree, OBJ_TREE);
829 /* Not having i18n.commitencoding is the same as having utf-8 */
830 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
832 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
833 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
836 * NOTE! This ordering means that the same exact tree merged with a
837 * different order of parents will be a _different_ changeset even
838 * if everything else stays the same.
840 while (parents) {
841 struct commit_list *next = parents->next;
842 strbuf_addf(&buffer, "parent %s\n",
843 sha1_to_hex(parents->item->object.sha1));
844 free(parents);
845 parents = next;
848 /* Person/date information */
849 if (!author)
850 author = git_author_info(IDENT_ERROR_ON_NO_NAME);
851 strbuf_addf(&buffer, "author %s\n", author);
852 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
853 if (!encoding_is_utf8)
854 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
855 strbuf_addch(&buffer, '\n');
857 /* And add the comment */
858 strbuf_addstr(&buffer, msg);
860 /* And check the encoding */
861 if (encoding_is_utf8 && !is_utf8(buffer.buf))
862 fprintf(stderr, commit_utf8_warn);
864 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
865 strbuf_release(&buffer);
866 return result;