]> git.ipfire.org Git - thirdparty/git.git/blame - graph.c
Make "git-remote rm" delete refs acccording to fetch specs
[thirdparty/git.git] / graph.c
CommitLineData
c12172d2
AS
1#include "cache.h"
2#include "commit.h"
3#include "graph.h"
4#include "diff.h"
5#include "revision.h"
6
7/*
8 * TODO:
9 * - Add colors to the graph.
10 * Pick a color for each column, and print all characters
11 * in that column with the specified color.
12 *
13 * - Limit the number of columns, similar to the way gitk does.
14 * If we reach more than a specified number of columns, omit
15 * sections of some columns.
16 *
17 * - The output during the GRAPH_PRE_COMMIT and GRAPH_COLLAPSING states
18 * could be made more compact by printing horizontal lines, instead of
19 * long diagonal lines. For example, during collapsing, something like
20 * this: instead of this:
21 * | | | | | | | | | |
22 * | |_|_|/ | | | |/
23 * |/| | | | | |/|
24 * | | | | | |/| |
25 * |/| | |
26 * | | | |
27 *
28 * If there are several parallel diagonal lines, they will need to be
29 * replaced with horizontal lines on subsequent rows.
30 */
31
32struct column {
33 /*
34 * The parent commit of this column.
35 */
36 struct commit *commit;
37 /*
38 * XXX: Once we add support for colors, struct column could also
39 * contain the color of its branch line.
40 */
41};
42
43enum graph_state {
44 GRAPH_PADDING,
45 GRAPH_SKIP,
46 GRAPH_PRE_COMMIT,
47 GRAPH_COMMIT,
48 GRAPH_POST_MERGE,
49 GRAPH_COLLAPSING
50};
51
52struct git_graph {
53 /*
54 * The commit currently being processed
55 */
56 struct commit *commit;
7528f27d
AS
57 /* The rev-info used for the current traversal */
58 struct rev_info *revs;
c12172d2 59 /*
37a75abc
AS
60 * The number of interesting parents that this commit has.
61 *
62 * Note that this is not the same as the actual number of parents.
63 * This count excludes parents that won't be printed in the graph
64 * output, as determined by graph_is_interesting().
c12172d2
AS
65 */
66 int num_parents;
0724cb86
AS
67 /*
68 * The width of the graph output for this commit.
69 * All rows for this commit are padded to this width, so that
70 * messages printed after the graph output are aligned.
71 */
72 int width;
c12172d2
AS
73 /*
74 * The next expansion row to print
75 * when state is GRAPH_PRE_COMMIT
76 */
77 int expansion_row;
78 /*
79 * The current output state.
80 * This tells us what kind of line graph_next_line() should output.
81 */
82 enum graph_state state;
83 /*
84 * The maximum number of columns that can be stored in the columns
85 * and new_columns arrays. This is also half the number of entries
86 * that can be stored in the mapping and new_mapping arrays.
87 */
88 int column_capacity;
89 /*
90 * The number of columns (also called "branch lines" in some places)
91 */
92 int num_columns;
93 /*
94 * The number of columns in the new_columns array
95 */
96 int num_new_columns;
97 /*
98 * The number of entries in the mapping array
99 */
100 int mapping_size;
101 /*
102 * The column state before we output the current commit.
103 */
104 struct column *columns;
105 /*
106 * The new column state after we output the current commit.
107 * Only valid when state is GRAPH_COLLAPSING.
108 */
109 struct column *new_columns;
110 /*
111 * An array that tracks the current state of each
112 * character in the output line during state GRAPH_COLLAPSING.
113 * Each entry is -1 if this character is empty, or a non-negative
114 * integer if the character contains a branch line. The value of
115 * the integer indicates the target position for this branch line.
116 * (I.e., this array maps the current column positions to their
117 * desired positions.)
118 *
119 * The maximum capacity of this array is always
120 * sizeof(int) * 2 * column_capacity.
121 */
122 int *mapping;
123 /*
124 * A temporary array for computing the next mapping state
125 * while we are outputting a mapping line. This is stored as part
126 * of the git_graph simply so we don't have to allocate a new
127 * temporary array each time we have to output a collapsing line.
128 */
129 int *new_mapping;
130};
131
7528f27d 132struct git_graph *graph_init(struct rev_info *opt)
c12172d2
AS
133{
134 struct git_graph *graph = xmalloc(sizeof(struct git_graph));
135 graph->commit = NULL;
7528f27d 136 graph->revs = opt;
c12172d2
AS
137 graph->num_parents = 0;
138 graph->expansion_row = 0;
139 graph->state = GRAPH_PADDING;
140 graph->num_columns = 0;
141 graph->num_new_columns = 0;
142 graph->mapping_size = 0;
143
144 /*
145 * Allocate a reasonably large default number of columns
146 * We'll automatically grow columns later if we need more room.
147 */
148 graph->column_capacity = 30;
149 graph->columns = xmalloc(sizeof(struct column) *
150 graph->column_capacity);
151 graph->new_columns = xmalloc(sizeof(struct column) *
152 graph->column_capacity);
153 graph->mapping = xmalloc(sizeof(int) * 2 * graph->column_capacity);
154 graph->new_mapping = xmalloc(sizeof(int) * 2 * graph->column_capacity);
155
156 return graph;
157}
158
159void graph_release(struct git_graph *graph)
160{
161 free(graph->columns);
162 free(graph->new_columns);
163 free(graph->mapping);
164 free(graph);
165}
166
167static void graph_ensure_capacity(struct git_graph *graph, int num_columns)
168{
169 if (graph->column_capacity >= num_columns)
170 return;
171
172 do {
173 graph->column_capacity *= 2;
174 } while (graph->column_capacity < num_columns);
175
176 graph->columns = xrealloc(graph->columns,
177 sizeof(struct column) *
178 graph->column_capacity);
179 graph->new_columns = xrealloc(graph->new_columns,
180 sizeof(struct column) *
181 graph->column_capacity);
182 graph->mapping = xrealloc(graph->mapping,
183 sizeof(int) * 2 * graph->column_capacity);
184 graph->new_mapping = xrealloc(graph->new_mapping,
185 sizeof(int) * 2 * graph->column_capacity);
186}
187
37a75abc
AS
188/*
189 * Returns 1 if the commit will be printed in the graph output,
190 * and 0 otherwise.
191 */
3c68d67b 192static int graph_is_interesting(struct git_graph *graph, struct commit *commit)
37a75abc 193{
3c68d67b
AS
194 /*
195 * If revs->boundary is set, commits whose children have
196 * been shown are always interesting, even if they have the
197 * UNINTERESTING or TREESAME flags set.
3c68d67b
AS
198 */
199 if (graph->revs && graph->revs->boundary) {
4603ec0f 200 if (commit->object.flags & CHILD_SHOWN)
3c68d67b
AS
201 return 1;
202 }
203
37a75abc
AS
204 /*
205 * Uninteresting and pruned commits won't be printed
206 */
207 return (commit->object.flags & (UNINTERESTING | TREESAME)) ? 0 : 1;
208}
209
c12172d2
AS
210static void graph_insert_into_new_columns(struct git_graph *graph,
211 struct commit *commit,
212 int *mapping_index)
213{
214 int i;
215
216 /*
37a75abc 217 * Ignore uinteresting commits
c12172d2 218 */
3c68d67b 219 if (!graph_is_interesting(graph, commit))
c12172d2
AS
220 return;
221
222 /*
223 * If the commit is already in the new_columns list, we don't need to
224 * add it. Just update the mapping correctly.
225 */
226 for (i = 0; i < graph->num_new_columns; i++) {
227 if (graph->new_columns[i].commit == commit) {
228 graph->mapping[*mapping_index] = i;
229 *mapping_index += 2;
230 return;
231 }
232 }
233
234 /*
235 * This commit isn't already in new_columns. Add it.
236 */
237 graph->new_columns[graph->num_new_columns].commit = commit;
238 graph->mapping[*mapping_index] = graph->num_new_columns;
239 *mapping_index += 2;
240 graph->num_new_columns++;
241}
242
0724cb86
AS
243static void graph_update_width(struct git_graph *graph,
244 int is_commit_in_existing_columns)
245{
246 /*
247 * Compute the width needed to display the graph for this commit.
248 * This is the maximum width needed for any row. All other rows
249 * will be padded to this width.
250 *
251 * Compute the number of columns in the widest row:
252 * Count each existing column (graph->num_columns), and each new
253 * column added by this commit.
254 */
255 int max_cols = graph->num_columns + graph->num_parents;
256
257 /*
37a75abc
AS
258 * Even if the current commit has no parents to be printed, it
259 * still takes up a column for itself.
0724cb86
AS
260 */
261 if (graph->num_parents < 1)
262 max_cols++;
263
264 /*
265 * We added a column for the the current commit as part of
266 * graph->num_parents. If the current commit was already in
267 * graph->columns, then we have double counted it.
268 */
269 if (is_commit_in_existing_columns)
270 max_cols--;
271
272 /*
273 * Each column takes up 2 spaces
274 */
275 graph->width = max_cols * 2;
276}
277
c12172d2
AS
278static void graph_update_columns(struct git_graph *graph)
279{
280 struct commit_list *parent;
281 struct column *tmp_columns;
282 int max_new_columns;
283 int mapping_idx;
0724cb86 284 int i, seen_this, is_commit_in_columns;
c12172d2
AS
285
286 /*
287 * Swap graph->columns with graph->new_columns
288 * graph->columns contains the state for the previous commit,
289 * and new_columns now contains the state for our commit.
290 *
291 * We'll re-use the old columns array as storage to compute the new
292 * columns list for the commit after this one.
293 */
294 tmp_columns = graph->columns;
295 graph->columns = graph->new_columns;
296 graph->num_columns = graph->num_new_columns;
297
298 graph->new_columns = tmp_columns;
299 graph->num_new_columns = 0;
300
301 /*
302 * Now update new_columns and mapping with the information for the
303 * commit after this one.
304 *
305 * First, make sure we have enough room. At most, there will
306 * be graph->num_columns + graph->num_parents columns for the next
307 * commit.
308 */
309 max_new_columns = graph->num_columns + graph->num_parents;
310 graph_ensure_capacity(graph, max_new_columns);
311
312 /*
313 * Clear out graph->mapping
314 */
315 graph->mapping_size = 2 * max_new_columns;
316 for (i = 0; i < graph->mapping_size; i++)
317 graph->mapping[i] = -1;
318
319 /*
320 * Populate graph->new_columns and graph->mapping
321 *
322 * Some of the parents of this commit may already be in
323 * graph->columns. If so, graph->new_columns should only contain a
324 * single entry for each such commit. graph->mapping should
325 * contain information about where each current branch line is
326 * supposed to end up after the collapsing is performed.
327 */
328 seen_this = 0;
329 mapping_idx = 0;
0724cb86 330 is_commit_in_columns = 1;
c12172d2
AS
331 for (i = 0; i <= graph->num_columns; i++) {
332 struct commit *col_commit;
333 if (i == graph->num_columns) {
334 if (seen_this)
335 break;
0724cb86 336 is_commit_in_columns = 0;
c12172d2
AS
337 col_commit = graph->commit;
338 } else {
339 col_commit = graph->columns[i].commit;
340 }
341
342 if (col_commit == graph->commit) {
37a75abc 343 int old_mapping_idx = mapping_idx;
c12172d2
AS
344 seen_this = 1;
345 for (parent = graph->commit->parents;
346 parent;
347 parent = parent->next) {
348 graph_insert_into_new_columns(graph,
349 parent->item,
350 &mapping_idx);
351 }
37a75abc
AS
352 /*
353 * We always need to increment mapping_idx by at
354 * least 2, even if it has no interesting parents.
355 * The current commit always takes up at least 2
356 * spaces.
357 */
358 if (mapping_idx == old_mapping_idx)
359 mapping_idx += 2;
c12172d2
AS
360 } else {
361 graph_insert_into_new_columns(graph, col_commit,
362 &mapping_idx);
363 }
364 }
365
366 /*
367 * Shrink mapping_size to be the minimum necessary
368 */
369 while (graph->mapping_size > 1 &&
370 graph->mapping[graph->mapping_size - 1] < 0)
371 graph->mapping_size--;
0724cb86
AS
372
373 /*
374 * Compute graph->width for this commit
375 */
376 graph_update_width(graph, is_commit_in_columns);
c12172d2
AS
377}
378
379void graph_update(struct git_graph *graph, struct commit *commit)
380{
381 struct commit_list *parent;
382
383 /*
384 * Set the new commit
385 */
386 graph->commit = commit;
387
388 /*
37a75abc 389 * Count how many interesting parents this commit has
c12172d2
AS
390 */
391 graph->num_parents = 0;
37a75abc 392 for (parent = commit->parents; parent; parent = parent->next) {
3c68d67b 393 if (graph_is_interesting(graph, parent->item))
37a75abc
AS
394 graph->num_parents++;
395 }
c12172d2
AS
396
397 /*
398 * Call graph_update_columns() to update
399 * columns, new_columns, and mapping.
400 */
401 graph_update_columns(graph);
402
403 graph->expansion_row = 0;
404
405 /*
406 * Update graph->state.
407 *
408 * If the previous commit didn't get to the GRAPH_PADDING state,
409 * it never finished its output. Goto GRAPH_SKIP, to print out
410 * a line to indicate that portion of the graph is missing.
411 *
412 * Otherwise, if there are 3 or more parents, we need to print
413 * extra rows before the commit, to expand the branch lines around
414 * it and make room for it.
415 *
416 * If there are less than 3 parents, we can immediately print the
417 * commit line.
418 */
419 if (graph->state != GRAPH_PADDING)
420 graph->state = GRAPH_SKIP;
421 else if (graph->num_parents >= 3)
422 graph->state = GRAPH_PRE_COMMIT;
423 else
424 graph->state = GRAPH_COMMIT;
425}
426
427static int graph_is_mapping_correct(struct git_graph *graph)
428{
429 int i;
430
431 /*
432 * The mapping is up to date if each entry is at its target,
433 * or is 1 greater than its target.
434 * (If it is 1 greater than the target, '/' will be printed, so it
435 * will look correct on the next row.)
436 */
437 for (i = 0; i < graph->mapping_size; i++) {
438 int target = graph->mapping[i];
439 if (target < 0)
440 continue;
441 if (target == (i / 2))
442 continue;
443 return 0;
444 }
445
446 return 1;
447}
448
449static void graph_pad_horizontally(struct git_graph *graph, struct strbuf *sb)
450{
451 /*
452 * Add additional spaces to the end of the strbuf, so that all
453 * lines for a particular commit have the same width.
454 *
455 * This way, fields printed to the right of the graph will remain
456 * aligned for the entire commit.
c12172d2 457 */
0724cb86
AS
458 int extra;
459 if (sb->len >= graph->width)
c12172d2
AS
460 return;
461
0724cb86 462 extra = graph->width - sb->len;
c12172d2
AS
463 strbuf_addf(sb, "%*s", (int) extra, "");
464}
465
466static void graph_output_padding_line(struct git_graph *graph,
467 struct strbuf *sb)
468{
469 int i;
470
471 /*
472 * We could conceivable be called with a NULL commit
473 * if our caller has a bug, and invokes graph_next_line()
474 * immediately after graph_init(), without first calling
475 * graph_update(). Return without outputting anything in this
476 * case.
477 */
478 if (!graph->commit)
479 return;
480
481 /*
482 * Output a padding row, that leaves all branch lines unchanged
483 */
484 for (i = 0; i < graph->num_new_columns; i++) {
485 strbuf_addstr(sb, "| ");
486 }
487
488 graph_pad_horizontally(graph, sb);
489}
490
491static void graph_output_skip_line(struct git_graph *graph, struct strbuf *sb)
492{
493 /*
494 * Output an ellipsis to indicate that a portion
495 * of the graph is missing.
496 */
497 strbuf_addstr(sb, "...");
498 graph_pad_horizontally(graph, sb);
499
500 if (graph->num_parents >= 3)
501 graph->state = GRAPH_PRE_COMMIT;
502 else
503 graph->state = GRAPH_COMMIT;
504}
505
506static void graph_output_pre_commit_line(struct git_graph *graph,
507 struct strbuf *sb)
508{
509 int num_expansion_rows;
510 int i, seen_this;
511
512 /*
513 * This function formats a row that increases the space around a commit
514 * with multiple parents, to make room for it. It should only be
515 * called when there are 3 or more parents.
516 *
517 * We need 2 extra rows for every parent over 2.
518 */
519 assert(graph->num_parents >= 3);
520 num_expansion_rows = (graph->num_parents - 2) * 2;
521
522 /*
523 * graph->expansion_row tracks the current expansion row we are on.
524 * It should be in the range [0, num_expansion_rows - 1]
525 */
526 assert(0 <= graph->expansion_row &&
527 graph->expansion_row < num_expansion_rows);
528
529 /*
530 * Output the row
531 */
532 seen_this = 0;
533 for (i = 0; i < graph->num_columns; i++) {
534 struct column *col = &graph->columns[i];
535 if (col->commit == graph->commit) {
536 seen_this = 1;
537 strbuf_addf(sb, "| %*s", graph->expansion_row, "");
538 } else if (seen_this) {
539 strbuf_addstr(sb, "\\ ");
540 } else {
541 strbuf_addstr(sb, "| ");
542 }
543 }
544
545 graph_pad_horizontally(graph, sb);
546
547 /*
548 * Increment graph->expansion_row,
549 * and move to state GRAPH_COMMIT if necessary
550 */
551 graph->expansion_row++;
552 if (graph->expansion_row >= num_expansion_rows)
553 graph->state = GRAPH_COMMIT;
554}
555
3c68d67b
AS
556static void graph_output_commit_char(struct git_graph *graph, struct strbuf *sb)
557{
558 /*
559 * For boundary commits, print 'o'
560 * (We should only see boundary commits when revs->boundary is set.)
561 */
562 if (graph->commit->object.flags & BOUNDARY) {
563 assert(graph->revs->boundary);
564 strbuf_addch(sb, 'o');
565 return;
566 }
567
568 /*
569 * If revs->left_right is set, print '<' for commits that
570 * come from the left side, and '>' for commits from the right
571 * side.
572 */
573 if (graph->revs && graph->revs->left_right) {
574 if (graph->commit->object.flags & SYMMETRIC_LEFT)
575 strbuf_addch(sb, '<');
576 else
577 strbuf_addch(sb, '>');
578 return;
579 }
580
581 /*
582 * Print 'M' for merge commits
583 *
584 * Note that we don't check graph->num_parents to determine if the
585 * commit is a merge, since that only tracks the number of
586 * "interesting" parents. We want to print 'M' for merge commits
587 * even if they have less than 2 interesting parents.
588 */
589 if (graph->commit->parents != NULL &&
590 graph->commit->parents->next != NULL) {
591 strbuf_addch(sb, 'M');
592 return;
593 }
594
595 /*
596 * Print '*' in all other cases
597 */
598 strbuf_addch(sb, '*');
599}
600
c12172d2
AS
601void graph_output_commit_line(struct git_graph *graph, struct strbuf *sb)
602{
603 int seen_this = 0;
604 int i, j;
605
606 /*
607 * Output the row containing this commit
608 * Iterate up to and including graph->num_columns,
609 * since the current commit may not be in any of the existing
610 * columns. (This happens when the current commit doesn't have any
611 * children that we have already processed.)
612 */
613 seen_this = 0;
614 for (i = 0; i <= graph->num_columns; i++) {
615 struct commit *col_commit;
616 if (i == graph->num_columns) {
617 if (seen_this)
618 break;
619 col_commit = graph->commit;
620 } else {
621 col_commit = graph->columns[i].commit;
622 }
623
624 if (col_commit == graph->commit) {
625 seen_this = 1;
3c68d67b 626 graph_output_commit_char(graph, sb);
c12172d2
AS
627
628 if (graph->num_parents < 2)
629 strbuf_addch(sb, ' ');
630 else if (graph->num_parents == 2)
631 strbuf_addstr(sb, " ");
632 else {
633 int num_dashes =
634 ((graph->num_parents - 2) * 2) - 1;
635 for (j = 0; j < num_dashes; j++)
636 strbuf_addch(sb, '-');
637 strbuf_addstr(sb, ". ");
638 }
639 } else if (seen_this && (graph->num_parents > 1)) {
640 strbuf_addstr(sb, "\\ ");
641 } else {
642 strbuf_addstr(sb, "| ");
643 }
644 }
645
646 graph_pad_horizontally(graph, sb);
647
648 /*
649 * Update graph->state
650 */
651 if (graph->num_parents > 1)
652 graph->state = GRAPH_POST_MERGE;
653 else if (graph_is_mapping_correct(graph))
654 graph->state = GRAPH_PADDING;
655 else
656 graph->state = GRAPH_COLLAPSING;
657}
658
659void graph_output_post_merge_line(struct git_graph *graph, struct strbuf *sb)
660{
661 int seen_this = 0;
662 int i, j;
663
664 /*
665 * Output the post-merge row
666 */
667 for (i = 0; i <= graph->num_columns; i++) {
668 struct commit *col_commit;
669 if (i == graph->num_columns) {
670 if (seen_this)
671 break;
672 col_commit = graph->commit;
673 } else {
674 col_commit = graph->columns[i].commit;
675 }
676
677 if (col_commit == graph->commit) {
678 seen_this = 1;
679 strbuf_addch(sb, '|');
680 for (j = 0; j < graph->num_parents - 1; j++)
681 strbuf_addstr(sb, "\\ ");
682 if (graph->num_parents == 2)
683 strbuf_addch(sb, ' ');
684 } else if (seen_this && (graph->num_parents > 2)) {
685 strbuf_addstr(sb, "\\ ");
686 } else {
687 strbuf_addstr(sb, "| ");
688 }
689 }
690
691 graph_pad_horizontally(graph, sb);
692
693 /*
694 * Update graph->state
695 */
696 if (graph_is_mapping_correct(graph))
697 graph->state = GRAPH_PADDING;
698 else
699 graph->state = GRAPH_COLLAPSING;
700}
701
702void graph_output_collapsing_line(struct git_graph *graph, struct strbuf *sb)
703{
704 int i;
705 int *tmp_mapping;
706
707 /*
708 * Clear out the new_mapping array
709 */
710 for (i = 0; i < graph->mapping_size; i++)
711 graph->new_mapping[i] = -1;
712
713 for (i = 0; i < graph->mapping_size; i++) {
714 int target = graph->mapping[i];
715 if (target < 0)
716 continue;
717
718 /*
719 * Since update_columns() always inserts the leftmost
720 * column first, each branch's target location should
721 * always be either its current location or to the left of
722 * its current location.
723 *
724 * We never have to move branches to the right. This makes
725 * the graph much more legible, since whenever branches
726 * cross, only one is moving directions.
727 */
728 assert(target * 2 <= i);
729
730 if (target * 2 == i) {
731 /*
732 * This column is already in the
733 * correct place
734 */
735 assert(graph->new_mapping[i] == -1);
736 graph->new_mapping[i] = target;
737 } else if (graph->new_mapping[i - 1] < 0) {
738 /*
739 * Nothing is to the left.
740 * Move to the left by one
741 */
742 graph->new_mapping[i - 1] = target;
743 } else if (graph->new_mapping[i - 1] == target) {
744 /*
745 * There is a branch line to our left
746 * already, and it is our target. We
747 * combine with this line, since we share
748 * the same parent commit.
749 *
750 * We don't have to add anything to the
751 * output or new_mapping, since the
752 * existing branch line has already taken
753 * care of it.
754 */
755 } else {
756 /*
757 * There is a branch line to our left,
758 * but it isn't our target. We need to
759 * cross over it.
760 *
761 * The space just to the left of this
762 * branch should always be empty.
763 */
764 assert(graph->new_mapping[i - 1] > target);
765 assert(graph->new_mapping[i - 2] < 0);
766 graph->new_mapping[i - 2] = target;
767 }
768 }
769
770 /*
771 * The new mapping may be 1 smaller than the old mapping
772 */
773 if (graph->new_mapping[graph->mapping_size - 1] < 0)
774 graph->mapping_size--;
775
776 /*
777 * Output out a line based on the new mapping info
778 */
779 for (i = 0; i < graph->mapping_size; i++) {
780 int target = graph->new_mapping[i];
781 if (target < 0)
782 strbuf_addch(sb, ' ');
783 else if (target * 2 == i)
784 strbuf_addch(sb, '|');
785 else
786 strbuf_addch(sb, '/');
787 }
788
789 graph_pad_horizontally(graph, sb);
790
791 /*
792 * Swap mapping and new_mapping
793 */
794 tmp_mapping = graph->mapping;
795 graph->mapping = graph->new_mapping;
796 graph->new_mapping = tmp_mapping;
797
798 /*
799 * If graph->mapping indicates that all of the branch lines
800 * are already in the correct positions, we are done.
801 * Otherwise, we need to collapse some branch lines together.
802 */
803 if (graph_is_mapping_correct(graph))
804 graph->state = GRAPH_PADDING;
805}
806
807int graph_next_line(struct git_graph *graph, struct strbuf *sb)
808{
809 switch (graph->state) {
810 case GRAPH_PADDING:
811 graph_output_padding_line(graph, sb);
812 return 0;
813 case GRAPH_SKIP:
814 graph_output_skip_line(graph, sb);
815 return 0;
816 case GRAPH_PRE_COMMIT:
817 graph_output_pre_commit_line(graph, sb);
818 return 0;
819 case GRAPH_COMMIT:
820 graph_output_commit_line(graph, sb);
821 return 1;
822 case GRAPH_POST_MERGE:
823 graph_output_post_merge_line(graph, sb);
824 return 0;
825 case GRAPH_COLLAPSING:
826 graph_output_collapsing_line(graph, sb);
827 return 0;
828 }
829
830 assert(0);
831 return 0;
832}
833
834void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
835{
836 int i, j;
837
838 if (graph->state != GRAPH_COMMIT) {
839 graph_next_line(graph, sb);
840 return;
841 }
842
843 /*
844 * Output the row containing this commit
845 * Iterate up to and including graph->num_columns,
846 * since the current commit may not be in any of the existing
847 * columns. (This happens when the current commit doesn't have any
848 * children that we have already processed.)
849 */
850 for (i = 0; i < graph->num_columns; i++) {
851 struct commit *col_commit = graph->columns[i].commit;
852 if (col_commit == graph->commit) {
853 strbuf_addch(sb, '|');
854
855 if (graph->num_parents < 3)
856 strbuf_addch(sb, ' ');
857 else {
858 int num_spaces = ((graph->num_parents - 2) * 2);
859 for (j = 0; j < num_spaces; j++)
860 strbuf_addch(sb, ' ');
861 }
862 } else {
863 strbuf_addstr(sb, "| ");
864 }
865 }
866
867 graph_pad_horizontally(graph, sb);
868}
869
870int graph_is_commit_finished(struct git_graph const *graph)
871{
872 return (graph->state == GRAPH_PADDING);
873}
874
875void graph_show_commit(struct git_graph *graph)
876{
877 struct strbuf msgbuf;
878 int shown_commit_line = 0;
879
880 if (!graph)
881 return;
882
883 strbuf_init(&msgbuf, 0);
884
885 while (!shown_commit_line) {
886 shown_commit_line = graph_next_line(graph, &msgbuf);
887 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
888 if (!shown_commit_line)
889 putchar('\n');
890 strbuf_setlen(&msgbuf, 0);
891 }
892
893 strbuf_release(&msgbuf);
894}
895
896void graph_show_oneline(struct git_graph *graph)
897{
898 struct strbuf msgbuf;
899
900 if (!graph)
901 return;
902
903 strbuf_init(&msgbuf, 0);
904 graph_next_line(graph, &msgbuf);
905 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
906 strbuf_release(&msgbuf);
907}
908
909void graph_show_padding(struct git_graph *graph)
910{
911 struct strbuf msgbuf;
912
913 if (!graph)
914 return;
915
916 strbuf_init(&msgbuf, 0);
917 graph_padding_line(graph, &msgbuf);
918 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
919 strbuf_release(&msgbuf);
920}
921
922int graph_show_remainder(struct git_graph *graph)
923{
924 struct strbuf msgbuf;
925 int shown = 0;
926
927 if (!graph)
928 return 0;
929
930 if (graph_is_commit_finished(graph))
931 return 0;
932
933 strbuf_init(&msgbuf, 0);
934 for (;;) {
935 graph_next_line(graph, &msgbuf);
936 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
937 strbuf_setlen(&msgbuf, 0);
938 shown = 1;
939
940 if (!graph_is_commit_finished(graph))
941 putchar('\n');
942 else
943 break;
944 }
945 strbuf_release(&msgbuf);
946
947 return shown;
948}
949
950
951void graph_show_strbuf(struct git_graph *graph, struct strbuf const *sb)
952{
953 char *p;
954
955 if (!graph) {
956 fwrite(sb->buf, sizeof(char), sb->len, stdout);
957 return;
958 }
959
960 /*
961 * Print the strbuf line by line,
962 * and display the graph info before each line but the first.
963 */
964 p = sb->buf;
965 while (p) {
966 size_t len;
967 char *next_p = strchr(p, '\n');
968 if (next_p) {
969 next_p++;
970 len = next_p - p;
971 } else {
972 len = (sb->buf + sb->len) - p;
973 }
974 fwrite(p, sizeof(char), len, stdout);
975 if (next_p && *next_p != '\0')
976 graph_show_oneline(graph);
977 p = next_p;
978 }
979}
980
981void graph_show_commit_msg(struct git_graph *graph,
982 struct strbuf const *sb)
983{
984 int newline_terminated;
985
986 if (!graph) {
987 /*
988 * If there's no graph, just print the message buffer.
989 *
990 * The message buffer for CMIT_FMT_ONELINE and
991 * CMIT_FMT_USERFORMAT are already missing a terminating
992 * newline. All of the other formats should have it.
993 */
994 fwrite(sb->buf, sizeof(char), sb->len, stdout);
995 return;
996 }
997
998 newline_terminated = (sb->len && sb->buf[sb->len - 1] == '\n');
999
1000 /*
1001 * Show the commit message
1002 */
1003 graph_show_strbuf(graph, sb);
1004
1005 /*
1006 * If there is more output needed for this commit, show it now
1007 */
1008 if (!graph_is_commit_finished(graph)) {
1009 /*
1010 * If sb doesn't have a terminating newline, print one now,
1011 * so we can start the remainder of the graph output on a
1012 * new line.
1013 */
1014 if (!newline_terminated)
1015 putchar('\n');
1016
1017 graph_show_remainder(graph);
1018
1019 /*
1020 * If sb ends with a newline, our output should too.
1021 */
1022 if (newline_terminated)
1023 putchar('\n');
1024 }
1025}