4 #include "commit-graph.h"
10 #include "gpg-interface.h"
11 #include "mergesort.h"
12 #include "commit-slab.h"
13 #include "prio-queue.h"
14 #include "sha1-lookup.h"
15 #include "wt-status.h"
18 static struct commit_extra_header
*read_commit_extra_header_lines(const char *buf
, size_t len
, const char **);
20 int save_commit_buffer
= 1;
22 const char *commit_type
= "commit";
24 struct commit
*lookup_commit_reference_gently(const struct object_id
*oid
,
27 struct object
*obj
= deref_tag(parse_object(oid
), NULL
, 0);
31 return object_as_type(obj
, OBJ_COMMIT
, quiet
);
34 struct commit
*lookup_commit_reference(const struct object_id
*oid
)
36 return lookup_commit_reference_gently(oid
, 0);
39 struct commit
*lookup_commit_or_die(const struct object_id
*oid
, const char *ref_name
)
41 struct commit
*c
= lookup_commit_reference(oid
);
43 die(_("could not parse %s"), ref_name
);
44 if (oidcmp(oid
, &c
->object
.oid
)) {
45 warning(_("%s %s is not a commit!"),
46 ref_name
, oid_to_hex(oid
));
51 struct commit
*lookup_commit(const struct object_id
*oid
)
53 struct object
*obj
= lookup_object(oid
->hash
);
55 return create_object(oid
->hash
, alloc_commit_node());
56 return object_as_type(obj
, OBJ_COMMIT
, 0);
59 struct commit
*lookup_commit_reference_by_name(const char *name
)
62 struct commit
*commit
;
64 if (get_oid_committish(name
, &oid
))
66 commit
= lookup_commit_reference(&oid
);
67 if (parse_commit(commit
))
72 static timestamp_t
parse_commit_date(const char *buf
, const char *tail
)
78 if (memcmp(buf
, "author", 6))
80 while (buf
< tail
&& *buf
++ != '\n')
84 if (memcmp(buf
, "committer", 9))
86 while (buf
< tail
&& *buf
++ != '>')
91 while (buf
< tail
&& *buf
++ != '\n')
95 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
96 return parse_timestamp(dateptr
, NULL
, 10);
99 static struct commit_graft
**commit_graft
;
100 static int commit_graft_alloc
, commit_graft_nr
;
102 static const unsigned char *commit_graft_sha1_access(size_t index
, void *table
)
104 struct commit_graft
**commit_graft_table
= table
;
105 return commit_graft_table
[index
]->oid
.hash
;
108 static int commit_graft_pos(const unsigned char *sha1
)
110 return sha1_pos(sha1
, commit_graft
, commit_graft_nr
,
111 commit_graft_sha1_access
);
114 int register_commit_graft(struct commit_graft
*graft
, int ignore_dups
)
116 int pos
= commit_graft_pos(graft
->oid
.hash
);
122 free(commit_graft
[pos
]);
123 commit_graft
[pos
] = graft
;
128 ALLOC_GROW(commit_graft
, commit_graft_nr
+ 1, commit_graft_alloc
);
130 if (pos
< commit_graft_nr
)
131 MOVE_ARRAY(commit_graft
+ pos
+ 1, commit_graft
+ pos
,
132 commit_graft_nr
- pos
- 1);
133 commit_graft
[pos
] = graft
;
137 struct commit_graft
*read_graft_line(struct strbuf
*line
)
139 /* The format is just "Commit Parent1 Parent2 ...\n" */
141 const char *tail
= NULL
;
142 struct commit_graft
*graft
= NULL
;
143 struct object_id dummy_oid
, *oid
;
146 if (!line
->len
|| line
->buf
[0] == '#')
149 * phase 0 verifies line, counts hashes in line and allocates graft
150 * phase 1 fills graft
152 for (phase
= 0; phase
< 2; phase
++) {
153 oid
= graft
? &graft
->oid
: &dummy_oid
;
154 if (parse_oid_hex(line
->buf
, oid
, &tail
))
156 for (i
= 0; *tail
!= '\0'; i
++) {
157 oid
= graft
? &graft
->parent
[i
] : &dummy_oid
;
158 if (!isspace(*tail
++) || parse_oid_hex(tail
, oid
, &tail
))
162 graft
= xmalloc(st_add(sizeof(*graft
),
163 st_mult(sizeof(struct object_id
), i
)));
164 graft
->nr_parent
= i
;
170 error("bad graft data: %s", line
->buf
);
175 static int read_graft_file(const char *graft_file
)
177 FILE *fp
= fopen_or_warn(graft_file
, "r");
178 struct strbuf buf
= STRBUF_INIT
;
181 if (advice_graft_file_deprecated
)
182 advise(_("Support for <GIT_DIR>/info/grafts is deprecated\n"
183 "and will be removed in a future Git version.\n"
185 "Please use \"git replace --convert-graft-file\"\n"
186 "to convert the grafts into replace refs.\n"
188 "Turn this message off by running\n"
189 "\"git config advice.graftFileDeprecated false\""));
190 while (!strbuf_getwholeline(&buf
, fp
, '\n')) {
191 /* The format is just "Commit Parent1 Parent2 ...\n" */
192 struct commit_graft
*graft
= read_graft_line(&buf
);
195 if (register_commit_graft(graft
, 1))
196 error("duplicate graft data: %s", buf
.buf
);
199 strbuf_release(&buf
);
203 static void prepare_commit_graft(void)
205 static int commit_graft_prepared
;
208 if (commit_graft_prepared
)
210 if (!startup_info
->have_repository
)
213 graft_file
= get_graft_file();
214 read_graft_file(graft_file
);
215 /* make sure shallows are read */
216 is_repository_shallow();
217 commit_graft_prepared
= 1;
220 struct commit_graft
*lookup_commit_graft(const struct object_id
*oid
)
223 prepare_commit_graft();
224 pos
= commit_graft_pos(oid
->hash
);
227 return commit_graft
[pos
];
230 int for_each_commit_graft(each_commit_graft_fn fn
, void *cb_data
)
233 for (i
= ret
= 0; i
< commit_graft_nr
&& !ret
; i
++)
234 ret
= fn(commit_graft
[i
], cb_data
);
238 int unregister_shallow(const struct object_id
*oid
)
240 int pos
= commit_graft_pos(oid
->hash
);
243 if (pos
+ 1 < commit_graft_nr
)
244 MOVE_ARRAY(commit_graft
+ pos
, commit_graft
+ pos
+ 1,
245 commit_graft_nr
- pos
- 1);
250 struct commit_buffer
{
254 define_commit_slab(buffer_slab
, struct commit_buffer
);
255 static struct buffer_slab buffer_slab
= COMMIT_SLAB_INIT(1, buffer_slab
);
257 void set_commit_buffer(struct commit
*commit
, void *buffer
, unsigned long size
)
259 struct commit_buffer
*v
= buffer_slab_at(&buffer_slab
, commit
);
264 const void *get_cached_commit_buffer(const struct commit
*commit
, unsigned long *sizep
)
266 struct commit_buffer
*v
= buffer_slab_peek(&buffer_slab
, commit
);
277 const void *get_commit_buffer(const struct commit
*commit
, unsigned long *sizep
)
279 const void *ret
= get_cached_commit_buffer(commit
, sizep
);
281 enum object_type type
;
283 ret
= read_object_file(&commit
->object
.oid
, &type
, &size
);
285 die("cannot read commit object %s",
286 oid_to_hex(&commit
->object
.oid
));
287 if (type
!= OBJ_COMMIT
)
288 die("expected commit for %s, got %s",
289 oid_to_hex(&commit
->object
.oid
), type_name(type
));
296 void unuse_commit_buffer(const struct commit
*commit
, const void *buffer
)
298 struct commit_buffer
*v
= buffer_slab_peek(&buffer_slab
, commit
);
299 if (!(v
&& v
->buffer
== buffer
))
300 free((void *)buffer
);
303 void free_commit_buffer(struct commit
*commit
)
305 struct commit_buffer
*v
= buffer_slab_peek(&buffer_slab
, commit
);
307 FREE_AND_NULL(v
->buffer
);
312 struct tree
*get_commit_tree(const struct commit
*commit
)
314 if (commit
->maybe_tree
|| !commit
->object
.parsed
)
315 return commit
->maybe_tree
;
317 if (commit
->graph_pos
== COMMIT_NOT_FROM_GRAPH
)
318 BUG("commit has NULL tree, but was not loaded from commit-graph");
320 return get_commit_tree_in_graph(commit
);
323 struct object_id
*get_commit_tree_oid(const struct commit
*commit
)
325 return &get_commit_tree(commit
)->object
.oid
;
328 const void *detach_commit_buffer(struct commit
*commit
, unsigned long *sizep
)
330 struct commit_buffer
*v
= buffer_slab_peek(&buffer_slab
, commit
);
347 int parse_commit_buffer(struct commit
*item
, const void *buffer
, unsigned long size
)
349 const char *tail
= buffer
;
350 const char *bufptr
= buffer
;
351 struct object_id parent
;
352 struct commit_list
**pptr
;
353 struct commit_graft
*graft
;
354 const int tree_entry_len
= GIT_SHA1_HEXSZ
+ 5;
355 const int parent_entry_len
= GIT_SHA1_HEXSZ
+ 7;
357 if (item
->object
.parsed
)
359 item
->object
.parsed
= 1;
361 if (tail
<= bufptr
+ tree_entry_len
+ 1 || memcmp(bufptr
, "tree ", 5) ||
362 bufptr
[tree_entry_len
] != '\n')
363 return error("bogus commit object %s", oid_to_hex(&item
->object
.oid
));
364 if (get_oid_hex(bufptr
+ 5, &parent
) < 0)
365 return error("bad tree pointer in commit %s",
366 oid_to_hex(&item
->object
.oid
));
367 item
->maybe_tree
= lookup_tree(&parent
);
368 bufptr
+= tree_entry_len
+ 1; /* "tree " + "hex sha1" + "\n" */
369 pptr
= &item
->parents
;
371 graft
= lookup_commit_graft(&item
->object
.oid
);
372 while (bufptr
+ parent_entry_len
< tail
&& !memcmp(bufptr
, "parent ", 7)) {
373 struct commit
*new_parent
;
375 if (tail
<= bufptr
+ parent_entry_len
+ 1 ||
376 get_oid_hex(bufptr
+ 7, &parent
) ||
377 bufptr
[parent_entry_len
] != '\n')
378 return error("bad parents in commit %s", oid_to_hex(&item
->object
.oid
));
379 bufptr
+= parent_entry_len
+ 1;
381 * The clone is shallow if nr_parent < 0, and we must
382 * not traverse its real parents even when we unhide them.
384 if (graft
&& (graft
->nr_parent
< 0 || grafts_replace_parents
))
386 new_parent
= lookup_commit(&parent
);
388 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
392 struct commit
*new_parent
;
393 for (i
= 0; i
< graft
->nr_parent
; i
++) {
394 new_parent
= lookup_commit(&graft
->parent
[i
]);
397 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
400 item
->date
= parse_commit_date(bufptr
, tail
);
405 int parse_commit_gently(struct commit
*item
, int quiet_on_missing
)
407 enum object_type type
;
414 if (item
->object
.parsed
)
416 if (parse_commit_in_graph(item
))
418 buffer
= read_object_file(&item
->object
.oid
, &type
, &size
);
420 return quiet_on_missing
? -1 :
421 error("Could not read %s",
422 oid_to_hex(&item
->object
.oid
));
423 if (type
!= OBJ_COMMIT
) {
425 return error("Object %s not a commit",
426 oid_to_hex(&item
->object
.oid
));
428 ret
= parse_commit_buffer(item
, buffer
, size
);
429 if (save_commit_buffer
&& !ret
) {
430 set_commit_buffer(item
, buffer
, size
);
437 void parse_commit_or_die(struct commit
*item
)
439 if (parse_commit(item
))
440 die("unable to parse commit %s",
441 item
? oid_to_hex(&item
->object
.oid
) : "(null)");
444 int find_commit_subject(const char *commit_buffer
, const char **subject
)
447 const char *p
= commit_buffer
;
449 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
452 p
= skip_blank_lines(p
+ 2);
453 eol
= strchrnul(p
, '\n');
462 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
464 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
465 new_list
->item
= item
;
466 new_list
->next
= *list_p
;
471 unsigned commit_list_count(const struct commit_list
*l
)
474 for (; l
; l
= l
->next
)
479 struct commit_list
*copy_commit_list(struct commit_list
*list
)
481 struct commit_list
*head
= NULL
;
482 struct commit_list
**pp
= &head
;
484 pp
= commit_list_append(list
->item
, pp
);
490 void free_commit_list(struct commit_list
*list
)
496 struct commit_list
* commit_list_insert_by_date(struct commit
*item
, struct commit_list
**list
)
498 struct commit_list
**pp
= list
;
499 struct commit_list
*p
;
500 while ((p
= *pp
) != NULL
) {
501 if (p
->item
->date
< item
->date
) {
506 return commit_list_insert(item
, pp
);
509 static int commit_list_compare_by_date(const void *a
, const void *b
)
511 timestamp_t a_date
= ((const struct commit_list
*)a
)->item
->date
;
512 timestamp_t b_date
= ((const struct commit_list
*)b
)->item
->date
;
520 static void *commit_list_get_next(const void *a
)
522 return ((const struct commit_list
*)a
)->next
;
525 static void commit_list_set_next(void *a
, void *next
)
527 ((struct commit_list
*)a
)->next
= next
;
530 void commit_list_sort_by_date(struct commit_list
**list
)
532 *list
= llist_mergesort(*list
, commit_list_get_next
, commit_list_set_next
,
533 commit_list_compare_by_date
);
536 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
539 struct commit
*ret
= pop_commit(list
);
540 struct commit_list
*parents
= ret
->parents
;
543 struct commit
*commit
= parents
->item
;
544 if (!parse_commit(commit
) && !(commit
->object
.flags
& mark
)) {
545 commit
->object
.flags
|= mark
;
546 commit_list_insert_by_date(commit
, list
);
548 parents
= parents
->next
;
553 static void clear_commit_marks_1(struct commit_list
**plist
,
554 struct commit
*commit
, unsigned int mark
)
557 struct commit_list
*parents
;
559 if (!(mark
& commit
->object
.flags
))
562 commit
->object
.flags
&= ~mark
;
564 parents
= commit
->parents
;
568 while ((parents
= parents
->next
))
569 commit_list_insert(parents
->item
, plist
);
571 commit
= commit
->parents
->item
;
575 void clear_commit_marks_many(int nr
, struct commit
**commit
, unsigned int mark
)
577 struct commit_list
*list
= NULL
;
580 clear_commit_marks_1(&list
, *commit
, mark
);
584 clear_commit_marks_1(&list
, pop_commit(&list
), mark
);
587 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
589 clear_commit_marks_many(1, &commit
, mark
);
592 struct commit
*pop_commit(struct commit_list
**stack
)
594 struct commit_list
*top
= *stack
;
595 struct commit
*item
= top
? top
->item
: NULL
;
605 * Topological sort support
608 /* count number of children that have not been emitted */
609 define_commit_slab(indegree_slab
, int);
611 /* record author-date for each commit object */
612 define_commit_slab(author_date_slab
, unsigned long);
614 static void record_author_date(struct author_date_slab
*author_date
,
615 struct commit
*commit
)
617 const char *buffer
= get_commit_buffer(commit
, NULL
);
618 struct ident_split ident
;
619 const char *ident_line
;
624 ident_line
= find_commit_header(buffer
, "author", &ident_len
);
626 goto fail_exit
; /* no author line */
627 if (split_ident_line(&ident
, ident_line
, ident_len
) ||
628 !ident
.date_begin
|| !ident
.date_end
)
629 goto fail_exit
; /* malformed "author" line */
631 date
= parse_timestamp(ident
.date_begin
, &date_end
, 10);
632 if (date_end
!= ident
.date_end
)
633 goto fail_exit
; /* malformed date */
634 *(author_date_slab_at(author_date
, commit
)) = date
;
637 unuse_commit_buffer(commit
, buffer
);
640 static int compare_commits_by_author_date(const void *a_
, const void *b_
,
643 const struct commit
*a
= a_
, *b
= b_
;
644 struct author_date_slab
*author_date
= cb_data
;
645 timestamp_t a_date
= *(author_date_slab_at(author_date
, a
));
646 timestamp_t b_date
= *(author_date_slab_at(author_date
, b
));
648 /* newer commits with larger date first */
651 else if (a_date
> b_date
)
656 int compare_commits_by_commit_date(const void *a_
, const void *b_
, void *unused
)
658 const struct commit
*a
= a_
, *b
= b_
;
659 /* newer commits with larger date first */
660 if (a
->date
< b
->date
)
662 else if (a
->date
> b
->date
)
668 * Performs an in-place topological sort on the list supplied.
670 void sort_in_topological_order(struct commit_list
**list
, enum rev_sort_order sort_order
)
672 struct commit_list
*next
, *orig
= *list
;
673 struct commit_list
**pptr
;
674 struct indegree_slab indegree
;
675 struct prio_queue queue
;
676 struct commit
*commit
;
677 struct author_date_slab author_date
;
683 init_indegree_slab(&indegree
);
684 memset(&queue
, '\0', sizeof(queue
));
686 switch (sort_order
) {
687 default: /* REV_SORT_IN_GRAPH_ORDER */
688 queue
.compare
= NULL
;
690 case REV_SORT_BY_COMMIT_DATE
:
691 queue
.compare
= compare_commits_by_commit_date
;
693 case REV_SORT_BY_AUTHOR_DATE
:
694 init_author_date_slab(&author_date
);
695 queue
.compare
= compare_commits_by_author_date
;
696 queue
.cb_data
= &author_date
;
700 /* Mark them and clear the indegree */
701 for (next
= orig
; next
; next
= next
->next
) {
702 struct commit
*commit
= next
->item
;
703 *(indegree_slab_at(&indegree
, commit
)) = 1;
704 /* also record the author dates, if needed */
705 if (sort_order
== REV_SORT_BY_AUTHOR_DATE
)
706 record_author_date(&author_date
, commit
);
709 /* update the indegree */
710 for (next
= orig
; next
; next
= next
->next
) {
711 struct commit_list
*parents
= next
->item
->parents
;
713 struct commit
*parent
= parents
->item
;
714 int *pi
= indegree_slab_at(&indegree
, parent
);
718 parents
= parents
->next
;
725 * tips are nodes not reachable from any other node in the list
727 * the tips serve as a starting set for the work queue.
729 for (next
= orig
; next
; next
= next
->next
) {
730 struct commit
*commit
= next
->item
;
732 if (*(indegree_slab_at(&indegree
, commit
)) == 1)
733 prio_queue_put(&queue
, commit
);
737 * This is unfortunate; the initial tips need to be shown
738 * in the order given from the revision traversal machinery.
740 if (sort_order
== REV_SORT_IN_GRAPH_ORDER
)
741 prio_queue_reverse(&queue
);
743 /* We no longer need the commit list */
744 free_commit_list(orig
);
748 while ((commit
= prio_queue_get(&queue
)) != NULL
) {
749 struct commit_list
*parents
;
751 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
752 struct commit
*parent
= parents
->item
;
753 int *pi
= indegree_slab_at(&indegree
, parent
);
759 * parents are only enqueued for emission
760 * when all their children have been emitted thereby
761 * guaranteeing topological order.
764 prio_queue_put(&queue
, parent
);
767 * all children of commit have already been
768 * emitted. we can emit it now.
770 *(indegree_slab_at(&indegree
, commit
)) = 0;
772 pptr
= &commit_list_insert(commit
, pptr
)->next
;
775 clear_indegree_slab(&indegree
);
776 clear_prio_queue(&queue
);
777 if (sort_order
== REV_SORT_BY_AUTHOR_DATE
)
778 clear_author_date_slab(&author_date
);
781 /* merge-base stuff */
783 /* Remember to update object flag allocation in object.h */
784 #define PARENT1 (1u<<16)
785 #define PARENT2 (1u<<17)
786 #define STALE (1u<<18)
787 #define RESULT (1u<<19)
789 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
791 static int queue_has_nonstale(struct prio_queue
*queue
)
794 for (i
= 0; i
< queue
->nr
; i
++) {
795 struct commit
*commit
= queue
->array
[i
].data
;
796 if (!(commit
->object
.flags
& STALE
))
802 /* all input commits in one and twos[] must have been parsed! */
803 static struct commit_list
*paint_down_to_common(struct commit
*one
, int n
, struct commit
**twos
)
805 struct prio_queue queue
= { compare_commits_by_commit_date
};
806 struct commit_list
*result
= NULL
;
809 one
->object
.flags
|= PARENT1
;
811 commit_list_append(one
, &result
);
814 prio_queue_put(&queue
, one
);
816 for (i
= 0; i
< n
; i
++) {
817 twos
[i
]->object
.flags
|= PARENT2
;
818 prio_queue_put(&queue
, twos
[i
]);
821 while (queue_has_nonstale(&queue
)) {
822 struct commit
*commit
= prio_queue_get(&queue
);
823 struct commit_list
*parents
;
826 flags
= commit
->object
.flags
& (PARENT1
| PARENT2
| STALE
);
827 if (flags
== (PARENT1
| PARENT2
)) {
828 if (!(commit
->object
.flags
& RESULT
)) {
829 commit
->object
.flags
|= RESULT
;
830 commit_list_insert_by_date(commit
, &result
);
832 /* Mark parents of a found merge stale */
835 parents
= commit
->parents
;
837 struct commit
*p
= parents
->item
;
838 parents
= parents
->next
;
839 if ((p
->object
.flags
& flags
) == flags
)
843 p
->object
.flags
|= flags
;
844 prio_queue_put(&queue
, p
);
848 clear_prio_queue(&queue
);
852 static struct commit_list
*merge_bases_many(struct commit
*one
, int n
, struct commit
**twos
)
854 struct commit_list
*list
= NULL
;
855 struct commit_list
*result
= NULL
;
858 for (i
= 0; i
< n
; i
++) {
861 * We do not mark this even with RESULT so we do not
862 * have to clean it up.
864 return commit_list_insert(one
, &result
);
867 if (parse_commit(one
))
869 for (i
= 0; i
< n
; i
++) {
870 if (parse_commit(twos
[i
]))
874 list
= paint_down_to_common(one
, n
, twos
);
877 struct commit
*commit
= pop_commit(&list
);
878 if (!(commit
->object
.flags
& STALE
))
879 commit_list_insert_by_date(commit
, &result
);
884 struct commit_list
*get_octopus_merge_bases(struct commit_list
*in
)
886 struct commit_list
*i
, *j
, *k
, *ret
= NULL
;
891 commit_list_insert(in
->item
, &ret
);
893 for (i
= in
->next
; i
; i
= i
->next
) {
894 struct commit_list
*new_commits
= NULL
, *end
= NULL
;
896 for (j
= ret
; j
; j
= j
->next
) {
897 struct commit_list
*bases
;
898 bases
= get_merge_bases(i
->item
, j
->item
);
903 for (k
= bases
; k
; k
= k
->next
)
911 static int remove_redundant(struct commit
**array
, int cnt
)
914 * Some commit in the array may be an ancestor of
915 * another commit. Move such commit to the end of
916 * the array, and return the number of commits that
917 * are independent from each other.
919 struct commit
**work
;
920 unsigned char *redundant
;
924 work
= xcalloc(cnt
, sizeof(*work
));
925 redundant
= xcalloc(cnt
, 1);
926 ALLOC_ARRAY(filled_index
, cnt
- 1);
928 for (i
= 0; i
< cnt
; i
++)
929 parse_commit(array
[i
]);
930 for (i
= 0; i
< cnt
; i
++) {
931 struct commit_list
*common
;
935 for (j
= filled
= 0; j
< cnt
; j
++) {
936 if (i
== j
|| redundant
[j
])
938 filled_index
[filled
] = j
;
939 work
[filled
++] = array
[j
];
941 common
= paint_down_to_common(array
[i
], filled
, work
);
942 if (array
[i
]->object
.flags
& PARENT2
)
944 for (j
= 0; j
< filled
; j
++)
945 if (work
[j
]->object
.flags
& PARENT1
)
946 redundant
[filled_index
[j
]] = 1;
947 clear_commit_marks(array
[i
], all_flags
);
948 clear_commit_marks_many(filled
, work
, all_flags
);
949 free_commit_list(common
);
952 /* Now collect the result */
953 COPY_ARRAY(work
, array
, cnt
);
954 for (i
= filled
= 0; i
< cnt
; i
++)
956 array
[filled
++] = work
[i
];
957 for (j
= filled
, i
= 0; i
< cnt
; i
++)
959 array
[j
++] = work
[i
];
966 static struct commit_list
*get_merge_bases_many_0(struct commit
*one
,
968 struct commit
**twos
,
971 struct commit_list
*list
;
972 struct commit
**rslt
;
973 struct commit_list
*result
;
976 result
= merge_bases_many(one
, n
, twos
);
977 for (i
= 0; i
< n
; i
++) {
981 if (!result
|| !result
->next
) {
983 clear_commit_marks(one
, all_flags
);
984 clear_commit_marks_many(n
, twos
, all_flags
);
989 /* There are more than one */
990 cnt
= commit_list_count(result
);
991 rslt
= xcalloc(cnt
, sizeof(*rslt
));
992 for (list
= result
, i
= 0; list
; list
= list
->next
)
993 rslt
[i
++] = list
->item
;
994 free_commit_list(result
);
996 clear_commit_marks(one
, all_flags
);
997 clear_commit_marks_many(n
, twos
, all_flags
);
999 cnt
= remove_redundant(rslt
, cnt
);
1001 for (i
= 0; i
< cnt
; i
++)
1002 commit_list_insert_by_date(rslt
[i
], &result
);
1007 struct commit_list
*get_merge_bases_many(struct commit
*one
,
1009 struct commit
**twos
)
1011 return get_merge_bases_many_0(one
, n
, twos
, 1);
1014 struct commit_list
*get_merge_bases_many_dirty(struct commit
*one
,
1016 struct commit
**twos
)
1018 return get_merge_bases_many_0(one
, n
, twos
, 0);
1021 struct commit_list
*get_merge_bases(struct commit
*one
, struct commit
*two
)
1023 return get_merge_bases_many_0(one
, 1, &two
, 1);
1027 * Is "commit" a descendant of one of the elements on the "with_commit" list?
1029 int is_descendant_of(struct commit
*commit
, struct commit_list
*with_commit
)
1033 while (with_commit
) {
1034 struct commit
*other
;
1036 other
= with_commit
->item
;
1037 with_commit
= with_commit
->next
;
1038 if (in_merge_bases(other
, commit
))
1045 * Is "commit" an ancestor of one of the "references"?
1047 int in_merge_bases_many(struct commit
*commit
, int nr_reference
, struct commit
**reference
)
1049 struct commit_list
*bases
;
1052 if (parse_commit(commit
))
1054 for (i
= 0; i
< nr_reference
; i
++)
1055 if (parse_commit(reference
[i
]))
1058 bases
= paint_down_to_common(commit
, nr_reference
, reference
);
1059 if (commit
->object
.flags
& PARENT2
)
1061 clear_commit_marks(commit
, all_flags
);
1062 clear_commit_marks_many(nr_reference
, reference
, all_flags
);
1063 free_commit_list(bases
);
1068 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
1070 int in_merge_bases(struct commit
*commit
, struct commit
*reference
)
1072 return in_merge_bases_many(commit
, 1, &reference
);
1075 struct commit_list
*reduce_heads(struct commit_list
*heads
)
1077 struct commit_list
*p
;
1078 struct commit_list
*result
= NULL
, **tail
= &result
;
1079 struct commit
**array
;
1086 for (p
= heads
; p
; p
= p
->next
)
1087 p
->item
->object
.flags
&= ~STALE
;
1088 for (p
= heads
, num_head
= 0; p
; p
= p
->next
) {
1089 if (p
->item
->object
.flags
& STALE
)
1091 p
->item
->object
.flags
|= STALE
;
1094 array
= xcalloc(num_head
, sizeof(*array
));
1095 for (p
= heads
, i
= 0; p
; p
= p
->next
) {
1096 if (p
->item
->object
.flags
& STALE
) {
1097 array
[i
++] = p
->item
;
1098 p
->item
->object
.flags
&= ~STALE
;
1101 num_head
= remove_redundant(array
, num_head
);
1102 for (i
= 0; i
< num_head
; i
++)
1103 tail
= &commit_list_insert(array
[i
], tail
)->next
;
1108 void reduce_heads_replace(struct commit_list
**heads
)
1110 struct commit_list
*result
= reduce_heads(*heads
);
1111 free_commit_list(*heads
);
1115 static const char gpg_sig_header
[] = "gpgsig";
1116 static const int gpg_sig_header_len
= sizeof(gpg_sig_header
) - 1;
1118 static int do_sign_commit(struct strbuf
*buf
, const char *keyid
)
1120 struct strbuf sig
= STRBUF_INIT
;
1121 int inspos
, copypos
;
1124 /* find the end of the header */
1125 eoh
= strstr(buf
->buf
, "\n\n");
1129 inspos
= eoh
- buf
->buf
+ 1;
1131 if (!keyid
|| !*keyid
)
1132 keyid
= get_signing_key();
1133 if (sign_buffer(buf
, &sig
, keyid
)) {
1134 strbuf_release(&sig
);
1138 for (copypos
= 0; sig
.buf
[copypos
]; ) {
1139 const char *bol
= sig
.buf
+ copypos
;
1140 const char *eol
= strchrnul(bol
, '\n');
1141 int len
= (eol
- bol
) + !!*eol
;
1144 strbuf_insert(buf
, inspos
, gpg_sig_header
, gpg_sig_header_len
);
1145 inspos
+= gpg_sig_header_len
;
1147 strbuf_insert(buf
, inspos
++, " ", 1);
1148 strbuf_insert(buf
, inspos
, bol
, len
);
1152 strbuf_release(&sig
);
1156 int parse_signed_commit(const struct commit
*commit
,
1157 struct strbuf
*payload
, struct strbuf
*signature
)
1161 const char *buffer
= get_commit_buffer(commit
, &size
);
1162 int in_signature
, saw_signature
= -1;
1163 const char *line
, *tail
;
1166 tail
= buffer
+ size
;
1169 while (line
< tail
) {
1170 const char *sig
= NULL
;
1171 const char *next
= memchr(line
, '\n', tail
- line
);
1173 next
= next
? next
+ 1 : tail
;
1174 if (in_signature
&& line
[0] == ' ')
1176 else if (starts_with(line
, gpg_sig_header
) &&
1177 line
[gpg_sig_header_len
] == ' ')
1178 sig
= line
+ gpg_sig_header_len
+ 1;
1180 strbuf_add(signature
, sig
, next
- sig
);
1185 /* dump the whole remainder of the buffer */
1187 strbuf_add(payload
, line
, next
- line
);
1192 unuse_commit_buffer(commit
, buffer
);
1193 return saw_signature
;
1196 int remove_signature(struct strbuf
*buf
)
1198 const char *line
= buf
->buf
;
1199 const char *tail
= buf
->buf
+ buf
->len
;
1200 int in_signature
= 0;
1201 const char *sig_start
= NULL
;
1202 const char *sig_end
= NULL
;
1204 while (line
< tail
) {
1205 const char *next
= memchr(line
, '\n', tail
- line
);
1206 next
= next
? next
+ 1 : tail
;
1208 if (in_signature
&& line
[0] == ' ')
1210 else if (starts_with(line
, gpg_sig_header
) &&
1211 line
[gpg_sig_header_len
] == ' ') {
1217 /* dump the whole remainder of the buffer */
1225 strbuf_remove(buf
, sig_start
- buf
->buf
, sig_end
- sig_start
);
1227 return sig_start
!= NULL
;
1230 static void handle_signed_tag(struct commit
*parent
, struct commit_extra_header
***tail
)
1232 struct merge_remote_desc
*desc
;
1233 struct commit_extra_header
*mergetag
;
1235 unsigned long size
, len
;
1236 enum object_type type
;
1238 desc
= merge_remote_util(parent
);
1239 if (!desc
|| !desc
->obj
)
1241 buf
= read_object_file(&desc
->obj
->oid
, &type
, &size
);
1242 if (!buf
|| type
!= OBJ_TAG
)
1244 len
= parse_signature(buf
, size
);
1248 * We could verify this signature and either omit the tag when
1249 * it does not validate, but the integrator may not have the
1250 * public key of the signer of the tag he is merging, while a
1251 * later auditor may have it while auditing, so let's not run
1252 * verify-signed-buffer here for now...
1254 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1255 * warn("warning: signed tag unverified.");
1257 mergetag
= xcalloc(1, sizeof(*mergetag
));
1258 mergetag
->key
= xstrdup("mergetag");
1259 mergetag
->value
= buf
;
1260 mergetag
->len
= size
;
1263 *tail
= &mergetag
->next
;
1270 int check_commit_signature(const struct commit
*commit
, struct signature_check
*sigc
)
1272 struct strbuf payload
= STRBUF_INIT
;
1273 struct strbuf signature
= STRBUF_INIT
;
1278 if (parse_signed_commit(commit
, &payload
, &signature
) <= 0)
1280 ret
= check_signature(payload
.buf
, payload
.len
, signature
.buf
,
1281 signature
.len
, sigc
);
1284 strbuf_release(&payload
);
1285 strbuf_release(&signature
);
1292 void append_merge_tag_headers(struct commit_list
*parents
,
1293 struct commit_extra_header
***tail
)
1296 struct commit
*parent
= parents
->item
;
1297 handle_signed_tag(parent
, tail
);
1298 parents
= parents
->next
;
1302 static void add_extra_header(struct strbuf
*buffer
,
1303 struct commit_extra_header
*extra
)
1305 strbuf_addstr(buffer
, extra
->key
);
1307 strbuf_add_lines(buffer
, " ", extra
->value
, extra
->len
);
1309 strbuf_addch(buffer
, '\n');
1312 struct commit_extra_header
*read_commit_extra_headers(struct commit
*commit
,
1313 const char **exclude
)
1315 struct commit_extra_header
*extra
= NULL
;
1317 const char *buffer
= get_commit_buffer(commit
, &size
);
1318 extra
= read_commit_extra_header_lines(buffer
, size
, exclude
);
1319 unuse_commit_buffer(commit
, buffer
);
1323 int for_each_mergetag(each_mergetag_fn fn
, struct commit
*commit
, void *data
)
1325 struct commit_extra_header
*extra
, *to_free
;
1328 to_free
= read_commit_extra_headers(commit
, NULL
);
1329 for (extra
= to_free
; !res
&& extra
; extra
= extra
->next
) {
1330 if (strcmp(extra
->key
, "mergetag"))
1331 continue; /* not a merge tag */
1332 res
= fn(commit
, extra
, data
);
1334 free_commit_extra_headers(to_free
);
1338 static inline int standard_header_field(const char *field
, size_t len
)
1340 return ((len
== 4 && !memcmp(field
, "tree", 4)) ||
1341 (len
== 6 && !memcmp(field
, "parent", 6)) ||
1342 (len
== 6 && !memcmp(field
, "author", 6)) ||
1343 (len
== 9 && !memcmp(field
, "committer", 9)) ||
1344 (len
== 8 && !memcmp(field
, "encoding", 8)));
1347 static int excluded_header_field(const char *field
, size_t len
, const char **exclude
)
1353 size_t xlen
= strlen(*exclude
);
1354 if (len
== xlen
&& !memcmp(field
, *exclude
, xlen
))
1361 static struct commit_extra_header
*read_commit_extra_header_lines(
1362 const char *buffer
, size_t size
,
1363 const char **exclude
)
1365 struct commit_extra_header
*extra
= NULL
, **tail
= &extra
, *it
= NULL
;
1366 const char *line
, *next
, *eof
, *eob
;
1367 struct strbuf buf
= STRBUF_INIT
;
1369 for (line
= buffer
, eob
= line
+ size
;
1370 line
< eob
&& *line
!= '\n';
1372 next
= memchr(line
, '\n', eob
- line
);
1373 next
= next
? next
+ 1 : eob
;
1377 strbuf_add(&buf
, line
+ 1, next
- (line
+ 1));
1381 it
->value
= strbuf_detach(&buf
, &it
->len
);
1385 eof
= memchr(line
, ' ', next
- line
);
1388 else if (standard_header_field(line
, eof
- line
) ||
1389 excluded_header_field(line
, eof
- line
, exclude
))
1392 it
= xcalloc(1, sizeof(*it
));
1393 it
->key
= xmemdupz(line
, eof
-line
);
1397 strbuf_add(&buf
, eof
+ 1, next
- (eof
+ 1));
1400 it
->value
= strbuf_detach(&buf
, &it
->len
);
1404 void free_commit_extra_headers(struct commit_extra_header
*extra
)
1407 struct commit_extra_header
*next
= extra
->next
;
1415 int commit_tree(const char *msg
, size_t msg_len
, const struct object_id
*tree
,
1416 struct commit_list
*parents
, struct object_id
*ret
,
1417 const char *author
, const char *sign_commit
)
1419 struct commit_extra_header
*extra
= NULL
, **tail
= &extra
;
1422 append_merge_tag_headers(parents
, &tail
);
1423 result
= commit_tree_extended(msg
, msg_len
, tree
, parents
, ret
,
1424 author
, sign_commit
, extra
);
1425 free_commit_extra_headers(extra
);
1429 static int find_invalid_utf8(const char *buf
, int len
)
1432 static const unsigned int max_codepoint
[] = {
1433 0x7f, 0x7ff, 0xffff, 0x10ffff
1437 unsigned char c
= *buf
++;
1438 int bytes
, bad_offset
;
1439 unsigned int codepoint
;
1440 unsigned int min_val
, max_val
;
1445 /* Simple US-ASCII? No worries. */
1449 bad_offset
= offset
-1;
1452 * Count how many more high bits set: that's how
1453 * many more bytes this sequence should have.
1462 * Must be between 1 and 3 more bytes. Longer sequences result in
1463 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1465 if (bytes
< 1 || 3 < bytes
)
1468 /* Do we *have* that many bytes? */
1473 * Place the encoded bits at the bottom of the value and compute the
1476 codepoint
= (c
& 0x7f) >> bytes
;
1477 min_val
= max_codepoint
[bytes
-1] + 1;
1478 max_val
= max_codepoint
[bytes
];
1483 /* And verify that they are good continuation bytes */
1486 codepoint
|= *buf
& 0x3f;
1487 if ((*buf
++ & 0xc0) != 0x80)
1491 /* Reject codepoints that are out of range for the sequence length. */
1492 if (codepoint
< min_val
|| codepoint
> max_val
)
1494 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1495 if ((codepoint
& 0x1ff800) == 0xd800)
1497 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1498 if ((codepoint
& 0xfffe) == 0xfffe)
1500 /* So are anything in the range U+FDD0..U+FDEF. */
1501 if (codepoint
>= 0xfdd0 && codepoint
<= 0xfdef)
1508 * This verifies that the buffer is in proper utf8 format.
1510 * If it isn't, it assumes any non-utf8 characters are Latin1,
1511 * and does the conversion.
1513 static int verify_utf8(struct strbuf
*buf
)
1521 unsigned char replace
[2];
1523 bad
= find_invalid_utf8(buf
->buf
+ pos
, buf
->len
- pos
);
1529 strbuf_remove(buf
, pos
, 1);
1531 /* We know 'c' must be in the range 128-255 */
1532 replace
[0] = 0xc0 + (c
>> 6);
1533 replace
[1] = 0x80 + (c
& 0x3f);
1534 strbuf_insert(buf
, pos
, replace
, 2);
1539 static const char commit_utf8_warn
[] =
1540 N_("Warning: commit message did not conform to UTF-8.\n"
1541 "You may want to amend it after fixing the message, or set the config\n"
1542 "variable i18n.commitencoding to the encoding your project uses.\n");
1544 int commit_tree_extended(const char *msg
, size_t msg_len
,
1545 const struct object_id
*tree
,
1546 struct commit_list
*parents
, struct object_id
*ret
,
1547 const char *author
, const char *sign_commit
,
1548 struct commit_extra_header
*extra
)
1551 int encoding_is_utf8
;
1552 struct strbuf buffer
;
1554 assert_oid_type(tree
, OBJ_TREE
);
1556 if (memchr(msg
, '\0', msg_len
))
1557 return error("a NUL byte in commit log message not allowed.");
1559 /* Not having i18n.commitencoding is the same as having utf-8 */
1560 encoding_is_utf8
= is_encoding_utf8(git_commit_encoding
);
1562 strbuf_init(&buffer
, 8192); /* should avoid reallocs for the headers */
1563 strbuf_addf(&buffer
, "tree %s\n", oid_to_hex(tree
));
1566 * NOTE! This ordering means that the same exact tree merged with a
1567 * different order of parents will be a _different_ changeset even
1568 * if everything else stays the same.
1571 struct commit
*parent
= pop_commit(&parents
);
1572 strbuf_addf(&buffer
, "parent %s\n",
1573 oid_to_hex(&parent
->object
.oid
));
1576 /* Person/date information */
1578 author
= git_author_info(IDENT_STRICT
);
1579 strbuf_addf(&buffer
, "author %s\n", author
);
1580 strbuf_addf(&buffer
, "committer %s\n", git_committer_info(IDENT_STRICT
));
1581 if (!encoding_is_utf8
)
1582 strbuf_addf(&buffer
, "encoding %s\n", git_commit_encoding
);
1585 add_extra_header(&buffer
, extra
);
1586 extra
= extra
->next
;
1588 strbuf_addch(&buffer
, '\n');
1590 /* And add the comment */
1591 strbuf_add(&buffer
, msg
, msg_len
);
1593 /* And check the encoding */
1594 if (encoding_is_utf8
&& !verify_utf8(&buffer
))
1595 fprintf(stderr
, _(commit_utf8_warn
));
1597 if (sign_commit
&& do_sign_commit(&buffer
, sign_commit
)) {
1602 result
= write_object_file(buffer
.buf
, buffer
.len
, commit_type
, ret
);
1604 strbuf_release(&buffer
);
1608 void set_merge_remote_desc(struct commit
*commit
,
1609 const char *name
, struct object
*obj
)
1611 struct merge_remote_desc
*desc
;
1612 FLEX_ALLOC_STR(desc
, name
, name
);
1614 commit
->util
= desc
;
1617 struct commit
*get_merge_parent(const char *name
)
1620 struct commit
*commit
;
1621 struct object_id oid
;
1622 if (get_oid(name
, &oid
))
1624 obj
= parse_object(&oid
);
1625 commit
= (struct commit
*)peel_to_type(name
, 0, obj
, OBJ_COMMIT
);
1626 if (commit
&& !commit
->util
)
1627 set_merge_remote_desc(commit
, name
, obj
);
1632 * Append a commit to the end of the commit_list.
1634 * next starts by pointing to the variable that holds the head of an
1635 * empty commit_list, and is updated to point to the "next" field of
1636 * the last item on the list as new commits are appended.
1640 * struct commit_list *list;
1641 * struct commit_list **next = &list;
1643 * next = commit_list_append(c1, next);
1644 * next = commit_list_append(c2, next);
1645 * assert(commit_list_count(list) == 2);
1648 struct commit_list
**commit_list_append(struct commit
*commit
,
1649 struct commit_list
**next
)
1651 struct commit_list
*new_commit
= xmalloc(sizeof(struct commit_list
));
1652 new_commit
->item
= commit
;
1654 new_commit
->next
= NULL
;
1655 return &new_commit
->next
;
1658 const char *find_commit_header(const char *msg
, const char *key
, size_t *out_len
)
1660 int key_len
= strlen(key
);
1661 const char *line
= msg
;
1664 const char *eol
= strchrnul(line
, '\n');
1669 if (eol
- line
> key_len
&&
1670 !strncmp(line
, key
, key_len
) &&
1671 line
[key_len
] == ' ') {
1672 *out_len
= eol
- line
- key_len
- 1;
1673 return line
+ key_len
+ 1;
1675 line
= *eol
? eol
+ 1 : NULL
;
1681 * Inspect the given string and determine the true "end" of the log message, in
1682 * order to find where to put a new Signed-off-by: line. Ignored are
1683 * trailing comment lines and blank lines. To support "git commit -s
1684 * --amend" on an existing commit, we also ignore "Conflicts:". To
1685 * support "git commit -v", we truncate at cut lines.
1687 * Returns the number of bytes from the tail to ignore, to be fed as
1688 * the second parameter to append_signoff().
1690 int ignore_non_trailer(const char *buf
, size_t len
)
1694 int in_old_conflicts_block
= 0;
1695 size_t cutoff
= wt_status_locate_end(buf
, len
);
1697 while (bol
< cutoff
) {
1698 const char *next_line
= memchr(buf
+ bol
, '\n', len
- bol
);
1701 next_line
= buf
+ len
;
1705 if (buf
[bol
] == comment_line_char
|| buf
[bol
] == '\n') {
1706 /* is this the first of the run of comments? */
1709 /* otherwise, it is just continuing */
1710 } else if (starts_with(buf
+ bol
, "Conflicts:\n")) {
1711 in_old_conflicts_block
= 1;
1714 } else if (in_old_conflicts_block
&& buf
[bol
] == '\t') {
1715 ; /* a pathname in the conflicts block */
1717 /* the previous was not trailing comment */
1719 in_old_conflicts_block
= 0;
1721 bol
= next_line
- buf
;
1723 return boc
? len
- boc
: len
- cutoff
;