9 int save_commit_buffer
= 1;
14 * the number of children of the associated commit
15 * that also occur in the list being sorted.
17 unsigned int indegree
;
20 * reference to original list item that we will re-use
23 struct commit_list
* list_item
;
27 const char *commit_type
= "commit";
29 static struct commit
*check_commit(struct object
*obj
,
30 const unsigned char *sha1
,
33 if (obj
->type
!= OBJ_COMMIT
) {
35 error("Object %s is a %s, not a commit",
36 sha1_to_hex(sha1
), typename(obj
->type
));
39 return (struct commit
*) obj
;
42 struct commit
*lookup_commit_reference_gently(const unsigned char *sha1
,
45 struct object
*obj
= deref_tag(parse_object(sha1
), NULL
, 0);
49 return check_commit(obj
, sha1
, quiet
);
52 struct commit
*lookup_commit_reference(const unsigned char *sha1
)
54 return lookup_commit_reference_gently(sha1
, 0);
57 struct commit
*lookup_commit(const unsigned char *sha1
)
59 struct object
*obj
= lookup_object(sha1
);
61 return create_object(sha1
, OBJ_COMMIT
, alloc_commit_node());
63 obj
->type
= OBJ_COMMIT
;
64 return check_commit(obj
, sha1
, 0);
67 static unsigned long parse_commit_date(const char *buf
)
71 if (memcmp(buf
, "author", 6))
73 while (*buf
++ != '\n')
75 if (memcmp(buf
, "committer", 9))
79 date
= strtoul(buf
, NULL
, 10);
80 if (date
== ULONG_MAX
)
85 static struct commit_graft
**commit_graft
;
86 static int commit_graft_alloc
, commit_graft_nr
;
88 static int commit_graft_pos(const unsigned char *sha1
)
94 int mi
= (lo
+ hi
) / 2;
95 struct commit_graft
*graft
= commit_graft
[mi
];
96 int cmp
= hashcmp(sha1
, graft
->sha1
);
107 int register_commit_graft(struct commit_graft
*graft
, int ignore_dups
)
109 int pos
= commit_graft_pos(graft
->sha1
);
115 free(commit_graft
[pos
]);
116 commit_graft
[pos
] = graft
;
121 if (commit_graft_alloc
<= ++commit_graft_nr
) {
122 commit_graft_alloc
= alloc_nr(commit_graft_alloc
);
123 commit_graft
= xrealloc(commit_graft
,
124 sizeof(*commit_graft
) *
127 if (pos
< commit_graft_nr
)
128 memmove(commit_graft
+ pos
+ 1,
130 (commit_graft_nr
- pos
- 1) *
131 sizeof(*commit_graft
));
132 commit_graft
[pos
] = graft
;
136 struct commit_graft
*read_graft_line(char *buf
, int len
)
138 /* The format is just "Commit Parent1 Parent2 ...\n" */
140 struct commit_graft
*graft
= NULL
;
142 if (buf
[len
-1] == '\n')
144 if (buf
[0] == '#' || buf
[0] == '\0')
146 if ((len
+ 1) % 41) {
148 error("bad graft data: %s", buf
);
152 i
= (len
+ 1) / 41 - 1;
153 graft
= xmalloc(sizeof(*graft
) + 20 * i
);
154 graft
->nr_parent
= i
;
155 if (get_sha1_hex(buf
, graft
->sha1
))
157 for (i
= 40; i
< len
; i
+= 41) {
160 if (get_sha1_hex(buf
+ i
+ 1, graft
->parent
[i
/41]))
166 int read_graft_file(const char *graft_file
)
168 FILE *fp
= fopen(graft_file
, "r");
172 while (fgets(buf
, sizeof(buf
), fp
)) {
173 /* The format is just "Commit Parent1 Parent2 ...\n" */
174 int len
= strlen(buf
);
175 struct commit_graft
*graft
= read_graft_line(buf
, len
);
178 if (register_commit_graft(graft
, 1))
179 error("duplicate graft data: %s", buf
);
185 static void prepare_commit_graft(void)
187 static int commit_graft_prepared
;
190 if (commit_graft_prepared
)
192 graft_file
= get_graft_file();
193 read_graft_file(graft_file
);
194 /* make sure shallows are read */
195 is_repository_shallow();
196 commit_graft_prepared
= 1;
199 static struct commit_graft
*lookup_commit_graft(const unsigned char *sha1
)
202 prepare_commit_graft();
203 pos
= commit_graft_pos(sha1
);
206 return commit_graft
[pos
];
209 int write_shallow_commits(int fd
, int use_pack_protocol
)
212 for (i
= 0; i
< commit_graft_nr
; i
++)
213 if (commit_graft
[i
]->nr_parent
< 0) {
215 sha1_to_hex(commit_graft
[i
]->sha1
);
217 if (use_pack_protocol
)
218 packet_write(fd
, "shallow %s", hex
);
220 if (write_in_full(fd
, hex
, 40) != 40)
222 if (write_in_full(fd
, "\n", 1) != 1)
229 int unregister_shallow(const unsigned char *sha1
)
231 int pos
= commit_graft_pos(sha1
);
234 if (pos
+ 1 < commit_graft_nr
)
235 memcpy(commit_graft
+ pos
, commit_graft
+ pos
+ 1,
236 sizeof(struct commit_graft
*)
237 * (commit_graft_nr
- pos
- 1));
242 int parse_commit_buffer(struct commit
*item
, void *buffer
, unsigned long size
)
245 char *bufptr
= buffer
;
246 unsigned char parent
[20];
247 struct commit_list
**pptr
;
248 struct commit_graft
*graft
;
251 if (item
->object
.parsed
)
253 item
->object
.parsed
= 1;
255 if (tail
<= bufptr
+ 5 || memcmp(bufptr
, "tree ", 5))
256 return error("bogus commit object %s", sha1_to_hex(item
->object
.sha1
));
257 if (tail
<= bufptr
+ 45 || get_sha1_hex(bufptr
+ 5, parent
) < 0)
258 return error("bad tree pointer in commit %s",
259 sha1_to_hex(item
->object
.sha1
));
260 item
->tree
= lookup_tree(parent
);
263 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
264 pptr
= &item
->parents
;
266 graft
= lookup_commit_graft(item
->object
.sha1
);
267 while (bufptr
+ 48 < tail
&& !memcmp(bufptr
, "parent ", 7)) {
268 struct commit
*new_parent
;
270 if (tail
<= bufptr
+ 48 ||
271 get_sha1_hex(bufptr
+ 7, parent
) ||
273 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
277 new_parent
= lookup_commit(parent
);
279 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
285 struct commit
*new_parent
;
286 for (i
= 0; i
< graft
->nr_parent
; i
++) {
287 new_parent
= lookup_commit(graft
->parent
[i
]);
290 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
294 item
->date
= parse_commit_date(bufptr
);
296 if (track_object_refs
) {
298 struct commit_list
*p
;
299 struct object_refs
*refs
= alloc_object_refs(n_refs
);
301 refs
->ref
[i
++] = &item
->tree
->object
;
302 for (p
= item
->parents
; p
; p
= p
->next
)
303 refs
->ref
[i
++] = &p
->item
->object
;
304 set_object_refs(&item
->object
, refs
);
310 int parse_commit(struct commit
*item
)
312 enum object_type type
;
317 if (item
->object
.parsed
)
319 buffer
= read_sha1_file(item
->object
.sha1
, &type
, &size
);
321 return error("Could not read %s",
322 sha1_to_hex(item
->object
.sha1
));
323 if (type
!= OBJ_COMMIT
) {
325 return error("Object %s not a commit",
326 sha1_to_hex(item
->object
.sha1
));
328 ret
= parse_commit_buffer(item
, buffer
, size
);
329 if (save_commit_buffer
&& !ret
) {
330 item
->buffer
= buffer
;
337 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
339 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
340 new_list
->item
= item
;
341 new_list
->next
= *list_p
;
346 void free_commit_list(struct commit_list
*list
)
349 struct commit_list
*temp
= list
;
355 struct commit_list
* insert_by_date(struct commit
*item
, struct commit_list
**list
)
357 struct commit_list
**pp
= list
;
358 struct commit_list
*p
;
359 while ((p
= *pp
) != NULL
) {
360 if (p
->item
->date
< item
->date
) {
365 return commit_list_insert(item
, pp
);
369 void sort_by_date(struct commit_list
**list
)
371 struct commit_list
*ret
= NULL
;
373 insert_by_date((*list
)->item
, &ret
);
374 *list
= (*list
)->next
;
379 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
382 struct commit
*ret
= (*list
)->item
;
383 struct commit_list
*parents
= ret
->parents
;
384 struct commit_list
*old
= *list
;
386 *list
= (*list
)->next
;
390 struct commit
*commit
= parents
->item
;
391 parse_commit(commit
);
392 if (!(commit
->object
.flags
& mark
)) {
393 commit
->object
.flags
|= mark
;
394 insert_by_date(commit
, list
);
396 parents
= parents
->next
;
401 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
404 struct commit_list
*parents
;
406 if (!(mark
& commit
->object
.flags
))
409 commit
->object
.flags
&= ~mark
;
411 parents
= commit
->parents
;
415 while ((parents
= parents
->next
))
416 clear_commit_marks(parents
->item
, mark
);
418 commit
= commit
->parents
->item
;
422 struct commit
*pop_commit(struct commit_list
**stack
)
424 struct commit_list
*top
= *stack
;
425 struct commit
*item
= top
? top
->item
: NULL
;
434 void topo_sort_default_setter(struct commit
*c
, void *data
)
439 void *topo_sort_default_getter(struct commit
*c
)
445 * Performs an in-place topological sort on the list supplied.
447 void sort_in_topological_order(struct commit_list
** list
, int lifo
)
449 sort_in_topological_order_fn(list
, lifo
, topo_sort_default_setter
,
450 topo_sort_default_getter
);
453 void sort_in_topological_order_fn(struct commit_list
** list
, int lifo
,
454 topo_sort_set_fn_t setter
,
455 topo_sort_get_fn_t getter
)
457 struct commit_list
* next
= *list
;
458 struct commit_list
* work
= NULL
, **insert
;
459 struct commit_list
** pptr
= list
;
460 struct sort_node
* nodes
;
461 struct sort_node
* next_nodes
;
464 /* determine the size of the list */
472 /* allocate an array to help sort the list */
473 nodes
= xcalloc(count
, sizeof(*nodes
));
474 /* link the list to the array */
478 next_nodes
->list_item
= next
;
479 setter(next
->item
, next_nodes
);
483 /* update the indegree */
486 struct commit_list
* parents
= next
->item
->parents
;
488 struct commit
* parent
=parents
->item
;
489 struct sort_node
* pn
= (struct sort_node
*) getter(parent
);
493 parents
=parents
->next
;
500 * tips are nodes not reachable from any other node in the list
502 * the tips serve as a starting set for the work queue.
507 struct sort_node
* node
= (struct sort_node
*) getter(next
->item
);
509 if (node
->indegree
== 0) {
510 insert
= &commit_list_insert(next
->item
, insert
)->next
;
515 /* process the list in topological order */
519 struct commit
* work_item
= pop_commit(&work
);
520 struct sort_node
* work_node
= (struct sort_node
*) getter(work_item
);
521 struct commit_list
* parents
= work_item
->parents
;
524 struct commit
* parent
=parents
->item
;
525 struct sort_node
* pn
= (struct sort_node
*) getter(parent
);
529 * parents are only enqueued for emission
530 * when all their children have been emitted thereby
531 * guaranteeing topological order.
536 insert_by_date(parent
, &work
);
538 commit_list_insert(parent
, &work
);
541 parents
=parents
->next
;
544 * work_item is a commit all of whose children
545 * have already been emitted. we can emit it now.
547 *pptr
= work_node
->list_item
;
548 pptr
= &(*pptr
)->next
;
550 setter(work_item
, NULL
);
555 /* merge-base stuff */
557 /* bits #0..15 in revision.h */
558 #define PARENT1 (1u<<16)
559 #define PARENT2 (1u<<17)
560 #define STALE (1u<<18)
561 #define RESULT (1u<<19)
563 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
565 static struct commit
*interesting(struct commit_list
*list
)
568 struct commit
*commit
= list
->item
;
570 if (commit
->object
.flags
& STALE
)
577 static struct commit_list
*merge_bases(struct commit
*one
, struct commit
*two
)
579 struct commit_list
*list
= NULL
;
580 struct commit_list
*result
= NULL
;
583 /* We do not mark this even with RESULT so we do not
584 * have to clean it up.
586 return commit_list_insert(one
, &result
);
591 one
->object
.flags
|= PARENT1
;
592 two
->object
.flags
|= PARENT2
;
593 insert_by_date(one
, &list
);
594 insert_by_date(two
, &list
);
596 while (interesting(list
)) {
597 struct commit
*commit
;
598 struct commit_list
*parents
;
599 struct commit_list
*n
;
607 flags
= commit
->object
.flags
& (PARENT1
| PARENT2
| STALE
);
608 if (flags
== (PARENT1
| PARENT2
)) {
609 if (!(commit
->object
.flags
& RESULT
)) {
610 commit
->object
.flags
|= RESULT
;
611 insert_by_date(commit
, &result
);
613 /* Mark parents of a found merge stale */
616 parents
= commit
->parents
;
618 struct commit
*p
= parents
->item
;
619 parents
= parents
->next
;
620 if ((p
->object
.flags
& flags
) == flags
)
623 p
->object
.flags
|= flags
;
624 insert_by_date(p
, &list
);
628 /* Clean up the result to remove stale ones */
629 free_commit_list(list
);
630 list
= result
; result
= NULL
;
632 struct commit_list
*n
= list
->next
;
633 if (!(list
->item
->object
.flags
& STALE
))
634 insert_by_date(list
->item
, &result
);
641 struct commit_list
*get_merge_bases(struct commit
*one
,
642 struct commit
*two
, int cleanup
)
644 struct commit_list
*list
;
645 struct commit
**rslt
;
646 struct commit_list
*result
;
649 result
= merge_bases(one
, two
);
652 if (!result
|| !result
->next
) {
654 clear_commit_marks(one
, all_flags
);
655 clear_commit_marks(two
, all_flags
);
660 /* There are more than one */
667 rslt
= xcalloc(cnt
, sizeof(*rslt
));
668 for (list
= result
, i
= 0; list
; list
= list
->next
)
669 rslt
[i
++] = list
->item
;
670 free_commit_list(result
);
672 clear_commit_marks(one
, all_flags
);
673 clear_commit_marks(two
, all_flags
);
674 for (i
= 0; i
< cnt
- 1; i
++) {
675 for (j
= i
+1; j
< cnt
; j
++) {
676 if (!rslt
[i
] || !rslt
[j
])
678 result
= merge_bases(rslt
[i
], 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
)
684 if (rslt
[j
] == list
->item
)
690 /* Surviving ones in rslt[] are the independent results */
692 for (i
= 0; i
< cnt
; i
++) {
694 insert_by_date(rslt
[i
], &result
);
700 int in_merge_bases(struct commit
*commit
, struct commit
**reference
, int num
)
702 struct commit_list
*bases
, *b
;
706 bases
= get_merge_bases(commit
, *reference
, 1);
709 for (b
= bases
; b
; b
= b
->next
) {
710 if (!hashcmp(commit
->object
.sha1
, b
->item
->object
.sha1
)) {
716 free_commit_list(bases
);