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
,
17 if (obj
->type
!= OBJ_COMMIT
) {
19 error("Object %s is a %s, not a commit",
20 sha1_to_hex(sha1
), typename(obj
->type
));
23 return (struct commit
*) obj
;
26 struct commit
*lookup_commit_reference_gently(const unsigned char *sha1
,
29 struct object
*obj
= deref_tag(parse_object(sha1
), NULL
, 0);
33 return check_commit(obj
, sha1
, quiet
);
36 struct commit
*lookup_commit_reference(const unsigned char *sha1
)
38 return lookup_commit_reference_gently(sha1
, 0);
41 struct commit
*lookup_commit(const unsigned char *sha1
)
43 struct object
*obj
= lookup_object(sha1
);
45 return create_object(sha1
, OBJ_COMMIT
, alloc_commit_node());
47 obj
->type
= OBJ_COMMIT
;
48 return check_commit(obj
, sha1
, 0);
51 static unsigned long parse_commit_date(const char *buf
, const char *tail
)
57 if (memcmp(buf
, "author", 6))
59 while (buf
< tail
&& *buf
++ != '\n')
63 if (memcmp(buf
, "committer", 9))
65 while (buf
< tail
&& *buf
++ != '>')
70 while (buf
< tail
&& *buf
++ != '\n')
74 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
75 return strtoul(dateptr
, NULL
, 10);
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
)
87 int mi
= (lo
+ hi
) / 2;
88 struct commit_graft
*graft
= commit_graft
[mi
];
89 int cmp
= hashcmp(sha1
, graft
->sha1
);
100 int register_commit_graft(struct commit_graft
*graft
, int ignore_dups
)
102 int pos
= commit_graft_pos(graft
->sha1
);
108 free(commit_graft
[pos
]);
109 commit_graft
[pos
] = graft
;
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
) *
120 if (pos
< commit_graft_nr
)
121 memmove(commit_graft
+ pos
+ 1,
123 (commit_graft_nr
- pos
- 1) *
124 sizeof(*commit_graft
));
125 commit_graft
[pos
] = graft
;
129 struct commit_graft
*read_graft_line(char *buf
, int len
)
131 /* The format is just "Commit Parent1 Parent2 ...\n" */
133 struct commit_graft
*graft
= NULL
;
135 if (buf
[len
-1] == '\n')
137 if (buf
[0] == '#' || buf
[0] == '\0')
139 if ((len
+ 1) % 41) {
141 error("bad graft data: %s", buf
);
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
))
150 for (i
= 40; i
< len
; i
+= 41) {
153 if (get_sha1_hex(buf
+ i
+ 1, graft
->parent
[i
/41]))
159 static int read_graft_file(const char *graft_file
)
161 FILE *fp
= fopen(graft_file
, "r");
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
);
171 if (register_commit_graft(graft
, 1))
172 error("duplicate graft data: %s", buf
);
178 static void prepare_commit_graft(void)
180 static int commit_graft_prepared
;
183 if (commit_graft_prepared
)
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
)
195 prepare_commit_graft();
196 pos
= commit_graft_pos(sha1
);
199 return commit_graft
[pos
];
202 int write_shallow_commits(int fd
, int use_pack_protocol
)
205 for (i
= 0; i
< commit_graft_nr
; i
++)
206 if (commit_graft
[i
]->nr_parent
< 0) {
208 sha1_to_hex(commit_graft
[i
]->sha1
);
210 if (use_pack_protocol
)
211 packet_write(fd
, "shallow %s", hex
);
213 if (write_in_full(fd
, hex
, 40) != 40)
215 if (write_str_in_full(fd
, "\n") != 1)
222 int unregister_shallow(const unsigned char *sha1
)
224 int pos
= commit_graft_pos(sha1
);
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));
235 int parse_commit_buffer(struct commit
*item
, void *buffer
, unsigned long size
)
238 char *bufptr
= buffer
;
239 unsigned char parent
[20];
240 struct commit_list
**pptr
;
241 struct commit_graft
*graft
;
243 if (item
->object
.parsed
)
245 item
->object
.parsed
= 1;
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 while (pop_commit(pptr
))
256 ; /* clear anything from cache */
258 graft
= lookup_commit_graft(item
->object
.sha1
);
259 while (bufptr
+ 48 < tail
&& !memcmp(bufptr
, "parent ", 7)) {
260 struct commit
*new_parent
;
262 if (tail
<= bufptr
+ 48 ||
263 get_sha1_hex(bufptr
+ 7, parent
) ||
265 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
268 * The clone is shallow if nr_parent < 0, and we must
269 * not traverse its real parents even when we unhide them.
271 if (graft
&& (graft
->nr_parent
< 0 || grafts_replace_parents
))
273 new_parent
= lookup_commit(parent
);
275 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
279 struct commit
*new_parent
;
280 for (i
= 0; i
< graft
->nr_parent
; i
++) {
281 new_parent
= lookup_commit(graft
->parent
[i
]);
284 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
287 item
->date
= parse_commit_date(bufptr
, tail
);
292 int parse_commit(struct commit
*item
)
294 enum object_type type
;
301 if (item
->object
.parsed
)
303 buffer
= read_sha1_file(item
->object
.sha1
, &type
, &size
);
305 return error("Could not read %s",
306 sha1_to_hex(item
->object
.sha1
));
307 if (type
!= OBJ_COMMIT
) {
309 return error("Object %s not a commit",
310 sha1_to_hex(item
->object
.sha1
));
312 ret
= parse_commit_buffer(item
, buffer
, size
);
313 if (save_commit_buffer
&& !ret
) {
314 item
->buffer
= buffer
;
321 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
323 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
324 new_list
->item
= item
;
325 new_list
->next
= *list_p
;
330 unsigned commit_list_count(const struct commit_list
*l
)
333 for (; l
; l
= l
->next
)
338 void free_commit_list(struct commit_list
*list
)
341 struct commit_list
*temp
= list
;
347 struct commit_list
* insert_by_date(struct commit
*item
, struct commit_list
**list
)
349 struct commit_list
**pp
= list
;
350 struct commit_list
*p
;
351 while ((p
= *pp
) != NULL
) {
352 if (p
->item
->date
< item
->date
) {
357 return commit_list_insert(item
, pp
);
361 void sort_by_date(struct commit_list
**list
)
363 struct commit_list
*ret
= NULL
;
365 insert_by_date((*list
)->item
, &ret
);
366 *list
= (*list
)->next
;
371 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
374 struct commit
*ret
= (*list
)->item
;
375 struct commit_list
*parents
= ret
->parents
;
376 struct commit_list
*old
= *list
;
378 *list
= (*list
)->next
;
382 struct commit
*commit
= parents
->item
;
383 if (!parse_commit(commit
) && !(commit
->object
.flags
& mark
)) {
384 commit
->object
.flags
|= mark
;
385 insert_by_date(commit
, list
);
387 parents
= parents
->next
;
392 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
395 struct commit_list
*parents
;
397 if (!(mark
& commit
->object
.flags
))
400 commit
->object
.flags
&= ~mark
;
402 parents
= commit
->parents
;
406 while ((parents
= parents
->next
))
407 clear_commit_marks(parents
->item
, mark
);
409 commit
= commit
->parents
->item
;
413 struct commit
*pop_commit(struct commit_list
**stack
)
415 struct commit_list
*top
= *stack
;
416 struct commit
*item
= top
? top
->item
: NULL
;
426 * Performs an in-place topological sort on the list supplied.
428 void sort_in_topological_order(struct commit_list
** list
, int lifo
)
430 struct commit_list
*next
, *orig
= *list
;
431 struct commit_list
*work
, **insert
;
432 struct commit_list
**pptr
;
438 /* Mark them and clear the indegree */
439 for (next
= orig
; next
; next
= next
->next
) {
440 struct commit
*commit
= next
->item
;
441 commit
->indegree
= 1;
444 /* update the indegree */
445 for (next
= orig
; next
; next
= next
->next
) {
446 struct commit_list
* parents
= next
->item
->parents
;
448 struct commit
*parent
= parents
->item
;
450 if (parent
->indegree
)
452 parents
= parents
->next
;
459 * tips are nodes not reachable from any other node in the list
461 * the tips serve as a starting set for the work queue.
465 for (next
= orig
; next
; next
= next
->next
) {
466 struct commit
*commit
= next
->item
;
468 if (commit
->indegree
== 1)
469 insert
= &commit_list_insert(commit
, insert
)->next
;
472 /* process the list in topological order */
479 struct commit
*commit
;
480 struct commit_list
*parents
, *work_item
;
483 work
= work_item
->next
;
484 work_item
->next
= NULL
;
486 commit
= work_item
->item
;
487 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
488 struct commit
*parent
=parents
->item
;
490 if (!parent
->indegree
)
494 * parents are only enqueued for emission
495 * when all their children have been emitted thereby
496 * guaranteeing topological order.
498 if (--parent
->indegree
== 1) {
500 insert_by_date(parent
, &work
);
502 commit_list_insert(parent
, &work
);
506 * work_item is a commit all of whose children
507 * have already been emitted. we can emit it now.
509 commit
->indegree
= 0;
511 pptr
= &work_item
->next
;
515 /* merge-base stuff */
517 /* bits #0..15 in revision.h */
518 #define PARENT1 (1u<<16)
519 #define PARENT2 (1u<<17)
520 #define STALE (1u<<18)
521 #define RESULT (1u<<19)
523 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
525 static struct commit
*interesting(struct commit_list
*list
)
528 struct commit
*commit
= list
->item
;
530 if (commit
->object
.flags
& STALE
)
537 static struct commit_list
*merge_bases_many(struct commit
*one
, int n
, struct commit
**twos
)
539 struct commit_list
*list
= NULL
;
540 struct commit_list
*result
= NULL
;
543 for (i
= 0; i
< n
; i
++) {
546 * We do not mark this even with RESULT so we do not
547 * have to clean it up.
549 return commit_list_insert(one
, &result
);
552 if (parse_commit(one
))
554 for (i
= 0; i
< n
; i
++) {
555 if (parse_commit(twos
[i
]))
559 one
->object
.flags
|= PARENT1
;
560 insert_by_date(one
, &list
);
561 for (i
= 0; i
< n
; i
++) {
562 twos
[i
]->object
.flags
|= PARENT2
;
563 insert_by_date(twos
[i
], &list
);
566 while (interesting(list
)) {
567 struct commit
*commit
;
568 struct commit_list
*parents
;
569 struct commit_list
*next
;
577 flags
= commit
->object
.flags
& (PARENT1
| PARENT2
| STALE
);
578 if (flags
== (PARENT1
| PARENT2
)) {
579 if (!(commit
->object
.flags
& RESULT
)) {
580 commit
->object
.flags
|= RESULT
;
581 insert_by_date(commit
, &result
);
583 /* Mark parents of a found merge stale */
586 parents
= commit
->parents
;
588 struct commit
*p
= parents
->item
;
589 parents
= parents
->next
;
590 if ((p
->object
.flags
& flags
) == flags
)
594 p
->object
.flags
|= flags
;
595 insert_by_date(p
, &list
);
599 /* Clean up the result to remove stale ones */
600 free_commit_list(list
);
601 list
= result
; result
= NULL
;
603 struct commit_list
*next
= list
->next
;
604 if (!(list
->item
->object
.flags
& STALE
))
605 insert_by_date(list
->item
, &result
);
612 struct commit_list
*get_octopus_merge_bases(struct commit_list
*in
)
614 struct commit_list
*i
, *j
, *k
, *ret
= NULL
;
615 struct commit_list
**pptr
= &ret
;
617 for (i
= in
; i
; i
= i
->next
) {
619 pptr
= &commit_list_insert(i
->item
, pptr
)->next
;
621 struct commit_list
*new = NULL
, *end
= NULL
;
623 for (j
= ret
; j
; j
= j
->next
) {
624 struct commit_list
*bases
;
625 bases
= get_merge_bases(i
->item
, j
->item
, 1);
630 for (k
= bases
; k
; k
= k
->next
)
639 struct commit_list
*get_merge_bases_many(struct commit
*one
,
641 struct commit
**twos
,
644 struct commit_list
*list
;
645 struct commit
**rslt
;
646 struct commit_list
*result
;
649 result
= merge_bases_many(one
, n
, twos
);
650 for (i
= 0; i
< n
; i
++) {
654 if (!result
|| !result
->next
) {
656 clear_commit_marks(one
, all_flags
);
657 for (i
= 0; i
< n
; i
++)
658 clear_commit_marks(twos
[i
], all_flags
);
663 /* There are more than one */
670 rslt
= xcalloc(cnt
, sizeof(*rslt
));
671 for (list
= result
, i
= 0; list
; list
= list
->next
)
672 rslt
[i
++] = list
->item
;
673 free_commit_list(result
);
675 clear_commit_marks(one
, all_flags
);
676 for (i
= 0; i
< n
; i
++)
677 clear_commit_marks(twos
[i
], all_flags
);
678 for (i
= 0; i
< cnt
- 1; i
++) {
679 for (j
= i
+1; j
< cnt
; j
++) {
680 if (!rslt
[i
] || !rslt
[j
])
682 result
= merge_bases_many(rslt
[i
], 1, &rslt
[j
]);
683 clear_commit_marks(rslt
[i
], all_flags
);
684 clear_commit_marks(rslt
[j
], all_flags
);
685 for (list
= result
; list
; list
= list
->next
) {
686 if (rslt
[i
] == list
->item
)
688 if (rslt
[j
] == list
->item
)
694 /* Surviving ones in rslt[] are the independent results */
696 for (i
= 0; i
< cnt
; i
++) {
698 insert_by_date(rslt
[i
], &result
);
704 struct commit_list
*get_merge_bases(struct commit
*one
, struct commit
*two
,
707 return get_merge_bases_many(one
, 1, &two
, cleanup
);
710 int is_descendant_of(struct commit
*commit
, struct commit_list
*with_commit
)
714 while (with_commit
) {
715 struct commit
*other
;
717 other
= with_commit
->item
;
718 with_commit
= with_commit
->next
;
719 if (in_merge_bases(other
, &commit
, 1))
725 int in_merge_bases(struct commit
*commit
, struct commit
**reference
, int num
)
727 struct commit_list
*bases
, *b
;
731 bases
= get_merge_bases(commit
, *reference
, 1);
734 for (b
= bases
; b
; b
= b
->next
) {
735 if (!hashcmp(commit
->object
.sha1
, b
->item
->object
.sha1
)) {
741 free_commit_list(bases
);
745 struct commit_list
*reduce_heads(struct commit_list
*heads
)
747 struct commit_list
*p
;
748 struct commit_list
*result
= NULL
, **tail
= &result
;
749 struct commit
**other
;
750 size_t num_head
, num_other
;
755 /* Avoid unnecessary reallocations */
756 for (p
= heads
, num_head
= 0; p
; p
= p
->next
)
758 other
= xcalloc(sizeof(*other
), num_head
);
760 /* For each commit, see if it can be reached by others */
761 for (p
= heads
; p
; p
= p
->next
) {
762 struct commit_list
*q
, *base
;
764 /* Do we already have this in the result? */
765 for (q
= result
; q
; q
= q
->next
)
766 if (p
->item
== q
->item
)
772 for (q
= heads
; q
; q
= q
->next
) {
773 if (p
->item
== q
->item
)
775 other
[num_other
++] = q
->item
;
778 base
= get_merge_bases_many(p
->item
, num_other
, other
, 1);
782 * If p->item does not have anything common with other
783 * commits, there won't be any merge base. If it is
784 * reachable from some of the others, p->item will be
785 * the merge base. If its history is connected with
786 * others, but p->item is not reachable by others, we
787 * will get something other than p->item back.
789 if (!base
|| (base
->item
!= p
->item
))
790 tail
= &(commit_list_insert(p
->item
, tail
)->next
);
791 free_commit_list(base
);