clear_ref_cache(): inline function
[git/jrn.git] / commit.c
blob97b43279cdf46159462d5f56abc20f2161f4c7ea
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 goto bad_graft_data;
155 i = (len + 1) / 41 - 1;
156 graft = xmalloc(sizeof(*graft) + 20 * i);
157 graft->nr_parent = i;
158 if (get_sha1_hex(buf, graft->sha1))
159 goto bad_graft_data;
160 for (i = 40; i < len; i += 41) {
161 if (buf[i] != ' ')
162 goto bad_graft_data;
163 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
164 goto bad_graft_data;
166 return graft;
168 bad_graft_data:
169 error("bad graft data: %s", buf);
170 free(graft);
171 return NULL;
174 static int read_graft_file(const char *graft_file)
176 FILE *fp = fopen(graft_file, "r");
177 char buf[1024];
178 if (!fp)
179 return -1;
180 while (fgets(buf, sizeof(buf), fp)) {
181 /* The format is just "Commit Parent1 Parent2 ...\n" */
182 int len = strlen(buf);
183 struct commit_graft *graft = read_graft_line(buf, len);
184 if (!graft)
185 continue;
186 if (register_commit_graft(graft, 1))
187 error("duplicate graft data: %s", buf);
189 fclose(fp);
190 return 0;
193 static void prepare_commit_graft(void)
195 static int commit_graft_prepared;
196 char *graft_file;
198 if (commit_graft_prepared)
199 return;
200 graft_file = get_graft_file();
201 read_graft_file(graft_file);
202 /* make sure shallows are read */
203 is_repository_shallow();
204 commit_graft_prepared = 1;
207 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
209 int pos;
210 prepare_commit_graft();
211 pos = commit_graft_pos(sha1);
212 if (pos < 0)
213 return NULL;
214 return commit_graft[pos];
217 int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
219 int i, ret;
220 for (i = ret = 0; i < commit_graft_nr && !ret; i++)
221 ret = fn(commit_graft[i], cb_data);
222 return ret;
225 int unregister_shallow(const unsigned char *sha1)
227 int pos = commit_graft_pos(sha1);
228 if (pos < 0)
229 return -1;
230 if (pos + 1 < commit_graft_nr)
231 memmove(commit_graft + pos, commit_graft + pos + 1,
232 sizeof(struct commit_graft *)
233 * (commit_graft_nr - pos - 1));
234 commit_graft_nr--;
235 return 0;
238 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
240 const char *tail = buffer;
241 const char *bufptr = buffer;
242 unsigned char parent[20];
243 struct commit_list **pptr;
244 struct commit_graft *graft;
246 if (item->object.parsed)
247 return 0;
248 item->object.parsed = 1;
249 tail += size;
250 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
251 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
252 if (get_sha1_hex(bufptr + 5, parent) < 0)
253 return error("bad tree pointer in commit %s",
254 sha1_to_hex(item->object.sha1));
255 item->tree = lookup_tree(parent);
256 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
257 pptr = &item->parents;
259 graft = lookup_commit_graft(item->object.sha1);
260 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
261 struct commit *new_parent;
263 if (tail <= bufptr + 48 ||
264 get_sha1_hex(bufptr + 7, parent) ||
265 bufptr[47] != '\n')
266 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
267 bufptr += 48;
269 * The clone is shallow if nr_parent < 0, and we must
270 * not traverse its real parents even when we unhide them.
272 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
273 continue;
274 new_parent = lookup_commit(parent);
275 if (new_parent)
276 pptr = &commit_list_insert(new_parent, pptr)->next;
278 if (graft) {
279 int i;
280 struct commit *new_parent;
281 for (i = 0; i < graft->nr_parent; i++) {
282 new_parent = lookup_commit(graft->parent[i]);
283 if (!new_parent)
284 continue;
285 pptr = &commit_list_insert(new_parent, pptr)->next;
288 item->date = parse_commit_date(bufptr, tail);
290 return 0;
293 int parse_commit(struct commit *item)
295 enum object_type type;
296 void *buffer;
297 unsigned long size;
298 int ret;
300 if (!item)
301 return -1;
302 if (item->object.parsed)
303 return 0;
304 buffer = read_sha1_file(item->object.sha1, &type, &size);
305 if (!buffer)
306 return error("Could not read %s",
307 sha1_to_hex(item->object.sha1));
308 if (type != OBJ_COMMIT) {
309 free(buffer);
310 return error("Object %s not a commit",
311 sha1_to_hex(item->object.sha1));
313 ret = parse_commit_buffer(item, buffer, size);
314 if (save_commit_buffer && !ret) {
315 item->buffer = buffer;
316 return 0;
318 free(buffer);
319 return ret;
322 int find_commit_subject(const char *commit_buffer, const char **subject)
324 const char *eol;
325 const char *p = commit_buffer;
327 while (*p && (*p != '\n' || p[1] != '\n'))
328 p++;
329 if (*p) {
330 p += 2;
331 for (eol = p; *eol && *eol != '\n'; eol++)
332 ; /* do nothing */
333 } else
334 eol = p;
336 *subject = p;
338 return eol - p;
341 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
343 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
344 new_list->item = item;
345 new_list->next = *list_p;
346 *list_p = new_list;
347 return new_list;
350 unsigned commit_list_count(const struct commit_list *l)
352 unsigned c = 0;
353 for (; l; l = l->next )
354 c++;
355 return c;
358 void free_commit_list(struct commit_list *list)
360 while (list) {
361 struct commit_list *temp = list;
362 list = temp->next;
363 free(temp);
367 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
369 struct commit_list **pp = list;
370 struct commit_list *p;
371 while ((p = *pp) != NULL) {
372 if (p->item->date < item->date) {
373 break;
375 pp = &p->next;
377 return commit_list_insert(item, pp);
381 void commit_list_sort_by_date(struct commit_list **list)
383 struct commit_list *ret = NULL;
384 while (*list) {
385 commit_list_insert_by_date((*list)->item, &ret);
386 *list = (*list)->next;
388 *list = ret;
391 struct commit *pop_most_recent_commit(struct commit_list **list,
392 unsigned int mark)
394 struct commit *ret = (*list)->item;
395 struct commit_list *parents = ret->parents;
396 struct commit_list *old = *list;
398 *list = (*list)->next;
399 free(old);
401 while (parents) {
402 struct commit *commit = parents->item;
403 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
404 commit->object.flags |= mark;
405 commit_list_insert_by_date(commit, list);
407 parents = parents->next;
409 return ret;
412 void clear_commit_marks(struct commit *commit, unsigned int mark)
414 while (commit) {
415 struct commit_list *parents;
417 if (!(mark & commit->object.flags))
418 return;
420 commit->object.flags &= ~mark;
422 parents = commit->parents;
423 if (!parents)
424 return;
426 while ((parents = parents->next))
427 clear_commit_marks(parents->item, mark);
429 commit = commit->parents->item;
433 struct commit *pop_commit(struct commit_list **stack)
435 struct commit_list *top = *stack;
436 struct commit *item = top ? top->item : NULL;
438 if (top) {
439 *stack = top->next;
440 free(top);
442 return item;
446 * Performs an in-place topological sort on the list supplied.
448 void sort_in_topological_order(struct commit_list ** list, int lifo)
450 struct commit_list *next, *orig = *list;
451 struct commit_list *work, **insert;
452 struct commit_list **pptr;
454 if (!orig)
455 return;
456 *list = NULL;
458 /* Mark them and clear the indegree */
459 for (next = orig; next; next = next->next) {
460 struct commit *commit = next->item;
461 commit->indegree = 1;
464 /* update the indegree */
465 for (next = orig; next; next = next->next) {
466 struct commit_list * parents = next->item->parents;
467 while (parents) {
468 struct commit *parent = parents->item;
470 if (parent->indegree)
471 parent->indegree++;
472 parents = parents->next;
477 * find the tips
479 * tips are nodes not reachable from any other node in the list
481 * the tips serve as a starting set for the work queue.
483 work = NULL;
484 insert = &work;
485 for (next = orig; next; next = next->next) {
486 struct commit *commit = next->item;
488 if (commit->indegree == 1)
489 insert = &commit_list_insert(commit, insert)->next;
492 /* process the list in topological order */
493 if (!lifo)
494 commit_list_sort_by_date(&work);
496 pptr = list;
497 *list = NULL;
498 while (work) {
499 struct commit *commit;
500 struct commit_list *parents, *work_item;
502 work_item = work;
503 work = work_item->next;
504 work_item->next = NULL;
506 commit = work_item->item;
507 for (parents = commit->parents; parents ; parents = parents->next) {
508 struct commit *parent = parents->item;
510 if (!parent->indegree)
511 continue;
514 * parents are only enqueued for emission
515 * when all their children have been emitted thereby
516 * guaranteeing topological order.
518 if (--parent->indegree == 1) {
519 if (!lifo)
520 commit_list_insert_by_date(parent, &work);
521 else
522 commit_list_insert(parent, &work);
526 * work_item is a commit all of whose children
527 * have already been emitted. we can emit it now.
529 commit->indegree = 0;
530 *pptr = work_item;
531 pptr = &work_item->next;
535 /* merge-base stuff */
537 /* bits #0..15 in revision.h */
538 #define PARENT1 (1u<<16)
539 #define PARENT2 (1u<<17)
540 #define STALE (1u<<18)
541 #define RESULT (1u<<19)
543 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
545 static struct commit *interesting(struct commit_list *list)
547 while (list) {
548 struct commit *commit = list->item;
549 list = list->next;
550 if (commit->object.flags & STALE)
551 continue;
552 return commit;
554 return NULL;
557 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
559 struct commit_list *list = NULL;
560 struct commit_list *result = NULL;
561 int i;
563 for (i = 0; i < n; i++) {
564 if (one == twos[i])
566 * We do not mark this even with RESULT so we do not
567 * have to clean it up.
569 return commit_list_insert(one, &result);
572 if (parse_commit(one))
573 return NULL;
574 for (i = 0; i < n; i++) {
575 if (parse_commit(twos[i]))
576 return NULL;
579 one->object.flags |= PARENT1;
580 commit_list_insert_by_date(one, &list);
581 for (i = 0; i < n; i++) {
582 twos[i]->object.flags |= PARENT2;
583 commit_list_insert_by_date(twos[i], &list);
586 while (interesting(list)) {
587 struct commit *commit;
588 struct commit_list *parents;
589 struct commit_list *next;
590 int flags;
592 commit = list->item;
593 next = list->next;
594 free(list);
595 list = next;
597 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
598 if (flags == (PARENT1 | PARENT2)) {
599 if (!(commit->object.flags & RESULT)) {
600 commit->object.flags |= RESULT;
601 commit_list_insert_by_date(commit, &result);
603 /* Mark parents of a found merge stale */
604 flags |= STALE;
606 parents = commit->parents;
607 while (parents) {
608 struct commit *p = parents->item;
609 parents = parents->next;
610 if ((p->object.flags & flags) == flags)
611 continue;
612 if (parse_commit(p))
613 return NULL;
614 p->object.flags |= flags;
615 commit_list_insert_by_date(p, &list);
619 /* Clean up the result to remove stale ones */
620 free_commit_list(list);
621 list = result; result = NULL;
622 while (list) {
623 struct commit_list *next = list->next;
624 if (!(list->item->object.flags & STALE))
625 commit_list_insert_by_date(list->item, &result);
626 free(list);
627 list = next;
629 return result;
632 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
634 struct commit_list *i, *j, *k, *ret = NULL;
635 struct commit_list **pptr = &ret;
637 for (i = in; i; i = i->next) {
638 if (!ret)
639 pptr = &commit_list_insert(i->item, pptr)->next;
640 else {
641 struct commit_list *new = NULL, *end = NULL;
643 for (j = ret; j; j = j->next) {
644 struct commit_list *bases;
645 bases = get_merge_bases(i->item, j->item, 1);
646 if (!new)
647 new = bases;
648 else
649 end->next = bases;
650 for (k = bases; k; k = k->next)
651 end = k;
653 ret = new;
656 return ret;
659 struct commit_list *get_merge_bases_many(struct commit *one,
660 int n,
661 struct commit **twos,
662 int cleanup)
664 struct commit_list *list;
665 struct commit **rslt;
666 struct commit_list *result;
667 int cnt, i, j;
669 result = merge_bases_many(one, n, twos);
670 for (i = 0; i < n; i++) {
671 if (one == twos[i])
672 return result;
674 if (!result || !result->next) {
675 if (cleanup) {
676 clear_commit_marks(one, all_flags);
677 for (i = 0; i < n; i++)
678 clear_commit_marks(twos[i], all_flags);
680 return result;
683 /* There are more than one */
684 cnt = 0;
685 list = result;
686 while (list) {
687 list = list->next;
688 cnt++;
690 rslt = xcalloc(cnt, sizeof(*rslt));
691 for (list = result, i = 0; list; list = list->next)
692 rslt[i++] = list->item;
693 free_commit_list(result);
695 clear_commit_marks(one, all_flags);
696 for (i = 0; i < n; i++)
697 clear_commit_marks(twos[i], all_flags);
698 for (i = 0; i < cnt - 1; i++) {
699 for (j = i+1; j < cnt; j++) {
700 if (!rslt[i] || !rslt[j])
701 continue;
702 result = merge_bases_many(rslt[i], 1, &rslt[j]);
703 clear_commit_marks(rslt[i], all_flags);
704 clear_commit_marks(rslt[j], all_flags);
705 for (list = result; list; list = list->next) {
706 if (rslt[i] == list->item)
707 rslt[i] = NULL;
708 if (rslt[j] == list->item)
709 rslt[j] = NULL;
714 /* Surviving ones in rslt[] are the independent results */
715 result = NULL;
716 for (i = 0; i < cnt; i++) {
717 if (rslt[i])
718 commit_list_insert_by_date(rslt[i], &result);
720 free(rslt);
721 return result;
724 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
725 int cleanup)
727 return get_merge_bases_many(one, 1, &two, cleanup);
730 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
732 if (!with_commit)
733 return 1;
734 while (with_commit) {
735 struct commit *other;
737 other = with_commit->item;
738 with_commit = with_commit->next;
739 if (in_merge_bases(other, &commit, 1))
740 return 1;
742 return 0;
745 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
747 struct commit_list *bases, *b;
748 int ret = 0;
750 if (num == 1)
751 bases = get_merge_bases(commit, *reference, 1);
752 else
753 die("not yet");
754 for (b = bases; b; b = b->next) {
755 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
756 ret = 1;
757 break;
761 free_commit_list(bases);
762 return ret;
765 struct commit_list *reduce_heads(struct commit_list *heads)
767 struct commit_list *p;
768 struct commit_list *result = NULL, **tail = &result;
769 struct commit **other;
770 size_t num_head, num_other;
772 if (!heads)
773 return NULL;
775 /* Avoid unnecessary reallocations */
776 for (p = heads, num_head = 0; p; p = p->next)
777 num_head++;
778 other = xcalloc(sizeof(*other), num_head);
780 /* For each commit, see if it can be reached by others */
781 for (p = heads; p; p = p->next) {
782 struct commit_list *q, *base;
784 /* Do we already have this in the result? */
785 for (q = result; q; q = q->next)
786 if (p->item == q->item)
787 break;
788 if (q)
789 continue;
791 num_other = 0;
792 for (q = heads; q; q = q->next) {
793 if (p->item == q->item)
794 continue;
795 other[num_other++] = q->item;
797 if (num_other)
798 base = get_merge_bases_many(p->item, num_other, other, 1);
799 else
800 base = NULL;
802 * If p->item does not have anything common with other
803 * commits, there won't be any merge base. If it is
804 * reachable from some of the others, p->item will be
805 * the merge base. If its history is connected with
806 * others, but p->item is not reachable by others, we
807 * will get something other than p->item back.
809 if (!base || (base->item != p->item))
810 tail = &(commit_list_insert(p->item, tail)->next);
811 free_commit_list(base);
813 free(other);
814 return result;
817 static const char commit_utf8_warn[] =
818 "Warning: commit message does not conform to UTF-8.\n"
819 "You may want to amend it after fixing the message, or set the config\n"
820 "variable i18n.commitencoding to the encoding your project uses.\n";
822 int commit_tree(const char *msg, unsigned char *tree,
823 struct commit_list *parents, unsigned char *ret,
824 const char *author)
826 int result;
827 int encoding_is_utf8;
828 struct strbuf buffer;
830 assert_sha1_type(tree, OBJ_TREE);
832 /* Not having i18n.commitencoding is the same as having utf-8 */
833 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
835 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
836 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
839 * NOTE! This ordering means that the same exact tree merged with a
840 * different order of parents will be a _different_ changeset even
841 * if everything else stays the same.
843 while (parents) {
844 struct commit_list *next = parents->next;
845 strbuf_addf(&buffer, "parent %s\n",
846 sha1_to_hex(parents->item->object.sha1));
847 free(parents);
848 parents = next;
851 /* Person/date information */
852 if (!author)
853 author = git_author_info(IDENT_ERROR_ON_NO_NAME);
854 strbuf_addf(&buffer, "author %s\n", author);
855 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
856 if (!encoding_is_utf8)
857 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
858 strbuf_addch(&buffer, '\n');
860 /* And add the comment */
861 strbuf_addstr(&buffer, msg);
863 /* And check the encoding */
864 if (encoding_is_utf8 && !is_utf8(buffer.buf))
865 fprintf(stderr, commit_utf8_warn);
867 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
868 strbuf_release(&buffer);
869 return result;