9 #include "gpg-interface.h"
10 #include "mergesort.h"
11 #include "commit-slab.h"
12 #include "prio-queue.h"
14 static struct commit_extra_header
*read_commit_extra_header_lines(const char *buf
, size_t len
, const char **);
16 int save_commit_buffer
= 1;
18 const char *commit_type
= "commit";
19 static int commit_count
;
21 static struct commit
*check_commit(struct object
*obj
,
22 const unsigned char *sha1
,
25 if (obj
->type
!= OBJ_COMMIT
) {
27 error("Object %s is a %s, not a commit",
28 sha1_to_hex(sha1
), typename(obj
->type
));
31 return (struct commit
*) obj
;
34 struct commit
*lookup_commit_reference_gently(const unsigned char *sha1
,
37 struct object
*obj
= deref_tag(parse_object(sha1
), NULL
, 0);
41 return check_commit(obj
, sha1
, quiet
);
44 struct commit
*lookup_commit_reference(const unsigned char *sha1
)
46 return lookup_commit_reference_gently(sha1
, 0);
49 struct commit
*lookup_commit_or_die(const unsigned char *sha1
, const char *ref_name
)
51 struct commit
*c
= lookup_commit_reference(sha1
);
53 die(_("could not parse %s"), ref_name
);
54 if (hashcmp(sha1
, c
->object
.sha1
)) {
55 warning(_("%s %s is not a commit!"),
56 ref_name
, sha1_to_hex(sha1
));
61 struct commit
*lookup_commit(const unsigned char *sha1
)
63 struct object
*obj
= lookup_object(sha1
);
65 struct commit
*c
= alloc_commit_node();
66 c
->index
= commit_count
++;
67 return create_object(sha1
, OBJ_COMMIT
, c
);
70 obj
->type
= OBJ_COMMIT
;
71 return check_commit(obj
, sha1
, 0);
74 struct commit
*lookup_commit_reference_by_name(const char *name
)
76 unsigned char sha1
[20];
77 struct commit
*commit
;
79 if (get_sha1_committish(name
, sha1
))
81 commit
= lookup_commit_reference(sha1
);
82 if (!commit
|| parse_commit(commit
))
87 static unsigned long parse_commit_date(const char *buf
, const char *tail
)
93 if (memcmp(buf
, "author", 6))
95 while (buf
< tail
&& *buf
++ != '\n')
99 if (memcmp(buf
, "committer", 9))
101 while (buf
< tail
&& *buf
++ != '>')
106 while (buf
< tail
&& *buf
++ != '\n')
110 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
111 return strtoul(dateptr
, NULL
, 10);
114 static struct commit_graft
**commit_graft
;
115 static int commit_graft_alloc
, commit_graft_nr
;
117 static int commit_graft_pos(const unsigned char *sha1
)
121 hi
= commit_graft_nr
;
123 int mi
= (lo
+ hi
) / 2;
124 struct commit_graft
*graft
= commit_graft
[mi
];
125 int cmp
= hashcmp(sha1
, graft
->sha1
);
136 int register_commit_graft(struct commit_graft
*graft
, int ignore_dups
)
138 int pos
= commit_graft_pos(graft
->sha1
);
144 free(commit_graft
[pos
]);
145 commit_graft
[pos
] = graft
;
150 if (commit_graft_alloc
<= ++commit_graft_nr
) {
151 commit_graft_alloc
= alloc_nr(commit_graft_alloc
);
152 commit_graft
= xrealloc(commit_graft
,
153 sizeof(*commit_graft
) *
156 if (pos
< commit_graft_nr
)
157 memmove(commit_graft
+ pos
+ 1,
159 (commit_graft_nr
- pos
- 1) *
160 sizeof(*commit_graft
));
161 commit_graft
[pos
] = graft
;
165 struct commit_graft
*read_graft_line(char *buf
, int len
)
167 /* The format is just "Commit Parent1 Parent2 ...\n" */
169 struct commit_graft
*graft
= NULL
;
171 while (len
&& isspace(buf
[len
-1]))
173 if (buf
[0] == '#' || buf
[0] == '\0')
177 i
= (len
+ 1) / 41 - 1;
178 graft
= xmalloc(sizeof(*graft
) + 20 * i
);
179 graft
->nr_parent
= i
;
180 if (get_sha1_hex(buf
, graft
->sha1
))
182 for (i
= 40; i
< len
; i
+= 41) {
185 if (get_sha1_hex(buf
+ i
+ 1, graft
->parent
[i
/41]))
191 error("bad graft data: %s", buf
);
196 static int read_graft_file(const char *graft_file
)
198 FILE *fp
= fopen(graft_file
, "r");
202 while (fgets(buf
, sizeof(buf
), fp
)) {
203 /* The format is just "Commit Parent1 Parent2 ...\n" */
204 int len
= strlen(buf
);
205 struct commit_graft
*graft
= read_graft_line(buf
, len
);
208 if (register_commit_graft(graft
, 1))
209 error("duplicate graft data: %s", buf
);
215 static void prepare_commit_graft(void)
217 static int commit_graft_prepared
;
220 if (commit_graft_prepared
)
222 graft_file
= get_graft_file();
223 read_graft_file(graft_file
);
224 /* make sure shallows are read */
225 is_repository_shallow();
226 commit_graft_prepared
= 1;
229 struct commit_graft
*lookup_commit_graft(const unsigned char *sha1
)
232 prepare_commit_graft();
233 pos
= commit_graft_pos(sha1
);
236 return commit_graft
[pos
];
239 int for_each_commit_graft(each_commit_graft_fn fn
, void *cb_data
)
242 for (i
= ret
= 0; i
< commit_graft_nr
&& !ret
; i
++)
243 ret
= fn(commit_graft
[i
], cb_data
);
247 int unregister_shallow(const unsigned char *sha1
)
249 int pos
= commit_graft_pos(sha1
);
252 if (pos
+ 1 < commit_graft_nr
)
253 memmove(commit_graft
+ pos
, commit_graft
+ pos
+ 1,
254 sizeof(struct commit_graft
*)
255 * (commit_graft_nr
- pos
- 1));
260 int parse_commit_buffer(struct commit
*item
, const void *buffer
, unsigned long size
)
262 const char *tail
= buffer
;
263 const char *bufptr
= buffer
;
264 unsigned char parent
[20];
265 struct commit_list
**pptr
;
266 struct commit_graft
*graft
;
268 if (item
->object
.parsed
)
270 item
->object
.parsed
= 1;
272 if (tail
<= bufptr
+ 46 || memcmp(bufptr
, "tree ", 5) || bufptr
[45] != '\n')
273 return error("bogus commit object %s", sha1_to_hex(item
->object
.sha1
));
274 if (get_sha1_hex(bufptr
+ 5, parent
) < 0)
275 return error("bad tree pointer in commit %s",
276 sha1_to_hex(item
->object
.sha1
));
277 item
->tree
= lookup_tree(parent
);
278 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
279 pptr
= &item
->parents
;
281 graft
= lookup_commit_graft(item
->object
.sha1
);
282 while (bufptr
+ 48 < tail
&& !memcmp(bufptr
, "parent ", 7)) {
283 struct commit
*new_parent
;
285 if (tail
<= bufptr
+ 48 ||
286 get_sha1_hex(bufptr
+ 7, parent
) ||
288 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
291 * The clone is shallow if nr_parent < 0, and we must
292 * not traverse its real parents even when we unhide them.
294 if (graft
&& (graft
->nr_parent
< 0 || grafts_replace_parents
))
296 new_parent
= lookup_commit(parent
);
298 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
302 struct commit
*new_parent
;
303 for (i
= 0; i
< graft
->nr_parent
; i
++) {
304 new_parent
= lookup_commit(graft
->parent
[i
]);
307 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
310 item
->date
= parse_commit_date(bufptr
, tail
);
315 int parse_commit(struct commit
*item
)
317 enum object_type type
;
324 if (item
->object
.parsed
)
326 buffer
= read_sha1_file(item
->object
.sha1
, &type
, &size
);
328 return error("Could not read %s",
329 sha1_to_hex(item
->object
.sha1
));
330 if (type
!= OBJ_COMMIT
) {
332 return error("Object %s not a commit",
333 sha1_to_hex(item
->object
.sha1
));
335 ret
= parse_commit_buffer(item
, buffer
, size
);
336 if (save_commit_buffer
&& !ret
) {
337 item
->buffer
= buffer
;
344 int find_commit_subject(const char *commit_buffer
, const char **subject
)
347 const char *p
= commit_buffer
;
349 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
353 for (eol
= p
; *eol
&& *eol
!= '\n'; eol
++)
363 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
365 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
366 new_list
->item
= item
;
367 new_list
->next
= *list_p
;
372 unsigned commit_list_count(const struct commit_list
*l
)
375 for (; l
; l
= l
->next
)
380 void free_commit_list(struct commit_list
*list
)
383 struct commit_list
*temp
= list
;
389 struct commit_list
* commit_list_insert_by_date(struct commit
*item
, struct commit_list
**list
)
391 struct commit_list
**pp
= list
;
392 struct commit_list
*p
;
393 while ((p
= *pp
) != NULL
) {
394 if (p
->item
->date
< item
->date
) {
399 return commit_list_insert(item
, pp
);
402 static int commit_list_compare_by_date(const void *a
, const void *b
)
404 unsigned long a_date
= ((const struct commit_list
*)a
)->item
->date
;
405 unsigned long b_date
= ((const struct commit_list
*)b
)->item
->date
;
413 static void *commit_list_get_next(const void *a
)
415 return ((const struct commit_list
*)a
)->next
;
418 static void commit_list_set_next(void *a
, void *next
)
420 ((struct commit_list
*)a
)->next
= next
;
423 void commit_list_sort_by_date(struct commit_list
**list
)
425 *list
= llist_mergesort(*list
, commit_list_get_next
, commit_list_set_next
,
426 commit_list_compare_by_date
);
429 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
432 struct commit
*ret
= (*list
)->item
;
433 struct commit_list
*parents
= ret
->parents
;
434 struct commit_list
*old
= *list
;
436 *list
= (*list
)->next
;
440 struct commit
*commit
= parents
->item
;
441 if (!parse_commit(commit
) && !(commit
->object
.flags
& mark
)) {
442 commit
->object
.flags
|= mark
;
443 commit_list_insert_by_date(commit
, list
);
445 parents
= parents
->next
;
450 static void clear_commit_marks_1(struct commit_list
**plist
,
451 struct commit
*commit
, unsigned int mark
)
454 struct commit_list
*parents
;
456 if (!(mark
& commit
->object
.flags
))
459 commit
->object
.flags
&= ~mark
;
461 parents
= commit
->parents
;
465 while ((parents
= parents
->next
))
466 commit_list_insert(parents
->item
, plist
);
468 commit
= commit
->parents
->item
;
472 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
474 struct commit_list
*list
= NULL
;
475 commit_list_insert(commit
, &list
);
477 clear_commit_marks_1(&list
, pop_commit(&list
), mark
);
480 void clear_commit_marks_for_object_array(struct object_array
*a
, unsigned mark
)
482 struct object
*object
;
483 struct commit
*commit
;
486 for (i
= 0; i
< a
->nr
; i
++) {
487 object
= a
->objects
[i
].item
;
488 commit
= lookup_commit_reference_gently(object
->sha1
, 1);
490 clear_commit_marks(commit
, mark
);
494 struct commit
*pop_commit(struct commit_list
**stack
)
496 struct commit_list
*top
= *stack
;
497 struct commit
*item
= top
? top
->item
: NULL
;
507 * Topological sort support
510 /* count number of children that have not been emitted */
511 define_commit_slab(indegree_slab
, int);
513 static int compare_commits_by_commit_date(const void *a_
, const void *b_
, void *unused
)
515 const struct commit
*a
= a_
, *b
= b_
;
516 /* newer commits with larger date first */
517 if (a
->date
< b
->date
)
519 else if (a
->date
> b
->date
)
525 * Performs an in-place topological sort on the list supplied.
527 void sort_in_topological_order(struct commit_list
**list
, enum rev_sort_order sort_order
)
529 struct commit_list
*next
, *orig
= *list
;
530 struct commit_list
**pptr
;
531 struct indegree_slab indegree
;
532 struct prio_queue queue
;
533 struct commit
*commit
;
539 init_indegree_slab(&indegree
);
540 memset(&queue
, '\0', sizeof(queue
));
541 switch (sort_order
) {
542 default: /* REV_SORT_IN_GRAPH_ORDER */
543 queue
.compare
= NULL
;
545 case REV_SORT_BY_COMMIT_DATE
:
546 queue
.compare
= compare_commits_by_commit_date
;
550 /* Mark them and clear the indegree */
551 for (next
= orig
; next
; next
= next
->next
) {
552 struct commit
*commit
= next
->item
;
553 *(indegree_slab_at(&indegree
, commit
)) = 1;
556 /* update the indegree */
557 for (next
= orig
; next
; next
= next
->next
) {
558 struct commit_list
*parents
= next
->item
->parents
;
560 struct commit
*parent
= parents
->item
;
561 int *pi
= indegree_slab_at(&indegree
, parent
);
565 parents
= parents
->next
;
572 * tips are nodes not reachable from any other node in the list
574 * the tips serve as a starting set for the work queue.
576 for (next
= orig
; next
; next
= next
->next
) {
577 struct commit
*commit
= next
->item
;
579 if (*(indegree_slab_at(&indegree
, commit
)) == 1)
580 prio_queue_put(&queue
, commit
);
584 * This is unfortunate; the initial tips need to be shown
585 * in the order given from the revision traversal machinery.
587 if (sort_order
== REV_SORT_IN_GRAPH_ORDER
)
588 prio_queue_reverse(&queue
);
590 /* We no longer need the commit list */
591 free_commit_list(orig
);
595 while ((commit
= prio_queue_get(&queue
)) != NULL
) {
596 struct commit_list
*parents
;
598 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
599 struct commit
*parent
= parents
->item
;
600 int *pi
= indegree_slab_at(&indegree
, parent
);
606 * parents are only enqueued for emission
607 * when all their children have been emitted thereby
608 * guaranteeing topological order.
611 prio_queue_put(&queue
, parent
);
614 * all children of commit have already been
615 * emitted. we can emit it now.
617 *(indegree_slab_at(&indegree
, commit
)) = 0;
619 pptr
= &commit_list_insert(commit
, pptr
)->next
;
622 clear_indegree_slab(&indegree
);
623 clear_prio_queue(&queue
);
626 /* merge-base stuff */
628 /* bits #0..15 in revision.h */
629 #define PARENT1 (1u<<16)
630 #define PARENT2 (1u<<17)
631 #define STALE (1u<<18)
632 #define RESULT (1u<<19)
634 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
636 static struct commit
*interesting(struct commit_list
*list
)
639 struct commit
*commit
= list
->item
;
641 if (commit
->object
.flags
& STALE
)
648 /* all input commits in one and twos[] must have been parsed! */
649 static struct commit_list
*paint_down_to_common(struct commit
*one
, int n
, struct commit
**twos
)
651 struct commit_list
*list
= NULL
;
652 struct commit_list
*result
= NULL
;
655 one
->object
.flags
|= PARENT1
;
656 commit_list_insert_by_date(one
, &list
);
659 for (i
= 0; i
< n
; i
++) {
660 twos
[i
]->object
.flags
|= PARENT2
;
661 commit_list_insert_by_date(twos
[i
], &list
);
664 while (interesting(list
)) {
665 struct commit
*commit
;
666 struct commit_list
*parents
;
667 struct commit_list
*next
;
675 flags
= commit
->object
.flags
& (PARENT1
| PARENT2
| STALE
);
676 if (flags
== (PARENT1
| PARENT2
)) {
677 if (!(commit
->object
.flags
& RESULT
)) {
678 commit
->object
.flags
|= RESULT
;
679 commit_list_insert_by_date(commit
, &result
);
681 /* Mark parents of a found merge stale */
684 parents
= commit
->parents
;
686 struct commit
*p
= parents
->item
;
687 parents
= parents
->next
;
688 if ((p
->object
.flags
& flags
) == flags
)
692 p
->object
.flags
|= flags
;
693 commit_list_insert_by_date(p
, &list
);
697 free_commit_list(list
);
701 static struct commit_list
*merge_bases_many(struct commit
*one
, int n
, struct commit
**twos
)
703 struct commit_list
*list
= NULL
;
704 struct commit_list
*result
= NULL
;
707 for (i
= 0; i
< n
; i
++) {
710 * We do not mark this even with RESULT so we do not
711 * have to clean it up.
713 return commit_list_insert(one
, &result
);
716 if (parse_commit(one
))
718 for (i
= 0; i
< n
; i
++) {
719 if (parse_commit(twos
[i
]))
723 list
= paint_down_to_common(one
, n
, twos
);
726 struct commit_list
*next
= list
->next
;
727 if (!(list
->item
->object
.flags
& STALE
))
728 commit_list_insert_by_date(list
->item
, &result
);
735 struct commit_list
*get_octopus_merge_bases(struct commit_list
*in
)
737 struct commit_list
*i
, *j
, *k
, *ret
= NULL
;
738 struct commit_list
**pptr
= &ret
;
740 for (i
= in
; i
; i
= i
->next
) {
742 pptr
= &commit_list_insert(i
->item
, pptr
)->next
;
744 struct commit_list
*new = NULL
, *end
= NULL
;
746 for (j
= ret
; j
; j
= j
->next
) {
747 struct commit_list
*bases
;
748 bases
= get_merge_bases(i
->item
, j
->item
, 1);
753 for (k
= bases
; k
; k
= k
->next
)
762 static int remove_redundant(struct commit
**array
, int cnt
)
765 * Some commit in the array may be an ancestor of
766 * another commit. Move such commit to the end of
767 * the array, and return the number of commits that
768 * are independent from each other.
770 struct commit
**work
;
771 unsigned char *redundant
;
775 work
= xcalloc(cnt
, sizeof(*work
));
776 redundant
= xcalloc(cnt
, 1);
777 filled_index
= xmalloc(sizeof(*filled_index
) * (cnt
- 1));
779 for (i
= 0; i
< cnt
; i
++)
780 parse_commit(array
[i
]);
781 for (i
= 0; i
< cnt
; i
++) {
782 struct commit_list
*common
;
786 for (j
= filled
= 0; j
< cnt
; j
++) {
787 if (i
== j
|| redundant
[j
])
789 filled_index
[filled
] = j
;
790 work
[filled
++] = array
[j
];
792 common
= paint_down_to_common(array
[i
], filled
, work
);
793 if (array
[i
]->object
.flags
& PARENT2
)
795 for (j
= 0; j
< filled
; j
++)
796 if (work
[j
]->object
.flags
& PARENT1
)
797 redundant
[filled_index
[j
]] = 1;
798 clear_commit_marks(array
[i
], all_flags
);
799 for (j
= 0; j
< filled
; j
++)
800 clear_commit_marks(work
[j
], all_flags
);
801 free_commit_list(common
);
804 /* Now collect the result */
805 memcpy(work
, array
, sizeof(*array
) * cnt
);
806 for (i
= filled
= 0; i
< cnt
; i
++)
808 array
[filled
++] = work
[i
];
809 for (j
= filled
, i
= 0; i
< cnt
; i
++)
811 array
[j
++] = work
[i
];
818 struct commit_list
*get_merge_bases_many(struct commit
*one
,
820 struct commit
**twos
,
823 struct commit_list
*list
;
824 struct commit
**rslt
;
825 struct commit_list
*result
;
828 result
= merge_bases_many(one
, n
, twos
);
829 for (i
= 0; i
< n
; i
++) {
833 if (!result
|| !result
->next
) {
835 clear_commit_marks(one
, all_flags
);
836 for (i
= 0; i
< n
; i
++)
837 clear_commit_marks(twos
[i
], all_flags
);
842 /* There are more than one */
849 rslt
= xcalloc(cnt
, sizeof(*rslt
));
850 for (list
= result
, i
= 0; list
; list
= list
->next
)
851 rslt
[i
++] = list
->item
;
852 free_commit_list(result
);
854 clear_commit_marks(one
, all_flags
);
855 for (i
= 0; i
< n
; i
++)
856 clear_commit_marks(twos
[i
], all_flags
);
858 cnt
= remove_redundant(rslt
, cnt
);
860 for (i
= 0; i
< cnt
; i
++)
861 commit_list_insert_by_date(rslt
[i
], &result
);
866 struct commit_list
*get_merge_bases(struct commit
*one
, struct commit
*two
,
869 return get_merge_bases_many(one
, 1, &two
, cleanup
);
873 * Is "commit" a descendant of one of the elements on the "with_commit" list?
875 int is_descendant_of(struct commit
*commit
, struct commit_list
*with_commit
)
879 while (with_commit
) {
880 struct commit
*other
;
882 other
= with_commit
->item
;
883 with_commit
= with_commit
->next
;
884 if (in_merge_bases(other
, commit
))
891 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
893 int in_merge_bases(struct commit
*commit
, struct commit
*reference
)
895 struct commit_list
*bases
;
898 if (parse_commit(commit
) || parse_commit(reference
))
901 bases
= paint_down_to_common(commit
, 1, &reference
);
902 if (commit
->object
.flags
& PARENT2
)
904 clear_commit_marks(commit
, all_flags
);
905 clear_commit_marks(reference
, all_flags
);
906 free_commit_list(bases
);
910 struct commit_list
*reduce_heads(struct commit_list
*heads
)
912 struct commit_list
*p
;
913 struct commit_list
*result
= NULL
, **tail
= &result
;
914 struct commit
**array
;
921 for (p
= heads
; p
; p
= p
->next
)
922 p
->item
->object
.flags
&= ~STALE
;
923 for (p
= heads
, num_head
= 0; p
; p
= p
->next
) {
924 if (p
->item
->object
.flags
& STALE
)
926 p
->item
->object
.flags
|= STALE
;
929 array
= xcalloc(sizeof(*array
), num_head
);
930 for (p
= heads
, i
= 0; p
; p
= p
->next
) {
931 if (p
->item
->object
.flags
& STALE
) {
932 array
[i
++] = p
->item
;
933 p
->item
->object
.flags
&= ~STALE
;
936 num_head
= remove_redundant(array
, num_head
);
937 for (i
= 0; i
< num_head
; i
++)
938 tail
= &commit_list_insert(array
[i
], tail
)->next
;
942 static const char gpg_sig_header
[] = "gpgsig";
943 static const int gpg_sig_header_len
= sizeof(gpg_sig_header
) - 1;
945 static int do_sign_commit(struct strbuf
*buf
, const char *keyid
)
947 struct strbuf sig
= STRBUF_INIT
;
950 /* find the end of the header */
951 inspos
= strstr(buf
->buf
, "\n\n") - buf
->buf
+ 1;
953 if (!keyid
|| !*keyid
)
954 keyid
= get_signing_key();
955 if (sign_buffer(buf
, &sig
, keyid
)) {
956 strbuf_release(&sig
);
960 for (copypos
= 0; sig
.buf
[copypos
]; ) {
961 const char *bol
= sig
.buf
+ copypos
;
962 const char *eol
= strchrnul(bol
, '\n');
963 int len
= (eol
- bol
) + !!*eol
;
966 strbuf_insert(buf
, inspos
, gpg_sig_header
, gpg_sig_header_len
);
967 inspos
+= gpg_sig_header_len
;
969 strbuf_insert(buf
, inspos
++, " ", 1);
970 strbuf_insert(buf
, inspos
, bol
, len
);
974 strbuf_release(&sig
);
978 int parse_signed_commit(const unsigned char *sha1
,
979 struct strbuf
*payload
, struct strbuf
*signature
)
982 enum object_type type
;
983 char *buffer
= read_sha1_file(sha1
, &type
, &size
);
984 int in_signature
, saw_signature
= -1;
987 if (!buffer
|| type
!= OBJ_COMMIT
)
991 tail
= buffer
+ size
;
994 while (line
< tail
) {
995 const char *sig
= NULL
;
996 char *next
= memchr(line
, '\n', tail
- line
);
998 next
= next
? next
+ 1 : tail
;
999 if (in_signature
&& line
[0] == ' ')
1001 else if (!prefixcmp(line
, gpg_sig_header
) &&
1002 line
[gpg_sig_header_len
] == ' ')
1003 sig
= line
+ gpg_sig_header_len
+ 1;
1005 strbuf_add(signature
, sig
, next
- sig
);
1010 /* dump the whole remainder of the buffer */
1012 strbuf_add(payload
, line
, next
- line
);
1019 return saw_signature
;
1022 static void handle_signed_tag(struct commit
*parent
, struct commit_extra_header
***tail
)
1024 struct merge_remote_desc
*desc
;
1025 struct commit_extra_header
*mergetag
;
1027 unsigned long size
, len
;
1028 enum object_type type
;
1030 desc
= merge_remote_util(parent
);
1031 if (!desc
|| !desc
->obj
)
1033 buf
= read_sha1_file(desc
->obj
->sha1
, &type
, &size
);
1034 if (!buf
|| type
!= OBJ_TAG
)
1036 len
= parse_signature(buf
, size
);
1040 * We could verify this signature and either omit the tag when
1041 * it does not validate, but the integrator may not have the
1042 * public key of the signer of the tag he is merging, while a
1043 * later auditor may have it while auditing, so let's not run
1044 * verify-signed-buffer here for now...
1046 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1047 * warn("warning: signed tag unverified.");
1049 mergetag
= xcalloc(1, sizeof(*mergetag
));
1050 mergetag
->key
= xstrdup("mergetag");
1051 mergetag
->value
= buf
;
1052 mergetag
->len
= size
;
1055 *tail
= &mergetag
->next
;
1062 void append_merge_tag_headers(struct commit_list
*parents
,
1063 struct commit_extra_header
***tail
)
1066 struct commit
*parent
= parents
->item
;
1067 handle_signed_tag(parent
, tail
);
1068 parents
= parents
->next
;
1072 static void add_extra_header(struct strbuf
*buffer
,
1073 struct commit_extra_header
*extra
)
1075 strbuf_addstr(buffer
, extra
->key
);
1077 strbuf_add_lines(buffer
, " ", extra
->value
, extra
->len
);
1079 strbuf_addch(buffer
, '\n');
1082 struct commit_extra_header
*read_commit_extra_headers(struct commit
*commit
,
1083 const char **exclude
)
1085 struct commit_extra_header
*extra
= NULL
;
1087 enum object_type type
;
1088 char *buffer
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
1089 if (buffer
&& type
== OBJ_COMMIT
)
1090 extra
= read_commit_extra_header_lines(buffer
, size
, exclude
);
1095 static inline int standard_header_field(const char *field
, size_t len
)
1097 return ((len
== 4 && !memcmp(field
, "tree ", 5)) ||
1098 (len
== 6 && !memcmp(field
, "parent ", 7)) ||
1099 (len
== 6 && !memcmp(field
, "author ", 7)) ||
1100 (len
== 9 && !memcmp(field
, "committer ", 10)) ||
1101 (len
== 8 && !memcmp(field
, "encoding ", 9)));
1104 static int excluded_header_field(const char *field
, size_t len
, const char **exclude
)
1110 size_t xlen
= strlen(*exclude
);
1112 !memcmp(field
, *exclude
, xlen
) && field
[xlen
] == ' ')
1119 static struct commit_extra_header
*read_commit_extra_header_lines(
1120 const char *buffer
, size_t size
,
1121 const char **exclude
)
1123 struct commit_extra_header
*extra
= NULL
, **tail
= &extra
, *it
= NULL
;
1124 const char *line
, *next
, *eof
, *eob
;
1125 struct strbuf buf
= STRBUF_INIT
;
1127 for (line
= buffer
, eob
= line
+ size
;
1128 line
< eob
&& *line
!= '\n';
1130 next
= memchr(line
, '\n', eob
- line
);
1131 next
= next
? next
+ 1 : eob
;
1135 strbuf_add(&buf
, line
+ 1, next
- (line
+ 1));
1139 it
->value
= strbuf_detach(&buf
, &it
->len
);
1143 eof
= strchr(line
, ' ');
1147 if (standard_header_field(line
, eof
- line
) ||
1148 excluded_header_field(line
, eof
- line
, exclude
))
1151 it
= xcalloc(1, sizeof(*it
));
1152 it
->key
= xmemdupz(line
, eof
-line
);
1156 strbuf_add(&buf
, eof
+ 1, next
- (eof
+ 1));
1159 it
->value
= strbuf_detach(&buf
, &it
->len
);
1163 void free_commit_extra_headers(struct commit_extra_header
*extra
)
1166 struct commit_extra_header
*next
= extra
->next
;
1174 int commit_tree(const struct strbuf
*msg
, unsigned char *tree
,
1175 struct commit_list
*parents
, unsigned char *ret
,
1176 const char *author
, const char *sign_commit
)
1178 struct commit_extra_header
*extra
= NULL
, **tail
= &extra
;
1181 append_merge_tag_headers(parents
, &tail
);
1182 result
= commit_tree_extended(msg
, tree
, parents
, ret
,
1183 author
, sign_commit
, extra
);
1184 free_commit_extra_headers(extra
);
1188 static int find_invalid_utf8(const char *buf
, int len
)
1193 unsigned char c
= *buf
++;
1194 int bytes
, bad_offset
;
1199 /* Simple US-ASCII? No worries. */
1203 bad_offset
= offset
-1;
1206 * Count how many more high bits set: that's how
1207 * many more bytes this sequence should have.
1215 /* Must be between 1 and 5 more bytes */
1216 if (bytes
< 1 || bytes
> 5)
1219 /* Do we *have* that many bytes? */
1226 /* And verify that they are good continuation bytes */
1228 if ((*buf
++ & 0xc0) != 0x80)
1232 /* We could/should check the value and length here too */
1238 * This verifies that the buffer is in proper utf8 format.
1240 * If it isn't, it assumes any non-utf8 characters are Latin1,
1241 * and does the conversion.
1243 * Fixme: we should probably also disallow overlong forms and
1244 * invalid characters. But we don't do that currently.
1246 static int verify_utf8(struct strbuf
*buf
)
1254 unsigned char replace
[2];
1256 bad
= find_invalid_utf8(buf
->buf
+ pos
, buf
->len
- pos
);
1262 strbuf_remove(buf
, pos
, 1);
1264 /* We know 'c' must be in the range 128-255 */
1265 replace
[0] = 0xc0 + (c
>> 6);
1266 replace
[1] = 0x80 + (c
& 0x3f);
1267 strbuf_insert(buf
, pos
, replace
, 2);
1272 static const char commit_utf8_warn
[] =
1273 "Warning: commit message did not conform to UTF-8.\n"
1274 "You may want to amend it after fixing the message, or set the config\n"
1275 "variable i18n.commitencoding to the encoding your project uses.\n";
1277 int commit_tree_extended(const struct strbuf
*msg
, unsigned char *tree
,
1278 struct commit_list
*parents
, unsigned char *ret
,
1279 const char *author
, const char *sign_commit
,
1280 struct commit_extra_header
*extra
)
1283 int encoding_is_utf8
;
1284 struct strbuf buffer
;
1286 assert_sha1_type(tree
, OBJ_TREE
);
1288 if (memchr(msg
->buf
, '\0', msg
->len
))
1289 return error("a NUL byte in commit log message not allowed.");
1291 /* Not having i18n.commitencoding is the same as having utf-8 */
1292 encoding_is_utf8
= is_encoding_utf8(git_commit_encoding
);
1294 strbuf_init(&buffer
, 8192); /* should avoid reallocs for the headers */
1295 strbuf_addf(&buffer
, "tree %s\n", sha1_to_hex(tree
));
1298 * NOTE! This ordering means that the same exact tree merged with a
1299 * different order of parents will be a _different_ changeset even
1300 * if everything else stays the same.
1303 struct commit_list
*next
= parents
->next
;
1304 struct commit
*parent
= parents
->item
;
1306 strbuf_addf(&buffer
, "parent %s\n",
1307 sha1_to_hex(parent
->object
.sha1
));
1312 /* Person/date information */
1314 author
= git_author_info(IDENT_STRICT
);
1315 strbuf_addf(&buffer
, "author %s\n", author
);
1316 strbuf_addf(&buffer
, "committer %s\n", git_committer_info(IDENT_STRICT
));
1317 if (!encoding_is_utf8
)
1318 strbuf_addf(&buffer
, "encoding %s\n", git_commit_encoding
);
1321 add_extra_header(&buffer
, extra
);
1322 extra
= extra
->next
;
1324 strbuf_addch(&buffer
, '\n');
1326 /* And add the comment */
1327 strbuf_addbuf(&buffer
, msg
);
1329 /* And check the encoding */
1330 if (encoding_is_utf8
&& !verify_utf8(&buffer
))
1331 fprintf(stderr
, commit_utf8_warn
);
1333 if (sign_commit
&& do_sign_commit(&buffer
, sign_commit
))
1336 result
= write_sha1_file(buffer
.buf
, buffer
.len
, commit_type
, ret
);
1337 strbuf_release(&buffer
);
1341 struct commit
*get_merge_parent(const char *name
)
1344 struct commit
*commit
;
1345 unsigned char sha1
[20];
1346 if (get_sha1(name
, sha1
))
1348 obj
= parse_object(sha1
);
1349 commit
= (struct commit
*)peel_to_type(name
, 0, obj
, OBJ_COMMIT
);
1350 if (commit
&& !commit
->util
) {
1351 struct merge_remote_desc
*desc
;
1352 desc
= xmalloc(sizeof(*desc
));
1354 desc
->name
= strdup(name
);
1355 commit
->util
= desc
;
1361 * Append a commit to the end of the commit_list.
1363 * next starts by pointing to the variable that holds the head of an
1364 * empty commit_list, and is updated to point to the "next" field of
1365 * the last item on the list as new commits are appended.
1369 * struct commit_list *list;
1370 * struct commit_list **next = &list;
1372 * next = commit_list_append(c1, next);
1373 * next = commit_list_append(c2, next);
1374 * assert(commit_list_count(list) == 2);
1377 struct commit_list
**commit_list_append(struct commit
*commit
,
1378 struct commit_list
**next
)
1380 struct commit_list
*new = xmalloc(sizeof(struct commit_list
));
1387 void print_commit_list(struct commit_list
*list
,
1388 const char *format_cur
,
1389 const char *format_last
)
1391 for ( ; list
; list
= list
->next
) {
1392 const char *format
= list
->next
? format_cur
: format_last
;
1393 printf(format
, sha1_to_hex(list
->item
->object
.sha1
));