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(struct strbuf
*out
, 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_buf_write(out
, "shallow %s", hex
);
213 strbuf_addstr(out
, hex
);
214 strbuf_addch(out
, '\n');
220 int unregister_shallow(const unsigned char *sha1
)
222 int pos
= commit_graft_pos(sha1
);
225 if (pos
+ 1 < commit_graft_nr
)
226 memcpy(commit_graft
+ pos
, commit_graft
+ pos
+ 1,
227 sizeof(struct commit_graft
*)
228 * (commit_graft_nr
- pos
- 1));
233 int parse_commit_buffer(struct commit
*item
, void *buffer
, unsigned long size
)
236 char *bufptr
= buffer
;
237 unsigned char parent
[20];
238 struct commit_list
**pptr
;
239 struct commit_graft
*graft
;
241 if (item
->object
.parsed
)
243 item
->object
.parsed
= 1;
245 if (tail
<= bufptr
+ 46 || memcmp(bufptr
, "tree ", 5) || bufptr
[45] != '\n')
246 return error("bogus commit object %s", sha1_to_hex(item
->object
.sha1
));
247 if (get_sha1_hex(bufptr
+ 5, parent
) < 0)
248 return error("bad tree pointer in commit %s",
249 sha1_to_hex(item
->object
.sha1
));
250 item
->tree
= lookup_tree(parent
);
251 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
252 pptr
= &item
->parents
;
254 graft
= lookup_commit_graft(item
->object
.sha1
);
255 while (bufptr
+ 48 < tail
&& !memcmp(bufptr
, "parent ", 7)) {
256 struct commit
*new_parent
;
258 if (tail
<= bufptr
+ 48 ||
259 get_sha1_hex(bufptr
+ 7, parent
) ||
261 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
264 * The clone is shallow if nr_parent < 0, and we must
265 * not traverse its real parents even when we unhide them.
267 if (graft
&& (graft
->nr_parent
< 0 || grafts_replace_parents
))
269 new_parent
= lookup_commit(parent
);
271 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
275 struct commit
*new_parent
;
276 for (i
= 0; i
< graft
->nr_parent
; i
++) {
277 new_parent
= lookup_commit(graft
->parent
[i
]);
280 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
283 item
->date
= parse_commit_date(bufptr
, tail
);
288 int parse_commit(struct commit
*item
)
290 enum object_type type
;
297 if (item
->object
.parsed
)
299 buffer
= read_sha1_file(item
->object
.sha1
, &type
, &size
);
301 return error("Could not read %s",
302 sha1_to_hex(item
->object
.sha1
));
303 if (type
!= OBJ_COMMIT
) {
305 return error("Object %s not a commit",
306 sha1_to_hex(item
->object
.sha1
));
308 ret
= parse_commit_buffer(item
, buffer
, size
);
309 if (save_commit_buffer
&& !ret
) {
310 item
->buffer
= buffer
;
317 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
319 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
320 new_list
->item
= item
;
321 new_list
->next
= *list_p
;
326 unsigned commit_list_count(const struct commit_list
*l
)
329 for (; l
; l
= l
->next
)
334 void free_commit_list(struct commit_list
*list
)
337 struct commit_list
*temp
= list
;
343 struct commit_list
* insert_by_date(struct commit
*item
, struct commit_list
**list
)
345 struct commit_list
**pp
= list
;
346 struct commit_list
*p
;
347 while ((p
= *pp
) != NULL
) {
348 if (p
->item
->date
< item
->date
) {
353 return commit_list_insert(item
, pp
);
357 void sort_by_date(struct commit_list
**list
)
359 struct commit_list
*ret
= NULL
;
361 insert_by_date((*list
)->item
, &ret
);
362 *list
= (*list
)->next
;
367 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
370 struct commit
*ret
= (*list
)->item
;
371 struct commit_list
*parents
= ret
->parents
;
372 struct commit_list
*old
= *list
;
374 *list
= (*list
)->next
;
378 struct commit
*commit
= parents
->item
;
379 if (!parse_commit(commit
) && !(commit
->object
.flags
& mark
)) {
380 commit
->object
.flags
|= mark
;
381 insert_by_date(commit
, list
);
383 parents
= parents
->next
;
388 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
391 struct commit_list
*parents
;
393 if (!(mark
& commit
->object
.flags
))
396 commit
->object
.flags
&= ~mark
;
398 parents
= commit
->parents
;
402 while ((parents
= parents
->next
))
403 clear_commit_marks(parents
->item
, mark
);
405 commit
= commit
->parents
->item
;
409 struct commit
*pop_commit(struct commit_list
**stack
)
411 struct commit_list
*top
= *stack
;
412 struct commit
*item
= top
? top
->item
: NULL
;
422 * Performs an in-place topological sort on the list supplied.
424 void sort_in_topological_order(struct commit_list
** list
, int lifo
)
426 struct commit_list
*next
, *orig
= *list
;
427 struct commit_list
*work
, **insert
;
428 struct commit_list
**pptr
;
434 /* Mark them and clear the indegree */
435 for (next
= orig
; next
; next
= next
->next
) {
436 struct commit
*commit
= next
->item
;
437 commit
->indegree
= 1;
440 /* update the indegree */
441 for (next
= orig
; next
; next
= next
->next
) {
442 struct commit_list
* parents
= next
->item
->parents
;
444 struct commit
*parent
= parents
->item
;
446 if (parent
->indegree
)
448 parents
= parents
->next
;
455 * tips are nodes not reachable from any other node in the list
457 * the tips serve as a starting set for the work queue.
461 for (next
= orig
; next
; next
= next
->next
) {
462 struct commit
*commit
= next
->item
;
464 if (commit
->indegree
== 1)
465 insert
= &commit_list_insert(commit
, insert
)->next
;
468 /* process the list in topological order */
475 struct commit
*commit
;
476 struct commit_list
*parents
, *work_item
;
479 work
= work_item
->next
;
480 work_item
->next
= NULL
;
482 commit
= work_item
->item
;
483 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
484 struct commit
*parent
=parents
->item
;
486 if (!parent
->indegree
)
490 * parents are only enqueued for emission
491 * when all their children have been emitted thereby
492 * guaranteeing topological order.
494 if (--parent
->indegree
== 1) {
496 insert_by_date(parent
, &work
);
498 commit_list_insert(parent
, &work
);
502 * work_item is a commit all of whose children
503 * have already been emitted. we can emit it now.
505 commit
->indegree
= 0;
507 pptr
= &work_item
->next
;
511 /* merge-base stuff */
513 /* bits #0..15 in revision.h */
514 #define PARENT1 (1u<<16)
515 #define PARENT2 (1u<<17)
516 #define STALE (1u<<18)
517 #define RESULT (1u<<19)
519 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
521 static struct commit
*interesting(struct commit_list
*list
)
524 struct commit
*commit
= list
->item
;
526 if (commit
->object
.flags
& STALE
)
533 static struct commit_list
*merge_bases_many(struct commit
*one
, int n
, struct commit
**twos
)
535 struct commit_list
*list
= NULL
;
536 struct commit_list
*result
= NULL
;
539 for (i
= 0; i
< n
; i
++) {
542 * We do not mark this even with RESULT so we do not
543 * have to clean it up.
545 return commit_list_insert(one
, &result
);
548 if (parse_commit(one
))
550 for (i
= 0; i
< n
; i
++) {
551 if (parse_commit(twos
[i
]))
555 one
->object
.flags
|= PARENT1
;
556 insert_by_date(one
, &list
);
557 for (i
= 0; i
< n
; i
++) {
558 twos
[i
]->object
.flags
|= PARENT2
;
559 insert_by_date(twos
[i
], &list
);
562 while (interesting(list
)) {
563 struct commit
*commit
;
564 struct commit_list
*parents
;
565 struct commit_list
*next
;
573 flags
= commit
->object
.flags
& (PARENT1
| PARENT2
| STALE
);
574 if (flags
== (PARENT1
| PARENT2
)) {
575 if (!(commit
->object
.flags
& RESULT
)) {
576 commit
->object
.flags
|= RESULT
;
577 insert_by_date(commit
, &result
);
579 /* Mark parents of a found merge stale */
582 parents
= commit
->parents
;
584 struct commit
*p
= parents
->item
;
585 parents
= parents
->next
;
586 if ((p
->object
.flags
& flags
) == flags
)
590 p
->object
.flags
|= flags
;
591 insert_by_date(p
, &list
);
595 /* Clean up the result to remove stale ones */
596 free_commit_list(list
);
597 list
= result
; result
= NULL
;
599 struct commit_list
*next
= list
->next
;
600 if (!(list
->item
->object
.flags
& STALE
))
601 insert_by_date(list
->item
, &result
);
608 struct commit_list
*get_octopus_merge_bases(struct commit_list
*in
)
610 struct commit_list
*i
, *j
, *k
, *ret
= NULL
;
611 struct commit_list
**pptr
= &ret
;
613 for (i
= in
; i
; i
= i
->next
) {
615 pptr
= &commit_list_insert(i
->item
, pptr
)->next
;
617 struct commit_list
*new = NULL
, *end
= NULL
;
619 for (j
= ret
; j
; j
= j
->next
) {
620 struct commit_list
*bases
;
621 bases
= get_merge_bases(i
->item
, j
->item
, 1);
626 for (k
= bases
; k
; k
= k
->next
)
635 struct commit_list
*get_merge_bases_many(struct commit
*one
,
637 struct commit
**twos
,
640 struct commit_list
*list
;
641 struct commit
**rslt
;
642 struct commit_list
*result
;
645 result
= merge_bases_many(one
, n
, twos
);
646 for (i
= 0; i
< n
; i
++) {
650 if (!result
|| !result
->next
) {
652 clear_commit_marks(one
, all_flags
);
653 for (i
= 0; i
< n
; i
++)
654 clear_commit_marks(twos
[i
], all_flags
);
659 /* There are more than one */
666 rslt
= xcalloc(cnt
, sizeof(*rslt
));
667 for (list
= result
, i
= 0; list
; list
= list
->next
)
668 rslt
[i
++] = list
->item
;
669 free_commit_list(result
);
671 clear_commit_marks(one
, all_flags
);
672 for (i
= 0; i
< n
; i
++)
673 clear_commit_marks(twos
[i
], 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_many(rslt
[i
], 1, &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 struct commit_list
*get_merge_bases(struct commit
*one
, struct commit
*two
,
703 return get_merge_bases_many(one
, 1, &two
, cleanup
);
706 int is_descendant_of(struct commit
*commit
, struct commit_list
*with_commit
)
710 while (with_commit
) {
711 struct commit
*other
;
713 other
= with_commit
->item
;
714 with_commit
= with_commit
->next
;
715 if (in_merge_bases(other
, &commit
, 1))
721 int in_merge_bases(struct commit
*commit
, struct commit
**reference
, int num
)
723 struct commit_list
*bases
, *b
;
727 bases
= get_merge_bases(commit
, *reference
, 1);
730 for (b
= bases
; b
; b
= b
->next
) {
731 if (!hashcmp(commit
->object
.sha1
, b
->item
->object
.sha1
)) {
737 free_commit_list(bases
);
741 struct commit_list
*reduce_heads(struct commit_list
*heads
)
743 struct commit_list
*p
;
744 struct commit_list
*result
= NULL
, **tail
= &result
;
745 struct commit
**other
;
746 size_t num_head
, num_other
;
751 /* Avoid unnecessary reallocations */
752 for (p
= heads
, num_head
= 0; p
; p
= p
->next
)
754 other
= xcalloc(sizeof(*other
), num_head
);
756 /* For each commit, see if it can be reached by others */
757 for (p
= heads
; p
; p
= p
->next
) {
758 struct commit_list
*q
, *base
;
760 /* Do we already have this in the result? */
761 for (q
= result
; q
; q
= q
->next
)
762 if (p
->item
== q
->item
)
768 for (q
= heads
; q
; q
= q
->next
) {
769 if (p
->item
== q
->item
)
771 other
[num_other
++] = q
->item
;
774 base
= get_merge_bases_many(p
->item
, num_other
, other
, 1);
778 * If p->item does not have anything common with other
779 * commits, there won't be any merge base. If it is
780 * reachable from some of the others, p->item will be
781 * the merge base. If its history is connected with
782 * others, but p->item is not reachable by others, we
783 * will get something other than p->item back.
785 if (!base
|| (base
->item
!= p
->item
))
786 tail
= &(commit_list_insert(p
->item
, tail
)->next
);
787 free_commit_list(base
);