10 int save_commit_buffer
= 1;
12 const char *commit_type
= "commit";
14 static struct commit
*check_commit(struct object
*obj
,
15 const unsigned char *sha1
,
18 if (obj
->type
!= OBJ_COMMIT
) {
20 error("Object %s is a %s, not a commit",
21 sha1_to_hex(sha1
), typename(obj
->type
));
24 return (struct commit
*) obj
;
27 struct commit
*lookup_commit_reference_gently(const unsigned char *sha1
,
30 struct object
*obj
= deref_tag(parse_object(sha1
), NULL
, 0);
34 return check_commit(obj
, sha1
, quiet
);
37 struct commit
*lookup_commit_reference(const unsigned char *sha1
)
39 return lookup_commit_reference_gently(sha1
, 0);
42 struct commit
*lookup_commit(const unsigned char *sha1
)
44 struct object
*obj
= lookup_object(sha1
);
46 return create_object(sha1
, OBJ_COMMIT
, alloc_commit_node());
48 obj
->type
= OBJ_COMMIT
;
49 return check_commit(obj
, sha1
, 0);
52 static unsigned long parse_commit_date(const char *buf
, const char *tail
)
58 if (memcmp(buf
, "author", 6))
60 while (buf
< tail
&& *buf
++ != '\n')
64 if (memcmp(buf
, "committer", 9))
66 while (buf
< tail
&& *buf
++ != '>')
71 while (buf
< tail
&& *buf
++ != '\n')
75 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
76 return strtoul(dateptr
, NULL
, 10);
79 static struct commit_graft
**commit_graft
;
80 static int commit_graft_alloc
, commit_graft_nr
;
82 static int commit_graft_pos(const unsigned char *sha1
)
88 int mi
= (lo
+ hi
) / 2;
89 struct commit_graft
*graft
= commit_graft
[mi
];
90 int cmp
= hashcmp(sha1
, graft
->sha1
);
101 int register_commit_graft(struct commit_graft
*graft
, int ignore_dups
)
103 int pos
= commit_graft_pos(graft
->sha1
);
109 free(commit_graft
[pos
]);
110 commit_graft
[pos
] = graft
;
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
) *
121 if (pos
< commit_graft_nr
)
122 memmove(commit_graft
+ pos
+ 1,
124 (commit_graft_nr
- pos
- 1) *
125 sizeof(*commit_graft
));
126 commit_graft
[pos
] = graft
;
130 struct commit_graft
*read_graft_line(char *buf
, int len
)
132 /* The format is just "Commit Parent1 Parent2 ...\n" */
134 struct commit_graft
*graft
= NULL
;
136 if (buf
[len
-1] == '\n')
138 if (buf
[0] == '#' || buf
[0] == '\0')
140 if ((len
+ 1) % 41) {
142 error("bad graft data: %s", buf
);
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
))
151 for (i
= 40; i
< len
; i
+= 41) {
154 if (get_sha1_hex(buf
+ i
+ 1, graft
->parent
[i
/41]))
160 static int read_graft_file(const char *graft_file
)
162 FILE *fp
= fopen(graft_file
, "r");
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
);
172 if (register_commit_graft(graft
, 1))
173 error("duplicate graft data: %s", buf
);
179 static void prepare_commit_graft(void)
181 static int commit_graft_prepared
;
184 if (commit_graft_prepared
)
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;
193 struct commit_graft
*lookup_commit_graft(const unsigned char *sha1
)
196 prepare_commit_graft();
197 pos
= commit_graft_pos(sha1
);
200 return commit_graft
[pos
];
203 int write_shallow_commits(int fd
, int use_pack_protocol
)
206 for (i
= 0; i
< commit_graft_nr
; i
++)
207 if (commit_graft
[i
]->nr_parent
< 0) {
209 sha1_to_hex(commit_graft
[i
]->sha1
);
211 if (use_pack_protocol
)
212 packet_write(fd
, "shallow %s", hex
);
214 if (write_in_full(fd
, hex
, 40) != 40)
216 if (write_str_in_full(fd
, "\n") != 1)
223 int unregister_shallow(const unsigned char *sha1
)
225 int pos
= commit_graft_pos(sha1
);
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));
236 int parse_commit_buffer(struct commit
*item
, void *buffer
, unsigned long size
)
239 char *bufptr
= buffer
;
240 unsigned char parent
[20];
241 struct commit_list
**pptr
;
242 struct commit_graft
*graft
;
244 if (item
->object
.parsed
)
246 item
->object
.parsed
= 1;
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 while (pop_commit(pptr
))
257 ; /* clear anything from cache */
259 graft
= lookup_commit_graft(item
->object
.sha1
);
260 while (bufptr
+ 48 < tail
&& !memcmp(bufptr
, "parent ", 7)) {
261 struct commit
*new_parent
;
263 if (tail
<= bufptr
+ 48 ||
264 get_sha1_hex(bufptr
+ 7, parent
) ||
266 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
269 * The clone is shallow if nr_parent < 0, and we must
270 * not traverse its real parents even when we unhide them.
272 if (graft
&& (graft
->nr_parent
< 0 || grafts_replace_parents
))
274 new_parent
= lookup_commit(parent
);
276 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
280 struct commit
*new_parent
;
281 for (i
= 0; i
< graft
->nr_parent
; i
++) {
282 new_parent
= lookup_commit(graft
->parent
[i
]);
285 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
288 item
->date
= parse_commit_date(bufptr
, tail
);
293 int parse_commit(struct commit
*item
)
295 enum object_type type
;
302 if (item
->object
.parsed
)
304 buffer
= read_sha1_file(item
->object
.sha1
, &type
, &size
);
306 return error("Could not read %s",
307 sha1_to_hex(item
->object
.sha1
));
308 if (type
!= OBJ_COMMIT
) {
310 return error("Object %s not a commit",
311 sha1_to_hex(item
->object
.sha1
));
313 ret
= parse_commit_buffer(item
, buffer
, size
);
314 if (save_commit_buffer
&& !ret
) {
315 item
->buffer
= buffer
;
322 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
324 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
325 new_list
->item
= item
;
326 new_list
->next
= *list_p
;
331 unsigned commit_list_count(const struct commit_list
*l
)
334 for (; l
; l
= l
->next
)
339 void free_commit_list(struct commit_list
*list
)
342 struct commit_list
*temp
= list
;
348 struct commit_list
* insert_by_date(struct commit
*item
, struct commit_list
**list
)
350 struct commit_list
**pp
= list
;
351 struct commit_list
*p
;
352 while ((p
= *pp
) != NULL
) {
353 if (p
->item
->date
< item
->date
) {
358 return commit_list_insert(item
, pp
);
362 void sort_by_date(struct commit_list
**list
)
364 struct commit_list
*ret
= NULL
;
366 insert_by_date((*list
)->item
, &ret
);
367 *list
= (*list
)->next
;
372 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
375 struct commit
*ret
= (*list
)->item
;
376 struct commit_list
*parents
= ret
->parents
;
377 struct commit_list
*old
= *list
;
379 *list
= (*list
)->next
;
383 struct commit
*commit
= parents
->item
;
384 if (!parse_commit(commit
) && !(commit
->object
.flags
& mark
)) {
385 commit
->object
.flags
|= mark
;
386 insert_by_date(commit
, list
);
388 parents
= parents
->next
;
393 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
396 struct commit_list
*parents
;
398 if (!(mark
& commit
->object
.flags
))
401 commit
->object
.flags
&= ~mark
;
403 parents
= commit
->parents
;
407 while ((parents
= parents
->next
))
408 clear_commit_marks(parents
->item
, mark
);
410 commit
= commit
->parents
->item
;
414 struct commit
*pop_commit(struct commit_list
**stack
)
416 struct commit_list
*top
= *stack
;
417 struct commit
*item
= top
? top
->item
: NULL
;
427 * Performs an in-place topological sort on the list supplied.
429 void sort_in_topological_order(struct commit_list
** list
, int lifo
)
431 struct commit_list
*next
, *orig
= *list
;
432 struct commit_list
*work
, **insert
;
433 struct commit_list
**pptr
;
439 /* Mark them and clear the indegree */
440 for (next
= orig
; next
; next
= next
->next
) {
441 struct commit
*commit
= next
->item
;
442 commit
->indegree
= 1;
445 /* update the indegree */
446 for (next
= orig
; next
; next
= next
->next
) {
447 struct commit_list
* parents
= next
->item
->parents
;
449 struct commit
*parent
= parents
->item
;
451 if (parent
->indegree
)
453 parents
= parents
->next
;
460 * tips are nodes not reachable from any other node in the list
462 * the tips serve as a starting set for the work queue.
466 for (next
= orig
; next
; next
= next
->next
) {
467 struct commit
*commit
= next
->item
;
469 if (commit
->indegree
== 1)
470 insert
= &commit_list_insert(commit
, insert
)->next
;
473 /* process the list in topological order */
480 struct commit
*commit
;
481 struct commit_list
*parents
, *work_item
;
484 work
= work_item
->next
;
485 work_item
->next
= NULL
;
487 commit
= work_item
->item
;
488 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
489 struct commit
*parent
=parents
->item
;
491 if (!parent
->indegree
)
495 * parents are only enqueued for emission
496 * when all their children have been emitted thereby
497 * guaranteeing topological order.
499 if (--parent
->indegree
== 1) {
501 insert_by_date(parent
, &work
);
503 commit_list_insert(parent
, &work
);
507 * work_item is a commit all of whose children
508 * have already been emitted. we can emit it now.
510 commit
->indegree
= 0;
512 pptr
= &work_item
->next
;
516 /* merge-base stuff */
518 /* bits #0..15 in revision.h */
519 #define PARENT1 (1u<<16)
520 #define PARENT2 (1u<<17)
521 #define STALE (1u<<18)
522 #define RESULT (1u<<19)
524 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
526 static struct commit
*interesting(struct commit_list
*list
)
529 struct commit
*commit
= list
->item
;
531 if (commit
->object
.flags
& STALE
)
538 static struct commit_list
*merge_bases_many(struct commit
*one
, int n
, struct commit
**twos
)
540 struct commit_list
*list
= NULL
;
541 struct commit_list
*result
= NULL
;
544 for (i
= 0; i
< n
; i
++) {
547 * We do not mark this even with RESULT so we do not
548 * have to clean it up.
550 return commit_list_insert(one
, &result
);
553 if (parse_commit(one
))
555 for (i
= 0; i
< n
; i
++) {
556 if (parse_commit(twos
[i
]))
560 one
->object
.flags
|= PARENT1
;
561 insert_by_date(one
, &list
);
562 for (i
= 0; i
< n
; i
++) {
563 twos
[i
]->object
.flags
|= PARENT2
;
564 insert_by_date(twos
[i
], &list
);
567 while (interesting(list
)) {
568 struct commit
*commit
;
569 struct commit_list
*parents
;
570 struct commit_list
*next
;
578 flags
= commit
->object
.flags
& (PARENT1
| PARENT2
| STALE
);
579 if (flags
== (PARENT1
| PARENT2
)) {
580 if (!(commit
->object
.flags
& RESULT
)) {
581 commit
->object
.flags
|= RESULT
;
582 insert_by_date(commit
, &result
);
584 /* Mark parents of a found merge stale */
587 parents
= commit
->parents
;
589 struct commit
*p
= parents
->item
;
590 parents
= parents
->next
;
591 if ((p
->object
.flags
& flags
) == flags
)
595 p
->object
.flags
|= flags
;
596 insert_by_date(p
, &list
);
600 /* Clean up the result to remove stale ones */
601 free_commit_list(list
);
602 list
= result
; result
= NULL
;
604 struct commit_list
*next
= list
->next
;
605 if (!(list
->item
->object
.flags
& STALE
))
606 insert_by_date(list
->item
, &result
);
613 struct commit_list
*get_octopus_merge_bases(struct commit_list
*in
)
615 struct commit_list
*i
, *j
, *k
, *ret
= NULL
;
616 struct commit_list
**pptr
= &ret
;
618 for (i
= in
; i
; i
= i
->next
) {
620 pptr
= &commit_list_insert(i
->item
, pptr
)->next
;
622 struct commit_list
*new = NULL
, *end
= NULL
;
624 for (j
= ret
; j
; j
= j
->next
) {
625 struct commit_list
*bases
;
626 bases
= get_merge_bases(i
->item
, j
->item
, 1);
631 for (k
= bases
; k
; k
= k
->next
)
640 struct commit_list
*get_merge_bases_many(struct commit
*one
,
642 struct commit
**twos
,
645 struct commit_list
*list
;
646 struct commit
**rslt
;
647 struct commit_list
*result
;
650 result
= merge_bases_many(one
, n
, twos
);
651 for (i
= 0; i
< n
; i
++) {
655 if (!result
|| !result
->next
) {
657 clear_commit_marks(one
, all_flags
);
658 for (i
= 0; i
< n
; i
++)
659 clear_commit_marks(twos
[i
], all_flags
);
664 /* There are more than one */
671 rslt
= xcalloc(cnt
, sizeof(*rslt
));
672 for (list
= result
, i
= 0; list
; list
= list
->next
)
673 rslt
[i
++] = list
->item
;
674 free_commit_list(result
);
676 clear_commit_marks(one
, all_flags
);
677 for (i
= 0; i
< n
; i
++)
678 clear_commit_marks(twos
[i
], all_flags
);
679 for (i
= 0; i
< cnt
- 1; i
++) {
680 for (j
= i
+1; j
< cnt
; j
++) {
681 if (!rslt
[i
] || !rslt
[j
])
683 result
= merge_bases_many(rslt
[i
], 1, &rslt
[j
]);
684 clear_commit_marks(rslt
[i
], all_flags
);
685 clear_commit_marks(rslt
[j
], all_flags
);
686 for (list
= result
; list
; list
= list
->next
) {
687 if (rslt
[i
] == list
->item
)
689 if (rslt
[j
] == list
->item
)
695 /* Surviving ones in rslt[] are the independent results */
697 for (i
= 0; i
< cnt
; i
++) {
699 insert_by_date(rslt
[i
], &result
);
705 struct commit_list
*get_merge_bases(struct commit
*one
, struct commit
*two
,
708 return get_merge_bases_many(one
, 1, &two
, cleanup
);
711 int is_descendant_of(struct commit
*commit
, struct commit_list
*with_commit
)
715 while (with_commit
) {
716 struct commit
*other
;
718 other
= with_commit
->item
;
719 with_commit
= with_commit
->next
;
720 if (in_merge_bases(other
, &commit
, 1))
726 int in_merge_bases(struct commit
*commit
, struct commit
**reference
, int num
)
728 struct commit_list
*bases
, *b
;
732 bases
= get_merge_bases(commit
, *reference
, 1);
735 for (b
= bases
; b
; b
= b
->next
) {
736 if (!hashcmp(commit
->object
.sha1
, b
->item
->object
.sha1
)) {
742 free_commit_list(bases
);
746 struct commit_list
*reduce_heads(struct commit_list
*heads
)
748 struct commit_list
*p
;
749 struct commit_list
*result
= NULL
, **tail
= &result
;
750 struct commit
**other
;
751 size_t num_head
, num_other
;
756 /* Avoid unnecessary reallocations */
757 for (p
= heads
, num_head
= 0; p
; p
= p
->next
)
759 other
= xcalloc(sizeof(*other
), num_head
);
761 /* For each commit, see if it can be reached by others */
762 for (p
= heads
; p
; p
= p
->next
) {
763 struct commit_list
*q
, *base
;
765 /* Do we already have this in the result? */
766 for (q
= result
; q
; q
= q
->next
)
767 if (p
->item
== q
->item
)
773 for (q
= heads
; q
; q
= q
->next
) {
774 if (p
->item
== q
->item
)
776 other
[num_other
++] = q
->item
;
779 base
= get_merge_bases_many(p
->item
, num_other
, other
, 1);
783 * If p->item does not have anything common with other
784 * commits, there won't be any merge base. If it is
785 * reachable from some of the others, p->item will be
786 * the merge base. If its history is connected with
787 * others, but p->item is not reachable by others, we
788 * will get something other than p->item back.
790 if (!base
|| (base
->item
!= p
->item
))
791 tail
= &(commit_list_insert(p
->item
, tail
)->next
);
792 free_commit_list(base
);