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
)
58 if (memcmp(buf
, "author", 6))
60 while (buf
< tail
&& *buf
++ != '\n')
64 if (memcmp(buf
, "committer", 9))
66 while (buf
< tail
&& *buf
++ != '>')
71 while (buf
< tail
&& *buf
++ != '\n')
75 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
76 date
= strtoul(dateptr
, NULL
, 10);
77 if (date
== ULONG_MAX
)
82 static struct commit_graft
**commit_graft
;
83 static int commit_graft_alloc
, commit_graft_nr
;
85 static int commit_graft_pos(const unsigned char *sha1
)
91 int mi
= (lo
+ hi
) / 2;
92 struct commit_graft
*graft
= commit_graft
[mi
];
93 int cmp
= hashcmp(sha1
, graft
->sha1
);
104 int register_commit_graft(struct commit_graft
*graft
, int ignore_dups
)
106 int pos
= commit_graft_pos(graft
->sha1
);
112 free(commit_graft
[pos
]);
113 commit_graft
[pos
] = graft
;
118 if (commit_graft_alloc
<= ++commit_graft_nr
) {
119 commit_graft_alloc
= alloc_nr(commit_graft_alloc
);
120 commit_graft
= xrealloc(commit_graft
,
121 sizeof(*commit_graft
) *
124 if (pos
< commit_graft_nr
)
125 memmove(commit_graft
+ pos
+ 1,
127 (commit_graft_nr
- pos
- 1) *
128 sizeof(*commit_graft
));
129 commit_graft
[pos
] = graft
;
133 struct commit_graft
*read_graft_line(char *buf
, int len
)
135 /* The format is just "Commit Parent1 Parent2 ...\n" */
137 struct commit_graft
*graft
= NULL
;
139 if (buf
[len
-1] == '\n')
141 if (buf
[0] == '#' || buf
[0] == '\0')
143 if ((len
+ 1) % 41) {
145 error("bad graft data: %s", buf
);
149 i
= (len
+ 1) / 41 - 1;
150 graft
= xmalloc(sizeof(*graft
) + 20 * i
);
151 graft
->nr_parent
= i
;
152 if (get_sha1_hex(buf
, graft
->sha1
))
154 for (i
= 40; i
< len
; i
+= 41) {
157 if (get_sha1_hex(buf
+ i
+ 1, graft
->parent
[i
/41]))
163 int read_graft_file(const char *graft_file
)
165 FILE *fp
= fopen(graft_file
, "r");
169 while (fgets(buf
, sizeof(buf
), fp
)) {
170 /* The format is just "Commit Parent1 Parent2 ...\n" */
171 int len
= strlen(buf
);
172 struct commit_graft
*graft
= read_graft_line(buf
, len
);
175 if (register_commit_graft(graft
, 1))
176 error("duplicate graft data: %s", buf
);
182 static void prepare_commit_graft(void)
184 static int commit_graft_prepared
;
187 if (commit_graft_prepared
)
189 graft_file
= get_graft_file();
190 read_graft_file(graft_file
);
191 /* make sure shallows are read */
192 is_repository_shallow();
193 commit_graft_prepared
= 1;
196 struct commit_graft
*lookup_commit_graft(const unsigned char *sha1
)
199 prepare_commit_graft();
200 pos
= commit_graft_pos(sha1
);
203 return commit_graft
[pos
];
206 int write_shallow_commits(int fd
, int use_pack_protocol
)
209 for (i
= 0; i
< commit_graft_nr
; i
++)
210 if (commit_graft
[i
]->nr_parent
< 0) {
212 sha1_to_hex(commit_graft
[i
]->sha1
);
214 if (use_pack_protocol
)
215 packet_write(fd
, "shallow %s", hex
);
217 if (write_in_full(fd
, hex
, 40) != 40)
219 if (write_in_full(fd
, "\n", 1) != 1)
226 int unregister_shallow(const unsigned char *sha1
)
228 int pos
= commit_graft_pos(sha1
);
231 if (pos
+ 1 < commit_graft_nr
)
232 memcpy(commit_graft
+ pos
, commit_graft
+ pos
+ 1,
233 sizeof(struct commit_graft
*)
234 * (commit_graft_nr
- pos
- 1));
239 int parse_commit_buffer(struct commit
*item
, void *buffer
, unsigned long size
)
242 char *bufptr
= buffer
;
243 unsigned char parent
[20];
244 struct commit_list
**pptr
;
245 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
);
260 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
261 pptr
= &item
->parents
;
263 graft
= lookup_commit_graft(item
->object
.sha1
);
264 while (bufptr
+ 48 < tail
&& !memcmp(bufptr
, "parent ", 7)) {
265 struct commit
*new_parent
;
267 if (tail
<= bufptr
+ 48 ||
268 get_sha1_hex(bufptr
+ 7, parent
) ||
270 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
274 new_parent
= lookup_commit(parent
);
276 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
282 struct commit
*new_parent
;
283 for (i
= 0; i
< graft
->nr_parent
; i
++) {
284 new_parent
= lookup_commit(graft
->parent
[i
]);
287 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
291 item
->date
= parse_commit_date(bufptr
, tail
);
296 int parse_commit(struct commit
*item
)
298 enum object_type type
;
305 if (item
->object
.parsed
)
307 buffer
= read_sha1_file(item
->object
.sha1
, &type
, &size
);
309 return error("Could not read %s",
310 sha1_to_hex(item
->object
.sha1
));
311 if (type
!= OBJ_COMMIT
) {
313 return error("Object %s not a commit",
314 sha1_to_hex(item
->object
.sha1
));
316 ret
= parse_commit_buffer(item
, buffer
, size
);
317 if (save_commit_buffer
&& !ret
) {
318 item
->buffer
= buffer
;
325 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
327 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
328 new_list
->item
= item
;
329 new_list
->next
= *list_p
;
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
->object
.flags
|= TOPOSORT
;
438 commit
->indegree
= 0;
441 /* update the indegree */
442 for (next
= orig
; next
; next
= next
->next
) {
443 struct commit_list
* parents
= next
->item
->parents
;
445 struct commit
*parent
= parents
->item
;
447 if (parent
->object
.flags
& TOPOSORT
)
449 parents
= parents
->next
;
456 * tips are nodes not reachable from any other node in the list
458 * the tips serve as a starting set for the work queue.
462 for (next
= orig
; next
; next
= next
->next
) {
463 struct commit
*commit
= next
->item
;
465 if (!commit
->indegree
)
466 insert
= &commit_list_insert(commit
, insert
)->next
;
469 /* process the list in topological order */
476 struct commit
*commit
;
477 struct commit_list
*parents
, *work_item
;
480 work
= work_item
->next
;
481 work_item
->next
= NULL
;
483 commit
= work_item
->item
;
484 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
485 struct commit
*parent
=parents
->item
;
487 if (!(parent
->object
.flags
& TOPOSORT
))
491 * parents are only enqueued for emission
492 * when all their children have been emitted thereby
493 * guaranteeing topological order.
495 if (!--parent
->indegree
) {
497 insert_by_date(parent
, &work
);
499 commit_list_insert(parent
, &work
);
503 * work_item is a commit all of whose children
504 * have already been emitted. we can emit it now.
506 commit
->object
.flags
&= ~TOPOSORT
;
508 pptr
= &work_item
->next
;
512 /* merge-base stuff */
514 /* bits #0..15 in revision.h */
515 #define PARENT1 (1u<<16)
516 #define PARENT2 (1u<<17)
517 #define STALE (1u<<18)
518 #define RESULT (1u<<19)
520 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
522 static struct commit
*interesting(struct commit_list
*list
)
525 struct commit
*commit
= list
->item
;
527 if (commit
->object
.flags
& STALE
)
534 static struct commit_list
*merge_bases(struct commit
*one
, struct commit
*two
)
536 struct commit_list
*list
= NULL
;
537 struct commit_list
*result
= NULL
;
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
);
545 if (parse_commit(one
))
547 if (parse_commit(two
))
550 one
->object
.flags
|= PARENT1
;
551 two
->object
.flags
|= PARENT2
;
552 insert_by_date(one
, &list
);
553 insert_by_date(two
, &list
);
555 while (interesting(list
)) {
556 struct commit
*commit
;
557 struct commit_list
*parents
;
558 struct commit_list
*n
;
566 flags
= commit
->object
.flags
& (PARENT1
| PARENT2
| STALE
);
567 if (flags
== (PARENT1
| PARENT2
)) {
568 if (!(commit
->object
.flags
& RESULT
)) {
569 commit
->object
.flags
|= RESULT
;
570 insert_by_date(commit
, &result
);
572 /* Mark parents of a found merge stale */
575 parents
= commit
->parents
;
577 struct commit
*p
= parents
->item
;
578 parents
= parents
->next
;
579 if ((p
->object
.flags
& flags
) == flags
)
583 p
->object
.flags
|= flags
;
584 insert_by_date(p
, &list
);
588 /* Clean up the result to remove stale ones */
589 free_commit_list(list
);
590 list
= result
; result
= NULL
;
592 struct commit_list
*n
= list
->next
;
593 if (!(list
->item
->object
.flags
& STALE
))
594 insert_by_date(list
->item
, &result
);
601 struct commit_list
*get_merge_bases(struct commit
*one
,
602 struct commit
*two
, int cleanup
)
604 struct commit_list
*list
;
605 struct commit
**rslt
;
606 struct commit_list
*result
;
609 result
= merge_bases(one
, two
);
612 if (!result
|| !result
->next
) {
614 clear_commit_marks(one
, all_flags
);
615 clear_commit_marks(two
, all_flags
);
620 /* There are more than one */
627 rslt
= xcalloc(cnt
, sizeof(*rslt
));
628 for (list
= result
, i
= 0; list
; list
= list
->next
)
629 rslt
[i
++] = list
->item
;
630 free_commit_list(result
);
632 clear_commit_marks(one
, all_flags
);
633 clear_commit_marks(two
, all_flags
);
634 for (i
= 0; i
< cnt
- 1; i
++) {
635 for (j
= i
+1; j
< cnt
; j
++) {
636 if (!rslt
[i
] || !rslt
[j
])
638 result
= merge_bases(rslt
[i
], rslt
[j
]);
639 clear_commit_marks(rslt
[i
], all_flags
);
640 clear_commit_marks(rslt
[j
], all_flags
);
641 for (list
= result
; list
; list
= list
->next
) {
642 if (rslt
[i
] == list
->item
)
644 if (rslt
[j
] == list
->item
)
650 /* Surviving ones in rslt[] are the independent results */
652 for (i
= 0; i
< cnt
; i
++) {
654 insert_by_date(rslt
[i
], &result
);
660 int in_merge_bases(struct commit
*commit
, struct commit
**reference
, int num
)
662 struct commit_list
*bases
, *b
;
666 bases
= get_merge_bases(commit
, *reference
, 1);
669 for (b
= bases
; b
; b
= b
->next
) {
670 if (!hashcmp(commit
->object
.sha1
, b
->item
->object
.sha1
)) {
676 free_commit_list(bases
);