4 #include "repository.h"
5 #include "object-store.h"
12 #include "gpg-interface.h"
13 #include "mergesort.h"
14 #include "commit-slab.h"
15 #include "prio-queue.h"
16 #include "sha1-lookup.h"
17 #include "wt-status.h"
19 static struct commit_extra_header
*read_commit_extra_header_lines(const char *buf
, size_t len
, const char **);
21 int save_commit_buffer
= 1;
23 const char *commit_type
= "commit";
25 struct commit
*lookup_commit_reference_gently(const struct object_id
*oid
,
28 struct object
*obj
= deref_tag(parse_object(oid
), NULL
, 0);
32 return object_as_type(obj
, OBJ_COMMIT
, quiet
);
35 struct commit
*lookup_commit_reference(const struct object_id
*oid
)
37 return lookup_commit_reference_gently(oid
, 0);
40 struct commit
*lookup_commit_or_die(const struct object_id
*oid
, const char *ref_name
)
42 struct commit
*c
= lookup_commit_reference(oid
);
44 die(_("could not parse %s"), ref_name
);
45 if (oidcmp(oid
, &c
->object
.oid
)) {
46 warning(_("%s %s is not a commit!"),
47 ref_name
, oid_to_hex(oid
));
52 struct commit
*lookup_commit(const struct object_id
*oid
)
54 struct object
*obj
= lookup_object(oid
->hash
);
56 return create_object(the_repository
, oid
->hash
,
57 alloc_commit_node(the_repository
));
58 return object_as_type(obj
, OBJ_COMMIT
, 0);
61 struct commit
*lookup_commit_reference_by_name(const char *name
)
64 struct commit
*commit
;
66 if (get_oid_committish(name
, &oid
))
68 commit
= lookup_commit_reference(&oid
);
69 if (parse_commit(commit
))
74 static timestamp_t
parse_commit_date(const char *buf
, const char *tail
)
80 if (memcmp(buf
, "author", 6))
82 while (buf
< tail
&& *buf
++ != '\n')
86 if (memcmp(buf
, "committer", 9))
88 while (buf
< tail
&& *buf
++ != '>')
93 while (buf
< tail
&& *buf
++ != '\n')
97 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
98 return parse_timestamp(dateptr
, NULL
, 10);
101 static const unsigned char *commit_graft_sha1_access(size_t index
, void *table
)
103 struct commit_graft
**commit_graft_table
= table
;
104 return commit_graft_table
[index
]->oid
.hash
;
107 #define commit_graft_pos(r, s) commit_graft_pos_##r(s)
108 static int commit_graft_pos_the_repository(const unsigned char *sha1
)
110 return sha1_pos(sha1
, the_repository
->parsed_objects
->grafts
,
111 the_repository
->parsed_objects
->grafts_nr
,
112 commit_graft_sha1_access
);
115 int register_commit_graft_the_repository(struct commit_graft
*graft
, int ignore_dups
)
117 int pos
= commit_graft_pos(the_repository
, graft
->oid
.hash
);
123 free(the_repository
->parsed_objects
->grafts
[pos
]);
124 the_repository
->parsed_objects
->grafts
[pos
] = graft
;
129 ALLOC_GROW(the_repository
->parsed_objects
->grafts
,
130 the_repository
->parsed_objects
->grafts_nr
+ 1,
131 the_repository
->parsed_objects
->grafts_alloc
);
132 the_repository
->parsed_objects
->grafts_nr
++;
133 if (pos
< the_repository
->parsed_objects
->grafts_nr
)
134 memmove(the_repository
->parsed_objects
->grafts
+ pos
+ 1,
135 the_repository
->parsed_objects
->grafts
+ pos
,
136 (the_repository
->parsed_objects
->grafts_nr
- pos
- 1) *
137 sizeof(*the_repository
->parsed_objects
->grafts
));
138 the_repository
->parsed_objects
->grafts
[pos
] = graft
;
142 struct commit_graft
*read_graft_line(struct strbuf
*line
)
144 /* The format is just "Commit Parent1 Parent2 ...\n" */
146 const char *tail
= NULL
;
147 struct commit_graft
*graft
= NULL
;
148 struct object_id dummy_oid
, *oid
;
151 if (!line
->len
|| line
->buf
[0] == '#')
154 * phase 0 verifies line, counts hashes in line and allocates graft
155 * phase 1 fills graft
157 for (phase
= 0; phase
< 2; phase
++) {
158 oid
= graft
? &graft
->oid
: &dummy_oid
;
159 if (parse_oid_hex(line
->buf
, oid
, &tail
))
161 for (i
= 0; *tail
!= '\0'; i
++) {
162 oid
= graft
? &graft
->parent
[i
] : &dummy_oid
;
163 if (!isspace(*tail
++) || parse_oid_hex(tail
, oid
, &tail
))
167 graft
= xmalloc(st_add(sizeof(*graft
),
168 st_mult(sizeof(struct object_id
), i
)));
169 graft
->nr_parent
= i
;
175 error("bad graft data: %s", line
->buf
);
180 #define read_graft_file(r, f) read_graft_file_##r(f)
181 static int read_graft_file_the_repository(const char *graft_file
)
183 FILE *fp
= fopen_or_warn(graft_file
, "r");
184 struct strbuf buf
= STRBUF_INIT
;
187 while (!strbuf_getwholeline(&buf
, fp
, '\n')) {
188 /* The format is just "Commit Parent1 Parent2 ...\n" */
189 struct commit_graft
*graft
= read_graft_line(&buf
);
192 if (register_commit_graft(the_repository
, graft
, 1))
193 error("duplicate graft data: %s", buf
.buf
);
196 strbuf_release(&buf
);
200 #define prepare_commit_graft(r) prepare_commit_graft_##r()
201 static void prepare_commit_graft_the_repository(void)
203 static int commit_graft_prepared
;
206 if (commit_graft_prepared
)
208 graft_file
= get_graft_file();
209 read_graft_file(the_repository
, graft_file
);
210 /* make sure shallows are read */
211 is_repository_shallow();
212 commit_graft_prepared
= 1;
215 struct commit_graft
*lookup_commit_graft_the_repository(const struct object_id
*oid
)
218 prepare_commit_graft(the_repository
);
219 pos
= commit_graft_pos(the_repository
, oid
->hash
);
222 return the_repository
->parsed_objects
->grafts
[pos
];
225 int for_each_commit_graft(each_commit_graft_fn fn
, void *cb_data
)
228 for (i
= ret
= 0; i
< the_repository
->parsed_objects
->grafts_nr
&& !ret
; i
++)
229 ret
= fn(the_repository
->parsed_objects
->grafts
[i
], cb_data
);
233 int unregister_shallow(const struct object_id
*oid
)
235 int pos
= commit_graft_pos(the_repository
, oid
->hash
);
238 if (pos
+ 1 < the_repository
->parsed_objects
->grafts_nr
)
239 MOVE_ARRAY(the_repository
->parsed_objects
->grafts
+ pos
,
240 the_repository
->parsed_objects
->grafts
+ pos
+ 1,
241 the_repository
->parsed_objects
->grafts_nr
- pos
- 1);
242 the_repository
->parsed_objects
->grafts_nr
--;
246 struct commit_buffer
{
250 define_commit_slab(buffer_slab
, struct commit_buffer
);
251 static struct buffer_slab buffer_slab
= COMMIT_SLAB_INIT(1, buffer_slab
);
253 void set_commit_buffer(struct commit
*commit
, void *buffer
, unsigned long size
)
255 struct commit_buffer
*v
= buffer_slab_at(&buffer_slab
, commit
);
260 const void *get_cached_commit_buffer(const struct commit
*commit
, unsigned long *sizep
)
262 struct commit_buffer
*v
= buffer_slab_peek(&buffer_slab
, commit
);
273 const void *get_commit_buffer(const struct commit
*commit
, unsigned long *sizep
)
275 const void *ret
= get_cached_commit_buffer(commit
, sizep
);
277 enum object_type type
;
279 ret
= read_object_file(&commit
->object
.oid
, &type
, &size
);
281 die("cannot read commit object %s",
282 oid_to_hex(&commit
->object
.oid
));
283 if (type
!= OBJ_COMMIT
)
284 die("expected commit for %s, got %s",
285 oid_to_hex(&commit
->object
.oid
), type_name(type
));
292 void unuse_commit_buffer(const struct commit
*commit
, const void *buffer
)
294 struct commit_buffer
*v
= buffer_slab_peek(&buffer_slab
, commit
);
295 if (!(v
&& v
->buffer
== buffer
))
296 free((void *)buffer
);
299 void free_commit_buffer(struct commit
*commit
)
301 struct commit_buffer
*v
= buffer_slab_peek(&buffer_slab
, commit
);
303 FREE_AND_NULL(v
->buffer
);
308 void release_commit_memory(struct commit
*c
)
312 free_commit_buffer(c
);
313 free_commit_list(c
->parents
);
314 /* TODO: what about commit->util? */
316 c
->object
.parsed
= 0;
319 const void *detach_commit_buffer(struct commit
*commit
, unsigned long *sizep
)
321 struct commit_buffer
*v
= buffer_slab_peek(&buffer_slab
, commit
);
338 int parse_commit_buffer(struct commit
*item
, const void *buffer
, unsigned long size
)
340 const char *tail
= buffer
;
341 const char *bufptr
= buffer
;
342 struct object_id parent
;
343 struct commit_list
**pptr
;
344 struct commit_graft
*graft
;
345 const int tree_entry_len
= GIT_SHA1_HEXSZ
+ 5;
346 const int parent_entry_len
= GIT_SHA1_HEXSZ
+ 7;
348 if (item
->object
.parsed
)
350 item
->object
.parsed
= 1;
352 if (tail
<= bufptr
+ tree_entry_len
+ 1 || memcmp(bufptr
, "tree ", 5) ||
353 bufptr
[tree_entry_len
] != '\n')
354 return error("bogus commit object %s", oid_to_hex(&item
->object
.oid
));
355 if (get_sha1_hex(bufptr
+ 5, parent
.hash
) < 0)
356 return error("bad tree pointer in commit %s",
357 oid_to_hex(&item
->object
.oid
));
358 item
->tree
= lookup_tree(&parent
);
359 bufptr
+= tree_entry_len
+ 1; /* "tree " + "hex sha1" + "\n" */
360 pptr
= &item
->parents
;
362 graft
= lookup_commit_graft(the_repository
, &item
->object
.oid
);
363 while (bufptr
+ parent_entry_len
< tail
&& !memcmp(bufptr
, "parent ", 7)) {
364 struct commit
*new_parent
;
366 if (tail
<= bufptr
+ parent_entry_len
+ 1 ||
367 get_sha1_hex(bufptr
+ 7, parent
.hash
) ||
368 bufptr
[parent_entry_len
] != '\n')
369 return error("bad parents in commit %s", oid_to_hex(&item
->object
.oid
));
370 bufptr
+= parent_entry_len
+ 1;
372 * The clone is shallow if nr_parent < 0, and we must
373 * not traverse its real parents even when we unhide them.
375 if (graft
&& (graft
->nr_parent
< 0 || grafts_replace_parents
))
377 new_parent
= lookup_commit(&parent
);
379 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
383 struct commit
*new_parent
;
384 for (i
= 0; i
< graft
->nr_parent
; i
++) {
385 new_parent
= lookup_commit(&graft
->parent
[i
]);
388 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
391 item
->date
= parse_commit_date(bufptr
, tail
);
396 int parse_commit_gently(struct commit
*item
, int quiet_on_missing
)
398 enum object_type type
;
405 if (item
->object
.parsed
)
407 buffer
= read_object_file(&item
->object
.oid
, &type
, &size
);
409 return quiet_on_missing
? -1 :
410 error("Could not read %s",
411 oid_to_hex(&item
->object
.oid
));
412 if (type
!= OBJ_COMMIT
) {
414 return error("Object %s not a commit",
415 oid_to_hex(&item
->object
.oid
));
417 ret
= parse_commit_buffer(item
, buffer
, size
);
418 if (save_commit_buffer
&& !ret
) {
419 set_commit_buffer(item
, buffer
, size
);
426 void parse_commit_or_die(struct commit
*item
)
428 if (parse_commit(item
))
429 die("unable to parse commit %s",
430 item
? oid_to_hex(&item
->object
.oid
) : "(null)");
433 int find_commit_subject(const char *commit_buffer
, const char **subject
)
436 const char *p
= commit_buffer
;
438 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
441 p
= skip_blank_lines(p
+ 2);
442 eol
= strchrnul(p
, '\n');
451 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
453 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
454 new_list
->item
= item
;
455 new_list
->next
= *list_p
;
460 unsigned commit_list_count(const struct commit_list
*l
)
463 for (; l
; l
= l
->next
)
468 struct commit_list
*copy_commit_list(struct commit_list
*list
)
470 struct commit_list
*head
= NULL
;
471 struct commit_list
**pp
= &head
;
473 pp
= commit_list_append(list
->item
, pp
);
479 void free_commit_list(struct commit_list
*list
)
485 struct commit_list
* commit_list_insert_by_date(struct commit
*item
, struct commit_list
**list
)
487 struct commit_list
**pp
= list
;
488 struct commit_list
*p
;
489 while ((p
= *pp
) != NULL
) {
490 if (p
->item
->date
< item
->date
) {
495 return commit_list_insert(item
, pp
);
498 static int commit_list_compare_by_date(const void *a
, const void *b
)
500 timestamp_t a_date
= ((const struct commit_list
*)a
)->item
->date
;
501 timestamp_t b_date
= ((const struct commit_list
*)b
)->item
->date
;
509 static void *commit_list_get_next(const void *a
)
511 return ((const struct commit_list
*)a
)->next
;
514 static void commit_list_set_next(void *a
, void *next
)
516 ((struct commit_list
*)a
)->next
= next
;
519 void commit_list_sort_by_date(struct commit_list
**list
)
521 *list
= llist_mergesort(*list
, commit_list_get_next
, commit_list_set_next
,
522 commit_list_compare_by_date
);
525 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
528 struct commit
*ret
= pop_commit(list
);
529 struct commit_list
*parents
= ret
->parents
;
532 struct commit
*commit
= parents
->item
;
533 if (!parse_commit(commit
) && !(commit
->object
.flags
& mark
)) {
534 commit
->object
.flags
|= mark
;
535 commit_list_insert_by_date(commit
, list
);
537 parents
= parents
->next
;
542 static void clear_commit_marks_1(struct commit_list
**plist
,
543 struct commit
*commit
, unsigned int mark
)
546 struct commit_list
*parents
;
548 if (!(mark
& commit
->object
.flags
))
551 commit
->object
.flags
&= ~mark
;
553 parents
= commit
->parents
;
557 while ((parents
= parents
->next
))
558 commit_list_insert(parents
->item
, plist
);
560 commit
= commit
->parents
->item
;
564 void clear_commit_marks_many(int nr
, struct commit
**commit
, unsigned int mark
)
566 struct commit_list
*list
= NULL
;
569 clear_commit_marks_1(&list
, *commit
, mark
);
573 clear_commit_marks_1(&list
, pop_commit(&list
), mark
);
576 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
578 clear_commit_marks_many(1, &commit
, mark
);
581 struct commit
*pop_commit(struct commit_list
**stack
)
583 struct commit_list
*top
= *stack
;
584 struct commit
*item
= top
? top
->item
: NULL
;
594 * Topological sort support
597 /* count number of children that have not been emitted */
598 define_commit_slab(indegree_slab
, int);
600 /* record author-date for each commit object */
601 define_commit_slab(author_date_slab
, unsigned long);
603 static void record_author_date(struct author_date_slab
*author_date
,
604 struct commit
*commit
)
606 const char *buffer
= get_commit_buffer(commit
, NULL
);
607 struct ident_split ident
;
608 const char *ident_line
;
613 ident_line
= find_commit_header(buffer
, "author", &ident_len
);
615 goto fail_exit
; /* no author line */
616 if (split_ident_line(&ident
, ident_line
, ident_len
) ||
617 !ident
.date_begin
|| !ident
.date_end
)
618 goto fail_exit
; /* malformed "author" line */
620 date
= parse_timestamp(ident
.date_begin
, &date_end
, 10);
621 if (date_end
!= ident
.date_end
)
622 goto fail_exit
; /* malformed date */
623 *(author_date_slab_at(author_date
, commit
)) = date
;
626 unuse_commit_buffer(commit
, buffer
);
629 static int compare_commits_by_author_date(const void *a_
, const void *b_
,
632 const struct commit
*a
= a_
, *b
= b_
;
633 struct author_date_slab
*author_date
= cb_data
;
634 timestamp_t a_date
= *(author_date_slab_at(author_date
, a
));
635 timestamp_t b_date
= *(author_date_slab_at(author_date
, b
));
637 /* newer commits with larger date first */
640 else if (a_date
> b_date
)
645 int compare_commits_by_commit_date(const void *a_
, const void *b_
, void *unused
)
647 const struct commit
*a
= a_
, *b
= b_
;
648 /* newer commits with larger date first */
649 if (a
->date
< b
->date
)
651 else if (a
->date
> b
->date
)
657 * Performs an in-place topological sort on the list supplied.
659 void sort_in_topological_order(struct commit_list
**list
, enum rev_sort_order sort_order
)
661 struct commit_list
*next
, *orig
= *list
;
662 struct commit_list
**pptr
;
663 struct indegree_slab indegree
;
664 struct prio_queue queue
;
665 struct commit
*commit
;
666 struct author_date_slab author_date
;
672 init_indegree_slab(&indegree
);
673 memset(&queue
, '\0', sizeof(queue
));
675 switch (sort_order
) {
676 default: /* REV_SORT_IN_GRAPH_ORDER */
677 queue
.compare
= NULL
;
679 case REV_SORT_BY_COMMIT_DATE
:
680 queue
.compare
= compare_commits_by_commit_date
;
682 case REV_SORT_BY_AUTHOR_DATE
:
683 init_author_date_slab(&author_date
);
684 queue
.compare
= compare_commits_by_author_date
;
685 queue
.cb_data
= &author_date
;
689 /* Mark them and clear the indegree */
690 for (next
= orig
; next
; next
= next
->next
) {
691 struct commit
*commit
= next
->item
;
692 *(indegree_slab_at(&indegree
, commit
)) = 1;
693 /* also record the author dates, if needed */
694 if (sort_order
== REV_SORT_BY_AUTHOR_DATE
)
695 record_author_date(&author_date
, commit
);
698 /* update the indegree */
699 for (next
= orig
; next
; next
= next
->next
) {
700 struct commit_list
*parents
= next
->item
->parents
;
702 struct commit
*parent
= parents
->item
;
703 int *pi
= indegree_slab_at(&indegree
, parent
);
707 parents
= parents
->next
;
714 * tips are nodes not reachable from any other node in the list
716 * the tips serve as a starting set for the work queue.
718 for (next
= orig
; next
; next
= next
->next
) {
719 struct commit
*commit
= next
->item
;
721 if (*(indegree_slab_at(&indegree
, commit
)) == 1)
722 prio_queue_put(&queue
, commit
);
726 * This is unfortunate; the initial tips need to be shown
727 * in the order given from the revision traversal machinery.
729 if (sort_order
== REV_SORT_IN_GRAPH_ORDER
)
730 prio_queue_reverse(&queue
);
732 /* We no longer need the commit list */
733 free_commit_list(orig
);
737 while ((commit
= prio_queue_get(&queue
)) != NULL
) {
738 struct commit_list
*parents
;
740 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
741 struct commit
*parent
= parents
->item
;
742 int *pi
= indegree_slab_at(&indegree
, parent
);
748 * parents are only enqueued for emission
749 * when all their children have been emitted thereby
750 * guaranteeing topological order.
753 prio_queue_put(&queue
, parent
);
756 * all children of commit have already been
757 * emitted. we can emit it now.
759 *(indegree_slab_at(&indegree
, commit
)) = 0;
761 pptr
= &commit_list_insert(commit
, pptr
)->next
;
764 clear_indegree_slab(&indegree
);
765 clear_prio_queue(&queue
);
766 if (sort_order
== REV_SORT_BY_AUTHOR_DATE
)
767 clear_author_date_slab(&author_date
);
770 /* merge-base stuff */
772 /* Remember to update object flag allocation in object.h */
773 #define PARENT1 (1u<<16)
774 #define PARENT2 (1u<<17)
775 #define STALE (1u<<18)
776 #define RESULT (1u<<19)
778 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
780 static int queue_has_nonstale(struct prio_queue
*queue
)
783 for (i
= 0; i
< queue
->nr
; i
++) {
784 struct commit
*commit
= queue
->array
[i
].data
;
785 if (!(commit
->object
.flags
& STALE
))
791 /* all input commits in one and twos[] must have been parsed! */
792 static struct commit_list
*paint_down_to_common(struct commit
*one
, int n
, struct commit
**twos
)
794 struct prio_queue queue
= { compare_commits_by_commit_date
};
795 struct commit_list
*result
= NULL
;
798 one
->object
.flags
|= PARENT1
;
800 commit_list_append(one
, &result
);
803 prio_queue_put(&queue
, one
);
805 for (i
= 0; i
< n
; i
++) {
806 twos
[i
]->object
.flags
|= PARENT2
;
807 prio_queue_put(&queue
, twos
[i
]);
810 while (queue_has_nonstale(&queue
)) {
811 struct commit
*commit
= prio_queue_get(&queue
);
812 struct commit_list
*parents
;
815 flags
= commit
->object
.flags
& (PARENT1
| PARENT2
| STALE
);
816 if (flags
== (PARENT1
| PARENT2
)) {
817 if (!(commit
->object
.flags
& RESULT
)) {
818 commit
->object
.flags
|= RESULT
;
819 commit_list_insert_by_date(commit
, &result
);
821 /* Mark parents of a found merge stale */
824 parents
= commit
->parents
;
826 struct commit
*p
= parents
->item
;
827 parents
= parents
->next
;
828 if ((p
->object
.flags
& flags
) == flags
)
832 p
->object
.flags
|= flags
;
833 prio_queue_put(&queue
, p
);
837 clear_prio_queue(&queue
);
841 static struct commit_list
*merge_bases_many(struct commit
*one
, int n
, struct commit
**twos
)
843 struct commit_list
*list
= NULL
;
844 struct commit_list
*result
= NULL
;
847 for (i
= 0; i
< n
; i
++) {
850 * We do not mark this even with RESULT so we do not
851 * have to clean it up.
853 return commit_list_insert(one
, &result
);
856 if (parse_commit(one
))
858 for (i
= 0; i
< n
; i
++) {
859 if (parse_commit(twos
[i
]))
863 list
= paint_down_to_common(one
, n
, twos
);
866 struct commit
*commit
= pop_commit(&list
);
867 if (!(commit
->object
.flags
& STALE
))
868 commit_list_insert_by_date(commit
, &result
);
873 struct commit_list
*get_octopus_merge_bases(struct commit_list
*in
)
875 struct commit_list
*i
, *j
, *k
, *ret
= NULL
;
880 commit_list_insert(in
->item
, &ret
);
882 for (i
= in
->next
; i
; i
= i
->next
) {
883 struct commit_list
*new_commits
= NULL
, *end
= NULL
;
885 for (j
= ret
; j
; j
= j
->next
) {
886 struct commit_list
*bases
;
887 bases
= get_merge_bases(i
->item
, j
->item
);
892 for (k
= bases
; k
; k
= k
->next
)
900 static int remove_redundant(struct commit
**array
, int cnt
)
903 * Some commit in the array may be an ancestor of
904 * another commit. Move such commit to the end of
905 * the array, and return the number of commits that
906 * are independent from each other.
908 struct commit
**work
;
909 unsigned char *redundant
;
913 work
= xcalloc(cnt
, sizeof(*work
));
914 redundant
= xcalloc(cnt
, 1);
915 ALLOC_ARRAY(filled_index
, cnt
- 1);
917 for (i
= 0; i
< cnt
; i
++)
918 parse_commit(array
[i
]);
919 for (i
= 0; i
< cnt
; i
++) {
920 struct commit_list
*common
;
924 for (j
= filled
= 0; j
< cnt
; j
++) {
925 if (i
== j
|| redundant
[j
])
927 filled_index
[filled
] = j
;
928 work
[filled
++] = array
[j
];
930 common
= paint_down_to_common(array
[i
], filled
, work
);
931 if (array
[i
]->object
.flags
& PARENT2
)
933 for (j
= 0; j
< filled
; j
++)
934 if (work
[j
]->object
.flags
& PARENT1
)
935 redundant
[filled_index
[j
]] = 1;
936 clear_commit_marks(array
[i
], all_flags
);
937 clear_commit_marks_many(filled
, work
, all_flags
);
938 free_commit_list(common
);
941 /* Now collect the result */
942 COPY_ARRAY(work
, array
, cnt
);
943 for (i
= filled
= 0; i
< cnt
; i
++)
945 array
[filled
++] = work
[i
];
946 for (j
= filled
, i
= 0; i
< cnt
; i
++)
948 array
[j
++] = work
[i
];
955 static struct commit_list
*get_merge_bases_many_0(struct commit
*one
,
957 struct commit
**twos
,
960 struct commit_list
*list
;
961 struct commit
**rslt
;
962 struct commit_list
*result
;
965 result
= merge_bases_many(one
, n
, twos
);
966 for (i
= 0; i
< n
; i
++) {
970 if (!result
|| !result
->next
) {
972 clear_commit_marks(one
, all_flags
);
973 clear_commit_marks_many(n
, twos
, all_flags
);
978 /* There are more than one */
979 cnt
= commit_list_count(result
);
980 rslt
= xcalloc(cnt
, sizeof(*rslt
));
981 for (list
= result
, i
= 0; list
; list
= list
->next
)
982 rslt
[i
++] = list
->item
;
983 free_commit_list(result
);
985 clear_commit_marks(one
, all_flags
);
986 clear_commit_marks_many(n
, twos
, all_flags
);
988 cnt
= remove_redundant(rslt
, cnt
);
990 for (i
= 0; i
< cnt
; i
++)
991 commit_list_insert_by_date(rslt
[i
], &result
);
996 struct commit_list
*get_merge_bases_many(struct commit
*one
,
998 struct commit
**twos
)
1000 return get_merge_bases_many_0(one
, n
, twos
, 1);
1003 struct commit_list
*get_merge_bases_many_dirty(struct commit
*one
,
1005 struct commit
**twos
)
1007 return get_merge_bases_many_0(one
, n
, twos
, 0);
1010 struct commit_list
*get_merge_bases(struct commit
*one
, struct commit
*two
)
1012 return get_merge_bases_many_0(one
, 1, &two
, 1);
1016 * Is "commit" a descendant of one of the elements on the "with_commit" list?
1018 int is_descendant_of(struct commit
*commit
, struct commit_list
*with_commit
)
1022 while (with_commit
) {
1023 struct commit
*other
;
1025 other
= with_commit
->item
;
1026 with_commit
= with_commit
->next
;
1027 if (in_merge_bases(other
, commit
))
1034 * Is "commit" an ancestor of one of the "references"?
1036 int in_merge_bases_many(struct commit
*commit
, int nr_reference
, struct commit
**reference
)
1038 struct commit_list
*bases
;
1041 if (parse_commit(commit
))
1043 for (i
= 0; i
< nr_reference
; i
++)
1044 if (parse_commit(reference
[i
]))
1047 bases
= paint_down_to_common(commit
, nr_reference
, reference
);
1048 if (commit
->object
.flags
& PARENT2
)
1050 clear_commit_marks(commit
, all_flags
);
1051 clear_commit_marks_many(nr_reference
, reference
, all_flags
);
1052 free_commit_list(bases
);
1057 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
1059 int in_merge_bases(struct commit
*commit
, struct commit
*reference
)
1061 return in_merge_bases_many(commit
, 1, &reference
);
1064 struct commit_list
*reduce_heads(struct commit_list
*heads
)
1066 struct commit_list
*p
;
1067 struct commit_list
*result
= NULL
, **tail
= &result
;
1068 struct commit
**array
;
1075 for (p
= heads
; p
; p
= p
->next
)
1076 p
->item
->object
.flags
&= ~STALE
;
1077 for (p
= heads
, num_head
= 0; p
; p
= p
->next
) {
1078 if (p
->item
->object
.flags
& STALE
)
1080 p
->item
->object
.flags
|= STALE
;
1083 array
= xcalloc(num_head
, sizeof(*array
));
1084 for (p
= heads
, i
= 0; p
; p
= p
->next
) {
1085 if (p
->item
->object
.flags
& STALE
) {
1086 array
[i
++] = p
->item
;
1087 p
->item
->object
.flags
&= ~STALE
;
1090 num_head
= remove_redundant(array
, num_head
);
1091 for (i
= 0; i
< num_head
; i
++)
1092 tail
= &commit_list_insert(array
[i
], tail
)->next
;
1097 void reduce_heads_replace(struct commit_list
**heads
)
1099 struct commit_list
*result
= reduce_heads(*heads
);
1100 free_commit_list(*heads
);
1104 static const char gpg_sig_header
[] = "gpgsig";
1105 static const int gpg_sig_header_len
= sizeof(gpg_sig_header
) - 1;
1107 static int do_sign_commit(struct strbuf
*buf
, const char *keyid
)
1109 struct strbuf sig
= STRBUF_INIT
;
1110 int inspos
, copypos
;
1113 /* find the end of the header */
1114 eoh
= strstr(buf
->buf
, "\n\n");
1118 inspos
= eoh
- buf
->buf
+ 1;
1120 if (!keyid
|| !*keyid
)
1121 keyid
= get_signing_key();
1122 if (sign_buffer(buf
, &sig
, keyid
)) {
1123 strbuf_release(&sig
);
1127 for (copypos
= 0; sig
.buf
[copypos
]; ) {
1128 const char *bol
= sig
.buf
+ copypos
;
1129 const char *eol
= strchrnul(bol
, '\n');
1130 int len
= (eol
- bol
) + !!*eol
;
1133 strbuf_insert(buf
, inspos
, gpg_sig_header
, gpg_sig_header_len
);
1134 inspos
+= gpg_sig_header_len
;
1136 strbuf_insert(buf
, inspos
++, " ", 1);
1137 strbuf_insert(buf
, inspos
, bol
, len
);
1141 strbuf_release(&sig
);
1145 int parse_signed_commit(const struct commit
*commit
,
1146 struct strbuf
*payload
, struct strbuf
*signature
)
1150 const char *buffer
= get_commit_buffer(commit
, &size
);
1151 int in_signature
, saw_signature
= -1;
1152 const char *line
, *tail
;
1155 tail
= buffer
+ size
;
1158 while (line
< tail
) {
1159 const char *sig
= NULL
;
1160 const char *next
= memchr(line
, '\n', tail
- line
);
1162 next
= next
? next
+ 1 : tail
;
1163 if (in_signature
&& line
[0] == ' ')
1165 else if (starts_with(line
, gpg_sig_header
) &&
1166 line
[gpg_sig_header_len
] == ' ')
1167 sig
= line
+ gpg_sig_header_len
+ 1;
1169 strbuf_add(signature
, sig
, next
- sig
);
1174 /* dump the whole remainder of the buffer */
1176 strbuf_add(payload
, line
, next
- line
);
1181 unuse_commit_buffer(commit
, buffer
);
1182 return saw_signature
;
1185 int remove_signature(struct strbuf
*buf
)
1187 const char *line
= buf
->buf
;
1188 const char *tail
= buf
->buf
+ buf
->len
;
1189 int in_signature
= 0;
1190 const char *sig_start
= NULL
;
1191 const char *sig_end
= NULL
;
1193 while (line
< tail
) {
1194 const char *next
= memchr(line
, '\n', tail
- line
);
1195 next
= next
? next
+ 1 : tail
;
1197 if (in_signature
&& line
[0] == ' ')
1199 else if (starts_with(line
, gpg_sig_header
) &&
1200 line
[gpg_sig_header_len
] == ' ') {
1206 /* dump the whole remainder of the buffer */
1214 strbuf_remove(buf
, sig_start
- buf
->buf
, sig_end
- sig_start
);
1216 return sig_start
!= NULL
;
1219 static void handle_signed_tag(struct commit
*parent
, struct commit_extra_header
***tail
)
1221 struct merge_remote_desc
*desc
;
1222 struct commit_extra_header
*mergetag
;
1224 unsigned long size
, len
;
1225 enum object_type type
;
1227 desc
= merge_remote_util(parent
);
1228 if (!desc
|| !desc
->obj
)
1230 buf
= read_object_file(&desc
->obj
->oid
, &type
, &size
);
1231 if (!buf
|| type
!= OBJ_TAG
)
1233 len
= parse_signature(buf
, size
);
1237 * We could verify this signature and either omit the tag when
1238 * it does not validate, but the integrator may not have the
1239 * public key of the signer of the tag he is merging, while a
1240 * later auditor may have it while auditing, so let's not run
1241 * verify-signed-buffer here for now...
1243 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1244 * warn("warning: signed tag unverified.");
1246 mergetag
= xcalloc(1, sizeof(*mergetag
));
1247 mergetag
->key
= xstrdup("mergetag");
1248 mergetag
->value
= buf
;
1249 mergetag
->len
= size
;
1252 *tail
= &mergetag
->next
;
1259 int check_commit_signature(const struct commit
*commit
, struct signature_check
*sigc
)
1261 struct strbuf payload
= STRBUF_INIT
;
1262 struct strbuf signature
= STRBUF_INIT
;
1267 if (parse_signed_commit(commit
, &payload
, &signature
) <= 0)
1269 ret
= check_signature(payload
.buf
, payload
.len
, signature
.buf
,
1270 signature
.len
, sigc
);
1273 strbuf_release(&payload
);
1274 strbuf_release(&signature
);
1281 void append_merge_tag_headers(struct commit_list
*parents
,
1282 struct commit_extra_header
***tail
)
1285 struct commit
*parent
= parents
->item
;
1286 handle_signed_tag(parent
, tail
);
1287 parents
= parents
->next
;
1291 static void add_extra_header(struct strbuf
*buffer
,
1292 struct commit_extra_header
*extra
)
1294 strbuf_addstr(buffer
, extra
->key
);
1296 strbuf_add_lines(buffer
, " ", extra
->value
, extra
->len
);
1298 strbuf_addch(buffer
, '\n');
1301 struct commit_extra_header
*read_commit_extra_headers(struct commit
*commit
,
1302 const char **exclude
)
1304 struct commit_extra_header
*extra
= NULL
;
1306 const char *buffer
= get_commit_buffer(commit
, &size
);
1307 extra
= read_commit_extra_header_lines(buffer
, size
, exclude
);
1308 unuse_commit_buffer(commit
, buffer
);
1312 void for_each_mergetag(each_mergetag_fn fn
, struct commit
*commit
, void *data
)
1314 struct commit_extra_header
*extra
, *to_free
;
1316 to_free
= read_commit_extra_headers(commit
, NULL
);
1317 for (extra
= to_free
; extra
; extra
= extra
->next
) {
1318 if (strcmp(extra
->key
, "mergetag"))
1319 continue; /* not a merge tag */
1320 fn(commit
, extra
, data
);
1322 free_commit_extra_headers(to_free
);
1325 static inline int standard_header_field(const char *field
, size_t len
)
1327 return ((len
== 4 && !memcmp(field
, "tree", 4)) ||
1328 (len
== 6 && !memcmp(field
, "parent", 6)) ||
1329 (len
== 6 && !memcmp(field
, "author", 6)) ||
1330 (len
== 9 && !memcmp(field
, "committer", 9)) ||
1331 (len
== 8 && !memcmp(field
, "encoding", 8)));
1334 static int excluded_header_field(const char *field
, size_t len
, const char **exclude
)
1340 size_t xlen
= strlen(*exclude
);
1341 if (len
== xlen
&& !memcmp(field
, *exclude
, xlen
))
1348 static struct commit_extra_header
*read_commit_extra_header_lines(
1349 const char *buffer
, size_t size
,
1350 const char **exclude
)
1352 struct commit_extra_header
*extra
= NULL
, **tail
= &extra
, *it
= NULL
;
1353 const char *line
, *next
, *eof
, *eob
;
1354 struct strbuf buf
= STRBUF_INIT
;
1356 for (line
= buffer
, eob
= line
+ size
;
1357 line
< eob
&& *line
!= '\n';
1359 next
= memchr(line
, '\n', eob
- line
);
1360 next
= next
? next
+ 1 : eob
;
1364 strbuf_add(&buf
, line
+ 1, next
- (line
+ 1));
1368 it
->value
= strbuf_detach(&buf
, &it
->len
);
1372 eof
= memchr(line
, ' ', next
- line
);
1375 else if (standard_header_field(line
, eof
- line
) ||
1376 excluded_header_field(line
, eof
- line
, exclude
))
1379 it
= xcalloc(1, sizeof(*it
));
1380 it
->key
= xmemdupz(line
, eof
-line
);
1384 strbuf_add(&buf
, eof
+ 1, next
- (eof
+ 1));
1387 it
->value
= strbuf_detach(&buf
, &it
->len
);
1391 void free_commit_extra_headers(struct commit_extra_header
*extra
)
1394 struct commit_extra_header
*next
= extra
->next
;
1402 int commit_tree(const char *msg
, size_t msg_len
, const struct object_id
*tree
,
1403 struct commit_list
*parents
, struct object_id
*ret
,
1404 const char *author
, const char *sign_commit
)
1406 struct commit_extra_header
*extra
= NULL
, **tail
= &extra
;
1409 append_merge_tag_headers(parents
, &tail
);
1410 result
= commit_tree_extended(msg
, msg_len
, tree
, parents
, ret
,
1411 author
, sign_commit
, extra
);
1412 free_commit_extra_headers(extra
);
1416 static int find_invalid_utf8(const char *buf
, int len
)
1419 static const unsigned int max_codepoint
[] = {
1420 0x7f, 0x7ff, 0xffff, 0x10ffff
1424 unsigned char c
= *buf
++;
1425 int bytes
, bad_offset
;
1426 unsigned int codepoint
;
1427 unsigned int min_val
, max_val
;
1432 /* Simple US-ASCII? No worries. */
1436 bad_offset
= offset
-1;
1439 * Count how many more high bits set: that's how
1440 * many more bytes this sequence should have.
1449 * Must be between 1 and 3 more bytes. Longer sequences result in
1450 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1452 if (bytes
< 1 || 3 < bytes
)
1455 /* Do we *have* that many bytes? */
1460 * Place the encoded bits at the bottom of the value and compute the
1463 codepoint
= (c
& 0x7f) >> bytes
;
1464 min_val
= max_codepoint
[bytes
-1] + 1;
1465 max_val
= max_codepoint
[bytes
];
1470 /* And verify that they are good continuation bytes */
1473 codepoint
|= *buf
& 0x3f;
1474 if ((*buf
++ & 0xc0) != 0x80)
1478 /* Reject codepoints that are out of range for the sequence length. */
1479 if (codepoint
< min_val
|| codepoint
> max_val
)
1481 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1482 if ((codepoint
& 0x1ff800) == 0xd800)
1484 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1485 if ((codepoint
& 0xfffe) == 0xfffe)
1487 /* So are anything in the range U+FDD0..U+FDEF. */
1488 if (codepoint
>= 0xfdd0 && codepoint
<= 0xfdef)
1495 * This verifies that the buffer is in proper utf8 format.
1497 * If it isn't, it assumes any non-utf8 characters are Latin1,
1498 * and does the conversion.
1500 static int verify_utf8(struct strbuf
*buf
)
1508 unsigned char replace
[2];
1510 bad
= find_invalid_utf8(buf
->buf
+ pos
, buf
->len
- pos
);
1516 strbuf_remove(buf
, pos
, 1);
1518 /* We know 'c' must be in the range 128-255 */
1519 replace
[0] = 0xc0 + (c
>> 6);
1520 replace
[1] = 0x80 + (c
& 0x3f);
1521 strbuf_insert(buf
, pos
, replace
, 2);
1526 static const char commit_utf8_warn
[] =
1527 N_("Warning: commit message did not conform to UTF-8.\n"
1528 "You may want to amend it after fixing the message, or set the config\n"
1529 "variable i18n.commitencoding to the encoding your project uses.\n");
1531 int commit_tree_extended(const char *msg
, size_t msg_len
,
1532 const struct object_id
*tree
,
1533 struct commit_list
*parents
, struct object_id
*ret
,
1534 const char *author
, const char *sign_commit
,
1535 struct commit_extra_header
*extra
)
1538 int encoding_is_utf8
;
1539 struct strbuf buffer
;
1541 assert_oid_type(tree
, OBJ_TREE
);
1543 if (memchr(msg
, '\0', msg_len
))
1544 return error("a NUL byte in commit log message not allowed.");
1546 /* Not having i18n.commitencoding is the same as having utf-8 */
1547 encoding_is_utf8
= is_encoding_utf8(git_commit_encoding
);
1549 strbuf_init(&buffer
, 8192); /* should avoid reallocs for the headers */
1550 strbuf_addf(&buffer
, "tree %s\n", oid_to_hex(tree
));
1553 * NOTE! This ordering means that the same exact tree merged with a
1554 * different order of parents will be a _different_ changeset even
1555 * if everything else stays the same.
1558 struct commit
*parent
= pop_commit(&parents
);
1559 strbuf_addf(&buffer
, "parent %s\n",
1560 oid_to_hex(&parent
->object
.oid
));
1563 /* Person/date information */
1565 author
= git_author_info(IDENT_STRICT
);
1566 strbuf_addf(&buffer
, "author %s\n", author
);
1567 strbuf_addf(&buffer
, "committer %s\n", git_committer_info(IDENT_STRICT
));
1568 if (!encoding_is_utf8
)
1569 strbuf_addf(&buffer
, "encoding %s\n", git_commit_encoding
);
1572 add_extra_header(&buffer
, extra
);
1573 extra
= extra
->next
;
1575 strbuf_addch(&buffer
, '\n');
1577 /* And add the comment */
1578 strbuf_add(&buffer
, msg
, msg_len
);
1580 /* And check the encoding */
1581 if (encoding_is_utf8
&& !verify_utf8(&buffer
))
1582 fprintf(stderr
, _(commit_utf8_warn
));
1584 if (sign_commit
&& do_sign_commit(&buffer
, sign_commit
)) {
1589 result
= write_object_file(buffer
.buf
, buffer
.len
, commit_type
, ret
);
1591 strbuf_release(&buffer
);
1595 void set_merge_remote_desc(struct commit
*commit
,
1596 const char *name
, struct object
*obj
)
1598 struct merge_remote_desc
*desc
;
1599 FLEX_ALLOC_STR(desc
, name
, name
);
1601 commit
->util
= desc
;
1604 struct commit
*get_merge_parent(const char *name
)
1607 struct commit
*commit
;
1608 struct object_id oid
;
1609 if (get_oid(name
, &oid
))
1611 obj
= parse_object(&oid
);
1612 commit
= (struct commit
*)peel_to_type(name
, 0, obj
, OBJ_COMMIT
);
1613 if (commit
&& !commit
->util
)
1614 set_merge_remote_desc(commit
, name
, obj
);
1619 * Append a commit to the end of the commit_list.
1621 * next starts by pointing to the variable that holds the head of an
1622 * empty commit_list, and is updated to point to the "next" field of
1623 * the last item on the list as new commits are appended.
1627 * struct commit_list *list;
1628 * struct commit_list **next = &list;
1630 * next = commit_list_append(c1, next);
1631 * next = commit_list_append(c2, next);
1632 * assert(commit_list_count(list) == 2);
1635 struct commit_list
**commit_list_append(struct commit
*commit
,
1636 struct commit_list
**next
)
1638 struct commit_list
*new_commit
= xmalloc(sizeof(struct commit_list
));
1639 new_commit
->item
= commit
;
1641 new_commit
->next
= NULL
;
1642 return &new_commit
->next
;
1645 const char *find_commit_header(const char *msg
, const char *key
, size_t *out_len
)
1647 int key_len
= strlen(key
);
1648 const char *line
= msg
;
1651 const char *eol
= strchrnul(line
, '\n');
1656 if (eol
- line
> key_len
&&
1657 !strncmp(line
, key
, key_len
) &&
1658 line
[key_len
] == ' ') {
1659 *out_len
= eol
- line
- key_len
- 1;
1660 return line
+ key_len
+ 1;
1662 line
= *eol
? eol
+ 1 : NULL
;
1668 * Inspect the given string and determine the true "end" of the log message, in
1669 * order to find where to put a new Signed-off-by: line. Ignored are
1670 * trailing comment lines and blank lines. To support "git commit -s
1671 * --amend" on an existing commit, we also ignore "Conflicts:". To
1672 * support "git commit -v", we truncate at cut lines.
1674 * Returns the number of bytes from the tail to ignore, to be fed as
1675 * the second parameter to append_signoff().
1677 int ignore_non_trailer(const char *buf
, size_t len
)
1681 int in_old_conflicts_block
= 0;
1682 size_t cutoff
= wt_status_locate_end(buf
, len
);
1684 while (bol
< cutoff
) {
1685 const char *next_line
= memchr(buf
+ bol
, '\n', len
- bol
);
1688 next_line
= buf
+ len
;
1692 if (buf
[bol
] == comment_line_char
|| buf
[bol
] == '\n') {
1693 /* is this the first of the run of comments? */
1696 /* otherwise, it is just continuing */
1697 } else if (starts_with(buf
+ bol
, "Conflicts:\n")) {
1698 in_old_conflicts_block
= 1;
1701 } else if (in_old_conflicts_block
&& buf
[bol
] == '\t') {
1702 ; /* a pathname in the conflicts block */
1704 /* the previous was not trailing comment */
1706 in_old_conflicts_block
= 0;
1708 bol
= next_line
- buf
;
1710 return boc
? len
- boc
: len
- cutoff
;