9 #include "gpg-interface.h"
10 #include "mergesort.h"
11 #include "commit-slab.h"
12 #include "prio-queue.h"
13 #include "sha1-lookup.h"
15 static struct commit_extra_header
*read_commit_extra_header_lines(const char *buf
, size_t len
, const char **);
17 int save_commit_buffer
= 1;
19 const char *commit_type
= "commit";
20 static int commit_count
;
22 static struct commit
*check_commit(struct object
*obj
,
23 const unsigned char *sha1
,
26 if (obj
->type
!= OBJ_COMMIT
) {
28 error("Object %s is a %s, not a commit",
29 sha1_to_hex(sha1
), typename(obj
->type
));
32 return (struct commit
*) obj
;
35 struct commit
*lookup_commit_reference_gently(const unsigned char *sha1
,
38 struct object
*obj
= deref_tag(parse_object(sha1
), NULL
, 0);
42 return check_commit(obj
, sha1
, quiet
);
45 struct commit
*lookup_commit_reference(const unsigned char *sha1
)
47 return lookup_commit_reference_gently(sha1
, 0);
50 struct commit
*lookup_commit_or_die(const unsigned char *sha1
, const char *ref_name
)
52 struct commit
*c
= lookup_commit_reference(sha1
);
54 die(_("could not parse %s"), ref_name
);
55 if (hashcmp(sha1
, c
->object
.sha1
)) {
56 warning(_("%s %s is not a commit!"),
57 ref_name
, sha1_to_hex(sha1
));
62 struct commit
*lookup_commit(const unsigned char *sha1
)
64 struct object
*obj
= lookup_object(sha1
);
66 struct commit
*c
= alloc_commit_node();
67 c
->index
= commit_count
++;
68 return create_object(sha1
, OBJ_COMMIT
, c
);
71 obj
->type
= OBJ_COMMIT
;
72 return check_commit(obj
, sha1
, 0);
75 struct commit
*lookup_commit_reference_by_name(const char *name
)
77 unsigned char sha1
[20];
78 struct commit
*commit
;
80 if (get_sha1_committish(name
, sha1
))
82 commit
= lookup_commit_reference(sha1
);
83 if (parse_commit(commit
))
88 static unsigned long parse_commit_date(const char *buf
, const char *tail
)
94 if (memcmp(buf
, "author", 6))
96 while (buf
< tail
&& *buf
++ != '\n')
100 if (memcmp(buf
, "committer", 9))
102 while (buf
< tail
&& *buf
++ != '>')
107 while (buf
< tail
&& *buf
++ != '\n')
111 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
112 return strtoul(dateptr
, NULL
, 10);
115 static struct commit_graft
**commit_graft
;
116 static int commit_graft_alloc
, commit_graft_nr
;
118 static const unsigned char *commit_graft_sha1_access(size_t index
, void *table
)
120 struct commit_graft
**commit_graft_table
= table
;
121 return commit_graft_table
[index
]->sha1
;
124 static int commit_graft_pos(const unsigned char *sha1
)
126 return sha1_pos(sha1
, commit_graft
, commit_graft_nr
,
127 commit_graft_sha1_access
);
130 int register_commit_graft(struct commit_graft
*graft
, int ignore_dups
)
132 int pos
= commit_graft_pos(graft
->sha1
);
138 free(commit_graft
[pos
]);
139 commit_graft
[pos
] = graft
;
144 if (commit_graft_alloc
<= ++commit_graft_nr
) {
145 commit_graft_alloc
= alloc_nr(commit_graft_alloc
);
146 commit_graft
= xrealloc(commit_graft
,
147 sizeof(*commit_graft
) *
150 if (pos
< commit_graft_nr
)
151 memmove(commit_graft
+ pos
+ 1,
153 (commit_graft_nr
- pos
- 1) *
154 sizeof(*commit_graft
));
155 commit_graft
[pos
] = graft
;
159 struct commit_graft
*read_graft_line(char *buf
, int len
)
161 /* The format is just "Commit Parent1 Parent2 ...\n" */
163 struct commit_graft
*graft
= NULL
;
165 while (len
&& isspace(buf
[len
-1]))
167 if (buf
[0] == '#' || buf
[0] == '\0')
171 i
= (len
+ 1) / 41 - 1;
172 graft
= xmalloc(sizeof(*graft
) + 20 * i
);
173 graft
->nr_parent
= i
;
174 if (get_sha1_hex(buf
, graft
->sha1
))
176 for (i
= 40; i
< len
; i
+= 41) {
179 if (get_sha1_hex(buf
+ i
+ 1, graft
->parent
[i
/41]))
185 error("bad graft data: %s", buf
);
190 static int read_graft_file(const char *graft_file
)
192 FILE *fp
= fopen(graft_file
, "r");
193 struct strbuf buf
= STRBUF_INIT
;
196 while (!strbuf_getwholeline(&buf
, fp
, '\n')) {
197 /* The format is just "Commit Parent1 Parent2 ...\n" */
198 struct commit_graft
*graft
= read_graft_line(buf
.buf
, buf
.len
);
201 if (register_commit_graft(graft
, 1))
202 error("duplicate graft data: %s", buf
.buf
);
205 strbuf_release(&buf
);
209 static void prepare_commit_graft(void)
211 static int commit_graft_prepared
;
214 if (commit_graft_prepared
)
216 graft_file
= get_graft_file();
217 read_graft_file(graft_file
);
218 /* make sure shallows are read */
219 is_repository_shallow();
220 commit_graft_prepared
= 1;
223 struct commit_graft
*lookup_commit_graft(const unsigned char *sha1
)
226 prepare_commit_graft();
227 pos
= commit_graft_pos(sha1
);
230 return commit_graft
[pos
];
233 int for_each_commit_graft(each_commit_graft_fn fn
, void *cb_data
)
236 for (i
= ret
= 0; i
< commit_graft_nr
&& !ret
; i
++)
237 ret
= fn(commit_graft
[i
], cb_data
);
241 int unregister_shallow(const unsigned char *sha1
)
243 int pos
= commit_graft_pos(sha1
);
246 if (pos
+ 1 < commit_graft_nr
)
247 memmove(commit_graft
+ pos
, commit_graft
+ pos
+ 1,
248 sizeof(struct commit_graft
*)
249 * (commit_graft_nr
- pos
- 1));
254 int parse_commit_buffer(struct commit
*item
, const void *buffer
, unsigned long size
)
256 const char *tail
= buffer
;
257 const char *bufptr
= buffer
;
258 unsigned char parent
[20];
259 struct commit_list
**pptr
;
260 struct commit_graft
*graft
;
262 if (item
->object
.parsed
)
264 item
->object
.parsed
= 1;
266 if (tail
<= bufptr
+ 46 || memcmp(bufptr
, "tree ", 5) || bufptr
[45] != '\n')
267 return error("bogus commit object %s", sha1_to_hex(item
->object
.sha1
));
268 if (get_sha1_hex(bufptr
+ 5, parent
) < 0)
269 return error("bad tree pointer in commit %s",
270 sha1_to_hex(item
->object
.sha1
));
271 item
->tree
= lookup_tree(parent
);
272 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
273 pptr
= &item
->parents
;
275 graft
= lookup_commit_graft(item
->object
.sha1
);
276 while (bufptr
+ 48 < tail
&& !memcmp(bufptr
, "parent ", 7)) {
277 struct commit
*new_parent
;
279 if (tail
<= bufptr
+ 48 ||
280 get_sha1_hex(bufptr
+ 7, parent
) ||
282 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
285 * The clone is shallow if nr_parent < 0, and we must
286 * not traverse its real parents even when we unhide them.
288 if (graft
&& (graft
->nr_parent
< 0 || grafts_replace_parents
))
290 new_parent
= lookup_commit(parent
);
292 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
296 struct commit
*new_parent
;
297 for (i
= 0; i
< graft
->nr_parent
; i
++) {
298 new_parent
= lookup_commit(graft
->parent
[i
]);
301 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
304 item
->date
= parse_commit_date(bufptr
, tail
);
309 int parse_commit(struct commit
*item
)
311 enum object_type type
;
318 if (item
->object
.parsed
)
320 buffer
= read_sha1_file(item
->object
.sha1
, &type
, &size
);
322 return error("Could not read %s",
323 sha1_to_hex(item
->object
.sha1
));
324 if (type
!= OBJ_COMMIT
) {
326 return error("Object %s not a commit",
327 sha1_to_hex(item
->object
.sha1
));
329 ret
= parse_commit_buffer(item
, buffer
, size
);
330 if (save_commit_buffer
&& !ret
) {
331 item
->buffer
= buffer
;
338 void parse_commit_or_die(struct commit
*item
)
340 if (parse_commit(item
))
341 die("unable to parse commit %s",
342 item
? sha1_to_hex(item
->object
.sha1
) : "(null)");
345 int find_commit_subject(const char *commit_buffer
, const char **subject
)
348 const char *p
= commit_buffer
;
350 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
354 for (eol
= p
; *eol
&& *eol
!= '\n'; eol
++)
364 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
366 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
367 new_list
->item
= item
;
368 new_list
->next
= *list_p
;
373 unsigned commit_list_count(const struct commit_list
*l
)
376 for (; l
; l
= l
->next
)
381 struct commit_list
*copy_commit_list(struct commit_list
*list
)
383 struct commit_list
*head
= NULL
;
384 struct commit_list
**pp
= &head
;
386 struct commit_list
*new;
387 new = xmalloc(sizeof(struct commit_list
));
388 new->item
= list
->item
;
397 void free_commit_list(struct commit_list
*list
)
400 struct commit_list
*temp
= list
;
406 struct commit_list
* commit_list_insert_by_date(struct commit
*item
, struct commit_list
**list
)
408 struct commit_list
**pp
= list
;
409 struct commit_list
*p
;
410 while ((p
= *pp
) != NULL
) {
411 if (p
->item
->date
< item
->date
) {
416 return commit_list_insert(item
, pp
);
419 static int commit_list_compare_by_date(const void *a
, const void *b
)
421 unsigned long a_date
= ((const struct commit_list
*)a
)->item
->date
;
422 unsigned long b_date
= ((const struct commit_list
*)b
)->item
->date
;
430 static void *commit_list_get_next(const void *a
)
432 return ((const struct commit_list
*)a
)->next
;
435 static void commit_list_set_next(void *a
, void *next
)
437 ((struct commit_list
*)a
)->next
= next
;
440 void commit_list_sort_by_date(struct commit_list
**list
)
442 *list
= llist_mergesort(*list
, commit_list_get_next
, commit_list_set_next
,
443 commit_list_compare_by_date
);
446 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
449 struct commit
*ret
= (*list
)->item
;
450 struct commit_list
*parents
= ret
->parents
;
451 struct commit_list
*old
= *list
;
453 *list
= (*list
)->next
;
457 struct commit
*commit
= parents
->item
;
458 if (!parse_commit(commit
) && !(commit
->object
.flags
& mark
)) {
459 commit
->object
.flags
|= mark
;
460 commit_list_insert_by_date(commit
, list
);
462 parents
= parents
->next
;
467 static void clear_commit_marks_1(struct commit_list
**plist
,
468 struct commit
*commit
, unsigned int mark
)
471 struct commit_list
*parents
;
473 if (!(mark
& commit
->object
.flags
))
476 commit
->object
.flags
&= ~mark
;
478 parents
= commit
->parents
;
482 while ((parents
= parents
->next
))
483 commit_list_insert(parents
->item
, plist
);
485 commit
= commit
->parents
->item
;
489 void clear_commit_marks_many(int nr
, struct commit
**commit
, unsigned int mark
)
491 struct commit_list
*list
= NULL
;
494 commit_list_insert(*commit
, &list
);
498 clear_commit_marks_1(&list
, pop_commit(&list
), mark
);
501 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
503 clear_commit_marks_many(1, &commit
, mark
);
506 void clear_commit_marks_for_object_array(struct object_array
*a
, unsigned mark
)
508 struct object
*object
;
509 struct commit
*commit
;
512 for (i
= 0; i
< a
->nr
; i
++) {
513 object
= a
->objects
[i
].item
;
514 commit
= lookup_commit_reference_gently(object
->sha1
, 1);
516 clear_commit_marks(commit
, mark
);
520 struct commit
*pop_commit(struct commit_list
**stack
)
522 struct commit_list
*top
= *stack
;
523 struct commit
*item
= top
? top
->item
: NULL
;
533 * Topological sort support
536 /* count number of children that have not been emitted */
537 define_commit_slab(indegree_slab
, int);
539 /* record author-date for each commit object */
540 define_commit_slab(author_date_slab
, unsigned long);
542 static void record_author_date(struct author_date_slab
*author_date
,
543 struct commit
*commit
)
545 const char *buf
, *line_end
;
547 struct ident_split ident
;
551 if (!commit
->buffer
) {
553 enum object_type type
;
554 buffer
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
559 for (buf
= commit
->buffer
? commit
->buffer
: buffer
;
561 buf
= line_end
+ 1) {
562 line_end
= strchrnul(buf
, '\n');
563 if (!starts_with(buf
, "author ")) {
564 if (!line_end
[0] || line_end
[1] == '\n')
565 return; /* end of header */
568 if (split_ident_line(&ident
,
569 buf
+ strlen("author "),
570 line_end
- (buf
+ strlen("author "))) ||
571 !ident
.date_begin
|| !ident
.date_end
)
572 goto fail_exit
; /* malformed "author" line */
576 date
= strtoul(ident
.date_begin
, &date_end
, 10);
577 if (date_end
!= ident
.date_end
)
578 goto fail_exit
; /* malformed date */
579 *(author_date_slab_at(author_date
, commit
)) = date
;
585 static int compare_commits_by_author_date(const void *a_
, const void *b_
,
588 const struct commit
*a
= a_
, *b
= b_
;
589 struct author_date_slab
*author_date
= cb_data
;
590 unsigned long a_date
= *(author_date_slab_at(author_date
, a
));
591 unsigned long b_date
= *(author_date_slab_at(author_date
, b
));
593 /* newer commits with larger date first */
596 else if (a_date
> b_date
)
601 int compare_commits_by_commit_date(const void *a_
, const void *b_
, void *unused
)
603 const struct commit
*a
= a_
, *b
= b_
;
604 /* newer commits with larger date first */
605 if (a
->date
< b
->date
)
607 else if (a
->date
> b
->date
)
613 * Performs an in-place topological sort on the list supplied.
615 void sort_in_topological_order(struct commit_list
**list
, enum rev_sort_order sort_order
)
617 struct commit_list
*next
, *orig
= *list
;
618 struct commit_list
**pptr
;
619 struct indegree_slab indegree
;
620 struct prio_queue queue
;
621 struct commit
*commit
;
622 struct author_date_slab author_date
;
628 init_indegree_slab(&indegree
);
629 memset(&queue
, '\0', sizeof(queue
));
631 switch (sort_order
) {
632 default: /* REV_SORT_IN_GRAPH_ORDER */
633 queue
.compare
= NULL
;
635 case REV_SORT_BY_COMMIT_DATE
:
636 queue
.compare
= compare_commits_by_commit_date
;
638 case REV_SORT_BY_AUTHOR_DATE
:
639 init_author_date_slab(&author_date
);
640 queue
.compare
= compare_commits_by_author_date
;
641 queue
.cb_data
= &author_date
;
645 /* Mark them and clear the indegree */
646 for (next
= orig
; next
; next
= next
->next
) {
647 struct commit
*commit
= next
->item
;
648 *(indegree_slab_at(&indegree
, commit
)) = 1;
649 /* also record the author dates, if needed */
650 if (sort_order
== REV_SORT_BY_AUTHOR_DATE
)
651 record_author_date(&author_date
, commit
);
654 /* update the indegree */
655 for (next
= orig
; next
; next
= next
->next
) {
656 struct commit_list
*parents
= next
->item
->parents
;
658 struct commit
*parent
= parents
->item
;
659 int *pi
= indegree_slab_at(&indegree
, parent
);
663 parents
= parents
->next
;
670 * tips are nodes not reachable from any other node in the list
672 * the tips serve as a starting set for the work queue.
674 for (next
= orig
; next
; next
= next
->next
) {
675 struct commit
*commit
= next
->item
;
677 if (*(indegree_slab_at(&indegree
, commit
)) == 1)
678 prio_queue_put(&queue
, commit
);
682 * This is unfortunate; the initial tips need to be shown
683 * in the order given from the revision traversal machinery.
685 if (sort_order
== REV_SORT_IN_GRAPH_ORDER
)
686 prio_queue_reverse(&queue
);
688 /* We no longer need the commit list */
689 free_commit_list(orig
);
693 while ((commit
= prio_queue_get(&queue
)) != NULL
) {
694 struct commit_list
*parents
;
696 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
697 struct commit
*parent
= parents
->item
;
698 int *pi
= indegree_slab_at(&indegree
, parent
);
704 * parents are only enqueued for emission
705 * when all their children have been emitted thereby
706 * guaranteeing topological order.
709 prio_queue_put(&queue
, parent
);
712 * all children of commit have already been
713 * emitted. we can emit it now.
715 *(indegree_slab_at(&indegree
, commit
)) = 0;
717 pptr
= &commit_list_insert(commit
, pptr
)->next
;
720 clear_indegree_slab(&indegree
);
721 clear_prio_queue(&queue
);
722 if (sort_order
== REV_SORT_BY_AUTHOR_DATE
)
723 clear_author_date_slab(&author_date
);
726 /* merge-base stuff */
728 /* bits #0..15 in revision.h */
729 #define PARENT1 (1u<<16)
730 #define PARENT2 (1u<<17)
731 #define STALE (1u<<18)
732 #define RESULT (1u<<19)
734 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
736 static struct commit
*interesting(struct commit_list
*list
)
739 struct commit
*commit
= list
->item
;
741 if (commit
->object
.flags
& STALE
)
748 /* all input commits in one and twos[] must have been parsed! */
749 static struct commit_list
*paint_down_to_common(struct commit
*one
, int n
, struct commit
**twos
)
751 struct commit_list
*list
= NULL
;
752 struct commit_list
*result
= NULL
;
755 one
->object
.flags
|= PARENT1
;
756 commit_list_insert_by_date(one
, &list
);
759 for (i
= 0; i
< n
; i
++) {
760 twos
[i
]->object
.flags
|= PARENT2
;
761 commit_list_insert_by_date(twos
[i
], &list
);
764 while (interesting(list
)) {
765 struct commit
*commit
;
766 struct commit_list
*parents
;
767 struct commit_list
*next
;
775 flags
= commit
->object
.flags
& (PARENT1
| PARENT2
| STALE
);
776 if (flags
== (PARENT1
| PARENT2
)) {
777 if (!(commit
->object
.flags
& RESULT
)) {
778 commit
->object
.flags
|= RESULT
;
779 commit_list_insert_by_date(commit
, &result
);
781 /* Mark parents of a found merge stale */
784 parents
= commit
->parents
;
786 struct commit
*p
= parents
->item
;
787 parents
= parents
->next
;
788 if ((p
->object
.flags
& flags
) == flags
)
792 p
->object
.flags
|= flags
;
793 commit_list_insert_by_date(p
, &list
);
797 free_commit_list(list
);
801 static struct commit_list
*merge_bases_many(struct commit
*one
, int n
, struct commit
**twos
)
803 struct commit_list
*list
= NULL
;
804 struct commit_list
*result
= NULL
;
807 for (i
= 0; i
< n
; i
++) {
810 * We do not mark this even with RESULT so we do not
811 * have to clean it up.
813 return commit_list_insert(one
, &result
);
816 if (parse_commit(one
))
818 for (i
= 0; i
< n
; i
++) {
819 if (parse_commit(twos
[i
]))
823 list
= paint_down_to_common(one
, n
, twos
);
826 struct commit_list
*next
= list
->next
;
827 if (!(list
->item
->object
.flags
& STALE
))
828 commit_list_insert_by_date(list
->item
, &result
);
835 struct commit_list
*get_octopus_merge_bases(struct commit_list
*in
)
837 struct commit_list
*i
, *j
, *k
, *ret
= NULL
;
842 commit_list_insert(in
->item
, &ret
);
844 for (i
= in
->next
; i
; i
= i
->next
) {
845 struct commit_list
*new = NULL
, *end
= NULL
;
847 for (j
= ret
; j
; j
= j
->next
) {
848 struct commit_list
*bases
;
849 bases
= get_merge_bases(i
->item
, j
->item
, 1);
854 for (k
= bases
; k
; k
= k
->next
)
862 static int remove_redundant(struct commit
**array
, int cnt
)
865 * Some commit in the array may be an ancestor of
866 * another commit. Move such commit to the end of
867 * the array, and return the number of commits that
868 * are independent from each other.
870 struct commit
**work
;
871 unsigned char *redundant
;
875 work
= xcalloc(cnt
, sizeof(*work
));
876 redundant
= xcalloc(cnt
, 1);
877 filled_index
= xmalloc(sizeof(*filled_index
) * (cnt
- 1));
879 for (i
= 0; i
< cnt
; i
++)
880 parse_commit(array
[i
]);
881 for (i
= 0; i
< cnt
; i
++) {
882 struct commit_list
*common
;
886 for (j
= filled
= 0; j
< cnt
; j
++) {
887 if (i
== j
|| redundant
[j
])
889 filled_index
[filled
] = j
;
890 work
[filled
++] = array
[j
];
892 common
= paint_down_to_common(array
[i
], filled
, work
);
893 if (array
[i
]->object
.flags
& PARENT2
)
895 for (j
= 0; j
< filled
; j
++)
896 if (work
[j
]->object
.flags
& PARENT1
)
897 redundant
[filled_index
[j
]] = 1;
898 clear_commit_marks(array
[i
], all_flags
);
899 for (j
= 0; j
< filled
; j
++)
900 clear_commit_marks(work
[j
], all_flags
);
901 free_commit_list(common
);
904 /* Now collect the result */
905 memcpy(work
, array
, sizeof(*array
) * cnt
);
906 for (i
= filled
= 0; i
< cnt
; i
++)
908 array
[filled
++] = work
[i
];
909 for (j
= filled
, i
= 0; i
< cnt
; i
++)
911 array
[j
++] = work
[i
];
918 struct commit_list
*get_merge_bases_many(struct commit
*one
,
920 struct commit
**twos
,
923 struct commit_list
*list
;
924 struct commit
**rslt
;
925 struct commit_list
*result
;
928 result
= merge_bases_many(one
, n
, twos
);
929 for (i
= 0; i
< n
; i
++) {
933 if (!result
|| !result
->next
) {
935 clear_commit_marks(one
, all_flags
);
936 clear_commit_marks_many(n
, twos
, all_flags
);
941 /* There are more than one */
948 rslt
= xcalloc(cnt
, sizeof(*rslt
));
949 for (list
= result
, i
= 0; list
; list
= list
->next
)
950 rslt
[i
++] = list
->item
;
951 free_commit_list(result
);
953 clear_commit_marks(one
, all_flags
);
954 clear_commit_marks_many(n
, twos
, all_flags
);
956 cnt
= remove_redundant(rslt
, cnt
);
958 for (i
= 0; i
< cnt
; i
++)
959 commit_list_insert_by_date(rslt
[i
], &result
);
964 struct commit_list
*get_merge_bases(struct commit
*one
, struct commit
*two
,
967 return get_merge_bases_many(one
, 1, &two
, cleanup
);
971 * Is "commit" a descendant of one of the elements on the "with_commit" list?
973 int is_descendant_of(struct commit
*commit
, struct commit_list
*with_commit
)
977 while (with_commit
) {
978 struct commit
*other
;
980 other
= with_commit
->item
;
981 with_commit
= with_commit
->next
;
982 if (in_merge_bases(other
, commit
))
989 * Is "commit" an ancestor of one of the "references"?
991 int in_merge_bases_many(struct commit
*commit
, int nr_reference
, struct commit
**reference
)
993 struct commit_list
*bases
;
996 if (parse_commit(commit
))
998 for (i
= 0; i
< nr_reference
; i
++)
999 if (parse_commit(reference
[i
]))
1002 bases
= paint_down_to_common(commit
, nr_reference
, reference
);
1003 if (commit
->object
.flags
& PARENT2
)
1005 clear_commit_marks(commit
, all_flags
);
1006 clear_commit_marks_many(nr_reference
, reference
, all_flags
);
1007 free_commit_list(bases
);
1012 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
1014 int in_merge_bases(struct commit
*commit
, struct commit
*reference
)
1016 return in_merge_bases_many(commit
, 1, &reference
);
1019 struct commit_list
*reduce_heads(struct commit_list
*heads
)
1021 struct commit_list
*p
;
1022 struct commit_list
*result
= NULL
, **tail
= &result
;
1023 struct commit
**array
;
1030 for (p
= heads
; p
; p
= p
->next
)
1031 p
->item
->object
.flags
&= ~STALE
;
1032 for (p
= heads
, num_head
= 0; p
; p
= p
->next
) {
1033 if (p
->item
->object
.flags
& STALE
)
1035 p
->item
->object
.flags
|= STALE
;
1038 array
= xcalloc(sizeof(*array
), num_head
);
1039 for (p
= heads
, i
= 0; p
; p
= p
->next
) {
1040 if (p
->item
->object
.flags
& STALE
) {
1041 array
[i
++] = p
->item
;
1042 p
->item
->object
.flags
&= ~STALE
;
1045 num_head
= remove_redundant(array
, num_head
);
1046 for (i
= 0; i
< num_head
; i
++)
1047 tail
= &commit_list_insert(array
[i
], tail
)->next
;
1051 static const char gpg_sig_header
[] = "gpgsig";
1052 static const int gpg_sig_header_len
= sizeof(gpg_sig_header
) - 1;
1054 static int do_sign_commit(struct strbuf
*buf
, const char *keyid
)
1056 struct strbuf sig
= STRBUF_INIT
;
1057 int inspos
, copypos
;
1059 /* find the end of the header */
1060 inspos
= strstr(buf
->buf
, "\n\n") - buf
->buf
+ 1;
1062 if (!keyid
|| !*keyid
)
1063 keyid
= get_signing_key();
1064 if (sign_buffer(buf
, &sig
, keyid
)) {
1065 strbuf_release(&sig
);
1069 for (copypos
= 0; sig
.buf
[copypos
]; ) {
1070 const char *bol
= sig
.buf
+ copypos
;
1071 const char *eol
= strchrnul(bol
, '\n');
1072 int len
= (eol
- bol
) + !!*eol
;
1075 strbuf_insert(buf
, inspos
, gpg_sig_header
, gpg_sig_header_len
);
1076 inspos
+= gpg_sig_header_len
;
1078 strbuf_insert(buf
, inspos
++, " ", 1);
1079 strbuf_insert(buf
, inspos
, bol
, len
);
1083 strbuf_release(&sig
);
1087 int parse_signed_commit(const unsigned char *sha1
,
1088 struct strbuf
*payload
, struct strbuf
*signature
)
1091 enum object_type type
;
1092 char *buffer
= read_sha1_file(sha1
, &type
, &size
);
1093 int in_signature
, saw_signature
= -1;
1096 if (!buffer
|| type
!= OBJ_COMMIT
)
1100 tail
= buffer
+ size
;
1103 while (line
< tail
) {
1104 const char *sig
= NULL
;
1105 char *next
= memchr(line
, '\n', tail
- line
);
1107 next
= next
? next
+ 1 : tail
;
1108 if (in_signature
&& line
[0] == ' ')
1110 else if (starts_with(line
, gpg_sig_header
) &&
1111 line
[gpg_sig_header_len
] == ' ')
1112 sig
= line
+ gpg_sig_header_len
+ 1;
1114 strbuf_add(signature
, sig
, next
- sig
);
1119 /* dump the whole remainder of the buffer */
1121 strbuf_add(payload
, line
, next
- line
);
1128 return saw_signature
;
1131 static void handle_signed_tag(struct commit
*parent
, struct commit_extra_header
***tail
)
1133 struct merge_remote_desc
*desc
;
1134 struct commit_extra_header
*mergetag
;
1136 unsigned long size
, len
;
1137 enum object_type type
;
1139 desc
= merge_remote_util(parent
);
1140 if (!desc
|| !desc
->obj
)
1142 buf
= read_sha1_file(desc
->obj
->sha1
, &type
, &size
);
1143 if (!buf
|| type
!= OBJ_TAG
)
1145 len
= parse_signature(buf
, size
);
1149 * We could verify this signature and either omit the tag when
1150 * it does not validate, but the integrator may not have the
1151 * public key of the signer of the tag he is merging, while a
1152 * later auditor may have it while auditing, so let's not run
1153 * verify-signed-buffer here for now...
1155 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1156 * warn("warning: signed tag unverified.");
1158 mergetag
= xcalloc(1, sizeof(*mergetag
));
1159 mergetag
->key
= xstrdup("mergetag");
1160 mergetag
->value
= buf
;
1161 mergetag
->len
= size
;
1164 *tail
= &mergetag
->next
;
1174 } sigcheck_gpg_status
[] = {
1175 { 'G', "\n[GNUPG:] GOODSIG " },
1176 { 'B', "\n[GNUPG:] BADSIG " },
1177 { 'U', "\n[GNUPG:] TRUST_NEVER" },
1178 { 'U', "\n[GNUPG:] TRUST_UNDEFINED" },
1181 static void parse_gpg_output(struct signature_check
*sigc
)
1183 const char *buf
= sigc
->gpg_status
;
1186 /* Iterate over all search strings */
1187 for (i
= 0; i
< ARRAY_SIZE(sigcheck_gpg_status
); i
++) {
1188 const char *found
, *next
;
1190 if (starts_with(buf
, sigcheck_gpg_status
[i
].check
+ 1)) {
1191 /* At the very beginning of the buffer */
1192 found
= buf
+ strlen(sigcheck_gpg_status
[i
].check
+ 1);
1194 found
= strstr(buf
, sigcheck_gpg_status
[i
].check
);
1197 found
+= strlen(sigcheck_gpg_status
[i
].check
);
1199 sigc
->result
= sigcheck_gpg_status
[i
].result
;
1200 /* The trust messages are not followed by key/signer information */
1201 if (sigc
->result
!= 'U') {
1202 sigc
->key
= xmemdupz(found
, 16);
1204 next
= strchrnul(found
, '\n');
1205 sigc
->signer
= xmemdupz(found
, next
- found
);
1210 void check_commit_signature(const struct commit
* commit
, struct signature_check
*sigc
)
1212 struct strbuf payload
= STRBUF_INIT
;
1213 struct strbuf signature
= STRBUF_INIT
;
1214 struct strbuf gpg_output
= STRBUF_INIT
;
1215 struct strbuf gpg_status
= STRBUF_INIT
;
1220 if (parse_signed_commit(commit
->object
.sha1
,
1221 &payload
, &signature
) <= 0)
1223 status
= verify_signed_buffer(payload
.buf
, payload
.len
,
1224 signature
.buf
, signature
.len
,
1225 &gpg_output
, &gpg_status
);
1226 if (status
&& !gpg_output
.len
)
1228 sigc
->gpg_output
= strbuf_detach(&gpg_output
, NULL
);
1229 sigc
->gpg_status
= strbuf_detach(&gpg_status
, NULL
);
1230 parse_gpg_output(sigc
);
1233 strbuf_release(&gpg_status
);
1234 strbuf_release(&gpg_output
);
1235 strbuf_release(&payload
);
1236 strbuf_release(&signature
);
1241 void append_merge_tag_headers(struct commit_list
*parents
,
1242 struct commit_extra_header
***tail
)
1245 struct commit
*parent
= parents
->item
;
1246 handle_signed_tag(parent
, tail
);
1247 parents
= parents
->next
;
1251 static void add_extra_header(struct strbuf
*buffer
,
1252 struct commit_extra_header
*extra
)
1254 strbuf_addstr(buffer
, extra
->key
);
1256 strbuf_add_lines(buffer
, " ", extra
->value
, extra
->len
);
1258 strbuf_addch(buffer
, '\n');
1261 struct commit_extra_header
*read_commit_extra_headers(struct commit
*commit
,
1262 const char **exclude
)
1264 struct commit_extra_header
*extra
= NULL
;
1266 enum object_type type
;
1267 char *buffer
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
1268 if (buffer
&& type
== OBJ_COMMIT
)
1269 extra
= read_commit_extra_header_lines(buffer
, size
, exclude
);
1274 static inline int standard_header_field(const char *field
, size_t len
)
1276 return ((len
== 4 && !memcmp(field
, "tree ", 5)) ||
1277 (len
== 6 && !memcmp(field
, "parent ", 7)) ||
1278 (len
== 6 && !memcmp(field
, "author ", 7)) ||
1279 (len
== 9 && !memcmp(field
, "committer ", 10)) ||
1280 (len
== 8 && !memcmp(field
, "encoding ", 9)));
1283 static int excluded_header_field(const char *field
, size_t len
, const char **exclude
)
1289 size_t xlen
= strlen(*exclude
);
1291 !memcmp(field
, *exclude
, xlen
) && field
[xlen
] == ' ')
1298 static struct commit_extra_header
*read_commit_extra_header_lines(
1299 const char *buffer
, size_t size
,
1300 const char **exclude
)
1302 struct commit_extra_header
*extra
= NULL
, **tail
= &extra
, *it
= NULL
;
1303 const char *line
, *next
, *eof
, *eob
;
1304 struct strbuf buf
= STRBUF_INIT
;
1306 for (line
= buffer
, eob
= line
+ size
;
1307 line
< eob
&& *line
!= '\n';
1309 next
= memchr(line
, '\n', eob
- line
);
1310 next
= next
? next
+ 1 : eob
;
1314 strbuf_add(&buf
, line
+ 1, next
- (line
+ 1));
1318 it
->value
= strbuf_detach(&buf
, &it
->len
);
1322 eof
= strchr(line
, ' ');
1326 if (standard_header_field(line
, eof
- line
) ||
1327 excluded_header_field(line
, eof
- line
, exclude
))
1330 it
= xcalloc(1, sizeof(*it
));
1331 it
->key
= xmemdupz(line
, eof
-line
);
1335 strbuf_add(&buf
, eof
+ 1, next
- (eof
+ 1));
1338 it
->value
= strbuf_detach(&buf
, &it
->len
);
1342 void free_commit_extra_headers(struct commit_extra_header
*extra
)
1345 struct commit_extra_header
*next
= extra
->next
;
1353 int commit_tree(const struct strbuf
*msg
, const unsigned char *tree
,
1354 struct commit_list
*parents
, unsigned char *ret
,
1355 const char *author
, const char *sign_commit
)
1357 struct commit_extra_header
*extra
= NULL
, **tail
= &extra
;
1360 append_merge_tag_headers(parents
, &tail
);
1361 result
= commit_tree_extended(msg
, tree
, parents
, ret
,
1362 author
, sign_commit
, extra
);
1363 free_commit_extra_headers(extra
);
1367 static int find_invalid_utf8(const char *buf
, int len
)
1370 static const unsigned int max_codepoint
[] = {
1371 0x7f, 0x7ff, 0xffff, 0x10ffff
1375 unsigned char c
= *buf
++;
1376 int bytes
, bad_offset
;
1377 unsigned int codepoint
;
1378 unsigned int min_val
, max_val
;
1383 /* Simple US-ASCII? No worries. */
1387 bad_offset
= offset
-1;
1390 * Count how many more high bits set: that's how
1391 * many more bytes this sequence should have.
1400 * Must be between 1 and 3 more bytes. Longer sequences result in
1401 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1403 if (bytes
< 1 || 3 < bytes
)
1406 /* Do we *have* that many bytes? */
1411 * Place the encoded bits at the bottom of the value and compute the
1414 codepoint
= (c
& 0x7f) >> bytes
;
1415 min_val
= max_codepoint
[bytes
-1] + 1;
1416 max_val
= max_codepoint
[bytes
];
1421 /* And verify that they are good continuation bytes */
1424 codepoint
|= *buf
& 0x3f;
1425 if ((*buf
++ & 0xc0) != 0x80)
1429 /* Reject codepoints that are out of range for the sequence length. */
1430 if (codepoint
< min_val
|| codepoint
> max_val
)
1432 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1433 if ((codepoint
& 0x1ff800) == 0xd800)
1435 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1436 if ((codepoint
& 0xfffe) == 0xfffe)
1438 /* So are anything in the range U+FDD0..U+FDEF. */
1439 if (codepoint
>= 0xfdd0 && codepoint
<= 0xfdef)
1446 * This verifies that the buffer is in proper utf8 format.
1448 * If it isn't, it assumes any non-utf8 characters are Latin1,
1449 * and does the conversion.
1451 static int verify_utf8(struct strbuf
*buf
)
1459 unsigned char replace
[2];
1461 bad
= find_invalid_utf8(buf
->buf
+ pos
, buf
->len
- pos
);
1467 strbuf_remove(buf
, pos
, 1);
1469 /* We know 'c' must be in the range 128-255 */
1470 replace
[0] = 0xc0 + (c
>> 6);
1471 replace
[1] = 0x80 + (c
& 0x3f);
1472 strbuf_insert(buf
, pos
, replace
, 2);
1477 static const char commit_utf8_warn
[] =
1478 "Warning: commit message did not conform to UTF-8.\n"
1479 "You may want to amend it after fixing the message, or set the config\n"
1480 "variable i18n.commitencoding to the encoding your project uses.\n";
1482 int commit_tree_extended(const struct strbuf
*msg
, const unsigned char *tree
,
1483 struct commit_list
*parents
, unsigned char *ret
,
1484 const char *author
, const char *sign_commit
,
1485 struct commit_extra_header
*extra
)
1488 int encoding_is_utf8
;
1489 struct strbuf buffer
;
1491 assert_sha1_type(tree
, OBJ_TREE
);
1493 if (memchr(msg
->buf
, '\0', msg
->len
))
1494 return error("a NUL byte in commit log message not allowed.");
1496 /* Not having i18n.commitencoding is the same as having utf-8 */
1497 encoding_is_utf8
= is_encoding_utf8(git_commit_encoding
);
1499 strbuf_init(&buffer
, 8192); /* should avoid reallocs for the headers */
1500 strbuf_addf(&buffer
, "tree %s\n", sha1_to_hex(tree
));
1503 * NOTE! This ordering means that the same exact tree merged with a
1504 * different order of parents will be a _different_ changeset even
1505 * if everything else stays the same.
1508 struct commit_list
*next
= parents
->next
;
1509 struct commit
*parent
= parents
->item
;
1511 strbuf_addf(&buffer
, "parent %s\n",
1512 sha1_to_hex(parent
->object
.sha1
));
1517 /* Person/date information */
1519 author
= git_author_info(IDENT_STRICT
);
1520 strbuf_addf(&buffer
, "author %s\n", author
);
1521 strbuf_addf(&buffer
, "committer %s\n", git_committer_info(IDENT_STRICT
));
1522 if (!encoding_is_utf8
)
1523 strbuf_addf(&buffer
, "encoding %s\n", git_commit_encoding
);
1526 add_extra_header(&buffer
, extra
);
1527 extra
= extra
->next
;
1529 strbuf_addch(&buffer
, '\n');
1531 /* And add the comment */
1532 strbuf_addbuf(&buffer
, msg
);
1534 /* And check the encoding */
1535 if (encoding_is_utf8
&& !verify_utf8(&buffer
))
1536 fprintf(stderr
, commit_utf8_warn
);
1538 if (sign_commit
&& do_sign_commit(&buffer
, sign_commit
))
1541 result
= write_sha1_file(buffer
.buf
, buffer
.len
, commit_type
, ret
);
1542 strbuf_release(&buffer
);
1546 struct commit
*get_merge_parent(const char *name
)
1549 struct commit
*commit
;
1550 unsigned char sha1
[20];
1551 if (get_sha1(name
, sha1
))
1553 obj
= parse_object(sha1
);
1554 commit
= (struct commit
*)peel_to_type(name
, 0, obj
, OBJ_COMMIT
);
1555 if (commit
&& !commit
->util
) {
1556 struct merge_remote_desc
*desc
;
1557 desc
= xmalloc(sizeof(*desc
));
1559 desc
->name
= strdup(name
);
1560 commit
->util
= desc
;
1566 * Append a commit to the end of the commit_list.
1568 * next starts by pointing to the variable that holds the head of an
1569 * empty commit_list, and is updated to point to the "next" field of
1570 * the last item on the list as new commits are appended.
1574 * struct commit_list *list;
1575 * struct commit_list **next = &list;
1577 * next = commit_list_append(c1, next);
1578 * next = commit_list_append(c2, next);
1579 * assert(commit_list_count(list) == 2);
1582 struct commit_list
**commit_list_append(struct commit
*commit
,
1583 struct commit_list
**next
)
1585 struct commit_list
*new = xmalloc(sizeof(struct commit_list
));
1592 void print_commit_list(struct commit_list
*list
,
1593 const char *format_cur
,
1594 const char *format_last
)
1596 for ( ; list
; list
= list
->next
) {
1597 const char *format
= list
->next
? format_cur
: format_last
;
1598 printf(format
, sha1_to_hex(list
->item
->object
.sha1
));