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)
55 if (memcmp(buf, "author", 6))
57 while (*buf++ != '\n')
59 if (memcmp(buf, "committer", 9))
63 date = strtoul(buf, NULL, 10);
64 if (date == ULONG_MAX)
69 static struct commit_graft **commit_graft;
70 static int commit_graft_alloc, commit_graft_nr;
72 static int commit_graft_pos(const unsigned char *sha1)
78 int mi = (lo + hi) / 2;
79 struct commit_graft *graft = commit_graft[mi];
80 int cmp = hashcmp(sha1, graft->sha1);
91 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
93 int pos = commit_graft_pos(graft->sha1);
99 free(commit_graft[pos]);
100 commit_graft[pos] = graft;
105 if (commit_graft_alloc <= ++commit_graft_nr) {
106 commit_graft_alloc = alloc_nr(commit_graft_alloc);
107 commit_graft = xrealloc(commit_graft,
108 sizeof(*commit_graft) *
111 if (pos < commit_graft_nr)
112 memmove(commit_graft + pos + 1,
114 (commit_graft_nr - pos - 1) *
115 sizeof(*commit_graft));
116 commit_graft[pos] = graft;
120 struct commit_graft *read_graft_line(char *buf, int len)
122 /* The format is just "Commit Parent1 Parent2 ...\n" */
124 struct commit_graft *graft = NULL;
126 if (buf[len-1] == '\n')
128 if (buf[0] == '#' || buf[0] == '\0')
130 if ((len + 1) % 41) {
132 error("bad graft data: %s", buf);
136 i = (len + 1) / 41 - 1;
137 graft = xmalloc(sizeof(*graft) + 20 * i);
138 graft->nr_parent = i;
139 if (get_sha1_hex(buf, graft->sha1))
141 for (i = 40; i < len; i += 41) {
144 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
150 int read_graft_file(const char *graft_file)
152 FILE *fp = fopen(graft_file, "r");
156 while (fgets(buf, sizeof(buf), fp)) {
157 /* The format is just "Commit Parent1 Parent2 ...\n" */
158 int len = strlen(buf);
159 struct commit_graft *graft = read_graft_line(buf, len);
162 if (register_commit_graft(graft, 1))
163 error("duplicate graft data: %s", buf);
169 static void prepare_commit_graft(void)
171 static int commit_graft_prepared;
174 if (commit_graft_prepared)
176 graft_file = get_graft_file();
177 read_graft_file(graft_file);
178 /* make sure shallows are read */
179 is_repository_shallow();
180 commit_graft_prepared = 1;
183 static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
186 prepare_commit_graft();
187 pos = commit_graft_pos(sha1);
190 return commit_graft[pos];
193 int write_shallow_commits(int fd, int use_pack_protocol)
196 for (i = 0; i < commit_graft_nr; i++)
197 if (commit_graft[i]->nr_parent < 0) {
199 sha1_to_hex(commit_graft[i]->sha1);
201 if (use_pack_protocol)
202 packet_write(fd, "shallow %s", hex);
204 if (write_in_full(fd, hex, 40) != 40)
206 if (write_in_full(fd, "\n", 1) != 1)
213 int unregister_shallow(const unsigned char *sha1)
215 int pos = commit_graft_pos(sha1);
218 if (pos + 1 < commit_graft_nr)
219 memcpy(commit_graft + pos, commit_graft + pos + 1,
220 sizeof(struct commit_graft *)
221 * (commit_graft_nr - pos - 1));
226 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
229 char *bufptr = buffer;
230 unsigned char parent[20];
231 struct commit_list **pptr;
232 struct commit_graft *graft;
235 if (item->object.parsed)
237 item->object.parsed = 1;
239 if (tail <= bufptr + 5 || memcmp(bufptr, "tree ", 5))
240 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
241 if (tail <= bufptr + 45 || get_sha1_hex(bufptr + 5, parent) < 0)
242 return error("bad tree pointer in commit %s",
243 sha1_to_hex(item->object.sha1));
244 item->tree = lookup_tree(parent);
247 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
248 pptr = &item->parents;
250 graft = lookup_commit_graft(item->object.sha1);
251 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
252 struct commit *new_parent;
254 if (tail <= bufptr + 48 ||
255 get_sha1_hex(bufptr + 7, parent) ||
257 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
261 new_parent = lookup_commit(parent);
263 pptr = &commit_list_insert(new_parent, pptr)->next;
269 struct commit *new_parent;
270 for (i = 0; i < graft->nr_parent; i++) {
271 new_parent = lookup_commit(graft->parent[i]);
274 pptr = &commit_list_insert(new_parent, pptr)->next;
278 item->date = parse_commit_date(bufptr);
280 if (track_object_refs) {
282 struct commit_list *p;
283 struct object_refs *refs = alloc_object_refs(n_refs);
285 refs->ref[i++] = &item->tree->object;
286 for (p = item->parents; p; p = p->next)
287 refs->ref[i++] = &p->item->object;
288 set_object_refs(&item->object, refs);
294 int parse_commit(struct commit *item)
296 enum object_type type;
301 if (item->object.parsed)
303 buffer = read_sha1_file(item->object.sha1, &type, &size);
305 return error("Could not read %s",
306 sha1_to_hex(item->object.sha1));
307 if (type != OBJ_COMMIT) {
309 return error("Object %s not a commit",
310 sha1_to_hex(item->object.sha1));
312 ret = parse_commit_buffer(item, buffer, size);
313 if (save_commit_buffer && !ret) {
314 item->buffer = buffer;
321 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
323 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
324 new_list->item = item;
325 new_list->next = *list_p;
330 void free_commit_list(struct commit_list *list)
333 struct commit_list *temp = list;
339 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
341 struct commit_list **pp = list;
342 struct commit_list *p;
343 while ((p = *pp) != NULL) {
344 if (p->item->date < item->date) {
349 return commit_list_insert(item, pp);
353 void sort_by_date(struct commit_list **list)
355 struct commit_list *ret = NULL;
357 insert_by_date((*list)->item, &ret);
358 *list = (*list)->next;
363 struct commit *pop_most_recent_commit(struct commit_list **list,
366 struct commit *ret = (*list)->item;
367 struct commit_list *parents = ret->parents;
368 struct commit_list *old = *list;
370 *list = (*list)->next;
374 struct commit *commit = parents->item;
375 parse_commit(commit);
376 if (!(commit->object.flags & mark)) {
377 commit->object.flags |= mark;
378 insert_by_date(commit, list);
380 parents = parents->next;
385 void clear_commit_marks(struct commit *commit, unsigned int mark)
388 struct commit_list *parents;
390 if (!(mark & commit->object.flags))
393 commit->object.flags &= ~mark;
395 parents = commit->parents;
399 while ((parents = parents->next))
400 clear_commit_marks(parents->item, mark);
402 commit = commit->parents->item;
406 struct commit *pop_commit(struct commit_list **stack)
408 struct commit_list *top = *stack;
409 struct commit *item = top ? top->item : NULL;
419 * Performs an in-place topological sort on the list supplied.
421 void sort_in_topological_order(struct commit_list ** list, int lifo)
423 struct commit_list *next, *orig = *list;
424 struct commit_list *work, **insert;
425 struct commit_list **pptr;
431 /* Mark them and clear the indegree */
432 for (next = orig; next; next = next->next) {
433 struct commit *commit = next->item;
434 commit->object.flags |= TOPOSORT;
435 commit->indegree = 0;
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->object.flags & TOPOSORT)
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)
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->object.flags & TOPOSORT))
488 * parents are only enqueued for emission
489 * when all their children have been emitted thereby
490 * guaranteeing topological order.
492 if (!--parent->indegree) {
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->object.flags &= ~TOPOSORT;
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(struct commit *one, struct commit *two)
533 struct commit_list *list = NULL;
534 struct commit_list *result = NULL;
537 /* We do not mark this even with RESULT so we do not
538 * have to clean it up.
540 return commit_list_insert(one, &result);
545 one->object.flags |= PARENT1;
546 two->object.flags |= PARENT2;
547 insert_by_date(one, &list);
548 insert_by_date(two, &list);
550 while (interesting(list)) {
551 struct commit *commit;
552 struct commit_list *parents;
553 struct commit_list *n;
561 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
562 if (flags == (PARENT1 | PARENT2)) {
563 if (!(commit->object.flags & RESULT)) {
564 commit->object.flags |= RESULT;
565 insert_by_date(commit, &result);
567 /* Mark parents of a found merge stale */
570 parents = commit->parents;
572 struct commit *p = parents->item;
573 parents = parents->next;
574 if ((p->object.flags & flags) == flags)
577 p->object.flags |= flags;
578 insert_by_date(p, &list);
582 /* Clean up the result to remove stale ones */
583 free_commit_list(list);
584 list = result; result = NULL;
586 struct commit_list *n = list->next;
587 if (!(list->item->object.flags & STALE))
588 insert_by_date(list->item, &result);
595 struct commit_list *get_merge_bases(struct commit *one,
596 struct commit *two, int cleanup)
598 struct commit_list *list;
599 struct commit **rslt;
600 struct commit_list *result;
603 result = merge_bases(one, two);
606 if (!result || !result->next) {
608 clear_commit_marks(one, all_flags);
609 clear_commit_marks(two, all_flags);
614 /* There are more than one */
621 rslt = xcalloc(cnt, sizeof(*rslt));
622 for (list = result, i = 0; list; list = list->next)
623 rslt[i++] = list->item;
624 free_commit_list(result);
626 clear_commit_marks(one, all_flags);
627 clear_commit_marks(two, all_flags);
628 for (i = 0; i < cnt - 1; i++) {
629 for (j = i+1; j < cnt; j++) {
630 if (!rslt[i] || !rslt[j])
632 result = merge_bases(rslt[i], rslt[j]);
633 clear_commit_marks(rslt[i], all_flags);
634 clear_commit_marks(rslt[j], all_flags);
635 for (list = result; list; list = list->next) {
636 if (rslt[i] == list->item)
638 if (rslt[j] == list->item)
644 /* Surviving ones in rslt[] are the independent results */
646 for (i = 0; i < cnt; i++) {
648 insert_by_date(rslt[i], &result);
654 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
656 struct commit_list *bases, *b;
660 bases = get_merge_bases(commit, *reference, 1);
663 for (b = bases; b; b = b->next) {
664 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
670 free_commit_list(bases);