9 #include "gpg-interface.h"
10 #include "mergesort.h"
12 static struct commit_extra_header
*read_commit_extra_header_lines(const char *buf
, size_t len
, const char **);
14 int save_commit_buffer
= 1;
16 const char *commit_type
= "commit";
18 static struct commit
*check_commit(struct object
*obj
,
19 const unsigned char *sha1
,
22 if (obj
->type
!= OBJ_COMMIT
) {
24 error("Object %s is a %s, not a commit",
25 sha1_to_hex(sha1
), typename(obj
->type
));
28 return (struct commit
*) obj
;
31 struct commit
*lookup_commit_reference_gently(const unsigned char *sha1
,
34 struct object
*obj
= deref_tag(parse_object(sha1
), NULL
, 0);
38 return check_commit(obj
, sha1
, quiet
);
41 struct commit
*lookup_commit_reference(const unsigned char *sha1
)
43 return lookup_commit_reference_gently(sha1
, 0);
46 struct commit
*lookup_commit_or_die(const unsigned char *sha1
, const char *ref_name
)
48 struct commit
*c
= lookup_commit_reference(sha1
);
50 die(_("could not parse %s"), ref_name
);
51 if (hashcmp(sha1
, c
->object
.sha1
)) {
52 warning(_("%s %s is not a commit!"),
53 ref_name
, sha1_to_hex(sha1
));
58 struct commit
*lookup_commit(const unsigned char *sha1
)
60 struct object
*obj
= lookup_object(sha1
);
62 return create_object(sha1
, OBJ_COMMIT
, alloc_commit_node());
64 obj
->type
= OBJ_COMMIT
;
65 return check_commit(obj
, sha1
, 0);
68 struct commit
*lookup_commit_reference_by_name(const char *name
)
70 unsigned char sha1
[20];
71 struct commit
*commit
;
73 if (get_sha1_committish(name
, sha1
))
75 commit
= lookup_commit_reference(sha1
);
76 if (!commit
|| parse_commit(commit
))
81 static unsigned long parse_commit_date(const char *buf
, const char *tail
)
87 if (memcmp(buf
, "author", 6))
89 while (buf
< tail
&& *buf
++ != '\n')
93 if (memcmp(buf
, "committer", 9))
95 while (buf
< tail
&& *buf
++ != '>')
100 while (buf
< tail
&& *buf
++ != '\n')
104 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
105 return strtoul(dateptr
, NULL
, 10);
108 static struct commit_graft
**commit_graft
;
109 static int commit_graft_alloc
, commit_graft_nr
;
111 static int commit_graft_pos(const unsigned char *sha1
)
115 hi
= commit_graft_nr
;
117 int mi
= (lo
+ hi
) / 2;
118 struct commit_graft
*graft
= commit_graft
[mi
];
119 int cmp
= hashcmp(sha1
, graft
->sha1
);
130 int register_commit_graft(struct commit_graft
*graft
, int ignore_dups
)
132 int pos
= commit_graft_pos(graft
->sha1
);
138 free(commit_graft
[pos
]);
139 commit_graft
[pos
] = graft
;
144 if (commit_graft_alloc
<= ++commit_graft_nr
) {
145 commit_graft_alloc
= alloc_nr(commit_graft_alloc
);
146 commit_graft
= xrealloc(commit_graft
,
147 sizeof(*commit_graft
) *
150 if (pos
< commit_graft_nr
)
151 memmove(commit_graft
+ pos
+ 1,
153 (commit_graft_nr
- pos
- 1) *
154 sizeof(*commit_graft
));
155 commit_graft
[pos
] = graft
;
159 struct commit_graft
*read_graft_line(char *buf
, int len
)
161 /* The format is just "Commit Parent1 Parent2 ...\n" */
163 struct commit_graft
*graft
= NULL
;
165 while (len
&& isspace(buf
[len
-1]))
167 if (buf
[0] == '#' || buf
[0] == '\0')
171 i
= (len
+ 1) / 41 - 1;
172 graft
= xmalloc(sizeof(*graft
) + 20 * i
);
173 graft
->nr_parent
= i
;
174 if (get_sha1_hex(buf
, graft
->sha1
))
176 for (i
= 40; i
< len
; i
+= 41) {
179 if (get_sha1_hex(buf
+ i
+ 1, graft
->parent
[i
/41]))
185 error("bad graft data: %s", buf
);
190 static int read_graft_file(const char *graft_file
)
192 FILE *fp
= fopen(graft_file
, "r");
196 while (fgets(buf
, sizeof(buf
), fp
)) {
197 /* The format is just "Commit Parent1 Parent2 ...\n" */
198 int len
= strlen(buf
);
199 struct commit_graft
*graft
= read_graft_line(buf
, len
);
202 if (register_commit_graft(graft
, 1))
203 error("duplicate graft data: %s", buf
);
209 static void prepare_commit_graft(void)
211 static int commit_graft_prepared
;
214 if (commit_graft_prepared
)
216 graft_file
= get_graft_file();
217 read_graft_file(graft_file
);
218 /* make sure shallows are read */
219 is_repository_shallow();
220 commit_graft_prepared
= 1;
223 struct commit_graft
*lookup_commit_graft(const unsigned char *sha1
)
226 prepare_commit_graft();
227 pos
= commit_graft_pos(sha1
);
230 return commit_graft
[pos
];
233 int for_each_commit_graft(each_commit_graft_fn fn
, void *cb_data
)
236 for (i
= ret
= 0; i
< commit_graft_nr
&& !ret
; i
++)
237 ret
= fn(commit_graft
[i
], cb_data
);
241 int unregister_shallow(const unsigned char *sha1
)
243 int pos
= commit_graft_pos(sha1
);
246 if (pos
+ 1 < commit_graft_nr
)
247 memmove(commit_graft
+ pos
, commit_graft
+ pos
+ 1,
248 sizeof(struct commit_graft
*)
249 * (commit_graft_nr
- pos
- 1));
254 int parse_commit_buffer(struct commit
*item
, const void *buffer
, unsigned long size
)
256 const char *tail
= buffer
;
257 const char *bufptr
= buffer
;
258 unsigned char parent
[20];
259 struct commit_list
**pptr
;
260 struct commit_graft
*graft
;
262 if (item
->object
.parsed
)
264 item
->object
.parsed
= 1;
266 if (tail
<= bufptr
+ 46 || memcmp(bufptr
, "tree ", 5) || bufptr
[45] != '\n')
267 return error("bogus commit object %s", sha1_to_hex(item
->object
.sha1
));
268 if (get_sha1_hex(bufptr
+ 5, parent
) < 0)
269 return error("bad tree pointer in commit %s",
270 sha1_to_hex(item
->object
.sha1
));
271 item
->tree
= lookup_tree(parent
);
272 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
273 pptr
= &item
->parents
;
275 graft
= lookup_commit_graft(item
->object
.sha1
);
276 while (bufptr
+ 48 < tail
&& !memcmp(bufptr
, "parent ", 7)) {
277 struct commit
*new_parent
;
279 if (tail
<= bufptr
+ 48 ||
280 get_sha1_hex(bufptr
+ 7, parent
) ||
282 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
285 * The clone is shallow if nr_parent < 0, and we must
286 * not traverse its real parents even when we unhide them.
288 if (graft
&& (graft
->nr_parent
< 0 || grafts_replace_parents
))
290 new_parent
= lookup_commit(parent
);
292 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
296 struct commit
*new_parent
;
297 for (i
= 0; i
< graft
->nr_parent
; i
++) {
298 new_parent
= lookup_commit(graft
->parent
[i
]);
301 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
304 item
->date
= parse_commit_date(bufptr
, tail
);
309 int parse_commit(struct commit
*item
)
311 enum object_type type
;
318 if (item
->object
.parsed
)
320 buffer
= read_sha1_file(item
->object
.sha1
, &type
, &size
);
322 return error("Could not read %s",
323 sha1_to_hex(item
->object
.sha1
));
324 if (type
!= OBJ_COMMIT
) {
326 return error("Object %s not a commit",
327 sha1_to_hex(item
->object
.sha1
));
329 ret
= parse_commit_buffer(item
, buffer
, size
);
330 if (save_commit_buffer
&& !ret
) {
331 item
->buffer
= buffer
;
338 int find_commit_subject(const char *commit_buffer
, const char **subject
)
341 const char *p
= commit_buffer
;
343 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
347 for (eol
= p
; *eol
&& *eol
!= '\n'; eol
++)
357 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
359 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
360 new_list
->item
= item
;
361 new_list
->next
= *list_p
;
366 unsigned commit_list_count(const struct commit_list
*l
)
369 for (; l
; l
= l
->next
)
374 void free_commit_list(struct commit_list
*list
)
377 struct commit_list
*temp
= list
;
383 struct commit_list
* commit_list_insert_by_date(struct commit
*item
, struct commit_list
**list
)
385 struct commit_list
**pp
= list
;
386 struct commit_list
*p
;
387 while ((p
= *pp
) != NULL
) {
388 if (p
->item
->date
< item
->date
) {
393 return commit_list_insert(item
, pp
);
396 static int commit_list_compare_by_date(const void *a
, const void *b
)
398 unsigned long a_date
= ((const struct commit_list
*)a
)->item
->date
;
399 unsigned long b_date
= ((const struct commit_list
*)b
)->item
->date
;
407 static void *commit_list_get_next(const void *a
)
409 return ((const struct commit_list
*)a
)->next
;
412 static void commit_list_set_next(void *a
, void *next
)
414 ((struct commit_list
*)a
)->next
= next
;
417 void commit_list_sort_by_date(struct commit_list
**list
)
419 *list
= llist_mergesort(*list
, commit_list_get_next
, commit_list_set_next
,
420 commit_list_compare_by_date
);
423 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
426 struct commit
*ret
= (*list
)->item
;
427 struct commit_list
*parents
= ret
->parents
;
428 struct commit_list
*old
= *list
;
430 *list
= (*list
)->next
;
434 struct commit
*commit
= parents
->item
;
435 if (!parse_commit(commit
) && !(commit
->object
.flags
& mark
)) {
436 commit
->object
.flags
|= mark
;
437 commit_list_insert_by_date(commit
, list
);
439 parents
= parents
->next
;
444 static void clear_commit_marks_1(struct commit_list
**plist
,
445 struct commit
*commit
, unsigned int mark
)
448 struct commit_list
*parents
;
450 if (!(mark
& commit
->object
.flags
))
453 commit
->object
.flags
&= ~mark
;
455 parents
= commit
->parents
;
459 while ((parents
= parents
->next
))
460 commit_list_insert(parents
->item
, plist
);
462 commit
= commit
->parents
->item
;
466 void clear_commit_marks_many(int nr
, struct commit
**commit
, unsigned int mark
)
468 struct commit_list
*list
= NULL
;
471 commit_list_insert(*commit
, &list
);
475 clear_commit_marks_1(&list
, pop_commit(&list
), mark
);
478 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
480 clear_commit_marks_many(1, &commit
, mark
);
483 void clear_commit_marks_for_object_array(struct object_array
*a
, unsigned mark
)
485 struct object
*object
;
486 struct commit
*commit
;
489 for (i
= 0; i
< a
->nr
; i
++) {
490 object
= a
->objects
[i
].item
;
491 commit
= lookup_commit_reference_gently(object
->sha1
, 1);
493 clear_commit_marks(commit
, mark
);
497 struct commit
*pop_commit(struct commit_list
**stack
)
499 struct commit_list
*top
= *stack
;
500 struct commit
*item
= top
? top
->item
: NULL
;
510 * Performs an in-place topological sort on the list supplied.
512 void sort_in_topological_order(struct commit_list
** list
, int lifo
)
514 struct commit_list
*next
, *orig
= *list
;
515 struct commit_list
*work
, **insert
;
516 struct commit_list
**pptr
;
522 /* Mark them and clear the indegree */
523 for (next
= orig
; next
; next
= next
->next
) {
524 struct commit
*commit
= next
->item
;
525 commit
->indegree
= 1;
528 /* update the indegree */
529 for (next
= orig
; next
; next
= next
->next
) {
530 struct commit_list
* parents
= next
->item
->parents
;
532 struct commit
*parent
= parents
->item
;
534 if (parent
->indegree
)
536 parents
= parents
->next
;
543 * tips are nodes not reachable from any other node in the list
545 * the tips serve as a starting set for the work queue.
549 for (next
= orig
; next
; next
= next
->next
) {
550 struct commit
*commit
= next
->item
;
552 if (commit
->indegree
== 1)
553 insert
= &commit_list_insert(commit
, insert
)->next
;
556 /* process the list in topological order */
558 commit_list_sort_by_date(&work
);
563 struct commit
*commit
;
564 struct commit_list
*parents
, *work_item
;
567 work
= work_item
->next
;
568 work_item
->next
= NULL
;
570 commit
= work_item
->item
;
571 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
572 struct commit
*parent
= parents
->item
;
574 if (!parent
->indegree
)
578 * parents are only enqueued for emission
579 * when all their children have been emitted thereby
580 * guaranteeing topological order.
582 if (--parent
->indegree
== 1) {
584 commit_list_insert_by_date(parent
, &work
);
586 commit_list_insert(parent
, &work
);
590 * work_item is a commit all of whose children
591 * have already been emitted. we can emit it now.
593 commit
->indegree
= 0;
595 pptr
= &work_item
->next
;
599 /* merge-base stuff */
601 /* bits #0..15 in revision.h */
602 #define PARENT1 (1u<<16)
603 #define PARENT2 (1u<<17)
604 #define STALE (1u<<18)
605 #define RESULT (1u<<19)
607 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
609 static struct commit
*interesting(struct commit_list
*list
)
612 struct commit
*commit
= list
->item
;
614 if (commit
->object
.flags
& STALE
)
621 /* all input commits in one and twos[] must have been parsed! */
622 static struct commit_list
*paint_down_to_common(struct commit
*one
, int n
, struct commit
**twos
)
624 struct commit_list
*list
= NULL
;
625 struct commit_list
*result
= NULL
;
628 one
->object
.flags
|= PARENT1
;
629 commit_list_insert_by_date(one
, &list
);
632 for (i
= 0; i
< n
; i
++) {
633 twos
[i
]->object
.flags
|= PARENT2
;
634 commit_list_insert_by_date(twos
[i
], &list
);
637 while (interesting(list
)) {
638 struct commit
*commit
;
639 struct commit_list
*parents
;
640 struct commit_list
*next
;
648 flags
= commit
->object
.flags
& (PARENT1
| PARENT2
| STALE
);
649 if (flags
== (PARENT1
| PARENT2
)) {
650 if (!(commit
->object
.flags
& RESULT
)) {
651 commit
->object
.flags
|= RESULT
;
652 commit_list_insert_by_date(commit
, &result
);
654 /* Mark parents of a found merge stale */
657 parents
= commit
->parents
;
659 struct commit
*p
= parents
->item
;
660 parents
= parents
->next
;
661 if ((p
->object
.flags
& flags
) == flags
)
665 p
->object
.flags
|= flags
;
666 commit_list_insert_by_date(p
, &list
);
670 free_commit_list(list
);
674 static struct commit_list
*merge_bases_many(struct commit
*one
, int n
, struct commit
**twos
)
676 struct commit_list
*list
= NULL
;
677 struct commit_list
*result
= NULL
;
680 for (i
= 0; i
< n
; i
++) {
683 * We do not mark this even with RESULT so we do not
684 * have to clean it up.
686 return commit_list_insert(one
, &result
);
689 if (parse_commit(one
))
691 for (i
= 0; i
< n
; i
++) {
692 if (parse_commit(twos
[i
]))
696 list
= paint_down_to_common(one
, n
, twos
);
699 struct commit_list
*next
= list
->next
;
700 if (!(list
->item
->object
.flags
& STALE
))
701 commit_list_insert_by_date(list
->item
, &result
);
708 struct commit_list
*get_octopus_merge_bases(struct commit_list
*in
)
710 struct commit_list
*i
, *j
, *k
, *ret
= NULL
;
711 struct commit_list
**pptr
= &ret
;
713 for (i
= in
; i
; i
= i
->next
) {
715 pptr
= &commit_list_insert(i
->item
, pptr
)->next
;
717 struct commit_list
*new = NULL
, *end
= NULL
;
719 for (j
= ret
; j
; j
= j
->next
) {
720 struct commit_list
*bases
;
721 bases
= get_merge_bases(i
->item
, j
->item
, 1);
726 for (k
= bases
; k
; k
= k
->next
)
735 static int remove_redundant(struct commit
**array
, int cnt
)
738 * Some commit in the array may be an ancestor of
739 * another commit. Move such commit to the end of
740 * the array, and return the number of commits that
741 * are independent from each other.
743 struct commit
**work
;
744 unsigned char *redundant
;
748 work
= xcalloc(cnt
, sizeof(*work
));
749 redundant
= xcalloc(cnt
, 1);
750 filled_index
= xmalloc(sizeof(*filled_index
) * (cnt
- 1));
752 for (i
= 0; i
< cnt
; i
++)
753 parse_commit(array
[i
]);
754 for (i
= 0; i
< cnt
; i
++) {
755 struct commit_list
*common
;
759 for (j
= filled
= 0; j
< cnt
; j
++) {
760 if (i
== j
|| redundant
[j
])
762 filled_index
[filled
] = j
;
763 work
[filled
++] = array
[j
];
765 common
= paint_down_to_common(array
[i
], filled
, work
);
766 if (array
[i
]->object
.flags
& PARENT2
)
768 for (j
= 0; j
< filled
; j
++)
769 if (work
[j
]->object
.flags
& PARENT1
)
770 redundant
[filled_index
[j
]] = 1;
771 clear_commit_marks(array
[i
], all_flags
);
772 for (j
= 0; j
< filled
; j
++)
773 clear_commit_marks(work
[j
], all_flags
);
774 free_commit_list(common
);
777 /* Now collect the result */
778 memcpy(work
, array
, sizeof(*array
) * cnt
);
779 for (i
= filled
= 0; i
< cnt
; i
++)
781 array
[filled
++] = work
[i
];
782 for (j
= filled
, i
= 0; i
< cnt
; i
++)
784 array
[j
++] = work
[i
];
791 struct commit_list
*get_merge_bases_many(struct commit
*one
,
793 struct commit
**twos
,
796 struct commit_list
*list
;
797 struct commit
**rslt
;
798 struct commit_list
*result
;
801 result
= merge_bases_many(one
, n
, twos
);
802 for (i
= 0; i
< n
; i
++) {
806 if (!result
|| !result
->next
) {
808 clear_commit_marks(one
, all_flags
);
809 clear_commit_marks_many(n
, twos
, all_flags
);
814 /* There are more than one */
821 rslt
= xcalloc(cnt
, sizeof(*rslt
));
822 for (list
= result
, i
= 0; list
; list
= list
->next
)
823 rslt
[i
++] = list
->item
;
824 free_commit_list(result
);
826 clear_commit_marks(one
, all_flags
);
827 clear_commit_marks_many(n
, twos
, all_flags
);
829 cnt
= remove_redundant(rslt
, cnt
);
831 for (i
= 0; i
< cnt
; i
++)
832 commit_list_insert_by_date(rslt
[i
], &result
);
837 struct commit_list
*get_merge_bases(struct commit
*one
, struct commit
*two
,
840 return get_merge_bases_many(one
, 1, &two
, cleanup
);
844 * Is "commit" a descendant of one of the elements on the "with_commit" list?
846 int is_descendant_of(struct commit
*commit
, struct commit_list
*with_commit
)
850 while (with_commit
) {
851 struct commit
*other
;
853 other
= with_commit
->item
;
854 with_commit
= with_commit
->next
;
855 if (in_merge_bases(other
, commit
))
862 * Is "commit" an ancestor of one of the "references"?
864 int in_merge_bases_many(struct commit
*commit
, int nr_reference
, struct commit
**reference
)
866 struct commit_list
*bases
;
869 if (parse_commit(commit
))
871 for (i
= 0; i
< nr_reference
; i
++)
872 if (parse_commit(reference
[i
]))
875 bases
= paint_down_to_common(commit
, nr_reference
, reference
);
876 if (commit
->object
.flags
& PARENT2
)
878 clear_commit_marks(commit
, all_flags
);
879 clear_commit_marks_many(nr_reference
, reference
, all_flags
);
880 free_commit_list(bases
);
885 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
887 int in_merge_bases(struct commit
*commit
, struct commit
*reference
)
889 return in_merge_bases_many(commit
, 1, &reference
);
892 struct commit_list
*reduce_heads(struct commit_list
*heads
)
894 struct commit_list
*p
;
895 struct commit_list
*result
= NULL
, **tail
= &result
;
896 struct commit
**array
;
903 for (p
= heads
; p
; p
= p
->next
)
904 p
->item
->object
.flags
&= ~STALE
;
905 for (p
= heads
, num_head
= 0; p
; p
= p
->next
) {
906 if (p
->item
->object
.flags
& STALE
)
908 p
->item
->object
.flags
|= STALE
;
911 array
= xcalloc(sizeof(*array
), num_head
);
912 for (p
= heads
, i
= 0; p
; p
= p
->next
) {
913 if (p
->item
->object
.flags
& STALE
) {
914 array
[i
++] = p
->item
;
915 p
->item
->object
.flags
&= ~STALE
;
918 num_head
= remove_redundant(array
, num_head
);
919 for (i
= 0; i
< num_head
; i
++)
920 tail
= &commit_list_insert(array
[i
], tail
)->next
;
924 static const char gpg_sig_header
[] = "gpgsig";
925 static const int gpg_sig_header_len
= sizeof(gpg_sig_header
) - 1;
927 static int do_sign_commit(struct strbuf
*buf
, const char *keyid
)
929 struct strbuf sig
= STRBUF_INIT
;
932 /* find the end of the header */
933 inspos
= strstr(buf
->buf
, "\n\n") - buf
->buf
+ 1;
935 if (!keyid
|| !*keyid
)
936 keyid
= get_signing_key();
937 if (sign_buffer(buf
, &sig
, keyid
)) {
938 strbuf_release(&sig
);
942 for (copypos
= 0; sig
.buf
[copypos
]; ) {
943 const char *bol
= sig
.buf
+ copypos
;
944 const char *eol
= strchrnul(bol
, '\n');
945 int len
= (eol
- bol
) + !!*eol
;
948 strbuf_insert(buf
, inspos
, gpg_sig_header
, gpg_sig_header_len
);
949 inspos
+= gpg_sig_header_len
;
951 strbuf_insert(buf
, inspos
++, " ", 1);
952 strbuf_insert(buf
, inspos
, bol
, len
);
956 strbuf_release(&sig
);
960 int parse_signed_commit(const unsigned char *sha1
,
961 struct strbuf
*payload
, struct strbuf
*signature
)
964 enum object_type type
;
965 char *buffer
= read_sha1_file(sha1
, &type
, &size
);
966 int in_signature
, saw_signature
= -1;
969 if (!buffer
|| type
!= OBJ_COMMIT
)
973 tail
= buffer
+ size
;
976 while (line
< tail
) {
977 const char *sig
= NULL
;
978 char *next
= memchr(line
, '\n', tail
- line
);
980 next
= next
? next
+ 1 : tail
;
981 if (in_signature
&& line
[0] == ' ')
983 else if (!prefixcmp(line
, gpg_sig_header
) &&
984 line
[gpg_sig_header_len
] == ' ')
985 sig
= line
+ gpg_sig_header_len
+ 1;
987 strbuf_add(signature
, sig
, next
- sig
);
992 /* dump the whole remainder of the buffer */
994 strbuf_add(payload
, line
, next
- line
);
1001 return saw_signature
;
1004 static void handle_signed_tag(struct commit
*parent
, struct commit_extra_header
***tail
)
1006 struct merge_remote_desc
*desc
;
1007 struct commit_extra_header
*mergetag
;
1009 unsigned long size
, len
;
1010 enum object_type type
;
1012 desc
= merge_remote_util(parent
);
1013 if (!desc
|| !desc
->obj
)
1015 buf
= read_sha1_file(desc
->obj
->sha1
, &type
, &size
);
1016 if (!buf
|| type
!= OBJ_TAG
)
1018 len
= parse_signature(buf
, size
);
1022 * We could verify this signature and either omit the tag when
1023 * it does not validate, but the integrator may not have the
1024 * public key of the signer of the tag he is merging, while a
1025 * later auditor may have it while auditing, so let's not run
1026 * verify-signed-buffer here for now...
1028 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1029 * warn("warning: signed tag unverified.");
1031 mergetag
= xcalloc(1, sizeof(*mergetag
));
1032 mergetag
->key
= xstrdup("mergetag");
1033 mergetag
->value
= buf
;
1034 mergetag
->len
= size
;
1037 *tail
= &mergetag
->next
;
1047 } sigcheck_gpg_status
[] = {
1048 { 'G', "\n[GNUPG:] GOODSIG " },
1049 { 'B', "\n[GNUPG:] BADSIG " },
1050 { 'U', "\n[GNUPG:] TRUST_NEVER" },
1051 { 'U', "\n[GNUPG:] TRUST_UNDEFINED" },
1054 static void parse_gpg_output(struct signature_check
*sigc
)
1056 const char *buf
= sigc
->gpg_status
;
1059 /* Iterate over all search strings */
1060 for (i
= 0; i
< ARRAY_SIZE(sigcheck_gpg_status
); i
++) {
1061 const char *found
, *next
;
1063 if (!prefixcmp(buf
, sigcheck_gpg_status
[i
].check
+ 1)) {
1064 /* At the very beginning of the buffer */
1065 found
= buf
+ strlen(sigcheck_gpg_status
[i
].check
+ 1);
1067 found
= strstr(buf
, sigcheck_gpg_status
[i
].check
);
1070 found
+= strlen(sigcheck_gpg_status
[i
].check
);
1072 sigc
->result
= sigcheck_gpg_status
[i
].result
;
1073 /* The trust messages are not followed by key/signer information */
1074 if (sigc
->result
!= 'U') {
1075 sigc
->key
= xmemdupz(found
, 16);
1077 next
= strchrnul(found
, '\n');
1078 sigc
->signer
= xmemdupz(found
, next
- found
);
1083 void check_commit_signature(const struct commit
* commit
, struct signature_check
*sigc
)
1085 struct strbuf payload
= STRBUF_INIT
;
1086 struct strbuf signature
= STRBUF_INIT
;
1087 struct strbuf gpg_output
= STRBUF_INIT
;
1088 struct strbuf gpg_status
= STRBUF_INIT
;
1093 if (parse_signed_commit(commit
->object
.sha1
,
1094 &payload
, &signature
) <= 0)
1096 status
= verify_signed_buffer(payload
.buf
, payload
.len
,
1097 signature
.buf
, signature
.len
,
1098 &gpg_output
, &gpg_status
);
1099 if (status
&& !gpg_output
.len
)
1101 sigc
->gpg_output
= strbuf_detach(&gpg_output
, NULL
);
1102 sigc
->gpg_status
= strbuf_detach(&gpg_status
, NULL
);
1103 parse_gpg_output(sigc
);
1106 strbuf_release(&gpg_status
);
1107 strbuf_release(&gpg_output
);
1108 strbuf_release(&payload
);
1109 strbuf_release(&signature
);
1114 void append_merge_tag_headers(struct commit_list
*parents
,
1115 struct commit_extra_header
***tail
)
1118 struct commit
*parent
= parents
->item
;
1119 handle_signed_tag(parent
, tail
);
1120 parents
= parents
->next
;
1124 static void add_extra_header(struct strbuf
*buffer
,
1125 struct commit_extra_header
*extra
)
1127 strbuf_addstr(buffer
, extra
->key
);
1129 strbuf_add_lines(buffer
, " ", extra
->value
, extra
->len
);
1131 strbuf_addch(buffer
, '\n');
1134 struct commit_extra_header
*read_commit_extra_headers(struct commit
*commit
,
1135 const char **exclude
)
1137 struct commit_extra_header
*extra
= NULL
;
1139 enum object_type type
;
1140 char *buffer
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
1141 if (buffer
&& type
== OBJ_COMMIT
)
1142 extra
= read_commit_extra_header_lines(buffer
, size
, exclude
);
1147 static inline int standard_header_field(const char *field
, size_t len
)
1149 return ((len
== 4 && !memcmp(field
, "tree ", 5)) ||
1150 (len
== 6 && !memcmp(field
, "parent ", 7)) ||
1151 (len
== 6 && !memcmp(field
, "author ", 7)) ||
1152 (len
== 9 && !memcmp(field
, "committer ", 10)) ||
1153 (len
== 8 && !memcmp(field
, "encoding ", 9)));
1156 static int excluded_header_field(const char *field
, size_t len
, const char **exclude
)
1162 size_t xlen
= strlen(*exclude
);
1164 !memcmp(field
, *exclude
, xlen
) && field
[xlen
] == ' ')
1171 static struct commit_extra_header
*read_commit_extra_header_lines(
1172 const char *buffer
, size_t size
,
1173 const char **exclude
)
1175 struct commit_extra_header
*extra
= NULL
, **tail
= &extra
, *it
= NULL
;
1176 const char *line
, *next
, *eof
, *eob
;
1177 struct strbuf buf
= STRBUF_INIT
;
1179 for (line
= buffer
, eob
= line
+ size
;
1180 line
< eob
&& *line
!= '\n';
1182 next
= memchr(line
, '\n', eob
- line
);
1183 next
= next
? next
+ 1 : eob
;
1187 strbuf_add(&buf
, line
+ 1, next
- (line
+ 1));
1191 it
->value
= strbuf_detach(&buf
, &it
->len
);
1195 eof
= strchr(line
, ' ');
1199 if (standard_header_field(line
, eof
- line
) ||
1200 excluded_header_field(line
, eof
- line
, exclude
))
1203 it
= xcalloc(1, sizeof(*it
));
1204 it
->key
= xmemdupz(line
, eof
-line
);
1208 strbuf_add(&buf
, eof
+ 1, next
- (eof
+ 1));
1211 it
->value
= strbuf_detach(&buf
, &it
->len
);
1215 void free_commit_extra_headers(struct commit_extra_header
*extra
)
1218 struct commit_extra_header
*next
= extra
->next
;
1226 int commit_tree(const struct strbuf
*msg
, unsigned char *tree
,
1227 struct commit_list
*parents
, unsigned char *ret
,
1228 const char *author
, const char *sign_commit
)
1230 struct commit_extra_header
*extra
= NULL
, **tail
= &extra
;
1233 append_merge_tag_headers(parents
, &tail
);
1234 result
= commit_tree_extended(msg
, tree
, parents
, ret
,
1235 author
, sign_commit
, extra
);
1236 free_commit_extra_headers(extra
);
1240 static int find_invalid_utf8(const char *buf
, int len
)
1243 static const unsigned int max_codepoint
[] = {
1244 0x7f, 0x7ff, 0xffff, 0x10ffff
1248 unsigned char c
= *buf
++;
1249 int bytes
, bad_offset
;
1250 unsigned int codepoint
;
1251 unsigned int min_val
, max_val
;
1256 /* Simple US-ASCII? No worries. */
1260 bad_offset
= offset
-1;
1263 * Count how many more high bits set: that's how
1264 * many more bytes this sequence should have.
1273 * Must be between 1 and 3 more bytes. Longer sequences result in
1274 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1276 if (bytes
< 1 || 3 < bytes
)
1279 /* Do we *have* that many bytes? */
1284 * Place the encoded bits at the bottom of the value and compute the
1287 codepoint
= (c
& 0x7f) >> bytes
;
1288 min_val
= max_codepoint
[bytes
-1] + 1;
1289 max_val
= max_codepoint
[bytes
];
1294 /* And verify that they are good continuation bytes */
1297 codepoint
|= *buf
& 0x3f;
1298 if ((*buf
++ & 0xc0) != 0x80)
1302 /* Reject codepoints that are out of range for the sequence length. */
1303 if (codepoint
< min_val
|| codepoint
> max_val
)
1305 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1306 if ((codepoint
& 0x1ff800) == 0xd800)
1308 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1309 if ((codepoint
& 0xfffe) == 0xfffe)
1311 /* So are anything in the range U+FDD0..U+FDEF. */
1312 if (codepoint
>= 0xfdd0 && codepoint
<= 0xfdef)
1319 * This verifies that the buffer is in proper utf8 format.
1321 * If it isn't, it assumes any non-utf8 characters are Latin1,
1322 * and does the conversion.
1324 static int verify_utf8(struct strbuf
*buf
)
1332 unsigned char replace
[2];
1334 bad
= find_invalid_utf8(buf
->buf
+ pos
, buf
->len
- pos
);
1340 strbuf_remove(buf
, pos
, 1);
1342 /* We know 'c' must be in the range 128-255 */
1343 replace
[0] = 0xc0 + (c
>> 6);
1344 replace
[1] = 0x80 + (c
& 0x3f);
1345 strbuf_insert(buf
, pos
, replace
, 2);
1350 static const char commit_utf8_warn
[] =
1351 "Warning: commit message did not conform to UTF-8.\n"
1352 "You may want to amend it after fixing the message, or set the config\n"
1353 "variable i18n.commitencoding to the encoding your project uses.\n";
1355 int commit_tree_extended(const struct strbuf
*msg
, unsigned char *tree
,
1356 struct commit_list
*parents
, unsigned char *ret
,
1357 const char *author
, const char *sign_commit
,
1358 struct commit_extra_header
*extra
)
1361 int encoding_is_utf8
;
1362 struct strbuf buffer
;
1364 assert_sha1_type(tree
, OBJ_TREE
);
1366 if (memchr(msg
->buf
, '\0', msg
->len
))
1367 return error("a NUL byte in commit log message not allowed.");
1369 /* Not having i18n.commitencoding is the same as having utf-8 */
1370 encoding_is_utf8
= is_encoding_utf8(git_commit_encoding
);
1372 strbuf_init(&buffer
, 8192); /* should avoid reallocs for the headers */
1373 strbuf_addf(&buffer
, "tree %s\n", sha1_to_hex(tree
));
1376 * NOTE! This ordering means that the same exact tree merged with a
1377 * different order of parents will be a _different_ changeset even
1378 * if everything else stays the same.
1381 struct commit_list
*next
= parents
->next
;
1382 struct commit
*parent
= parents
->item
;
1384 strbuf_addf(&buffer
, "parent %s\n",
1385 sha1_to_hex(parent
->object
.sha1
));
1390 /* Person/date information */
1392 author
= git_author_info(IDENT_STRICT
);
1393 strbuf_addf(&buffer
, "author %s\n", author
);
1394 strbuf_addf(&buffer
, "committer %s\n", git_committer_info(IDENT_STRICT
));
1395 if (!encoding_is_utf8
)
1396 strbuf_addf(&buffer
, "encoding %s\n", git_commit_encoding
);
1399 add_extra_header(&buffer
, extra
);
1400 extra
= extra
->next
;
1402 strbuf_addch(&buffer
, '\n');
1404 /* And add the comment */
1405 strbuf_addbuf(&buffer
, msg
);
1407 /* And check the encoding */
1408 if (encoding_is_utf8
&& !verify_utf8(&buffer
))
1409 fprintf(stderr
, commit_utf8_warn
);
1411 if (sign_commit
&& do_sign_commit(&buffer
, sign_commit
))
1414 result
= write_sha1_file(buffer
.buf
, buffer
.len
, commit_type
, ret
);
1415 strbuf_release(&buffer
);
1419 struct commit
*get_merge_parent(const char *name
)
1422 struct commit
*commit
;
1423 unsigned char sha1
[20];
1424 if (get_sha1(name
, sha1
))
1426 obj
= parse_object(sha1
);
1427 commit
= (struct commit
*)peel_to_type(name
, 0, obj
, OBJ_COMMIT
);
1428 if (commit
&& !commit
->util
) {
1429 struct merge_remote_desc
*desc
;
1430 desc
= xmalloc(sizeof(*desc
));
1432 desc
->name
= strdup(name
);
1433 commit
->util
= desc
;
1439 * Append a commit to the end of the commit_list.
1441 * next starts by pointing to the variable that holds the head of an
1442 * empty commit_list, and is updated to point to the "next" field of
1443 * the last item on the list as new commits are appended.
1447 * struct commit_list *list;
1448 * struct commit_list **next = &list;
1450 * next = commit_list_append(c1, next);
1451 * next = commit_list_append(c2, next);
1452 * assert(commit_list_count(list) == 2);
1455 struct commit_list
**commit_list_append(struct commit
*commit
,
1456 struct commit_list
**next
)
1458 struct commit_list
*new = xmalloc(sizeof(struct commit_list
));
1465 void print_commit_list(struct commit_list
*list
,
1466 const char *format_cur
,
1467 const char *format_last
)
1469 for ( ; list
; list
= list
->next
) {
1470 const char *format
= list
->next
? format_cur
: format_last
;
1471 printf(format
, sha1_to_hex(list
->item
->object
.sha1
));