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