packfile: keep prepare_packed_git() private
[alt-git.git] / commit.c
blobc948c8b0e46a4b13d1e3045944ca1cea05d75517
1 #include "cache.h"
2 #include "tag.h"
3 #include "commit.h"
4 #include "pkt-line.h"
5 #include "utf8.h"
6 #include "diff.h"
7 #include "revision.h"
8 #include "notes.h"
9 #include "gpg-interface.h"
10 #include "mergesort.h"
11 #include "commit-slab.h"
12 #include "prio-queue.h"
13 #include "sha1-lookup.h"
14 #include "wt-status.h"
16 static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
18 int save_commit_buffer = 1;
20 const char *commit_type = "commit";
22 struct commit *lookup_commit_reference_gently(const struct object_id *oid,
23 int quiet)
25 struct object *obj = deref_tag(parse_object(oid), NULL, 0);
27 if (!obj)
28 return NULL;
29 return object_as_type(obj, OBJ_COMMIT, quiet);
32 struct commit *lookup_commit_reference(const struct object_id *oid)
34 return lookup_commit_reference_gently(oid, 0);
37 struct commit *lookup_commit_or_die(const struct object_id *oid, const char *ref_name)
39 struct commit *c = lookup_commit_reference(oid);
40 if (!c)
41 die(_("could not parse %s"), ref_name);
42 if (oidcmp(oid, &c->object.oid)) {
43 warning(_("%s %s is not a commit!"),
44 ref_name, oid_to_hex(oid));
46 return c;
49 struct commit *lookup_commit(const struct object_id *oid)
51 struct object *obj = lookup_object(oid->hash);
52 if (!obj)
53 return create_object(oid->hash, alloc_commit_node());
54 return object_as_type(obj, OBJ_COMMIT, 0);
57 struct commit *lookup_commit_reference_by_name(const char *name)
59 struct object_id oid;
60 struct commit *commit;
62 if (get_oid_committish(name, &oid))
63 return NULL;
64 commit = lookup_commit_reference(&oid);
65 if (parse_commit(commit))
66 return NULL;
67 return commit;
70 static timestamp_t parse_commit_date(const char *buf, const char *tail)
72 const char *dateptr;
74 if (buf + 6 >= tail)
75 return 0;
76 if (memcmp(buf, "author", 6))
77 return 0;
78 while (buf < tail && *buf++ != '\n')
79 /* nada */;
80 if (buf + 9 >= tail)
81 return 0;
82 if (memcmp(buf, "committer", 9))
83 return 0;
84 while (buf < tail && *buf++ != '>')
85 /* nada */;
86 if (buf >= tail)
87 return 0;
88 dateptr = buf;
89 while (buf < tail && *buf++ != '\n')
90 /* nada */;
91 if (buf >= tail)
92 return 0;
93 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
94 return parse_timestamp(dateptr, NULL, 10);
97 static struct commit_graft **commit_graft;
98 static int commit_graft_alloc, commit_graft_nr;
100 static const unsigned char *commit_graft_sha1_access(size_t index, void *table)
102 struct commit_graft **commit_graft_table = table;
103 return commit_graft_table[index]->oid.hash;
106 static int commit_graft_pos(const unsigned char *sha1)
108 return sha1_pos(sha1, commit_graft, commit_graft_nr,
109 commit_graft_sha1_access);
112 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
114 int pos = commit_graft_pos(graft->oid.hash);
116 if (0 <= pos) {
117 if (ignore_dups)
118 free(graft);
119 else {
120 free(commit_graft[pos]);
121 commit_graft[pos] = graft;
123 return 1;
125 pos = -pos - 1;
126 ALLOC_GROW(commit_graft, commit_graft_nr + 1, commit_graft_alloc);
127 commit_graft_nr++;
128 if (pos < commit_graft_nr)
129 MOVE_ARRAY(commit_graft + pos + 1, commit_graft + pos,
130 commit_graft_nr - pos - 1);
131 commit_graft[pos] = graft;
132 return 0;
135 struct commit_graft *read_graft_line(struct strbuf *line)
137 /* The format is just "Commit Parent1 Parent2 ...\n" */
138 int i, phase;
139 const char *tail = NULL;
140 struct commit_graft *graft = NULL;
141 struct object_id dummy_oid, *oid;
143 strbuf_rtrim(line);
144 if (!line->len || line->buf[0] == '#')
145 return NULL;
147 * phase 0 verifies line, counts hashes in line and allocates graft
148 * phase 1 fills graft
150 for (phase = 0; phase < 2; phase++) {
151 oid = graft ? &graft->oid : &dummy_oid;
152 if (parse_oid_hex(line->buf, oid, &tail))
153 goto bad_graft_data;
154 for (i = 0; *tail != '\0'; i++) {
155 oid = graft ? &graft->parent[i] : &dummy_oid;
156 if (!isspace(*tail++) || parse_oid_hex(tail, oid, &tail))
157 goto bad_graft_data;
159 if (!graft) {
160 graft = xmalloc(st_add(sizeof(*graft),
161 st_mult(sizeof(struct object_id), i)));
162 graft->nr_parent = i;
165 return graft;
167 bad_graft_data:
168 error("bad graft data: %s", line->buf);
169 assert(!graft);
170 return NULL;
173 static int read_graft_file(const char *graft_file)
175 FILE *fp = fopen_or_warn(graft_file, "r");
176 struct strbuf buf = STRBUF_INIT;
177 if (!fp)
178 return -1;
179 while (!strbuf_getwholeline(&buf, fp, '\n')) {
180 /* The format is just "Commit Parent1 Parent2 ...\n" */
181 struct commit_graft *graft = read_graft_line(&buf);
182 if (!graft)
183 continue;
184 if (register_commit_graft(graft, 1))
185 error("duplicate graft data: %s", buf.buf);
187 fclose(fp);
188 strbuf_release(&buf);
189 return 0;
192 static void prepare_commit_graft(void)
194 static int commit_graft_prepared;
195 char *graft_file;
197 if (commit_graft_prepared)
198 return;
199 graft_file = get_graft_file();
200 read_graft_file(graft_file);
201 /* make sure shallows are read */
202 is_repository_shallow();
203 commit_graft_prepared = 1;
206 struct commit_graft *lookup_commit_graft(const struct object_id *oid)
208 int pos;
209 prepare_commit_graft();
210 pos = commit_graft_pos(oid->hash);
211 if (pos < 0)
212 return NULL;
213 return commit_graft[pos];
216 int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
218 int i, ret;
219 for (i = ret = 0; i < commit_graft_nr && !ret; i++)
220 ret = fn(commit_graft[i], cb_data);
221 return ret;
224 int unregister_shallow(const struct object_id *oid)
226 int pos = commit_graft_pos(oid->hash);
227 if (pos < 0)
228 return -1;
229 if (pos + 1 < commit_graft_nr)
230 MOVE_ARRAY(commit_graft + pos, commit_graft + pos + 1,
231 commit_graft_nr - pos - 1);
232 commit_graft_nr--;
233 return 0;
236 struct commit_buffer {
237 void *buffer;
238 unsigned long size;
240 define_commit_slab(buffer_slab, struct commit_buffer);
241 static struct buffer_slab buffer_slab = COMMIT_SLAB_INIT(1, buffer_slab);
243 void set_commit_buffer(struct commit *commit, void *buffer, unsigned long size)
245 struct commit_buffer *v = buffer_slab_at(&buffer_slab, commit);
246 v->buffer = buffer;
247 v->size = size;
250 const void *get_cached_commit_buffer(const struct commit *commit, unsigned long *sizep)
252 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
253 if (!v) {
254 if (sizep)
255 *sizep = 0;
256 return NULL;
258 if (sizep)
259 *sizep = v->size;
260 return v->buffer;
263 const void *get_commit_buffer(const struct commit *commit, unsigned long *sizep)
265 const void *ret = get_cached_commit_buffer(commit, sizep);
266 if (!ret) {
267 enum object_type type;
268 unsigned long size;
269 ret = read_sha1_file(commit->object.oid.hash, &type, &size);
270 if (!ret)
271 die("cannot read commit object %s",
272 oid_to_hex(&commit->object.oid));
273 if (type != OBJ_COMMIT)
274 die("expected commit for %s, got %s",
275 oid_to_hex(&commit->object.oid), typename(type));
276 if (sizep)
277 *sizep = size;
279 return ret;
282 void unuse_commit_buffer(const struct commit *commit, const void *buffer)
284 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
285 if (!(v && v->buffer == buffer))
286 free((void *)buffer);
289 void free_commit_buffer(struct commit *commit)
291 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
292 if (v) {
293 FREE_AND_NULL(v->buffer);
294 v->size = 0;
298 const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep)
300 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
301 void *ret;
303 if (!v) {
304 if (sizep)
305 *sizep = 0;
306 return NULL;
308 ret = v->buffer;
309 if (sizep)
310 *sizep = v->size;
312 v->buffer = NULL;
313 v->size = 0;
314 return ret;
317 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
319 const char *tail = buffer;
320 const char *bufptr = buffer;
321 struct object_id parent;
322 struct commit_list **pptr;
323 struct commit_graft *graft;
324 const int tree_entry_len = GIT_SHA1_HEXSZ + 5;
325 const int parent_entry_len = GIT_SHA1_HEXSZ + 7;
327 if (item->object.parsed)
328 return 0;
329 item->object.parsed = 1;
330 tail += size;
331 if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) ||
332 bufptr[tree_entry_len] != '\n')
333 return error("bogus commit object %s", oid_to_hex(&item->object.oid));
334 if (get_sha1_hex(bufptr + 5, parent.hash) < 0)
335 return error("bad tree pointer in commit %s",
336 oid_to_hex(&item->object.oid));
337 item->tree = lookup_tree(&parent);
338 bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
339 pptr = &item->parents;
341 graft = lookup_commit_graft(&item->object.oid);
342 while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
343 struct commit *new_parent;
345 if (tail <= bufptr + parent_entry_len + 1 ||
346 get_sha1_hex(bufptr + 7, parent.hash) ||
347 bufptr[parent_entry_len] != '\n')
348 return error("bad parents in commit %s", oid_to_hex(&item->object.oid));
349 bufptr += parent_entry_len + 1;
351 * The clone is shallow if nr_parent < 0, and we must
352 * not traverse its real parents even when we unhide them.
354 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
355 continue;
356 new_parent = lookup_commit(&parent);
357 if (new_parent)
358 pptr = &commit_list_insert(new_parent, pptr)->next;
360 if (graft) {
361 int i;
362 struct commit *new_parent;
363 for (i = 0; i < graft->nr_parent; i++) {
364 new_parent = lookup_commit(&graft->parent[i]);
365 if (!new_parent)
366 continue;
367 pptr = &commit_list_insert(new_parent, pptr)->next;
370 item->date = parse_commit_date(bufptr, tail);
372 return 0;
375 int parse_commit_gently(struct commit *item, int quiet_on_missing)
377 enum object_type type;
378 void *buffer;
379 unsigned long size;
380 int ret;
382 if (!item)
383 return -1;
384 if (item->object.parsed)
385 return 0;
386 buffer = read_sha1_file(item->object.oid.hash, &type, &size);
387 if (!buffer)
388 return quiet_on_missing ? -1 :
389 error("Could not read %s",
390 oid_to_hex(&item->object.oid));
391 if (type != OBJ_COMMIT) {
392 free(buffer);
393 return error("Object %s not a commit",
394 oid_to_hex(&item->object.oid));
396 ret = parse_commit_buffer(item, buffer, size);
397 if (save_commit_buffer && !ret) {
398 set_commit_buffer(item, buffer, size);
399 return 0;
401 free(buffer);
402 return ret;
405 void parse_commit_or_die(struct commit *item)
407 if (parse_commit(item))
408 die("unable to parse commit %s",
409 item ? oid_to_hex(&item->object.oid) : "(null)");
412 int find_commit_subject(const char *commit_buffer, const char **subject)
414 const char *eol;
415 const char *p = commit_buffer;
417 while (*p && (*p != '\n' || p[1] != '\n'))
418 p++;
419 if (*p) {
420 p = skip_blank_lines(p + 2);
421 eol = strchrnul(p, '\n');
422 } else
423 eol = p;
425 *subject = p;
427 return eol - p;
430 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
432 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
433 new_list->item = item;
434 new_list->next = *list_p;
435 *list_p = new_list;
436 return new_list;
439 unsigned commit_list_count(const struct commit_list *l)
441 unsigned c = 0;
442 for (; l; l = l->next )
443 c++;
444 return c;
447 struct commit_list *copy_commit_list(struct commit_list *list)
449 struct commit_list *head = NULL;
450 struct commit_list **pp = &head;
451 while (list) {
452 pp = commit_list_append(list->item, pp);
453 list = list->next;
455 return head;
458 void free_commit_list(struct commit_list *list)
460 while (list)
461 pop_commit(&list);
464 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
466 struct commit_list **pp = list;
467 struct commit_list *p;
468 while ((p = *pp) != NULL) {
469 if (p->item->date < item->date) {
470 break;
472 pp = &p->next;
474 return commit_list_insert(item, pp);
477 static int commit_list_compare_by_date(const void *a, const void *b)
479 timestamp_t a_date = ((const struct commit_list *)a)->item->date;
480 timestamp_t b_date = ((const struct commit_list *)b)->item->date;
481 if (a_date < b_date)
482 return 1;
483 if (a_date > b_date)
484 return -1;
485 return 0;
488 static void *commit_list_get_next(const void *a)
490 return ((const struct commit_list *)a)->next;
493 static void commit_list_set_next(void *a, void *next)
495 ((struct commit_list *)a)->next = next;
498 void commit_list_sort_by_date(struct commit_list **list)
500 *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
501 commit_list_compare_by_date);
504 struct commit *pop_most_recent_commit(struct commit_list **list,
505 unsigned int mark)
507 struct commit *ret = pop_commit(list);
508 struct commit_list *parents = ret->parents;
510 while (parents) {
511 struct commit *commit = parents->item;
512 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
513 commit->object.flags |= mark;
514 commit_list_insert_by_date(commit, list);
516 parents = parents->next;
518 return ret;
521 static void clear_commit_marks_1(struct commit_list **plist,
522 struct commit *commit, unsigned int mark)
524 while (commit) {
525 struct commit_list *parents;
527 if (!(mark & commit->object.flags))
528 return;
530 commit->object.flags &= ~mark;
532 parents = commit->parents;
533 if (!parents)
534 return;
536 while ((parents = parents->next))
537 commit_list_insert(parents->item, plist);
539 commit = commit->parents->item;
543 void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
545 struct commit_list *list = NULL;
547 while (nr--) {
548 clear_commit_marks_1(&list, *commit, mark);
549 commit++;
551 while (list)
552 clear_commit_marks_1(&list, pop_commit(&list), mark);
555 void clear_commit_marks(struct commit *commit, unsigned int mark)
557 clear_commit_marks_many(1, &commit, mark);
560 struct commit *pop_commit(struct commit_list **stack)
562 struct commit_list *top = *stack;
563 struct commit *item = top ? top->item : NULL;
565 if (top) {
566 *stack = top->next;
567 free(top);
569 return item;
573 * Topological sort support
576 /* count number of children that have not been emitted */
577 define_commit_slab(indegree_slab, int);
579 /* record author-date for each commit object */
580 define_commit_slab(author_date_slab, unsigned long);
582 static void record_author_date(struct author_date_slab *author_date,
583 struct commit *commit)
585 const char *buffer = get_commit_buffer(commit, NULL);
586 struct ident_split ident;
587 const char *ident_line;
588 size_t ident_len;
589 char *date_end;
590 timestamp_t date;
592 ident_line = find_commit_header(buffer, "author", &ident_len);
593 if (!ident_line)
594 goto fail_exit; /* no author line */
595 if (split_ident_line(&ident, ident_line, ident_len) ||
596 !ident.date_begin || !ident.date_end)
597 goto fail_exit; /* malformed "author" line */
599 date = parse_timestamp(ident.date_begin, &date_end, 10);
600 if (date_end != ident.date_end)
601 goto fail_exit; /* malformed date */
602 *(author_date_slab_at(author_date, commit)) = date;
604 fail_exit:
605 unuse_commit_buffer(commit, buffer);
608 static int compare_commits_by_author_date(const void *a_, const void *b_,
609 void *cb_data)
611 const struct commit *a = a_, *b = b_;
612 struct author_date_slab *author_date = cb_data;
613 timestamp_t a_date = *(author_date_slab_at(author_date, a));
614 timestamp_t b_date = *(author_date_slab_at(author_date, b));
616 /* newer commits with larger date first */
617 if (a_date < b_date)
618 return 1;
619 else if (a_date > b_date)
620 return -1;
621 return 0;
624 int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused)
626 const struct commit *a = a_, *b = b_;
627 /* newer commits with larger date first */
628 if (a->date < b->date)
629 return 1;
630 else if (a->date > b->date)
631 return -1;
632 return 0;
636 * Performs an in-place topological sort on the list supplied.
638 void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order)
640 struct commit_list *next, *orig = *list;
641 struct commit_list **pptr;
642 struct indegree_slab indegree;
643 struct prio_queue queue;
644 struct commit *commit;
645 struct author_date_slab author_date;
647 if (!orig)
648 return;
649 *list = NULL;
651 init_indegree_slab(&indegree);
652 memset(&queue, '\0', sizeof(queue));
654 switch (sort_order) {
655 default: /* REV_SORT_IN_GRAPH_ORDER */
656 queue.compare = NULL;
657 break;
658 case REV_SORT_BY_COMMIT_DATE:
659 queue.compare = compare_commits_by_commit_date;
660 break;
661 case REV_SORT_BY_AUTHOR_DATE:
662 init_author_date_slab(&author_date);
663 queue.compare = compare_commits_by_author_date;
664 queue.cb_data = &author_date;
665 break;
668 /* Mark them and clear the indegree */
669 for (next = orig; next; next = next->next) {
670 struct commit *commit = next->item;
671 *(indegree_slab_at(&indegree, commit)) = 1;
672 /* also record the author dates, if needed */
673 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
674 record_author_date(&author_date, commit);
677 /* update the indegree */
678 for (next = orig; next; next = next->next) {
679 struct commit_list *parents = next->item->parents;
680 while (parents) {
681 struct commit *parent = parents->item;
682 int *pi = indegree_slab_at(&indegree, parent);
684 if (*pi)
685 (*pi)++;
686 parents = parents->next;
691 * find the tips
693 * tips are nodes not reachable from any other node in the list
695 * the tips serve as a starting set for the work queue.
697 for (next = orig; next; next = next->next) {
698 struct commit *commit = next->item;
700 if (*(indegree_slab_at(&indegree, commit)) == 1)
701 prio_queue_put(&queue, commit);
705 * This is unfortunate; the initial tips need to be shown
706 * in the order given from the revision traversal machinery.
708 if (sort_order == REV_SORT_IN_GRAPH_ORDER)
709 prio_queue_reverse(&queue);
711 /* We no longer need the commit list */
712 free_commit_list(orig);
714 pptr = list;
715 *list = NULL;
716 while ((commit = prio_queue_get(&queue)) != NULL) {
717 struct commit_list *parents;
719 for (parents = commit->parents; parents ; parents = parents->next) {
720 struct commit *parent = parents->item;
721 int *pi = indegree_slab_at(&indegree, parent);
723 if (!*pi)
724 continue;
727 * parents are only enqueued for emission
728 * when all their children have been emitted thereby
729 * guaranteeing topological order.
731 if (--(*pi) == 1)
732 prio_queue_put(&queue, parent);
735 * all children of commit have already been
736 * emitted. we can emit it now.
738 *(indegree_slab_at(&indegree, commit)) = 0;
740 pptr = &commit_list_insert(commit, pptr)->next;
743 clear_indegree_slab(&indegree);
744 clear_prio_queue(&queue);
745 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
746 clear_author_date_slab(&author_date);
749 /* merge-base stuff */
751 /* Remember to update object flag allocation in object.h */
752 #define PARENT1 (1u<<16)
753 #define PARENT2 (1u<<17)
754 #define STALE (1u<<18)
755 #define RESULT (1u<<19)
757 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
759 static int queue_has_nonstale(struct prio_queue *queue)
761 int i;
762 for (i = 0; i < queue->nr; i++) {
763 struct commit *commit = queue->array[i].data;
764 if (!(commit->object.flags & STALE))
765 return 1;
767 return 0;
770 /* all input commits in one and twos[] must have been parsed! */
771 static struct commit_list *paint_down_to_common(struct commit *one, int n, struct commit **twos)
773 struct prio_queue queue = { compare_commits_by_commit_date };
774 struct commit_list *result = NULL;
775 int i;
777 one->object.flags |= PARENT1;
778 if (!n) {
779 commit_list_append(one, &result);
780 return result;
782 prio_queue_put(&queue, one);
784 for (i = 0; i < n; i++) {
785 twos[i]->object.flags |= PARENT2;
786 prio_queue_put(&queue, twos[i]);
789 while (queue_has_nonstale(&queue)) {
790 struct commit *commit = prio_queue_get(&queue);
791 struct commit_list *parents;
792 int flags;
794 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
795 if (flags == (PARENT1 | PARENT2)) {
796 if (!(commit->object.flags & RESULT)) {
797 commit->object.flags |= RESULT;
798 commit_list_insert_by_date(commit, &result);
800 /* Mark parents of a found merge stale */
801 flags |= STALE;
803 parents = commit->parents;
804 while (parents) {
805 struct commit *p = parents->item;
806 parents = parents->next;
807 if ((p->object.flags & flags) == flags)
808 continue;
809 if (parse_commit(p))
810 return NULL;
811 p->object.flags |= flags;
812 prio_queue_put(&queue, p);
816 clear_prio_queue(&queue);
817 return result;
820 static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
822 struct commit_list *list = NULL;
823 struct commit_list *result = NULL;
824 int i;
826 for (i = 0; i < n; i++) {
827 if (one == twos[i])
829 * We do not mark this even with RESULT so we do not
830 * have to clean it up.
832 return commit_list_insert(one, &result);
835 if (parse_commit(one))
836 return NULL;
837 for (i = 0; i < n; i++) {
838 if (parse_commit(twos[i]))
839 return NULL;
842 list = paint_down_to_common(one, n, twos);
844 while (list) {
845 struct commit *commit = pop_commit(&list);
846 if (!(commit->object.flags & STALE))
847 commit_list_insert_by_date(commit, &result);
849 return result;
852 struct commit_list *get_octopus_merge_bases(struct commit_list *in)
854 struct commit_list *i, *j, *k, *ret = NULL;
856 if (!in)
857 return ret;
859 commit_list_insert(in->item, &ret);
861 for (i = in->next; i; i = i->next) {
862 struct commit_list *new = NULL, *end = NULL;
864 for (j = ret; j; j = j->next) {
865 struct commit_list *bases;
866 bases = get_merge_bases(i->item, j->item);
867 if (!new)
868 new = bases;
869 else
870 end->next = bases;
871 for (k = bases; k; k = k->next)
872 end = k;
874 ret = new;
876 return ret;
879 static int remove_redundant(struct commit **array, int cnt)
882 * Some commit in the array may be an ancestor of
883 * another commit. Move such commit to the end of
884 * the array, and return the number of commits that
885 * are independent from each other.
887 struct commit **work;
888 unsigned char *redundant;
889 int *filled_index;
890 int i, j, filled;
892 work = xcalloc(cnt, sizeof(*work));
893 redundant = xcalloc(cnt, 1);
894 ALLOC_ARRAY(filled_index, cnt - 1);
896 for (i = 0; i < cnt; i++)
897 parse_commit(array[i]);
898 for (i = 0; i < cnt; i++) {
899 struct commit_list *common;
901 if (redundant[i])
902 continue;
903 for (j = filled = 0; j < cnt; j++) {
904 if (i == j || redundant[j])
905 continue;
906 filled_index[filled] = j;
907 work[filled++] = array[j];
909 common = paint_down_to_common(array[i], filled, work);
910 if (array[i]->object.flags & PARENT2)
911 redundant[i] = 1;
912 for (j = 0; j < filled; j++)
913 if (work[j]->object.flags & PARENT1)
914 redundant[filled_index[j]] = 1;
915 clear_commit_marks(array[i], all_flags);
916 clear_commit_marks_many(filled, work, all_flags);
917 free_commit_list(common);
920 /* Now collect the result */
921 COPY_ARRAY(work, array, cnt);
922 for (i = filled = 0; i < cnt; i++)
923 if (!redundant[i])
924 array[filled++] = work[i];
925 for (j = filled, i = 0; i < cnt; i++)
926 if (redundant[i])
927 array[j++] = work[i];
928 free(work);
929 free(redundant);
930 free(filled_index);
931 return filled;
934 static struct commit_list *get_merge_bases_many_0(struct commit *one,
935 int n,
936 struct commit **twos,
937 int cleanup)
939 struct commit_list *list;
940 struct commit **rslt;
941 struct commit_list *result;
942 int cnt, i;
944 result = merge_bases_many(one, n, twos);
945 for (i = 0; i < n; i++) {
946 if (one == twos[i])
947 return result;
949 if (!result || !result->next) {
950 if (cleanup) {
951 clear_commit_marks(one, all_flags);
952 clear_commit_marks_many(n, twos, all_flags);
954 return result;
957 /* There are more than one */
958 cnt = commit_list_count(result);
959 rslt = xcalloc(cnt, sizeof(*rslt));
960 for (list = result, i = 0; list; list = list->next)
961 rslt[i++] = list->item;
962 free_commit_list(result);
964 clear_commit_marks(one, all_flags);
965 clear_commit_marks_many(n, twos, all_flags);
967 cnt = remove_redundant(rslt, cnt);
968 result = NULL;
969 for (i = 0; i < cnt; i++)
970 commit_list_insert_by_date(rslt[i], &result);
971 free(rslt);
972 return result;
975 struct commit_list *get_merge_bases_many(struct commit *one,
976 int n,
977 struct commit **twos)
979 return get_merge_bases_many_0(one, n, twos, 1);
982 struct commit_list *get_merge_bases_many_dirty(struct commit *one,
983 int n,
984 struct commit **twos)
986 return get_merge_bases_many_0(one, n, twos, 0);
989 struct commit_list *get_merge_bases(struct commit *one, struct commit *two)
991 return get_merge_bases_many_0(one, 1, &two, 1);
995 * Is "commit" a descendant of one of the elements on the "with_commit" list?
997 int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
999 if (!with_commit)
1000 return 1;
1001 while (with_commit) {
1002 struct commit *other;
1004 other = with_commit->item;
1005 with_commit = with_commit->next;
1006 if (in_merge_bases(other, commit))
1007 return 1;
1009 return 0;
1013 * Is "commit" an ancestor of one of the "references"?
1015 int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference)
1017 struct commit_list *bases;
1018 int ret = 0, i;
1020 if (parse_commit(commit))
1021 return ret;
1022 for (i = 0; i < nr_reference; i++)
1023 if (parse_commit(reference[i]))
1024 return ret;
1026 bases = paint_down_to_common(commit, nr_reference, reference);
1027 if (commit->object.flags & PARENT2)
1028 ret = 1;
1029 clear_commit_marks(commit, all_flags);
1030 clear_commit_marks_many(nr_reference, reference, all_flags);
1031 free_commit_list(bases);
1032 return ret;
1036 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
1038 int in_merge_bases(struct commit *commit, struct commit *reference)
1040 return in_merge_bases_many(commit, 1, &reference);
1043 struct commit_list *reduce_heads(struct commit_list *heads)
1045 struct commit_list *p;
1046 struct commit_list *result = NULL, **tail = &result;
1047 struct commit **array;
1048 int num_head, i;
1050 if (!heads)
1051 return NULL;
1053 /* Uniquify */
1054 for (p = heads; p; p = p->next)
1055 p->item->object.flags &= ~STALE;
1056 for (p = heads, num_head = 0; p; p = p->next) {
1057 if (p->item->object.flags & STALE)
1058 continue;
1059 p->item->object.flags |= STALE;
1060 num_head++;
1062 array = xcalloc(num_head, sizeof(*array));
1063 for (p = heads, i = 0; p; p = p->next) {
1064 if (p->item->object.flags & STALE) {
1065 array[i++] = p->item;
1066 p->item->object.flags &= ~STALE;
1069 num_head = remove_redundant(array, num_head);
1070 for (i = 0; i < num_head; i++)
1071 tail = &commit_list_insert(array[i], tail)->next;
1072 free(array);
1073 return result;
1076 void reduce_heads_replace(struct commit_list **heads)
1078 struct commit_list *result = reduce_heads(*heads);
1079 free_commit_list(*heads);
1080 *heads = result;
1083 static const char gpg_sig_header[] = "gpgsig";
1084 static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
1086 static int do_sign_commit(struct strbuf *buf, const char *keyid)
1088 struct strbuf sig = STRBUF_INIT;
1089 int inspos, copypos;
1090 const char *eoh;
1092 /* find the end of the header */
1093 eoh = strstr(buf->buf, "\n\n");
1094 if (!eoh)
1095 inspos = buf->len;
1096 else
1097 inspos = eoh - buf->buf + 1;
1099 if (!keyid || !*keyid)
1100 keyid = get_signing_key();
1101 if (sign_buffer(buf, &sig, keyid)) {
1102 strbuf_release(&sig);
1103 return -1;
1106 for (copypos = 0; sig.buf[copypos]; ) {
1107 const char *bol = sig.buf + copypos;
1108 const char *eol = strchrnul(bol, '\n');
1109 int len = (eol - bol) + !!*eol;
1111 if (!copypos) {
1112 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
1113 inspos += gpg_sig_header_len;
1115 strbuf_insert(buf, inspos++, " ", 1);
1116 strbuf_insert(buf, inspos, bol, len);
1117 inspos += len;
1118 copypos += len;
1120 strbuf_release(&sig);
1121 return 0;
1124 int parse_signed_commit(const struct commit *commit,
1125 struct strbuf *payload, struct strbuf *signature)
1128 unsigned long size;
1129 const char *buffer = get_commit_buffer(commit, &size);
1130 int in_signature, saw_signature = -1;
1131 const char *line, *tail;
1133 line = buffer;
1134 tail = buffer + size;
1135 in_signature = 0;
1136 saw_signature = 0;
1137 while (line < tail) {
1138 const char *sig = NULL;
1139 const char *next = memchr(line, '\n', tail - line);
1141 next = next ? next + 1 : tail;
1142 if (in_signature && line[0] == ' ')
1143 sig = line + 1;
1144 else if (starts_with(line, gpg_sig_header) &&
1145 line[gpg_sig_header_len] == ' ')
1146 sig = line + gpg_sig_header_len + 1;
1147 if (sig) {
1148 strbuf_add(signature, sig, next - sig);
1149 saw_signature = 1;
1150 in_signature = 1;
1151 } else {
1152 if (*line == '\n')
1153 /* dump the whole remainder of the buffer */
1154 next = tail;
1155 strbuf_add(payload, line, next - line);
1156 in_signature = 0;
1158 line = next;
1160 unuse_commit_buffer(commit, buffer);
1161 return saw_signature;
1164 int remove_signature(struct strbuf *buf)
1166 const char *line = buf->buf;
1167 const char *tail = buf->buf + buf->len;
1168 int in_signature = 0;
1169 const char *sig_start = NULL;
1170 const char *sig_end = NULL;
1172 while (line < tail) {
1173 const char *next = memchr(line, '\n', tail - line);
1174 next = next ? next + 1 : tail;
1176 if (in_signature && line[0] == ' ')
1177 sig_end = next;
1178 else if (starts_with(line, gpg_sig_header) &&
1179 line[gpg_sig_header_len] == ' ') {
1180 sig_start = line;
1181 sig_end = next;
1182 in_signature = 1;
1183 } else {
1184 if (*line == '\n')
1185 /* dump the whole remainder of the buffer */
1186 next = tail;
1187 in_signature = 0;
1189 line = next;
1192 if (sig_start)
1193 strbuf_remove(buf, sig_start - buf->buf, sig_end - sig_start);
1195 return sig_start != NULL;
1198 static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
1200 struct merge_remote_desc *desc;
1201 struct commit_extra_header *mergetag;
1202 char *buf;
1203 unsigned long size, len;
1204 enum object_type type;
1206 desc = merge_remote_util(parent);
1207 if (!desc || !desc->obj)
1208 return;
1209 buf = read_sha1_file(desc->obj->oid.hash, &type, &size);
1210 if (!buf || type != OBJ_TAG)
1211 goto free_return;
1212 len = parse_signature(buf, size);
1213 if (size == len)
1214 goto free_return;
1216 * We could verify this signature and either omit the tag when
1217 * it does not validate, but the integrator may not have the
1218 * public key of the signer of the tag he is merging, while a
1219 * later auditor may have it while auditing, so let's not run
1220 * verify-signed-buffer here for now...
1222 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1223 * warn("warning: signed tag unverified.");
1225 mergetag = xcalloc(1, sizeof(*mergetag));
1226 mergetag->key = xstrdup("mergetag");
1227 mergetag->value = buf;
1228 mergetag->len = size;
1230 **tail = mergetag;
1231 *tail = &mergetag->next;
1232 return;
1234 free_return:
1235 free(buf);
1238 int check_commit_signature(const struct commit *commit, struct signature_check *sigc)
1240 struct strbuf payload = STRBUF_INIT;
1241 struct strbuf signature = STRBUF_INIT;
1242 int ret = 1;
1244 sigc->result = 'N';
1246 if (parse_signed_commit(commit, &payload, &signature) <= 0)
1247 goto out;
1248 ret = check_signature(payload.buf, payload.len, signature.buf,
1249 signature.len, sigc);
1251 out:
1252 strbuf_release(&payload);
1253 strbuf_release(&signature);
1255 return ret;
1260 void append_merge_tag_headers(struct commit_list *parents,
1261 struct commit_extra_header ***tail)
1263 while (parents) {
1264 struct commit *parent = parents->item;
1265 handle_signed_tag(parent, tail);
1266 parents = parents->next;
1270 static void add_extra_header(struct strbuf *buffer,
1271 struct commit_extra_header *extra)
1273 strbuf_addstr(buffer, extra->key);
1274 if (extra->len)
1275 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1276 else
1277 strbuf_addch(buffer, '\n');
1280 struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1281 const char **exclude)
1283 struct commit_extra_header *extra = NULL;
1284 unsigned long size;
1285 const char *buffer = get_commit_buffer(commit, &size);
1286 extra = read_commit_extra_header_lines(buffer, size, exclude);
1287 unuse_commit_buffer(commit, buffer);
1288 return extra;
1291 void for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
1293 struct commit_extra_header *extra, *to_free;
1295 to_free = read_commit_extra_headers(commit, NULL);
1296 for (extra = to_free; extra; extra = extra->next) {
1297 if (strcmp(extra->key, "mergetag"))
1298 continue; /* not a merge tag */
1299 fn(commit, extra, data);
1301 free_commit_extra_headers(to_free);
1304 static inline int standard_header_field(const char *field, size_t len)
1306 return ((len == 4 && !memcmp(field, "tree", 4)) ||
1307 (len == 6 && !memcmp(field, "parent", 6)) ||
1308 (len == 6 && !memcmp(field, "author", 6)) ||
1309 (len == 9 && !memcmp(field, "committer", 9)) ||
1310 (len == 8 && !memcmp(field, "encoding", 8)));
1313 static int excluded_header_field(const char *field, size_t len, const char **exclude)
1315 if (!exclude)
1316 return 0;
1318 while (*exclude) {
1319 size_t xlen = strlen(*exclude);
1320 if (len == xlen && !memcmp(field, *exclude, xlen))
1321 return 1;
1322 exclude++;
1324 return 0;
1327 static struct commit_extra_header *read_commit_extra_header_lines(
1328 const char *buffer, size_t size,
1329 const char **exclude)
1331 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1332 const char *line, *next, *eof, *eob;
1333 struct strbuf buf = STRBUF_INIT;
1335 for (line = buffer, eob = line + size;
1336 line < eob && *line != '\n';
1337 line = next) {
1338 next = memchr(line, '\n', eob - line);
1339 next = next ? next + 1 : eob;
1340 if (*line == ' ') {
1341 /* continuation */
1342 if (it)
1343 strbuf_add(&buf, line + 1, next - (line + 1));
1344 continue;
1346 if (it)
1347 it->value = strbuf_detach(&buf, &it->len);
1348 strbuf_reset(&buf);
1349 it = NULL;
1351 eof = memchr(line, ' ', next - line);
1352 if (!eof)
1353 eof = next;
1354 else if (standard_header_field(line, eof - line) ||
1355 excluded_header_field(line, eof - line, exclude))
1356 continue;
1358 it = xcalloc(1, sizeof(*it));
1359 it->key = xmemdupz(line, eof-line);
1360 *tail = it;
1361 tail = &it->next;
1362 if (eof + 1 < next)
1363 strbuf_add(&buf, eof + 1, next - (eof + 1));
1365 if (it)
1366 it->value = strbuf_detach(&buf, &it->len);
1367 return extra;
1370 void free_commit_extra_headers(struct commit_extra_header *extra)
1372 while (extra) {
1373 struct commit_extra_header *next = extra->next;
1374 free(extra->key);
1375 free(extra->value);
1376 free(extra);
1377 extra = next;
1381 int commit_tree(const char *msg, size_t msg_len,
1382 const unsigned char *tree,
1383 struct commit_list *parents, unsigned char *ret,
1384 const char *author, const char *sign_commit)
1386 struct commit_extra_header *extra = NULL, **tail = &extra;
1387 int result;
1389 append_merge_tag_headers(parents, &tail);
1390 result = commit_tree_extended(msg, msg_len, tree, parents, ret,
1391 author, sign_commit, extra);
1392 free_commit_extra_headers(extra);
1393 return result;
1396 static int find_invalid_utf8(const char *buf, int len)
1398 int offset = 0;
1399 static const unsigned int max_codepoint[] = {
1400 0x7f, 0x7ff, 0xffff, 0x10ffff
1403 while (len) {
1404 unsigned char c = *buf++;
1405 int bytes, bad_offset;
1406 unsigned int codepoint;
1407 unsigned int min_val, max_val;
1409 len--;
1410 offset++;
1412 /* Simple US-ASCII? No worries. */
1413 if (c < 0x80)
1414 continue;
1416 bad_offset = offset-1;
1419 * Count how many more high bits set: that's how
1420 * many more bytes this sequence should have.
1422 bytes = 0;
1423 while (c & 0x40) {
1424 c <<= 1;
1425 bytes++;
1429 * Must be between 1 and 3 more bytes. Longer sequences result in
1430 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1432 if (bytes < 1 || 3 < bytes)
1433 return bad_offset;
1435 /* Do we *have* that many bytes? */
1436 if (len < bytes)
1437 return bad_offset;
1440 * Place the encoded bits at the bottom of the value and compute the
1441 * valid range.
1443 codepoint = (c & 0x7f) >> bytes;
1444 min_val = max_codepoint[bytes-1] + 1;
1445 max_val = max_codepoint[bytes];
1447 offset += bytes;
1448 len -= bytes;
1450 /* And verify that they are good continuation bytes */
1451 do {
1452 codepoint <<= 6;
1453 codepoint |= *buf & 0x3f;
1454 if ((*buf++ & 0xc0) != 0x80)
1455 return bad_offset;
1456 } while (--bytes);
1458 /* Reject codepoints that are out of range for the sequence length. */
1459 if (codepoint < min_val || codepoint > max_val)
1460 return bad_offset;
1461 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1462 if ((codepoint & 0x1ff800) == 0xd800)
1463 return bad_offset;
1464 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1465 if ((codepoint & 0xfffe) == 0xfffe)
1466 return bad_offset;
1467 /* So are anything in the range U+FDD0..U+FDEF. */
1468 if (codepoint >= 0xfdd0 && codepoint <= 0xfdef)
1469 return bad_offset;
1471 return -1;
1475 * This verifies that the buffer is in proper utf8 format.
1477 * If it isn't, it assumes any non-utf8 characters are Latin1,
1478 * and does the conversion.
1480 static int verify_utf8(struct strbuf *buf)
1482 int ok = 1;
1483 long pos = 0;
1485 for (;;) {
1486 int bad;
1487 unsigned char c;
1488 unsigned char replace[2];
1490 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1491 if (bad < 0)
1492 return ok;
1493 pos += bad;
1494 ok = 0;
1495 c = buf->buf[pos];
1496 strbuf_remove(buf, pos, 1);
1498 /* We know 'c' must be in the range 128-255 */
1499 replace[0] = 0xc0 + (c >> 6);
1500 replace[1] = 0x80 + (c & 0x3f);
1501 strbuf_insert(buf, pos, replace, 2);
1502 pos += 2;
1506 static const char commit_utf8_warn[] =
1507 N_("Warning: commit message did not conform to UTF-8.\n"
1508 "You may want to amend it after fixing the message, or set the config\n"
1509 "variable i18n.commitencoding to the encoding your project uses.\n");
1511 int commit_tree_extended(const char *msg, size_t msg_len,
1512 const unsigned char *tree,
1513 struct commit_list *parents, unsigned char *ret,
1514 const char *author, const char *sign_commit,
1515 struct commit_extra_header *extra)
1517 int result;
1518 int encoding_is_utf8;
1519 struct strbuf buffer;
1521 assert_sha1_type(tree, OBJ_TREE);
1523 if (memchr(msg, '\0', msg_len))
1524 return error("a NUL byte in commit log message not allowed.");
1526 /* Not having i18n.commitencoding is the same as having utf-8 */
1527 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1529 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1530 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
1533 * NOTE! This ordering means that the same exact tree merged with a
1534 * different order of parents will be a _different_ changeset even
1535 * if everything else stays the same.
1537 while (parents) {
1538 struct commit *parent = pop_commit(&parents);
1539 strbuf_addf(&buffer, "parent %s\n",
1540 oid_to_hex(&parent->object.oid));
1543 /* Person/date information */
1544 if (!author)
1545 author = git_author_info(IDENT_STRICT);
1546 strbuf_addf(&buffer, "author %s\n", author);
1547 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
1548 if (!encoding_is_utf8)
1549 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
1551 while (extra) {
1552 add_extra_header(&buffer, extra);
1553 extra = extra->next;
1555 strbuf_addch(&buffer, '\n');
1557 /* And add the comment */
1558 strbuf_add(&buffer, msg, msg_len);
1560 /* And check the encoding */
1561 if (encoding_is_utf8 && !verify_utf8(&buffer))
1562 fprintf(stderr, _(commit_utf8_warn));
1564 if (sign_commit && do_sign_commit(&buffer, sign_commit)) {
1565 result = -1;
1566 goto out;
1569 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
1570 out:
1571 strbuf_release(&buffer);
1572 return result;
1575 void set_merge_remote_desc(struct commit *commit,
1576 const char *name, struct object *obj)
1578 struct merge_remote_desc *desc;
1579 FLEX_ALLOC_STR(desc, name, name);
1580 desc->obj = obj;
1581 commit->util = desc;
1584 struct commit *get_merge_parent(const char *name)
1586 struct object *obj;
1587 struct commit *commit;
1588 struct object_id oid;
1589 if (get_oid(name, &oid))
1590 return NULL;
1591 obj = parse_object(&oid);
1592 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1593 if (commit && !commit->util)
1594 set_merge_remote_desc(commit, name, obj);
1595 return commit;
1599 * Append a commit to the end of the commit_list.
1601 * next starts by pointing to the variable that holds the head of an
1602 * empty commit_list, and is updated to point to the "next" field of
1603 * the last item on the list as new commits are appended.
1605 * Usage example:
1607 * struct commit_list *list;
1608 * struct commit_list **next = &list;
1610 * next = commit_list_append(c1, next);
1611 * next = commit_list_append(c2, next);
1612 * assert(commit_list_count(list) == 2);
1613 * return list;
1615 struct commit_list **commit_list_append(struct commit *commit,
1616 struct commit_list **next)
1618 struct commit_list *new = xmalloc(sizeof(struct commit_list));
1619 new->item = commit;
1620 *next = new;
1621 new->next = NULL;
1622 return &new->next;
1625 const char *find_commit_header(const char *msg, const char *key, size_t *out_len)
1627 int key_len = strlen(key);
1628 const char *line = msg;
1630 while (line) {
1631 const char *eol = strchrnul(line, '\n');
1633 if (line == eol)
1634 return NULL;
1636 if (eol - line > key_len &&
1637 !strncmp(line, key, key_len) &&
1638 line[key_len] == ' ') {
1639 *out_len = eol - line - key_len - 1;
1640 return line + key_len + 1;
1642 line = *eol ? eol + 1 : NULL;
1644 return NULL;
1648 * Inspect the given string and determine the true "end" of the log message, in
1649 * order to find where to put a new Signed-off-by: line. Ignored are
1650 * trailing comment lines and blank lines. To support "git commit -s
1651 * --amend" on an existing commit, we also ignore "Conflicts:". To
1652 * support "git commit -v", we truncate at cut lines.
1654 * Returns the number of bytes from the tail to ignore, to be fed as
1655 * the second parameter to append_signoff().
1657 int ignore_non_trailer(const char *buf, size_t len)
1659 int boc = 0;
1660 int bol = 0;
1661 int in_old_conflicts_block = 0;
1662 size_t cutoff = wt_status_locate_end(buf, len);
1664 while (bol < cutoff) {
1665 const char *next_line = memchr(buf + bol, '\n', len - bol);
1667 if (!next_line)
1668 next_line = buf + len;
1669 else
1670 next_line++;
1672 if (buf[bol] == comment_line_char || buf[bol] == '\n') {
1673 /* is this the first of the run of comments? */
1674 if (!boc)
1675 boc = bol;
1676 /* otherwise, it is just continuing */
1677 } else if (starts_with(buf + bol, "Conflicts:\n")) {
1678 in_old_conflicts_block = 1;
1679 if (!boc)
1680 boc = bol;
1681 } else if (in_old_conflicts_block && buf[bol] == '\t') {
1682 ; /* a pathname in the conflicts block */
1683 } else if (boc) {
1684 /* the previous was not trailing comment */
1685 boc = 0;
1686 in_old_conflicts_block = 0;
1688 bol = next_line - buf;
1690 return boc ? len - boc : len - cutoff;