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 for_each_commit_graft(each_commit_graft_fn fn
, void *cb_data
)
220 for (i
= ret
= 0; i
< commit_graft_nr
&& !ret
; i
++)
221 ret
= fn(commit_graft
[i
], cb_data
);
225 int unregister_shallow(const unsigned char *sha1
)
227 int pos
= commit_graft_pos(sha1
);
230 if (pos
+ 1 < commit_graft_nr
)
231 memmove(commit_graft
+ pos
, commit_graft
+ pos
+ 1,
232 sizeof(struct commit_graft
*)
233 * (commit_graft_nr
- pos
- 1));
238 int parse_commit_buffer(struct commit
*item
, const void *buffer
, unsigned long size
)
240 const char *tail
= buffer
;
241 const char *bufptr
= buffer
;
242 unsigned char parent
[20];
243 struct commit_list
**pptr
;
244 struct commit_graft
*graft
;
246 if (item
->object
.parsed
)
248 item
->object
.parsed
= 1;
250 if (tail
<= bufptr
+ 46 || memcmp(bufptr
, "tree ", 5) || bufptr
[45] != '\n')
251 return error("bogus commit object %s", sha1_to_hex(item
->object
.sha1
));
252 if (get_sha1_hex(bufptr
+ 5, parent
) < 0)
253 return error("bad tree pointer in commit %s",
254 sha1_to_hex(item
->object
.sha1
));
255 item
->tree
= lookup_tree(parent
);
256 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
257 pptr
= &item
->parents
;
259 graft
= lookup_commit_graft(item
->object
.sha1
);
260 while (bufptr
+ 48 < tail
&& !memcmp(bufptr
, "parent ", 7)) {
261 struct commit
*new_parent
;
263 if (tail
<= bufptr
+ 48 ||
264 get_sha1_hex(bufptr
+ 7, parent
) ||
266 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
269 * The clone is shallow if nr_parent < 0, and we must
270 * not traverse its real parents even when we unhide them.
272 if (graft
&& (graft
->nr_parent
< 0 || grafts_replace_parents
))
274 new_parent
= lookup_commit(parent
);
276 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
280 struct commit
*new_parent
;
281 for (i
= 0; i
< graft
->nr_parent
; i
++) {
282 new_parent
= lookup_commit(graft
->parent
[i
]);
285 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
288 item
->date
= parse_commit_date(bufptr
, tail
);
293 int parse_commit(struct commit
*item
)
295 enum object_type type
;
302 if (item
->object
.parsed
)
304 buffer
= read_sha1_file(item
->object
.sha1
, &type
, &size
);
306 return error("Could not read %s",
307 sha1_to_hex(item
->object
.sha1
));
308 if (type
!= OBJ_COMMIT
) {
310 return error("Object %s not a commit",
311 sha1_to_hex(item
->object
.sha1
));
313 ret
= parse_commit_buffer(item
, buffer
, size
);
314 if (save_commit_buffer
&& !ret
) {
315 item
->buffer
= buffer
;
322 int find_commit_subject(const char *commit_buffer
, const char **subject
)
325 const char *p
= commit_buffer
;
327 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
331 for (eol
= p
; *eol
&& *eol
!= '\n'; eol
++)
341 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
343 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
344 new_list
->item
= item
;
345 new_list
->next
= *list_p
;
350 unsigned commit_list_count(const struct commit_list
*l
)
353 for (; l
; l
= l
->next
)
358 void free_commit_list(struct commit_list
*list
)
361 struct commit_list
*temp
= list
;
367 struct commit_list
* commit_list_insert_by_date(struct commit
*item
, struct commit_list
**list
)
369 struct commit_list
**pp
= list
;
370 struct commit_list
*p
;
371 while ((p
= *pp
) != NULL
) {
372 if (p
->item
->date
< item
->date
) {
377 return commit_list_insert(item
, pp
);
381 void commit_list_sort_by_date(struct commit_list
**list
)
383 struct commit_list
*ret
= NULL
;
385 commit_list_insert_by_date((*list
)->item
, &ret
);
386 *list
= (*list
)->next
;
391 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
394 struct commit
*ret
= (*list
)->item
;
395 struct commit_list
*parents
= ret
->parents
;
396 struct commit_list
*old
= *list
;
398 *list
= (*list
)->next
;
402 struct commit
*commit
= parents
->item
;
403 if (!parse_commit(commit
) && !(commit
->object
.flags
& mark
)) {
404 commit
->object
.flags
|= mark
;
405 commit_list_insert_by_date(commit
, list
);
407 parents
= parents
->next
;
412 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
415 struct commit_list
*parents
;
417 if (!(mark
& commit
->object
.flags
))
420 commit
->object
.flags
&= ~mark
;
422 parents
= commit
->parents
;
426 while ((parents
= parents
->next
))
427 clear_commit_marks(parents
->item
, mark
);
429 commit
= commit
->parents
->item
;
433 struct commit
*pop_commit(struct commit_list
**stack
)
435 struct commit_list
*top
= *stack
;
436 struct commit
*item
= top
? top
->item
: NULL
;
446 * Performs an in-place topological sort on the list supplied.
448 void sort_in_topological_order(struct commit_list
** list
, int lifo
)
450 struct commit_list
*next
, *orig
= *list
;
451 struct commit_list
*work
, **insert
;
452 struct commit_list
**pptr
;
458 /* Mark them and clear the indegree */
459 for (next
= orig
; next
; next
= next
->next
) {
460 struct commit
*commit
= next
->item
;
461 commit
->indegree
= 1;
464 /* update the indegree */
465 for (next
= orig
; next
; next
= next
->next
) {
466 struct commit_list
* parents
= next
->item
->parents
;
468 struct commit
*parent
= parents
->item
;
470 if (parent
->indegree
)
472 parents
= parents
->next
;
479 * tips are nodes not reachable from any other node in the list
481 * the tips serve as a starting set for the work queue.
485 for (next
= orig
; next
; next
= next
->next
) {
486 struct commit
*commit
= next
->item
;
488 if (commit
->indegree
== 1)
489 insert
= &commit_list_insert(commit
, insert
)->next
;
492 /* process the list in topological order */
494 commit_list_sort_by_date(&work
);
499 struct commit
*commit
;
500 struct commit_list
*parents
, *work_item
;
503 work
= work_item
->next
;
504 work_item
->next
= NULL
;
506 commit
= work_item
->item
;
507 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
508 struct commit
*parent
= parents
->item
;
510 if (!parent
->indegree
)
514 * parents are only enqueued for emission
515 * when all their children have been emitted thereby
516 * guaranteeing topological order.
518 if (--parent
->indegree
== 1) {
520 commit_list_insert_by_date(parent
, &work
);
522 commit_list_insert(parent
, &work
);
526 * work_item is a commit all of whose children
527 * have already been emitted. we can emit it now.
529 commit
->indegree
= 0;
531 pptr
= &work_item
->next
;
535 /* merge-base stuff */
537 /* bits #0..15 in revision.h */
538 #define PARENT1 (1u<<16)
539 #define PARENT2 (1u<<17)
540 #define STALE (1u<<18)
541 #define RESULT (1u<<19)
543 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
545 static struct commit
*interesting(struct commit_list
*list
)
548 struct commit
*commit
= list
->item
;
550 if (commit
->object
.flags
& STALE
)
557 static struct commit_list
*merge_bases_many(struct commit
*one
, int n
, struct commit
**twos
)
559 struct commit_list
*list
= NULL
;
560 struct commit_list
*result
= NULL
;
563 for (i
= 0; i
< n
; i
++) {
566 * We do not mark this even with RESULT so we do not
567 * have to clean it up.
569 return commit_list_insert(one
, &result
);
572 if (parse_commit(one
))
574 for (i
= 0; i
< n
; i
++) {
575 if (parse_commit(twos
[i
]))
579 one
->object
.flags
|= PARENT1
;
580 commit_list_insert_by_date(one
, &list
);
581 for (i
= 0; i
< n
; i
++) {
582 twos
[i
]->object
.flags
|= PARENT2
;
583 commit_list_insert_by_date(twos
[i
], &list
);
586 while (interesting(list
)) {
587 struct commit
*commit
;
588 struct commit_list
*parents
;
589 struct commit_list
*next
;
597 flags
= commit
->object
.flags
& (PARENT1
| PARENT2
| STALE
);
598 if (flags
== (PARENT1
| PARENT2
)) {
599 if (!(commit
->object
.flags
& RESULT
)) {
600 commit
->object
.flags
|= RESULT
;
601 commit_list_insert_by_date(commit
, &result
);
603 /* Mark parents of a found merge stale */
606 parents
= commit
->parents
;
608 struct commit
*p
= parents
->item
;
609 parents
= parents
->next
;
610 if ((p
->object
.flags
& flags
) == flags
)
614 p
->object
.flags
|= flags
;
615 commit_list_insert_by_date(p
, &list
);
619 /* Clean up the result to remove stale ones */
620 free_commit_list(list
);
621 list
= result
; result
= NULL
;
623 struct commit_list
*next
= list
->next
;
624 if (!(list
->item
->object
.flags
& STALE
))
625 commit_list_insert_by_date(list
->item
, &result
);
632 struct commit_list
*get_octopus_merge_bases(struct commit_list
*in
)
634 struct commit_list
*i
, *j
, *k
, *ret
= NULL
;
635 struct commit_list
**pptr
= &ret
;
637 for (i
= in
; i
; i
= i
->next
) {
639 pptr
= &commit_list_insert(i
->item
, pptr
)->next
;
641 struct commit_list
*new = NULL
, *end
= NULL
;
643 for (j
= ret
; j
; j
= j
->next
) {
644 struct commit_list
*bases
;
645 bases
= get_merge_bases(i
->item
, j
->item
, 1);
650 for (k
= bases
; k
; k
= k
->next
)
659 struct commit_list
*get_merge_bases_many(struct commit
*one
,
661 struct commit
**twos
,
664 struct commit_list
*list
;
665 struct commit
**rslt
;
666 struct commit_list
*result
;
669 result
= merge_bases_many(one
, n
, twos
);
670 for (i
= 0; i
< n
; i
++) {
674 if (!result
|| !result
->next
) {
676 clear_commit_marks(one
, all_flags
);
677 for (i
= 0; i
< n
; i
++)
678 clear_commit_marks(twos
[i
], all_flags
);
683 /* There are more than one */
690 rslt
= xcalloc(cnt
, sizeof(*rslt
));
691 for (list
= result
, i
= 0; list
; list
= list
->next
)
692 rslt
[i
++] = list
->item
;
693 free_commit_list(result
);
695 clear_commit_marks(one
, all_flags
);
696 for (i
= 0; i
< n
; i
++)
697 clear_commit_marks(twos
[i
], all_flags
);
698 for (i
= 0; i
< cnt
- 1; i
++) {
699 for (j
= i
+1; j
< cnt
; j
++) {
700 if (!rslt
[i
] || !rslt
[j
])
702 result
= merge_bases_many(rslt
[i
], 1, &rslt
[j
]);
703 clear_commit_marks(rslt
[i
], all_flags
);
704 clear_commit_marks(rslt
[j
], all_flags
);
705 for (list
= result
; list
; list
= list
->next
) {
706 if (rslt
[i
] == list
->item
)
708 if (rslt
[j
] == list
->item
)
714 /* Surviving ones in rslt[] are the independent results */
716 for (i
= 0; i
< cnt
; i
++) {
718 commit_list_insert_by_date(rslt
[i
], &result
);
724 struct commit_list
*get_merge_bases(struct commit
*one
, struct commit
*two
,
727 return get_merge_bases_many(one
, 1, &two
, cleanup
);
730 int is_descendant_of(struct commit
*commit
, struct commit_list
*with_commit
)
734 while (with_commit
) {
735 struct commit
*other
;
737 other
= with_commit
->item
;
738 with_commit
= with_commit
->next
;
739 if (in_merge_bases(other
, &commit
, 1))
745 int in_merge_bases(struct commit
*commit
, struct commit
**reference
, int num
)
747 struct commit_list
*bases
, *b
;
751 bases
= get_merge_bases(commit
, *reference
, 1);
754 for (b
= bases
; b
; b
= b
->next
) {
755 if (!hashcmp(commit
->object
.sha1
, b
->item
->object
.sha1
)) {
761 free_commit_list(bases
);
765 struct commit_list
*reduce_heads(struct commit_list
*heads
)
767 struct commit_list
*p
;
768 struct commit_list
*result
= NULL
, **tail
= &result
;
769 struct commit
**other
;
770 size_t num_head
, num_other
;
775 /* Avoid unnecessary reallocations */
776 for (p
= heads
, num_head
= 0; p
; p
= p
->next
)
778 other
= xcalloc(sizeof(*other
), num_head
);
780 /* For each commit, see if it can be reached by others */
781 for (p
= heads
; p
; p
= p
->next
) {
782 struct commit_list
*q
, *base
;
784 /* Do we already have this in the result? */
785 for (q
= result
; q
; q
= q
->next
)
786 if (p
->item
== q
->item
)
792 for (q
= heads
; q
; q
= q
->next
) {
793 if (p
->item
== q
->item
)
795 other
[num_other
++] = q
->item
;
798 base
= get_merge_bases_many(p
->item
, num_other
, other
, 1);
802 * If p->item does not have anything common with other
803 * commits, there won't be any merge base. If it is
804 * reachable from some of the others, p->item will be
805 * the merge base. If its history is connected with
806 * others, but p->item is not reachable by others, we
807 * will get something other than p->item back.
809 if (!base
|| (base
->item
!= p
->item
))
810 tail
= &(commit_list_insert(p
->item
, tail
)->next
);
811 free_commit_list(base
);
817 static const char commit_utf8_warn
[] =
818 "Warning: commit message does not conform to UTF-8.\n"
819 "You may want to amend it after fixing the message, or set the config\n"
820 "variable i18n.commitencoding to the encoding your project uses.\n";
822 int commit_tree(const char *msg
, unsigned char *tree
,
823 struct commit_list
*parents
, unsigned char *ret
,
827 int encoding_is_utf8
;
828 struct strbuf buffer
;
830 assert_sha1_type(tree
, OBJ_TREE
);
832 /* Not having i18n.commitencoding is the same as having utf-8 */
833 encoding_is_utf8
= is_encoding_utf8(git_commit_encoding
);
835 strbuf_init(&buffer
, 8192); /* should avoid reallocs for the headers */
836 strbuf_addf(&buffer
, "tree %s\n", sha1_to_hex(tree
));
839 * NOTE! This ordering means that the same exact tree merged with a
840 * different order of parents will be a _different_ changeset even
841 * if everything else stays the same.
844 struct commit_list
*next
= parents
->next
;
845 strbuf_addf(&buffer
, "parent %s\n",
846 sha1_to_hex(parents
->item
->object
.sha1
));
851 /* Person/date information */
853 author
= git_author_info(IDENT_ERROR_ON_NO_NAME
);
854 strbuf_addf(&buffer
, "author %s\n", author
);
855 strbuf_addf(&buffer
, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME
));
856 if (!encoding_is_utf8
)
857 strbuf_addf(&buffer
, "encoding %s\n", git_commit_encoding
);
858 strbuf_addch(&buffer
, '\n');
860 /* And add the comment */
861 strbuf_addstr(&buffer
, msg
);
863 /* And check the encoding */
864 if (encoding_is_utf8
&& !is_utf8(buffer
.buf
))
865 fprintf(stderr
, commit_utf8_warn
);
867 result
= write_sha1_file(buffer
.buf
, buffer
.len
, commit_type
, ret
);
868 strbuf_release(&buffer
);