9 #include "gpg-interface.h"
11 int save_commit_buffer
= 1;
13 const char *commit_type
= "commit";
15 static struct commit
*check_commit(struct object
*obj
,
16 const unsigned char *sha1
,
19 if (obj
->type
!= OBJ_COMMIT
) {
21 error("Object %s is a %s, not a commit",
22 sha1_to_hex(sha1
), typename(obj
->type
));
25 return (struct commit
*) obj
;
28 struct commit
*lookup_commit_reference_gently(const unsigned char *sha1
,
31 struct object
*obj
= deref_tag(parse_object(sha1
), NULL
, 0);
35 return check_commit(obj
, sha1
, quiet
);
38 struct commit
*lookup_commit_reference(const unsigned char *sha1
)
40 return lookup_commit_reference_gently(sha1
, 0);
43 struct commit
*lookup_commit_or_die(const unsigned char *sha1
, const char *ref_name
)
45 struct commit
*c
= lookup_commit_reference(sha1
);
47 die(_("could not parse %s"), ref_name
);
48 if (hashcmp(sha1
, c
->object
.sha1
)) {
49 warning(_("%s %s is not a commit!"),
50 ref_name
, sha1_to_hex(sha1
));
55 struct commit
*lookup_commit(const unsigned char *sha1
)
57 struct object
*obj
= lookup_object(sha1
);
59 return create_object(sha1
, OBJ_COMMIT
, alloc_commit_node());
61 obj
->type
= OBJ_COMMIT
;
62 return check_commit(obj
, sha1
, 0);
65 struct commit
*lookup_commit_reference_by_name(const char *name
)
67 unsigned char sha1
[20];
68 struct commit
*commit
;
70 if (get_sha1(name
, sha1
))
72 commit
= lookup_commit_reference(sha1
);
73 if (!commit
|| parse_commit(commit
))
78 static unsigned long parse_commit_date(const char *buf
, const char *tail
)
84 if (memcmp(buf
, "author", 6))
86 while (buf
< tail
&& *buf
++ != '\n')
90 if (memcmp(buf
, "committer", 9))
92 while (buf
< tail
&& *buf
++ != '>')
97 while (buf
< tail
&& *buf
++ != '\n')
101 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
102 return strtoul(dateptr
, NULL
, 10);
105 static struct commit_graft
**commit_graft
;
106 static int commit_graft_alloc
, commit_graft_nr
;
108 static int commit_graft_pos(const unsigned char *sha1
)
112 hi
= commit_graft_nr
;
114 int mi
= (lo
+ hi
) / 2;
115 struct commit_graft
*graft
= commit_graft
[mi
];
116 int cmp
= hashcmp(sha1
, graft
->sha1
);
127 int register_commit_graft(struct commit_graft
*graft
, int ignore_dups
)
129 int pos
= commit_graft_pos(graft
->sha1
);
135 free(commit_graft
[pos
]);
136 commit_graft
[pos
] = graft
;
141 if (commit_graft_alloc
<= ++commit_graft_nr
) {
142 commit_graft_alloc
= alloc_nr(commit_graft_alloc
);
143 commit_graft
= xrealloc(commit_graft
,
144 sizeof(*commit_graft
) *
147 if (pos
< commit_graft_nr
)
148 memmove(commit_graft
+ pos
+ 1,
150 (commit_graft_nr
- pos
- 1) *
151 sizeof(*commit_graft
));
152 commit_graft
[pos
] = graft
;
156 struct commit_graft
*read_graft_line(char *buf
, int len
)
158 /* The format is just "Commit Parent1 Parent2 ...\n" */
160 struct commit_graft
*graft
= NULL
;
162 while (len
&& isspace(buf
[len
-1]))
164 if (buf
[0] == '#' || buf
[0] == '\0')
168 i
= (len
+ 1) / 41 - 1;
169 graft
= xmalloc(sizeof(*graft
) + 20 * i
);
170 graft
->nr_parent
= i
;
171 if (get_sha1_hex(buf
, graft
->sha1
))
173 for (i
= 40; i
< len
; i
+= 41) {
176 if (get_sha1_hex(buf
+ i
+ 1, graft
->parent
[i
/41]))
182 error("bad graft data: %s", buf
);
187 static int read_graft_file(const char *graft_file
)
189 FILE *fp
= fopen(graft_file
, "r");
193 while (fgets(buf
, sizeof(buf
), fp
)) {
194 /* The format is just "Commit Parent1 Parent2 ...\n" */
195 int len
= strlen(buf
);
196 struct commit_graft
*graft
= read_graft_line(buf
, len
);
199 if (register_commit_graft(graft
, 1))
200 error("duplicate graft data: %s", buf
);
206 static void prepare_commit_graft(void)
208 static int commit_graft_prepared
;
211 if (commit_graft_prepared
)
213 graft_file
= get_graft_file();
214 read_graft_file(graft_file
);
215 /* make sure shallows are read */
216 is_repository_shallow();
217 commit_graft_prepared
= 1;
220 struct commit_graft
*lookup_commit_graft(const unsigned char *sha1
)
223 prepare_commit_graft();
224 pos
= commit_graft_pos(sha1
);
227 return commit_graft
[pos
];
230 int for_each_commit_graft(each_commit_graft_fn fn
, void *cb_data
)
233 for (i
= ret
= 0; i
< commit_graft_nr
&& !ret
; i
++)
234 ret
= fn(commit_graft
[i
], cb_data
);
238 int unregister_shallow(const unsigned char *sha1
)
240 int pos
= commit_graft_pos(sha1
);
243 if (pos
+ 1 < commit_graft_nr
)
244 memmove(commit_graft
+ pos
, commit_graft
+ pos
+ 1,
245 sizeof(struct commit_graft
*)
246 * (commit_graft_nr
- pos
- 1));
251 int parse_commit_buffer(struct commit
*item
, const void *buffer
, unsigned long size
)
253 const char *tail
= buffer
;
254 const char *bufptr
= buffer
;
255 unsigned char parent
[20];
256 struct commit_list
**pptr
;
257 struct commit_graft
*graft
;
259 if (item
->object
.parsed
)
261 item
->object
.parsed
= 1;
263 if (tail
<= bufptr
+ 46 || memcmp(bufptr
, "tree ", 5) || bufptr
[45] != '\n')
264 return error("bogus commit object %s", sha1_to_hex(item
->object
.sha1
));
265 if (get_sha1_hex(bufptr
+ 5, parent
) < 0)
266 return error("bad tree pointer in commit %s",
267 sha1_to_hex(item
->object
.sha1
));
268 item
->tree
= lookup_tree(parent
);
269 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
270 pptr
= &item
->parents
;
272 graft
= lookup_commit_graft(item
->object
.sha1
);
273 while (bufptr
+ 48 < tail
&& !memcmp(bufptr
, "parent ", 7)) {
274 struct commit
*new_parent
;
276 if (tail
<= bufptr
+ 48 ||
277 get_sha1_hex(bufptr
+ 7, parent
) ||
279 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
282 * The clone is shallow if nr_parent < 0, and we must
283 * not traverse its real parents even when we unhide them.
285 if (graft
&& (graft
->nr_parent
< 0 || grafts_replace_parents
))
287 new_parent
= lookup_commit(parent
);
289 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
293 struct commit
*new_parent
;
294 for (i
= 0; i
< graft
->nr_parent
; i
++) {
295 new_parent
= lookup_commit(graft
->parent
[i
]);
298 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
301 item
->date
= parse_commit_date(bufptr
, tail
);
306 int parse_commit(struct commit
*item
)
308 enum object_type type
;
315 if (item
->object
.parsed
)
317 buffer
= read_sha1_file(item
->object
.sha1
, &type
, &size
);
319 return error("Could not read %s",
320 sha1_to_hex(item
->object
.sha1
));
321 if (type
!= OBJ_COMMIT
) {
323 return error("Object %s not a commit",
324 sha1_to_hex(item
->object
.sha1
));
326 ret
= parse_commit_buffer(item
, buffer
, size
);
327 if (save_commit_buffer
&& !ret
) {
328 item
->buffer
= buffer
;
335 int find_commit_subject(const char *commit_buffer
, const char **subject
)
338 const char *p
= commit_buffer
;
340 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
344 for (eol
= p
; *eol
&& *eol
!= '\n'; eol
++)
354 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
356 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
357 new_list
->item
= item
;
358 new_list
->next
= *list_p
;
363 unsigned commit_list_count(const struct commit_list
*l
)
366 for (; l
; l
= l
->next
)
371 void free_commit_list(struct commit_list
*list
)
374 struct commit_list
*temp
= list
;
380 struct commit_list
* commit_list_insert_by_date(struct commit
*item
, struct commit_list
**list
)
382 struct commit_list
**pp
= list
;
383 struct commit_list
*p
;
384 while ((p
= *pp
) != NULL
) {
385 if (p
->item
->date
< item
->date
) {
390 return commit_list_insert(item
, pp
);
394 void commit_list_sort_by_date(struct commit_list
**list
)
396 struct commit_list
*ret
= NULL
;
398 commit_list_insert_by_date((*list
)->item
, &ret
);
399 *list
= (*list
)->next
;
404 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
407 struct commit
*ret
= (*list
)->item
;
408 struct commit_list
*parents
= ret
->parents
;
409 struct commit_list
*old
= *list
;
411 *list
= (*list
)->next
;
415 struct commit
*commit
= parents
->item
;
416 if (!parse_commit(commit
) && !(commit
->object
.flags
& mark
)) {
417 commit
->object
.flags
|= mark
;
418 commit_list_insert_by_date(commit
, list
);
420 parents
= parents
->next
;
425 static void clear_commit_marks_1(struct commit_list
**plist
,
426 struct commit
*commit
, unsigned int mark
)
429 struct commit_list
*parents
;
431 if (!(mark
& commit
->object
.flags
))
434 commit
->object
.flags
&= ~mark
;
436 parents
= commit
->parents
;
440 while ((parents
= parents
->next
))
441 commit_list_insert(parents
->item
, plist
);
443 commit
= commit
->parents
->item
;
447 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
449 struct commit_list
*list
= NULL
;
450 commit_list_insert(commit
, &list
);
452 clear_commit_marks_1(&list
, pop_commit(&list
), mark
);
455 void clear_commit_marks_for_object_array(struct object_array
*a
, unsigned mark
)
457 struct object
*object
;
458 struct commit
*commit
;
461 for (i
= 0; i
< a
->nr
; i
++) {
462 object
= a
->objects
[i
].item
;
463 commit
= lookup_commit_reference_gently(object
->sha1
, 1);
465 clear_commit_marks(commit
, mark
);
469 struct commit
*pop_commit(struct commit_list
**stack
)
471 struct commit_list
*top
= *stack
;
472 struct commit
*item
= top
? top
->item
: NULL
;
482 * Performs an in-place topological sort on the list supplied.
484 void sort_in_topological_order(struct commit_list
** list
, int lifo
)
486 struct commit_list
*next
, *orig
= *list
;
487 struct commit_list
*work
, **insert
;
488 struct commit_list
**pptr
;
494 /* Mark them and clear the indegree */
495 for (next
= orig
; next
; next
= next
->next
) {
496 struct commit
*commit
= next
->item
;
497 commit
->indegree
= 1;
500 /* update the indegree */
501 for (next
= orig
; next
; next
= next
->next
) {
502 struct commit_list
* parents
= next
->item
->parents
;
504 struct commit
*parent
= parents
->item
;
506 if (parent
->indegree
)
508 parents
= parents
->next
;
515 * tips are nodes not reachable from any other node in the list
517 * the tips serve as a starting set for the work queue.
521 for (next
= orig
; next
; next
= next
->next
) {
522 struct commit
*commit
= next
->item
;
524 if (commit
->indegree
== 1)
525 insert
= &commit_list_insert(commit
, insert
)->next
;
528 /* process the list in topological order */
530 commit_list_sort_by_date(&work
);
535 struct commit
*commit
;
536 struct commit_list
*parents
, *work_item
;
539 work
= work_item
->next
;
540 work_item
->next
= NULL
;
542 commit
= work_item
->item
;
543 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
544 struct commit
*parent
= parents
->item
;
546 if (!parent
->indegree
)
550 * parents are only enqueued for emission
551 * when all their children have been emitted thereby
552 * guaranteeing topological order.
554 if (--parent
->indegree
== 1) {
556 commit_list_insert_by_date(parent
, &work
);
558 commit_list_insert(parent
, &work
);
562 * work_item is a commit all of whose children
563 * have already been emitted. we can emit it now.
565 commit
->indegree
= 0;
567 pptr
= &work_item
->next
;
571 /* merge-base stuff */
573 /* bits #0..15 in revision.h */
574 #define PARENT1 (1u<<16)
575 #define PARENT2 (1u<<17)
576 #define STALE (1u<<18)
577 #define RESULT (1u<<19)
579 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
581 static struct commit
*interesting(struct commit_list
*list
)
584 struct commit
*commit
= list
->item
;
586 if (commit
->object
.flags
& STALE
)
593 static struct commit_list
*merge_bases_many(struct commit
*one
, int n
, struct commit
**twos
)
595 struct commit_list
*list
= NULL
;
596 struct commit_list
*result
= NULL
;
599 for (i
= 0; i
< n
; i
++) {
602 * We do not mark this even with RESULT so we do not
603 * have to clean it up.
605 return commit_list_insert(one
, &result
);
608 if (parse_commit(one
))
610 for (i
= 0; i
< n
; i
++) {
611 if (parse_commit(twos
[i
]))
615 one
->object
.flags
|= PARENT1
;
616 commit_list_insert_by_date(one
, &list
);
617 for (i
= 0; i
< n
; i
++) {
618 twos
[i
]->object
.flags
|= PARENT2
;
619 commit_list_insert_by_date(twos
[i
], &list
);
622 while (interesting(list
)) {
623 struct commit
*commit
;
624 struct commit_list
*parents
;
625 struct commit_list
*next
;
633 flags
= commit
->object
.flags
& (PARENT1
| PARENT2
| STALE
);
634 if (flags
== (PARENT1
| PARENT2
)) {
635 if (!(commit
->object
.flags
& RESULT
)) {
636 commit
->object
.flags
|= RESULT
;
637 commit_list_insert_by_date(commit
, &result
);
639 /* Mark parents of a found merge stale */
642 parents
= commit
->parents
;
644 struct commit
*p
= parents
->item
;
645 parents
= parents
->next
;
646 if ((p
->object
.flags
& flags
) == flags
)
650 p
->object
.flags
|= flags
;
651 commit_list_insert_by_date(p
, &list
);
655 /* Clean up the result to remove stale ones */
656 free_commit_list(list
);
657 list
= result
; result
= NULL
;
659 struct commit_list
*next
= list
->next
;
660 if (!(list
->item
->object
.flags
& STALE
))
661 commit_list_insert_by_date(list
->item
, &result
);
668 struct commit_list
*get_octopus_merge_bases(struct commit_list
*in
)
670 struct commit_list
*i
, *j
, *k
, *ret
= NULL
;
671 struct commit_list
**pptr
= &ret
;
673 for (i
= in
; i
; i
= i
->next
) {
675 pptr
= &commit_list_insert(i
->item
, pptr
)->next
;
677 struct commit_list
*new = NULL
, *end
= NULL
;
679 for (j
= ret
; j
; j
= j
->next
) {
680 struct commit_list
*bases
;
681 bases
= get_merge_bases(i
->item
, j
->item
, 1);
686 for (k
= bases
; k
; k
= k
->next
)
695 struct commit_list
*get_merge_bases_many(struct commit
*one
,
697 struct commit
**twos
,
700 struct commit_list
*list
;
701 struct commit
**rslt
;
702 struct commit_list
*result
;
705 result
= merge_bases_many(one
, n
, twos
);
706 for (i
= 0; i
< n
; i
++) {
710 if (!result
|| !result
->next
) {
712 clear_commit_marks(one
, all_flags
);
713 for (i
= 0; i
< n
; i
++)
714 clear_commit_marks(twos
[i
], all_flags
);
719 /* There are more than one */
726 rslt
= xcalloc(cnt
, sizeof(*rslt
));
727 for (list
= result
, i
= 0; list
; list
= list
->next
)
728 rslt
[i
++] = list
->item
;
729 free_commit_list(result
);
731 clear_commit_marks(one
, all_flags
);
732 for (i
= 0; i
< n
; i
++)
733 clear_commit_marks(twos
[i
], all_flags
);
734 for (i
= 0; i
< cnt
- 1; i
++) {
735 for (j
= i
+1; j
< cnt
; j
++) {
736 if (!rslt
[i
] || !rslt
[j
])
738 result
= merge_bases_many(rslt
[i
], 1, &rslt
[j
]);
739 clear_commit_marks(rslt
[i
], all_flags
);
740 clear_commit_marks(rslt
[j
], all_flags
);
741 for (list
= result
; list
; list
= list
->next
) {
742 if (rslt
[i
] == list
->item
)
744 if (rslt
[j
] == list
->item
)
750 /* Surviving ones in rslt[] are the independent results */
752 for (i
= 0; i
< cnt
; i
++) {
754 commit_list_insert_by_date(rslt
[i
], &result
);
760 struct commit_list
*get_merge_bases(struct commit
*one
, struct commit
*two
,
763 return get_merge_bases_many(one
, 1, &two
, cleanup
);
766 int is_descendant_of(struct commit
*commit
, struct commit_list
*with_commit
)
770 while (with_commit
) {
771 struct commit
*other
;
773 other
= with_commit
->item
;
774 with_commit
= with_commit
->next
;
775 if (in_merge_bases(other
, &commit
, 1))
781 int in_merge_bases(struct commit
*commit
, struct commit
**reference
, int num
)
783 struct commit_list
*bases
, *b
;
787 bases
= get_merge_bases(commit
, *reference
, 1);
790 for (b
= bases
; b
; b
= b
->next
) {
791 if (!hashcmp(commit
->object
.sha1
, b
->item
->object
.sha1
)) {
797 free_commit_list(bases
);
801 struct commit_list
*reduce_heads(struct commit_list
*heads
)
803 struct commit_list
*p
;
804 struct commit_list
*result
= NULL
, **tail
= &result
;
805 struct commit
**other
;
806 size_t num_head
, num_other
;
811 /* Avoid unnecessary reallocations */
812 for (p
= heads
, num_head
= 0; p
; p
= p
->next
)
814 other
= xcalloc(sizeof(*other
), num_head
);
816 /* For each commit, see if it can be reached by others */
817 for (p
= heads
; p
; p
= p
->next
) {
818 struct commit_list
*q
, *base
;
820 /* Do we already have this in the result? */
821 for (q
= result
; q
; q
= q
->next
)
822 if (p
->item
== q
->item
)
828 for (q
= heads
; q
; q
= q
->next
) {
829 if (p
->item
== q
->item
)
831 other
[num_other
++] = q
->item
;
834 base
= get_merge_bases_many(p
->item
, num_other
, other
, 1);
838 * If p->item does not have anything common with other
839 * commits, there won't be any merge base. If it is
840 * reachable from some of the others, p->item will be
841 * the merge base. If its history is connected with
842 * others, but p->item is not reachable by others, we
843 * will get something other than p->item back.
845 if (!base
|| (base
->item
!= p
->item
))
846 tail
= &(commit_list_insert(p
->item
, tail
)->next
);
847 free_commit_list(base
);
853 static const char gpg_sig_header
[] = "gpgsig";
854 static const int gpg_sig_header_len
= sizeof(gpg_sig_header
) - 1;
856 static int do_sign_commit(struct strbuf
*buf
, const char *keyid
)
858 struct strbuf sig
= STRBUF_INIT
;
861 /* find the end of the header */
862 inspos
= strstr(buf
->buf
, "\n\n") - buf
->buf
+ 1;
864 if (!keyid
|| !*keyid
)
865 keyid
= get_signing_key();
866 if (sign_buffer(buf
, &sig
, keyid
)) {
867 strbuf_release(&sig
);
871 for (copypos
= 0; sig
.buf
[copypos
]; ) {
872 const char *bol
= sig
.buf
+ copypos
;
873 const char *eol
= strchrnul(bol
, '\n');
874 int len
= (eol
- bol
) + !!*eol
;
877 strbuf_insert(buf
, inspos
, gpg_sig_header
, gpg_sig_header_len
);
878 inspos
+= gpg_sig_header_len
;
880 strbuf_insert(buf
, inspos
++, " ", 1);
881 strbuf_insert(buf
, inspos
, bol
, len
);
885 strbuf_release(&sig
);
889 int parse_signed_commit(const unsigned char *sha1
,
890 struct strbuf
*payload
, struct strbuf
*signature
)
893 enum object_type type
;
894 char *buffer
= read_sha1_file(sha1
, &type
, &size
);
895 int in_signature
, saw_signature
= -1;
898 if (!buffer
|| type
!= OBJ_COMMIT
)
902 tail
= buffer
+ size
;
905 while (line
< tail
) {
906 const char *sig
= NULL
;
907 char *next
= memchr(line
, '\n', tail
- line
);
909 next
= next
? next
+ 1 : tail
;
910 if (in_signature
&& line
[0] == ' ')
912 else if (!prefixcmp(line
, gpg_sig_header
) &&
913 line
[gpg_sig_header_len
] == ' ')
914 sig
= line
+ gpg_sig_header_len
+ 1;
916 strbuf_add(signature
, sig
, next
- sig
);
921 /* dump the whole remainder of the buffer */
923 strbuf_add(payload
, line
, next
- line
);
930 return saw_signature
;
933 static void handle_signed_tag(struct commit
*parent
, struct commit_extra_header
***tail
)
935 struct merge_remote_desc
*desc
;
936 struct commit_extra_header
*mergetag
;
938 unsigned long size
, len
;
939 enum object_type type
;
941 desc
= merge_remote_util(parent
);
942 if (!desc
|| !desc
->obj
)
944 buf
= read_sha1_file(desc
->obj
->sha1
, &type
, &size
);
945 if (!buf
|| type
!= OBJ_TAG
)
947 len
= parse_signature(buf
, size
);
951 * We could verify this signature and either omit the tag when
952 * it does not validate, but the integrator may not have the
953 * public key of the signer of the tag he is merging, while a
954 * later auditor may have it while auditing, so let's not run
955 * verify-signed-buffer here for now...
957 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
958 * warn("warning: signed tag unverified.");
960 mergetag
= xcalloc(1, sizeof(*mergetag
));
961 mergetag
->key
= xstrdup("mergetag");
962 mergetag
->value
= buf
;
963 mergetag
->len
= size
;
966 *tail
= &mergetag
->next
;
973 void append_merge_tag_headers(struct commit_list
*parents
,
974 struct commit_extra_header
***tail
)
977 struct commit
*parent
= parents
->item
;
978 handle_signed_tag(parent
, tail
);
979 parents
= parents
->next
;
983 static void add_extra_header(struct strbuf
*buffer
,
984 struct commit_extra_header
*extra
)
986 strbuf_addstr(buffer
, extra
->key
);
988 strbuf_add_lines(buffer
, " ", extra
->value
, extra
->len
);
990 strbuf_addch(buffer
, '\n');
993 struct commit_extra_header
*read_commit_extra_headers(struct commit
*commit
,
994 const char **exclude
)
996 struct commit_extra_header
*extra
= NULL
;
998 enum object_type type
;
999 char *buffer
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
1000 if (buffer
&& type
== OBJ_COMMIT
)
1001 extra
= read_commit_extra_header_lines(buffer
, size
, exclude
);
1006 static inline int standard_header_field(const char *field
, size_t len
)
1008 return ((len
== 4 && !memcmp(field
, "tree ", 5)) ||
1009 (len
== 6 && !memcmp(field
, "parent ", 7)) ||
1010 (len
== 6 && !memcmp(field
, "author ", 7)) ||
1011 (len
== 9 && !memcmp(field
, "committer ", 10)) ||
1012 (len
== 8 && !memcmp(field
, "encoding ", 9)));
1015 static int excluded_header_field(const char *field
, size_t len
, const char **exclude
)
1021 size_t xlen
= strlen(*exclude
);
1023 !memcmp(field
, *exclude
, xlen
) && field
[xlen
] == ' ')
1030 struct commit_extra_header
*read_commit_extra_header_lines(const char *buffer
, size_t size
,
1031 const char **exclude
)
1033 struct commit_extra_header
*extra
= NULL
, **tail
= &extra
, *it
= NULL
;
1034 const char *line
, *next
, *eof
, *eob
;
1035 struct strbuf buf
= STRBUF_INIT
;
1037 for (line
= buffer
, eob
= line
+ size
;
1038 line
< eob
&& *line
!= '\n';
1040 next
= memchr(line
, '\n', eob
- line
);
1041 next
= next
? next
+ 1 : eob
;
1045 strbuf_add(&buf
, line
+ 1, next
- (line
+ 1));
1049 it
->value
= strbuf_detach(&buf
, &it
->len
);
1053 eof
= strchr(line
, ' ');
1057 if (standard_header_field(line
, eof
- line
) ||
1058 excluded_header_field(line
, eof
- line
, exclude
))
1061 it
= xcalloc(1, sizeof(*it
));
1062 it
->key
= xmemdupz(line
, eof
-line
);
1066 strbuf_add(&buf
, eof
+ 1, next
- (eof
+ 1));
1069 it
->value
= strbuf_detach(&buf
, &it
->len
);
1073 void free_commit_extra_headers(struct commit_extra_header
*extra
)
1076 struct commit_extra_header
*next
= extra
->next
;
1084 int commit_tree(const struct strbuf
*msg
, unsigned char *tree
,
1085 struct commit_list
*parents
, unsigned char *ret
,
1086 const char *author
, const char *sign_commit
)
1088 struct commit_extra_header
*extra
= NULL
, **tail
= &extra
;
1091 append_merge_tag_headers(parents
, &tail
);
1092 result
= commit_tree_extended(msg
, tree
, parents
, ret
,
1093 author
, sign_commit
, extra
);
1094 free_commit_extra_headers(extra
);
1098 static const char commit_utf8_warn
[] =
1099 "Warning: commit message does not conform to UTF-8.\n"
1100 "You may want to amend it after fixing the message, or set the config\n"
1101 "variable i18n.commitencoding to the encoding your project uses.\n";
1103 int commit_tree_extended(const struct strbuf
*msg
, unsigned char *tree
,
1104 struct commit_list
*parents
, unsigned char *ret
,
1105 const char *author
, const char *sign_commit
,
1106 struct commit_extra_header
*extra
)
1109 int encoding_is_utf8
;
1110 struct strbuf buffer
;
1112 assert_sha1_type(tree
, OBJ_TREE
);
1114 if (memchr(msg
->buf
, '\0', msg
->len
))
1115 return error("a NUL byte in commit log message not allowed.");
1117 /* Not having i18n.commitencoding is the same as having utf-8 */
1118 encoding_is_utf8
= is_encoding_utf8(git_commit_encoding
);
1120 strbuf_init(&buffer
, 8192); /* should avoid reallocs for the headers */
1121 strbuf_addf(&buffer
, "tree %s\n", sha1_to_hex(tree
));
1124 * NOTE! This ordering means that the same exact tree merged with a
1125 * different order of parents will be a _different_ changeset even
1126 * if everything else stays the same.
1129 struct commit_list
*next
= parents
->next
;
1130 struct commit
*parent
= parents
->item
;
1132 strbuf_addf(&buffer
, "parent %s\n",
1133 sha1_to_hex(parent
->object
.sha1
));
1138 /* Person/date information */
1140 author
= git_author_info(IDENT_ERROR_ON_NO_NAME
);
1141 strbuf_addf(&buffer
, "author %s\n", author
);
1142 strbuf_addf(&buffer
, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME
));
1143 if (!encoding_is_utf8
)
1144 strbuf_addf(&buffer
, "encoding %s\n", git_commit_encoding
);
1147 add_extra_header(&buffer
, extra
);
1148 extra
= extra
->next
;
1150 strbuf_addch(&buffer
, '\n');
1152 /* And add the comment */
1153 strbuf_addbuf(&buffer
, msg
);
1155 /* And check the encoding */
1156 if (encoding_is_utf8
&& !is_utf8(buffer
.buf
))
1157 fprintf(stderr
, commit_utf8_warn
);
1159 if (sign_commit
&& do_sign_commit(&buffer
, sign_commit
))
1162 result
= write_sha1_file(buffer
.buf
, buffer
.len
, commit_type
, ret
);
1163 strbuf_release(&buffer
);
1167 struct commit
*get_merge_parent(const char *name
)
1170 struct commit
*commit
;
1171 unsigned char sha1
[20];
1172 if (get_sha1(name
, sha1
))
1174 obj
= parse_object(sha1
);
1175 commit
= (struct commit
*)peel_to_type(name
, 0, obj
, OBJ_COMMIT
);
1176 if (commit
&& !commit
->util
) {
1177 struct merge_remote_desc
*desc
;
1178 desc
= xmalloc(sizeof(*desc
));
1180 desc
->name
= strdup(name
);
1181 commit
->util
= desc
;