4 #include "commit-graph.h"
5 #include "repository.h"
6 #include "object-store.h"
13 #include "gpg-interface.h"
14 #include "mergesort.h"
15 #include "commit-slab.h"
16 #include "prio-queue.h"
17 #include "sha1-lookup.h"
18 #include "wt-status.h"
21 static struct commit_extra_header
*read_commit_extra_header_lines(const char *buf
, size_t len
, const char **);
23 int save_commit_buffer
= 1;
25 const char *commit_type
= "commit";
27 struct commit
*lookup_commit_reference_gently_the_repository(
28 const struct object_id
*oid
, int quiet
)
30 struct object
*obj
= deref_tag(parse_object(the_repository
, oid
),
35 return object_as_type(the_repository
, obj
, OBJ_COMMIT
, quiet
);
38 struct commit
*lookup_commit_reference_the_repository(const struct object_id
*oid
)
40 return lookup_commit_reference_gently(the_repository
, oid
, 0);
43 struct commit
*lookup_commit_or_die(const struct object_id
*oid
, const char *ref_name
)
45 struct commit
*c
= lookup_commit_reference(the_repository
, oid
);
47 die(_("could not parse %s"), ref_name
);
48 if (oidcmp(oid
, &c
->object
.oid
)) {
49 warning(_("%s %s is not a commit!"),
50 ref_name
, oid_to_hex(oid
));
55 struct commit
*lookup_commit_the_repository(const struct object_id
*oid
)
57 struct object
*obj
= lookup_object(the_repository
, oid
->hash
);
59 return create_object(the_repository
, oid
->hash
,
60 alloc_commit_node(the_repository
));
61 return object_as_type(the_repository
, obj
, OBJ_COMMIT
, 0);
64 struct commit
*lookup_commit_reference_by_name(const char *name
)
67 struct commit
*commit
;
69 if (get_oid_committish(name
, &oid
))
71 commit
= lookup_commit_reference(the_repository
, &oid
);
72 if (parse_commit(commit
))
77 static timestamp_t
parse_commit_date(const char *buf
, const char *tail
)
83 if (memcmp(buf
, "author", 6))
85 while (buf
< tail
&& *buf
++ != '\n')
89 if (memcmp(buf
, "committer", 9))
91 while (buf
< tail
&& *buf
++ != '>')
96 while (buf
< tail
&& *buf
++ != '\n')
100 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
101 return parse_timestamp(dateptr
, NULL
, 10);
104 static const unsigned char *commit_graft_sha1_access(size_t index
, void *table
)
106 struct commit_graft
**commit_graft_table
= table
;
107 return commit_graft_table
[index
]->oid
.hash
;
110 static int commit_graft_pos(struct repository
*r
, const unsigned char *sha1
)
112 return sha1_pos(sha1
, r
->parsed_objects
->grafts
,
113 r
->parsed_objects
->grafts_nr
,
114 commit_graft_sha1_access
);
117 int register_commit_graft(struct repository
*r
, struct commit_graft
*graft
,
120 int pos
= commit_graft_pos(r
, graft
->oid
.hash
);
126 free(r
->parsed_objects
->grafts
[pos
]);
127 r
->parsed_objects
->grafts
[pos
] = graft
;
132 ALLOC_GROW(r
->parsed_objects
->grafts
,
133 r
->parsed_objects
->grafts_nr
+ 1,
134 r
->parsed_objects
->grafts_alloc
);
135 r
->parsed_objects
->grafts_nr
++;
136 if (pos
< r
->parsed_objects
->grafts_nr
)
137 memmove(r
->parsed_objects
->grafts
+ pos
+ 1,
138 r
->parsed_objects
->grafts
+ pos
,
139 (r
->parsed_objects
->grafts_nr
- pos
- 1) *
140 sizeof(*r
->parsed_objects
->grafts
));
141 r
->parsed_objects
->grafts
[pos
] = graft
;
145 struct commit_graft
*read_graft_line(struct strbuf
*line
)
147 /* The format is just "Commit Parent1 Parent2 ...\n" */
149 const char *tail
= NULL
;
150 struct commit_graft
*graft
= NULL
;
151 struct object_id dummy_oid
, *oid
;
154 if (!line
->len
|| line
->buf
[0] == '#')
157 * phase 0 verifies line, counts hashes in line and allocates graft
158 * phase 1 fills graft
160 for (phase
= 0; phase
< 2; phase
++) {
161 oid
= graft
? &graft
->oid
: &dummy_oid
;
162 if (parse_oid_hex(line
->buf
, oid
, &tail
))
164 for (i
= 0; *tail
!= '\0'; i
++) {
165 oid
= graft
? &graft
->parent
[i
] : &dummy_oid
;
166 if (!isspace(*tail
++) || parse_oid_hex(tail
, oid
, &tail
))
170 graft
= xmalloc(st_add(sizeof(*graft
),
171 st_mult(sizeof(struct object_id
), i
)));
172 graft
->nr_parent
= i
;
178 error("bad graft data: %s", line
->buf
);
183 static int read_graft_file(struct repository
*r
, const char *graft_file
)
185 FILE *fp
= fopen_or_warn(graft_file
, "r");
186 struct strbuf buf
= STRBUF_INIT
;
189 if (advice_graft_file_deprecated
)
190 advise(_("Support for <GIT_DIR>/info/grafts is deprecated\n"
191 "and will be removed in a future Git version.\n"
193 "Please use \"git replace --convert-graft-file\"\n"
194 "to convert the grafts into replace refs.\n"
196 "Turn this message off by running\n"
197 "\"git config advice.graftFileDeprecated false\""));
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
);
203 if (register_commit_graft(r
, graft
, 1))
204 error("duplicate graft data: %s", buf
.buf
);
207 strbuf_release(&buf
);
211 static void prepare_commit_graft(struct repository
*r
)
215 if (r
->parsed_objects
->commit_graft_prepared
)
217 if (!startup_info
->have_repository
)
220 graft_file
= get_graft_file(r
);
221 read_graft_file(r
, graft_file
);
222 /* make sure shallows are read */
223 is_repository_shallow(r
);
224 r
->parsed_objects
->commit_graft_prepared
= 1;
227 struct commit_graft
*lookup_commit_graft(struct repository
*r
, const struct object_id
*oid
)
230 prepare_commit_graft(r
);
231 pos
= commit_graft_pos(r
, oid
->hash
);
234 return r
->parsed_objects
->grafts
[pos
];
237 int for_each_commit_graft(each_commit_graft_fn fn
, void *cb_data
)
240 for (i
= ret
= 0; i
< the_repository
->parsed_objects
->grafts_nr
&& !ret
; i
++)
241 ret
= fn(the_repository
->parsed_objects
->grafts
[i
], cb_data
);
245 int unregister_shallow(const struct object_id
*oid
)
247 int pos
= commit_graft_pos(the_repository
, oid
->hash
);
250 if (pos
+ 1 < the_repository
->parsed_objects
->grafts_nr
)
251 MOVE_ARRAY(the_repository
->parsed_objects
->grafts
+ pos
,
252 the_repository
->parsed_objects
->grafts
+ pos
+ 1,
253 the_repository
->parsed_objects
->grafts_nr
- pos
- 1);
254 the_repository
->parsed_objects
->grafts_nr
--;
258 struct commit_buffer
{
262 define_commit_slab(buffer_slab
, struct commit_buffer
);
263 static struct buffer_slab buffer_slab
= COMMIT_SLAB_INIT(1, buffer_slab
);
265 void set_commit_buffer_the_repository(struct commit
*commit
, void *buffer
, unsigned long size
)
267 struct commit_buffer
*v
= buffer_slab_at(&buffer_slab
, commit
);
272 const void *get_cached_commit_buffer_the_repository(const struct commit
*commit
, unsigned long *sizep
)
274 struct commit_buffer
*v
= buffer_slab_peek(&buffer_slab
, commit
);
285 const void *get_commit_buffer(const struct commit
*commit
, unsigned long *sizep
)
287 const void *ret
= get_cached_commit_buffer(the_repository
, commit
, sizep
);
289 enum object_type type
;
291 ret
= read_object_file(&commit
->object
.oid
, &type
, &size
);
293 die("cannot read commit object %s",
294 oid_to_hex(&commit
->object
.oid
));
295 if (type
!= OBJ_COMMIT
)
296 die("expected commit for %s, got %s",
297 oid_to_hex(&commit
->object
.oid
), type_name(type
));
304 void unuse_commit_buffer(const struct commit
*commit
, const void *buffer
)
306 struct commit_buffer
*v
= buffer_slab_peek(&buffer_slab
, commit
);
307 if (!(v
&& v
->buffer
== buffer
))
308 free((void *)buffer
);
311 void free_commit_buffer(struct commit
*commit
)
313 struct commit_buffer
*v
= buffer_slab_peek(&buffer_slab
, commit
);
315 FREE_AND_NULL(v
->buffer
);
320 struct tree
*get_commit_tree(const struct commit
*commit
)
322 if (commit
->maybe_tree
|| !commit
->object
.parsed
)
323 return commit
->maybe_tree
;
325 if (commit
->graph_pos
== COMMIT_NOT_FROM_GRAPH
)
326 BUG("commit has NULL tree, but was not loaded from commit-graph");
328 return get_commit_tree_in_graph(commit
);
331 struct object_id
*get_commit_tree_oid(const struct commit
*commit
)
333 return &get_commit_tree(commit
)->object
.oid
;
336 void release_commit_memory(struct commit
*c
)
338 c
->maybe_tree
= NULL
;
340 free_commit_buffer(c
);
341 free_commit_list(c
->parents
);
342 /* TODO: what about commit->util? */
344 c
->object
.parsed
= 0;
347 const void *detach_commit_buffer(struct commit
*commit
, unsigned long *sizep
)
349 struct commit_buffer
*v
= buffer_slab_peek(&buffer_slab
, commit
);
366 int parse_commit_buffer_the_repository(struct commit
*item
, const void *buffer
, unsigned long size
, int check_graph
)
368 const char *tail
= buffer
;
369 const char *bufptr
= buffer
;
370 struct object_id parent
;
371 struct commit_list
**pptr
;
372 struct commit_graft
*graft
;
373 const int tree_entry_len
= GIT_SHA1_HEXSZ
+ 5;
374 const int parent_entry_len
= GIT_SHA1_HEXSZ
+ 7;
376 if (item
->object
.parsed
)
378 item
->object
.parsed
= 1;
380 if (tail
<= bufptr
+ tree_entry_len
+ 1 || memcmp(bufptr
, "tree ", 5) ||
381 bufptr
[tree_entry_len
] != '\n')
382 return error("bogus commit object %s", oid_to_hex(&item
->object
.oid
));
383 if (get_oid_hex(bufptr
+ 5, &parent
) < 0)
384 return error("bad tree pointer in commit %s",
385 oid_to_hex(&item
->object
.oid
));
386 item
->maybe_tree
= lookup_tree(the_repository
, &parent
);
387 bufptr
+= tree_entry_len
+ 1; /* "tree " + "hex sha1" + "\n" */
388 pptr
= &item
->parents
;
390 graft
= lookup_commit_graft(the_repository
, &item
->object
.oid
);
391 while (bufptr
+ parent_entry_len
< tail
&& !memcmp(bufptr
, "parent ", 7)) {
392 struct commit
*new_parent
;
394 if (tail
<= bufptr
+ parent_entry_len
+ 1 ||
395 get_oid_hex(bufptr
+ 7, &parent
) ||
396 bufptr
[parent_entry_len
] != '\n')
397 return error("bad parents in commit %s", oid_to_hex(&item
->object
.oid
));
398 bufptr
+= parent_entry_len
+ 1;
400 * The clone is shallow if nr_parent < 0, and we must
401 * not traverse its real parents even when we unhide them.
403 if (graft
&& (graft
->nr_parent
< 0 || grafts_replace_parents
))
405 new_parent
= lookup_commit(the_repository
, &parent
);
407 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
411 struct commit
*new_parent
;
412 for (i
= 0; i
< graft
->nr_parent
; i
++) {
413 new_parent
= lookup_commit(the_repository
,
417 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
420 item
->date
= parse_commit_date(bufptr
, tail
);
423 load_commit_graph_info(item
);
428 int parse_commit_gently(struct commit
*item
, int quiet_on_missing
)
430 enum object_type type
;
437 if (item
->object
.parsed
)
439 if (parse_commit_in_graph(item
))
441 buffer
= read_object_file(&item
->object
.oid
, &type
, &size
);
443 return quiet_on_missing
? -1 :
444 error("Could not read %s",
445 oid_to_hex(&item
->object
.oid
));
446 if (type
!= OBJ_COMMIT
) {
448 return error("Object %s not a commit",
449 oid_to_hex(&item
->object
.oid
));
451 ret
= parse_commit_buffer(the_repository
, item
, buffer
, size
, 0);
452 if (save_commit_buffer
&& !ret
) {
453 set_commit_buffer(the_repository
, item
, buffer
, size
);
460 void parse_commit_or_die(struct commit
*item
)
462 if (parse_commit(item
))
463 die("unable to parse commit %s",
464 item
? oid_to_hex(&item
->object
.oid
) : "(null)");
467 int find_commit_subject(const char *commit_buffer
, const char **subject
)
470 const char *p
= commit_buffer
;
472 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
475 p
= skip_blank_lines(p
+ 2);
476 eol
= strchrnul(p
, '\n');
485 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
487 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
488 new_list
->item
= item
;
489 new_list
->next
= *list_p
;
494 unsigned commit_list_count(const struct commit_list
*l
)
497 for (; l
; l
= l
->next
)
502 struct commit_list
*copy_commit_list(struct commit_list
*list
)
504 struct commit_list
*head
= NULL
;
505 struct commit_list
**pp
= &head
;
507 pp
= commit_list_append(list
->item
, pp
);
513 void free_commit_list(struct commit_list
*list
)
519 struct commit_list
* commit_list_insert_by_date(struct commit
*item
, struct commit_list
**list
)
521 struct commit_list
**pp
= list
;
522 struct commit_list
*p
;
523 while ((p
= *pp
) != NULL
) {
524 if (p
->item
->date
< item
->date
) {
529 return commit_list_insert(item
, pp
);
532 static int commit_list_compare_by_date(const void *a
, const void *b
)
534 timestamp_t a_date
= ((const struct commit_list
*)a
)->item
->date
;
535 timestamp_t b_date
= ((const struct commit_list
*)b
)->item
->date
;
543 static void *commit_list_get_next(const void *a
)
545 return ((const struct commit_list
*)a
)->next
;
548 static void commit_list_set_next(void *a
, void *next
)
550 ((struct commit_list
*)a
)->next
= next
;
553 void commit_list_sort_by_date(struct commit_list
**list
)
555 *list
= llist_mergesort(*list
, commit_list_get_next
, commit_list_set_next
,
556 commit_list_compare_by_date
);
559 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
562 struct commit
*ret
= pop_commit(list
);
563 struct commit_list
*parents
= ret
->parents
;
566 struct commit
*commit
= parents
->item
;
567 if (!parse_commit(commit
) && !(commit
->object
.flags
& mark
)) {
568 commit
->object
.flags
|= mark
;
569 commit_list_insert_by_date(commit
, list
);
571 parents
= parents
->next
;
576 static void clear_commit_marks_1(struct commit_list
**plist
,
577 struct commit
*commit
, unsigned int mark
)
580 struct commit_list
*parents
;
582 if (!(mark
& commit
->object
.flags
))
585 commit
->object
.flags
&= ~mark
;
587 parents
= commit
->parents
;
591 while ((parents
= parents
->next
))
592 commit_list_insert(parents
->item
, plist
);
594 commit
= commit
->parents
->item
;
598 void clear_commit_marks_many(int nr
, struct commit
**commit
, unsigned int mark
)
600 struct commit_list
*list
= NULL
;
603 clear_commit_marks_1(&list
, *commit
, mark
);
607 clear_commit_marks_1(&list
, pop_commit(&list
), mark
);
610 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
612 clear_commit_marks_many(1, &commit
, mark
);
615 struct commit
*pop_commit(struct commit_list
**stack
)
617 struct commit_list
*top
= *stack
;
618 struct commit
*item
= top
? top
->item
: NULL
;
628 * Topological sort support
631 /* count number of children that have not been emitted */
632 define_commit_slab(indegree_slab
, int);
634 /* record author-date for each commit object */
635 define_commit_slab(author_date_slab
, unsigned long);
637 static void record_author_date(struct author_date_slab
*author_date
,
638 struct commit
*commit
)
640 const char *buffer
= get_commit_buffer(commit
, NULL
);
641 struct ident_split ident
;
642 const char *ident_line
;
647 ident_line
= find_commit_header(buffer
, "author", &ident_len
);
649 goto fail_exit
; /* no author line */
650 if (split_ident_line(&ident
, ident_line
, ident_len
) ||
651 !ident
.date_begin
|| !ident
.date_end
)
652 goto fail_exit
; /* malformed "author" line */
654 date
= parse_timestamp(ident
.date_begin
, &date_end
, 10);
655 if (date_end
!= ident
.date_end
)
656 goto fail_exit
; /* malformed date */
657 *(author_date_slab_at(author_date
, commit
)) = date
;
660 unuse_commit_buffer(commit
, buffer
);
663 static int compare_commits_by_author_date(const void *a_
, const void *b_
,
666 const struct commit
*a
= a_
, *b
= b_
;
667 struct author_date_slab
*author_date
= cb_data
;
668 timestamp_t a_date
= *(author_date_slab_at(author_date
, a
));
669 timestamp_t b_date
= *(author_date_slab_at(author_date
, b
));
671 /* newer commits with larger date first */
674 else if (a_date
> b_date
)
679 int compare_commits_by_gen_then_commit_date(const void *a_
, const void *b_
, void *unused
)
681 const struct commit
*a
= a_
, *b
= b_
;
683 /* newer commits first */
684 if (a
->generation
< b
->generation
)
686 else if (a
->generation
> b
->generation
)
689 /* use date as a heuristic when generations are equal */
690 if (a
->date
< b
->date
)
692 else if (a
->date
> b
->date
)
697 int compare_commits_by_commit_date(const void *a_
, const void *b_
, void *unused
)
699 const struct commit
*a
= a_
, *b
= b_
;
700 /* newer commits with larger date first */
701 if (a
->date
< b
->date
)
703 else if (a
->date
> b
->date
)
709 * Performs an in-place topological sort on the list supplied.
711 void sort_in_topological_order(struct commit_list
**list
, enum rev_sort_order sort_order
)
713 struct commit_list
*next
, *orig
= *list
;
714 struct commit_list
**pptr
;
715 struct indegree_slab indegree
;
716 struct prio_queue queue
;
717 struct commit
*commit
;
718 struct author_date_slab author_date
;
724 init_indegree_slab(&indegree
);
725 memset(&queue
, '\0', sizeof(queue
));
727 switch (sort_order
) {
728 default: /* REV_SORT_IN_GRAPH_ORDER */
729 queue
.compare
= NULL
;
731 case REV_SORT_BY_COMMIT_DATE
:
732 queue
.compare
= compare_commits_by_commit_date
;
734 case REV_SORT_BY_AUTHOR_DATE
:
735 init_author_date_slab(&author_date
);
736 queue
.compare
= compare_commits_by_author_date
;
737 queue
.cb_data
= &author_date
;
741 /* Mark them and clear the indegree */
742 for (next
= orig
; next
; next
= next
->next
) {
743 struct commit
*commit
= next
->item
;
744 *(indegree_slab_at(&indegree
, commit
)) = 1;
745 /* also record the author dates, if needed */
746 if (sort_order
== REV_SORT_BY_AUTHOR_DATE
)
747 record_author_date(&author_date
, commit
);
750 /* update the indegree */
751 for (next
= orig
; next
; next
= next
->next
) {
752 struct commit_list
*parents
= next
->item
->parents
;
754 struct commit
*parent
= parents
->item
;
755 int *pi
= indegree_slab_at(&indegree
, parent
);
759 parents
= parents
->next
;
766 * tips are nodes not reachable from any other node in the list
768 * the tips serve as a starting set for the work queue.
770 for (next
= orig
; next
; next
= next
->next
) {
771 struct commit
*commit
= next
->item
;
773 if (*(indegree_slab_at(&indegree
, commit
)) == 1)
774 prio_queue_put(&queue
, commit
);
778 * This is unfortunate; the initial tips need to be shown
779 * in the order given from the revision traversal machinery.
781 if (sort_order
== REV_SORT_IN_GRAPH_ORDER
)
782 prio_queue_reverse(&queue
);
784 /* We no longer need the commit list */
785 free_commit_list(orig
);
789 while ((commit
= prio_queue_get(&queue
)) != NULL
) {
790 struct commit_list
*parents
;
792 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
793 struct commit
*parent
= parents
->item
;
794 int *pi
= indegree_slab_at(&indegree
, parent
);
800 * parents are only enqueued for emission
801 * when all their children have been emitted thereby
802 * guaranteeing topological order.
805 prio_queue_put(&queue
, parent
);
808 * all children of commit have already been
809 * emitted. we can emit it now.
811 *(indegree_slab_at(&indegree
, commit
)) = 0;
813 pptr
= &commit_list_insert(commit
, pptr
)->next
;
816 clear_indegree_slab(&indegree
);
817 clear_prio_queue(&queue
);
818 if (sort_order
== REV_SORT_BY_AUTHOR_DATE
)
819 clear_author_date_slab(&author_date
);
822 /* merge-base stuff */
824 /* Remember to update object flag allocation in object.h */
825 #define PARENT1 (1u<<16)
826 #define PARENT2 (1u<<17)
827 #define STALE (1u<<18)
828 #define RESULT (1u<<19)
830 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
832 static int queue_has_nonstale(struct prio_queue
*queue
)
835 for (i
= 0; i
< queue
->nr
; i
++) {
836 struct commit
*commit
= queue
->array
[i
].data
;
837 if (!(commit
->object
.flags
& STALE
))
843 /* all input commits in one and twos[] must have been parsed! */
844 static struct commit_list
*paint_down_to_common(struct commit
*one
, int n
,
845 struct commit
**twos
,
848 struct prio_queue queue
= { compare_commits_by_gen_then_commit_date
};
849 struct commit_list
*result
= NULL
;
851 uint32_t last_gen
= GENERATION_NUMBER_INFINITY
;
853 one
->object
.flags
|= PARENT1
;
855 commit_list_append(one
, &result
);
858 prio_queue_put(&queue
, one
);
860 for (i
= 0; i
< n
; i
++) {
861 twos
[i
]->object
.flags
|= PARENT2
;
862 prio_queue_put(&queue
, twos
[i
]);
865 while (queue_has_nonstale(&queue
)) {
866 struct commit
*commit
= prio_queue_get(&queue
);
867 struct commit_list
*parents
;
870 if (commit
->generation
> last_gen
)
871 BUG("bad generation skip %8x > %8x at %s",
872 commit
->generation
, last_gen
,
873 oid_to_hex(&commit
->object
.oid
));
874 last_gen
= commit
->generation
;
876 if (commit
->generation
< min_generation
)
879 flags
= commit
->object
.flags
& (PARENT1
| PARENT2
| STALE
);
880 if (flags
== (PARENT1
| PARENT2
)) {
881 if (!(commit
->object
.flags
& RESULT
)) {
882 commit
->object
.flags
|= RESULT
;
883 commit_list_insert_by_date(commit
, &result
);
885 /* Mark parents of a found merge stale */
888 parents
= commit
->parents
;
890 struct commit
*p
= parents
->item
;
891 parents
= parents
->next
;
892 if ((p
->object
.flags
& flags
) == flags
)
896 p
->object
.flags
|= flags
;
897 prio_queue_put(&queue
, p
);
901 clear_prio_queue(&queue
);
905 static struct commit_list
*merge_bases_many(struct commit
*one
, int n
, struct commit
**twos
)
907 struct commit_list
*list
= NULL
;
908 struct commit_list
*result
= NULL
;
911 for (i
= 0; i
< n
; i
++) {
914 * We do not mark this even with RESULT so we do not
915 * have to clean it up.
917 return commit_list_insert(one
, &result
);
920 if (parse_commit(one
))
922 for (i
= 0; i
< n
; i
++) {
923 if (parse_commit(twos
[i
]))
927 list
= paint_down_to_common(one
, n
, twos
, 0);
930 struct commit
*commit
= pop_commit(&list
);
931 if (!(commit
->object
.flags
& STALE
))
932 commit_list_insert_by_date(commit
, &result
);
937 struct commit_list
*get_octopus_merge_bases(struct commit_list
*in
)
939 struct commit_list
*i
, *j
, *k
, *ret
= NULL
;
944 commit_list_insert(in
->item
, &ret
);
946 for (i
= in
->next
; i
; i
= i
->next
) {
947 struct commit_list
*new_commits
= NULL
, *end
= NULL
;
949 for (j
= ret
; j
; j
= j
->next
) {
950 struct commit_list
*bases
;
951 bases
= get_merge_bases(i
->item
, j
->item
);
956 for (k
= bases
; k
; k
= k
->next
)
964 static int remove_redundant(struct commit
**array
, int cnt
)
967 * Some commit in the array may be an ancestor of
968 * another commit. Move such commit to the end of
969 * the array, and return the number of commits that
970 * are independent from each other.
972 struct commit
**work
;
973 unsigned char *redundant
;
977 work
= xcalloc(cnt
, sizeof(*work
));
978 redundant
= xcalloc(cnt
, 1);
979 ALLOC_ARRAY(filled_index
, cnt
- 1);
981 for (i
= 0; i
< cnt
; i
++)
982 parse_commit(array
[i
]);
983 for (i
= 0; i
< cnt
; i
++) {
984 struct commit_list
*common
;
985 uint32_t min_generation
= array
[i
]->generation
;
989 for (j
= filled
= 0; j
< cnt
; j
++) {
990 if (i
== j
|| redundant
[j
])
992 filled_index
[filled
] = j
;
993 work
[filled
++] = array
[j
];
995 if (array
[j
]->generation
< min_generation
)
996 min_generation
= array
[j
]->generation
;
998 common
= paint_down_to_common(array
[i
], filled
, work
,
1000 if (array
[i
]->object
.flags
& PARENT2
)
1002 for (j
= 0; j
< filled
; j
++)
1003 if (work
[j
]->object
.flags
& PARENT1
)
1004 redundant
[filled_index
[j
]] = 1;
1005 clear_commit_marks(array
[i
], all_flags
);
1006 clear_commit_marks_many(filled
, work
, all_flags
);
1007 free_commit_list(common
);
1010 /* Now collect the result */
1011 COPY_ARRAY(work
, array
, cnt
);
1012 for (i
= filled
= 0; i
< cnt
; i
++)
1014 array
[filled
++] = work
[i
];
1015 for (j
= filled
, i
= 0; i
< cnt
; i
++)
1017 array
[j
++] = work
[i
];
1024 static struct commit_list
*get_merge_bases_many_0(struct commit
*one
,
1026 struct commit
**twos
,
1029 struct commit_list
*list
;
1030 struct commit
**rslt
;
1031 struct commit_list
*result
;
1034 result
= merge_bases_many(one
, n
, twos
);
1035 for (i
= 0; i
< n
; i
++) {
1039 if (!result
|| !result
->next
) {
1041 clear_commit_marks(one
, all_flags
);
1042 clear_commit_marks_many(n
, twos
, all_flags
);
1047 /* There are more than one */
1048 cnt
= commit_list_count(result
);
1049 rslt
= xcalloc(cnt
, sizeof(*rslt
));
1050 for (list
= result
, i
= 0; list
; list
= list
->next
)
1051 rslt
[i
++] = list
->item
;
1052 free_commit_list(result
);
1054 clear_commit_marks(one
, all_flags
);
1055 clear_commit_marks_many(n
, twos
, all_flags
);
1057 cnt
= remove_redundant(rslt
, cnt
);
1059 for (i
= 0; i
< cnt
; i
++)
1060 commit_list_insert_by_date(rslt
[i
], &result
);
1065 struct commit_list
*get_merge_bases_many(struct commit
*one
,
1067 struct commit
**twos
)
1069 return get_merge_bases_many_0(one
, n
, twos
, 1);
1072 struct commit_list
*get_merge_bases_many_dirty(struct commit
*one
,
1074 struct commit
**twos
)
1076 return get_merge_bases_many_0(one
, n
, twos
, 0);
1079 struct commit_list
*get_merge_bases(struct commit
*one
, struct commit
*two
)
1081 return get_merge_bases_many_0(one
, 1, &two
, 1);
1085 * Is "commit" a descendant of one of the elements on the "with_commit" list?
1087 int is_descendant_of(struct commit
*commit
, struct commit_list
*with_commit
)
1091 while (with_commit
) {
1092 struct commit
*other
;
1094 other
= with_commit
->item
;
1095 with_commit
= with_commit
->next
;
1096 if (in_merge_bases(other
, commit
))
1103 * Is "commit" an ancestor of one of the "references"?
1105 int in_merge_bases_many(struct commit
*commit
, int nr_reference
, struct commit
**reference
)
1107 struct commit_list
*bases
;
1109 uint32_t min_generation
= GENERATION_NUMBER_INFINITY
;
1111 if (parse_commit(commit
))
1113 for (i
= 0; i
< nr_reference
; i
++) {
1114 if (parse_commit(reference
[i
]))
1116 if (reference
[i
]->generation
< min_generation
)
1117 min_generation
= reference
[i
]->generation
;
1120 if (commit
->generation
> min_generation
)
1123 bases
= paint_down_to_common(commit
, nr_reference
, reference
, commit
->generation
);
1124 if (commit
->object
.flags
& PARENT2
)
1126 clear_commit_marks(commit
, all_flags
);
1127 clear_commit_marks_many(nr_reference
, reference
, all_flags
);
1128 free_commit_list(bases
);
1133 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
1135 int in_merge_bases(struct commit
*commit
, struct commit
*reference
)
1137 return in_merge_bases_many(commit
, 1, &reference
);
1140 struct commit_list
*reduce_heads(struct commit_list
*heads
)
1142 struct commit_list
*p
;
1143 struct commit_list
*result
= NULL
, **tail
= &result
;
1144 struct commit
**array
;
1151 for (p
= heads
; p
; p
= p
->next
)
1152 p
->item
->object
.flags
&= ~STALE
;
1153 for (p
= heads
, num_head
= 0; p
; p
= p
->next
) {
1154 if (p
->item
->object
.flags
& STALE
)
1156 p
->item
->object
.flags
|= STALE
;
1159 array
= xcalloc(num_head
, sizeof(*array
));
1160 for (p
= heads
, i
= 0; p
; p
= p
->next
) {
1161 if (p
->item
->object
.flags
& STALE
) {
1162 array
[i
++] = p
->item
;
1163 p
->item
->object
.flags
&= ~STALE
;
1166 num_head
= remove_redundant(array
, num_head
);
1167 for (i
= 0; i
< num_head
; i
++)
1168 tail
= &commit_list_insert(array
[i
], tail
)->next
;
1173 void reduce_heads_replace(struct commit_list
**heads
)
1175 struct commit_list
*result
= reduce_heads(*heads
);
1176 free_commit_list(*heads
);
1180 static const char gpg_sig_header
[] = "gpgsig";
1181 static const int gpg_sig_header_len
= sizeof(gpg_sig_header
) - 1;
1183 static int do_sign_commit(struct strbuf
*buf
, const char *keyid
)
1185 struct strbuf sig
= STRBUF_INIT
;
1186 int inspos
, copypos
;
1189 /* find the end of the header */
1190 eoh
= strstr(buf
->buf
, "\n\n");
1194 inspos
= eoh
- buf
->buf
+ 1;
1196 if (!keyid
|| !*keyid
)
1197 keyid
= get_signing_key();
1198 if (sign_buffer(buf
, &sig
, keyid
)) {
1199 strbuf_release(&sig
);
1203 for (copypos
= 0; sig
.buf
[copypos
]; ) {
1204 const char *bol
= sig
.buf
+ copypos
;
1205 const char *eol
= strchrnul(bol
, '\n');
1206 int len
= (eol
- bol
) + !!*eol
;
1209 strbuf_insert(buf
, inspos
, gpg_sig_header
, gpg_sig_header_len
);
1210 inspos
+= gpg_sig_header_len
;
1212 strbuf_insert(buf
, inspos
++, " ", 1);
1213 strbuf_insert(buf
, inspos
, bol
, len
);
1217 strbuf_release(&sig
);
1221 int parse_signed_commit(const struct commit
*commit
,
1222 struct strbuf
*payload
, struct strbuf
*signature
)
1226 const char *buffer
= get_commit_buffer(commit
, &size
);
1227 int in_signature
, saw_signature
= -1;
1228 const char *line
, *tail
;
1231 tail
= buffer
+ size
;
1234 while (line
< tail
) {
1235 const char *sig
= NULL
;
1236 const char *next
= memchr(line
, '\n', tail
- line
);
1238 next
= next
? next
+ 1 : tail
;
1239 if (in_signature
&& line
[0] == ' ')
1241 else if (starts_with(line
, gpg_sig_header
) &&
1242 line
[gpg_sig_header_len
] == ' ')
1243 sig
= line
+ gpg_sig_header_len
+ 1;
1245 strbuf_add(signature
, sig
, next
- sig
);
1250 /* dump the whole remainder of the buffer */
1252 strbuf_add(payload
, line
, next
- line
);
1257 unuse_commit_buffer(commit
, buffer
);
1258 return saw_signature
;
1261 int remove_signature(struct strbuf
*buf
)
1263 const char *line
= buf
->buf
;
1264 const char *tail
= buf
->buf
+ buf
->len
;
1265 int in_signature
= 0;
1266 const char *sig_start
= NULL
;
1267 const char *sig_end
= NULL
;
1269 while (line
< tail
) {
1270 const char *next
= memchr(line
, '\n', tail
- line
);
1271 next
= next
? next
+ 1 : tail
;
1273 if (in_signature
&& line
[0] == ' ')
1275 else if (starts_with(line
, gpg_sig_header
) &&
1276 line
[gpg_sig_header_len
] == ' ') {
1282 /* dump the whole remainder of the buffer */
1290 strbuf_remove(buf
, sig_start
- buf
->buf
, sig_end
- sig_start
);
1292 return sig_start
!= NULL
;
1295 static void handle_signed_tag(struct commit
*parent
, struct commit_extra_header
***tail
)
1297 struct merge_remote_desc
*desc
;
1298 struct commit_extra_header
*mergetag
;
1300 unsigned long size
, len
;
1301 enum object_type type
;
1303 desc
= merge_remote_util(parent
);
1304 if (!desc
|| !desc
->obj
)
1306 buf
= read_object_file(&desc
->obj
->oid
, &type
, &size
);
1307 if (!buf
|| type
!= OBJ_TAG
)
1309 len
= parse_signature(buf
, size
);
1313 * We could verify this signature and either omit the tag when
1314 * it does not validate, but the integrator may not have the
1315 * public key of the signer of the tag he is merging, while a
1316 * later auditor may have it while auditing, so let's not run
1317 * verify-signed-buffer here for now...
1319 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1320 * warn("warning: signed tag unverified.");
1322 mergetag
= xcalloc(1, sizeof(*mergetag
));
1323 mergetag
->key
= xstrdup("mergetag");
1324 mergetag
->value
= buf
;
1325 mergetag
->len
= size
;
1328 *tail
= &mergetag
->next
;
1335 int check_commit_signature(const struct commit
*commit
, struct signature_check
*sigc
)
1337 struct strbuf payload
= STRBUF_INIT
;
1338 struct strbuf signature
= STRBUF_INIT
;
1343 if (parse_signed_commit(commit
, &payload
, &signature
) <= 0)
1345 ret
= check_signature(payload
.buf
, payload
.len
, signature
.buf
,
1346 signature
.len
, sigc
);
1349 strbuf_release(&payload
);
1350 strbuf_release(&signature
);
1357 void append_merge_tag_headers(struct commit_list
*parents
,
1358 struct commit_extra_header
***tail
)
1361 struct commit
*parent
= parents
->item
;
1362 handle_signed_tag(parent
, tail
);
1363 parents
= parents
->next
;
1367 static void add_extra_header(struct strbuf
*buffer
,
1368 struct commit_extra_header
*extra
)
1370 strbuf_addstr(buffer
, extra
->key
);
1372 strbuf_add_lines(buffer
, " ", extra
->value
, extra
->len
);
1374 strbuf_addch(buffer
, '\n');
1377 struct commit_extra_header
*read_commit_extra_headers(struct commit
*commit
,
1378 const char **exclude
)
1380 struct commit_extra_header
*extra
= NULL
;
1382 const char *buffer
= get_commit_buffer(commit
, &size
);
1383 extra
= read_commit_extra_header_lines(buffer
, size
, exclude
);
1384 unuse_commit_buffer(commit
, buffer
);
1388 int for_each_mergetag(each_mergetag_fn fn
, struct commit
*commit
, void *data
)
1390 struct commit_extra_header
*extra
, *to_free
;
1393 to_free
= read_commit_extra_headers(commit
, NULL
);
1394 for (extra
= to_free
; !res
&& extra
; extra
= extra
->next
) {
1395 if (strcmp(extra
->key
, "mergetag"))
1396 continue; /* not a merge tag */
1397 res
= fn(commit
, extra
, data
);
1399 free_commit_extra_headers(to_free
);
1403 static inline int standard_header_field(const char *field
, size_t len
)
1405 return ((len
== 4 && !memcmp(field
, "tree", 4)) ||
1406 (len
== 6 && !memcmp(field
, "parent", 6)) ||
1407 (len
== 6 && !memcmp(field
, "author", 6)) ||
1408 (len
== 9 && !memcmp(field
, "committer", 9)) ||
1409 (len
== 8 && !memcmp(field
, "encoding", 8)));
1412 static int excluded_header_field(const char *field
, size_t len
, const char **exclude
)
1418 size_t xlen
= strlen(*exclude
);
1419 if (len
== xlen
&& !memcmp(field
, *exclude
, xlen
))
1426 static struct commit_extra_header
*read_commit_extra_header_lines(
1427 const char *buffer
, size_t size
,
1428 const char **exclude
)
1430 struct commit_extra_header
*extra
= NULL
, **tail
= &extra
, *it
= NULL
;
1431 const char *line
, *next
, *eof
, *eob
;
1432 struct strbuf buf
= STRBUF_INIT
;
1434 for (line
= buffer
, eob
= line
+ size
;
1435 line
< eob
&& *line
!= '\n';
1437 next
= memchr(line
, '\n', eob
- line
);
1438 next
= next
? next
+ 1 : eob
;
1442 strbuf_add(&buf
, line
+ 1, next
- (line
+ 1));
1446 it
->value
= strbuf_detach(&buf
, &it
->len
);
1450 eof
= memchr(line
, ' ', next
- line
);
1453 else if (standard_header_field(line
, eof
- line
) ||
1454 excluded_header_field(line
, eof
- line
, exclude
))
1457 it
= xcalloc(1, sizeof(*it
));
1458 it
->key
= xmemdupz(line
, eof
-line
);
1462 strbuf_add(&buf
, eof
+ 1, next
- (eof
+ 1));
1465 it
->value
= strbuf_detach(&buf
, &it
->len
);
1469 void free_commit_extra_headers(struct commit_extra_header
*extra
)
1472 struct commit_extra_header
*next
= extra
->next
;
1480 int commit_tree(const char *msg
, size_t msg_len
, const struct object_id
*tree
,
1481 struct commit_list
*parents
, struct object_id
*ret
,
1482 const char *author
, const char *sign_commit
)
1484 struct commit_extra_header
*extra
= NULL
, **tail
= &extra
;
1487 append_merge_tag_headers(parents
, &tail
);
1488 result
= commit_tree_extended(msg
, msg_len
, tree
, parents
, ret
,
1489 author
, sign_commit
, extra
);
1490 free_commit_extra_headers(extra
);
1494 static int find_invalid_utf8(const char *buf
, int len
)
1497 static const unsigned int max_codepoint
[] = {
1498 0x7f, 0x7ff, 0xffff, 0x10ffff
1502 unsigned char c
= *buf
++;
1503 int bytes
, bad_offset
;
1504 unsigned int codepoint
;
1505 unsigned int min_val
, max_val
;
1510 /* Simple US-ASCII? No worries. */
1514 bad_offset
= offset
-1;
1517 * Count how many more high bits set: that's how
1518 * many more bytes this sequence should have.
1527 * Must be between 1 and 3 more bytes. Longer sequences result in
1528 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1530 if (bytes
< 1 || 3 < bytes
)
1533 /* Do we *have* that many bytes? */
1538 * Place the encoded bits at the bottom of the value and compute the
1541 codepoint
= (c
& 0x7f) >> bytes
;
1542 min_val
= max_codepoint
[bytes
-1] + 1;
1543 max_val
= max_codepoint
[bytes
];
1548 /* And verify that they are good continuation bytes */
1551 codepoint
|= *buf
& 0x3f;
1552 if ((*buf
++ & 0xc0) != 0x80)
1556 /* Reject codepoints that are out of range for the sequence length. */
1557 if (codepoint
< min_val
|| codepoint
> max_val
)
1559 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1560 if ((codepoint
& 0x1ff800) == 0xd800)
1562 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1563 if ((codepoint
& 0xfffe) == 0xfffe)
1565 /* So are anything in the range U+FDD0..U+FDEF. */
1566 if (codepoint
>= 0xfdd0 && codepoint
<= 0xfdef)
1573 * This verifies that the buffer is in proper utf8 format.
1575 * If it isn't, it assumes any non-utf8 characters are Latin1,
1576 * and does the conversion.
1578 static int verify_utf8(struct strbuf
*buf
)
1586 unsigned char replace
[2];
1588 bad
= find_invalid_utf8(buf
->buf
+ pos
, buf
->len
- pos
);
1594 strbuf_remove(buf
, pos
, 1);
1596 /* We know 'c' must be in the range 128-255 */
1597 replace
[0] = 0xc0 + (c
>> 6);
1598 replace
[1] = 0x80 + (c
& 0x3f);
1599 strbuf_insert(buf
, pos
, replace
, 2);
1604 static const char commit_utf8_warn
[] =
1605 N_("Warning: commit message did not conform to UTF-8.\n"
1606 "You may want to amend it after fixing the message, or set the config\n"
1607 "variable i18n.commitencoding to the encoding your project uses.\n");
1609 int commit_tree_extended(const char *msg
, size_t msg_len
,
1610 const struct object_id
*tree
,
1611 struct commit_list
*parents
, struct object_id
*ret
,
1612 const char *author
, const char *sign_commit
,
1613 struct commit_extra_header
*extra
)
1616 int encoding_is_utf8
;
1617 struct strbuf buffer
;
1619 assert_oid_type(tree
, OBJ_TREE
);
1621 if (memchr(msg
, '\0', msg_len
))
1622 return error("a NUL byte in commit log message not allowed.");
1624 /* Not having i18n.commitencoding is the same as having utf-8 */
1625 encoding_is_utf8
= is_encoding_utf8(git_commit_encoding
);
1627 strbuf_init(&buffer
, 8192); /* should avoid reallocs for the headers */
1628 strbuf_addf(&buffer
, "tree %s\n", oid_to_hex(tree
));
1631 * NOTE! This ordering means that the same exact tree merged with a
1632 * different order of parents will be a _different_ changeset even
1633 * if everything else stays the same.
1636 struct commit
*parent
= pop_commit(&parents
);
1637 strbuf_addf(&buffer
, "parent %s\n",
1638 oid_to_hex(&parent
->object
.oid
));
1641 /* Person/date information */
1643 author
= git_author_info(IDENT_STRICT
);
1644 strbuf_addf(&buffer
, "author %s\n", author
);
1645 strbuf_addf(&buffer
, "committer %s\n", git_committer_info(IDENT_STRICT
));
1646 if (!encoding_is_utf8
)
1647 strbuf_addf(&buffer
, "encoding %s\n", git_commit_encoding
);
1650 add_extra_header(&buffer
, extra
);
1651 extra
= extra
->next
;
1653 strbuf_addch(&buffer
, '\n');
1655 /* And add the comment */
1656 strbuf_add(&buffer
, msg
, msg_len
);
1658 /* And check the encoding */
1659 if (encoding_is_utf8
&& !verify_utf8(&buffer
))
1660 fprintf(stderr
, _(commit_utf8_warn
));
1662 if (sign_commit
&& do_sign_commit(&buffer
, sign_commit
)) {
1667 result
= write_object_file(buffer
.buf
, buffer
.len
, commit_type
, ret
);
1669 strbuf_release(&buffer
);
1673 define_commit_slab(merge_desc_slab
, struct merge_remote_desc
*);
1674 static struct merge_desc_slab merge_desc_slab
= COMMIT_SLAB_INIT(1, merge_desc_slab
);
1676 struct merge_remote_desc
*merge_remote_util(struct commit
*commit
)
1678 return *merge_desc_slab_at(&merge_desc_slab
, commit
);
1681 void set_merge_remote_desc(struct commit
*commit
,
1682 const char *name
, struct object
*obj
)
1684 struct merge_remote_desc
*desc
;
1685 FLEX_ALLOC_STR(desc
, name
, name
);
1687 *merge_desc_slab_at(&merge_desc_slab
, commit
) = desc
;
1690 struct commit
*get_merge_parent(const char *name
)
1693 struct commit
*commit
;
1694 struct object_id oid
;
1695 if (get_oid(name
, &oid
))
1697 obj
= parse_object(the_repository
, &oid
);
1698 commit
= (struct commit
*)peel_to_type(name
, 0, obj
, OBJ_COMMIT
);
1699 if (commit
&& !merge_remote_util(commit
))
1700 set_merge_remote_desc(commit
, name
, obj
);
1705 * Append a commit to the end of the commit_list.
1707 * next starts by pointing to the variable that holds the head of an
1708 * empty commit_list, and is updated to point to the "next" field of
1709 * the last item on the list as new commits are appended.
1713 * struct commit_list *list;
1714 * struct commit_list **next = &list;
1716 * next = commit_list_append(c1, next);
1717 * next = commit_list_append(c2, next);
1718 * assert(commit_list_count(list) == 2);
1721 struct commit_list
**commit_list_append(struct commit
*commit
,
1722 struct commit_list
**next
)
1724 struct commit_list
*new_commit
= xmalloc(sizeof(struct commit_list
));
1725 new_commit
->item
= commit
;
1727 new_commit
->next
= NULL
;
1728 return &new_commit
->next
;
1731 const char *find_commit_header(const char *msg
, const char *key
, size_t *out_len
)
1733 int key_len
= strlen(key
);
1734 const char *line
= msg
;
1737 const char *eol
= strchrnul(line
, '\n');
1742 if (eol
- line
> key_len
&&
1743 !strncmp(line
, key
, key_len
) &&
1744 line
[key_len
] == ' ') {
1745 *out_len
= eol
- line
- key_len
- 1;
1746 return line
+ key_len
+ 1;
1748 line
= *eol
? eol
+ 1 : NULL
;
1754 * Inspect the given string and determine the true "end" of the log message, in
1755 * order to find where to put a new Signed-off-by: line. Ignored are
1756 * trailing comment lines and blank lines. To support "git commit -s
1757 * --amend" on an existing commit, we also ignore "Conflicts:". To
1758 * support "git commit -v", we truncate at cut lines.
1760 * Returns the number of bytes from the tail to ignore, to be fed as
1761 * the second parameter to append_signoff().
1763 int ignore_non_trailer(const char *buf
, size_t len
)
1767 int in_old_conflicts_block
= 0;
1768 size_t cutoff
= wt_status_locate_end(buf
, len
);
1770 while (bol
< cutoff
) {
1771 const char *next_line
= memchr(buf
+ bol
, '\n', len
- bol
);
1774 next_line
= buf
+ len
;
1778 if (buf
[bol
] == comment_line_char
|| buf
[bol
] == '\n') {
1779 /* is this the first of the run of comments? */
1782 /* otherwise, it is just continuing */
1783 } else if (starts_with(buf
+ bol
, "Conflicts:\n")) {
1784 in_old_conflicts_block
= 1;
1787 } else if (in_old_conflicts_block
&& buf
[bol
] == '\t') {
1788 ; /* a pathname in the conflicts block */
1790 /* the previous was not trailing comment */
1792 in_old_conflicts_block
= 0;
1794 bol
= next_line
- buf
;
1796 return boc
? len
- boc
: len
- cutoff
;