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_in_full(fd
, "\n", 1) != 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
;
256 graft
= lookup_commit_graft(item
->object
.sha1
);
257 while (bufptr
+ 48 < tail
&& !memcmp(bufptr
, "parent ", 7)) {
258 struct commit
*new_parent
;
260 if (tail
<= bufptr
+ 48 ||
261 get_sha1_hex(bufptr
+ 7, parent
) ||
263 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
267 new_parent
= lookup_commit(parent
);
269 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
273 struct commit
*new_parent
;
274 for (i
= 0; i
< graft
->nr_parent
; i
++) {
275 new_parent
= lookup_commit(graft
->parent
[i
]);
278 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
281 item
->date
= parse_commit_date(bufptr
, tail
);
286 int parse_commit(struct commit
*item
)
288 enum object_type type
;
295 if (item
->object
.parsed
)
297 buffer
= read_sha1_file(item
->object
.sha1
, &type
, &size
);
299 return error("Could not read %s",
300 sha1_to_hex(item
->object
.sha1
));
301 if (type
!= OBJ_COMMIT
) {
303 return error("Object %s not a commit",
304 sha1_to_hex(item
->object
.sha1
));
306 ret
= parse_commit_buffer(item
, buffer
, size
);
307 if (save_commit_buffer
&& !ret
) {
308 item
->buffer
= buffer
;
315 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
317 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
318 new_list
->item
= item
;
319 new_list
->next
= *list_p
;
324 unsigned commit_list_count(const struct commit_list
*l
)
327 for (; l
; l
= l
->next
)
332 void free_commit_list(struct commit_list
*list
)
335 struct commit_list
*temp
= list
;
341 struct commit_list
* insert_by_date(struct commit
*item
, struct commit_list
**list
)
343 struct commit_list
**pp
= list
;
344 struct commit_list
*p
;
345 while ((p
= *pp
) != NULL
) {
346 if (p
->item
->date
< item
->date
) {
351 return commit_list_insert(item
, pp
);
355 void sort_by_date(struct commit_list
**list
)
357 struct commit_list
*ret
= NULL
;
359 insert_by_date((*list
)->item
, &ret
);
360 *list
= (*list
)->next
;
365 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
368 struct commit
*ret
= (*list
)->item
;
369 struct commit_list
*parents
= ret
->parents
;
370 struct commit_list
*old
= *list
;
372 *list
= (*list
)->next
;
376 struct commit
*commit
= parents
->item
;
377 if (!parse_commit(commit
) && !(commit
->object
.flags
& mark
)) {
378 commit
->object
.flags
|= mark
;
379 insert_by_date(commit
, list
);
381 parents
= parents
->next
;
386 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
389 struct commit_list
*parents
;
391 if (!(mark
& commit
->object
.flags
))
394 commit
->object
.flags
&= ~mark
;
396 parents
= commit
->parents
;
400 while ((parents
= parents
->next
))
401 clear_commit_marks(parents
->item
, mark
);
403 commit
= commit
->parents
->item
;
407 struct commit
*pop_commit(struct commit_list
**stack
)
409 struct commit_list
*top
= *stack
;
410 struct commit
*item
= top
? top
->item
: NULL
;
420 * Performs an in-place topological sort on the list supplied.
422 void sort_in_topological_order(struct commit_list
** list
, int lifo
)
424 struct commit_list
*next
, *orig
= *list
;
425 struct commit_list
*work
, **insert
;
426 struct commit_list
**pptr
;
432 /* Mark them and clear the indegree */
433 for (next
= orig
; next
; next
= next
->next
) {
434 struct commit
*commit
= next
->item
;
435 commit
->indegree
= 1;
438 /* update the indegree */
439 for (next
= orig
; next
; next
= next
->next
) {
440 struct commit_list
* parents
= next
->item
->parents
;
442 struct commit
*parent
= parents
->item
;
444 if (parent
->indegree
)
446 parents
= parents
->next
;
453 * tips are nodes not reachable from any other node in the list
455 * the tips serve as a starting set for the work queue.
459 for (next
= orig
; next
; next
= next
->next
) {
460 struct commit
*commit
= next
->item
;
462 if (commit
->indegree
== 1)
463 insert
= &commit_list_insert(commit
, insert
)->next
;
466 /* process the list in topological order */
473 struct commit
*commit
;
474 struct commit_list
*parents
, *work_item
;
477 work
= work_item
->next
;
478 work_item
->next
= NULL
;
480 commit
= work_item
->item
;
481 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
482 struct commit
*parent
=parents
->item
;
484 if (!parent
->indegree
)
488 * parents are only enqueued for emission
489 * when all their children have been emitted thereby
490 * guaranteeing topological order.
492 if (--parent
->indegree
== 1) {
494 insert_by_date(parent
, &work
);
496 commit_list_insert(parent
, &work
);
500 * work_item is a commit all of whose children
501 * have already been emitted. we can emit it now.
503 commit
->indegree
= 0;
505 pptr
= &work_item
->next
;
509 /* merge-base stuff */
511 /* bits #0..15 in revision.h */
512 #define PARENT1 (1u<<16)
513 #define PARENT2 (1u<<17)
514 #define STALE (1u<<18)
515 #define RESULT (1u<<19)
517 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
519 static struct commit
*interesting(struct commit_list
*list
)
522 struct commit
*commit
= list
->item
;
524 if (commit
->object
.flags
& STALE
)
531 static struct commit_list
*merge_bases_many(struct commit
*one
, int n
, struct commit
**twos
)
533 struct commit_list
*list
= NULL
;
534 struct commit_list
*result
= NULL
;
537 for (i
= 0; i
< n
; i
++) {
540 * We do not mark this even with RESULT so we do not
541 * have to clean it up.
543 return commit_list_insert(one
, &result
);
546 if (parse_commit(one
))
548 for (i
= 0; i
< n
; i
++) {
549 if (parse_commit(twos
[i
]))
553 one
->object
.flags
|= PARENT1
;
554 insert_by_date(one
, &list
);
555 for (i
= 0; i
< n
; i
++) {
556 twos
[i
]->object
.flags
|= PARENT2
;
557 insert_by_date(twos
[i
], &list
);
560 while (interesting(list
)) {
561 struct commit
*commit
;
562 struct commit_list
*parents
;
563 struct commit_list
*n
;
571 flags
= commit
->object
.flags
& (PARENT1
| PARENT2
| STALE
);
572 if (flags
== (PARENT1
| PARENT2
)) {
573 if (!(commit
->object
.flags
& RESULT
)) {
574 commit
->object
.flags
|= RESULT
;
575 insert_by_date(commit
, &result
);
577 /* Mark parents of a found merge stale */
580 parents
= commit
->parents
;
582 struct commit
*p
= parents
->item
;
583 parents
= parents
->next
;
584 if ((p
->object
.flags
& flags
) == flags
)
588 p
->object
.flags
|= flags
;
589 insert_by_date(p
, &list
);
593 /* Clean up the result to remove stale ones */
594 free_commit_list(list
);
595 list
= result
; result
= NULL
;
597 struct commit_list
*n
= list
->next
;
598 if (!(list
->item
->object
.flags
& STALE
))
599 insert_by_date(list
->item
, &result
);
606 struct commit_list
*get_octopus_merge_bases(struct commit_list
*in
)
608 struct commit_list
*i
, *j
, *k
, *ret
= NULL
;
609 struct commit_list
**pptr
= &ret
;
611 for (i
= in
; i
; i
= i
->next
) {
613 pptr
= &commit_list_insert(i
->item
, pptr
)->next
;
615 struct commit_list
*new = NULL
, *end
= NULL
;
617 for (j
= ret
; j
; j
= j
->next
) {
618 struct commit_list
*bases
;
619 bases
= get_merge_bases(i
->item
, j
->item
, 1);
624 for (k
= bases
; k
; k
= k
->next
)
633 struct commit_list
*get_merge_bases_many(struct commit
*one
,
635 struct commit
**twos
,
638 struct commit_list
*list
;
639 struct commit
**rslt
;
640 struct commit_list
*result
;
643 result
= merge_bases_many(one
, n
, twos
);
644 for (i
= 0; i
< n
; i
++) {
648 if (!result
|| !result
->next
) {
650 clear_commit_marks(one
, all_flags
);
651 for (i
= 0; i
< n
; i
++)
652 clear_commit_marks(twos
[i
], all_flags
);
657 /* There are more than one */
664 rslt
= xcalloc(cnt
, sizeof(*rslt
));
665 for (list
= result
, i
= 0; list
; list
= list
->next
)
666 rslt
[i
++] = list
->item
;
667 free_commit_list(result
);
669 clear_commit_marks(one
, all_flags
);
670 for (i
= 0; i
< n
; i
++)
671 clear_commit_marks(twos
[i
], all_flags
);
672 for (i
= 0; i
< cnt
- 1; i
++) {
673 for (j
= i
+1; j
< cnt
; j
++) {
674 if (!rslt
[i
] || !rslt
[j
])
676 result
= merge_bases_many(rslt
[i
], 1, &rslt
[j
]);
677 clear_commit_marks(rslt
[i
], all_flags
);
678 clear_commit_marks(rslt
[j
], all_flags
);
679 for (list
= result
; list
; list
= list
->next
) {
680 if (rslt
[i
] == list
->item
)
682 if (rslt
[j
] == list
->item
)
688 /* Surviving ones in rslt[] are the independent results */
690 for (i
= 0; i
< cnt
; i
++) {
692 insert_by_date(rslt
[i
], &result
);
698 struct commit_list
*get_merge_bases(struct commit
*one
, struct commit
*two
,
701 return get_merge_bases_many(one
, 1, &two
, cleanup
);
704 int is_descendant_of(struct commit
*commit
, struct commit_list
*with_commit
)
708 while (with_commit
) {
709 struct commit
*other
;
711 other
= with_commit
->item
;
712 with_commit
= with_commit
->next
;
713 if (in_merge_bases(other
, &commit
, 1))
719 int in_merge_bases(struct commit
*commit
, struct commit
**reference
, int num
)
721 struct commit_list
*bases
, *b
;
725 bases
= get_merge_bases(commit
, *reference
, 1);
728 for (b
= bases
; b
; b
= b
->next
) {
729 if (!hashcmp(commit
->object
.sha1
, b
->item
->object
.sha1
)) {
735 free_commit_list(bases
);
739 struct commit_list
*reduce_heads(struct commit_list
*heads
)
741 struct commit_list
*p
;
742 struct commit_list
*result
= NULL
, **tail
= &result
;
743 struct commit
**other
;
744 size_t num_head
, num_other
;
749 /* Avoid unnecessary reallocations */
750 for (p
= heads
, num_head
= 0; p
; p
= p
->next
)
752 other
= xcalloc(sizeof(*other
), num_head
);
754 /* For each commit, see if it can be reached by others */
755 for (p
= heads
; p
; p
= p
->next
) {
756 struct commit_list
*q
, *base
;
758 /* Do we already have this in the result? */
759 for (q
= result
; q
; q
= q
->next
)
760 if (p
->item
== q
->item
)
766 for (q
= heads
; q
; q
= q
->next
) {
767 if (p
->item
== q
->item
)
769 other
[num_other
++] = q
->item
;
772 base
= get_merge_bases_many(p
->item
, num_other
, other
, 1);
776 * If p->item does not have anything common with other
777 * commits, there won't be any merge base. If it is
778 * reachable from some of the others, p->item will be
779 * the merge base. If its history is connected with
780 * others, but p->item is not reachable by others, we
781 * will get something other than p->item back.
783 if (!base
|| (base
->item
!= p
->item
))
784 tail
= &(commit_list_insert(p
->item
, tail
)->next
);
785 free_commit_list(base
);