1 #include "git-compat-util.h"
4 #include "commit-graph.h"
5 #include "environment.h"
8 #include "repository.h"
9 #include "object-name.h"
10 #include "object-store-ll.h"
17 #include "gpg-interface.h"
18 #include "mergesort.h"
19 #include "commit-slab.h"
20 #include "prio-queue.h"
21 #include "hash-lookup.h"
22 #include "wt-status.h"
25 #include "commit-reach.h"
26 #include "run-command.h"
33 static struct commit_extra_header
*read_commit_extra_header_lines(const char *buf
, size_t len
, const char **);
35 int save_commit_buffer
= 1;
36 int no_graft_file_deprecated_advice
;
38 const char *commit_type
= "commit";
40 struct commit
*lookup_commit_reference_gently(struct repository
*r
,
41 const struct object_id
*oid
, int quiet
)
43 struct object
*obj
= deref_tag(r
,
49 return object_as_type(obj
, OBJ_COMMIT
, quiet
);
52 struct commit
*lookup_commit_reference(struct repository
*r
, const struct object_id
*oid
)
54 return lookup_commit_reference_gently(r
, oid
, 0);
57 struct commit
*lookup_commit_or_die(const struct object_id
*oid
, const char *ref_name
)
59 struct commit
*c
= lookup_commit_reference(the_repository
, oid
);
61 die(_("could not parse %s"), ref_name
);
62 if (!oideq(oid
, &c
->object
.oid
)) {
63 warning(_("%s %s is not a commit!"),
64 ref_name
, oid_to_hex(oid
));
69 struct commit
*lookup_commit_object(struct repository
*r
,
70 const struct object_id
*oid
)
72 struct object
*obj
= parse_object(r
, oid
);
73 return obj
? object_as_type(obj
, OBJ_COMMIT
, 0) : NULL
;
77 struct commit
*lookup_commit(struct repository
*r
, const struct object_id
*oid
)
79 struct object
*obj
= lookup_object(r
, oid
);
81 return create_object(r
, oid
, alloc_commit_node(r
));
82 return object_as_type(obj
, OBJ_COMMIT
, 0);
85 struct commit
*lookup_commit_reference_by_name(const char *name
)
88 struct commit
*commit
;
90 if (repo_get_oid_committish(the_repository
, name
, &oid
))
92 commit
= lookup_commit_reference(the_repository
, &oid
);
93 if (repo_parse_commit(the_repository
, commit
))
98 static timestamp_t
parse_commit_date(const char *buf
, const char *tail
)
105 if (memcmp(buf
, "author", 6))
107 while (buf
< tail
&& *buf
++ != '\n')
111 if (memcmp(buf
, "committer", 9))
115 * Jump to end-of-line so that we can walk backwards to find the
116 * end-of-email ">". This is more forgiving of malformed cases
117 * because unexpected characters tend to be in the name and email
120 eol
= memchr(buf
, '\n', tail
- buf
);
124 while (dateptr
> buf
&& dateptr
[-1] != '>')
130 * Trim leading whitespace, but make sure we have at least one
131 * non-whitespace character, as parse_timestamp() will otherwise walk
132 * right past the newline we found in "eol" when skipping whitespace
135 * In theory it would be sufficient to allow any character not matched
136 * by isspace(), but there's a catch: our isspace() does not
137 * necessarily match the behavior of parse_timestamp(), as the latter
138 * is implemented by system routines which match more exotic control
139 * codes, or even locale-dependent sequences.
141 * Since we expect the timestamp to be a number, we can check for that.
142 * Anything else (e.g., a non-numeric token like "foo") would just
143 * cause parse_timestamp() to return 0 anyway.
145 while (dateptr
< eol
&& isspace(*dateptr
))
147 if (!isdigit(*dateptr
) && *dateptr
!= '-')
151 * We know there is at least one digit (or dash), so we'll begin
152 * parsing there and stop at worst case at eol.
154 * Note that we may feed parse_timestamp() extra characters here if the
155 * commit is malformed, and it will parse as far as it can. For
156 * example, "123foo456" would return "123". That might be questionable
157 * (versus returning "0"), but it would help in a hypothetical case
158 * like "123456+0100", where the whitespace from the timezone is
159 * missing. Since such syntactic errors may be baked into history and
160 * hard to correct now, let's err on trying to make our best guess
161 * here, rather than insist on perfect syntax.
163 return parse_timestamp(dateptr
, NULL
, 10);
166 static const struct object_id
*commit_graft_oid_access(size_t index
, const void *table
)
168 const struct commit_graft
* const *commit_graft_table
= table
;
169 return &commit_graft_table
[index
]->oid
;
172 int commit_graft_pos(struct repository
*r
, const struct object_id
*oid
)
174 return oid_pos(oid
, r
->parsed_objects
->grafts
,
175 r
->parsed_objects
->grafts_nr
,
176 commit_graft_oid_access
);
179 static void unparse_commit(struct repository
*r
, const struct object_id
*oid
)
181 struct commit
*c
= lookup_commit(r
, oid
);
183 if (!c
->object
.parsed
)
185 free_commit_list(c
->parents
);
187 c
->object
.parsed
= 0;
190 int register_commit_graft(struct repository
*r
, struct commit_graft
*graft
,
193 int pos
= commit_graft_pos(r
, &graft
->oid
);
199 free(r
->parsed_objects
->grafts
[pos
]);
200 r
->parsed_objects
->grafts
[pos
] = graft
;
205 ALLOC_GROW(r
->parsed_objects
->grafts
,
206 r
->parsed_objects
->grafts_nr
+ 1,
207 r
->parsed_objects
->grafts_alloc
);
208 r
->parsed_objects
->grafts_nr
++;
209 if (pos
< r
->parsed_objects
->grafts_nr
)
210 memmove(r
->parsed_objects
->grafts
+ pos
+ 1,
211 r
->parsed_objects
->grafts
+ pos
,
212 (r
->parsed_objects
->grafts_nr
- pos
- 1) *
213 sizeof(*r
->parsed_objects
->grafts
));
214 r
->parsed_objects
->grafts
[pos
] = graft
;
215 unparse_commit(r
, &graft
->oid
);
219 struct commit_graft
*read_graft_line(struct strbuf
*line
)
221 /* The format is just "Commit Parent1 Parent2 ...\n" */
223 const char *tail
= NULL
;
224 struct commit_graft
*graft
= NULL
;
225 struct object_id dummy_oid
, *oid
;
228 if (!line
->len
|| line
->buf
[0] == '#')
231 * phase 0 verifies line, counts hashes in line and allocates graft
232 * phase 1 fills graft
234 for (phase
= 0; phase
< 2; phase
++) {
235 oid
= graft
? &graft
->oid
: &dummy_oid
;
236 if (parse_oid_hex(line
->buf
, oid
, &tail
))
238 for (i
= 0; *tail
!= '\0'; i
++) {
239 oid
= graft
? &graft
->parent
[i
] : &dummy_oid
;
240 if (!isspace(*tail
++) || parse_oid_hex(tail
, oid
, &tail
))
244 graft
= xmalloc(st_add(sizeof(*graft
),
245 st_mult(sizeof(struct object_id
), i
)));
246 graft
->nr_parent
= i
;
252 error("bad graft data: %s", line
->buf
);
257 static int read_graft_file(struct repository
*r
, const char *graft_file
)
259 FILE *fp
= fopen_or_warn(graft_file
, "r");
260 struct strbuf buf
= STRBUF_INIT
;
263 if (!no_graft_file_deprecated_advice
&&
264 advice_enabled(ADVICE_GRAFT_FILE_DEPRECATED
))
265 advise(_("Support for <GIT_DIR>/info/grafts is deprecated\n"
266 "and will be removed in a future Git version.\n"
268 "Please use \"git replace --convert-graft-file\"\n"
269 "to convert the grafts into replace refs.\n"
271 "Turn this message off by running\n"
272 "\"git config advice.graftFileDeprecated false\""));
273 while (!strbuf_getwholeline(&buf
, fp
, '\n')) {
274 /* The format is just "Commit Parent1 Parent2 ...\n" */
275 struct commit_graft
*graft
= read_graft_line(&buf
);
278 if (register_commit_graft(r
, graft
, 1))
279 error("duplicate graft data: %s", buf
.buf
);
282 strbuf_release(&buf
);
286 void prepare_commit_graft(struct repository
*r
)
290 if (r
->parsed_objects
->commit_graft_prepared
)
292 if (!startup_info
->have_repository
)
295 graft_file
= get_graft_file(r
);
296 read_graft_file(r
, graft_file
);
297 /* make sure shallows are read */
298 is_repository_shallow(r
);
299 r
->parsed_objects
->commit_graft_prepared
= 1;
302 struct commit_graft
*lookup_commit_graft(struct repository
*r
, const struct object_id
*oid
)
305 prepare_commit_graft(r
);
306 pos
= commit_graft_pos(r
, oid
);
309 return r
->parsed_objects
->grafts
[pos
];
312 int for_each_commit_graft(each_commit_graft_fn fn
, void *cb_data
)
315 for (i
= ret
= 0; i
< the_repository
->parsed_objects
->grafts_nr
&& !ret
; i
++)
316 ret
= fn(the_repository
->parsed_objects
->grafts
[i
], cb_data
);
320 void reset_commit_grafts(struct repository
*r
)
324 for (i
= 0; i
< r
->parsed_objects
->grafts_nr
; i
++) {
325 unparse_commit(r
, &r
->parsed_objects
->grafts
[i
]->oid
);
326 free(r
->parsed_objects
->grafts
[i
]);
328 r
->parsed_objects
->grafts_nr
= 0;
329 r
->parsed_objects
->commit_graft_prepared
= 0;
332 struct commit_buffer
{
336 define_commit_slab(buffer_slab
, struct commit_buffer
);
338 struct buffer_slab
*allocate_commit_buffer_slab(void)
340 struct buffer_slab
*bs
= xmalloc(sizeof(*bs
));
341 init_buffer_slab(bs
);
345 void free_commit_buffer_slab(struct buffer_slab
*bs
)
347 clear_buffer_slab(bs
);
351 void set_commit_buffer(struct repository
*r
, struct commit
*commit
, void *buffer
, unsigned long size
)
353 struct commit_buffer
*v
= buffer_slab_at(
354 r
->parsed_objects
->buffer_slab
, commit
);
359 const void *get_cached_commit_buffer(struct repository
*r
, const struct commit
*commit
, unsigned long *sizep
)
361 struct commit_buffer
*v
= buffer_slab_peek(
362 r
->parsed_objects
->buffer_slab
, commit
);
373 const void *repo_get_commit_buffer(struct repository
*r
,
374 const struct commit
*commit
,
375 unsigned long *sizep
)
377 const void *ret
= get_cached_commit_buffer(r
, commit
, sizep
);
379 enum object_type type
;
381 ret
= repo_read_object_file(r
, &commit
->object
.oid
, &type
, &size
);
383 die("cannot read commit object %s",
384 oid_to_hex(&commit
->object
.oid
));
385 if (type
!= OBJ_COMMIT
)
386 die("expected commit for %s, got %s",
387 oid_to_hex(&commit
->object
.oid
), type_name(type
));
394 void repo_unuse_commit_buffer(struct repository
*r
,
395 const struct commit
*commit
,
398 struct commit_buffer
*v
= buffer_slab_peek(
399 r
->parsed_objects
->buffer_slab
, commit
);
400 if (!(v
&& v
->buffer
== buffer
))
401 free((void *)buffer
);
404 void free_commit_buffer(struct parsed_object_pool
*pool
, struct commit
*commit
)
406 struct commit_buffer
*v
= buffer_slab_peek(
407 pool
->buffer_slab
, commit
);
409 FREE_AND_NULL(v
->buffer
);
414 static inline void set_commit_tree(struct commit
*c
, struct tree
*t
)
419 struct tree
*repo_get_commit_tree(struct repository
*r
,
420 const struct commit
*commit
)
422 if (commit
->maybe_tree
|| !commit
->object
.parsed
)
423 return commit
->maybe_tree
;
425 if (commit_graph_position(commit
) != COMMIT_NOT_FROM_GRAPH
)
426 return get_commit_tree_in_graph(r
, commit
);
431 struct object_id
*get_commit_tree_oid(const struct commit
*commit
)
433 struct tree
*tree
= repo_get_commit_tree(the_repository
, commit
);
434 return tree
? &tree
->object
.oid
: NULL
;
437 void release_commit_memory(struct parsed_object_pool
*pool
, struct commit
*c
)
439 set_commit_tree(c
, NULL
);
440 free_commit_buffer(pool
, c
);
442 free_commit_list(c
->parents
);
444 c
->object
.parsed
= 0;
447 const void *detach_commit_buffer(struct commit
*commit
, unsigned long *sizep
)
449 struct commit_buffer
*v
= buffer_slab_peek(
450 the_repository
->parsed_objects
->buffer_slab
, commit
);
467 int parse_commit_buffer(struct repository
*r
, struct commit
*item
, const void *buffer
, unsigned long size
, int check_graph
)
469 const char *tail
= buffer
;
470 const char *bufptr
= buffer
;
471 struct object_id parent
;
472 struct commit_list
**pptr
;
473 struct commit_graft
*graft
;
474 const int tree_entry_len
= the_hash_algo
->hexsz
+ 5;
475 const int parent_entry_len
= the_hash_algo
->hexsz
+ 7;
478 if (item
->object
.parsed
)
481 * Presumably this is leftover from an earlier failed parse;
482 * clear it out in preparation for us re-parsing (we'll hit the
483 * same error, but that's good, since it lets our caller know
484 * the result cannot be trusted.
486 free_commit_list(item
->parents
);
487 item
->parents
= NULL
;
490 if (tail
<= bufptr
+ tree_entry_len
+ 1 || memcmp(bufptr
, "tree ", 5) ||
491 bufptr
[tree_entry_len
] != '\n')
492 return error("bogus commit object %s", oid_to_hex(&item
->object
.oid
));
493 if (get_oid_hex(bufptr
+ 5, &parent
) < 0)
494 return error("bad tree pointer in commit %s",
495 oid_to_hex(&item
->object
.oid
));
496 tree
= lookup_tree(r
, &parent
);
498 return error("bad tree pointer %s in commit %s",
500 oid_to_hex(&item
->object
.oid
));
501 set_commit_tree(item
, tree
);
502 bufptr
+= tree_entry_len
+ 1; /* "tree " + "hex sha1" + "\n" */
503 pptr
= &item
->parents
;
505 graft
= lookup_commit_graft(r
, &item
->object
.oid
);
507 r
->parsed_objects
->substituted_parent
= 1;
508 while (bufptr
+ parent_entry_len
< tail
&& !memcmp(bufptr
, "parent ", 7)) {
509 struct commit
*new_parent
;
511 if (tail
<= bufptr
+ parent_entry_len
+ 1 ||
512 get_oid_hex(bufptr
+ 7, &parent
) ||
513 bufptr
[parent_entry_len
] != '\n')
514 return error("bad parents in commit %s", oid_to_hex(&item
->object
.oid
));
515 bufptr
+= parent_entry_len
+ 1;
517 * The clone is shallow if nr_parent < 0, and we must
518 * not traverse its real parents even when we unhide them.
520 if (graft
&& (graft
->nr_parent
< 0 || !grafts_keep_true_parents
))
522 new_parent
= lookup_commit(r
, &parent
);
524 return error("bad parent %s in commit %s",
526 oid_to_hex(&item
->object
.oid
));
527 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
531 struct commit
*new_parent
;
532 for (i
= 0; i
< graft
->nr_parent
; i
++) {
533 new_parent
= lookup_commit(r
,
536 return error("bad graft parent %s in commit %s",
537 oid_to_hex(&graft
->parent
[i
]),
538 oid_to_hex(&item
->object
.oid
));
539 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
542 item
->date
= parse_commit_date(bufptr
, tail
);
545 load_commit_graph_info(r
, item
);
547 item
->object
.parsed
= 1;
551 int repo_parse_commit_internal(struct repository
*r
,
553 int quiet_on_missing
,
554 int use_commit_graph
)
556 enum object_type type
;
559 struct object_info oi
= {
565 * Git does not support partial clones that exclude commits, so set
566 * OBJECT_INFO_SKIP_FETCH_OBJECT to fail fast when an object is missing.
568 int flags
= OBJECT_INFO_LOOKUP_REPLACE
| OBJECT_INFO_SKIP_FETCH_OBJECT
|
569 OBJECT_INFO_DIE_IF_CORRUPT
;
574 if (item
->object
.parsed
)
576 if (use_commit_graph
&& parse_commit_in_graph(r
, item
)) {
577 static int commit_graph_paranoia
= -1;
579 if (commit_graph_paranoia
== -1)
580 commit_graph_paranoia
= git_env_bool(GIT_COMMIT_GRAPH_PARANOIA
, 0);
582 if (commit_graph_paranoia
&& !has_object(r
, &item
->object
.oid
, 0)) {
583 unparse_commit(r
, &item
->object
.oid
);
584 return quiet_on_missing
? -1 :
585 error(_("commit %s exists in commit-graph but not in the object database"),
586 oid_to_hex(&item
->object
.oid
));
592 if (oid_object_info_extended(r
, &item
->object
.oid
, &oi
, flags
) < 0)
593 return quiet_on_missing
? -1 :
594 error("Could not read %s",
595 oid_to_hex(&item
->object
.oid
));
596 if (type
!= OBJ_COMMIT
) {
598 return error("Object %s not a commit",
599 oid_to_hex(&item
->object
.oid
));
602 ret
= parse_commit_buffer(r
, item
, buffer
, size
, 0);
603 if (save_commit_buffer
&& !ret
) {
604 set_commit_buffer(r
, item
, buffer
, size
);
611 int repo_parse_commit_gently(struct repository
*r
,
612 struct commit
*item
, int quiet_on_missing
)
614 return repo_parse_commit_internal(r
, item
, quiet_on_missing
, 1);
617 void parse_commit_or_die(struct commit
*item
)
619 if (repo_parse_commit(the_repository
, item
))
620 die("unable to parse commit %s",
621 item
? oid_to_hex(&item
->object
.oid
) : "(null)");
624 int find_commit_subject(const char *commit_buffer
, const char **subject
)
627 const char *p
= commit_buffer
;
629 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
632 p
= skip_blank_lines(p
+ 2);
633 eol
= strchrnul(p
, '\n');
642 size_t commit_subject_length(const char *body
)
644 const char *p
= body
;
646 const char *next
= skip_blank_lines(p
);
649 p
= strchrnul(p
, '\n');
656 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
658 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
659 new_list
->item
= item
;
660 new_list
->next
= *list_p
;
665 int commit_list_contains(struct commit
*item
, struct commit_list
*list
)
668 if (list
->item
== item
)
676 unsigned commit_list_count(const struct commit_list
*l
)
679 for (; l
; l
= l
->next
)
684 struct commit_list
*copy_commit_list(struct commit_list
*list
)
686 struct commit_list
*head
= NULL
;
687 struct commit_list
**pp
= &head
;
689 pp
= commit_list_append(list
->item
, pp
);
695 struct commit_list
*reverse_commit_list(struct commit_list
*list
)
697 struct commit_list
*next
= NULL
, *current
, *backup
;
698 for (current
= list
; current
; current
= backup
) {
699 backup
= current
->next
;
700 current
->next
= next
;
706 void free_commit_list(struct commit_list
*list
)
712 struct commit_list
* commit_list_insert_by_date(struct commit
*item
, struct commit_list
**list
)
714 struct commit_list
**pp
= list
;
715 struct commit_list
*p
;
716 while ((p
= *pp
) != NULL
) {
717 if (p
->item
->date
< item
->date
) {
722 return commit_list_insert(item
, pp
);
725 static int commit_list_compare_by_date(const struct commit_list
*a
,
726 const struct commit_list
*b
)
728 timestamp_t a_date
= a
->item
->date
;
729 timestamp_t b_date
= b
->item
->date
;
737 DEFINE_LIST_SORT(static, commit_list_sort
, struct commit_list
, next
);
739 void commit_list_sort_by_date(struct commit_list
**list
)
741 commit_list_sort(list
, commit_list_compare_by_date
);
744 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
747 struct commit
*ret
= pop_commit(list
);
748 struct commit_list
*parents
= ret
->parents
;
751 struct commit
*commit
= parents
->item
;
752 if (!repo_parse_commit(the_repository
, commit
) && !(commit
->object
.flags
& mark
)) {
753 commit
->object
.flags
|= mark
;
754 commit_list_insert_by_date(commit
, list
);
756 parents
= parents
->next
;
761 static void clear_commit_marks_1(struct commit_list
**plist
,
762 struct commit
*commit
, unsigned int mark
)
765 struct commit_list
*parents
;
767 if (!(mark
& commit
->object
.flags
))
770 commit
->object
.flags
&= ~mark
;
772 parents
= commit
->parents
;
776 while ((parents
= parents
->next
)) {
777 if (parents
->item
->object
.flags
& mark
)
778 commit_list_insert(parents
->item
, plist
);
781 commit
= commit
->parents
->item
;
785 void clear_commit_marks_many(int nr
, struct commit
**commit
, unsigned int mark
)
787 struct commit_list
*list
= NULL
;
790 clear_commit_marks_1(&list
, *commit
, mark
);
794 clear_commit_marks_1(&list
, pop_commit(&list
), mark
);
797 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
799 clear_commit_marks_many(1, &commit
, mark
);
802 struct commit
*pop_commit(struct commit_list
**stack
)
804 struct commit_list
*top
= *stack
;
805 struct commit
*item
= top
? top
->item
: NULL
;
815 * Topological sort support
818 /* count number of children that have not been emitted */
819 define_commit_slab(indegree_slab
, int);
821 define_commit_slab(author_date_slab
, timestamp_t
);
823 void record_author_date(struct author_date_slab
*author_date
,
824 struct commit
*commit
)
826 const char *buffer
= repo_get_commit_buffer(the_repository
, commit
,
828 struct ident_split ident
;
829 const char *ident_line
;
834 ident_line
= find_commit_header(buffer
, "author", &ident_len
);
836 goto fail_exit
; /* no author line */
837 if (split_ident_line(&ident
, ident_line
, ident_len
) ||
838 !ident
.date_begin
|| !ident
.date_end
)
839 goto fail_exit
; /* malformed "author" line */
841 date
= parse_timestamp(ident
.date_begin
, &date_end
, 10);
842 if (date_end
!= ident
.date_end
)
843 goto fail_exit
; /* malformed date */
844 *(author_date_slab_at(author_date
, commit
)) = date
;
847 repo_unuse_commit_buffer(the_repository
, commit
, buffer
);
850 int compare_commits_by_author_date(const void *a_
, const void *b_
,
853 const struct commit
*a
= a_
, *b
= b_
;
854 struct author_date_slab
*author_date
= cb_data
;
855 timestamp_t a_date
= *(author_date_slab_at(author_date
, a
));
856 timestamp_t b_date
= *(author_date_slab_at(author_date
, b
));
858 /* newer commits with larger date first */
861 else if (a_date
> b_date
)
866 int compare_commits_by_gen_then_commit_date(const void *a_
, const void *b_
,
869 const struct commit
*a
= a_
, *b
= b_
;
870 const timestamp_t generation_a
= commit_graph_generation(a
),
871 generation_b
= commit_graph_generation(b
);
873 /* newer commits first */
874 if (generation_a
< generation_b
)
876 else if (generation_a
> generation_b
)
879 /* use date as a heuristic when generations are equal */
880 if (a
->date
< b
->date
)
882 else if (a
->date
> b
->date
)
887 int compare_commits_by_commit_date(const void *a_
, const void *b_
,
890 const struct commit
*a
= a_
, *b
= b_
;
891 /* newer commits with larger date first */
892 if (a
->date
< b
->date
)
894 else if (a
->date
> b
->date
)
900 * Performs an in-place topological sort on the list supplied.
902 void sort_in_topological_order(struct commit_list
**list
, enum rev_sort_order sort_order
)
904 struct commit_list
*next
, *orig
= *list
;
905 struct commit_list
**pptr
;
906 struct indegree_slab indegree
;
907 struct prio_queue queue
;
908 struct commit
*commit
;
909 struct author_date_slab author_date
;
915 init_indegree_slab(&indegree
);
916 memset(&queue
, '\0', sizeof(queue
));
918 switch (sort_order
) {
919 default: /* REV_SORT_IN_GRAPH_ORDER */
920 queue
.compare
= NULL
;
922 case REV_SORT_BY_COMMIT_DATE
:
923 queue
.compare
= compare_commits_by_commit_date
;
925 case REV_SORT_BY_AUTHOR_DATE
:
926 init_author_date_slab(&author_date
);
927 queue
.compare
= compare_commits_by_author_date
;
928 queue
.cb_data
= &author_date
;
932 /* Mark them and clear the indegree */
933 for (next
= orig
; next
; next
= next
->next
) {
934 struct commit
*commit
= next
->item
;
935 *(indegree_slab_at(&indegree
, commit
)) = 1;
936 /* also record the author dates, if needed */
937 if (sort_order
== REV_SORT_BY_AUTHOR_DATE
)
938 record_author_date(&author_date
, commit
);
941 /* update the indegree */
942 for (next
= orig
; next
; next
= next
->next
) {
943 struct commit_list
*parents
= next
->item
->parents
;
945 struct commit
*parent
= parents
->item
;
946 int *pi
= indegree_slab_at(&indegree
, parent
);
950 parents
= parents
->next
;
957 * tips are nodes not reachable from any other node in the list
959 * the tips serve as a starting set for the work queue.
961 for (next
= orig
; next
; next
= next
->next
) {
962 struct commit
*commit
= next
->item
;
964 if (*(indegree_slab_at(&indegree
, commit
)) == 1)
965 prio_queue_put(&queue
, commit
);
969 * This is unfortunate; the initial tips need to be shown
970 * in the order given from the revision traversal machinery.
972 if (sort_order
== REV_SORT_IN_GRAPH_ORDER
)
973 prio_queue_reverse(&queue
);
975 /* We no longer need the commit list */
976 free_commit_list(orig
);
980 while ((commit
= prio_queue_get(&queue
)) != NULL
) {
981 struct commit_list
*parents
;
983 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
984 struct commit
*parent
= parents
->item
;
985 int *pi
= indegree_slab_at(&indegree
, parent
);
991 * parents are only enqueued for emission
992 * when all their children have been emitted thereby
993 * guaranteeing topological order.
996 prio_queue_put(&queue
, parent
);
999 * all children of commit have already been
1000 * emitted. we can emit it now.
1002 *(indegree_slab_at(&indegree
, commit
)) = 0;
1004 pptr
= &commit_list_insert(commit
, pptr
)->next
;
1007 clear_indegree_slab(&indegree
);
1008 clear_prio_queue(&queue
);
1009 if (sort_order
== REV_SORT_BY_AUTHOR_DATE
)
1010 clear_author_date_slab(&author_date
);
1013 struct rev_collect
{
1014 struct commit
**commit
;
1017 unsigned int initial
: 1;
1020 static void add_one_commit(struct object_id
*oid
, struct rev_collect
*revs
)
1022 struct commit
*commit
;
1024 if (is_null_oid(oid
))
1027 commit
= lookup_commit(the_repository
, oid
);
1029 (commit
->object
.flags
& TMP_MARK
) ||
1030 repo_parse_commit(the_repository
, commit
))
1033 ALLOC_GROW(revs
->commit
, revs
->nr
+ 1, revs
->alloc
);
1034 revs
->commit
[revs
->nr
++] = commit
;
1035 commit
->object
.flags
|= TMP_MARK
;
1038 static int collect_one_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
1039 const char *ident UNUSED
,
1040 timestamp_t timestamp UNUSED
, int tz UNUSED
,
1041 const char *message UNUSED
, void *cbdata
)
1043 struct rev_collect
*revs
= cbdata
;
1045 if (revs
->initial
) {
1047 add_one_commit(ooid
, revs
);
1049 add_one_commit(noid
, revs
);
1053 struct commit
*get_fork_point(const char *refname
, struct commit
*commit
)
1055 struct object_id oid
;
1056 struct rev_collect revs
;
1057 struct commit_list
*bases
;
1059 struct commit
*ret
= NULL
;
1062 switch (repo_dwim_ref(the_repository
, refname
, strlen(refname
), &oid
,
1063 &full_refname
, 0)) {
1065 die("No such ref: '%s'", refname
);
1069 die("Ambiguous refname: '%s'", refname
);
1072 memset(&revs
, 0, sizeof(revs
));
1074 for_each_reflog_ent(full_refname
, collect_one_reflog_ent
, &revs
);
1077 add_one_commit(&oid
, &revs
);
1079 for (i
= 0; i
< revs
.nr
; i
++)
1080 revs
.commit
[i
]->object
.flags
&= ~TMP_MARK
;
1082 bases
= repo_get_merge_bases_many(the_repository
, commit
, revs
.nr
,
1086 * There should be one and only one merge base, when we found
1087 * a common ancestor among reflog entries.
1089 if (!bases
|| bases
->next
)
1090 goto cleanup_return
;
1092 /* And the found one must be one of the reflog entries */
1093 for (i
= 0; i
< revs
.nr
; i
++)
1094 if (&bases
->item
->object
== &revs
.commit
[i
]->object
)
1097 goto cleanup_return
;
1103 free_commit_list(bases
);
1109 * Indexed by hash algorithm identifier.
1111 static const char *gpg_sig_headers
[] = {
1117 int sign_with_header(struct strbuf
*buf
, const char *keyid
)
1119 struct strbuf sig
= STRBUF_INIT
;
1120 int inspos
, copypos
;
1122 const char *gpg_sig_header
= gpg_sig_headers
[hash_algo_by_ptr(the_hash_algo
)];
1123 int gpg_sig_header_len
= strlen(gpg_sig_header
);
1125 /* find the end of the header */
1126 eoh
= strstr(buf
->buf
, "\n\n");
1130 inspos
= eoh
- buf
->buf
+ 1;
1132 if (!keyid
|| !*keyid
)
1133 keyid
= get_signing_key();
1134 if (sign_buffer(buf
, &sig
, keyid
)) {
1135 strbuf_release(&sig
);
1139 for (copypos
= 0; sig
.buf
[copypos
]; ) {
1140 const char *bol
= sig
.buf
+ copypos
;
1141 const char *eol
= strchrnul(bol
, '\n');
1142 int len
= (eol
- bol
) + !!*eol
;
1145 strbuf_insert(buf
, inspos
, gpg_sig_header
, gpg_sig_header_len
);
1146 inspos
+= gpg_sig_header_len
;
1148 strbuf_insertstr(buf
, inspos
++, " ");
1149 strbuf_insert(buf
, inspos
, bol
, len
);
1153 strbuf_release(&sig
);
1159 int parse_signed_commit(const struct commit
*commit
,
1160 struct strbuf
*payload
, struct strbuf
*signature
,
1161 const struct git_hash_algo
*algop
)
1164 const char *buffer
= repo_get_commit_buffer(the_repository
, commit
,
1166 int ret
= parse_buffer_signed_by_header(buffer
, size
, payload
, signature
, algop
);
1168 repo_unuse_commit_buffer(the_repository
, commit
, buffer
);
1172 int parse_buffer_signed_by_header(const char *buffer
,
1174 struct strbuf
*payload
,
1175 struct strbuf
*signature
,
1176 const struct git_hash_algo
*algop
)
1178 int in_signature
= 0, saw_signature
= 0, other_signature
= 0;
1179 const char *line
, *tail
, *p
;
1180 const char *gpg_sig_header
= gpg_sig_headers
[hash_algo_by_ptr(algop
)];
1183 tail
= buffer
+ size
;
1184 while (line
< tail
) {
1185 const char *sig
= NULL
;
1186 const char *next
= memchr(line
, '\n', tail
- line
);
1188 next
= next
? next
+ 1 : tail
;
1189 if (in_signature
&& line
[0] == ' ')
1191 else if (skip_prefix(line
, gpg_sig_header
, &p
) &&
1193 sig
= line
+ strlen(gpg_sig_header
) + 1;
1194 other_signature
= 0;
1196 else if (starts_with(line
, "gpgsig"))
1197 other_signature
= 1;
1198 else if (other_signature
&& line
[0] != ' ')
1199 other_signature
= 0;
1201 strbuf_add(signature
, sig
, next
- sig
);
1206 /* dump the whole remainder of the buffer */
1208 if (!other_signature
)
1209 strbuf_add(payload
, line
, next
- line
);
1214 return saw_signature
;
1217 int remove_signature(struct strbuf
*buf
)
1219 const char *line
= buf
->buf
;
1220 const char *tail
= buf
->buf
+ buf
->len
;
1221 int in_signature
= 0;
1225 } sigs
[2], *sigp
= &sigs
[0];
1227 const char *orig_buf
= buf
->buf
;
1229 memset(sigs
, 0, sizeof(sigs
));
1231 while (line
< tail
) {
1232 const char *next
= memchr(line
, '\n', tail
- line
);
1233 next
= next
? next
+ 1 : tail
;
1235 if (in_signature
&& line
[0] == ' ')
1237 else if (starts_with(line
, "gpgsig")) {
1239 for (i
= 1; i
< GIT_HASH_NALGOS
; i
++) {
1241 if (skip_prefix(line
, gpg_sig_headers
[i
], &p
) &&
1250 /* dump the whole remainder of the buffer */
1252 if (in_signature
&& sigp
- sigs
!= ARRAY_SIZE(sigs
))
1259 for (i
= ARRAY_SIZE(sigs
) - 1; i
>= 0; i
--)
1261 strbuf_remove(buf
, sigs
[i
].start
- orig_buf
, sigs
[i
].end
- sigs
[i
].start
);
1263 return sigs
[0].start
!= NULL
;
1266 static void handle_signed_tag(struct commit
*parent
, struct commit_extra_header
***tail
)
1268 struct merge_remote_desc
*desc
;
1269 struct commit_extra_header
*mergetag
;
1272 enum object_type type
;
1273 struct strbuf payload
= STRBUF_INIT
;
1274 struct strbuf signature
= STRBUF_INIT
;
1276 desc
= merge_remote_util(parent
);
1277 if (!desc
|| !desc
->obj
)
1279 buf
= repo_read_object_file(the_repository
, &desc
->obj
->oid
, &type
,
1281 if (!buf
|| type
!= OBJ_TAG
)
1283 if (!parse_signature(buf
, size
, &payload
, &signature
))
1286 * We could verify this signature and either omit the tag when
1287 * it does not validate, but the integrator may not have the
1288 * public key of the signer of the tag being merged, while a
1289 * later auditor may have it while auditing, so let's not run
1290 * verify-signed-buffer here for now...
1292 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1293 * warn("warning: signed tag unverified.");
1295 CALLOC_ARRAY(mergetag
, 1);
1296 mergetag
->key
= xstrdup("mergetag");
1297 mergetag
->value
= buf
;
1298 mergetag
->len
= size
;
1301 *tail
= &mergetag
->next
;
1302 strbuf_release(&payload
);
1303 strbuf_release(&signature
);
1310 int check_commit_signature(const struct commit
*commit
, struct signature_check
*sigc
)
1312 struct strbuf payload
= STRBUF_INIT
;
1313 struct strbuf signature
= STRBUF_INIT
;
1318 if (parse_signed_commit(commit
, &payload
, &signature
, the_hash_algo
) <= 0)
1321 sigc
->payload_type
= SIGNATURE_PAYLOAD_COMMIT
;
1322 sigc
->payload
= strbuf_detach(&payload
, &sigc
->payload_len
);
1323 ret
= check_signature(sigc
, signature
.buf
, signature
.len
);
1326 strbuf_release(&payload
);
1327 strbuf_release(&signature
);
1332 void verify_merge_signature(struct commit
*commit
, int verbosity
,
1335 char hex
[GIT_MAX_HEXSZ
+ 1];
1336 struct signature_check signature_check
;
1338 memset(&signature_check
, 0, sizeof(signature_check
));
1340 ret
= check_commit_signature(commit
, &signature_check
);
1342 repo_find_unique_abbrev_r(the_repository
, hex
, &commit
->object
.oid
,
1344 switch (signature_check
.result
) {
1346 if (ret
|| (check_trust
&& signature_check
.trust_level
< TRUST_MARGINAL
))
1347 die(_("Commit %s has an untrusted GPG signature, "
1348 "allegedly by %s."), hex
, signature_check
.signer
);
1351 die(_("Commit %s has a bad GPG signature "
1352 "allegedly by %s."), hex
, signature_check
.signer
);
1354 die(_("Commit %s does not have a GPG signature."), hex
);
1356 if (verbosity
>= 0 && signature_check
.result
== 'G')
1357 printf(_("Commit %s has a good GPG signature by %s\n"),
1358 hex
, signature_check
.signer
);
1360 signature_check_clear(&signature_check
);
1363 void append_merge_tag_headers(struct commit_list
*parents
,
1364 struct commit_extra_header
***tail
)
1367 struct commit
*parent
= parents
->item
;
1368 handle_signed_tag(parent
, tail
);
1369 parents
= parents
->next
;
1373 static void add_extra_header(struct strbuf
*buffer
,
1374 struct commit_extra_header
*extra
)
1376 strbuf_addstr(buffer
, extra
->key
);
1378 strbuf_add_lines(buffer
, " ", extra
->value
, extra
->len
);
1380 strbuf_addch(buffer
, '\n');
1383 struct commit_extra_header
*read_commit_extra_headers(struct commit
*commit
,
1384 const char **exclude
)
1386 struct commit_extra_header
*extra
= NULL
;
1388 const char *buffer
= repo_get_commit_buffer(the_repository
, commit
,
1390 extra
= read_commit_extra_header_lines(buffer
, size
, exclude
);
1391 repo_unuse_commit_buffer(the_repository
, commit
, buffer
);
1395 int for_each_mergetag(each_mergetag_fn fn
, struct commit
*commit
, void *data
)
1397 struct commit_extra_header
*extra
, *to_free
;
1400 to_free
= read_commit_extra_headers(commit
, NULL
);
1401 for (extra
= to_free
; !res
&& extra
; extra
= extra
->next
) {
1402 if (strcmp(extra
->key
, "mergetag"))
1403 continue; /* not a merge tag */
1404 res
= fn(commit
, extra
, data
);
1406 free_commit_extra_headers(to_free
);
1410 static inline int standard_header_field(const char *field
, size_t len
)
1412 return ((len
== 4 && !memcmp(field
, "tree", 4)) ||
1413 (len
== 6 && !memcmp(field
, "parent", 6)) ||
1414 (len
== 6 && !memcmp(field
, "author", 6)) ||
1415 (len
== 9 && !memcmp(field
, "committer", 9)) ||
1416 (len
== 8 && !memcmp(field
, "encoding", 8)));
1419 static int excluded_header_field(const char *field
, size_t len
, const char **exclude
)
1425 size_t xlen
= strlen(*exclude
);
1426 if (len
== xlen
&& !memcmp(field
, *exclude
, xlen
))
1433 static struct commit_extra_header
*read_commit_extra_header_lines(
1434 const char *buffer
, size_t size
,
1435 const char **exclude
)
1437 struct commit_extra_header
*extra
= NULL
, **tail
= &extra
, *it
= NULL
;
1438 const char *line
, *next
, *eof
, *eob
;
1439 struct strbuf buf
= STRBUF_INIT
;
1441 for (line
= buffer
, eob
= line
+ size
;
1442 line
< eob
&& *line
!= '\n';
1444 next
= memchr(line
, '\n', eob
- line
);
1445 next
= next
? next
+ 1 : eob
;
1449 strbuf_add(&buf
, line
+ 1, next
- (line
+ 1));
1453 it
->value
= strbuf_detach(&buf
, &it
->len
);
1457 eof
= memchr(line
, ' ', next
- line
);
1460 else if (standard_header_field(line
, eof
- line
) ||
1461 excluded_header_field(line
, eof
- line
, exclude
))
1464 CALLOC_ARRAY(it
, 1);
1465 it
->key
= xmemdupz(line
, eof
-line
);
1469 strbuf_add(&buf
, eof
+ 1, next
- (eof
+ 1));
1472 it
->value
= strbuf_detach(&buf
, &it
->len
);
1476 void free_commit_extra_headers(struct commit_extra_header
*extra
)
1479 struct commit_extra_header
*next
= extra
->next
;
1487 int commit_tree(const char *msg
, size_t msg_len
, const struct object_id
*tree
,
1488 struct commit_list
*parents
, struct object_id
*ret
,
1489 const char *author
, const char *sign_commit
)
1491 struct commit_extra_header
*extra
= NULL
, **tail
= &extra
;
1494 append_merge_tag_headers(parents
, &tail
);
1495 result
= commit_tree_extended(msg
, msg_len
, tree
, parents
, ret
, author
,
1496 NULL
, sign_commit
, extra
);
1497 free_commit_extra_headers(extra
);
1501 static int find_invalid_utf8(const char *buf
, int len
)
1504 static const unsigned int max_codepoint
[] = {
1505 0x7f, 0x7ff, 0xffff, 0x10ffff
1509 unsigned char c
= *buf
++;
1510 int bytes
, bad_offset
;
1511 unsigned int codepoint
;
1512 unsigned int min_val
, max_val
;
1517 /* Simple US-ASCII? No worries. */
1521 bad_offset
= offset
-1;
1524 * Count how many more high bits set: that's how
1525 * many more bytes this sequence should have.
1534 * Must be between 1 and 3 more bytes. Longer sequences result in
1535 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1537 if (bytes
< 1 || 3 < bytes
)
1540 /* Do we *have* that many bytes? */
1545 * Place the encoded bits at the bottom of the value and compute the
1548 codepoint
= (c
& 0x7f) >> bytes
;
1549 min_val
= max_codepoint
[bytes
-1] + 1;
1550 max_val
= max_codepoint
[bytes
];
1555 /* And verify that they are good continuation bytes */
1558 codepoint
|= *buf
& 0x3f;
1559 if ((*buf
++ & 0xc0) != 0x80)
1563 /* Reject codepoints that are out of range for the sequence length. */
1564 if (codepoint
< min_val
|| codepoint
> max_val
)
1566 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1567 if ((codepoint
& 0x1ff800) == 0xd800)
1569 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1570 if ((codepoint
& 0xfffe) == 0xfffe)
1572 /* So are anything in the range U+FDD0..U+FDEF. */
1573 if (codepoint
>= 0xfdd0 && codepoint
<= 0xfdef)
1580 * This verifies that the buffer is in proper utf8 format.
1582 * If it isn't, it assumes any non-utf8 characters are Latin1,
1583 * and does the conversion.
1585 static int verify_utf8(struct strbuf
*buf
)
1593 unsigned char replace
[2];
1595 bad
= find_invalid_utf8(buf
->buf
+ pos
, buf
->len
- pos
);
1601 strbuf_remove(buf
, pos
, 1);
1603 /* We know 'c' must be in the range 128-255 */
1604 replace
[0] = 0xc0 + (c
>> 6);
1605 replace
[1] = 0x80 + (c
& 0x3f);
1606 strbuf_insert(buf
, pos
, replace
, 2);
1611 static const char commit_utf8_warn
[] =
1612 N_("Warning: commit message did not conform to UTF-8.\n"
1613 "You may want to amend it after fixing the message, or set the config\n"
1614 "variable i18n.commitEncoding to the encoding your project uses.\n");
1616 int commit_tree_extended(const char *msg
, size_t msg_len
,
1617 const struct object_id
*tree
,
1618 struct commit_list
*parents
, struct object_id
*ret
,
1619 const char *author
, const char *committer
,
1620 const char *sign_commit
,
1621 struct commit_extra_header
*extra
)
1624 int encoding_is_utf8
;
1625 struct strbuf buffer
;
1627 assert_oid_type(tree
, OBJ_TREE
);
1629 if (memchr(msg
, '\0', msg_len
))
1630 return error("a NUL byte in commit log message not allowed.");
1632 /* Not having i18n.commitencoding is the same as having utf-8 */
1633 encoding_is_utf8
= is_encoding_utf8(git_commit_encoding
);
1635 strbuf_init(&buffer
, 8192); /* should avoid reallocs for the headers */
1636 strbuf_addf(&buffer
, "tree %s\n", oid_to_hex(tree
));
1639 * NOTE! This ordering means that the same exact tree merged with a
1640 * different order of parents will be a _different_ changeset even
1641 * if everything else stays the same.
1644 struct commit
*parent
= pop_commit(&parents
);
1645 strbuf_addf(&buffer
, "parent %s\n",
1646 oid_to_hex(&parent
->object
.oid
));
1649 /* Person/date information */
1651 author
= git_author_info(IDENT_STRICT
);
1652 strbuf_addf(&buffer
, "author %s\n", author
);
1654 committer
= git_committer_info(IDENT_STRICT
);
1655 strbuf_addf(&buffer
, "committer %s\n", committer
);
1656 if (!encoding_is_utf8
)
1657 strbuf_addf(&buffer
, "encoding %s\n", git_commit_encoding
);
1660 add_extra_header(&buffer
, extra
);
1661 extra
= extra
->next
;
1663 strbuf_addch(&buffer
, '\n');
1665 /* And add the comment */
1666 strbuf_add(&buffer
, msg
, msg_len
);
1668 /* And check the encoding */
1669 if (encoding_is_utf8
&& !verify_utf8(&buffer
))
1670 fprintf(stderr
, _(commit_utf8_warn
));
1672 if (sign_commit
&& sign_with_header(&buffer
, sign_commit
)) {
1677 result
= write_object_file(buffer
.buf
, buffer
.len
, OBJ_COMMIT
, ret
);
1679 strbuf_release(&buffer
);
1683 define_commit_slab(merge_desc_slab
, struct merge_remote_desc
*);
1684 static struct merge_desc_slab merge_desc_slab
= COMMIT_SLAB_INIT(1, merge_desc_slab
);
1686 struct merge_remote_desc
*merge_remote_util(struct commit
*commit
)
1688 return *merge_desc_slab_at(&merge_desc_slab
, commit
);
1691 void set_merge_remote_desc(struct commit
*commit
,
1692 const char *name
, struct object
*obj
)
1694 struct merge_remote_desc
*desc
;
1695 FLEX_ALLOC_STR(desc
, name
, name
);
1697 *merge_desc_slab_at(&merge_desc_slab
, commit
) = desc
;
1700 struct commit
*get_merge_parent(const char *name
)
1703 struct commit
*commit
;
1704 struct object_id oid
;
1705 if (repo_get_oid(the_repository
, name
, &oid
))
1707 obj
= parse_object(the_repository
, &oid
);
1708 commit
= (struct commit
*)repo_peel_to_type(the_repository
, name
, 0,
1710 if (commit
&& !merge_remote_util(commit
))
1711 set_merge_remote_desc(commit
, name
, obj
);
1716 * Append a commit to the end of the commit_list.
1718 * next starts by pointing to the variable that holds the head of an
1719 * empty commit_list, and is updated to point to the "next" field of
1720 * the last item on the list as new commits are appended.
1724 * struct commit_list *list;
1725 * struct commit_list **next = &list;
1727 * next = commit_list_append(c1, next);
1728 * next = commit_list_append(c2, next);
1729 * assert(commit_list_count(list) == 2);
1732 struct commit_list
**commit_list_append(struct commit
*commit
,
1733 struct commit_list
**next
)
1735 struct commit_list
*new_commit
= xmalloc(sizeof(struct commit_list
));
1736 new_commit
->item
= commit
;
1738 new_commit
->next
= NULL
;
1739 return &new_commit
->next
;
1742 const char *find_header_mem(const char *msg
, size_t len
,
1743 const char *key
, size_t *out_len
)
1745 int key_len
= strlen(key
);
1746 const char *line
= msg
;
1749 * NEEDSWORK: It's possible for strchrnul() to scan beyond the range
1750 * given by len. However, current callers are safe because they compute
1751 * len by scanning a NUL-terminated block of memory starting at msg.
1752 * Nonetheless, it would be better to ensure the function does not look
1753 * at msg beyond the len provided by the caller.
1755 while (line
&& line
< msg
+ len
) {
1756 const char *eol
= strchrnul(line
, '\n');
1761 if (eol
- line
> key_len
&&
1762 !strncmp(line
, key
, key_len
) &&
1763 line
[key_len
] == ' ') {
1764 *out_len
= eol
- line
- key_len
- 1;
1765 return line
+ key_len
+ 1;
1767 line
= *eol
? eol
+ 1 : NULL
;
1772 const char *find_commit_header(const char *msg
, const char *key
, size_t *out_len
)
1774 return find_header_mem(msg
, strlen(msg
), key
, out_len
);
1777 * Inspect the given string and determine the true "end" of the log message, in
1778 * order to find where to put a new Signed-off-by trailer. Ignored are
1779 * trailing comment lines and blank lines. To support "git commit -s
1780 * --amend" on an existing commit, we also ignore "Conflicts:". To
1781 * support "git commit -v", we truncate at cut lines.
1783 * Returns the number of bytes from the tail to ignore, to be fed as
1784 * the second parameter to append_signoff().
1786 size_t ignore_non_trailer(const char *buf
, size_t len
)
1790 int in_old_conflicts_block
= 0;
1791 size_t cutoff
= wt_status_locate_end(buf
, len
);
1793 while (bol
< cutoff
) {
1794 const char *next_line
= memchr(buf
+ bol
, '\n', len
- bol
);
1797 next_line
= buf
+ len
;
1801 if (buf
[bol
] == comment_line_char
|| buf
[bol
] == '\n') {
1802 /* is this the first of the run of comments? */
1805 /* otherwise, it is just continuing */
1806 } else if (starts_with(buf
+ bol
, "Conflicts:\n")) {
1807 in_old_conflicts_block
= 1;
1810 } else if (in_old_conflicts_block
&& buf
[bol
] == '\t') {
1811 ; /* a pathname in the conflicts block */
1813 /* the previous was not trailing comment */
1815 in_old_conflicts_block
= 0;
1817 bol
= next_line
- buf
;
1819 return boc
? len
- boc
: len
- cutoff
;
1822 int run_commit_hook(int editor_is_used
, const char *index_file
,
1823 int *invoked_hook
, const char *name
, ...)
1825 struct run_hooks_opt opt
= RUN_HOOKS_OPT_INIT
;
1829 strvec_pushf(&opt
.env
, "GIT_INDEX_FILE=%s", index_file
);
1832 * Let the hook know that no editor will be launched.
1834 if (!editor_is_used
)
1835 strvec_push(&opt
.env
, "GIT_EDITOR=:");
1837 va_start(args
, name
);
1838 while ((arg
= va_arg(args
, const char *)))
1839 strvec_push(&opt
.args
, arg
);
1842 opt
.invoked_hook
= invoked_hook
;
1843 return run_hooks_opt(name
, &opt
);