9 #include "gpg-interface.h"
10 #include "mergesort.h"
11 #include "commit-slab.h"
12 #include "prio-queue.h"
14 static struct commit_extra_header
*read_commit_extra_header_lines(const char *buf
, size_t len
, const char **);
16 int save_commit_buffer
= 1;
18 const char *commit_type
= "commit";
19 static int commit_count
;
21 static struct commit
*check_commit(struct object
*obj
,
22 const unsigned char *sha1
,
25 if (obj
->type
!= OBJ_COMMIT
) {
27 error("Object %s is a %s, not a commit",
28 sha1_to_hex(sha1
), typename(obj
->type
));
31 return (struct commit
*) obj
;
34 struct commit
*lookup_commit_reference_gently(const unsigned char *sha1
,
37 struct object
*obj
= deref_tag(parse_object(sha1
), NULL
, 0);
41 return check_commit(obj
, sha1
, quiet
);
44 struct commit
*lookup_commit_reference(const unsigned char *sha1
)
46 return lookup_commit_reference_gently(sha1
, 0);
49 struct commit
*lookup_commit_or_die(const unsigned char *sha1
, const char *ref_name
)
51 struct commit
*c
= lookup_commit_reference(sha1
);
53 die(_("could not parse %s"), ref_name
);
54 if (hashcmp(sha1
, c
->object
.sha1
)) {
55 warning(_("%s %s is not a commit!"),
56 ref_name
, sha1_to_hex(sha1
));
61 struct commit
*lookup_commit(const unsigned char *sha1
)
63 struct object
*obj
= lookup_object(sha1
);
65 struct commit
*c
= alloc_commit_node();
66 c
->index
= commit_count
++;
67 return create_object(sha1
, OBJ_COMMIT
, c
);
70 obj
->type
= OBJ_COMMIT
;
71 return check_commit(obj
, sha1
, 0);
74 struct commit
*lookup_commit_reference_by_name(const char *name
)
76 unsigned char sha1
[20];
77 struct commit
*commit
;
79 if (get_sha1_committish(name
, sha1
))
81 commit
= lookup_commit_reference(sha1
);
82 if (parse_commit(commit
))
87 static unsigned long parse_commit_date(const char *buf
, const char *tail
)
93 if (memcmp(buf
, "author", 6))
95 while (buf
< tail
&& *buf
++ != '\n')
99 if (memcmp(buf
, "committer", 9))
101 while (buf
< tail
&& *buf
++ != '>')
106 while (buf
< tail
&& *buf
++ != '\n')
110 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
111 return strtoul(dateptr
, NULL
, 10);
114 static struct commit_graft
**commit_graft
;
115 static int commit_graft_alloc
, commit_graft_nr
;
117 static int commit_graft_pos(const unsigned char *sha1
)
121 hi
= commit_graft_nr
;
123 int mi
= (lo
+ hi
) / 2;
124 struct commit_graft
*graft
= commit_graft
[mi
];
125 int cmp
= hashcmp(sha1
, graft
->sha1
);
136 int register_commit_graft(struct commit_graft
*graft
, int ignore_dups
)
138 int pos
= commit_graft_pos(graft
->sha1
);
144 free(commit_graft
[pos
]);
145 commit_graft
[pos
] = graft
;
150 ALLOC_GROW(commit_graft
, commit_graft_nr
+ 1, commit_graft_alloc
);
152 if (pos
< commit_graft_nr
)
153 memmove(commit_graft
+ pos
+ 1,
155 (commit_graft_nr
- pos
- 1) *
156 sizeof(*commit_graft
));
157 commit_graft
[pos
] = graft
;
161 struct commit_graft
*read_graft_line(char *buf
, int len
)
163 /* The format is just "Commit Parent1 Parent2 ...\n" */
165 struct commit_graft
*graft
= NULL
;
167 while (len
&& isspace(buf
[len
-1]))
169 if (buf
[0] == '#' || buf
[0] == '\0')
173 i
= (len
+ 1) / 41 - 1;
174 graft
= xmalloc(sizeof(*graft
) + 20 * i
);
175 graft
->nr_parent
= i
;
176 if (get_sha1_hex(buf
, graft
->sha1
))
178 for (i
= 40; i
< len
; i
+= 41) {
181 if (get_sha1_hex(buf
+ i
+ 1, graft
->parent
[i
/41]))
187 error("bad graft data: %s", buf
);
192 static int read_graft_file(const char *graft_file
)
194 FILE *fp
= fopen(graft_file
, "r");
195 struct strbuf buf
= STRBUF_INIT
;
198 while (!strbuf_getwholeline(&buf
, fp
, '\n')) {
199 /* The format is just "Commit Parent1 Parent2 ...\n" */
200 struct commit_graft
*graft
= read_graft_line(buf
.buf
, buf
.len
);
203 if (register_commit_graft(graft
, 1))
204 error("duplicate graft data: %s", buf
.buf
);
207 strbuf_release(&buf
);
211 static void prepare_commit_graft(void)
213 static int commit_graft_prepared
;
216 if (commit_graft_prepared
)
218 graft_file
= get_graft_file();
219 read_graft_file(graft_file
);
220 /* make sure shallows are read */
221 is_repository_shallow();
222 commit_graft_prepared
= 1;
225 struct commit_graft
*lookup_commit_graft(const unsigned char *sha1
)
228 prepare_commit_graft();
229 pos
= commit_graft_pos(sha1
);
232 return commit_graft
[pos
];
235 int for_each_commit_graft(each_commit_graft_fn fn
, void *cb_data
)
238 for (i
= ret
= 0; i
< commit_graft_nr
&& !ret
; i
++)
239 ret
= fn(commit_graft
[i
], cb_data
);
243 int unregister_shallow(const unsigned char *sha1
)
245 int pos
= commit_graft_pos(sha1
);
248 if (pos
+ 1 < commit_graft_nr
)
249 memmove(commit_graft
+ pos
, commit_graft
+ pos
+ 1,
250 sizeof(struct commit_graft
*)
251 * (commit_graft_nr
- pos
- 1));
256 int parse_commit_buffer(struct commit
*item
, const void *buffer
, unsigned long size
)
258 const char *tail
= buffer
;
259 const char *bufptr
= buffer
;
260 unsigned char parent
[20];
261 struct commit_list
**pptr
;
262 struct commit_graft
*graft
;
264 if (item
->object
.parsed
)
266 item
->object
.parsed
= 1;
268 if (tail
<= bufptr
+ 46 || memcmp(bufptr
, "tree ", 5) || bufptr
[45] != '\n')
269 return error("bogus commit object %s", sha1_to_hex(item
->object
.sha1
));
270 if (get_sha1_hex(bufptr
+ 5, parent
) < 0)
271 return error("bad tree pointer in commit %s",
272 sha1_to_hex(item
->object
.sha1
));
273 item
->tree
= lookup_tree(parent
);
274 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
275 pptr
= &item
->parents
;
277 graft
= lookup_commit_graft(item
->object
.sha1
);
278 while (bufptr
+ 48 < tail
&& !memcmp(bufptr
, "parent ", 7)) {
279 struct commit
*new_parent
;
281 if (tail
<= bufptr
+ 48 ||
282 get_sha1_hex(bufptr
+ 7, parent
) ||
284 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
287 * The clone is shallow if nr_parent < 0, and we must
288 * not traverse its real parents even when we unhide them.
290 if (graft
&& (graft
->nr_parent
< 0 || grafts_replace_parents
))
292 new_parent
= lookup_commit(parent
);
294 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
298 struct commit
*new_parent
;
299 for (i
= 0; i
< graft
->nr_parent
; i
++) {
300 new_parent
= lookup_commit(graft
->parent
[i
]);
303 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
306 item
->date
= parse_commit_date(bufptr
, tail
);
311 int parse_commit(struct commit
*item
)
313 enum object_type type
;
320 if (item
->object
.parsed
)
322 buffer
= read_sha1_file(item
->object
.sha1
, &type
, &size
);
324 return error("Could not read %s",
325 sha1_to_hex(item
->object
.sha1
));
326 if (type
!= OBJ_COMMIT
) {
328 return error("Object %s not a commit",
329 sha1_to_hex(item
->object
.sha1
));
331 ret
= parse_commit_buffer(item
, buffer
, size
);
332 if (save_commit_buffer
&& !ret
) {
333 item
->buffer
= buffer
;
340 void parse_commit_or_die(struct commit
*item
)
342 if (parse_commit(item
))
343 die("unable to parse commit %s",
344 item
? sha1_to_hex(item
->object
.sha1
) : "(null)");
347 int find_commit_subject(const char *commit_buffer
, const char **subject
)
350 const char *p
= commit_buffer
;
352 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
356 for (eol
= p
; *eol
&& *eol
!= '\n'; eol
++)
366 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
368 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
369 new_list
->item
= item
;
370 new_list
->next
= *list_p
;
375 unsigned commit_list_count(const struct commit_list
*l
)
378 for (; l
; l
= l
->next
)
383 struct commit_list
*copy_commit_list(struct commit_list
*list
)
385 struct commit_list
*head
= NULL
;
386 struct commit_list
**pp
= &head
;
388 struct commit_list
*new;
389 new = xmalloc(sizeof(struct commit_list
));
390 new->item
= list
->item
;
399 void free_commit_list(struct commit_list
*list
)
402 struct commit_list
*temp
= list
;
408 struct commit_list
* commit_list_insert_by_date(struct commit
*item
, struct commit_list
**list
)
410 struct commit_list
**pp
= list
;
411 struct commit_list
*p
;
412 while ((p
= *pp
) != NULL
) {
413 if (p
->item
->date
< item
->date
) {
418 return commit_list_insert(item
, pp
);
421 static int commit_list_compare_by_date(const void *a
, const void *b
)
423 unsigned long a_date
= ((const struct commit_list
*)a
)->item
->date
;
424 unsigned long b_date
= ((const struct commit_list
*)b
)->item
->date
;
432 static void *commit_list_get_next(const void *a
)
434 return ((const struct commit_list
*)a
)->next
;
437 static void commit_list_set_next(void *a
, void *next
)
439 ((struct commit_list
*)a
)->next
= next
;
442 void commit_list_sort_by_date(struct commit_list
**list
)
444 *list
= llist_mergesort(*list
, commit_list_get_next
, commit_list_set_next
,
445 commit_list_compare_by_date
);
448 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
451 struct commit
*ret
= (*list
)->item
;
452 struct commit_list
*parents
= ret
->parents
;
453 struct commit_list
*old
= *list
;
455 *list
= (*list
)->next
;
459 struct commit
*commit
= parents
->item
;
460 if (!parse_commit(commit
) && !(commit
->object
.flags
& mark
)) {
461 commit
->object
.flags
|= mark
;
462 commit_list_insert_by_date(commit
, list
);
464 parents
= parents
->next
;
469 static void clear_commit_marks_1(struct commit_list
**plist
,
470 struct commit
*commit
, unsigned int mark
)
473 struct commit_list
*parents
;
475 if (!(mark
& commit
->object
.flags
))
478 commit
->object
.flags
&= ~mark
;
480 parents
= commit
->parents
;
484 while ((parents
= parents
->next
))
485 commit_list_insert(parents
->item
, plist
);
487 commit
= commit
->parents
->item
;
491 void clear_commit_marks_many(int nr
, struct commit
**commit
, unsigned int mark
)
493 struct commit_list
*list
= NULL
;
496 commit_list_insert(*commit
, &list
);
500 clear_commit_marks_1(&list
, pop_commit(&list
), mark
);
503 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
505 clear_commit_marks_many(1, &commit
, mark
);
508 void clear_commit_marks_for_object_array(struct object_array
*a
, unsigned mark
)
510 struct object
*object
;
511 struct commit
*commit
;
514 for (i
= 0; i
< a
->nr
; i
++) {
515 object
= a
->objects
[i
].item
;
516 commit
= lookup_commit_reference_gently(object
->sha1
, 1);
518 clear_commit_marks(commit
, mark
);
522 struct commit
*pop_commit(struct commit_list
**stack
)
524 struct commit_list
*top
= *stack
;
525 struct commit
*item
= top
? top
->item
: NULL
;
535 * Topological sort support
538 /* count number of children that have not been emitted */
539 define_commit_slab(indegree_slab
, int);
541 /* record author-date for each commit object */
542 define_commit_slab(author_date_slab
, unsigned long);
544 static void record_author_date(struct author_date_slab
*author_date
,
545 struct commit
*commit
)
547 const char *buf
, *line_end
;
549 struct ident_split ident
;
553 if (!commit
->buffer
) {
555 enum object_type type
;
556 buffer
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
561 for (buf
= commit
->buffer
? commit
->buffer
: buffer
;
563 buf
= line_end
+ 1) {
564 line_end
= strchrnul(buf
, '\n');
565 if (!starts_with(buf
, "author ")) {
566 if (!line_end
[0] || line_end
[1] == '\n')
567 return; /* end of header */
570 if (split_ident_line(&ident
,
571 buf
+ strlen("author "),
572 line_end
- (buf
+ strlen("author "))) ||
573 !ident
.date_begin
|| !ident
.date_end
)
574 goto fail_exit
; /* malformed "author" line */
578 date
= strtoul(ident
.date_begin
, &date_end
, 10);
579 if (date_end
!= ident
.date_end
)
580 goto fail_exit
; /* malformed date */
581 *(author_date_slab_at(author_date
, commit
)) = date
;
587 static int compare_commits_by_author_date(const void *a_
, const void *b_
,
590 const struct commit
*a
= a_
, *b
= b_
;
591 struct author_date_slab
*author_date
= cb_data
;
592 unsigned long a_date
= *(author_date_slab_at(author_date
, a
));
593 unsigned long b_date
= *(author_date_slab_at(author_date
, b
));
595 /* newer commits with larger date first */
598 else if (a_date
> b_date
)
603 int compare_commits_by_commit_date(const void *a_
, const void *b_
, void *unused
)
605 const struct commit
*a
= a_
, *b
= b_
;
606 /* newer commits with larger date first */
607 if (a
->date
< b
->date
)
609 else if (a
->date
> b
->date
)
615 * Performs an in-place topological sort on the list supplied.
617 void sort_in_topological_order(struct commit_list
**list
, enum rev_sort_order sort_order
)
619 struct commit_list
*next
, *orig
= *list
;
620 struct commit_list
**pptr
;
621 struct indegree_slab indegree
;
622 struct prio_queue queue
;
623 struct commit
*commit
;
624 struct author_date_slab author_date
;
630 init_indegree_slab(&indegree
);
631 memset(&queue
, '\0', sizeof(queue
));
633 switch (sort_order
) {
634 default: /* REV_SORT_IN_GRAPH_ORDER */
635 queue
.compare
= NULL
;
637 case REV_SORT_BY_COMMIT_DATE
:
638 queue
.compare
= compare_commits_by_commit_date
;
640 case REV_SORT_BY_AUTHOR_DATE
:
641 init_author_date_slab(&author_date
);
642 queue
.compare
= compare_commits_by_author_date
;
643 queue
.cb_data
= &author_date
;
647 /* Mark them and clear the indegree */
648 for (next
= orig
; next
; next
= next
->next
) {
649 struct commit
*commit
= next
->item
;
650 *(indegree_slab_at(&indegree
, commit
)) = 1;
651 /* also record the author dates, if needed */
652 if (sort_order
== REV_SORT_BY_AUTHOR_DATE
)
653 record_author_date(&author_date
, commit
);
656 /* update the indegree */
657 for (next
= orig
; next
; next
= next
->next
) {
658 struct commit_list
*parents
= next
->item
->parents
;
660 struct commit
*parent
= parents
->item
;
661 int *pi
= indegree_slab_at(&indegree
, parent
);
665 parents
= parents
->next
;
672 * tips are nodes not reachable from any other node in the list
674 * the tips serve as a starting set for the work queue.
676 for (next
= orig
; next
; next
= next
->next
) {
677 struct commit
*commit
= next
->item
;
679 if (*(indegree_slab_at(&indegree
, commit
)) == 1)
680 prio_queue_put(&queue
, commit
);
684 * This is unfortunate; the initial tips need to be shown
685 * in the order given from the revision traversal machinery.
687 if (sort_order
== REV_SORT_IN_GRAPH_ORDER
)
688 prio_queue_reverse(&queue
);
690 /* We no longer need the commit list */
691 free_commit_list(orig
);
695 while ((commit
= prio_queue_get(&queue
)) != NULL
) {
696 struct commit_list
*parents
;
698 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
699 struct commit
*parent
= parents
->item
;
700 int *pi
= indegree_slab_at(&indegree
, parent
);
706 * parents are only enqueued for emission
707 * when all their children have been emitted thereby
708 * guaranteeing topological order.
711 prio_queue_put(&queue
, parent
);
714 * all children of commit have already been
715 * emitted. we can emit it now.
717 *(indegree_slab_at(&indegree
, commit
)) = 0;
719 pptr
= &commit_list_insert(commit
, pptr
)->next
;
722 clear_indegree_slab(&indegree
);
723 clear_prio_queue(&queue
);
724 if (sort_order
== REV_SORT_BY_AUTHOR_DATE
)
725 clear_author_date_slab(&author_date
);
728 /* merge-base stuff */
730 /* bits #0..15 in revision.h */
731 #define PARENT1 (1u<<16)
732 #define PARENT2 (1u<<17)
733 #define STALE (1u<<18)
734 #define RESULT (1u<<19)
736 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
738 static struct commit
*interesting(struct commit_list
*list
)
741 struct commit
*commit
= list
->item
;
743 if (commit
->object
.flags
& STALE
)
750 /* all input commits in one and twos[] must have been parsed! */
751 static struct commit_list
*paint_down_to_common(struct commit
*one
, int n
, struct commit
**twos
)
753 struct commit_list
*list
= NULL
;
754 struct commit_list
*result
= NULL
;
757 one
->object
.flags
|= PARENT1
;
758 commit_list_insert_by_date(one
, &list
);
761 for (i
= 0; i
< n
; i
++) {
762 twos
[i
]->object
.flags
|= PARENT2
;
763 commit_list_insert_by_date(twos
[i
], &list
);
766 while (interesting(list
)) {
767 struct commit
*commit
;
768 struct commit_list
*parents
;
769 struct commit_list
*next
;
777 flags
= commit
->object
.flags
& (PARENT1
| PARENT2
| STALE
);
778 if (flags
== (PARENT1
| PARENT2
)) {
779 if (!(commit
->object
.flags
& RESULT
)) {
780 commit
->object
.flags
|= RESULT
;
781 commit_list_insert_by_date(commit
, &result
);
783 /* Mark parents of a found merge stale */
786 parents
= commit
->parents
;
788 struct commit
*p
= parents
->item
;
789 parents
= parents
->next
;
790 if ((p
->object
.flags
& flags
) == flags
)
794 p
->object
.flags
|= flags
;
795 commit_list_insert_by_date(p
, &list
);
799 free_commit_list(list
);
803 static struct commit_list
*merge_bases_many(struct commit
*one
, int n
, struct commit
**twos
)
805 struct commit_list
*list
= NULL
;
806 struct commit_list
*result
= NULL
;
809 for (i
= 0; i
< n
; i
++) {
812 * We do not mark this even with RESULT so we do not
813 * have to clean it up.
815 return commit_list_insert(one
, &result
);
818 if (parse_commit(one
))
820 for (i
= 0; i
< n
; i
++) {
821 if (parse_commit(twos
[i
]))
825 list
= paint_down_to_common(one
, n
, twos
);
828 struct commit_list
*next
= list
->next
;
829 if (!(list
->item
->object
.flags
& STALE
))
830 commit_list_insert_by_date(list
->item
, &result
);
837 struct commit_list
*get_octopus_merge_bases(struct commit_list
*in
)
839 struct commit_list
*i
, *j
, *k
, *ret
= NULL
;
844 commit_list_insert(in
->item
, &ret
);
846 for (i
= in
->next
; i
; i
= i
->next
) {
847 struct commit_list
*new = NULL
, *end
= NULL
;
849 for (j
= ret
; j
; j
= j
->next
) {
850 struct commit_list
*bases
;
851 bases
= get_merge_bases(i
->item
, j
->item
, 1);
856 for (k
= bases
; k
; k
= k
->next
)
864 static int remove_redundant(struct commit
**array
, int cnt
)
867 * Some commit in the array may be an ancestor of
868 * another commit. Move such commit to the end of
869 * the array, and return the number of commits that
870 * are independent from each other.
872 struct commit
**work
;
873 unsigned char *redundant
;
877 work
= xcalloc(cnt
, sizeof(*work
));
878 redundant
= xcalloc(cnt
, 1);
879 filled_index
= xmalloc(sizeof(*filled_index
) * (cnt
- 1));
881 for (i
= 0; i
< cnt
; i
++)
882 parse_commit(array
[i
]);
883 for (i
= 0; i
< cnt
; i
++) {
884 struct commit_list
*common
;
888 for (j
= filled
= 0; j
< cnt
; j
++) {
889 if (i
== j
|| redundant
[j
])
891 filled_index
[filled
] = j
;
892 work
[filled
++] = array
[j
];
894 common
= paint_down_to_common(array
[i
], filled
, work
);
895 if (array
[i
]->object
.flags
& PARENT2
)
897 for (j
= 0; j
< filled
; j
++)
898 if (work
[j
]->object
.flags
& PARENT1
)
899 redundant
[filled_index
[j
]] = 1;
900 clear_commit_marks(array
[i
], all_flags
);
901 for (j
= 0; j
< filled
; j
++)
902 clear_commit_marks(work
[j
], all_flags
);
903 free_commit_list(common
);
906 /* Now collect the result */
907 memcpy(work
, array
, sizeof(*array
) * cnt
);
908 for (i
= filled
= 0; i
< cnt
; i
++)
910 array
[filled
++] = work
[i
];
911 for (j
= filled
, i
= 0; i
< cnt
; i
++)
913 array
[j
++] = work
[i
];
920 struct commit_list
*get_merge_bases_many(struct commit
*one
,
922 struct commit
**twos
,
925 struct commit_list
*list
;
926 struct commit
**rslt
;
927 struct commit_list
*result
;
930 result
= merge_bases_many(one
, n
, twos
);
931 for (i
= 0; i
< n
; i
++) {
935 if (!result
|| !result
->next
) {
937 clear_commit_marks(one
, all_flags
);
938 clear_commit_marks_many(n
, twos
, all_flags
);
943 /* There are more than one */
950 rslt
= xcalloc(cnt
, sizeof(*rslt
));
951 for (list
= result
, i
= 0; list
; list
= list
->next
)
952 rslt
[i
++] = list
->item
;
953 free_commit_list(result
);
955 clear_commit_marks(one
, all_flags
);
956 clear_commit_marks_many(n
, twos
, all_flags
);
958 cnt
= remove_redundant(rslt
, cnt
);
960 for (i
= 0; i
< cnt
; i
++)
961 commit_list_insert_by_date(rslt
[i
], &result
);
966 struct commit_list
*get_merge_bases(struct commit
*one
, struct commit
*two
,
969 return get_merge_bases_many(one
, 1, &two
, cleanup
);
973 * Is "commit" a descendant of one of the elements on the "with_commit" list?
975 int is_descendant_of(struct commit
*commit
, struct commit_list
*with_commit
)
979 while (with_commit
) {
980 struct commit
*other
;
982 other
= with_commit
->item
;
983 with_commit
= with_commit
->next
;
984 if (in_merge_bases(other
, commit
))
991 * Is "commit" an ancestor of one of the "references"?
993 int in_merge_bases_many(struct commit
*commit
, int nr_reference
, struct commit
**reference
)
995 struct commit_list
*bases
;
998 if (parse_commit(commit
))
1000 for (i
= 0; i
< nr_reference
; i
++)
1001 if (parse_commit(reference
[i
]))
1004 bases
= paint_down_to_common(commit
, nr_reference
, reference
);
1005 if (commit
->object
.flags
& PARENT2
)
1007 clear_commit_marks(commit
, all_flags
);
1008 clear_commit_marks_many(nr_reference
, reference
, all_flags
);
1009 free_commit_list(bases
);
1014 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
1016 int in_merge_bases(struct commit
*commit
, struct commit
*reference
)
1018 return in_merge_bases_many(commit
, 1, &reference
);
1021 struct commit_list
*reduce_heads(struct commit_list
*heads
)
1023 struct commit_list
*p
;
1024 struct commit_list
*result
= NULL
, **tail
= &result
;
1025 struct commit
**array
;
1032 for (p
= heads
; p
; p
= p
->next
)
1033 p
->item
->object
.flags
&= ~STALE
;
1034 for (p
= heads
, num_head
= 0; p
; p
= p
->next
) {
1035 if (p
->item
->object
.flags
& STALE
)
1037 p
->item
->object
.flags
|= STALE
;
1040 array
= xcalloc(sizeof(*array
), num_head
);
1041 for (p
= heads
, i
= 0; p
; p
= p
->next
) {
1042 if (p
->item
->object
.flags
& STALE
) {
1043 array
[i
++] = p
->item
;
1044 p
->item
->object
.flags
&= ~STALE
;
1047 num_head
= remove_redundant(array
, num_head
);
1048 for (i
= 0; i
< num_head
; i
++)
1049 tail
= &commit_list_insert(array
[i
], tail
)->next
;
1053 static const char gpg_sig_header
[] = "gpgsig";
1054 static const int gpg_sig_header_len
= sizeof(gpg_sig_header
) - 1;
1056 static int do_sign_commit(struct strbuf
*buf
, const char *keyid
)
1058 struct strbuf sig
= STRBUF_INIT
;
1059 int inspos
, copypos
;
1061 /* find the end of the header */
1062 inspos
= strstr(buf
->buf
, "\n\n") - buf
->buf
+ 1;
1064 if (!keyid
|| !*keyid
)
1065 keyid
= get_signing_key();
1066 if (sign_buffer(buf
, &sig
, keyid
)) {
1067 strbuf_release(&sig
);
1071 for (copypos
= 0; sig
.buf
[copypos
]; ) {
1072 const char *bol
= sig
.buf
+ copypos
;
1073 const char *eol
= strchrnul(bol
, '\n');
1074 int len
= (eol
- bol
) + !!*eol
;
1077 strbuf_insert(buf
, inspos
, gpg_sig_header
, gpg_sig_header_len
);
1078 inspos
+= gpg_sig_header_len
;
1080 strbuf_insert(buf
, inspos
++, " ", 1);
1081 strbuf_insert(buf
, inspos
, bol
, len
);
1085 strbuf_release(&sig
);
1089 int parse_signed_commit(const unsigned char *sha1
,
1090 struct strbuf
*payload
, struct strbuf
*signature
)
1093 enum object_type type
;
1094 char *buffer
= read_sha1_file(sha1
, &type
, &size
);
1095 int in_signature
, saw_signature
= -1;
1098 if (!buffer
|| type
!= OBJ_COMMIT
)
1102 tail
= buffer
+ size
;
1105 while (line
< tail
) {
1106 const char *sig
= NULL
;
1107 char *next
= memchr(line
, '\n', tail
- line
);
1109 next
= next
? next
+ 1 : tail
;
1110 if (in_signature
&& line
[0] == ' ')
1112 else if (starts_with(line
, gpg_sig_header
) &&
1113 line
[gpg_sig_header_len
] == ' ')
1114 sig
= line
+ gpg_sig_header_len
+ 1;
1116 strbuf_add(signature
, sig
, next
- sig
);
1121 /* dump the whole remainder of the buffer */
1123 strbuf_add(payload
, line
, next
- line
);
1130 return saw_signature
;
1133 static void handle_signed_tag(struct commit
*parent
, struct commit_extra_header
***tail
)
1135 struct merge_remote_desc
*desc
;
1136 struct commit_extra_header
*mergetag
;
1138 unsigned long size
, len
;
1139 enum object_type type
;
1141 desc
= merge_remote_util(parent
);
1142 if (!desc
|| !desc
->obj
)
1144 buf
= read_sha1_file(desc
->obj
->sha1
, &type
, &size
);
1145 if (!buf
|| type
!= OBJ_TAG
)
1147 len
= parse_signature(buf
, size
);
1151 * We could verify this signature and either omit the tag when
1152 * it does not validate, but the integrator may not have the
1153 * public key of the signer of the tag he is merging, while a
1154 * later auditor may have it while auditing, so let's not run
1155 * verify-signed-buffer here for now...
1157 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1158 * warn("warning: signed tag unverified.");
1160 mergetag
= xcalloc(1, sizeof(*mergetag
));
1161 mergetag
->key
= xstrdup("mergetag");
1162 mergetag
->value
= buf
;
1163 mergetag
->len
= size
;
1166 *tail
= &mergetag
->next
;
1176 } sigcheck_gpg_status
[] = {
1177 { 'G', "\n[GNUPG:] GOODSIG " },
1178 { 'B', "\n[GNUPG:] BADSIG " },
1179 { 'U', "\n[GNUPG:] TRUST_NEVER" },
1180 { 'U', "\n[GNUPG:] TRUST_UNDEFINED" },
1183 static void parse_gpg_output(struct signature_check
*sigc
)
1185 const char *buf
= sigc
->gpg_status
;
1188 /* Iterate over all search strings */
1189 for (i
= 0; i
< ARRAY_SIZE(sigcheck_gpg_status
); i
++) {
1190 const char *found
, *next
;
1192 if (starts_with(buf
, sigcheck_gpg_status
[i
].check
+ 1)) {
1193 /* At the very beginning of the buffer */
1194 found
= buf
+ strlen(sigcheck_gpg_status
[i
].check
+ 1);
1196 found
= strstr(buf
, sigcheck_gpg_status
[i
].check
);
1199 found
+= strlen(sigcheck_gpg_status
[i
].check
);
1201 sigc
->result
= sigcheck_gpg_status
[i
].result
;
1202 /* The trust messages are not followed by key/signer information */
1203 if (sigc
->result
!= 'U') {
1204 sigc
->key
= xmemdupz(found
, 16);
1206 next
= strchrnul(found
, '\n');
1207 sigc
->signer
= xmemdupz(found
, next
- found
);
1212 void check_commit_signature(const struct commit
* commit
, struct signature_check
*sigc
)
1214 struct strbuf payload
= STRBUF_INIT
;
1215 struct strbuf signature
= STRBUF_INIT
;
1216 struct strbuf gpg_output
= STRBUF_INIT
;
1217 struct strbuf gpg_status
= STRBUF_INIT
;
1222 if (parse_signed_commit(commit
->object
.sha1
,
1223 &payload
, &signature
) <= 0)
1225 status
= verify_signed_buffer(payload
.buf
, payload
.len
,
1226 signature
.buf
, signature
.len
,
1227 &gpg_output
, &gpg_status
);
1228 if (status
&& !gpg_output
.len
)
1230 sigc
->gpg_output
= strbuf_detach(&gpg_output
, NULL
);
1231 sigc
->gpg_status
= strbuf_detach(&gpg_status
, NULL
);
1232 parse_gpg_output(sigc
);
1235 strbuf_release(&gpg_status
);
1236 strbuf_release(&gpg_output
);
1237 strbuf_release(&payload
);
1238 strbuf_release(&signature
);
1243 void append_merge_tag_headers(struct commit_list
*parents
,
1244 struct commit_extra_header
***tail
)
1247 struct commit
*parent
= parents
->item
;
1248 handle_signed_tag(parent
, tail
);
1249 parents
= parents
->next
;
1253 static void add_extra_header(struct strbuf
*buffer
,
1254 struct commit_extra_header
*extra
)
1256 strbuf_addstr(buffer
, extra
->key
);
1258 strbuf_add_lines(buffer
, " ", extra
->value
, extra
->len
);
1260 strbuf_addch(buffer
, '\n');
1263 struct commit_extra_header
*read_commit_extra_headers(struct commit
*commit
,
1264 const char **exclude
)
1266 struct commit_extra_header
*extra
= NULL
;
1268 enum object_type type
;
1269 char *buffer
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
1270 if (buffer
&& type
== OBJ_COMMIT
)
1271 extra
= read_commit_extra_header_lines(buffer
, size
, exclude
);
1276 static inline int standard_header_field(const char *field
, size_t len
)
1278 return ((len
== 4 && !memcmp(field
, "tree ", 5)) ||
1279 (len
== 6 && !memcmp(field
, "parent ", 7)) ||
1280 (len
== 6 && !memcmp(field
, "author ", 7)) ||
1281 (len
== 9 && !memcmp(field
, "committer ", 10)) ||
1282 (len
== 8 && !memcmp(field
, "encoding ", 9)));
1285 static int excluded_header_field(const char *field
, size_t len
, const char **exclude
)
1291 size_t xlen
= strlen(*exclude
);
1293 !memcmp(field
, *exclude
, xlen
) && field
[xlen
] == ' ')
1300 static struct commit_extra_header
*read_commit_extra_header_lines(
1301 const char *buffer
, size_t size
,
1302 const char **exclude
)
1304 struct commit_extra_header
*extra
= NULL
, **tail
= &extra
, *it
= NULL
;
1305 const char *line
, *next
, *eof
, *eob
;
1306 struct strbuf buf
= STRBUF_INIT
;
1308 for (line
= buffer
, eob
= line
+ size
;
1309 line
< eob
&& *line
!= '\n';
1311 next
= memchr(line
, '\n', eob
- line
);
1312 next
= next
? next
+ 1 : eob
;
1316 strbuf_add(&buf
, line
+ 1, next
- (line
+ 1));
1320 it
->value
= strbuf_detach(&buf
, &it
->len
);
1324 eof
= strchr(line
, ' ');
1328 if (standard_header_field(line
, eof
- line
) ||
1329 excluded_header_field(line
, eof
- line
, exclude
))
1332 it
= xcalloc(1, sizeof(*it
));
1333 it
->key
= xmemdupz(line
, eof
-line
);
1337 strbuf_add(&buf
, eof
+ 1, next
- (eof
+ 1));
1340 it
->value
= strbuf_detach(&buf
, &it
->len
);
1344 void free_commit_extra_headers(struct commit_extra_header
*extra
)
1347 struct commit_extra_header
*next
= extra
->next
;
1355 int commit_tree(const struct strbuf
*msg
, const unsigned char *tree
,
1356 struct commit_list
*parents
, unsigned char *ret
,
1357 const char *author
, const char *sign_commit
)
1359 struct commit_extra_header
*extra
= NULL
, **tail
= &extra
;
1362 append_merge_tag_headers(parents
, &tail
);
1363 result
= commit_tree_extended(msg
, tree
, parents
, ret
,
1364 author
, sign_commit
, extra
);
1365 free_commit_extra_headers(extra
);
1369 static int find_invalid_utf8(const char *buf
, int len
)
1372 static const unsigned int max_codepoint
[] = {
1373 0x7f, 0x7ff, 0xffff, 0x10ffff
1377 unsigned char c
= *buf
++;
1378 int bytes
, bad_offset
;
1379 unsigned int codepoint
;
1380 unsigned int min_val
, max_val
;
1385 /* Simple US-ASCII? No worries. */
1389 bad_offset
= offset
-1;
1392 * Count how many more high bits set: that's how
1393 * many more bytes this sequence should have.
1402 * Must be between 1 and 3 more bytes. Longer sequences result in
1403 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1405 if (bytes
< 1 || 3 < bytes
)
1408 /* Do we *have* that many bytes? */
1413 * Place the encoded bits at the bottom of the value and compute the
1416 codepoint
= (c
& 0x7f) >> bytes
;
1417 min_val
= max_codepoint
[bytes
-1] + 1;
1418 max_val
= max_codepoint
[bytes
];
1423 /* And verify that they are good continuation bytes */
1426 codepoint
|= *buf
& 0x3f;
1427 if ((*buf
++ & 0xc0) != 0x80)
1431 /* Reject codepoints that are out of range for the sequence length. */
1432 if (codepoint
< min_val
|| codepoint
> max_val
)
1434 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1435 if ((codepoint
& 0x1ff800) == 0xd800)
1437 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1438 if ((codepoint
& 0xfffe) == 0xfffe)
1440 /* So are anything in the range U+FDD0..U+FDEF. */
1441 if (codepoint
>= 0xfdd0 && codepoint
<= 0xfdef)
1448 * This verifies that the buffer is in proper utf8 format.
1450 * If it isn't, it assumes any non-utf8 characters are Latin1,
1451 * and does the conversion.
1453 static int verify_utf8(struct strbuf
*buf
)
1461 unsigned char replace
[2];
1463 bad
= find_invalid_utf8(buf
->buf
+ pos
, buf
->len
- pos
);
1469 strbuf_remove(buf
, pos
, 1);
1471 /* We know 'c' must be in the range 128-255 */
1472 replace
[0] = 0xc0 + (c
>> 6);
1473 replace
[1] = 0x80 + (c
& 0x3f);
1474 strbuf_insert(buf
, pos
, replace
, 2);
1479 static const char commit_utf8_warn
[] =
1480 "Warning: commit message did not conform to UTF-8.\n"
1481 "You may want to amend it after fixing the message, or set the config\n"
1482 "variable i18n.commitencoding to the encoding your project uses.\n";
1484 int commit_tree_extended(const struct strbuf
*msg
, const unsigned char *tree
,
1485 struct commit_list
*parents
, unsigned char *ret
,
1486 const char *author
, const char *sign_commit
,
1487 struct commit_extra_header
*extra
)
1490 int encoding_is_utf8
;
1491 struct strbuf buffer
;
1493 assert_sha1_type(tree
, OBJ_TREE
);
1495 if (memchr(msg
->buf
, '\0', msg
->len
))
1496 return error("a NUL byte in commit log message not allowed.");
1498 /* Not having i18n.commitencoding is the same as having utf-8 */
1499 encoding_is_utf8
= is_encoding_utf8(git_commit_encoding
);
1501 strbuf_init(&buffer
, 8192); /* should avoid reallocs for the headers */
1502 strbuf_addf(&buffer
, "tree %s\n", sha1_to_hex(tree
));
1505 * NOTE! This ordering means that the same exact tree merged with a
1506 * different order of parents will be a _different_ changeset even
1507 * if everything else stays the same.
1510 struct commit_list
*next
= parents
->next
;
1511 struct commit
*parent
= parents
->item
;
1513 strbuf_addf(&buffer
, "parent %s\n",
1514 sha1_to_hex(parent
->object
.sha1
));
1519 /* Person/date information */
1521 author
= git_author_info(IDENT_STRICT
);
1522 strbuf_addf(&buffer
, "author %s\n", author
);
1523 strbuf_addf(&buffer
, "committer %s\n", git_committer_info(IDENT_STRICT
));
1524 if (!encoding_is_utf8
)
1525 strbuf_addf(&buffer
, "encoding %s\n", git_commit_encoding
);
1528 add_extra_header(&buffer
, extra
);
1529 extra
= extra
->next
;
1531 strbuf_addch(&buffer
, '\n');
1533 /* And add the comment */
1534 strbuf_addbuf(&buffer
, msg
);
1536 /* And check the encoding */
1537 if (encoding_is_utf8
&& !verify_utf8(&buffer
))
1538 fprintf(stderr
, commit_utf8_warn
);
1540 if (sign_commit
&& do_sign_commit(&buffer
, sign_commit
))
1543 result
= write_sha1_file(buffer
.buf
, buffer
.len
, commit_type
, ret
);
1544 strbuf_release(&buffer
);
1548 struct commit
*get_merge_parent(const char *name
)
1551 struct commit
*commit
;
1552 unsigned char sha1
[20];
1553 if (get_sha1(name
, sha1
))
1555 obj
= parse_object(sha1
);
1556 commit
= (struct commit
*)peel_to_type(name
, 0, obj
, OBJ_COMMIT
);
1557 if (commit
&& !commit
->util
) {
1558 struct merge_remote_desc
*desc
;
1559 desc
= xmalloc(sizeof(*desc
));
1561 desc
->name
= strdup(name
);
1562 commit
->util
= desc
;
1568 * Append a commit to the end of the commit_list.
1570 * next starts by pointing to the variable that holds the head of an
1571 * empty commit_list, and is updated to point to the "next" field of
1572 * the last item on the list as new commits are appended.
1576 * struct commit_list *list;
1577 * struct commit_list **next = &list;
1579 * next = commit_list_append(c1, next);
1580 * next = commit_list_append(c2, next);
1581 * assert(commit_list_count(list) == 2);
1584 struct commit_list
**commit_list_append(struct commit
*commit
,
1585 struct commit_list
**next
)
1587 struct commit_list
*new = xmalloc(sizeof(struct commit_list
));
1594 void print_commit_list(struct commit_list
*list
,
1595 const char *format_cur
,
1596 const char *format_last
)
1598 for ( ; list
; list
= list
->next
) {
1599 const char *format
= list
->next
? format_cur
: format_last
;
1600 printf(format
, sha1_to_hex(list
->item
->object
.sha1
));