4 #include "commit-graph.h"
7 #include "repository.h"
8 #include "object-store.h"
15 #include "gpg-interface.h"
16 #include "mergesort.h"
17 #include "commit-slab.h"
18 #include "prio-queue.h"
19 #include "hash-lookup.h"
20 #include "wt-status.h"
23 #include "commit-reach.h"
24 #include "run-command.h"
28 static struct commit_extra_header
*read_commit_extra_header_lines(const char *buf
, size_t len
, const char **);
30 int save_commit_buffer
= 1;
31 int no_graft_file_deprecated_advice
;
33 const char *commit_type
= "commit";
35 struct commit
*lookup_commit_reference_gently(struct repository
*r
,
36 const struct object_id
*oid
, int quiet
)
38 struct object
*obj
= deref_tag(r
,
44 return object_as_type(obj
, OBJ_COMMIT
, quiet
);
47 struct commit
*lookup_commit_reference(struct repository
*r
, const struct object_id
*oid
)
49 return lookup_commit_reference_gently(r
, oid
, 0);
52 struct commit
*lookup_commit_or_die(const struct object_id
*oid
, const char *ref_name
)
54 struct commit
*c
= lookup_commit_reference(the_repository
, oid
);
56 die(_("could not parse %s"), ref_name
);
57 if (!oideq(oid
, &c
->object
.oid
)) {
58 warning(_("%s %s is not a commit!"),
59 ref_name
, oid_to_hex(oid
));
64 struct commit
*lookup_commit_object(struct repository
*r
,
65 const struct object_id
*oid
)
67 struct object
*obj
= parse_object(r
, oid
);
68 return obj
? object_as_type(obj
, OBJ_COMMIT
, 0) : NULL
;
72 struct commit
*lookup_commit(struct repository
*r
, const struct object_id
*oid
)
74 struct object
*obj
= lookup_object(r
, oid
);
76 return create_object(r
, oid
, alloc_commit_node(r
));
77 return object_as_type(obj
, OBJ_COMMIT
, 0);
80 struct commit
*lookup_commit_reference_by_name(const char *name
)
83 struct commit
*commit
;
85 if (get_oid_committish(name
, &oid
))
87 commit
= lookup_commit_reference(the_repository
, &oid
);
88 if (parse_commit(commit
))
93 static timestamp_t
parse_commit_date(const char *buf
, const char *tail
)
99 if (memcmp(buf
, "author", 6))
101 while (buf
< tail
&& *buf
++ != '\n')
105 if (memcmp(buf
, "committer", 9))
107 while (buf
< tail
&& *buf
++ != '>')
112 while (buf
< tail
&& *buf
++ != '\n')
116 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
117 return parse_timestamp(dateptr
, NULL
, 10);
120 static const struct object_id
*commit_graft_oid_access(size_t index
, const void *table
)
122 const struct commit_graft
* const *commit_graft_table
= table
;
123 return &commit_graft_table
[index
]->oid
;
126 int commit_graft_pos(struct repository
*r
, const struct object_id
*oid
)
128 return oid_pos(oid
, r
->parsed_objects
->grafts
,
129 r
->parsed_objects
->grafts_nr
,
130 commit_graft_oid_access
);
133 static void unparse_commit(struct repository
*r
, const struct object_id
*oid
)
135 struct commit
*c
= lookup_commit(r
, oid
);
137 if (!c
->object
.parsed
)
139 free_commit_list(c
->parents
);
141 c
->object
.parsed
= 0;
144 int register_commit_graft(struct repository
*r
, struct commit_graft
*graft
,
147 int pos
= commit_graft_pos(r
, &graft
->oid
);
153 free(r
->parsed_objects
->grafts
[pos
]);
154 r
->parsed_objects
->grafts
[pos
] = graft
;
159 ALLOC_GROW(r
->parsed_objects
->grafts
,
160 r
->parsed_objects
->grafts_nr
+ 1,
161 r
->parsed_objects
->grafts_alloc
);
162 r
->parsed_objects
->grafts_nr
++;
163 if (pos
< r
->parsed_objects
->grafts_nr
)
164 memmove(r
->parsed_objects
->grafts
+ pos
+ 1,
165 r
->parsed_objects
->grafts
+ pos
,
166 (r
->parsed_objects
->grafts_nr
- pos
- 1) *
167 sizeof(*r
->parsed_objects
->grafts
));
168 r
->parsed_objects
->grafts
[pos
] = graft
;
169 unparse_commit(r
, &graft
->oid
);
173 struct commit_graft
*read_graft_line(struct strbuf
*line
)
175 /* The format is just "Commit Parent1 Parent2 ...\n" */
177 const char *tail
= NULL
;
178 struct commit_graft
*graft
= NULL
;
179 struct object_id dummy_oid
, *oid
;
182 if (!line
->len
|| line
->buf
[0] == '#')
185 * phase 0 verifies line, counts hashes in line and allocates graft
186 * phase 1 fills graft
188 for (phase
= 0; phase
< 2; phase
++) {
189 oid
= graft
? &graft
->oid
: &dummy_oid
;
190 if (parse_oid_hex(line
->buf
, oid
, &tail
))
192 for (i
= 0; *tail
!= '\0'; i
++) {
193 oid
= graft
? &graft
->parent
[i
] : &dummy_oid
;
194 if (!isspace(*tail
++) || parse_oid_hex(tail
, oid
, &tail
))
198 graft
= xmalloc(st_add(sizeof(*graft
),
199 st_mult(sizeof(struct object_id
), i
)));
200 graft
->nr_parent
= i
;
206 error("bad graft data: %s", line
->buf
);
211 static int read_graft_file(struct repository
*r
, const char *graft_file
)
213 FILE *fp
= fopen_or_warn(graft_file
, "r");
214 struct strbuf buf
= STRBUF_INIT
;
217 if (!no_graft_file_deprecated_advice
&&
218 advice_enabled(ADVICE_GRAFT_FILE_DEPRECATED
))
219 advise(_("Support for <GIT_DIR>/info/grafts is deprecated\n"
220 "and will be removed in a future Git version.\n"
222 "Please use \"git replace --convert-graft-file\"\n"
223 "to convert the grafts into replace refs.\n"
225 "Turn this message off by running\n"
226 "\"git config advice.graftFileDeprecated false\""));
227 while (!strbuf_getwholeline(&buf
, fp
, '\n')) {
228 /* The format is just "Commit Parent1 Parent2 ...\n" */
229 struct commit_graft
*graft
= read_graft_line(&buf
);
232 if (register_commit_graft(r
, graft
, 1))
233 error("duplicate graft data: %s", buf
.buf
);
236 strbuf_release(&buf
);
240 void prepare_commit_graft(struct repository
*r
)
244 if (r
->parsed_objects
->commit_graft_prepared
)
246 if (!startup_info
->have_repository
)
249 graft_file
= get_graft_file(r
);
250 read_graft_file(r
, graft_file
);
251 /* make sure shallows are read */
252 is_repository_shallow(r
);
253 r
->parsed_objects
->commit_graft_prepared
= 1;
256 struct commit_graft
*lookup_commit_graft(struct repository
*r
, const struct object_id
*oid
)
259 prepare_commit_graft(r
);
260 pos
= commit_graft_pos(r
, oid
);
263 return r
->parsed_objects
->grafts
[pos
];
266 int for_each_commit_graft(each_commit_graft_fn fn
, void *cb_data
)
269 for (i
= ret
= 0; i
< the_repository
->parsed_objects
->grafts_nr
&& !ret
; i
++)
270 ret
= fn(the_repository
->parsed_objects
->grafts
[i
], cb_data
);
274 void reset_commit_grafts(struct repository
*r
)
278 for (i
= 0; i
< r
->parsed_objects
->grafts_nr
; i
++) {
279 unparse_commit(r
, &r
->parsed_objects
->grafts
[i
]->oid
);
280 free(r
->parsed_objects
->grafts
[i
]);
282 r
->parsed_objects
->grafts_nr
= 0;
283 r
->parsed_objects
->commit_graft_prepared
= 0;
286 struct commit_buffer
{
290 define_commit_slab(buffer_slab
, struct commit_buffer
);
292 struct buffer_slab
*allocate_commit_buffer_slab(void)
294 struct buffer_slab
*bs
= xmalloc(sizeof(*bs
));
295 init_buffer_slab(bs
);
299 void free_commit_buffer_slab(struct buffer_slab
*bs
)
301 clear_buffer_slab(bs
);
305 void set_commit_buffer(struct repository
*r
, struct commit
*commit
, void *buffer
, unsigned long size
)
307 struct commit_buffer
*v
= buffer_slab_at(
308 r
->parsed_objects
->buffer_slab
, commit
);
313 const void *get_cached_commit_buffer(struct repository
*r
, const struct commit
*commit
, unsigned long *sizep
)
315 struct commit_buffer
*v
= buffer_slab_peek(
316 r
->parsed_objects
->buffer_slab
, commit
);
327 const void *repo_get_commit_buffer(struct repository
*r
,
328 const struct commit
*commit
,
329 unsigned long *sizep
)
331 const void *ret
= get_cached_commit_buffer(r
, commit
, sizep
);
333 enum object_type type
;
335 ret
= repo_read_object_file(r
, &commit
->object
.oid
, &type
, &size
);
337 die("cannot read commit object %s",
338 oid_to_hex(&commit
->object
.oid
));
339 if (type
!= OBJ_COMMIT
)
340 die("expected commit for %s, got %s",
341 oid_to_hex(&commit
->object
.oid
), type_name(type
));
348 void repo_unuse_commit_buffer(struct repository
*r
,
349 const struct commit
*commit
,
352 struct commit_buffer
*v
= buffer_slab_peek(
353 r
->parsed_objects
->buffer_slab
, commit
);
354 if (!(v
&& v
->buffer
== buffer
))
355 free((void *)buffer
);
358 void free_commit_buffer(struct parsed_object_pool
*pool
, struct commit
*commit
)
360 struct commit_buffer
*v
= buffer_slab_peek(
361 pool
->buffer_slab
, commit
);
363 FREE_AND_NULL(v
->buffer
);
368 static inline void set_commit_tree(struct commit
*c
, struct tree
*t
)
373 struct tree
*repo_get_commit_tree(struct repository
*r
,
374 const struct commit
*commit
)
376 if (commit
->maybe_tree
|| !commit
->object
.parsed
)
377 return commit
->maybe_tree
;
379 if (commit_graph_position(commit
) != COMMIT_NOT_FROM_GRAPH
)
380 return get_commit_tree_in_graph(r
, commit
);
385 struct object_id
*get_commit_tree_oid(const struct commit
*commit
)
387 struct tree
*tree
= get_commit_tree(commit
);
388 return tree
? &tree
->object
.oid
: NULL
;
391 void release_commit_memory(struct parsed_object_pool
*pool
, struct commit
*c
)
393 set_commit_tree(c
, NULL
);
394 free_commit_buffer(pool
, c
);
396 free_commit_list(c
->parents
);
398 c
->object
.parsed
= 0;
401 const void *detach_commit_buffer(struct commit
*commit
, unsigned long *sizep
)
403 struct commit_buffer
*v
= buffer_slab_peek(
404 the_repository
->parsed_objects
->buffer_slab
, commit
);
421 int parse_commit_buffer(struct repository
*r
, struct commit
*item
, const void *buffer
, unsigned long size
, int check_graph
)
423 const char *tail
= buffer
;
424 const char *bufptr
= buffer
;
425 struct object_id parent
;
426 struct commit_list
**pptr
;
427 struct commit_graft
*graft
;
428 const int tree_entry_len
= the_hash_algo
->hexsz
+ 5;
429 const int parent_entry_len
= the_hash_algo
->hexsz
+ 7;
432 if (item
->object
.parsed
)
435 * Presumably this is leftover from an earlier failed parse;
436 * clear it out in preparation for us re-parsing (we'll hit the
437 * same error, but that's good, since it lets our caller know
438 * the result cannot be trusted.
440 free_commit_list(item
->parents
);
441 item
->parents
= NULL
;
444 if (tail
<= bufptr
+ tree_entry_len
+ 1 || memcmp(bufptr
, "tree ", 5) ||
445 bufptr
[tree_entry_len
] != '\n')
446 return error("bogus commit object %s", oid_to_hex(&item
->object
.oid
));
447 if (get_oid_hex(bufptr
+ 5, &parent
) < 0)
448 return error("bad tree pointer in commit %s",
449 oid_to_hex(&item
->object
.oid
));
450 tree
= lookup_tree(r
, &parent
);
452 return error("bad tree pointer %s in commit %s",
454 oid_to_hex(&item
->object
.oid
));
455 set_commit_tree(item
, tree
);
456 bufptr
+= tree_entry_len
+ 1; /* "tree " + "hex sha1" + "\n" */
457 pptr
= &item
->parents
;
459 graft
= lookup_commit_graft(r
, &item
->object
.oid
);
461 r
->parsed_objects
->substituted_parent
= 1;
462 while (bufptr
+ parent_entry_len
< tail
&& !memcmp(bufptr
, "parent ", 7)) {
463 struct commit
*new_parent
;
465 if (tail
<= bufptr
+ parent_entry_len
+ 1 ||
466 get_oid_hex(bufptr
+ 7, &parent
) ||
467 bufptr
[parent_entry_len
] != '\n')
468 return error("bad parents in commit %s", oid_to_hex(&item
->object
.oid
));
469 bufptr
+= parent_entry_len
+ 1;
471 * The clone is shallow if nr_parent < 0, and we must
472 * not traverse its real parents even when we unhide them.
474 if (graft
&& (graft
->nr_parent
< 0 || grafts_replace_parents
))
476 new_parent
= lookup_commit(r
, &parent
);
478 return error("bad parent %s in commit %s",
480 oid_to_hex(&item
->object
.oid
));
481 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
485 struct commit
*new_parent
;
486 for (i
= 0; i
< graft
->nr_parent
; i
++) {
487 new_parent
= lookup_commit(r
,
490 return error("bad graft parent %s in commit %s",
491 oid_to_hex(&graft
->parent
[i
]),
492 oid_to_hex(&item
->object
.oid
));
493 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
496 item
->date
= parse_commit_date(bufptr
, tail
);
499 load_commit_graph_info(r
, item
);
501 item
->object
.parsed
= 1;
505 int repo_parse_commit_internal(struct repository
*r
,
507 int quiet_on_missing
,
508 int use_commit_graph
)
510 enum object_type type
;
513 struct object_info oi
= {
519 * Git does not support partial clones that exclude commits, so set
520 * OBJECT_INFO_SKIP_FETCH_OBJECT to fail fast when an object is missing.
522 int flags
= OBJECT_INFO_LOOKUP_REPLACE
| OBJECT_INFO_SKIP_FETCH_OBJECT
|
523 OBJECT_INFO_DIE_IF_CORRUPT
;
528 if (item
->object
.parsed
)
530 if (use_commit_graph
&& parse_commit_in_graph(r
, item
))
533 if (oid_object_info_extended(r
, &item
->object
.oid
, &oi
, flags
) < 0)
534 return quiet_on_missing
? -1 :
535 error("Could not read %s",
536 oid_to_hex(&item
->object
.oid
));
537 if (type
!= OBJ_COMMIT
) {
539 return error("Object %s not a commit",
540 oid_to_hex(&item
->object
.oid
));
543 ret
= parse_commit_buffer(r
, item
, buffer
, size
, 0);
544 if (save_commit_buffer
&& !ret
) {
545 set_commit_buffer(r
, item
, buffer
, size
);
552 int repo_parse_commit_gently(struct repository
*r
,
553 struct commit
*item
, int quiet_on_missing
)
555 return repo_parse_commit_internal(r
, item
, quiet_on_missing
, 1);
558 void parse_commit_or_die(struct commit
*item
)
560 if (parse_commit(item
))
561 die("unable to parse commit %s",
562 item
? oid_to_hex(&item
->object
.oid
) : "(null)");
565 int find_commit_subject(const char *commit_buffer
, const char **subject
)
568 const char *p
= commit_buffer
;
570 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
573 p
= skip_blank_lines(p
+ 2);
574 eol
= strchrnul(p
, '\n');
583 size_t commit_subject_length(const char *body
)
585 const char *p
= body
;
587 const char *next
= skip_blank_lines(p
);
590 p
= strchrnul(p
, '\n');
597 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
599 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
600 new_list
->item
= item
;
601 new_list
->next
= *list_p
;
606 int commit_list_contains(struct commit
*item
, struct commit_list
*list
)
609 if (list
->item
== item
)
617 unsigned commit_list_count(const struct commit_list
*l
)
620 for (; l
; l
= l
->next
)
625 struct commit_list
*copy_commit_list(struct commit_list
*list
)
627 struct commit_list
*head
= NULL
;
628 struct commit_list
**pp
= &head
;
630 pp
= commit_list_append(list
->item
, pp
);
636 struct commit_list
*reverse_commit_list(struct commit_list
*list
)
638 struct commit_list
*next
= NULL
, *current
, *backup
;
639 for (current
= list
; current
; current
= backup
) {
640 backup
= current
->next
;
641 current
->next
= next
;
647 void free_commit_list(struct commit_list
*list
)
653 struct commit_list
* commit_list_insert_by_date(struct commit
*item
, struct commit_list
**list
)
655 struct commit_list
**pp
= list
;
656 struct commit_list
*p
;
657 while ((p
= *pp
) != NULL
) {
658 if (p
->item
->date
< item
->date
) {
663 return commit_list_insert(item
, pp
);
666 static int commit_list_compare_by_date(const struct commit_list
*a
,
667 const struct commit_list
*b
)
669 timestamp_t a_date
= a
->item
->date
;
670 timestamp_t b_date
= b
->item
->date
;
678 DEFINE_LIST_SORT(static, commit_list_sort
, struct commit_list
, next
);
680 void commit_list_sort_by_date(struct commit_list
**list
)
682 commit_list_sort(list
, commit_list_compare_by_date
);
685 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
688 struct commit
*ret
= pop_commit(list
);
689 struct commit_list
*parents
= ret
->parents
;
692 struct commit
*commit
= parents
->item
;
693 if (!parse_commit(commit
) && !(commit
->object
.flags
& mark
)) {
694 commit
->object
.flags
|= mark
;
695 commit_list_insert_by_date(commit
, list
);
697 parents
= parents
->next
;
702 static void clear_commit_marks_1(struct commit_list
**plist
,
703 struct commit
*commit
, unsigned int mark
)
706 struct commit_list
*parents
;
708 if (!(mark
& commit
->object
.flags
))
711 commit
->object
.flags
&= ~mark
;
713 parents
= commit
->parents
;
717 while ((parents
= parents
->next
)) {
718 if (parents
->item
->object
.flags
& mark
)
719 commit_list_insert(parents
->item
, plist
);
722 commit
= commit
->parents
->item
;
726 void clear_commit_marks_many(int nr
, struct commit
**commit
, unsigned int mark
)
728 struct commit_list
*list
= NULL
;
731 clear_commit_marks_1(&list
, *commit
, mark
);
735 clear_commit_marks_1(&list
, pop_commit(&list
), mark
);
738 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
740 clear_commit_marks_many(1, &commit
, mark
);
743 struct commit
*pop_commit(struct commit_list
**stack
)
745 struct commit_list
*top
= *stack
;
746 struct commit
*item
= top
? top
->item
: NULL
;
756 * Topological sort support
759 /* count number of children that have not been emitted */
760 define_commit_slab(indegree_slab
, int);
762 define_commit_slab(author_date_slab
, timestamp_t
);
764 void record_author_date(struct author_date_slab
*author_date
,
765 struct commit
*commit
)
767 const char *buffer
= get_commit_buffer(commit
, NULL
);
768 struct ident_split ident
;
769 const char *ident_line
;
774 ident_line
= find_commit_header(buffer
, "author", &ident_len
);
776 goto fail_exit
; /* no author line */
777 if (split_ident_line(&ident
, ident_line
, ident_len
) ||
778 !ident
.date_begin
|| !ident
.date_end
)
779 goto fail_exit
; /* malformed "author" line */
781 date
= parse_timestamp(ident
.date_begin
, &date_end
, 10);
782 if (date_end
!= ident
.date_end
)
783 goto fail_exit
; /* malformed date */
784 *(author_date_slab_at(author_date
, commit
)) = date
;
787 unuse_commit_buffer(commit
, buffer
);
790 int compare_commits_by_author_date(const void *a_
, const void *b_
,
793 const struct commit
*a
= a_
, *b
= b_
;
794 struct author_date_slab
*author_date
= cb_data
;
795 timestamp_t a_date
= *(author_date_slab_at(author_date
, a
));
796 timestamp_t b_date
= *(author_date_slab_at(author_date
, b
));
798 /* newer commits with larger date first */
801 else if (a_date
> b_date
)
806 int compare_commits_by_gen_then_commit_date(const void *a_
, const void *b_
,
809 const struct commit
*a
= a_
, *b
= b_
;
810 const timestamp_t generation_a
= commit_graph_generation(a
),
811 generation_b
= commit_graph_generation(b
);
813 /* newer commits first */
814 if (generation_a
< generation_b
)
816 else if (generation_a
> generation_b
)
819 /* use date as a heuristic when generations are equal */
820 if (a
->date
< b
->date
)
822 else if (a
->date
> b
->date
)
827 int compare_commits_by_commit_date(const void *a_
, const void *b_
,
830 const struct commit
*a
= a_
, *b
= b_
;
831 /* newer commits with larger date first */
832 if (a
->date
< b
->date
)
834 else if (a
->date
> b
->date
)
840 * Performs an in-place topological sort on the list supplied.
842 void sort_in_topological_order(struct commit_list
**list
, enum rev_sort_order sort_order
)
844 struct commit_list
*next
, *orig
= *list
;
845 struct commit_list
**pptr
;
846 struct indegree_slab indegree
;
847 struct prio_queue queue
;
848 struct commit
*commit
;
849 struct author_date_slab author_date
;
855 init_indegree_slab(&indegree
);
856 memset(&queue
, '\0', sizeof(queue
));
858 switch (sort_order
) {
859 default: /* REV_SORT_IN_GRAPH_ORDER */
860 queue
.compare
= NULL
;
862 case REV_SORT_BY_COMMIT_DATE
:
863 queue
.compare
= compare_commits_by_commit_date
;
865 case REV_SORT_BY_AUTHOR_DATE
:
866 init_author_date_slab(&author_date
);
867 queue
.compare
= compare_commits_by_author_date
;
868 queue
.cb_data
= &author_date
;
872 /* Mark them and clear the indegree */
873 for (next
= orig
; next
; next
= next
->next
) {
874 struct commit
*commit
= next
->item
;
875 *(indegree_slab_at(&indegree
, commit
)) = 1;
876 /* also record the author dates, if needed */
877 if (sort_order
== REV_SORT_BY_AUTHOR_DATE
)
878 record_author_date(&author_date
, commit
);
881 /* update the indegree */
882 for (next
= orig
; next
; next
= next
->next
) {
883 struct commit_list
*parents
= next
->item
->parents
;
885 struct commit
*parent
= parents
->item
;
886 int *pi
= indegree_slab_at(&indegree
, parent
);
890 parents
= parents
->next
;
897 * tips are nodes not reachable from any other node in the list
899 * the tips serve as a starting set for the work queue.
901 for (next
= orig
; next
; next
= next
->next
) {
902 struct commit
*commit
= next
->item
;
904 if (*(indegree_slab_at(&indegree
, commit
)) == 1)
905 prio_queue_put(&queue
, commit
);
909 * This is unfortunate; the initial tips need to be shown
910 * in the order given from the revision traversal machinery.
912 if (sort_order
== REV_SORT_IN_GRAPH_ORDER
)
913 prio_queue_reverse(&queue
);
915 /* We no longer need the commit list */
916 free_commit_list(orig
);
920 while ((commit
= prio_queue_get(&queue
)) != NULL
) {
921 struct commit_list
*parents
;
923 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
924 struct commit
*parent
= parents
->item
;
925 int *pi
= indegree_slab_at(&indegree
, parent
);
931 * parents are only enqueued for emission
932 * when all their children have been emitted thereby
933 * guaranteeing topological order.
936 prio_queue_put(&queue
, parent
);
939 * all children of commit have already been
940 * emitted. we can emit it now.
942 *(indegree_slab_at(&indegree
, commit
)) = 0;
944 pptr
= &commit_list_insert(commit
, pptr
)->next
;
947 clear_indegree_slab(&indegree
);
948 clear_prio_queue(&queue
);
949 if (sort_order
== REV_SORT_BY_AUTHOR_DATE
)
950 clear_author_date_slab(&author_date
);
954 struct commit
**commit
;
957 unsigned int initial
: 1;
960 static void add_one_commit(struct object_id
*oid
, struct rev_collect
*revs
)
962 struct commit
*commit
;
964 if (is_null_oid(oid
))
967 commit
= lookup_commit(the_repository
, oid
);
969 (commit
->object
.flags
& TMP_MARK
) ||
970 parse_commit(commit
))
973 ALLOC_GROW(revs
->commit
, revs
->nr
+ 1, revs
->alloc
);
974 revs
->commit
[revs
->nr
++] = commit
;
975 commit
->object
.flags
|= TMP_MARK
;
978 static int collect_one_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
979 const char *ident UNUSED
,
980 timestamp_t timestamp UNUSED
, int tz UNUSED
,
981 const char *message UNUSED
, void *cbdata
)
983 struct rev_collect
*revs
= cbdata
;
987 add_one_commit(ooid
, revs
);
989 add_one_commit(noid
, revs
);
993 struct commit
*get_fork_point(const char *refname
, struct commit
*commit
)
995 struct object_id oid
;
996 struct rev_collect revs
;
997 struct commit_list
*bases
;
999 struct commit
*ret
= NULL
;
1002 switch (dwim_ref(refname
, strlen(refname
), &oid
, &full_refname
, 0)) {
1004 die("No such ref: '%s'", refname
);
1008 die("Ambiguous refname: '%s'", refname
);
1011 memset(&revs
, 0, sizeof(revs
));
1013 for_each_reflog_ent(full_refname
, collect_one_reflog_ent
, &revs
);
1016 add_one_commit(&oid
, &revs
);
1018 for (i
= 0; i
< revs
.nr
; i
++)
1019 revs
.commit
[i
]->object
.flags
&= ~TMP_MARK
;
1021 bases
= get_merge_bases_many(commit
, revs
.nr
, revs
.commit
);
1024 * There should be one and only one merge base, when we found
1025 * a common ancestor among reflog entries.
1027 if (!bases
|| bases
->next
)
1028 goto cleanup_return
;
1030 /* And the found one must be one of the reflog entries */
1031 for (i
= 0; i
< revs
.nr
; i
++)
1032 if (&bases
->item
->object
== &revs
.commit
[i
]->object
)
1035 goto cleanup_return
;
1041 free_commit_list(bases
);
1047 * Indexed by hash algorithm identifier.
1049 static const char *gpg_sig_headers
[] = {
1055 int sign_with_header(struct strbuf
*buf
, const char *keyid
)
1057 struct strbuf sig
= STRBUF_INIT
;
1058 int inspos
, copypos
;
1060 const char *gpg_sig_header
= gpg_sig_headers
[hash_algo_by_ptr(the_hash_algo
)];
1061 int gpg_sig_header_len
= strlen(gpg_sig_header
);
1063 /* find the end of the header */
1064 eoh
= strstr(buf
->buf
, "\n\n");
1068 inspos
= eoh
- buf
->buf
+ 1;
1070 if (!keyid
|| !*keyid
)
1071 keyid
= get_signing_key();
1072 if (sign_buffer(buf
, &sig
, keyid
)) {
1073 strbuf_release(&sig
);
1077 for (copypos
= 0; sig
.buf
[copypos
]; ) {
1078 const char *bol
= sig
.buf
+ copypos
;
1079 const char *eol
= strchrnul(bol
, '\n');
1080 int len
= (eol
- bol
) + !!*eol
;
1083 strbuf_insert(buf
, inspos
, gpg_sig_header
, gpg_sig_header_len
);
1084 inspos
+= gpg_sig_header_len
;
1086 strbuf_insertstr(buf
, inspos
++, " ");
1087 strbuf_insert(buf
, inspos
, bol
, len
);
1091 strbuf_release(&sig
);
1097 int parse_signed_commit(const struct commit
*commit
,
1098 struct strbuf
*payload
, struct strbuf
*signature
,
1099 const struct git_hash_algo
*algop
)
1102 const char *buffer
= get_commit_buffer(commit
, &size
);
1103 int ret
= parse_buffer_signed_by_header(buffer
, size
, payload
, signature
, algop
);
1105 unuse_commit_buffer(commit
, buffer
);
1109 int parse_buffer_signed_by_header(const char *buffer
,
1111 struct strbuf
*payload
,
1112 struct strbuf
*signature
,
1113 const struct git_hash_algo
*algop
)
1115 int in_signature
= 0, saw_signature
= 0, other_signature
= 0;
1116 const char *line
, *tail
, *p
;
1117 const char *gpg_sig_header
= gpg_sig_headers
[hash_algo_by_ptr(algop
)];
1120 tail
= buffer
+ size
;
1121 while (line
< tail
) {
1122 const char *sig
= NULL
;
1123 const char *next
= memchr(line
, '\n', tail
- line
);
1125 next
= next
? next
+ 1 : tail
;
1126 if (in_signature
&& line
[0] == ' ')
1128 else if (skip_prefix(line
, gpg_sig_header
, &p
) &&
1130 sig
= line
+ strlen(gpg_sig_header
) + 1;
1131 other_signature
= 0;
1133 else if (starts_with(line
, "gpgsig"))
1134 other_signature
= 1;
1135 else if (other_signature
&& line
[0] != ' ')
1136 other_signature
= 0;
1138 strbuf_add(signature
, sig
, next
- sig
);
1143 /* dump the whole remainder of the buffer */
1145 if (!other_signature
)
1146 strbuf_add(payload
, line
, next
- line
);
1151 return saw_signature
;
1154 int remove_signature(struct strbuf
*buf
)
1156 const char *line
= buf
->buf
;
1157 const char *tail
= buf
->buf
+ buf
->len
;
1158 int in_signature
= 0;
1162 } sigs
[2], *sigp
= &sigs
[0];
1164 const char *orig_buf
= buf
->buf
;
1166 memset(sigs
, 0, sizeof(sigs
));
1168 while (line
< tail
) {
1169 const char *next
= memchr(line
, '\n', tail
- line
);
1170 next
= next
? next
+ 1 : tail
;
1172 if (in_signature
&& line
[0] == ' ')
1174 else if (starts_with(line
, "gpgsig")) {
1176 for (i
= 1; i
< GIT_HASH_NALGOS
; i
++) {
1178 if (skip_prefix(line
, gpg_sig_headers
[i
], &p
) &&
1187 /* dump the whole remainder of the buffer */
1189 if (in_signature
&& sigp
- sigs
!= ARRAY_SIZE(sigs
))
1196 for (i
= ARRAY_SIZE(sigs
) - 1; i
>= 0; i
--)
1198 strbuf_remove(buf
, sigs
[i
].start
- orig_buf
, sigs
[i
].end
- sigs
[i
].start
);
1200 return sigs
[0].start
!= NULL
;
1203 static void handle_signed_tag(struct commit
*parent
, struct commit_extra_header
***tail
)
1205 struct merge_remote_desc
*desc
;
1206 struct commit_extra_header
*mergetag
;
1209 enum object_type type
;
1210 struct strbuf payload
= STRBUF_INIT
;
1211 struct strbuf signature
= STRBUF_INIT
;
1213 desc
= merge_remote_util(parent
);
1214 if (!desc
|| !desc
->obj
)
1216 buf
= read_object_file(&desc
->obj
->oid
, &type
, &size
);
1217 if (!buf
|| type
!= OBJ_TAG
)
1219 if (!parse_signature(buf
, size
, &payload
, &signature
))
1222 * We could verify this signature and either omit the tag when
1223 * it does not validate, but the integrator may not have the
1224 * public key of the signer of the tag being merged, while a
1225 * later auditor may have it while auditing, so let's not run
1226 * verify-signed-buffer here for now...
1228 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1229 * warn("warning: signed tag unverified.");
1231 CALLOC_ARRAY(mergetag
, 1);
1232 mergetag
->key
= xstrdup("mergetag");
1233 mergetag
->value
= buf
;
1234 mergetag
->len
= size
;
1237 *tail
= &mergetag
->next
;
1238 strbuf_release(&payload
);
1239 strbuf_release(&signature
);
1246 int check_commit_signature(const struct commit
*commit
, struct signature_check
*sigc
)
1248 struct strbuf payload
= STRBUF_INIT
;
1249 struct strbuf signature
= STRBUF_INIT
;
1254 if (parse_signed_commit(commit
, &payload
, &signature
, the_hash_algo
) <= 0)
1257 sigc
->payload_type
= SIGNATURE_PAYLOAD_COMMIT
;
1258 sigc
->payload
= strbuf_detach(&payload
, &sigc
->payload_len
);
1259 ret
= check_signature(sigc
, signature
.buf
, signature
.len
);
1262 strbuf_release(&payload
);
1263 strbuf_release(&signature
);
1268 void verify_merge_signature(struct commit
*commit
, int verbosity
,
1271 char hex
[GIT_MAX_HEXSZ
+ 1];
1272 struct signature_check signature_check
;
1274 memset(&signature_check
, 0, sizeof(signature_check
));
1276 ret
= check_commit_signature(commit
, &signature_check
);
1278 find_unique_abbrev_r(hex
, &commit
->object
.oid
, DEFAULT_ABBREV
);
1279 switch (signature_check
.result
) {
1281 if (ret
|| (check_trust
&& signature_check
.trust_level
< TRUST_MARGINAL
))
1282 die(_("Commit %s has an untrusted GPG signature, "
1283 "allegedly by %s."), hex
, signature_check
.signer
);
1286 die(_("Commit %s has a bad GPG signature "
1287 "allegedly by %s."), hex
, signature_check
.signer
);
1289 die(_("Commit %s does not have a GPG signature."), hex
);
1291 if (verbosity
>= 0 && signature_check
.result
== 'G')
1292 printf(_("Commit %s has a good GPG signature by %s\n"),
1293 hex
, signature_check
.signer
);
1295 signature_check_clear(&signature_check
);
1298 void append_merge_tag_headers(struct commit_list
*parents
,
1299 struct commit_extra_header
***tail
)
1302 struct commit
*parent
= parents
->item
;
1303 handle_signed_tag(parent
, tail
);
1304 parents
= parents
->next
;
1308 static void add_extra_header(struct strbuf
*buffer
,
1309 struct commit_extra_header
*extra
)
1311 strbuf_addstr(buffer
, extra
->key
);
1313 strbuf_add_lines(buffer
, " ", extra
->value
, extra
->len
);
1315 strbuf_addch(buffer
, '\n');
1318 struct commit_extra_header
*read_commit_extra_headers(struct commit
*commit
,
1319 const char **exclude
)
1321 struct commit_extra_header
*extra
= NULL
;
1323 const char *buffer
= get_commit_buffer(commit
, &size
);
1324 extra
= read_commit_extra_header_lines(buffer
, size
, exclude
);
1325 unuse_commit_buffer(commit
, buffer
);
1329 int for_each_mergetag(each_mergetag_fn fn
, struct commit
*commit
, void *data
)
1331 struct commit_extra_header
*extra
, *to_free
;
1334 to_free
= read_commit_extra_headers(commit
, NULL
);
1335 for (extra
= to_free
; !res
&& extra
; extra
= extra
->next
) {
1336 if (strcmp(extra
->key
, "mergetag"))
1337 continue; /* not a merge tag */
1338 res
= fn(commit
, extra
, data
);
1340 free_commit_extra_headers(to_free
);
1344 static inline int standard_header_field(const char *field
, size_t len
)
1346 return ((len
== 4 && !memcmp(field
, "tree", 4)) ||
1347 (len
== 6 && !memcmp(field
, "parent", 6)) ||
1348 (len
== 6 && !memcmp(field
, "author", 6)) ||
1349 (len
== 9 && !memcmp(field
, "committer", 9)) ||
1350 (len
== 8 && !memcmp(field
, "encoding", 8)));
1353 static int excluded_header_field(const char *field
, size_t len
, const char **exclude
)
1359 size_t xlen
= strlen(*exclude
);
1360 if (len
== xlen
&& !memcmp(field
, *exclude
, xlen
))
1367 static struct commit_extra_header
*read_commit_extra_header_lines(
1368 const char *buffer
, size_t size
,
1369 const char **exclude
)
1371 struct commit_extra_header
*extra
= NULL
, **tail
= &extra
, *it
= NULL
;
1372 const char *line
, *next
, *eof
, *eob
;
1373 struct strbuf buf
= STRBUF_INIT
;
1375 for (line
= buffer
, eob
= line
+ size
;
1376 line
< eob
&& *line
!= '\n';
1378 next
= memchr(line
, '\n', eob
- line
);
1379 next
= next
? next
+ 1 : eob
;
1383 strbuf_add(&buf
, line
+ 1, next
- (line
+ 1));
1387 it
->value
= strbuf_detach(&buf
, &it
->len
);
1391 eof
= memchr(line
, ' ', next
- line
);
1394 else if (standard_header_field(line
, eof
- line
) ||
1395 excluded_header_field(line
, eof
- line
, exclude
))
1398 CALLOC_ARRAY(it
, 1);
1399 it
->key
= xmemdupz(line
, eof
-line
);
1403 strbuf_add(&buf
, eof
+ 1, next
- (eof
+ 1));
1406 it
->value
= strbuf_detach(&buf
, &it
->len
);
1410 void free_commit_extra_headers(struct commit_extra_header
*extra
)
1413 struct commit_extra_header
*next
= extra
->next
;
1421 int commit_tree(const char *msg
, size_t msg_len
, const struct object_id
*tree
,
1422 struct commit_list
*parents
, struct object_id
*ret
,
1423 const char *author
, const char *sign_commit
)
1425 struct commit_extra_header
*extra
= NULL
, **tail
= &extra
;
1428 append_merge_tag_headers(parents
, &tail
);
1429 result
= commit_tree_extended(msg
, msg_len
, tree
, parents
, ret
, author
,
1430 NULL
, sign_commit
, extra
);
1431 free_commit_extra_headers(extra
);
1435 static int find_invalid_utf8(const char *buf
, int len
)
1438 static const unsigned int max_codepoint
[] = {
1439 0x7f, 0x7ff, 0xffff, 0x10ffff
1443 unsigned char c
= *buf
++;
1444 int bytes
, bad_offset
;
1445 unsigned int codepoint
;
1446 unsigned int min_val
, max_val
;
1451 /* Simple US-ASCII? No worries. */
1455 bad_offset
= offset
-1;
1458 * Count how many more high bits set: that's how
1459 * many more bytes this sequence should have.
1468 * Must be between 1 and 3 more bytes. Longer sequences result in
1469 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1471 if (bytes
< 1 || 3 < bytes
)
1474 /* Do we *have* that many bytes? */
1479 * Place the encoded bits at the bottom of the value and compute the
1482 codepoint
= (c
& 0x7f) >> bytes
;
1483 min_val
= max_codepoint
[bytes
-1] + 1;
1484 max_val
= max_codepoint
[bytes
];
1489 /* And verify that they are good continuation bytes */
1492 codepoint
|= *buf
& 0x3f;
1493 if ((*buf
++ & 0xc0) != 0x80)
1497 /* Reject codepoints that are out of range for the sequence length. */
1498 if (codepoint
< min_val
|| codepoint
> max_val
)
1500 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1501 if ((codepoint
& 0x1ff800) == 0xd800)
1503 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1504 if ((codepoint
& 0xfffe) == 0xfffe)
1506 /* So are anything in the range U+FDD0..U+FDEF. */
1507 if (codepoint
>= 0xfdd0 && codepoint
<= 0xfdef)
1514 * This verifies that the buffer is in proper utf8 format.
1516 * If it isn't, it assumes any non-utf8 characters are Latin1,
1517 * and does the conversion.
1519 static int verify_utf8(struct strbuf
*buf
)
1527 unsigned char replace
[2];
1529 bad
= find_invalid_utf8(buf
->buf
+ pos
, buf
->len
- pos
);
1535 strbuf_remove(buf
, pos
, 1);
1537 /* We know 'c' must be in the range 128-255 */
1538 replace
[0] = 0xc0 + (c
>> 6);
1539 replace
[1] = 0x80 + (c
& 0x3f);
1540 strbuf_insert(buf
, pos
, replace
, 2);
1545 static const char commit_utf8_warn
[] =
1546 N_("Warning: commit message did not conform to UTF-8.\n"
1547 "You may want to amend it after fixing the message, or set the config\n"
1548 "variable i18n.commitEncoding to the encoding your project uses.\n");
1550 int commit_tree_extended(const char *msg
, size_t msg_len
,
1551 const struct object_id
*tree
,
1552 struct commit_list
*parents
, struct object_id
*ret
,
1553 const char *author
, const char *committer
,
1554 const char *sign_commit
,
1555 struct commit_extra_header
*extra
)
1558 int encoding_is_utf8
;
1559 struct strbuf buffer
;
1561 assert_oid_type(tree
, OBJ_TREE
);
1563 if (memchr(msg
, '\0', msg_len
))
1564 return error("a NUL byte in commit log message not allowed.");
1566 /* Not having i18n.commitencoding is the same as having utf-8 */
1567 encoding_is_utf8
= is_encoding_utf8(git_commit_encoding
);
1569 strbuf_init(&buffer
, 8192); /* should avoid reallocs for the headers */
1570 strbuf_addf(&buffer
, "tree %s\n", oid_to_hex(tree
));
1573 * NOTE! This ordering means that the same exact tree merged with a
1574 * different order of parents will be a _different_ changeset even
1575 * if everything else stays the same.
1578 struct commit
*parent
= pop_commit(&parents
);
1579 strbuf_addf(&buffer
, "parent %s\n",
1580 oid_to_hex(&parent
->object
.oid
));
1583 /* Person/date information */
1585 author
= git_author_info(IDENT_STRICT
);
1586 strbuf_addf(&buffer
, "author %s\n", author
);
1588 committer
= git_committer_info(IDENT_STRICT
);
1589 strbuf_addf(&buffer
, "committer %s\n", committer
);
1590 if (!encoding_is_utf8
)
1591 strbuf_addf(&buffer
, "encoding %s\n", git_commit_encoding
);
1594 add_extra_header(&buffer
, extra
);
1595 extra
= extra
->next
;
1597 strbuf_addch(&buffer
, '\n');
1599 /* And add the comment */
1600 strbuf_add(&buffer
, msg
, msg_len
);
1602 /* And check the encoding */
1603 if (encoding_is_utf8
&& !verify_utf8(&buffer
))
1604 fprintf(stderr
, _(commit_utf8_warn
));
1606 if (sign_commit
&& sign_with_header(&buffer
, sign_commit
)) {
1611 result
= write_object_file(buffer
.buf
, buffer
.len
, OBJ_COMMIT
, ret
);
1613 strbuf_release(&buffer
);
1617 define_commit_slab(merge_desc_slab
, struct merge_remote_desc
*);
1618 static struct merge_desc_slab merge_desc_slab
= COMMIT_SLAB_INIT(1, merge_desc_slab
);
1620 struct merge_remote_desc
*merge_remote_util(struct commit
*commit
)
1622 return *merge_desc_slab_at(&merge_desc_slab
, commit
);
1625 void set_merge_remote_desc(struct commit
*commit
,
1626 const char *name
, struct object
*obj
)
1628 struct merge_remote_desc
*desc
;
1629 FLEX_ALLOC_STR(desc
, name
, name
);
1631 *merge_desc_slab_at(&merge_desc_slab
, commit
) = desc
;
1634 struct commit
*get_merge_parent(const char *name
)
1637 struct commit
*commit
;
1638 struct object_id oid
;
1639 if (get_oid(name
, &oid
))
1641 obj
= parse_object(the_repository
, &oid
);
1642 commit
= (struct commit
*)peel_to_type(name
, 0, obj
, OBJ_COMMIT
);
1643 if (commit
&& !merge_remote_util(commit
))
1644 set_merge_remote_desc(commit
, name
, obj
);
1649 * Append a commit to the end of the commit_list.
1651 * next starts by pointing to the variable that holds the head of an
1652 * empty commit_list, and is updated to point to the "next" field of
1653 * the last item on the list as new commits are appended.
1657 * struct commit_list *list;
1658 * struct commit_list **next = &list;
1660 * next = commit_list_append(c1, next);
1661 * next = commit_list_append(c2, next);
1662 * assert(commit_list_count(list) == 2);
1665 struct commit_list
**commit_list_append(struct commit
*commit
,
1666 struct commit_list
**next
)
1668 struct commit_list
*new_commit
= xmalloc(sizeof(struct commit_list
));
1669 new_commit
->item
= commit
;
1671 new_commit
->next
= NULL
;
1672 return &new_commit
->next
;
1675 const char *find_header_mem(const char *msg
, size_t len
,
1676 const char *key
, size_t *out_len
)
1678 int key_len
= strlen(key
);
1679 const char *line
= msg
;
1682 * NEEDSWORK: It's possible for strchrnul() to scan beyond the range
1683 * given by len. However, current callers are safe because they compute
1684 * len by scanning a NUL-terminated block of memory starting at msg.
1685 * Nonetheless, it would be better to ensure the function does not look
1686 * at msg beyond the len provided by the caller.
1688 while (line
&& line
< msg
+ len
) {
1689 const char *eol
= strchrnul(line
, '\n');
1694 if (eol
- line
> key_len
&&
1695 !strncmp(line
, key
, key_len
) &&
1696 line
[key_len
] == ' ') {
1697 *out_len
= eol
- line
- key_len
- 1;
1698 return line
+ key_len
+ 1;
1700 line
= *eol
? eol
+ 1 : NULL
;
1705 const char *find_commit_header(const char *msg
, const char *key
, size_t *out_len
)
1707 return find_header_mem(msg
, strlen(msg
), key
, out_len
);
1710 * Inspect the given string and determine the true "end" of the log message, in
1711 * order to find where to put a new Signed-off-by trailer. Ignored are
1712 * trailing comment lines and blank lines. To support "git commit -s
1713 * --amend" on an existing commit, we also ignore "Conflicts:". To
1714 * support "git commit -v", we truncate at cut lines.
1716 * Returns the number of bytes from the tail to ignore, to be fed as
1717 * the second parameter to append_signoff().
1719 size_t ignore_non_trailer(const char *buf
, size_t len
)
1723 int in_old_conflicts_block
= 0;
1724 size_t cutoff
= wt_status_locate_end(buf
, len
);
1726 while (bol
< cutoff
) {
1727 const char *next_line
= memchr(buf
+ bol
, '\n', len
- bol
);
1730 next_line
= buf
+ len
;
1734 if (buf
[bol
] == comment_line_char
|| buf
[bol
] == '\n') {
1735 /* is this the first of the run of comments? */
1738 /* otherwise, it is just continuing */
1739 } else if (starts_with(buf
+ bol
, "Conflicts:\n")) {
1740 in_old_conflicts_block
= 1;
1743 } else if (in_old_conflicts_block
&& buf
[bol
] == '\t') {
1744 ; /* a pathname in the conflicts block */
1746 /* the previous was not trailing comment */
1748 in_old_conflicts_block
= 0;
1750 bol
= next_line
- buf
;
1752 return boc
? len
- boc
: len
- cutoff
;
1755 int run_commit_hook(int editor_is_used
, const char *index_file
,
1756 int *invoked_hook
, const char *name
, ...)
1758 struct run_hooks_opt opt
= RUN_HOOKS_OPT_INIT
;
1762 strvec_pushf(&opt
.env
, "GIT_INDEX_FILE=%s", index_file
);
1765 * Let the hook know that no editor will be launched.
1767 if (!editor_is_used
)
1768 strvec_push(&opt
.env
, "GIT_EDITOR=:");
1770 va_start(args
, name
);
1771 while ((arg
= va_arg(args
, const char *)))
1772 strvec_push(&opt
.args
, arg
);
1775 opt
.invoked_hook
= invoked_hook
;
1776 return run_hooks_opt(name
, &opt
);