]> git.ipfire.org Git - thirdparty/git.git/blame - graph.c
is_ntfs_dotgit(): only verify the leading segment
[thirdparty/git.git] / graph.c
CommitLineData
c12172d2 1#include "cache.h"
b2141fc1 2#include "config.h"
c12172d2 3#include "commit.h"
427fc5b8 4#include "color.h"
c12172d2 5#include "graph.h"
c12172d2 6#include "revision.h"
73c727d6 7#include "argv-array.h"
c12172d2 8
064bfbde
NS
9/* Internal API */
10
064bfbde
NS
11/*
12 * Output a padding line in the graph.
13 * This is similar to graph_next_line(). However, it is guaranteed to
14 * never print the current commit line. Instead, if the commit line is
15 * next, it will simply output a line of vertical padding, extending the
16 * branch lines downwards, but leaving them otherwise unchanged.
17 */
18static void graph_padding_line(struct git_graph *graph, struct strbuf *sb);
19
20/*
c61008fd
JS
21 * Print a strbuf. If the graph is non-NULL, all lines but the first will be
22 * prefixed with the graph output.
064bfbde
NS
23 *
24 * If the strbuf ends with a newline, the output will end after this
25 * newline. A new graph line will not be printed after the final newline.
26 * If the strbuf is empty, no output will be printed.
27 *
3ea3c215 28 * Since the first line will not include the graph output, the caller is
064bfbde
NS
29 * responsible for printing this line's graph (perhaps via
30 * graph_show_commit() or graph_show_oneline()) before calling
31 * graph_show_strbuf().
660e113c
JK
32 *
33 * Note that unlike some other graph display functions, you must pass the file
34 * handle directly. It is assumed that this is the same file handle as the
35 * file specified by the graph diff options. This is necessary so that
36 * graph_show_strbuf can be called even with a NULL graph.
064bfbde 37 */
660e113c
JK
38static void graph_show_strbuf(struct git_graph *graph,
39 FILE *file,
40 struct strbuf const *sb);
064bfbde 41
c12172d2
AS
42/*
43 * TODO:
c12172d2
AS
44 * - Limit the number of columns, similar to the way gitk does.
45 * If we reach more than a specified number of columns, omit
46 * sections of some columns.
c12172d2
AS
47 */
48
49struct column {
50 /*
51 * The parent commit of this column.
52 */
53 struct commit *commit;
54 /*
427fc5b8
AC
55 * The color to (optionally) print this column in. This is an
56 * index into column_colors.
c12172d2 57 */
427fc5b8 58 unsigned short color;
c12172d2
AS
59};
60
61enum graph_state {
62 GRAPH_PADDING,
63 GRAPH_SKIP,
64 GRAPH_PRE_COMMIT,
65 GRAPH_COMMIT,
66 GRAPH_POST_MERGE,
67 GRAPH_COLLAPSING
68};
69
660e113c
JK
70static void graph_show_line_prefix(const struct diff_options *diffopt)
71{
72 if (!diffopt || !diffopt->line_prefix)
73 return;
74
75 fwrite(diffopt->line_prefix,
76 sizeof(char),
77 diffopt->line_prefix_length,
78 diffopt->file);
79}
80
1e3d4119
JH
81static const char **column_colors;
82static unsigned short column_colors_max;
83
73c727d6
NTND
84static void parse_graph_colors_config(struct argv_array *colors, const char *string)
85{
86 const char *end, *start;
87
88 start = string;
89 end = string + strlen(string);
90 while (start < end) {
91 const char *comma = strchrnul(start, ',');
92 char color[COLOR_MAXLEN];
93
94 if (!color_parse_mem(start, comma - start, color))
95 argv_array_push(colors, color);
96 else
97 warning(_("ignore invalid color '%.*s' in log.graphColors"),
98 (int)(comma - start), start);
99 start = comma + 1;
100 }
101 argv_array_push(colors, GIT_COLOR_RESET);
102}
103
ac751a0b 104void graph_set_column_colors(const char **colors, unsigned short colors_max)
1e3d4119
JH
105{
106 column_colors = colors;
107 column_colors_max = colors_max;
108}
109
110static const char *column_get_color_code(unsigned short color)
427fc5b8 111{
1e3d4119 112 return column_colors[color];
427fc5b8
AC
113}
114
115static void strbuf_write_column(struct strbuf *sb, const struct column *c,
116 char col_char)
117{
1e3d4119
JH
118 if (c->color < column_colors_max)
119 strbuf_addstr(sb, column_get_color_code(c->color));
427fc5b8 120 strbuf_addch(sb, col_char);
1e3d4119
JH
121 if (c->color < column_colors_max)
122 strbuf_addstr(sb, column_get_color_code(column_colors_max));
427fc5b8
AC
123}
124
c12172d2
AS
125struct git_graph {
126 /*
127 * The commit currently being processed
128 */
129 struct commit *commit;
7528f27d
AS
130 /* The rev-info used for the current traversal */
131 struct rev_info *revs;
c12172d2 132 /*
37a75abc
AS
133 * The number of interesting parents that this commit has.
134 *
135 * Note that this is not the same as the actual number of parents.
136 * This count excludes parents that won't be printed in the graph
137 * output, as determined by graph_is_interesting().
c12172d2
AS
138 */
139 int num_parents;
0724cb86
AS
140 /*
141 * The width of the graph output for this commit.
142 * All rows for this commit are padded to this width, so that
143 * messages printed after the graph output are aligned.
144 */
145 int width;
c12172d2
AS
146 /*
147 * The next expansion row to print
148 * when state is GRAPH_PRE_COMMIT
149 */
150 int expansion_row;
151 /*
152 * The current output state.
153 * This tells us what kind of line graph_next_line() should output.
154 */
155 enum graph_state state;
3395908e
AS
156 /*
157 * The output state for the previous line of output.
158 * This is primarily used to determine how the first merge line
159 * should appear, based on the last line of the previous commit.
160 */
161 enum graph_state prev_state;
162 /*
163 * The index of the column that refers to this commit.
164 *
165 * If none of the incoming columns refer to this commit,
166 * this will be equal to num_columns.
167 */
168 int commit_index;
169 /*
170 * The commit_index for the previously displayed commit.
171 *
172 * This is used to determine how the first line of a merge
173 * graph output should appear, based on the last line of the
174 * previous commit.
175 */
176 int prev_commit_index;
c12172d2
AS
177 /*
178 * The maximum number of columns that can be stored in the columns
179 * and new_columns arrays. This is also half the number of entries
180 * that can be stored in the mapping and new_mapping arrays.
181 */
182 int column_capacity;
183 /*
184 * The number of columns (also called "branch lines" in some places)
185 */
186 int num_columns;
187 /*
188 * The number of columns in the new_columns array
189 */
190 int num_new_columns;
191 /*
192 * The number of entries in the mapping array
193 */
194 int mapping_size;
195 /*
196 * The column state before we output the current commit.
197 */
198 struct column *columns;
199 /*
200 * The new column state after we output the current commit.
201 * Only valid when state is GRAPH_COLLAPSING.
202 */
203 struct column *new_columns;
204 /*
205 * An array that tracks the current state of each
206 * character in the output line during state GRAPH_COLLAPSING.
207 * Each entry is -1 if this character is empty, or a non-negative
208 * integer if the character contains a branch line. The value of
209 * the integer indicates the target position for this branch line.
210 * (I.e., this array maps the current column positions to their
211 * desired positions.)
212 *
213 * The maximum capacity of this array is always
214 * sizeof(int) * 2 * column_capacity.
215 */
216 int *mapping;
217 /*
218 * A temporary array for computing the next mapping state
219 * while we are outputting a mapping line. This is stored as part
220 * of the git_graph simply so we don't have to allocate a new
221 * temporary array each time we have to output a collapsing line.
222 */
223 int *new_mapping;
427fc5b8
AC
224 /*
225 * The current default column color being used. This is
226 * stored as an index into the array column_colors.
227 */
228 unsigned short default_column_color;
c12172d2
AS
229};
230
b5a4de9d
BY
231static struct strbuf *diff_output_prefix_callback(struct diff_options *opt, void *data)
232{
233 struct git_graph *graph = data;
234 static struct strbuf msgbuf = STRBUF_INIT;
235
5e71a84a 236 assert(opt);
b5a4de9d
BY
237
238 strbuf_reset(&msgbuf);
660e113c
JK
239 if (opt->line_prefix)
240 strbuf_add(&msgbuf, opt->line_prefix,
241 opt->line_prefix_length);
242 if (graph)
243 graph_padding_line(graph, &msgbuf);
b5a4de9d
BY
244 return &msgbuf;
245}
246
660e113c
JK
247static const struct diff_options *default_diffopt;
248
249void graph_setup_line_prefix(struct diff_options *diffopt)
250{
251 default_diffopt = diffopt;
252
253 /* setup an output prefix callback if necessary */
254 if (diffopt && !diffopt->output_prefix)
255 diffopt->output_prefix = diff_output_prefix_callback;
256}
257
258
7528f27d 259struct git_graph *graph_init(struct rev_info *opt)
c12172d2
AS
260{
261 struct git_graph *graph = xmalloc(sizeof(struct git_graph));
1e3d4119 262
73c727d6
NTND
263 if (!column_colors) {
264 char *string;
265 if (git_config_get_string("log.graphcolors", &string)) {
266 /* not configured -- use default */
267 graph_set_column_colors(column_colors_ansi,
268 column_colors_ansi_max);
269 } else {
270 static struct argv_array custom_colors = ARGV_ARRAY_INIT;
271 argv_array_clear(&custom_colors);
272 parse_graph_colors_config(&custom_colors, string);
273 free(string);
274 /* graph_set_column_colors takes a max-index, not a count */
275 graph_set_column_colors(custom_colors.argv,
276 custom_colors.argc - 1);
277 }
278 }
1e3d4119 279
c12172d2 280 graph->commit = NULL;
7528f27d 281 graph->revs = opt;
c12172d2
AS
282 graph->num_parents = 0;
283 graph->expansion_row = 0;
284 graph->state = GRAPH_PADDING;
3395908e
AS
285 graph->prev_state = GRAPH_PADDING;
286 graph->commit_index = 0;
287 graph->prev_commit_index = 0;
c12172d2
AS
288 graph->num_columns = 0;
289 graph->num_new_columns = 0;
290 graph->mapping_size = 0;
91e50b2c
AS
291 /*
292 * Start the column color at the maximum value, since we'll
293 * always increment it for the first commit we output.
294 * This way we start at 0 for the first commit.
295 */
1e3d4119 296 graph->default_column_color = column_colors_max - 1;
c12172d2
AS
297
298 /*
299 * Allocate a reasonably large default number of columns
300 * We'll automatically grow columns later if we need more room.
301 */
302 graph->column_capacity = 30;
b32fa95f
JK
303 ALLOC_ARRAY(graph->columns, graph->column_capacity);
304 ALLOC_ARRAY(graph->new_columns, graph->column_capacity);
305 ALLOC_ARRAY(graph->mapping, 2 * graph->column_capacity);
306 ALLOC_ARRAY(graph->new_mapping, 2 * graph->column_capacity);
c12172d2 307
b5a4de9d
BY
308 /*
309 * The diff output prefix callback, with this we can make
310 * all the diff output to align with the graph lines.
311 */
312 opt->diffopt.output_prefix = diff_output_prefix_callback;
313 opt->diffopt.output_prefix_data = graph;
314
c12172d2
AS
315 return graph;
316}
317
3395908e
AS
318static void graph_update_state(struct git_graph *graph, enum graph_state s)
319{
320 graph->prev_state = graph->state;
321 graph->state = s;
322}
323
c12172d2
AS
324static void graph_ensure_capacity(struct git_graph *graph, int num_columns)
325{
326 if (graph->column_capacity >= num_columns)
327 return;
328
329 do {
330 graph->column_capacity *= 2;
331 } while (graph->column_capacity < num_columns);
332
2756ca43
RS
333 REALLOC_ARRAY(graph->columns, graph->column_capacity);
334 REALLOC_ARRAY(graph->new_columns, graph->column_capacity);
335 REALLOC_ARRAY(graph->mapping, graph->column_capacity * 2);
336 REALLOC_ARRAY(graph->new_mapping, graph->column_capacity * 2);
c12172d2
AS
337}
338
37a75abc
AS
339/*
340 * Returns 1 if the commit will be printed in the graph output,
341 * and 0 otherwise.
342 */
3c68d67b 343static int graph_is_interesting(struct git_graph *graph, struct commit *commit)
37a75abc 344{
3c68d67b
AS
345 /*
346 * If revs->boundary is set, commits whose children have
347 * been shown are always interesting, even if they have the
348 * UNINTERESTING or TREESAME flags set.
3c68d67b
AS
349 */
350 if (graph->revs && graph->revs->boundary) {
4603ec0f 351 if (commit->object.flags & CHILD_SHOWN)
3c68d67b
AS
352 return 1;
353 }
354
37a75abc 355 /*
beb5af43
AS
356 * Otherwise, use get_commit_action() to see if this commit is
357 * interesting
37a75abc 358 */
beb5af43 359 return get_commit_action(graph->revs, commit) == commit_show;
37a75abc
AS
360}
361
a0ebe573
AS
362static struct commit_list *next_interesting_parent(struct git_graph *graph,
363 struct commit_list *orig)
364{
365 struct commit_list *list;
366
367 /*
368 * If revs->first_parent_only is set, only the first
369 * parent is interesting. None of the others are.
370 */
371 if (graph->revs->first_parent_only)
372 return NULL;
373
374 /*
375 * Return the next interesting commit after orig
376 */
377 for (list = orig->next; list; list = list->next) {
378 if (graph_is_interesting(graph, list->item))
379 return list;
380 }
381
382 return NULL;
383}
384
385static struct commit_list *first_interesting_parent(struct git_graph *graph)
386{
387 struct commit_list *parents = graph->commit->parents;
388
389 /*
390 * If this commit has no parents, ignore it
391 */
392 if (!parents)
393 return NULL;
394
395 /*
396 * If the first parent is interesting, return it
397 */
398 if (graph_is_interesting(graph, parents->item))
399 return parents;
400
401 /*
402 * Otherwise, call next_interesting_parent() to get
403 * the next interesting parent
404 */
405 return next_interesting_parent(graph, parents);
406}
407
427fc5b8
AC
408static unsigned short graph_get_current_column_color(const struct git_graph *graph)
409{
daa0c3d9 410 if (!want_color(graph->revs->diffopt.use_color))
1e3d4119 411 return column_colors_max;
427fc5b8
AC
412 return graph->default_column_color;
413}
414
415/*
416 * Update the graph's default column color.
417 */
418static void graph_increment_column_color(struct git_graph *graph)
419{
420 graph->default_column_color = (graph->default_column_color + 1) %
1e3d4119 421 column_colors_max;
427fc5b8
AC
422}
423
424static unsigned short graph_find_commit_color(const struct git_graph *graph,
425 const struct commit *commit)
426{
427 int i;
428 for (i = 0; i < graph->num_columns; i++) {
429 if (graph->columns[i].commit == commit)
430 return graph->columns[i].color;
431 }
432 return graph_get_current_column_color(graph);
433}
434
c12172d2
AS
435static void graph_insert_into_new_columns(struct git_graph *graph,
436 struct commit *commit,
437 int *mapping_index)
438{
439 int i;
440
c12172d2
AS
441 /*
442 * If the commit is already in the new_columns list, we don't need to
443 * add it. Just update the mapping correctly.
444 */
445 for (i = 0; i < graph->num_new_columns; i++) {
446 if (graph->new_columns[i].commit == commit) {
447 graph->mapping[*mapping_index] = i;
448 *mapping_index += 2;
449 return;
450 }
451 }
452
453 /*
454 * This commit isn't already in new_columns. Add it.
455 */
456 graph->new_columns[graph->num_new_columns].commit = commit;
427fc5b8 457 graph->new_columns[graph->num_new_columns].color = graph_find_commit_color(graph, commit);
c12172d2
AS
458 graph->mapping[*mapping_index] = graph->num_new_columns;
459 *mapping_index += 2;
460 graph->num_new_columns++;
461}
462
0724cb86
AS
463static void graph_update_width(struct git_graph *graph,
464 int is_commit_in_existing_columns)
465{
466 /*
467 * Compute the width needed to display the graph for this commit.
468 * This is the maximum width needed for any row. All other rows
469 * will be padded to this width.
470 *
471 * Compute the number of columns in the widest row:
472 * Count each existing column (graph->num_columns), and each new
473 * column added by this commit.
474 */
475 int max_cols = graph->num_columns + graph->num_parents;
476
477 /*
37a75abc
AS
478 * Even if the current commit has no parents to be printed, it
479 * still takes up a column for itself.
0724cb86
AS
480 */
481 if (graph->num_parents < 1)
482 max_cols++;
483
484 /*
22e5e58a 485 * We added a column for the current commit as part of
0724cb86
AS
486 * graph->num_parents. If the current commit was already in
487 * graph->columns, then we have double counted it.
488 */
489 if (is_commit_in_existing_columns)
490 max_cols--;
491
492 /*
493 * Each column takes up 2 spaces
494 */
495 graph->width = max_cols * 2;
496}
497
c12172d2
AS
498static void graph_update_columns(struct git_graph *graph)
499{
500 struct commit_list *parent;
c12172d2
AS
501 int max_new_columns;
502 int mapping_idx;
0724cb86 503 int i, seen_this, is_commit_in_columns;
c12172d2
AS
504
505 /*
506 * Swap graph->columns with graph->new_columns
507 * graph->columns contains the state for the previous commit,
508 * and new_columns now contains the state for our commit.
509 *
510 * We'll re-use the old columns array as storage to compute the new
511 * columns list for the commit after this one.
512 */
9e2edd66 513 SWAP(graph->columns, graph->new_columns);
c12172d2 514 graph->num_columns = graph->num_new_columns;
c12172d2
AS
515 graph->num_new_columns = 0;
516
517 /*
518 * Now update new_columns and mapping with the information for the
519 * commit after this one.
520 *
521 * First, make sure we have enough room. At most, there will
522 * be graph->num_columns + graph->num_parents columns for the next
523 * commit.
524 */
525 max_new_columns = graph->num_columns + graph->num_parents;
526 graph_ensure_capacity(graph, max_new_columns);
527
528 /*
529 * Clear out graph->mapping
530 */
531 graph->mapping_size = 2 * max_new_columns;
532 for (i = 0; i < graph->mapping_size; i++)
533 graph->mapping[i] = -1;
534
535 /*
536 * Populate graph->new_columns and graph->mapping
537 *
538 * Some of the parents of this commit may already be in
539 * graph->columns. If so, graph->new_columns should only contain a
540 * single entry for each such commit. graph->mapping should
541 * contain information about where each current branch line is
542 * supposed to end up after the collapsing is performed.
543 */
544 seen_this = 0;
545 mapping_idx = 0;
0724cb86 546 is_commit_in_columns = 1;
c12172d2
AS
547 for (i = 0; i <= graph->num_columns; i++) {
548 struct commit *col_commit;
549 if (i == graph->num_columns) {
550 if (seen_this)
551 break;
0724cb86 552 is_commit_in_columns = 0;
c12172d2
AS
553 col_commit = graph->commit;
554 } else {
555 col_commit = graph->columns[i].commit;
556 }
557
558 if (col_commit == graph->commit) {
37a75abc 559 int old_mapping_idx = mapping_idx;
c12172d2 560 seen_this = 1;
3395908e 561 graph->commit_index = i;
a0ebe573 562 for (parent = first_interesting_parent(graph);
c12172d2 563 parent;
a0ebe573 564 parent = next_interesting_parent(graph, parent)) {
427fc5b8 565 /*
91e50b2c
AS
566 * If this is a merge, or the start of a new
567 * childless column, increment the current
427fc5b8
AC
568 * color.
569 */
91e50b2c
AS
570 if (graph->num_parents > 1 ||
571 !is_commit_in_columns) {
427fc5b8 572 graph_increment_column_color(graph);
91e50b2c 573 }
c12172d2
AS
574 graph_insert_into_new_columns(graph,
575 parent->item,
576 &mapping_idx);
577 }
37a75abc
AS
578 /*
579 * We always need to increment mapping_idx by at
580 * least 2, even if it has no interesting parents.
581 * The current commit always takes up at least 2
582 * spaces.
583 */
584 if (mapping_idx == old_mapping_idx)
585 mapping_idx += 2;
c12172d2
AS
586 } else {
587 graph_insert_into_new_columns(graph, col_commit,
588 &mapping_idx);
589 }
590 }
591
592 /*
593 * Shrink mapping_size to be the minimum necessary
594 */
595 while (graph->mapping_size > 1 &&
596 graph->mapping[graph->mapping_size - 1] < 0)
597 graph->mapping_size--;
0724cb86
AS
598
599 /*
600 * Compute graph->width for this commit
601 */
602 graph_update_width(graph, is_commit_in_columns);
c12172d2
AS
603}
604
605void graph_update(struct git_graph *graph, struct commit *commit)
606{
607 struct commit_list *parent;
608
609 /*
610 * Set the new commit
611 */
612 graph->commit = commit;
613
614 /*
37a75abc 615 * Count how many interesting parents this commit has
c12172d2
AS
616 */
617 graph->num_parents = 0;
a0ebe573
AS
618 for (parent = first_interesting_parent(graph);
619 parent;
620 parent = next_interesting_parent(graph, parent))
621 {
622 graph->num_parents++;
37a75abc 623 }
c12172d2 624
3395908e
AS
625 /*
626 * Store the old commit_index in prev_commit_index.
627 * graph_update_columns() will update graph->commit_index for this
628 * commit.
629 */
630 graph->prev_commit_index = graph->commit_index;
631
c12172d2
AS
632 /*
633 * Call graph_update_columns() to update
634 * columns, new_columns, and mapping.
635 */
636 graph_update_columns(graph);
637
638 graph->expansion_row = 0;
639
640 /*
641 * Update graph->state.
3395908e
AS
642 * Note that we don't call graph_update_state() here, since
643 * we don't want to update graph->prev_state. No line for
644 * graph->state was ever printed.
c12172d2
AS
645 *
646 * If the previous commit didn't get to the GRAPH_PADDING state,
647 * it never finished its output. Goto GRAPH_SKIP, to print out
648 * a line to indicate that portion of the graph is missing.
649 *
f1979d6b
AS
650 * If there are 3 or more parents, we may need to print extra rows
651 * before the commit, to expand the branch lines around it and make
652 * room for it. We need to do this only if there is a branch row
653 * (or more) to the right of this commit.
c12172d2
AS
654 *
655 * If there are less than 3 parents, we can immediately print the
656 * commit line.
657 */
658 if (graph->state != GRAPH_PADDING)
659 graph->state = GRAPH_SKIP;
f1979d6b
AS
660 else if (graph->num_parents >= 3 &&
661 graph->commit_index < (graph->num_columns - 1))
c12172d2
AS
662 graph->state = GRAPH_PRE_COMMIT;
663 else
664 graph->state = GRAPH_COMMIT;
665}
666
667static int graph_is_mapping_correct(struct git_graph *graph)
668{
669 int i;
670
671 /*
672 * The mapping is up to date if each entry is at its target,
673 * or is 1 greater than its target.
674 * (If it is 1 greater than the target, '/' will be printed, so it
675 * will look correct on the next row.)
676 */
677 for (i = 0; i < graph->mapping_size; i++) {
678 int target = graph->mapping[i];
679 if (target < 0)
680 continue;
681 if (target == (i / 2))
682 continue;
683 return 0;
684 }
685
686 return 1;
687}
688
427fc5b8
AC
689static void graph_pad_horizontally(struct git_graph *graph, struct strbuf *sb,
690 int chars_written)
c12172d2
AS
691{
692 /*
693 * Add additional spaces to the end of the strbuf, so that all
694 * lines for a particular commit have the same width.
695 *
696 * This way, fields printed to the right of the graph will remain
697 * aligned for the entire commit.
c12172d2 698 */
0724cb86 699 int extra;
427fc5b8 700 if (chars_written >= graph->width)
c12172d2
AS
701 return;
702
427fc5b8 703 extra = graph->width - chars_written;
c12172d2
AS
704 strbuf_addf(sb, "%*s", (int) extra, "");
705}
706
707static void graph_output_padding_line(struct git_graph *graph,
708 struct strbuf *sb)
709{
710 int i;
711
712 /*
713 * We could conceivable be called with a NULL commit
714 * if our caller has a bug, and invokes graph_next_line()
715 * immediately after graph_init(), without first calling
716 * graph_update(). Return without outputting anything in this
717 * case.
718 */
719 if (!graph->commit)
720 return;
721
722 /*
723 * Output a padding row, that leaves all branch lines unchanged
724 */
725 for (i = 0; i < graph->num_new_columns; i++) {
427fc5b8
AC
726 strbuf_write_column(sb, &graph->new_columns[i], '|');
727 strbuf_addch(sb, ' ');
c12172d2
AS
728 }
729
427fc5b8 730 graph_pad_horizontally(graph, sb, graph->num_new_columns * 2);
c12172d2
AS
731}
732
3ad87c80
JK
733
734int graph_width(struct git_graph *graph)
735{
736 return graph->width;
737}
738
739
c12172d2
AS
740static void graph_output_skip_line(struct git_graph *graph, struct strbuf *sb)
741{
742 /*
743 * Output an ellipsis to indicate that a portion
744 * of the graph is missing.
745 */
746 strbuf_addstr(sb, "...");
427fc5b8 747 graph_pad_horizontally(graph, sb, 3);
c12172d2 748
f1979d6b
AS
749 if (graph->num_parents >= 3 &&
750 graph->commit_index < (graph->num_columns - 1))
3395908e 751 graph_update_state(graph, GRAPH_PRE_COMMIT);
c12172d2 752 else
3395908e 753 graph_update_state(graph, GRAPH_COMMIT);
c12172d2
AS
754}
755
756static void graph_output_pre_commit_line(struct git_graph *graph,
757 struct strbuf *sb)
758{
759 int num_expansion_rows;
760 int i, seen_this;
427fc5b8 761 int chars_written;
c12172d2
AS
762
763 /*
764 * This function formats a row that increases the space around a commit
765 * with multiple parents, to make room for it. It should only be
766 * called when there are 3 or more parents.
767 *
768 * We need 2 extra rows for every parent over 2.
769 */
770 assert(graph->num_parents >= 3);
771 num_expansion_rows = (graph->num_parents - 2) * 2;
772
773 /*
774 * graph->expansion_row tracks the current expansion row we are on.
775 * It should be in the range [0, num_expansion_rows - 1]
776 */
777 assert(0 <= graph->expansion_row &&
778 graph->expansion_row < num_expansion_rows);
779
780 /*
781 * Output the row
782 */
783 seen_this = 0;
427fc5b8 784 chars_written = 0;
c12172d2
AS
785 for (i = 0; i < graph->num_columns; i++) {
786 struct column *col = &graph->columns[i];
787 if (col->commit == graph->commit) {
788 seen_this = 1;
427fc5b8 789 strbuf_write_column(sb, col, '|');
36a31fea
AC
790 strbuf_addf(sb, "%*s", graph->expansion_row, "");
791 chars_written += 1 + graph->expansion_row;
3395908e
AS
792 } else if (seen_this && (graph->expansion_row == 0)) {
793 /*
794 * This is the first line of the pre-commit output.
795 * If the previous commit was a merge commit and
796 * ended in the GRAPH_POST_MERGE state, all branch
797 * lines after graph->prev_commit_index were
798 * printed as "\" on the previous line. Continue
799 * to print them as "\" on this line. Otherwise,
800 * print the branch lines as "|".
801 */
802 if (graph->prev_state == GRAPH_POST_MERGE &&
803 graph->prev_commit_index < i)
427fc5b8 804 strbuf_write_column(sb, col, '\\');
3395908e 805 else
427fc5b8
AC
806 strbuf_write_column(sb, col, '|');
807 chars_written++;
3395908e 808 } else if (seen_this && (graph->expansion_row > 0)) {
427fc5b8
AC
809 strbuf_write_column(sb, col, '\\');
810 chars_written++;
c12172d2 811 } else {
427fc5b8
AC
812 strbuf_write_column(sb, col, '|');
813 chars_written++;
c12172d2 814 }
427fc5b8
AC
815 strbuf_addch(sb, ' ');
816 chars_written++;
c12172d2
AS
817 }
818
427fc5b8 819 graph_pad_horizontally(graph, sb, chars_written);
c12172d2
AS
820
821 /*
822 * Increment graph->expansion_row,
823 * and move to state GRAPH_COMMIT if necessary
824 */
825 graph->expansion_row++;
826 if (graph->expansion_row >= num_expansion_rows)
3395908e 827 graph_update_state(graph, GRAPH_COMMIT);
c12172d2
AS
828}
829
3c68d67b
AS
830static void graph_output_commit_char(struct git_graph *graph, struct strbuf *sb)
831{
832 /*
833 * For boundary commits, print 'o'
834 * (We should only see boundary commits when revs->boundary is set.)
835 */
836 if (graph->commit->object.flags & BOUNDARY) {
837 assert(graph->revs->boundary);
838 strbuf_addch(sb, 'o');
839 return;
840 }
841
842 /*
1df2d656 843 * get_revision_mark() handles all other cases without assert()
3c68d67b 844 */
1df2d656 845 strbuf_addstr(sb, get_revision_mark(graph->revs, graph->commit));
3c68d67b
AS
846}
847
427fc5b8
AC
848/*
849 * Draw an octopus merge and return the number of characters written.
850 */
851static int graph_draw_octopus_merge(struct git_graph *graph,
852 struct strbuf *sb)
853{
854 /*
855 * Here dashless_commits represents the number of parents
856 * which don't need to have dashes (because their edges fit
857 * neatly under the commit).
858 */
859 const int dashless_commits = 2;
860 int col_num, i;
861 int num_dashes =
862 ((graph->num_parents - dashless_commits) * 2) - 1;
863 for (i = 0; i < num_dashes; i++) {
339c17bc 864 col_num = (i / 2) + dashless_commits + graph->commit_index;
427fc5b8
AC
865 strbuf_write_column(sb, &graph->new_columns[col_num], '-');
866 }
339c17bc 867 col_num = (i / 2) + dashless_commits + graph->commit_index;
427fc5b8
AC
868 strbuf_write_column(sb, &graph->new_columns[col_num], '.');
869 return num_dashes + 1;
870}
871
064bfbde 872static void graph_output_commit_line(struct git_graph *graph, struct strbuf *sb)
c12172d2
AS
873{
874 int seen_this = 0;
427fc5b8 875 int i, chars_written;
c12172d2
AS
876
877 /*
878 * Output the row containing this commit
879 * Iterate up to and including graph->num_columns,
880 * since the current commit may not be in any of the existing
881 * columns. (This happens when the current commit doesn't have any
882 * children that we have already processed.)
883 */
884 seen_this = 0;
427fc5b8 885 chars_written = 0;
c12172d2 886 for (i = 0; i <= graph->num_columns; i++) {
427fc5b8 887 struct column *col = &graph->columns[i];
c12172d2
AS
888 struct commit *col_commit;
889 if (i == graph->num_columns) {
890 if (seen_this)
891 break;
892 col_commit = graph->commit;
893 } else {
894 col_commit = graph->columns[i].commit;
895 }
896
897 if (col_commit == graph->commit) {
898 seen_this = 1;
3c68d67b 899 graph_output_commit_char(graph, sb);
427fc5b8 900 chars_written++;
c12172d2 901
a6c1a382 902 if (graph->num_parents > 2)
427fc5b8
AC
903 chars_written += graph_draw_octopus_merge(graph,
904 sb);
3395908e 905 } else if (seen_this && (graph->num_parents > 2)) {
427fc5b8
AC
906 strbuf_write_column(sb, col, '\\');
907 chars_written++;
3395908e
AS
908 } else if (seen_this && (graph->num_parents == 2)) {
909 /*
910 * This is a 2-way merge commit.
911 * There is no GRAPH_PRE_COMMIT stage for 2-way
912 * merges, so this is the first line of output
913 * for this commit. Check to see what the previous
914 * line of output was.
915 *
916 * If it was GRAPH_POST_MERGE, the branch line
917 * coming into this commit may have been '\',
918 * and not '|' or '/'. If so, output the branch
919 * line as '\' on this line, instead of '|'. This
920 * makes the output look nicer.
921 */
922 if (graph->prev_state == GRAPH_POST_MERGE &&
923 graph->prev_commit_index < i)
427fc5b8 924 strbuf_write_column(sb, col, '\\');
3395908e 925 else
427fc5b8
AC
926 strbuf_write_column(sb, col, '|');
927 chars_written++;
c12172d2 928 } else {
427fc5b8
AC
929 strbuf_write_column(sb, col, '|');
930 chars_written++;
c12172d2 931 }
427fc5b8
AC
932 strbuf_addch(sb, ' ');
933 chars_written++;
c12172d2
AS
934 }
935
427fc5b8 936 graph_pad_horizontally(graph, sb, chars_written);
c12172d2
AS
937
938 /*
939 * Update graph->state
940 */
941 if (graph->num_parents > 1)
3395908e 942 graph_update_state(graph, GRAPH_POST_MERGE);
c12172d2 943 else if (graph_is_mapping_correct(graph))
3395908e 944 graph_update_state(graph, GRAPH_PADDING);
c12172d2 945 else
3395908e 946 graph_update_state(graph, GRAPH_COLLAPSING);
c12172d2
AS
947}
948
427fc5b8
AC
949static struct column *find_new_column_by_commit(struct git_graph *graph,
950 struct commit *commit)
951{
952 int i;
953 for (i = 0; i < graph->num_new_columns; i++) {
954 if (graph->new_columns[i].commit == commit)
955 return &graph->new_columns[i];
956 }
67da52be 957 return NULL;
427fc5b8
AC
958}
959
064bfbde 960static void graph_output_post_merge_line(struct git_graph *graph, struct strbuf *sb)
c12172d2
AS
961{
962 int seen_this = 0;
427fc5b8 963 int i, j, chars_written;
c12172d2
AS
964
965 /*
966 * Output the post-merge row
967 */
427fc5b8 968 chars_written = 0;
c12172d2 969 for (i = 0; i <= graph->num_columns; i++) {
427fc5b8 970 struct column *col = &graph->columns[i];
c12172d2
AS
971 struct commit *col_commit;
972 if (i == graph->num_columns) {
973 if (seen_this)
974 break;
975 col_commit = graph->commit;
976 } else {
427fc5b8 977 col_commit = col->commit;
c12172d2
AS
978 }
979
980 if (col_commit == graph->commit) {
427fc5b8
AC
981 /*
982 * Since the current commit is a merge find
983 * the columns for the parent commits in
984 * new_columns and use those to format the
985 * edges.
986 */
987 struct commit_list *parents = NULL;
988 struct column *par_column;
c12172d2 989 seen_this = 1;
427fc5b8
AC
990 parents = first_interesting_parent(graph);
991 assert(parents);
992 par_column = find_new_column_by_commit(graph, parents->item);
993 assert(par_column);
994
995 strbuf_write_column(sb, par_column, '|');
996 chars_written++;
997 for (j = 0; j < graph->num_parents - 1; j++) {
998 parents = next_interesting_parent(graph, parents);
999 assert(parents);
1000 par_column = find_new_column_by_commit(graph, parents->item);
1001 assert(par_column);
1002 strbuf_write_column(sb, par_column, '\\');
1003 strbuf_addch(sb, ' ');
1004 }
1005 chars_written += j * 2;
3395908e 1006 } else if (seen_this) {
427fc5b8
AC
1007 strbuf_write_column(sb, col, '\\');
1008 strbuf_addch(sb, ' ');
1009 chars_written += 2;
c12172d2 1010 } else {
427fc5b8
AC
1011 strbuf_write_column(sb, col, '|');
1012 strbuf_addch(sb, ' ');
1013 chars_written += 2;
c12172d2
AS
1014 }
1015 }
1016
427fc5b8 1017 graph_pad_horizontally(graph, sb, chars_written);
c12172d2
AS
1018
1019 /*
1020 * Update graph->state
1021 */
1022 if (graph_is_mapping_correct(graph))
3395908e 1023 graph_update_state(graph, GRAPH_PADDING);
c12172d2 1024 else
3395908e 1025 graph_update_state(graph, GRAPH_COLLAPSING);
c12172d2
AS
1026}
1027
064bfbde 1028static void graph_output_collapsing_line(struct git_graph *graph, struct strbuf *sb)
c12172d2
AS
1029{
1030 int i;
eaf158f8
AC
1031 short used_horizontal = 0;
1032 int horizontal_edge = -1;
1033 int horizontal_edge_target = -1;
c12172d2
AS
1034
1035 /*
1036 * Clear out the new_mapping array
1037 */
1038 for (i = 0; i < graph->mapping_size; i++)
1039 graph->new_mapping[i] = -1;
1040
1041 for (i = 0; i < graph->mapping_size; i++) {
1042 int target = graph->mapping[i];
1043 if (target < 0)
1044 continue;
1045
1046 /*
1047 * Since update_columns() always inserts the leftmost
1048 * column first, each branch's target location should
1049 * always be either its current location or to the left of
1050 * its current location.
1051 *
1052 * We never have to move branches to the right. This makes
1053 * the graph much more legible, since whenever branches
1054 * cross, only one is moving directions.
1055 */
1056 assert(target * 2 <= i);
1057
1058 if (target * 2 == i) {
1059 /*
1060 * This column is already in the
1061 * correct place
1062 */
1063 assert(graph->new_mapping[i] == -1);
1064 graph->new_mapping[i] = target;
1065 } else if (graph->new_mapping[i - 1] < 0) {
1066 /*
1067 * Nothing is to the left.
1068 * Move to the left by one
1069 */
1070 graph->new_mapping[i - 1] = target;
eaf158f8
AC
1071 /*
1072 * If there isn't already an edge moving horizontally
1073 * select this one.
1074 */
1075 if (horizontal_edge == -1) {
1076 int j;
1077 horizontal_edge = i;
1078 horizontal_edge_target = target;
1079 /*
1080 * The variable target is the index of the graph
1081 * column, and therefore target*2+3 is the
1082 * actual screen column of the first horizontal
1083 * line.
1084 */
1085 for (j = (target * 2)+3; j < (i - 2); j += 2)
1086 graph->new_mapping[j] = target;
1087 }
c12172d2
AS
1088 } else if (graph->new_mapping[i - 1] == target) {
1089 /*
1090 * There is a branch line to our left
1091 * already, and it is our target. We
1092 * combine with this line, since we share
1093 * the same parent commit.
1094 *
1095 * We don't have to add anything to the
1096 * output or new_mapping, since the
1097 * existing branch line has already taken
1098 * care of it.
1099 */
1100 } else {
1101 /*
1102 * There is a branch line to our left,
1103 * but it isn't our target. We need to
1104 * cross over it.
1105 *
1106 * The space just to the left of this
1107 * branch should always be empty.
eaf158f8
AC
1108 *
1109 * The branch to the left of that space
1110 * should be our eventual target.
c12172d2
AS
1111 */
1112 assert(graph->new_mapping[i - 1] > target);
1113 assert(graph->new_mapping[i - 2] < 0);
eaf158f8 1114 assert(graph->new_mapping[i - 3] == target);
c12172d2 1115 graph->new_mapping[i - 2] = target;
eaf158f8
AC
1116 /*
1117 * Mark this branch as the horizontal edge to
1118 * prevent any other edges from moving
1119 * horizontally.
1120 */
1121 if (horizontal_edge == -1)
1122 horizontal_edge = i;
c12172d2
AS
1123 }
1124 }
1125
1126 /*
1127 * The new mapping may be 1 smaller than the old mapping
1128 */
1129 if (graph->new_mapping[graph->mapping_size - 1] < 0)
1130 graph->mapping_size--;
1131
1132 /*
1133 * Output out a line based on the new mapping info
1134 */
1135 for (i = 0; i < graph->mapping_size; i++) {
1136 int target = graph->new_mapping[i];
1137 if (target < 0)
1138 strbuf_addch(sb, ' ');
1139 else if (target * 2 == i)
427fc5b8 1140 strbuf_write_column(sb, &graph->new_columns[target], '|');
eaf158f8
AC
1141 else if (target == horizontal_edge_target &&
1142 i != horizontal_edge - 1) {
1143 /*
1144 * Set the mappings for all but the
1145 * first segment to -1 so that they
1146 * won't continue into the next line.
1147 */
1148 if (i != (target * 2)+3)
1149 graph->new_mapping[i] = -1;
1150 used_horizontal = 1;
1151 strbuf_write_column(sb, &graph->new_columns[target], '_');
1152 } else {
1153 if (used_horizontal && i < horizontal_edge)
1154 graph->new_mapping[i] = -1;
427fc5b8 1155 strbuf_write_column(sb, &graph->new_columns[target], '/');
eaf158f8
AC
1156
1157 }
c12172d2
AS
1158 }
1159
427fc5b8 1160 graph_pad_horizontally(graph, sb, graph->mapping_size);
c12172d2
AS
1161
1162 /*
1163 * Swap mapping and new_mapping
1164 */
35d803bc 1165 SWAP(graph->mapping, graph->new_mapping);
c12172d2
AS
1166
1167 /*
1168 * If graph->mapping indicates that all of the branch lines
1169 * are already in the correct positions, we are done.
1170 * Otherwise, we need to collapse some branch lines together.
1171 */
1172 if (graph_is_mapping_correct(graph))
3395908e 1173 graph_update_state(graph, GRAPH_PADDING);
c12172d2
AS
1174}
1175
ac751a0b 1176int graph_next_line(struct git_graph *graph, struct strbuf *sb)
c12172d2
AS
1177{
1178 switch (graph->state) {
1179 case GRAPH_PADDING:
1180 graph_output_padding_line(graph, sb);
1181 return 0;
1182 case GRAPH_SKIP:
1183 graph_output_skip_line(graph, sb);
1184 return 0;
1185 case GRAPH_PRE_COMMIT:
1186 graph_output_pre_commit_line(graph, sb);
1187 return 0;
1188 case GRAPH_COMMIT:
1189 graph_output_commit_line(graph, sb);
1190 return 1;
1191 case GRAPH_POST_MERGE:
1192 graph_output_post_merge_line(graph, sb);
1193 return 0;
1194 case GRAPH_COLLAPSING:
1195 graph_output_collapsing_line(graph, sb);
1196 return 0;
1197 }
1198
1199 assert(0);
1200 return 0;
1201}
1202
064bfbde 1203static void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
c12172d2 1204{
415792ed 1205 int i;
16477935 1206 int chars_written = 0;
c12172d2
AS
1207
1208 if (graph->state != GRAPH_COMMIT) {
1209 graph_next_line(graph, sb);
1210 return;
1211 }
1212
1213 /*
1214 * Output the row containing this commit
1215 * Iterate up to and including graph->num_columns,
1216 * since the current commit may not be in any of the existing
1217 * columns. (This happens when the current commit doesn't have any
1218 * children that we have already processed.)
1219 */
1220 for (i = 0; i < graph->num_columns; i++) {
427fc5b8 1221 struct column *col = &graph->columns[i];
16477935 1222
0176e7a7 1223 strbuf_write_column(sb, col, '|');
16477935
JK
1224 chars_written++;
1225
1226 if (col->commit == graph->commit && graph->num_parents > 2) {
1227 int len = (graph->num_parents - 2) * 2;
1228 strbuf_addchars(sb, ' ', len);
1229 chars_written += len;
1230 } else {
427fc5b8 1231 strbuf_addch(sb, ' ');
16477935
JK
1232 chars_written++;
1233 }
c12172d2
AS
1234 }
1235
16477935 1236 graph_pad_horizontally(graph, sb, chars_written);
3395908e
AS
1237
1238 /*
1239 * Update graph->prev_state since we have output a padding line
1240 */
1241 graph->prev_state = GRAPH_PADDING;
c12172d2
AS
1242}
1243
1244int graph_is_commit_finished(struct git_graph const *graph)
1245{
1246 return (graph->state == GRAPH_PADDING);
1247}
1248
1249void graph_show_commit(struct git_graph *graph)
1250{
f285a2d7 1251 struct strbuf msgbuf = STRBUF_INIT;
c12172d2
AS
1252 int shown_commit_line = 0;
1253
660e113c
JK
1254 graph_show_line_prefix(default_diffopt);
1255
c12172d2
AS
1256 if (!graph)
1257 return;
1258
a48ec241
JK
1259 /*
1260 * When showing a diff of a merge against each of its parents, we
1261 * are called once for each parent without graph_update having been
1262 * called. In this case, simply output a single padding line.
1263 */
1264 if (graph_is_commit_finished(graph)) {
1265 graph_show_padding(graph);
1266 shown_commit_line = 1;
1267 }
1268
656197ad 1269 while (!shown_commit_line && !graph_is_commit_finished(graph)) {
c12172d2 1270 shown_commit_line = graph_next_line(graph, &msgbuf);
c61008fd
JS
1271 fwrite(msgbuf.buf, sizeof(char), msgbuf.len,
1272 graph->revs->diffopt.file);
660e113c 1273 if (!shown_commit_line) {
c61008fd 1274 putc('\n', graph->revs->diffopt.file);
660e113c
JK
1275 graph_show_line_prefix(&graph->revs->diffopt);
1276 }
c12172d2
AS
1277 strbuf_setlen(&msgbuf, 0);
1278 }
1279
1280 strbuf_release(&msgbuf);
1281}
1282
1283void graph_show_oneline(struct git_graph *graph)
1284{
f285a2d7 1285 struct strbuf msgbuf = STRBUF_INIT;
c12172d2 1286
660e113c
JK
1287 graph_show_line_prefix(default_diffopt);
1288
c12172d2
AS
1289 if (!graph)
1290 return;
1291
c12172d2 1292 graph_next_line(graph, &msgbuf);
c61008fd 1293 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file);
c12172d2
AS
1294 strbuf_release(&msgbuf);
1295}
1296
1297void graph_show_padding(struct git_graph *graph)
1298{
f285a2d7 1299 struct strbuf msgbuf = STRBUF_INIT;
c12172d2 1300
660e113c
JK
1301 graph_show_line_prefix(default_diffopt);
1302
c12172d2
AS
1303 if (!graph)
1304 return;
1305
c12172d2 1306 graph_padding_line(graph, &msgbuf);
c61008fd 1307 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file);
c12172d2
AS
1308 strbuf_release(&msgbuf);
1309}
1310
1311int graph_show_remainder(struct git_graph *graph)
1312{
f285a2d7 1313 struct strbuf msgbuf = STRBUF_INIT;
c12172d2
AS
1314 int shown = 0;
1315
660e113c
JK
1316 graph_show_line_prefix(default_diffopt);
1317
c12172d2
AS
1318 if (!graph)
1319 return 0;
1320
1321 if (graph_is_commit_finished(graph))
1322 return 0;
1323
c12172d2
AS
1324 for (;;) {
1325 graph_next_line(graph, &msgbuf);
c61008fd
JS
1326 fwrite(msgbuf.buf, sizeof(char), msgbuf.len,
1327 graph->revs->diffopt.file);
c12172d2
AS
1328 strbuf_setlen(&msgbuf, 0);
1329 shown = 1;
1330
660e113c 1331 if (!graph_is_commit_finished(graph)) {
c61008fd 1332 putc('\n', graph->revs->diffopt.file);
660e113c
JK
1333 graph_show_line_prefix(&graph->revs->diffopt);
1334 } else {
c12172d2 1335 break;
660e113c 1336 }
c12172d2
AS
1337 }
1338 strbuf_release(&msgbuf);
1339
1340 return shown;
1341}
1342
660e113c
JK
1343static void graph_show_strbuf(struct git_graph *graph,
1344 FILE *file,
1345 struct strbuf const *sb)
c12172d2
AS
1346{
1347 char *p;
1348
c12172d2
AS
1349 /*
1350 * Print the strbuf line by line,
1351 * and display the graph info before each line but the first.
1352 */
1353 p = sb->buf;
1354 while (p) {
1355 size_t len;
1356 char *next_p = strchr(p, '\n');
1357 if (next_p) {
1358 next_p++;
1359 len = next_p - p;
1360 } else {
1361 len = (sb->buf + sb->len) - p;
1362 }
660e113c 1363 fwrite(p, sizeof(char), len, file);
c12172d2
AS
1364 if (next_p && *next_p != '\0')
1365 graph_show_oneline(graph);
1366 p = next_p;
1367 }
1368}
1369
1370void graph_show_commit_msg(struct git_graph *graph,
660e113c 1371 FILE *file,
c12172d2
AS
1372 struct strbuf const *sb)
1373{
1374 int newline_terminated;
1375
c12172d2
AS
1376 /*
1377 * Show the commit message
1378 */
660e113c
JK
1379 graph_show_strbuf(graph, file, sb);
1380
1381 if (!graph)
1382 return;
1383
1384 newline_terminated = (sb->len && sb->buf[sb->len - 1] == '\n');
c12172d2
AS
1385
1386 /*
1387 * If there is more output needed for this commit, show it now
1388 */
1389 if (!graph_is_commit_finished(graph)) {
1390 /*
1391 * If sb doesn't have a terminating newline, print one now,
1392 * so we can start the remainder of the graph output on a
1393 * new line.
1394 */
1395 if (!newline_terminated)
660e113c 1396 putc('\n', file);
c12172d2
AS
1397
1398 graph_show_remainder(graph);
1399
1400 /*
1401 * If sb ends with a newline, our output should too.
1402 */
1403 if (newline_terminated)
660e113c 1404 putc('\n', file);
c12172d2
AS
1405 }
1406}