1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
6 #include "commit-graph.h"
7 #include "environment.h"
10 #include "repository.h"
11 #include "object-name.h"
12 #include "object-store-ll.h"
18 #include "gpg-interface.h"
19 #include "mergesort.h"
20 #include "commit-slab.h"
21 #include "prio-queue.h"
22 #include "hash-lookup.h"
23 #include "wt-status.h"
26 #include "commit-reach.h"
32 #include "object-file-convert.h"
34 static struct commit_extra_header
*read_commit_extra_header_lines(const char *buf
, size_t len
, const char **);
36 int save_commit_buffer
= 1;
37 int no_graft_file_deprecated_advice
;
39 const char *commit_type
= "commit";
41 struct commit
*lookup_commit_reference_gently(struct repository
*r
,
42 const struct object_id
*oid
, int quiet
)
44 struct object
*obj
= deref_tag(r
,
50 return object_as_type(obj
, OBJ_COMMIT
, quiet
);
53 struct commit
*lookup_commit_reference(struct repository
*r
, const struct object_id
*oid
)
55 return lookup_commit_reference_gently(r
, oid
, 0);
58 struct commit
*lookup_commit_or_die(const struct object_id
*oid
, const char *ref_name
)
60 struct commit
*c
= lookup_commit_reference(the_repository
, oid
);
62 die(_("could not parse %s"), ref_name
);
63 if (!oideq(oid
, &c
->object
.oid
)) {
64 warning(_("%s %s is not a commit!"),
65 ref_name
, oid_to_hex(oid
));
70 struct commit
*lookup_commit_object(struct repository
*r
,
71 const struct object_id
*oid
)
73 struct object
*obj
= parse_object(r
, oid
);
74 return obj
? object_as_type(obj
, OBJ_COMMIT
, 0) : NULL
;
78 struct commit
*lookup_commit(struct repository
*r
, const struct object_id
*oid
)
80 struct object
*obj
= lookup_object(r
, oid
);
82 return create_object(r
, oid
, alloc_commit_node(r
));
83 return object_as_type(obj
, OBJ_COMMIT
, 0);
86 struct commit
*lookup_commit_reference_by_name(const char *name
)
89 struct commit
*commit
;
91 if (repo_get_oid_committish(the_repository
, name
, &oid
))
93 commit
= lookup_commit_reference(the_repository
, &oid
);
94 if (repo_parse_commit(the_repository
, commit
))
99 static timestamp_t
parse_commit_date(const char *buf
, const char *tail
)
106 if (memcmp(buf
, "author", 6))
108 while (buf
< tail
&& *buf
++ != '\n')
112 if (memcmp(buf
, "committer", 9))
116 * Jump to end-of-line so that we can walk backwards to find the
117 * end-of-email ">". This is more forgiving of malformed cases
118 * because unexpected characters tend to be in the name and email
121 eol
= memchr(buf
, '\n', tail
- buf
);
125 while (dateptr
> buf
&& dateptr
[-1] != '>')
131 * Trim leading whitespace, but make sure we have at least one
132 * non-whitespace character, as parse_timestamp() will otherwise walk
133 * right past the newline we found in "eol" when skipping whitespace
136 * In theory it would be sufficient to allow any character not matched
137 * by isspace(), but there's a catch: our isspace() does not
138 * necessarily match the behavior of parse_timestamp(), as the latter
139 * is implemented by system routines which match more exotic control
140 * codes, or even locale-dependent sequences.
142 * Since we expect the timestamp to be a number, we can check for that.
143 * Anything else (e.g., a non-numeric token like "foo") would just
144 * cause parse_timestamp() to return 0 anyway.
146 while (dateptr
< eol
&& isspace(*dateptr
))
148 if (!isdigit(*dateptr
) && *dateptr
!= '-')
152 * We know there is at least one digit (or dash), so we'll begin
153 * parsing there and stop at worst case at eol.
155 * Note that we may feed parse_timestamp() extra characters here if the
156 * commit is malformed, and it will parse as far as it can. For
157 * example, "123foo456" would return "123". That might be questionable
158 * (versus returning "0"), but it would help in a hypothetical case
159 * like "123456+0100", where the whitespace from the timezone is
160 * missing. Since such syntactic errors may be baked into history and
161 * hard to correct now, let's err on trying to make our best guess
162 * here, rather than insist on perfect syntax.
164 return parse_timestamp(dateptr
, NULL
, 10);
167 static const struct object_id
*commit_graft_oid_access(size_t index
, const void *table
)
169 const struct commit_graft
* const *commit_graft_table
= table
;
170 return &commit_graft_table
[index
]->oid
;
173 int commit_graft_pos(struct repository
*r
, const struct object_id
*oid
)
175 return oid_pos(oid
, r
->parsed_objects
->grafts
,
176 r
->parsed_objects
->grafts_nr
,
177 commit_graft_oid_access
);
180 static void unparse_commit(struct repository
*r
, const struct object_id
*oid
)
182 struct commit
*c
= lookup_commit(r
, oid
);
184 if (!c
->object
.parsed
)
186 free_commit_list(c
->parents
);
188 c
->object
.parsed
= 0;
191 int register_commit_graft(struct repository
*r
, struct commit_graft
*graft
,
194 int pos
= commit_graft_pos(r
, &graft
->oid
);
200 free(r
->parsed_objects
->grafts
[pos
]);
201 r
->parsed_objects
->grafts
[pos
] = graft
;
206 ALLOC_GROW(r
->parsed_objects
->grafts
,
207 r
->parsed_objects
->grafts_nr
+ 1,
208 r
->parsed_objects
->grafts_alloc
);
209 r
->parsed_objects
->grafts_nr
++;
210 if (pos
< r
->parsed_objects
->grafts_nr
)
211 memmove(r
->parsed_objects
->grafts
+ pos
+ 1,
212 r
->parsed_objects
->grafts
+ pos
,
213 (r
->parsed_objects
->grafts_nr
- pos
- 1) *
214 sizeof(*r
->parsed_objects
->grafts
));
215 r
->parsed_objects
->grafts
[pos
] = graft
;
216 unparse_commit(r
, &graft
->oid
);
220 struct commit_graft
*read_graft_line(struct strbuf
*line
)
222 /* The format is just "Commit Parent1 Parent2 ...\n" */
224 const char *tail
= NULL
;
225 struct commit_graft
*graft
= NULL
;
226 struct object_id dummy_oid
, *oid
;
229 if (!line
->len
|| line
->buf
[0] == '#')
232 * phase 0 verifies line, counts hashes in line and allocates graft
233 * phase 1 fills graft
235 for (phase
= 0; phase
< 2; phase
++) {
236 oid
= graft
? &graft
->oid
: &dummy_oid
;
237 if (parse_oid_hex(line
->buf
, oid
, &tail
))
239 for (i
= 0; *tail
!= '\0'; i
++) {
240 oid
= graft
? &graft
->parent
[i
] : &dummy_oid
;
241 if (!isspace(*tail
++) || parse_oid_hex(tail
, oid
, &tail
))
245 graft
= xmalloc(st_add(sizeof(*graft
),
246 st_mult(sizeof(struct object_id
), i
)));
247 graft
->nr_parent
= i
;
253 error("bad graft data: %s", line
->buf
);
258 static int read_graft_file(struct repository
*r
, const char *graft_file
)
260 FILE *fp
= fopen_or_warn(graft_file
, "r");
261 struct strbuf buf
= STRBUF_INIT
;
264 if (!no_graft_file_deprecated_advice
&&
265 advice_enabled(ADVICE_GRAFT_FILE_DEPRECATED
))
266 advise(_("Support for <GIT_DIR>/info/grafts is deprecated\n"
267 "and will be removed in a future Git version.\n"
269 "Please use \"git replace --convert-graft-file\"\n"
270 "to convert the grafts into replace refs.\n"
272 "Turn this message off by running\n"
273 "\"git config advice.graftFileDeprecated false\""));
274 while (!strbuf_getwholeline(&buf
, fp
, '\n')) {
275 /* The format is just "Commit Parent1 Parent2 ...\n" */
276 struct commit_graft
*graft
= read_graft_line(&buf
);
279 if (register_commit_graft(r
, graft
, 1))
280 error("duplicate graft data: %s", buf
.buf
);
283 strbuf_release(&buf
);
287 void prepare_commit_graft(struct repository
*r
)
291 if (r
->parsed_objects
->commit_graft_prepared
)
293 if (!startup_info
->have_repository
)
296 graft_file
= get_graft_file(r
);
297 read_graft_file(r
, graft_file
);
298 /* make sure shallows are read */
299 is_repository_shallow(r
);
300 r
->parsed_objects
->commit_graft_prepared
= 1;
303 struct commit_graft
*lookup_commit_graft(struct repository
*r
, const struct object_id
*oid
)
306 prepare_commit_graft(r
);
307 pos
= commit_graft_pos(r
, oid
);
310 return r
->parsed_objects
->grafts
[pos
];
313 int for_each_commit_graft(each_commit_graft_fn fn
, void *cb_data
)
316 for (i
= ret
= 0; i
< the_repository
->parsed_objects
->grafts_nr
&& !ret
; i
++)
317 ret
= fn(the_repository
->parsed_objects
->grafts
[i
], cb_data
);
321 void reset_commit_grafts(struct repository
*r
)
325 for (i
= 0; i
< r
->parsed_objects
->grafts_nr
; i
++) {
326 unparse_commit(r
, &r
->parsed_objects
->grafts
[i
]->oid
);
327 free(r
->parsed_objects
->grafts
[i
]);
329 r
->parsed_objects
->grafts_nr
= 0;
330 r
->parsed_objects
->commit_graft_prepared
= 0;
333 struct commit_buffer
{
337 define_commit_slab(buffer_slab
, struct commit_buffer
);
339 struct buffer_slab
*allocate_commit_buffer_slab(void)
341 struct buffer_slab
*bs
= xmalloc(sizeof(*bs
));
342 init_buffer_slab(bs
);
346 void free_commit_buffer_slab(struct buffer_slab
*bs
)
348 clear_buffer_slab(bs
);
352 void set_commit_buffer(struct repository
*r
, struct commit
*commit
, void *buffer
, unsigned long size
)
354 struct commit_buffer
*v
= buffer_slab_at(
355 r
->parsed_objects
->buffer_slab
, commit
);
360 const void *get_cached_commit_buffer(struct repository
*r
, const struct commit
*commit
, unsigned long *sizep
)
362 struct commit_buffer
*v
= buffer_slab_peek(
363 r
->parsed_objects
->buffer_slab
, commit
);
374 const void *repo_get_commit_buffer(struct repository
*r
,
375 const struct commit
*commit
,
376 unsigned long *sizep
)
378 const void *ret
= get_cached_commit_buffer(r
, commit
, sizep
);
380 enum object_type type
;
382 ret
= repo_read_object_file(r
, &commit
->object
.oid
, &type
, &size
);
384 die("cannot read commit object %s",
385 oid_to_hex(&commit
->object
.oid
));
386 if (type
!= OBJ_COMMIT
)
387 die("expected commit for %s, got %s",
388 oid_to_hex(&commit
->object
.oid
), type_name(type
));
395 void repo_unuse_commit_buffer(struct repository
*r
,
396 const struct commit
*commit
,
399 struct commit_buffer
*v
= buffer_slab_peek(
400 r
->parsed_objects
->buffer_slab
, commit
);
401 if (!(v
&& v
->buffer
== buffer
))
402 free((void *)buffer
);
405 void free_commit_buffer(struct parsed_object_pool
*pool
, struct commit
*commit
)
407 struct commit_buffer
*v
= buffer_slab_peek(
408 pool
->buffer_slab
, commit
);
410 FREE_AND_NULL(v
->buffer
);
415 static inline void set_commit_tree(struct commit
*c
, struct tree
*t
)
420 struct tree
*repo_get_commit_tree(struct repository
*r
,
421 const struct commit
*commit
)
423 if (commit
->maybe_tree
|| !commit
->object
.parsed
)
424 return commit
->maybe_tree
;
426 if (commit_graph_position(commit
) != COMMIT_NOT_FROM_GRAPH
)
427 return get_commit_tree_in_graph(r
, commit
);
432 struct object_id
*get_commit_tree_oid(const struct commit
*commit
)
434 struct tree
*tree
= repo_get_commit_tree(the_repository
, commit
);
435 return tree
? &tree
->object
.oid
: NULL
;
438 void release_commit_memory(struct parsed_object_pool
*pool
, struct commit
*c
)
440 set_commit_tree(c
, NULL
);
441 free_commit_buffer(pool
, c
);
443 free_commit_list(c
->parents
);
445 c
->object
.parsed
= 0;
448 const void *detach_commit_buffer(struct commit
*commit
, unsigned long *sizep
)
450 struct commit_buffer
*v
= buffer_slab_peek(
451 the_repository
->parsed_objects
->buffer_slab
, commit
);
468 int parse_commit_buffer(struct repository
*r
, struct commit
*item
, const void *buffer
, unsigned long size
, int check_graph
)
470 const char *tail
= buffer
;
471 const char *bufptr
= buffer
;
472 struct object_id parent
;
473 struct commit_list
**pptr
;
474 struct commit_graft
*graft
;
475 const int tree_entry_len
= the_hash_algo
->hexsz
+ 5;
476 const int parent_entry_len
= the_hash_algo
->hexsz
+ 7;
479 if (item
->object
.parsed
)
482 * Presumably this is leftover from an earlier failed parse;
483 * clear it out in preparation for us re-parsing (we'll hit the
484 * same error, but that's good, since it lets our caller know
485 * the result cannot be trusted.
487 free_commit_list(item
->parents
);
488 item
->parents
= NULL
;
491 if (tail
<= bufptr
+ tree_entry_len
+ 1 || memcmp(bufptr
, "tree ", 5) ||
492 bufptr
[tree_entry_len
] != '\n')
493 return error("bogus commit object %s", oid_to_hex(&item
->object
.oid
));
494 if (get_oid_hex(bufptr
+ 5, &parent
) < 0)
495 return error("bad tree pointer in commit %s",
496 oid_to_hex(&item
->object
.oid
));
497 tree
= lookup_tree(r
, &parent
);
499 return error("bad tree pointer %s in commit %s",
501 oid_to_hex(&item
->object
.oid
));
502 set_commit_tree(item
, tree
);
503 bufptr
+= tree_entry_len
+ 1; /* "tree " + "hex sha1" + "\n" */
504 pptr
= &item
->parents
;
506 graft
= lookup_commit_graft(r
, &item
->object
.oid
);
508 r
->parsed_objects
->substituted_parent
= 1;
509 while (bufptr
+ parent_entry_len
< tail
&& !memcmp(bufptr
, "parent ", 7)) {
510 struct commit
*new_parent
;
512 if (tail
<= bufptr
+ parent_entry_len
+ 1 ||
513 get_oid_hex(bufptr
+ 7, &parent
) ||
514 bufptr
[parent_entry_len
] != '\n')
515 return error("bad parents in commit %s", oid_to_hex(&item
->object
.oid
));
516 bufptr
+= parent_entry_len
+ 1;
518 * The clone is shallow if nr_parent < 0, and we must
519 * not traverse its real parents even when we unhide them.
521 if (graft
&& (graft
->nr_parent
< 0 || !grafts_keep_true_parents
))
523 new_parent
= lookup_commit(r
, &parent
);
525 return error("bad parent %s in commit %s",
527 oid_to_hex(&item
->object
.oid
));
528 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
532 struct commit
*new_parent
;
533 for (i
= 0; i
< graft
->nr_parent
; i
++) {
534 new_parent
= lookup_commit(r
,
537 return error("bad graft parent %s in commit %s",
538 oid_to_hex(&graft
->parent
[i
]),
539 oid_to_hex(&item
->object
.oid
));
540 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
543 item
->date
= parse_commit_date(bufptr
, tail
);
546 load_commit_graph_info(r
, item
);
548 item
->object
.parsed
= 1;
552 int repo_parse_commit_internal(struct repository
*r
,
554 int quiet_on_missing
,
555 int use_commit_graph
)
557 enum object_type type
;
560 struct object_info oi
= {
566 * Git does not support partial clones that exclude commits, so set
567 * OBJECT_INFO_SKIP_FETCH_OBJECT to fail fast when an object is missing.
569 int flags
= OBJECT_INFO_LOOKUP_REPLACE
| OBJECT_INFO_SKIP_FETCH_OBJECT
|
570 OBJECT_INFO_DIE_IF_CORRUPT
;
575 if (item
->object
.parsed
)
577 if (use_commit_graph
&& parse_commit_in_graph(r
, item
)) {
578 static int commit_graph_paranoia
= -1;
580 if (commit_graph_paranoia
== -1)
581 commit_graph_paranoia
= git_env_bool(GIT_COMMIT_GRAPH_PARANOIA
, 0);
583 if (commit_graph_paranoia
&& !has_object(r
, &item
->object
.oid
, 0)) {
584 unparse_commit(r
, &item
->object
.oid
);
585 return quiet_on_missing
? -1 :
586 error(_("commit %s exists in commit-graph but not in the object database"),
587 oid_to_hex(&item
->object
.oid
));
593 if (oid_object_info_extended(r
, &item
->object
.oid
, &oi
, flags
) < 0)
594 return quiet_on_missing
? -1 :
595 error("Could not read %s",
596 oid_to_hex(&item
->object
.oid
));
597 if (type
!= OBJ_COMMIT
) {
599 return error("Object %s not a commit",
600 oid_to_hex(&item
->object
.oid
));
603 ret
= parse_commit_buffer(r
, item
, buffer
, size
, 0);
604 if (save_commit_buffer
&& !ret
) {
605 set_commit_buffer(r
, item
, buffer
, size
);
612 int repo_parse_commit_gently(struct repository
*r
,
613 struct commit
*item
, int quiet_on_missing
)
615 return repo_parse_commit_internal(r
, item
, quiet_on_missing
, 1);
618 void parse_commit_or_die(struct commit
*item
)
620 if (repo_parse_commit(the_repository
, item
))
621 die("unable to parse commit %s",
622 item
? oid_to_hex(&item
->object
.oid
) : "(null)");
625 int find_commit_subject(const char *commit_buffer
, const char **subject
)
628 const char *p
= commit_buffer
;
630 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
633 p
= skip_blank_lines(p
+ 2);
634 eol
= strchrnul(p
, '\n');
643 size_t commit_subject_length(const char *body
)
645 const char *p
= body
;
647 const char *next
= skip_blank_lines(p
);
650 p
= strchrnul(p
, '\n');
657 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
659 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
660 new_list
->item
= item
;
661 new_list
->next
= *list_p
;
666 int commit_list_contains(struct commit
*item
, struct commit_list
*list
)
669 if (list
->item
== item
)
677 unsigned commit_list_count(const struct commit_list
*l
)
680 for (; l
; l
= l
->next
)
685 struct commit_list
*copy_commit_list(const struct commit_list
*list
)
687 struct commit_list
*head
= NULL
;
688 struct commit_list
**pp
= &head
;
690 pp
= commit_list_append(list
->item
, pp
);
696 struct commit_list
*reverse_commit_list(struct commit_list
*list
)
698 struct commit_list
*next
= NULL
, *current
, *backup
;
699 for (current
= list
; current
; current
= backup
) {
700 backup
= current
->next
;
701 current
->next
= next
;
707 void free_commit_list(struct commit_list
*list
)
713 struct commit_list
* commit_list_insert_by_date(struct commit
*item
, struct commit_list
**list
)
715 struct commit_list
**pp
= list
;
716 struct commit_list
*p
;
717 while ((p
= *pp
) != NULL
) {
718 if (p
->item
->date
< item
->date
) {
723 return commit_list_insert(item
, pp
);
726 static int commit_list_compare_by_date(const struct commit_list
*a
,
727 const struct commit_list
*b
)
729 timestamp_t a_date
= a
->item
->date
;
730 timestamp_t b_date
= b
->item
->date
;
738 DEFINE_LIST_SORT(static, commit_list_sort
, struct commit_list
, next
);
740 void commit_list_sort_by_date(struct commit_list
**list
)
742 commit_list_sort(list
, commit_list_compare_by_date
);
745 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
748 struct commit
*ret
= pop_commit(list
);
749 struct commit_list
*parents
= ret
->parents
;
752 struct commit
*commit
= parents
->item
;
753 if (!repo_parse_commit(the_repository
, commit
) && !(commit
->object
.flags
& mark
)) {
754 commit
->object
.flags
|= mark
;
755 commit_list_insert_by_date(commit
, list
);
757 parents
= parents
->next
;
762 static void clear_commit_marks_1(struct commit_list
**plist
,
763 struct commit
*commit
, unsigned int mark
)
766 struct commit_list
*parents
;
768 if (!(mark
& commit
->object
.flags
))
771 commit
->object
.flags
&= ~mark
;
773 parents
= commit
->parents
;
777 while ((parents
= parents
->next
)) {
778 if (parents
->item
->object
.flags
& mark
)
779 commit_list_insert(parents
->item
, plist
);
782 commit
= commit
->parents
->item
;
786 void clear_commit_marks_many(int nr
, struct commit
**commit
, unsigned int mark
)
788 struct commit_list
*list
= NULL
;
791 clear_commit_marks_1(&list
, *commit
, mark
);
795 clear_commit_marks_1(&list
, pop_commit(&list
), mark
);
798 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
800 clear_commit_marks_many(1, &commit
, mark
);
803 struct commit
*pop_commit(struct commit_list
**stack
)
805 struct commit_list
*top
= *stack
;
806 struct commit
*item
= top
? top
->item
: NULL
;
816 * Topological sort support
819 /* count number of children that have not been emitted */
820 define_commit_slab(indegree_slab
, int);
822 define_commit_slab(author_date_slab
, timestamp_t
);
824 void record_author_date(struct author_date_slab
*author_date
,
825 struct commit
*commit
)
827 const char *buffer
= repo_get_commit_buffer(the_repository
, commit
,
829 struct ident_split ident
;
830 const char *ident_line
;
835 ident_line
= find_commit_header(buffer
, "author", &ident_len
);
837 goto fail_exit
; /* no author line */
838 if (split_ident_line(&ident
, ident_line
, ident_len
) ||
839 !ident
.date_begin
|| !ident
.date_end
)
840 goto fail_exit
; /* malformed "author" line */
842 date
= parse_timestamp(ident
.date_begin
, &date_end
, 10);
843 if (date_end
!= ident
.date_end
)
844 goto fail_exit
; /* malformed date */
845 *(author_date_slab_at(author_date
, commit
)) = date
;
848 repo_unuse_commit_buffer(the_repository
, commit
, buffer
);
851 int compare_commits_by_author_date(const void *a_
, const void *b_
,
854 const struct commit
*a
= a_
, *b
= b_
;
855 struct author_date_slab
*author_date
= cb_data
;
856 timestamp_t a_date
= *(author_date_slab_at(author_date
, a
));
857 timestamp_t b_date
= *(author_date_slab_at(author_date
, b
));
859 /* newer commits with larger date first */
862 else if (a_date
> b_date
)
867 int compare_commits_by_gen_then_commit_date(const void *a_
, const void *b_
,
870 const struct commit
*a
= a_
, *b
= b_
;
871 const timestamp_t generation_a
= commit_graph_generation(a
),
872 generation_b
= commit_graph_generation(b
);
874 /* newer commits first */
875 if (generation_a
< generation_b
)
877 else if (generation_a
> generation_b
)
880 /* use date as a heuristic when generations are equal */
881 if (a
->date
< b
->date
)
883 else if (a
->date
> b
->date
)
888 int compare_commits_by_commit_date(const void *a_
, const void *b_
,
891 const struct commit
*a
= a_
, *b
= b_
;
892 /* newer commits with larger date first */
893 if (a
->date
< b
->date
)
895 else if (a
->date
> b
->date
)
901 * Performs an in-place topological sort on the list supplied.
903 void sort_in_topological_order(struct commit_list
**list
, enum rev_sort_order sort_order
)
905 struct commit_list
*next
, *orig
= *list
;
906 struct commit_list
**pptr
;
907 struct indegree_slab indegree
;
908 struct prio_queue queue
;
909 struct commit
*commit
;
910 struct author_date_slab author_date
;
916 init_indegree_slab(&indegree
);
917 memset(&queue
, '\0', sizeof(queue
));
919 switch (sort_order
) {
920 default: /* REV_SORT_IN_GRAPH_ORDER */
921 queue
.compare
= NULL
;
923 case REV_SORT_BY_COMMIT_DATE
:
924 queue
.compare
= compare_commits_by_commit_date
;
926 case REV_SORT_BY_AUTHOR_DATE
:
927 init_author_date_slab(&author_date
);
928 queue
.compare
= compare_commits_by_author_date
;
929 queue
.cb_data
= &author_date
;
933 /* Mark them and clear the indegree */
934 for (next
= orig
; next
; next
= next
->next
) {
935 struct commit
*commit
= next
->item
;
936 *(indegree_slab_at(&indegree
, commit
)) = 1;
937 /* also record the author dates, if needed */
938 if (sort_order
== REV_SORT_BY_AUTHOR_DATE
)
939 record_author_date(&author_date
, commit
);
942 /* update the indegree */
943 for (next
= orig
; next
; next
= next
->next
) {
944 struct commit_list
*parents
= next
->item
->parents
;
946 struct commit
*parent
= parents
->item
;
947 int *pi
= indegree_slab_at(&indegree
, parent
);
951 parents
= parents
->next
;
958 * tips are nodes not reachable from any other node in the list
960 * the tips serve as a starting set for the work queue.
962 for (next
= orig
; next
; next
= next
->next
) {
963 struct commit
*commit
= next
->item
;
965 if (*(indegree_slab_at(&indegree
, commit
)) == 1)
966 prio_queue_put(&queue
, commit
);
970 * This is unfortunate; the initial tips need to be shown
971 * in the order given from the revision traversal machinery.
973 if (sort_order
== REV_SORT_IN_GRAPH_ORDER
)
974 prio_queue_reverse(&queue
);
976 /* We no longer need the commit list */
977 free_commit_list(orig
);
981 while ((commit
= prio_queue_get(&queue
)) != NULL
) {
982 struct commit_list
*parents
;
984 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
985 struct commit
*parent
= parents
->item
;
986 int *pi
= indegree_slab_at(&indegree
, parent
);
992 * parents are only enqueued for emission
993 * when all their children have been emitted thereby
994 * guaranteeing topological order.
997 prio_queue_put(&queue
, parent
);
1000 * all children of commit have already been
1001 * emitted. we can emit it now.
1003 *(indegree_slab_at(&indegree
, commit
)) = 0;
1005 pptr
= &commit_list_insert(commit
, pptr
)->next
;
1008 clear_indegree_slab(&indegree
);
1009 clear_prio_queue(&queue
);
1010 if (sort_order
== REV_SORT_BY_AUTHOR_DATE
)
1011 clear_author_date_slab(&author_date
);
1014 struct rev_collect
{
1015 struct commit
**commit
;
1018 unsigned int initial
: 1;
1021 static void add_one_commit(struct object_id
*oid
, struct rev_collect
*revs
)
1023 struct commit
*commit
;
1025 if (is_null_oid(oid
))
1028 commit
= lookup_commit(the_repository
, oid
);
1030 (commit
->object
.flags
& TMP_MARK
) ||
1031 repo_parse_commit(the_repository
, commit
))
1034 ALLOC_GROW(revs
->commit
, revs
->nr
+ 1, revs
->alloc
);
1035 revs
->commit
[revs
->nr
++] = commit
;
1036 commit
->object
.flags
|= TMP_MARK
;
1039 static int collect_one_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
1040 const char *ident UNUSED
,
1041 timestamp_t timestamp UNUSED
, int tz UNUSED
,
1042 const char *message UNUSED
, void *cbdata
)
1044 struct rev_collect
*revs
= cbdata
;
1046 if (revs
->initial
) {
1048 add_one_commit(ooid
, revs
);
1050 add_one_commit(noid
, revs
);
1054 struct commit
*get_fork_point(const char *refname
, struct commit
*commit
)
1056 struct object_id oid
;
1057 struct rev_collect revs
;
1058 struct commit_list
*bases
= NULL
;
1060 struct commit
*ret
= NULL
;
1063 switch (repo_dwim_ref(the_repository
, refname
, strlen(refname
), &oid
,
1064 &full_refname
, 0)) {
1066 die("No such ref: '%s'", refname
);
1070 die("Ambiguous refname: '%s'", refname
);
1073 memset(&revs
, 0, sizeof(revs
));
1075 refs_for_each_reflog_ent(get_main_ref_store(the_repository
),
1076 full_refname
, collect_one_reflog_ent
, &revs
);
1079 add_one_commit(&oid
, &revs
);
1081 for (i
= 0; i
< revs
.nr
; i
++)
1082 revs
.commit
[i
]->object
.flags
&= ~TMP_MARK
;
1084 if (repo_get_merge_bases_many(the_repository
, commit
, revs
.nr
,
1085 revs
.commit
, &bases
) < 0)
1089 * There should be one and only one merge base, when we found
1090 * a common ancestor among reflog entries.
1092 if (!bases
|| bases
->next
)
1093 goto cleanup_return
;
1095 /* And the found one must be one of the reflog entries */
1096 for (i
= 0; i
< revs
.nr
; i
++)
1097 if (&bases
->item
->object
== &revs
.commit
[i
]->object
)
1100 goto cleanup_return
;
1106 free_commit_list(bases
);
1112 * Indexed by hash algorithm identifier.
1114 static const char *gpg_sig_headers
[] = {
1120 int add_header_signature(struct strbuf
*buf
, struct strbuf
*sig
, const struct git_hash_algo
*algo
)
1122 int inspos
, copypos
;
1124 const char *gpg_sig_header
= gpg_sig_headers
[hash_algo_by_ptr(algo
)];
1125 int gpg_sig_header_len
= strlen(gpg_sig_header
);
1127 /* find the end of the header */
1128 eoh
= strstr(buf
->buf
, "\n\n");
1132 inspos
= eoh
- buf
->buf
+ 1;
1134 for (copypos
= 0; sig
->buf
[copypos
]; ) {
1135 const char *bol
= sig
->buf
+ copypos
;
1136 const char *eol
= strchrnul(bol
, '\n');
1137 int len
= (eol
- bol
) + !!*eol
;
1140 strbuf_insert(buf
, inspos
, gpg_sig_header
, gpg_sig_header_len
);
1141 inspos
+= gpg_sig_header_len
;
1143 strbuf_insertstr(buf
, inspos
++, " ");
1144 strbuf_insert(buf
, inspos
, bol
, len
);
1151 static int sign_commit_to_strbuf(struct strbuf
*sig
, struct strbuf
*buf
, const char *keyid
)
1153 if (!keyid
|| !*keyid
)
1154 keyid
= get_signing_key();
1155 if (sign_buffer(buf
, sig
, keyid
))
1160 int parse_signed_commit(const struct commit
*commit
,
1161 struct strbuf
*payload
, struct strbuf
*signature
,
1162 const struct git_hash_algo
*algop
)
1165 const char *buffer
= repo_get_commit_buffer(the_repository
, commit
,
1167 int ret
= parse_buffer_signed_by_header(buffer
, size
, payload
, signature
, algop
);
1169 repo_unuse_commit_buffer(the_repository
, commit
, buffer
);
1173 int parse_buffer_signed_by_header(const char *buffer
,
1175 struct strbuf
*payload
,
1176 struct strbuf
*signature
,
1177 const struct git_hash_algo
*algop
)
1179 int in_signature
= 0, saw_signature
= 0, other_signature
= 0;
1180 const char *line
, *tail
, *p
;
1181 const char *gpg_sig_header
= gpg_sig_headers
[hash_algo_by_ptr(algop
)];
1184 tail
= buffer
+ size
;
1185 while (line
< tail
) {
1186 const char *sig
= NULL
;
1187 const char *next
= memchr(line
, '\n', tail
- line
);
1189 next
= next
? next
+ 1 : tail
;
1190 if (in_signature
&& line
[0] == ' ')
1192 else if (skip_prefix(line
, gpg_sig_header
, &p
) &&
1194 sig
= line
+ strlen(gpg_sig_header
) + 1;
1195 other_signature
= 0;
1197 else if (starts_with(line
, "gpgsig"))
1198 other_signature
= 1;
1199 else if (other_signature
&& line
[0] != ' ')
1200 other_signature
= 0;
1202 strbuf_add(signature
, sig
, next
- sig
);
1207 /* dump the whole remainder of the buffer */
1209 if (!other_signature
)
1210 strbuf_add(payload
, line
, next
- line
);
1215 return saw_signature
;
1218 int remove_signature(struct strbuf
*buf
)
1220 const char *line
= buf
->buf
;
1221 const char *tail
= buf
->buf
+ buf
->len
;
1222 int in_signature
= 0;
1226 } sigs
[2], *sigp
= &sigs
[0];
1228 const char *orig_buf
= buf
->buf
;
1230 memset(sigs
, 0, sizeof(sigs
));
1232 while (line
< tail
) {
1233 const char *next
= memchr(line
, '\n', tail
- line
);
1234 next
= next
? next
+ 1 : tail
;
1236 if (in_signature
&& line
[0] == ' ')
1238 else if (starts_with(line
, "gpgsig")) {
1240 for (i
= 1; i
< GIT_HASH_NALGOS
; i
++) {
1242 if (skip_prefix(line
, gpg_sig_headers
[i
], &p
) &&
1251 /* dump the whole remainder of the buffer */
1253 if (in_signature
&& sigp
- sigs
!= ARRAY_SIZE(sigs
))
1260 for (i
= ARRAY_SIZE(sigs
) - 1; i
>= 0; i
--)
1262 strbuf_remove(buf
, sigs
[i
].start
- orig_buf
, sigs
[i
].end
- sigs
[i
].start
);
1264 return sigs
[0].start
!= NULL
;
1267 static void handle_signed_tag(const struct commit
*parent
, struct commit_extra_header
***tail
)
1269 struct merge_remote_desc
*desc
;
1270 struct commit_extra_header
*mergetag
;
1273 enum object_type type
;
1274 struct strbuf payload
= STRBUF_INIT
;
1275 struct strbuf signature
= STRBUF_INIT
;
1277 desc
= merge_remote_util(parent
);
1278 if (!desc
|| !desc
->obj
)
1280 buf
= repo_read_object_file(the_repository
, &desc
->obj
->oid
, &type
,
1282 if (!buf
|| type
!= OBJ_TAG
)
1284 if (!parse_signature(buf
, size
, &payload
, &signature
))
1287 * We could verify this signature and either omit the tag when
1288 * it does not validate, but the integrator may not have the
1289 * public key of the signer of the tag being merged, while a
1290 * later auditor may have it while auditing, so let's not run
1291 * verify-signed-buffer here for now...
1293 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1294 * warn("warning: signed tag unverified.");
1296 CALLOC_ARRAY(mergetag
, 1);
1297 mergetag
->key
= xstrdup("mergetag");
1298 mergetag
->value
= buf
;
1299 mergetag
->len
= size
;
1302 *tail
= &mergetag
->next
;
1303 strbuf_release(&payload
);
1304 strbuf_release(&signature
);
1311 int check_commit_signature(const struct commit
*commit
, struct signature_check
*sigc
)
1313 struct strbuf payload
= STRBUF_INIT
;
1314 struct strbuf signature
= STRBUF_INIT
;
1319 if (parse_signed_commit(commit
, &payload
, &signature
, the_hash_algo
) <= 0)
1322 sigc
->payload_type
= SIGNATURE_PAYLOAD_COMMIT
;
1323 sigc
->payload
= strbuf_detach(&payload
, &sigc
->payload_len
);
1324 ret
= check_signature(sigc
, signature
.buf
, signature
.len
);
1327 strbuf_release(&payload
);
1328 strbuf_release(&signature
);
1333 void verify_merge_signature(struct commit
*commit
, int verbosity
,
1336 char hex
[GIT_MAX_HEXSZ
+ 1];
1337 struct signature_check signature_check
;
1339 memset(&signature_check
, 0, sizeof(signature_check
));
1341 ret
= check_commit_signature(commit
, &signature_check
);
1343 repo_find_unique_abbrev_r(the_repository
, hex
, &commit
->object
.oid
,
1345 switch (signature_check
.result
) {
1347 if (ret
|| (check_trust
&& signature_check
.trust_level
< TRUST_MARGINAL
))
1348 die(_("Commit %s has an untrusted GPG signature, "
1349 "allegedly by %s."), hex
, signature_check
.signer
);
1352 die(_("Commit %s has a bad GPG signature "
1353 "allegedly by %s."), hex
, signature_check
.signer
);
1355 die(_("Commit %s does not have a GPG signature."), hex
);
1357 if (verbosity
>= 0 && signature_check
.result
== 'G')
1358 printf(_("Commit %s has a good GPG signature by %s\n"),
1359 hex
, signature_check
.signer
);
1361 signature_check_clear(&signature_check
);
1364 void append_merge_tag_headers(const struct commit_list
*parents
,
1365 struct commit_extra_header
***tail
)
1368 const struct commit
*parent
= parents
->item
;
1369 handle_signed_tag(parent
, tail
);
1370 parents
= parents
->next
;
1374 static int convert_commit_extra_headers(const struct commit_extra_header
*orig
,
1375 struct commit_extra_header
**result
)
1377 const struct git_hash_algo
*compat
= the_repository
->compat_hash_algo
;
1378 const struct git_hash_algo
*algo
= the_repository
->hash_algo
;
1379 struct commit_extra_header
*extra
= NULL
, **tail
= &extra
;
1380 struct strbuf out
= STRBUF_INIT
;
1382 struct commit_extra_header
*new;
1383 CALLOC_ARRAY(new, 1);
1384 if (!strcmp(orig
->key
, "mergetag")) {
1385 if (convert_object_file(&out
, algo
, compat
,
1386 orig
->value
, orig
->len
,
1389 free_commit_extra_headers(extra
);
1392 new->key
= xstrdup("mergetag");
1393 new->value
= strbuf_detach(&out
, &new->len
);
1395 new->key
= xstrdup(orig
->key
);
1396 new->len
= orig
->len
;
1397 new->value
= xmemdupz(orig
->value
, orig
->len
);
1407 static void add_extra_header(struct strbuf
*buffer
,
1408 const struct commit_extra_header
*extra
)
1410 strbuf_addstr(buffer
, extra
->key
);
1412 strbuf_add_lines(buffer
, " ", extra
->value
, extra
->len
);
1414 strbuf_addch(buffer
, '\n');
1417 struct commit_extra_header
*read_commit_extra_headers(struct commit
*commit
,
1418 const char **exclude
)
1420 struct commit_extra_header
*extra
= NULL
;
1422 const char *buffer
= repo_get_commit_buffer(the_repository
, commit
,
1424 extra
= read_commit_extra_header_lines(buffer
, size
, exclude
);
1425 repo_unuse_commit_buffer(the_repository
, commit
, buffer
);
1429 int for_each_mergetag(each_mergetag_fn fn
, struct commit
*commit
, void *data
)
1431 struct commit_extra_header
*extra
, *to_free
;
1434 to_free
= read_commit_extra_headers(commit
, NULL
);
1435 for (extra
= to_free
; !res
&& extra
; extra
= extra
->next
) {
1436 if (strcmp(extra
->key
, "mergetag"))
1437 continue; /* not a merge tag */
1438 res
= fn(commit
, extra
, data
);
1440 free_commit_extra_headers(to_free
);
1444 static inline int standard_header_field(const char *field
, size_t len
)
1446 return ((len
== 4 && !memcmp(field
, "tree", 4)) ||
1447 (len
== 6 && !memcmp(field
, "parent", 6)) ||
1448 (len
== 6 && !memcmp(field
, "author", 6)) ||
1449 (len
== 9 && !memcmp(field
, "committer", 9)) ||
1450 (len
== 8 && !memcmp(field
, "encoding", 8)));
1453 static int excluded_header_field(const char *field
, size_t len
, const char **exclude
)
1459 size_t xlen
= strlen(*exclude
);
1460 if (len
== xlen
&& !memcmp(field
, *exclude
, xlen
))
1467 static struct commit_extra_header
*read_commit_extra_header_lines(
1468 const char *buffer
, size_t size
,
1469 const char **exclude
)
1471 struct commit_extra_header
*extra
= NULL
, **tail
= &extra
, *it
= NULL
;
1472 const char *line
, *next
, *eof
, *eob
;
1473 struct strbuf buf
= STRBUF_INIT
;
1475 for (line
= buffer
, eob
= line
+ size
;
1476 line
< eob
&& *line
!= '\n';
1478 next
= memchr(line
, '\n', eob
- line
);
1479 next
= next
? next
+ 1 : eob
;
1483 strbuf_add(&buf
, line
+ 1, next
- (line
+ 1));
1487 it
->value
= strbuf_detach(&buf
, &it
->len
);
1491 eof
= memchr(line
, ' ', next
- line
);
1494 else if (standard_header_field(line
, eof
- line
) ||
1495 excluded_header_field(line
, eof
- line
, exclude
))
1498 CALLOC_ARRAY(it
, 1);
1499 it
->key
= xmemdupz(line
, eof
-line
);
1503 strbuf_add(&buf
, eof
+ 1, next
- (eof
+ 1));
1506 it
->value
= strbuf_detach(&buf
, &it
->len
);
1510 void free_commit_extra_headers(struct commit_extra_header
*extra
)
1513 struct commit_extra_header
*next
= extra
->next
;
1521 int commit_tree(const char *msg
, size_t msg_len
, const struct object_id
*tree
,
1522 const struct commit_list
*parents
, struct object_id
*ret
,
1523 const char *author
, const char *sign_commit
)
1525 struct commit_extra_header
*extra
= NULL
, **tail
= &extra
;
1528 append_merge_tag_headers(parents
, &tail
);
1529 result
= commit_tree_extended(msg
, msg_len
, tree
, parents
, ret
, author
,
1530 NULL
, sign_commit
, extra
);
1531 free_commit_extra_headers(extra
);
1535 static int find_invalid_utf8(const char *buf
, int len
)
1538 static const unsigned int max_codepoint
[] = {
1539 0x7f, 0x7ff, 0xffff, 0x10ffff
1543 unsigned char c
= *buf
++;
1544 int bytes
, bad_offset
;
1545 unsigned int codepoint
;
1546 unsigned int min_val
, max_val
;
1551 /* Simple US-ASCII? No worries. */
1555 bad_offset
= offset
-1;
1558 * Count how many more high bits set: that's how
1559 * many more bytes this sequence should have.
1568 * Must be between 1 and 3 more bytes. Longer sequences result in
1569 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1571 if (bytes
< 1 || 3 < bytes
)
1574 /* Do we *have* that many bytes? */
1579 * Place the encoded bits at the bottom of the value and compute the
1582 codepoint
= (c
& 0x7f) >> bytes
;
1583 min_val
= max_codepoint
[bytes
-1] + 1;
1584 max_val
= max_codepoint
[bytes
];
1589 /* And verify that they are good continuation bytes */
1592 codepoint
|= *buf
& 0x3f;
1593 if ((*buf
++ & 0xc0) != 0x80)
1597 /* Reject codepoints that are out of range for the sequence length. */
1598 if (codepoint
< min_val
|| codepoint
> max_val
)
1600 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1601 if ((codepoint
& 0x1ff800) == 0xd800)
1603 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1604 if ((codepoint
& 0xfffe) == 0xfffe)
1606 /* So are anything in the range U+FDD0..U+FDEF. */
1607 if (codepoint
>= 0xfdd0 && codepoint
<= 0xfdef)
1614 * This verifies that the buffer is in proper utf8 format.
1616 * If it isn't, it assumes any non-utf8 characters are Latin1,
1617 * and does the conversion.
1619 static int verify_utf8(struct strbuf
*buf
)
1627 unsigned char replace
[2];
1629 bad
= find_invalid_utf8(buf
->buf
+ pos
, buf
->len
- pos
);
1635 strbuf_remove(buf
, pos
, 1);
1637 /* We know 'c' must be in the range 128-255 */
1638 replace
[0] = 0xc0 + (c
>> 6);
1639 replace
[1] = 0x80 + (c
& 0x3f);
1640 strbuf_insert(buf
, pos
, replace
, 2);
1645 static const char commit_utf8_warn
[] =
1646 N_("Warning: commit message did not conform to UTF-8.\n"
1647 "You may want to amend it after fixing the message, or set the config\n"
1648 "variable i18n.commitEncoding to the encoding your project uses.\n");
1650 static void write_commit_tree(struct strbuf
*buffer
, const char *msg
, size_t msg_len
,
1651 const struct object_id
*tree
,
1652 const struct object_id
*parents
, size_t parents_len
,
1653 const char *author
, const char *committer
,
1654 const struct commit_extra_header
*extra
)
1656 int encoding_is_utf8
;
1659 /* Not having i18n.commitencoding is the same as having utf-8 */
1660 encoding_is_utf8
= is_encoding_utf8(git_commit_encoding
);
1662 strbuf_grow(buffer
, 8192); /* should avoid reallocs for the headers */
1663 strbuf_addf(buffer
, "tree %s\n", oid_to_hex(tree
));
1666 * NOTE! This ordering means that the same exact tree merged with a
1667 * different order of parents will be a _different_ changeset even
1668 * if everything else stays the same.
1670 for (i
= 0; i
< parents_len
; i
++)
1671 strbuf_addf(buffer
, "parent %s\n", oid_to_hex(&parents
[i
]));
1673 /* Person/date information */
1675 author
= git_author_info(IDENT_STRICT
);
1676 strbuf_addf(buffer
, "author %s\n", author
);
1678 committer
= git_committer_info(IDENT_STRICT
);
1679 strbuf_addf(buffer
, "committer %s\n", committer
);
1680 if (!encoding_is_utf8
)
1681 strbuf_addf(buffer
, "encoding %s\n", git_commit_encoding
);
1684 add_extra_header(buffer
, extra
);
1685 extra
= extra
->next
;
1687 strbuf_addch(buffer
, '\n');
1689 /* And add the comment */
1690 strbuf_add(buffer
, msg
, msg_len
);
1693 int commit_tree_extended(const char *msg
, size_t msg_len
,
1694 const struct object_id
*tree
,
1695 const struct commit_list
*parents
, struct object_id
*ret
,
1696 const char *author
, const char *committer
,
1697 const char *sign_commit
,
1698 const struct commit_extra_header
*extra
)
1700 struct repository
*r
= the_repository
;
1702 int encoding_is_utf8
;
1703 struct strbuf buffer
= STRBUF_INIT
, compat_buffer
= STRBUF_INIT
;
1704 struct strbuf sig
= STRBUF_INIT
, compat_sig
= STRBUF_INIT
;
1705 struct object_id
*parent_buf
= NULL
, *compat_oid
= NULL
;
1706 struct object_id compat_oid_buf
;
1709 /* Not having i18n.commitencoding is the same as having utf-8 */
1710 encoding_is_utf8
= is_encoding_utf8(git_commit_encoding
);
1712 assert_oid_type(tree
, OBJ_TREE
);
1714 if (memchr(msg
, '\0', msg_len
))
1715 return error("a NUL byte in commit log message not allowed.");
1717 nparents
= commit_list_count(parents
);
1718 CALLOC_ARRAY(parent_buf
, nparents
);
1720 for (const struct commit_list
*p
= parents
; p
; p
= p
->next
)
1721 oidcpy(&parent_buf
[i
++], &p
->item
->object
.oid
);
1723 write_commit_tree(&buffer
, msg
, msg_len
, tree
, parent_buf
, nparents
, author
, committer
, extra
);
1724 if (sign_commit
&& sign_commit_to_strbuf(&sig
, &buffer
, sign_commit
)) {
1728 if (r
->compat_hash_algo
) {
1729 struct commit_extra_header
*compat_extra
= NULL
;
1730 struct object_id mapped_tree
;
1731 struct object_id
*mapped_parents
;
1733 CALLOC_ARRAY(mapped_parents
, nparents
);
1735 if (repo_oid_to_algop(r
, tree
, r
->compat_hash_algo
, &mapped_tree
)) {
1737 free(mapped_parents
);
1740 for (i
= 0; i
< nparents
; i
++)
1741 if (repo_oid_to_algop(r
, &parent_buf
[i
], r
->compat_hash_algo
, &mapped_parents
[i
])) {
1743 free(mapped_parents
);
1746 if (convert_commit_extra_headers(extra
, &compat_extra
)) {
1748 free(mapped_parents
);
1751 write_commit_tree(&compat_buffer
, msg
, msg_len
, &mapped_tree
,
1752 mapped_parents
, nparents
, author
, committer
, compat_extra
);
1753 free_commit_extra_headers(compat_extra
);
1754 free(mapped_parents
);
1756 if (sign_commit
&& sign_commit_to_strbuf(&compat_sig
, &compat_buffer
, sign_commit
)) {
1765 const struct git_hash_algo
*algo
;
1767 { &compat_sig
, r
->compat_hash_algo
},
1768 { &sig
, r
->hash_algo
},
1773 * We write algorithms in the order they were implemented in
1774 * Git to produce a stable hash when multiple algorithms are
1777 if (r
->compat_hash_algo
&& hash_algo_by_ptr(bufs
[0].algo
) > hash_algo_by_ptr(bufs
[1].algo
))
1778 SWAP(bufs
[0], bufs
[1]);
1781 * We traverse each algorithm in order, and apply the signature
1784 for (i
= 0; i
< ARRAY_SIZE(bufs
); i
++) {
1787 add_header_signature(&buffer
, bufs
[i
].sig
, bufs
[i
].algo
);
1788 if (r
->compat_hash_algo
)
1789 add_header_signature(&compat_buffer
, bufs
[i
].sig
, bufs
[i
].algo
);
1793 /* And check the encoding. */
1794 if (encoding_is_utf8
&& (!verify_utf8(&buffer
) || !verify_utf8(&compat_buffer
)))
1795 fprintf(stderr
, _(commit_utf8_warn
));
1797 if (r
->compat_hash_algo
) {
1798 hash_object_file(r
->compat_hash_algo
, compat_buffer
.buf
, compat_buffer
.len
,
1799 OBJ_COMMIT
, &compat_oid_buf
);
1800 compat_oid
= &compat_oid_buf
;
1803 result
= write_object_file_flags(buffer
.buf
, buffer
.len
, OBJ_COMMIT
,
1804 ret
, compat_oid
, 0);
1807 strbuf_release(&buffer
);
1808 strbuf_release(&compat_buffer
);
1809 strbuf_release(&sig
);
1810 strbuf_release(&compat_sig
);
1814 define_commit_slab(merge_desc_slab
, struct merge_remote_desc
*);
1815 static struct merge_desc_slab merge_desc_slab
= COMMIT_SLAB_INIT(1, merge_desc_slab
);
1817 struct merge_remote_desc
*merge_remote_util(const struct commit
*commit
)
1819 return *merge_desc_slab_at(&merge_desc_slab
, commit
);
1822 void set_merge_remote_desc(struct commit
*commit
,
1823 const char *name
, struct object
*obj
)
1825 struct merge_remote_desc
*desc
;
1826 FLEX_ALLOC_STR(desc
, name
, name
);
1828 *merge_desc_slab_at(&merge_desc_slab
, commit
) = desc
;
1831 struct commit
*get_merge_parent(const char *name
)
1834 struct commit
*commit
;
1835 struct object_id oid
;
1836 if (repo_get_oid(the_repository
, name
, &oid
))
1838 obj
= parse_object(the_repository
, &oid
);
1839 commit
= (struct commit
*)repo_peel_to_type(the_repository
, name
, 0,
1841 if (commit
&& !merge_remote_util(commit
))
1842 set_merge_remote_desc(commit
, name
, obj
);
1847 * Append a commit to the end of the commit_list.
1849 * next starts by pointing to the variable that holds the head of an
1850 * empty commit_list, and is updated to point to the "next" field of
1851 * the last item on the list as new commits are appended.
1855 * struct commit_list *list;
1856 * struct commit_list **next = &list;
1858 * next = commit_list_append(c1, next);
1859 * next = commit_list_append(c2, next);
1860 * assert(commit_list_count(list) == 2);
1863 struct commit_list
**commit_list_append(struct commit
*commit
,
1864 struct commit_list
**next
)
1866 struct commit_list
*new_commit
= xmalloc(sizeof(struct commit_list
));
1867 new_commit
->item
= commit
;
1869 new_commit
->next
= NULL
;
1870 return &new_commit
->next
;
1873 const char *find_commit_header(const char *msg
, const char *key
, size_t *out_len
)
1875 int key_len
= strlen(key
);
1876 const char *line
= msg
;
1879 const char *eol
= strchrnul(line
, '\n');
1884 if (eol
- line
> key_len
&&
1885 !strncmp(line
, key
, key_len
) &&
1886 line
[key_len
] == ' ') {
1887 *out_len
= eol
- line
- key_len
- 1;
1888 return line
+ key_len
+ 1;
1890 line
= *eol
? eol
+ 1 : NULL
;
1896 * Inspect the given string and determine the true "end" of the log message, in
1897 * order to find where to put a new Signed-off-by trailer. Ignored are
1898 * trailing comment lines and blank lines. To support "git commit -s
1899 * --amend" on an existing commit, we also ignore "Conflicts:". To
1900 * support "git commit -v", we truncate at cut lines.
1902 * Returns the number of bytes from the tail to ignore, to be fed as
1903 * the second parameter to append_signoff().
1905 size_t ignored_log_message_bytes(const char *buf
, size_t len
)
1909 int in_old_conflicts_block
= 0;
1910 size_t cutoff
= wt_status_locate_end(buf
, len
);
1912 while (bol
< cutoff
) {
1913 const char *next_line
= memchr(buf
+ bol
, '\n', len
- bol
);
1916 next_line
= buf
+ len
;
1920 if (starts_with_mem(buf
+ bol
, cutoff
- bol
, comment_line_str
) ||
1922 /* is this the first of the run of comments? */
1925 /* otherwise, it is just continuing */
1926 } else if (starts_with(buf
+ bol
, "Conflicts:\n")) {
1927 in_old_conflicts_block
= 1;
1930 } else if (in_old_conflicts_block
&& buf
[bol
] == '\t') {
1931 ; /* a pathname in the conflicts block */
1933 /* the previous was not trailing comment */
1935 in_old_conflicts_block
= 0;
1937 bol
= next_line
- buf
;
1939 return boc
? len
- boc
: len
- cutoff
;
1942 int run_commit_hook(int editor_is_used
, const char *index_file
,
1943 int *invoked_hook
, const char *name
, ...)
1945 struct run_hooks_opt opt
= RUN_HOOKS_OPT_INIT
;
1949 strvec_pushf(&opt
.env
, "GIT_INDEX_FILE=%s", index_file
);
1952 * Let the hook know that no editor will be launched.
1954 if (!editor_is_used
)
1955 strvec_push(&opt
.env
, "GIT_EDITOR=:");
1957 va_start(args
, name
);
1958 while ((arg
= va_arg(args
, const char *)))
1959 strvec_push(&opt
.args
, arg
);
1962 opt
.invoked_hook
= invoked_hook
;
1963 return run_hooks_opt(name
, &opt
);