10 int save_commit_buffer
= 1;
12 const char *commit_type
= "commit";
14 static struct commit
*check_commit(struct object
*obj
,
15 const unsigned char *sha1
,
18 if (obj
->type
!= OBJ_COMMIT
) {
20 error("Object %s is a %s, not a commit",
21 sha1_to_hex(sha1
), typename(obj
->type
));
24 return (struct commit
*) obj
;
27 struct commit
*lookup_commit_reference_gently(const unsigned char *sha1
,
30 struct object
*obj
= deref_tag(parse_object(sha1
), NULL
, 0);
34 return check_commit(obj
, sha1
, quiet
);
37 struct commit
*lookup_commit_reference(const unsigned char *sha1
)
39 return lookup_commit_reference_gently(sha1
, 0);
42 struct commit
*lookup_commit(const unsigned char *sha1
)
44 struct object
*obj
= lookup_object(sha1
);
46 return create_object(sha1
, OBJ_COMMIT
, alloc_commit_node());
48 obj
->type
= OBJ_COMMIT
;
49 return check_commit(obj
, sha1
, 0);
52 struct commit
*lookup_commit_reference_by_name(const char *name
)
54 unsigned char sha1
[20];
55 struct commit
*commit
;
57 if (get_sha1(name
, sha1
))
59 commit
= lookup_commit_reference(sha1
);
60 if (!commit
|| parse_commit(commit
))
65 static unsigned long parse_commit_date(const char *buf
, const char *tail
)
71 if (memcmp(buf
, "author", 6))
73 while (buf
< tail
&& *buf
++ != '\n')
77 if (memcmp(buf
, "committer", 9))
79 while (buf
< tail
&& *buf
++ != '>')
84 while (buf
< tail
&& *buf
++ != '\n')
88 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
89 return strtoul(dateptr
, NULL
, 10);
92 static struct commit_graft
**commit_graft
;
93 static int commit_graft_alloc
, commit_graft_nr
;
95 static int commit_graft_pos(const unsigned char *sha1
)
101 int mi
= (lo
+ hi
) / 2;
102 struct commit_graft
*graft
= commit_graft
[mi
];
103 int cmp
= hashcmp(sha1
, graft
->sha1
);
114 int register_commit_graft(struct commit_graft
*graft
, int ignore_dups
)
116 int pos
= commit_graft_pos(graft
->sha1
);
122 free(commit_graft
[pos
]);
123 commit_graft
[pos
] = graft
;
128 if (commit_graft_alloc
<= ++commit_graft_nr
) {
129 commit_graft_alloc
= alloc_nr(commit_graft_alloc
);
130 commit_graft
= xrealloc(commit_graft
,
131 sizeof(*commit_graft
) *
134 if (pos
< commit_graft_nr
)
135 memmove(commit_graft
+ pos
+ 1,
137 (commit_graft_nr
- pos
- 1) *
138 sizeof(*commit_graft
));
139 commit_graft
[pos
] = graft
;
143 struct commit_graft
*read_graft_line(char *buf
, int len
)
145 /* The format is just "Commit Parent1 Parent2 ...\n" */
147 struct commit_graft
*graft
= NULL
;
149 while (len
&& isspace(buf
[len
-1]))
151 if (buf
[0] == '#' || buf
[0] == '\0')
155 i
= (len
+ 1) / 41 - 1;
156 graft
= xmalloc(sizeof(*graft
) + 20 * i
);
157 graft
->nr_parent
= i
;
158 if (get_sha1_hex(buf
, graft
->sha1
))
160 for (i
= 40; i
< len
; i
+= 41) {
163 if (get_sha1_hex(buf
+ i
+ 1, graft
->parent
[i
/41]))
169 error("bad graft data: %s", buf
);
174 static int read_graft_file(const char *graft_file
)
176 FILE *fp
= fopen(graft_file
, "r");
180 while (fgets(buf
, sizeof(buf
), fp
)) {
181 /* The format is just "Commit Parent1 Parent2 ...\n" */
182 int len
= strlen(buf
);
183 struct commit_graft
*graft
= read_graft_line(buf
, len
);
186 if (register_commit_graft(graft
, 1))
187 error("duplicate graft data: %s", buf
);
193 static void prepare_commit_graft(void)
195 static int commit_graft_prepared
;
198 if (commit_graft_prepared
)
200 graft_file
= get_graft_file();
201 read_graft_file(graft_file
);
202 /* make sure shallows are read */
203 is_repository_shallow();
204 commit_graft_prepared
= 1;
207 struct commit_graft
*lookup_commit_graft(const unsigned char *sha1
)
210 prepare_commit_graft();
211 pos
= commit_graft_pos(sha1
);
214 return commit_graft
[pos
];
217 int write_shallow_commits(struct strbuf
*out
, int use_pack_protocol
)
220 for (i
= 0; i
< commit_graft_nr
; i
++)
221 if (commit_graft
[i
]->nr_parent
< 0) {
223 sha1_to_hex(commit_graft
[i
]->sha1
);
225 if (use_pack_protocol
)
226 packet_buf_write(out
, "shallow %s", hex
);
228 strbuf_addstr(out
, hex
);
229 strbuf_addch(out
, '\n');
235 int unregister_shallow(const unsigned char *sha1
)
237 int pos
= commit_graft_pos(sha1
);
240 if (pos
+ 1 < commit_graft_nr
)
241 memmove(commit_graft
+ pos
, commit_graft
+ pos
+ 1,
242 sizeof(struct commit_graft
*)
243 * (commit_graft_nr
- pos
- 1));
248 int parse_commit_buffer(struct commit
*item
, const void *buffer
, unsigned long size
)
250 const char *tail
= buffer
;
251 const char *bufptr
= buffer
;
252 unsigned char parent
[20];
253 struct commit_list
**pptr
;
254 struct commit_graft
*graft
;
256 if (item
->object
.parsed
)
258 item
->object
.parsed
= 1;
260 if (tail
<= bufptr
+ 46 || memcmp(bufptr
, "tree ", 5) || bufptr
[45] != '\n')
261 return error("bogus commit object %s", sha1_to_hex(item
->object
.sha1
));
262 if (get_sha1_hex(bufptr
+ 5, parent
) < 0)
263 return error("bad tree pointer in commit %s",
264 sha1_to_hex(item
->object
.sha1
));
265 item
->tree
= lookup_tree(parent
);
266 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
267 pptr
= &item
->parents
;
269 graft
= lookup_commit_graft(item
->object
.sha1
);
270 while (bufptr
+ 48 < tail
&& !memcmp(bufptr
, "parent ", 7)) {
271 struct commit
*new_parent
;
273 if (tail
<= bufptr
+ 48 ||
274 get_sha1_hex(bufptr
+ 7, parent
) ||
276 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
279 * The clone is shallow if nr_parent < 0, and we must
280 * not traverse its real parents even when we unhide them.
282 if (graft
&& (graft
->nr_parent
< 0 || grafts_replace_parents
))
284 new_parent
= lookup_commit(parent
);
286 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
290 struct commit
*new_parent
;
291 for (i
= 0; i
< graft
->nr_parent
; i
++) {
292 new_parent
= lookup_commit(graft
->parent
[i
]);
295 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
298 item
->date
= parse_commit_date(bufptr
, tail
);
303 int parse_commit(struct commit
*item
)
305 enum object_type type
;
312 if (item
->object
.parsed
)
314 buffer
= read_sha1_file(item
->object
.sha1
, &type
, &size
);
316 return error("Could not read %s",
317 sha1_to_hex(item
->object
.sha1
));
318 if (type
!= OBJ_COMMIT
) {
320 return error("Object %s not a commit",
321 sha1_to_hex(item
->object
.sha1
));
323 ret
= parse_commit_buffer(item
, buffer
, size
);
324 if (save_commit_buffer
&& !ret
) {
325 item
->buffer
= buffer
;
332 int find_commit_subject(const char *commit_buffer
, const char **subject
)
335 const char *p
= commit_buffer
;
337 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
341 for (eol
= p
; *eol
&& *eol
!= '\n'; eol
++)
351 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
353 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
354 new_list
->item
= item
;
355 new_list
->next
= *list_p
;
360 unsigned commit_list_count(const struct commit_list
*l
)
363 for (; l
; l
= l
->next
)
368 void free_commit_list(struct commit_list
*list
)
371 struct commit_list
*temp
= list
;
377 struct commit_list
* commit_list_insert_by_date(struct commit
*item
, struct commit_list
**list
)
379 struct commit_list
**pp
= list
;
380 struct commit_list
*p
;
381 while ((p
= *pp
) != NULL
) {
382 if (p
->item
->date
< item
->date
) {
387 return commit_list_insert(item
, pp
);
391 void commit_list_sort_by_date(struct commit_list
**list
)
393 struct commit_list
*ret
= NULL
;
395 commit_list_insert_by_date((*list
)->item
, &ret
);
396 *list
= (*list
)->next
;
401 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
404 struct commit
*ret
= (*list
)->item
;
405 struct commit_list
*parents
= ret
->parents
;
406 struct commit_list
*old
= *list
;
408 *list
= (*list
)->next
;
412 struct commit
*commit
= parents
->item
;
413 if (!parse_commit(commit
) && !(commit
->object
.flags
& mark
)) {
414 commit
->object
.flags
|= mark
;
415 commit_list_insert_by_date(commit
, list
);
417 parents
= parents
->next
;
422 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
425 struct commit_list
*parents
;
427 if (!(mark
& commit
->object
.flags
))
430 commit
->object
.flags
&= ~mark
;
432 parents
= commit
->parents
;
436 while ((parents
= parents
->next
))
437 clear_commit_marks(parents
->item
, mark
);
439 commit
= commit
->parents
->item
;
443 struct commit
*pop_commit(struct commit_list
**stack
)
445 struct commit_list
*top
= *stack
;
446 struct commit
*item
= top
? top
->item
: NULL
;
456 * Performs an in-place topological sort on the list supplied.
458 void sort_in_topological_order(struct commit_list
** list
, int lifo
)
460 struct commit_list
*next
, *orig
= *list
;
461 struct commit_list
*work
, **insert
;
462 struct commit_list
**pptr
;
468 /* Mark them and clear the indegree */
469 for (next
= orig
; next
; next
= next
->next
) {
470 struct commit
*commit
= next
->item
;
471 commit
->indegree
= 1;
474 /* update the indegree */
475 for (next
= orig
; next
; next
= next
->next
) {
476 struct commit_list
* parents
= next
->item
->parents
;
478 struct commit
*parent
= parents
->item
;
480 if (parent
->indegree
)
482 parents
= parents
->next
;
489 * tips are nodes not reachable from any other node in the list
491 * the tips serve as a starting set for the work queue.
495 for (next
= orig
; next
; next
= next
->next
) {
496 struct commit
*commit
= next
->item
;
498 if (commit
->indegree
== 1)
499 insert
= &commit_list_insert(commit
, insert
)->next
;
502 /* process the list in topological order */
504 commit_list_sort_by_date(&work
);
509 struct commit
*commit
;
510 struct commit_list
*parents
, *work_item
;
513 work
= work_item
->next
;
514 work_item
->next
= NULL
;
516 commit
= work_item
->item
;
517 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
518 struct commit
*parent
=parents
->item
;
520 if (!parent
->indegree
)
524 * parents are only enqueued for emission
525 * when all their children have been emitted thereby
526 * guaranteeing topological order.
528 if (--parent
->indegree
== 1) {
530 commit_list_insert_by_date(parent
, &work
);
532 commit_list_insert(parent
, &work
);
536 * work_item is a commit all of whose children
537 * have already been emitted. we can emit it now.
539 commit
->indegree
= 0;
541 pptr
= &work_item
->next
;
545 /* merge-base stuff */
547 /* bits #0..15 in revision.h */
548 #define PARENT1 (1u<<16)
549 #define PARENT2 (1u<<17)
550 #define STALE (1u<<18)
551 #define RESULT (1u<<19)
553 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
555 static struct commit
*interesting(struct commit_list
*list
)
558 struct commit
*commit
= list
->item
;
560 if (commit
->object
.flags
& STALE
)
567 static struct commit_list
*merge_bases_many(struct commit
*one
, int n
, struct commit
**twos
)
569 struct commit_list
*list
= NULL
;
570 struct commit_list
*result
= NULL
;
573 for (i
= 0; i
< n
; i
++) {
576 * We do not mark this even with RESULT so we do not
577 * have to clean it up.
579 return commit_list_insert(one
, &result
);
582 if (parse_commit(one
))
584 for (i
= 0; i
< n
; i
++) {
585 if (parse_commit(twos
[i
]))
589 one
->object
.flags
|= PARENT1
;
590 commit_list_insert_by_date(one
, &list
);
591 for (i
= 0; i
< n
; i
++) {
592 twos
[i
]->object
.flags
|= PARENT2
;
593 commit_list_insert_by_date(twos
[i
], &list
);
596 while (interesting(list
)) {
597 struct commit
*commit
;
598 struct commit_list
*parents
;
599 struct commit_list
*next
;
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 commit_list_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
)
624 p
->object
.flags
|= flags
;
625 commit_list_insert_by_date(p
, &list
);
629 /* Clean up the result to remove stale ones */
630 free_commit_list(list
);
631 list
= result
; result
= NULL
;
633 struct commit_list
*next
= list
->next
;
634 if (!(list
->item
->object
.flags
& STALE
))
635 commit_list_insert_by_date(list
->item
, &result
);
642 struct commit_list
*get_octopus_merge_bases(struct commit_list
*in
)
644 struct commit_list
*i
, *j
, *k
, *ret
= NULL
;
645 struct commit_list
**pptr
= &ret
;
647 for (i
= in
; i
; i
= i
->next
) {
649 pptr
= &commit_list_insert(i
->item
, pptr
)->next
;
651 struct commit_list
*new = NULL
, *end
= NULL
;
653 for (j
= ret
; j
; j
= j
->next
) {
654 struct commit_list
*bases
;
655 bases
= get_merge_bases(i
->item
, j
->item
, 1);
660 for (k
= bases
; k
; k
= k
->next
)
669 struct commit_list
*get_merge_bases_many(struct commit
*one
,
671 struct commit
**twos
,
674 struct commit_list
*list
;
675 struct commit
**rslt
;
676 struct commit_list
*result
;
679 result
= merge_bases_many(one
, n
, twos
);
680 for (i
= 0; i
< n
; i
++) {
684 if (!result
|| !result
->next
) {
686 clear_commit_marks(one
, all_flags
);
687 for (i
= 0; i
< n
; i
++)
688 clear_commit_marks(twos
[i
], all_flags
);
693 /* There are more than one */
700 rslt
= xcalloc(cnt
, sizeof(*rslt
));
701 for (list
= result
, i
= 0; list
; list
= list
->next
)
702 rslt
[i
++] = list
->item
;
703 free_commit_list(result
);
705 clear_commit_marks(one
, all_flags
);
706 for (i
= 0; i
< n
; i
++)
707 clear_commit_marks(twos
[i
], all_flags
);
708 for (i
= 0; i
< cnt
- 1; i
++) {
709 for (j
= i
+1; j
< cnt
; j
++) {
710 if (!rslt
[i
] || !rslt
[j
])
712 result
= merge_bases_many(rslt
[i
], 1, &rslt
[j
]);
713 clear_commit_marks(rslt
[i
], all_flags
);
714 clear_commit_marks(rslt
[j
], all_flags
);
715 for (list
= result
; list
; list
= list
->next
) {
716 if (rslt
[i
] == list
->item
)
718 if (rslt
[j
] == list
->item
)
724 /* Surviving ones in rslt[] are the independent results */
726 for (i
= 0; i
< cnt
; i
++) {
728 commit_list_insert_by_date(rslt
[i
], &result
);
734 struct commit_list
*get_merge_bases(struct commit
*one
, struct commit
*two
,
737 return get_merge_bases_many(one
, 1, &two
, cleanup
);
740 int is_descendant_of(struct commit
*commit
, struct commit_list
*with_commit
)
744 while (with_commit
) {
745 struct commit
*other
;
747 other
= with_commit
->item
;
748 with_commit
= with_commit
->next
;
749 if (in_merge_bases(other
, &commit
, 1))
755 int in_merge_bases(struct commit
*commit
, struct commit
**reference
, int num
)
757 struct commit_list
*bases
, *b
;
761 bases
= get_merge_bases(commit
, *reference
, 1);
764 for (b
= bases
; b
; b
= b
->next
) {
765 if (!hashcmp(commit
->object
.sha1
, b
->item
->object
.sha1
)) {
771 free_commit_list(bases
);
775 struct commit_list
*reduce_heads(struct commit_list
*heads
)
777 struct commit_list
*p
;
778 struct commit_list
*result
= NULL
, **tail
= &result
;
779 struct commit
**other
;
780 size_t num_head
, num_other
;
785 /* Avoid unnecessary reallocations */
786 for (p
= heads
, num_head
= 0; p
; p
= p
->next
)
788 other
= xcalloc(sizeof(*other
), num_head
);
790 /* For each commit, see if it can be reached by others */
791 for (p
= heads
; p
; p
= p
->next
) {
792 struct commit_list
*q
, *base
;
794 /* Do we already have this in the result? */
795 for (q
= result
; q
; q
= q
->next
)
796 if (p
->item
== q
->item
)
802 for (q
= heads
; q
; q
= q
->next
) {
803 if (p
->item
== q
->item
)
805 other
[num_other
++] = q
->item
;
808 base
= get_merge_bases_many(p
->item
, num_other
, other
, 1);
812 * If p->item does not have anything common with other
813 * commits, there won't be any merge base. If it is
814 * reachable from some of the others, p->item will be
815 * the merge base. If its history is connected with
816 * others, but p->item is not reachable by others, we
817 * will get something other than p->item back.
819 if (!base
|| (base
->item
!= p
->item
))
820 tail
= &(commit_list_insert(p
->item
, tail
)->next
);
821 free_commit_list(base
);
827 static const char commit_utf8_warn
[] =
828 "Warning: commit message does not conform to UTF-8.\n"
829 "You may want to amend it after fixing the message, or set the config\n"
830 "variable i18n.commitencoding to the encoding your project uses.\n";
832 int commit_tree(const char *msg
, unsigned char *tree
,
833 struct commit_list
*parents
, unsigned char *ret
,
837 int encoding_is_utf8
;
838 struct strbuf buffer
;
840 assert_sha1_type(tree
, OBJ_TREE
);
842 /* Not having i18n.commitencoding is the same as having utf-8 */
843 encoding_is_utf8
= is_encoding_utf8(git_commit_encoding
);
845 strbuf_init(&buffer
, 8192); /* should avoid reallocs for the headers */
846 strbuf_addf(&buffer
, "tree %s\n", sha1_to_hex(tree
));
849 * NOTE! This ordering means that the same exact tree merged with a
850 * different order of parents will be a _different_ changeset even
851 * if everything else stays the same.
854 struct commit_list
*next
= parents
->next
;
855 strbuf_addf(&buffer
, "parent %s\n",
856 sha1_to_hex(parents
->item
->object
.sha1
));
861 /* Person/date information */
863 author
= git_author_info(IDENT_ERROR_ON_NO_NAME
);
864 strbuf_addf(&buffer
, "author %s\n", author
);
865 strbuf_addf(&buffer
, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME
));
866 if (!encoding_is_utf8
)
867 strbuf_addf(&buffer
, "encoding %s\n", git_commit_encoding
);
868 strbuf_addch(&buffer
, '\n');
870 /* And add the comment */
871 strbuf_addstr(&buffer
, msg
);
873 /* And check the encoding */
874 if (encoding_is_utf8
&& !is_utf8(buffer
.buf
))
875 fprintf(stderr
, commit_utf8_warn
);
877 result
= write_sha1_file(buffer
.buf
, buffer
.len
, commit_type
, ret
);
878 strbuf_release(&buffer
);