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
)
59 if (memcmp(buf
, "author", 6))
61 while (buf
< tail
&& *buf
++ != '\n')
65 if (memcmp(buf
, "committer", 9))
67 while (buf
< tail
&& *buf
++ != '>')
72 while (buf
< tail
&& *buf
++ != '\n')
76 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
77 date
= strtoul(dateptr
, NULL
, 10);
78 if (date
== ULONG_MAX
)
83 static struct commit_graft
**commit_graft
;
84 static int commit_graft_alloc
, commit_graft_nr
;
86 static int commit_graft_pos(const unsigned char *sha1
)
92 int mi
= (lo
+ hi
) / 2;
93 struct commit_graft
*graft
= commit_graft
[mi
];
94 int cmp
= hashcmp(sha1
, graft
->sha1
);
105 int register_commit_graft(struct commit_graft
*graft
, int ignore_dups
)
107 int pos
= commit_graft_pos(graft
->sha1
);
113 free(commit_graft
[pos
]);
114 commit_graft
[pos
] = graft
;
119 if (commit_graft_alloc
<= ++commit_graft_nr
) {
120 commit_graft_alloc
= alloc_nr(commit_graft_alloc
);
121 commit_graft
= xrealloc(commit_graft
,
122 sizeof(*commit_graft
) *
125 if (pos
< commit_graft_nr
)
126 memmove(commit_graft
+ pos
+ 1,
128 (commit_graft_nr
- pos
- 1) *
129 sizeof(*commit_graft
));
130 commit_graft
[pos
] = graft
;
134 struct commit_graft
*read_graft_line(char *buf
, int len
)
136 /* The format is just "Commit Parent1 Parent2 ...\n" */
138 struct commit_graft
*graft
= NULL
;
140 if (buf
[len
-1] == '\n')
142 if (buf
[0] == '#' || buf
[0] == '\0')
144 if ((len
+ 1) % 41) {
146 error("bad graft data: %s", buf
);
150 i
= (len
+ 1) / 41 - 1;
151 graft
= xmalloc(sizeof(*graft
) + 20 * i
);
152 graft
->nr_parent
= i
;
153 if (get_sha1_hex(buf
, graft
->sha1
))
155 for (i
= 40; i
< len
; i
+= 41) {
158 if (get_sha1_hex(buf
+ i
+ 1, graft
->parent
[i
/41]))
164 static int read_graft_file(const char *graft_file
)
166 FILE *fp
= fopen(graft_file
, "r");
170 while (fgets(buf
, sizeof(buf
), fp
)) {
171 /* The format is just "Commit Parent1 Parent2 ...\n" */
172 int len
= strlen(buf
);
173 struct commit_graft
*graft
= read_graft_line(buf
, len
);
176 if (register_commit_graft(graft
, 1))
177 error("duplicate graft data: %s", buf
);
183 static void prepare_commit_graft(void)
185 static int commit_graft_prepared
;
188 if (commit_graft_prepared
)
190 graft_file
= get_graft_file();
191 read_graft_file(graft_file
);
192 /* make sure shallows are read */
193 is_repository_shallow();
194 commit_graft_prepared
= 1;
197 struct commit_graft
*lookup_commit_graft(const unsigned char *sha1
)
200 prepare_commit_graft();
201 pos
= commit_graft_pos(sha1
);
204 return commit_graft
[pos
];
207 int write_shallow_commits(int fd
, int use_pack_protocol
)
210 for (i
= 0; i
< commit_graft_nr
; i
++)
211 if (commit_graft
[i
]->nr_parent
< 0) {
213 sha1_to_hex(commit_graft
[i
]->sha1
);
215 if (use_pack_protocol
)
216 packet_write(fd
, "shallow %s", hex
);
218 if (write_in_full(fd
, hex
, 40) != 40)
220 if (write_in_full(fd
, "\n", 1) != 1)
227 int unregister_shallow(const unsigned char *sha1
)
229 int pos
= commit_graft_pos(sha1
);
232 if (pos
+ 1 < commit_graft_nr
)
233 memcpy(commit_graft
+ pos
, commit_graft
+ pos
+ 1,
234 sizeof(struct commit_graft
*)
235 * (commit_graft_nr
- pos
- 1));
240 int parse_commit_buffer(struct commit
*item
, void *buffer
, unsigned long size
)
243 char *bufptr
= buffer
;
244 unsigned char parent
[20];
245 struct commit_list
**pptr
;
246 struct commit_graft
*graft
;
248 if (item
->object
.parsed
)
250 item
->object
.parsed
= 1;
252 if (tail
<= bufptr
+ 46 || memcmp(bufptr
, "tree ", 5) || bufptr
[45] != '\n')
253 return error("bogus commit object %s", sha1_to_hex(item
->object
.sha1
));
254 if (get_sha1_hex(bufptr
+ 5, parent
) < 0)
255 return error("bad tree pointer in commit %s",
256 sha1_to_hex(item
->object
.sha1
));
257 item
->tree
= lookup_tree(parent
);
258 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
259 pptr
= &item
->parents
;
261 graft
= lookup_commit_graft(item
->object
.sha1
);
262 while (bufptr
+ 48 < tail
&& !memcmp(bufptr
, "parent ", 7)) {
263 struct commit
*new_parent
;
265 if (tail
<= bufptr
+ 48 ||
266 get_sha1_hex(bufptr
+ 7, parent
) ||
268 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
272 new_parent
= lookup_commit(parent
);
274 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
278 struct commit
*new_parent
;
279 for (i
= 0; i
< graft
->nr_parent
; i
++) {
280 new_parent
= lookup_commit(graft
->parent
[i
]);
283 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
286 item
->date
= parse_commit_date(bufptr
, tail
);
291 int parse_commit(struct commit
*item
)
293 enum object_type type
;
300 if (item
->object
.parsed
)
302 buffer
= read_sha1_file(item
->object
.sha1
, &type
, &size
);
304 return error("Could not read %s",
305 sha1_to_hex(item
->object
.sha1
));
306 if (type
!= OBJ_COMMIT
) {
308 return error("Object %s not a commit",
309 sha1_to_hex(item
->object
.sha1
));
311 ret
= parse_commit_buffer(item
, buffer
, size
);
312 if (save_commit_buffer
&& !ret
) {
313 item
->buffer
= buffer
;
320 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
322 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
323 new_list
->item
= item
;
324 new_list
->next
= *list_p
;
329 unsigned commit_list_count(const struct commit_list
*l
)
332 for (; l
; l
= l
->next
)
337 void free_commit_list(struct commit_list
*list
)
340 struct commit_list
*temp
= list
;
346 struct commit_list
* insert_by_date(struct commit
*item
, struct commit_list
**list
)
348 struct commit_list
**pp
= list
;
349 struct commit_list
*p
;
350 while ((p
= *pp
) != NULL
) {
351 if (p
->item
->date
< item
->date
) {
356 return commit_list_insert(item
, pp
);
360 void sort_by_date(struct commit_list
**list
)
362 struct commit_list
*ret
= NULL
;
364 insert_by_date((*list
)->item
, &ret
);
365 *list
= (*list
)->next
;
370 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
373 struct commit
*ret
= (*list
)->item
;
374 struct commit_list
*parents
= ret
->parents
;
375 struct commit_list
*old
= *list
;
377 *list
= (*list
)->next
;
381 struct commit
*commit
= parents
->item
;
382 if (!parse_commit(commit
) && !(commit
->object
.flags
& mark
)) {
383 commit
->object
.flags
|= mark
;
384 insert_by_date(commit
, list
);
386 parents
= parents
->next
;
391 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
394 struct commit_list
*parents
;
396 if (!(mark
& commit
->object
.flags
))
399 commit
->object
.flags
&= ~mark
;
401 parents
= commit
->parents
;
405 while ((parents
= parents
->next
))
406 clear_commit_marks(parents
->item
, mark
);
408 commit
= commit
->parents
->item
;
412 struct commit
*pop_commit(struct commit_list
**stack
)
414 struct commit_list
*top
= *stack
;
415 struct commit
*item
= top
? top
->item
: NULL
;
425 * Performs an in-place topological sort on the list supplied.
427 void sort_in_topological_order(struct commit_list
** list
, int lifo
)
429 struct commit_list
*next
, *orig
= *list
;
430 struct commit_list
*work
, **insert
;
431 struct commit_list
**pptr
;
437 /* Mark them and clear the indegree */
438 for (next
= orig
; next
; next
= next
->next
) {
439 struct commit
*commit
= next
->item
;
440 commit
->indegree
= 1;
443 /* update the indegree */
444 for (next
= orig
; next
; next
= next
->next
) {
445 struct commit_list
* parents
= next
->item
->parents
;
447 struct commit
*parent
= parents
->item
;
449 if (parent
->indegree
)
451 parents
= parents
->next
;
458 * tips are nodes not reachable from any other node in the list
460 * the tips serve as a starting set for the work queue.
464 for (next
= orig
; next
; next
= next
->next
) {
465 struct commit
*commit
= next
->item
;
467 if (commit
->indegree
== 1)
468 insert
= &commit_list_insert(commit
, insert
)->next
;
471 /* process the list in topological order */
478 struct commit
*commit
;
479 struct commit_list
*parents
, *work_item
;
482 work
= work_item
->next
;
483 work_item
->next
= NULL
;
485 commit
= work_item
->item
;
486 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
487 struct commit
*parent
=parents
->item
;
489 if (!parent
->indegree
)
493 * parents are only enqueued for emission
494 * when all their children have been emitted thereby
495 * guaranteeing topological order.
497 if (--parent
->indegree
== 1) {
499 insert_by_date(parent
, &work
);
501 commit_list_insert(parent
, &work
);
505 * work_item is a commit all of whose children
506 * have already been emitted. we can emit it now.
508 commit
->indegree
= 0;
510 pptr
= &work_item
->next
;
514 /* merge-base stuff */
516 /* bits #0..15 in revision.h */
517 #define PARENT1 (1u<<16)
518 #define PARENT2 (1u<<17)
519 #define STALE (1u<<18)
520 #define RESULT (1u<<19)
522 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
524 static struct commit
*interesting(struct commit_list
*list
)
527 struct commit
*commit
= list
->item
;
529 if (commit
->object
.flags
& STALE
)
536 static struct commit_list
*merge_bases_many(struct commit
*one
, int n
, struct commit
**twos
)
538 struct commit_list
*list
= NULL
;
539 struct commit_list
*result
= NULL
;
542 for (i
= 0; i
< n
; i
++) {
545 * We do not mark this even with RESULT so we do not
546 * have to clean it up.
548 return commit_list_insert(one
, &result
);
551 if (parse_commit(one
))
553 for (i
= 0; i
< n
; i
++) {
554 if (parse_commit(twos
[i
]))
558 one
->object
.flags
|= PARENT1
;
559 insert_by_date(one
, &list
);
560 for (i
= 0; i
< n
; i
++) {
561 twos
[i
]->object
.flags
|= PARENT2
;
562 insert_by_date(twos
[i
], &list
);
565 while (interesting(list
)) {
566 struct commit
*commit
;
567 struct commit_list
*parents
;
568 struct commit_list
*n
;
576 flags
= commit
->object
.flags
& (PARENT1
| PARENT2
| STALE
);
577 if (flags
== (PARENT1
| PARENT2
)) {
578 if (!(commit
->object
.flags
& RESULT
)) {
579 commit
->object
.flags
|= RESULT
;
580 insert_by_date(commit
, &result
);
582 /* Mark parents of a found merge stale */
585 parents
= commit
->parents
;
587 struct commit
*p
= parents
->item
;
588 parents
= parents
->next
;
589 if ((p
->object
.flags
& flags
) == flags
)
593 p
->object
.flags
|= flags
;
594 insert_by_date(p
, &list
);
598 /* Clean up the result to remove stale ones */
599 free_commit_list(list
);
600 list
= result
; result
= NULL
;
602 struct commit_list
*n
= list
->next
;
603 if (!(list
->item
->object
.flags
& STALE
))
604 insert_by_date(list
->item
, &result
);
611 struct commit_list
*get_octopus_merge_bases(struct commit_list
*in
)
613 struct commit_list
*i
, *j
, *k
, *ret
= NULL
;
614 struct commit_list
**pptr
= &ret
;
616 for (i
= in
; i
; i
= i
->next
) {
618 pptr
= &commit_list_insert(i
->item
, pptr
)->next
;
620 struct commit_list
*new = NULL
, *end
= NULL
;
622 for (j
= ret
; j
; j
= j
->next
) {
623 struct commit_list
*bases
;
624 bases
= get_merge_bases(i
->item
, j
->item
, 1);
629 for (k
= bases
; k
; k
= k
->next
)
638 struct commit_list
*get_merge_bases_many(struct commit
*one
,
640 struct commit
**twos
,
643 struct commit_list
*list
;
644 struct commit
**rslt
;
645 struct commit_list
*result
;
648 result
= merge_bases_many(one
, n
, twos
);
649 for (i
= 0; i
< n
; i
++) {
653 if (!result
|| !result
->next
) {
655 clear_commit_marks(one
, all_flags
);
656 for (i
= 0; i
< n
; i
++)
657 clear_commit_marks(twos
[i
], all_flags
);
662 /* There are more than one */
669 rslt
= xcalloc(cnt
, sizeof(*rslt
));
670 for (list
= result
, i
= 0; list
; list
= list
->next
)
671 rslt
[i
++] = list
->item
;
672 free_commit_list(result
);
674 clear_commit_marks(one
, all_flags
);
675 for (i
= 0; i
< n
; i
++)
676 clear_commit_marks(twos
[i
], all_flags
);
677 for (i
= 0; i
< cnt
- 1; i
++) {
678 for (j
= i
+1; j
< cnt
; j
++) {
679 if (!rslt
[i
] || !rslt
[j
])
681 result
= merge_bases_many(rslt
[i
], 1, &rslt
[j
]);
682 clear_commit_marks(rslt
[i
], all_flags
);
683 clear_commit_marks(rslt
[j
], all_flags
);
684 for (list
= result
; list
; list
= list
->next
) {
685 if (rslt
[i
] == list
->item
)
687 if (rslt
[j
] == list
->item
)
693 /* Surviving ones in rslt[] are the independent results */
695 for (i
= 0; i
< cnt
; i
++) {
697 insert_by_date(rslt
[i
], &result
);
703 struct commit_list
*get_merge_bases(struct commit
*one
, struct commit
*two
,
706 return get_merge_bases_many(one
, 1, &two
, cleanup
);
709 int is_descendant_of(struct commit
*commit
, struct commit_list
*with_commit
)
713 while (with_commit
) {
714 struct commit
*other
;
716 other
= with_commit
->item
;
717 with_commit
= with_commit
->next
;
718 if (in_merge_bases(other
, &commit
, 1))
724 int in_merge_bases(struct commit
*commit
, struct commit
**reference
, int num
)
726 struct commit_list
*bases
, *b
;
730 bases
= get_merge_bases(commit
, *reference
, 1);
733 for (b
= bases
; b
; b
= b
->next
) {
734 if (!hashcmp(commit
->object
.sha1
, b
->item
->object
.sha1
)) {
740 free_commit_list(bases
);
744 struct commit_list
*reduce_heads(struct commit_list
*heads
)
746 struct commit_list
*p
;
747 struct commit_list
*result
= NULL
, **tail
= &result
;
748 struct commit
**other
;
749 size_t num_head
, num_other
;
754 /* Avoid unnecessary reallocations */
755 for (p
= heads
, num_head
= 0; p
; p
= p
->next
)
757 other
= xcalloc(sizeof(*other
), num_head
);
759 /* For each commit, see if it can be reached by others */
760 for (p
= heads
; p
; p
= p
->next
) {
761 struct commit_list
*q
, *base
;
763 /* Do we already have this in the result? */
764 for (q
= result
; q
; q
= q
->next
)
765 if (p
->item
== q
->item
)
771 for (q
= heads
; q
; q
= q
->next
) {
772 if (p
->item
== q
->item
)
774 other
[num_other
++] = q
->item
;
777 base
= get_merge_bases_many(p
->item
, num_other
, other
, 1);
781 * If p->item does not have anything common with other
782 * commits, there won't be any merge base. If it is
783 * reachable from some of the others, p->item will be
784 * the merge base. If its history is connected with
785 * others, but p->item is not reachable by others, we
786 * will get something other than p->item back.
788 if (!base
|| (base
->item
!= p
->item
))
789 tail
= &(commit_list_insert(p
->item
, tail
)->next
);
790 free_commit_list(base
);