Introduce commit notes
[git] / commit.c
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"
9
10 int save_commit_buffer = 1;
11
12 const char *commit_type = "commit";
13
14 static struct commit *check_commit(struct object *obj,
15                                    const unsigned char *sha1,
16                                    int quiet)
17 {
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;
23         }
24         return (struct commit *) obj;
25 }
26
27 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
28                                               int quiet)
29 {
30         struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
31
32         if (!obj)
33                 return NULL;
34         return check_commit(obj, sha1, quiet);
35 }
36
37 struct commit *lookup_commit_reference(const unsigned char *sha1)
38 {
39         return lookup_commit_reference_gently(sha1, 0);
40 }
41
42 struct commit *lookup_commit(const unsigned char *sha1)
43 {
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);
50 }
51
52 static unsigned long parse_commit_date(const char *buf, const char *tail)
53 {
54         const char *dateptr;
55
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);
77 }
78
79 static struct commit_graft **commit_graft;
80 static int commit_graft_alloc, commit_graft_nr;
81
82 static int commit_graft_pos(const unsigned char *sha1)
83 {
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;
97         }
98         return -lo - 1;
99 }
100
101 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
102 {
103         int pos = commit_graft_pos(graft->sha1);
104
105         if (0 <= pos) {
106                 if (ignore_dups)
107                         free(graft);
108                 else {
109                         free(commit_graft[pos]);
110                         commit_graft[pos] = graft;
111                 }
112                 return 1;
113         }
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);
120         }
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;
128 }
129
130 struct commit_graft *read_graft_line(char *buf, int len)
131 {
132         /* The format is just "Commit Parent1 Parent2 ...\n" */
133         int i;
134         struct commit_graft *graft = NULL;
135
136         if (buf[len-1] == '\n')
137                 buf[--len] = 0;
138         if (buf[0] == '#' || buf[0] == '\0')
139                 return NULL;
140         if ((len + 1) % 41) {
141         bad_graft_data:
142                 error("bad graft data: %s", buf);
143                 free(graft);
144                 return NULL;
145         }
146         i = (len + 1) / 41 - 1;
147         graft = xmalloc(sizeof(*graft) + 20 * i);
148         graft->nr_parent = i;
149         if (get_sha1_hex(buf, graft->sha1))
150                 goto bad_graft_data;
151         for (i = 40; i < len; i += 41) {
152                 if (buf[i] != ' ')
153                         goto bad_graft_data;
154                 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
155                         goto bad_graft_data;
156         }
157         return graft;
158 }
159
160 static int read_graft_file(const char *graft_file)
161 {
162         FILE *fp = fopen(graft_file, "r");
163         char buf[1024];
164         if (!fp)
165                 return -1;
166         while (fgets(buf, sizeof(buf), fp)) {
167                 /* The format is just "Commit Parent1 Parent2 ...\n" */
168                 int len = strlen(buf);
169                 struct commit_graft *graft = read_graft_line(buf, len);
170                 if (!graft)
171                         continue;
172                 if (register_commit_graft(graft, 1))
173                         error("duplicate graft data: %s", buf);
174         }
175         fclose(fp);
176         return 0;
177 }
178
179 static void prepare_commit_graft(void)
180 {
181         static int commit_graft_prepared;
182         char *graft_file;
183
184         if (commit_graft_prepared)
185                 return;
186         graft_file = get_graft_file();
187         read_graft_file(graft_file);
188         /* make sure shallows are read */
189         is_repository_shallow();
190         commit_graft_prepared = 1;
191 }
192
193 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
194 {
195         int pos;
196         prepare_commit_graft();
197         pos = commit_graft_pos(sha1);
198         if (pos < 0)
199                 return NULL;
200         return commit_graft[pos];
201 }
202
203 int write_shallow_commits(int fd, int use_pack_protocol)
204 {
205         int i, count = 0;
206         for (i = 0; i < commit_graft_nr; i++)
207                 if (commit_graft[i]->nr_parent < 0) {
208                         const char *hex =
209                                 sha1_to_hex(commit_graft[i]->sha1);
210                         count++;
211                         if (use_pack_protocol)
212                                 packet_write(fd, "shallow %s", hex);
213                         else {
214                                 if (write_in_full(fd, hex,  40) != 40)
215                                         break;
216                                 if (write_in_full(fd, "\n", 1) != 1)
217                                         break;
218                         }
219                 }
220         return count;
221 }
222
223 int unregister_shallow(const unsigned char *sha1)
224 {
225         int pos = commit_graft_pos(sha1);
226         if (pos < 0)
227                 return -1;
228         if (pos + 1 < commit_graft_nr)
229                 memcpy(commit_graft + pos, commit_graft + pos + 1,
230                                 sizeof(struct commit_graft *)
231                                 * (commit_graft_nr - pos - 1));
232         commit_graft_nr--;
233         return 0;
234 }
235
236 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
237 {
238         char *tail = buffer;
239         char *bufptr = buffer;
240         unsigned char parent[20];
241         struct commit_list **pptr;
242         struct commit_graft *graft;
243
244         if (item->object.parsed)
245                 return 0;
246         item->object.parsed = 1;
247         tail += size;
248         if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
249                 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
250         if (get_sha1_hex(bufptr + 5, parent) < 0)
251                 return error("bad tree pointer in commit %s",
252                              sha1_to_hex(item->object.sha1));
253         item->tree = lookup_tree(parent);
254         bufptr += 46; /* "tree " + "hex sha1" + "\n" */
255         pptr = &item->parents;
256
257         graft = lookup_commit_graft(item->object.sha1);
258         while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
259                 struct commit *new_parent;
260
261                 if (tail <= bufptr + 48 ||
262                     get_sha1_hex(bufptr + 7, parent) ||
263                     bufptr[47] != '\n')
264                         return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
265                 bufptr += 48;
266                 /*
267                  * The clone is shallow if nr_parent < 0, and we must
268                  * not traverse its real parents even when we unhide them.
269                  */
270                 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
271                         continue;
272                 new_parent = lookup_commit(parent);
273                 if (new_parent)
274                         pptr = &commit_list_insert(new_parent, pptr)->next;
275         }
276         if (graft) {
277                 int i;
278                 struct commit *new_parent;
279                 for (i = 0; i < graft->nr_parent; i++) {
280                         new_parent = lookup_commit(graft->parent[i]);
281                         if (!new_parent)
282                                 continue;
283                         pptr = &commit_list_insert(new_parent, pptr)->next;
284                 }
285         }
286         item->date = parse_commit_date(bufptr, tail);
287
288         return 0;
289 }
290
291 int parse_commit(struct commit *item)
292 {
293         enum object_type type;
294         void *buffer;
295         unsigned long size;
296         int ret;
297
298         if (!item)
299                 return -1;
300         if (item->object.parsed)
301                 return 0;
302         buffer = read_sha1_file(item->object.sha1, &type, &size);
303         if (!buffer)
304                 return error("Could not read %s",
305                              sha1_to_hex(item->object.sha1));
306         if (type != OBJ_COMMIT) {
307                 free(buffer);
308                 return error("Object %s not a commit",
309                              sha1_to_hex(item->object.sha1));
310         }
311         ret = parse_commit_buffer(item, buffer, size);
312         if (save_commit_buffer && !ret) {
313                 item->buffer = buffer;
314                 return 0;
315         }
316         free(buffer);
317         return ret;
318 }
319
320 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
321 {
322         struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
323         new_list->item = item;
324         new_list->next = *list_p;
325         *list_p = new_list;
326         return new_list;
327 }
328
329 unsigned commit_list_count(const struct commit_list *l)
330 {
331         unsigned c = 0;
332         for (; l; l = l->next )
333                 c++;
334         return c;
335 }
336
337 void free_commit_list(struct commit_list *list)
338 {
339         while (list) {
340                 struct commit_list *temp = list;
341                 list = temp->next;
342                 free(temp);
343         }
344 }
345
346 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
347 {
348         struct commit_list **pp = list;
349         struct commit_list *p;
350         while ((p = *pp) != NULL) {
351                 if (p->item->date < item->date) {
352                         break;
353                 }
354                 pp = &p->next;
355         }
356         return commit_list_insert(item, pp);
357 }
358
359
360 void sort_by_date(struct commit_list **list)
361 {
362         struct commit_list *ret = NULL;
363         while (*list) {
364                 insert_by_date((*list)->item, &ret);
365                 *list = (*list)->next;
366         }
367         *list = ret;
368 }
369
370 struct commit *pop_most_recent_commit(struct commit_list **list,
371                                       unsigned int mark)
372 {
373         struct commit *ret = (*list)->item;
374         struct commit_list *parents = ret->parents;
375         struct commit_list *old = *list;
376
377         *list = (*list)->next;
378         free(old);
379
380         while (parents) {
381                 struct commit *commit = parents->item;
382                 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
383                         commit->object.flags |= mark;
384                         insert_by_date(commit, list);
385                 }
386                 parents = parents->next;
387         }
388         return ret;
389 }
390
391 void clear_commit_marks(struct commit *commit, unsigned int mark)
392 {
393         while (commit) {
394                 struct commit_list *parents;
395
396                 if (!(mark & commit->object.flags))
397                         return;
398
399                 commit->object.flags &= ~mark;
400
401                 parents = commit->parents;
402                 if (!parents)
403                         return;
404
405                 while ((parents = parents->next))
406                         clear_commit_marks(parents->item, mark);
407
408                 commit = commit->parents->item;
409         }
410 }
411
412 struct commit *pop_commit(struct commit_list **stack)
413 {
414         struct commit_list *top = *stack;
415         struct commit *item = top ? top->item : NULL;
416
417         if (top) {
418                 *stack = top->next;
419                 free(top);
420         }
421         return item;
422 }
423
424 /*
425  * Performs an in-place topological sort on the list supplied.
426  */
427 void sort_in_topological_order(struct commit_list ** list, int lifo)
428 {
429         struct commit_list *next, *orig = *list;
430         struct commit_list *work, **insert;
431         struct commit_list **pptr;
432
433         if (!orig)
434                 return;
435         *list = NULL;
436
437         /* Mark them and clear the indegree */
438         for (next = orig; next; next = next->next) {
439                 struct commit *commit = next->item;
440                 commit->indegree = 1;
441         }
442
443         /* update the indegree */
444         for (next = orig; next; next = next->next) {
445                 struct commit_list * parents = next->item->parents;
446                 while (parents) {
447                         struct commit *parent = parents->item;
448
449                         if (parent->indegree)
450                                 parent->indegree++;
451                         parents = parents->next;
452                 }
453         }
454
455         /*
456          * find the tips
457          *
458          * tips are nodes not reachable from any other node in the list
459          *
460          * the tips serve as a starting set for the work queue.
461          */
462         work = NULL;
463         insert = &work;
464         for (next = orig; next; next = next->next) {
465                 struct commit *commit = next->item;
466
467                 if (commit->indegree == 1)
468                         insert = &commit_list_insert(commit, insert)->next;
469         }
470
471         /* process the list in topological order */
472         if (!lifo)
473                 sort_by_date(&work);
474
475         pptr = list;
476         *list = NULL;
477         while (work) {
478                 struct commit *commit;
479                 struct commit_list *parents, *work_item;
480
481                 work_item = work;
482                 work = work_item->next;
483                 work_item->next = NULL;
484
485                 commit = work_item->item;
486                 for (parents = commit->parents; parents ; parents = parents->next) {
487                         struct commit *parent=parents->item;
488
489                         if (!parent->indegree)
490                                 continue;
491
492                         /*
493                          * parents are only enqueued for emission
494                          * when all their children have been emitted thereby
495                          * guaranteeing topological order.
496                          */
497                         if (--parent->indegree == 1) {
498                                 if (!lifo)
499                                         insert_by_date(parent, &work);
500                                 else
501                                         commit_list_insert(parent, &work);
502                         }
503                 }
504                 /*
505                  * work_item is a commit all of whose children
506                  * have already been emitted. we can emit it now.
507                  */
508                 commit->indegree = 0;
509                 *pptr = work_item;
510                 pptr = &work_item->next;
511         }
512 }
513
514 /* merge-base stuff */
515
516 /* bits #0..15 in revision.h */
517 #define PARENT1         (1u<<16)
518 #define PARENT2         (1u<<17)
519 #define STALE           (1u<<18)
520 #define RESULT          (1u<<19)
521
522 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
523
524 static struct commit *interesting(struct commit_list *list)
525 {
526         while (list) {
527                 struct commit *commit = list->item;
528                 list = list->next;
529                 if (commit->object.flags & STALE)
530                         continue;
531                 return commit;
532         }
533         return NULL;
534 }
535
536 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
537 {
538         struct commit_list *list = NULL;
539         struct commit_list *result = NULL;
540         int i;
541
542         for (i = 0; i < n; i++) {
543                 if (one == twos[i])
544                         /*
545                          * We do not mark this even with RESULT so we do not
546                          * have to clean it up.
547                          */
548                         return commit_list_insert(one, &result);
549         }
550
551         if (parse_commit(one))
552                 return NULL;
553         for (i = 0; i < n; i++) {
554                 if (parse_commit(twos[i]))
555                         return NULL;
556         }
557
558         one->object.flags |= PARENT1;
559         insert_by_date(one, &list);
560         for (i = 0; i < n; i++) {
561                 twos[i]->object.flags |= PARENT2;
562                 insert_by_date(twos[i], &list);
563         }
564
565         while (interesting(list)) {
566                 struct commit *commit;
567                 struct commit_list *parents;
568                 struct commit_list *n;
569                 int flags;
570
571                 commit = list->item;
572                 n = list->next;
573                 free(list);
574                 list = n;
575
576                 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
577                 if (flags == (PARENT1 | PARENT2)) {
578                         if (!(commit->object.flags & RESULT)) {
579                                 commit->object.flags |= RESULT;
580                                 insert_by_date(commit, &result);
581                         }
582                         /* Mark parents of a found merge stale */
583                         flags |= STALE;
584                 }
585                 parents = commit->parents;
586                 while (parents) {
587                         struct commit *p = parents->item;
588                         parents = parents->next;
589                         if ((p->object.flags & flags) == flags)
590                                 continue;
591                         if (parse_commit(p))
592                                 return NULL;
593                         p->object.flags |= flags;
594                         insert_by_date(p, &list);
595                 }
596         }
597
598         /* Clean up the result to remove stale ones */
599         free_commit_list(list);
600         list = result; result = NULL;
601         while (list) {
602                 struct commit_list *n = list->next;
603                 if (!(list->item->object.flags & STALE))
604                         insert_by_date(list->item, &result);
605                 free(list);
606                 list = n;
607         }
608         return result;
609 }
610
611 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
612 {
613         struct commit_list *i, *j, *k, *ret = NULL;
614         struct commit_list **pptr = &ret;
615
616         for (i = in; i; i = i->next) {
617                 if (!ret)
618                         pptr = &commit_list_insert(i->item, pptr)->next;
619                 else {
620                         struct commit_list *new = NULL, *end = NULL;
621
622                         for (j = ret; j; j = j->next) {
623                                 struct commit_list *bases;
624                                 bases = get_merge_bases(i->item, j->item, 1);
625                                 if (!new)
626                                         new = bases;
627                                 else
628                                         end->next = bases;
629                                 for (k = bases; k; k = k->next)
630                                         end = k;
631                         }
632                         ret = new;
633                 }
634         }
635         return ret;
636 }
637
638 struct commit_list *get_merge_bases_many(struct commit *one,
639                                          int n,
640                                          struct commit **twos,
641                                          int cleanup)
642 {
643         struct commit_list *list;
644         struct commit **rslt;
645         struct commit_list *result;
646         int cnt, i, j;
647
648         result = merge_bases_many(one, n, twos);
649         for (i = 0; i < n; i++) {
650                 if (one == twos[i])
651                         return result;
652         }
653         if (!result || !result->next) {
654                 if (cleanup) {
655                         clear_commit_marks(one, all_flags);
656                         for (i = 0; i < n; i++)
657                                 clear_commit_marks(twos[i], all_flags);
658                 }
659                 return result;
660         }
661
662         /* There are more than one */
663         cnt = 0;
664         list = result;
665         while (list) {
666                 list = list->next;
667                 cnt++;
668         }
669         rslt = xcalloc(cnt, sizeof(*rslt));
670         for (list = result, i = 0; list; list = list->next)
671                 rslt[i++] = list->item;
672         free_commit_list(result);
673
674         clear_commit_marks(one, all_flags);
675         for (i = 0; i < n; i++)
676                 clear_commit_marks(twos[i], all_flags);
677         for (i = 0; i < cnt - 1; i++) {
678                 for (j = i+1; j < cnt; j++) {
679                         if (!rslt[i] || !rslt[j])
680                                 continue;
681                         result = merge_bases_many(rslt[i], 1, &rslt[j]);
682                         clear_commit_marks(rslt[i], all_flags);
683                         clear_commit_marks(rslt[j], all_flags);
684                         for (list = result; list; list = list->next) {
685                                 if (rslt[i] == list->item)
686                                         rslt[i] = NULL;
687                                 if (rslt[j] == list->item)
688                                         rslt[j] = NULL;
689                         }
690                 }
691         }
692
693         /* Surviving ones in rslt[] are the independent results */
694         result = NULL;
695         for (i = 0; i < cnt; i++) {
696                 if (rslt[i])
697                         insert_by_date(rslt[i], &result);
698         }
699         free(rslt);
700         return result;
701 }
702
703 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
704                                     int cleanup)
705 {
706         return get_merge_bases_many(one, 1, &two, cleanup);
707 }
708
709 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
710 {
711         if (!with_commit)
712                 return 1;
713         while (with_commit) {
714                 struct commit *other;
715
716                 other = with_commit->item;
717                 with_commit = with_commit->next;
718                 if (in_merge_bases(other, &commit, 1))
719                         return 1;
720         }
721         return 0;
722 }
723
724 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
725 {
726         struct commit_list *bases, *b;
727         int ret = 0;
728
729         if (num == 1)
730                 bases = get_merge_bases(commit, *reference, 1);
731         else
732                 die("not yet");
733         for (b = bases; b; b = b->next) {
734                 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
735                         ret = 1;
736                         break;
737                 }
738         }
739
740         free_commit_list(bases);
741         return ret;
742 }
743
744 struct commit_list *reduce_heads(struct commit_list *heads)
745 {
746         struct commit_list *p;
747         struct commit_list *result = NULL, **tail = &result;
748         struct commit **other;
749         size_t num_head, num_other;
750
751         if (!heads)
752                 return NULL;
753
754         /* Avoid unnecessary reallocations */
755         for (p = heads, num_head = 0; p; p = p->next)
756                 num_head++;
757         other = xcalloc(sizeof(*other), num_head);
758
759         /* For each commit, see if it can be reached by others */
760         for (p = heads; p; p = p->next) {
761                 struct commit_list *q, *base;
762
763                 /* Do we already have this in the result? */
764                 for (q = result; q; q = q->next)
765                         if (p->item == q->item)
766                                 break;
767                 if (q)
768                         continue;
769
770                 num_other = 0;
771                 for (q = heads; q; q = q->next) {
772                         if (p->item == q->item)
773                                 continue;
774                         other[num_other++] = q->item;
775                 }
776                 if (num_other)
777                         base = get_merge_bases_many(p->item, num_other, other, 1);
778                 else
779                         base = NULL;
780                 /*
781                  * If p->item does not have anything common with other
782                  * commits, there won't be any merge base.  If it is
783                  * reachable from some of the others, p->item will be
784                  * the merge base.  If its history is connected with
785                  * others, but p->item is not reachable by others, we
786                  * will get something other than p->item back.
787                  */
788                 if (!base || (base->item != p->item))
789                         tail = &(commit_list_insert(p->item, tail)->next);
790                 free_commit_list(base);
791         }
792         free(other);
793         return result;
794 }