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