Code

test smart http fetch and push
[git.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"
9 int save_commit_buffer = 1;
11 const char *commit_type = "commit";
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 }
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);
31         if (!obj)
32                 return NULL;
33         return check_commit(obj, sha1, quiet);
34 }
36 struct commit *lookup_commit_reference(const unsigned char *sha1)
37 {
38         return lookup_commit_reference_gently(sha1, 0);
39 }
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 }
51 static unsigned long parse_commit_date(const char *buf, const char *tail)
52 {
53         const char *dateptr;
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 }
78 static struct commit_graft **commit_graft;
79 static int commit_graft_alloc, commit_graft_nr;
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 }
100 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
102         int pos = commit_graft_pos(graft->sha1);
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;
129 struct commit_graft *read_graft_line(char *buf, int len)
131         /* The format is just "Commit Parent1 Parent2 ...\n" */
132         int i;
133         struct commit_graft *graft = NULL;
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;
159 static int read_graft_file(const char *graft_file)
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;
178 static void prepare_commit_graft(void)
180         static int commit_graft_prepared;
181         char *graft_file;
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;
192 struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
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];
202 int write_shallow_commits(struct strbuf *out, int use_pack_protocol)
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_buf_write(out, "shallow %s", hex);
212                         else {
213                                 strbuf_addstr(out, hex);
214                                 strbuf_addch(out, '\n');
215                         }
216                 }
217         return count;
220 int unregister_shallow(const unsigned char *sha1)
222         int pos = commit_graft_pos(sha1);
223         if (pos < 0)
224                 return -1;
225         if (pos + 1 < commit_graft_nr)
226                 memcpy(commit_graft + pos, commit_graft + pos + 1,
227                                 sizeof(struct commit_graft *)
228                                 * (commit_graft_nr - pos - 1));
229         commit_graft_nr--;
230         return 0;
233 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
235         char *tail = buffer;
236         char *bufptr = buffer;
237         unsigned char parent[20];
238         struct commit_list **pptr;
239         struct commit_graft *graft;
241         if (item->object.parsed)
242                 return 0;
243         item->object.parsed = 1;
244         tail += size;
245         if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
246                 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
247         if (get_sha1_hex(bufptr + 5, parent) < 0)
248                 return error("bad tree pointer in commit %s",
249                              sha1_to_hex(item->object.sha1));
250         item->tree = lookup_tree(parent);
251         bufptr += 46; /* "tree " + "hex sha1" + "\n" */
252         pptr = &item->parents;
254         graft = lookup_commit_graft(item->object.sha1);
255         while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
256                 struct commit *new_parent;
258                 if (tail <= bufptr + 48 ||
259                     get_sha1_hex(bufptr + 7, parent) ||
260                     bufptr[47] != '\n')
261                         return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
262                 bufptr += 48;
263                 /*
264                  * The clone is shallow if nr_parent < 0, and we must
265                  * not traverse its real parents even when we unhide them.
266                  */
267                 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
268                         continue;
269                 new_parent = lookup_commit(parent);
270                 if (new_parent)
271                         pptr = &commit_list_insert(new_parent, pptr)->next;
272         }
273         if (graft) {
274                 int i;
275                 struct commit *new_parent;
276                 for (i = 0; i < graft->nr_parent; i++) {
277                         new_parent = lookup_commit(graft->parent[i]);
278                         if (!new_parent)
279                                 continue;
280                         pptr = &commit_list_insert(new_parent, pptr)->next;
281                 }
282         }
283         item->date = parse_commit_date(bufptr, tail);
285         return 0;
288 int parse_commit(struct commit *item)
290         enum object_type type;
291         void *buffer;
292         unsigned long size;
293         int ret;
295         if (!item)
296                 return -1;
297         if (item->object.parsed)
298                 return 0;
299         buffer = read_sha1_file(item->object.sha1, &type, &size);
300         if (!buffer)
301                 return error("Could not read %s",
302                              sha1_to_hex(item->object.sha1));
303         if (type != OBJ_COMMIT) {
304                 free(buffer);
305                 return error("Object %s not a commit",
306                              sha1_to_hex(item->object.sha1));
307         }
308         ret = parse_commit_buffer(item, buffer, size);
309         if (save_commit_buffer && !ret) {
310                 item->buffer = buffer;
311                 return 0;
312         }
313         free(buffer);
314         return ret;
317 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
319         struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
320         new_list->item = item;
321         new_list->next = *list_p;
322         *list_p = new_list;
323         return new_list;
326 unsigned commit_list_count(const struct commit_list *l)
328         unsigned c = 0;
329         for (; l; l = l->next )
330                 c++;
331         return c;
334 void free_commit_list(struct commit_list *list)
336         while (list) {
337                 struct commit_list *temp = list;
338                 list = temp->next;
339                 free(temp);
340         }
343 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
345         struct commit_list **pp = list;
346         struct commit_list *p;
347         while ((p = *pp) != NULL) {
348                 if (p->item->date < item->date) {
349                         break;
350                 }
351                 pp = &p->next;
352         }
353         return commit_list_insert(item, pp);
357 void sort_by_date(struct commit_list **list)
359         struct commit_list *ret = NULL;
360         while (*list) {
361                 insert_by_date((*list)->item, &ret);
362                 *list = (*list)->next;
363         }
364         *list = ret;
367 struct commit *pop_most_recent_commit(struct commit_list **list,
368                                       unsigned int mark)
370         struct commit *ret = (*list)->item;
371         struct commit_list *parents = ret->parents;
372         struct commit_list *old = *list;
374         *list = (*list)->next;
375         free(old);
377         while (parents) {
378                 struct commit *commit = parents->item;
379                 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
380                         commit->object.flags |= mark;
381                         insert_by_date(commit, list);
382                 }
383                 parents = parents->next;
384         }
385         return ret;
388 void clear_commit_marks(struct commit *commit, unsigned int mark)
390         while (commit) {
391                 struct commit_list *parents;
393                 if (!(mark & commit->object.flags))
394                         return;
396                 commit->object.flags &= ~mark;
398                 parents = commit->parents;
399                 if (!parents)
400                         return;
402                 while ((parents = parents->next))
403                         clear_commit_marks(parents->item, mark);
405                 commit = commit->parents->item;
406         }
409 struct commit *pop_commit(struct commit_list **stack)
411         struct commit_list *top = *stack;
412         struct commit *item = top ? top->item : NULL;
414         if (top) {
415                 *stack = top->next;
416                 free(top);
417         }
418         return item;
421 /*
422  * Performs an in-place topological sort on the list supplied.
423  */
424 void sort_in_topological_order(struct commit_list ** list, int lifo)
426         struct commit_list *next, *orig = *list;
427         struct commit_list *work, **insert;
428         struct commit_list **pptr;
430         if (!orig)
431                 return;
432         *list = NULL;
434         /* Mark them and clear the indegree */
435         for (next = orig; next; next = next->next) {
436                 struct commit *commit = next->item;
437                 commit->indegree = 1;
438         }
440         /* update the indegree */
441         for (next = orig; next; next = next->next) {
442                 struct commit_list * parents = next->item->parents;
443                 while (parents) {
444                         struct commit *parent = parents->item;
446                         if (parent->indegree)
447                                 parent->indegree++;
448                         parents = parents->next;
449                 }
450         }
452         /*
453          * find the tips
454          *
455          * tips are nodes not reachable from any other node in the list
456          *
457          * the tips serve as a starting set for the work queue.
458          */
459         work = NULL;
460         insert = &work;
461         for (next = orig; next; next = next->next) {
462                 struct commit *commit = next->item;
464                 if (commit->indegree == 1)
465                         insert = &commit_list_insert(commit, insert)->next;
466         }
468         /* process the list in topological order */
469         if (!lifo)
470                 sort_by_date(&work);
472         pptr = list;
473         *list = NULL;
474         while (work) {
475                 struct commit *commit;
476                 struct commit_list *parents, *work_item;
478                 work_item = work;
479                 work = work_item->next;
480                 work_item->next = NULL;
482                 commit = work_item->item;
483                 for (parents = commit->parents; parents ; parents = parents->next) {
484                         struct commit *parent=parents->item;
486                         if (!parent->indegree)
487                                 continue;
489                         /*
490                          * parents are only enqueued for emission
491                          * when all their children have been emitted thereby
492                          * guaranteeing topological order.
493                          */
494                         if (--parent->indegree == 1) {
495                                 if (!lifo)
496                                         insert_by_date(parent, &work);
497                                 else
498                                         commit_list_insert(parent, &work);
499                         }
500                 }
501                 /*
502                  * work_item is a commit all of whose children
503                  * have already been emitted. we can emit it now.
504                  */
505                 commit->indegree = 0;
506                 *pptr = work_item;
507                 pptr = &work_item->next;
508         }
511 /* merge-base stuff */
513 /* bits #0..15 in revision.h */
514 #define PARENT1         (1u<<16)
515 #define PARENT2         (1u<<17)
516 #define STALE           (1u<<18)
517 #define RESULT          (1u<<19)
519 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
521 static struct commit *interesting(struct commit_list *list)
523         while (list) {
524                 struct commit *commit = list->item;
525                 list = list->next;
526                 if (commit->object.flags & STALE)
527                         continue;
528                 return commit;
529         }
530         return NULL;
533 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
535         struct commit_list *list = NULL;
536         struct commit_list *result = NULL;
537         int i;
539         for (i = 0; i < n; i++) {
540                 if (one == twos[i])
541                         /*
542                          * We do not mark this even with RESULT so we do not
543                          * have to clean it up.
544                          */
545                         return commit_list_insert(one, &result);
546         }
548         if (parse_commit(one))
549                 return NULL;
550         for (i = 0; i < n; i++) {
551                 if (parse_commit(twos[i]))
552                         return NULL;
553         }
555         one->object.flags |= PARENT1;
556         insert_by_date(one, &list);
557         for (i = 0; i < n; i++) {
558                 twos[i]->object.flags |= PARENT2;
559                 insert_by_date(twos[i], &list);
560         }
562         while (interesting(list)) {
563                 struct commit *commit;
564                 struct commit_list *parents;
565                 struct commit_list *next;
566                 int flags;
568                 commit = list->item;
569                 next = list->next;
570                 free(list);
571                 list = next;
573                 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
574                 if (flags == (PARENT1 | PARENT2)) {
575                         if (!(commit->object.flags & RESULT)) {
576                                 commit->object.flags |= RESULT;
577                                 insert_by_date(commit, &result);
578                         }
579                         /* Mark parents of a found merge stale */
580                         flags |= STALE;
581                 }
582                 parents = commit->parents;
583                 while (parents) {
584                         struct commit *p = parents->item;
585                         parents = parents->next;
586                         if ((p->object.flags & flags) == flags)
587                                 continue;
588                         if (parse_commit(p))
589                                 return NULL;
590                         p->object.flags |= flags;
591                         insert_by_date(p, &list);
592                 }
593         }
595         /* Clean up the result to remove stale ones */
596         free_commit_list(list);
597         list = result; result = NULL;
598         while (list) {
599                 struct commit_list *next = list->next;
600                 if (!(list->item->object.flags & STALE))
601                         insert_by_date(list->item, &result);
602                 free(list);
603                 list = next;
604         }
605         return result;
608 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
610         struct commit_list *i, *j, *k, *ret = NULL;
611         struct commit_list **pptr = &ret;
613         for (i = in; i; i = i->next) {
614                 if (!ret)
615                         pptr = &commit_list_insert(i->item, pptr)->next;
616                 else {
617                         struct commit_list *new = NULL, *end = NULL;
619                         for (j = ret; j; j = j->next) {
620                                 struct commit_list *bases;
621                                 bases = get_merge_bases(i->item, j->item, 1);
622                                 if (!new)
623                                         new = bases;
624                                 else
625                                         end->next = bases;
626                                 for (k = bases; k; k = k->next)
627                                         end = k;
628                         }
629                         ret = new;
630                 }
631         }
632         return ret;
635 struct commit_list *get_merge_bases_many(struct commit *one,
636                                          int n,
637                                          struct commit **twos,
638                                          int cleanup)
640         struct commit_list *list;
641         struct commit **rslt;
642         struct commit_list *result;
643         int cnt, i, j;
645         result = merge_bases_many(one, n, twos);
646         for (i = 0; i < n; i++) {
647                 if (one == twos[i])
648                         return result;
649         }
650         if (!result || !result->next) {
651                 if (cleanup) {
652                         clear_commit_marks(one, all_flags);
653                         for (i = 0; i < n; i++)
654                                 clear_commit_marks(twos[i], all_flags);
655                 }
656                 return result;
657         }
659         /* There are more than one */
660         cnt = 0;
661         list = result;
662         while (list) {
663                 list = list->next;
664                 cnt++;
665         }
666         rslt = xcalloc(cnt, sizeof(*rslt));
667         for (list = result, i = 0; list; list = list->next)
668                 rslt[i++] = list->item;
669         free_commit_list(result);
671         clear_commit_marks(one, all_flags);
672         for (i = 0; i < n; i++)
673                 clear_commit_marks(twos[i], all_flags);
674         for (i = 0; i < cnt - 1; i++) {
675                 for (j = i+1; j < cnt; j++) {
676                         if (!rslt[i] || !rslt[j])
677                                 continue;
678                         result = merge_bases_many(rslt[i], 1, &rslt[j]);
679                         clear_commit_marks(rslt[i], all_flags);
680                         clear_commit_marks(rslt[j], all_flags);
681                         for (list = result; list; list = list->next) {
682                                 if (rslt[i] == list->item)
683                                         rslt[i] = NULL;
684                                 if (rslt[j] == list->item)
685                                         rslt[j] = NULL;
686                         }
687                 }
688         }
690         /* Surviving ones in rslt[] are the independent results */
691         result = NULL;
692         for (i = 0; i < cnt; i++) {
693                 if (rslt[i])
694                         insert_by_date(rslt[i], &result);
695         }
696         free(rslt);
697         return result;
700 struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
701                                     int cleanup)
703         return get_merge_bases_many(one, 1, &two, cleanup);
706 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
708         if (!with_commit)
709                 return 1;
710         while (with_commit) {
711                 struct commit *other;
713                 other = with_commit->item;
714                 with_commit = with_commit->next;
715                 if (in_merge_bases(other, &commit, 1))
716                         return 1;
717         }
718         return 0;
721 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
723         struct commit_list *bases, *b;
724         int ret = 0;
726         if (num == 1)
727                 bases = get_merge_bases(commit, *reference, 1);
728         else
729                 die("not yet");
730         for (b = bases; b; b = b->next) {
731                 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
732                         ret = 1;
733                         break;
734                 }
735         }
737         free_commit_list(bases);
738         return ret;
741 struct commit_list *reduce_heads(struct commit_list *heads)
743         struct commit_list *p;
744         struct commit_list *result = NULL, **tail = &result;
745         struct commit **other;
746         size_t num_head, num_other;
748         if (!heads)
749                 return NULL;
751         /* Avoid unnecessary reallocations */
752         for (p = heads, num_head = 0; p; p = p->next)
753                 num_head++;
754         other = xcalloc(sizeof(*other), num_head);
756         /* For each commit, see if it can be reached by others */
757         for (p = heads; p; p = p->next) {
758                 struct commit_list *q, *base;
760                 /* Do we already have this in the result? */
761                 for (q = result; q; q = q->next)
762                         if (p->item == q->item)
763                                 break;
764                 if (q)
765                         continue;
767                 num_other = 0;
768                 for (q = heads; q; q = q->next) {
769                         if (p->item == q->item)
770                                 continue;
771                         other[num_other++] = q->item;
772                 }
773                 if (num_other)
774                         base = get_merge_bases_many(p->item, num_other, other, 1);
775                 else
776                         base = NULL;
777                 /*
778                  * If p->item does not have anything common with other
779                  * commits, there won't be any merge base.  If it is
780                  * reachable from some of the others, p->item will be
781                  * the merge base.  If its history is connected with
782                  * others, but p->item is not reachable by others, we
783                  * will get something other than p->item back.
784                  */
785                 if (!base || (base->item != p->item))
786                         tail = &(commit_list_insert(p->item, tail)->next);
787                 free_commit_list(base);
788         }
789         free(other);
790         return result;