Add history graph API
[git/spearce.git] / graph.c
blob616e18b13f496cd97bce5333b6c7655ae3cfa2f0
1 #include "cache.h"
2 #include "commit.h"
3 #include "graph.h"
4 #include "diff.h"
5 #include "revision.h"
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.
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.
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 * | | | |
28 * If there are several parallel diagonal lines, they will need to be
29 * replaced with horizontal lines on subsequent rows.
32 struct column {
34 * The parent commit of this column.
36 struct commit *commit;
38 * XXX: Once we add support for colors, struct column could also
39 * contain the color of its branch line.
43 enum graph_state {
44 GRAPH_PADDING,
45 GRAPH_SKIP,
46 GRAPH_PRE_COMMIT,
47 GRAPH_COMMIT,
48 GRAPH_POST_MERGE,
49 GRAPH_COLLAPSING
52 struct git_graph {
54 * The commit currently being processed
56 struct commit *commit;
58 * The number of parents this commit has.
59 * (Stored so we don't have to walk over them each time we need
60 * this number)
62 int num_parents;
64 * The next expansion row to print
65 * when state is GRAPH_PRE_COMMIT
67 int expansion_row;
69 * The current output state.
70 * This tells us what kind of line graph_next_line() should output.
72 enum graph_state state;
74 * The maximum number of columns that can be stored in the columns
75 * and new_columns arrays. This is also half the number of entries
76 * that can be stored in the mapping and new_mapping arrays.
78 int column_capacity;
80 * The number of columns (also called "branch lines" in some places)
82 int num_columns;
84 * The number of columns in the new_columns array
86 int num_new_columns;
88 * The number of entries in the mapping array
90 int mapping_size;
92 * The column state before we output the current commit.
94 struct column *columns;
96 * The new column state after we output the current commit.
97 * Only valid when state is GRAPH_COLLAPSING.
99 struct column *new_columns;
101 * An array that tracks the current state of each
102 * character in the output line during state GRAPH_COLLAPSING.
103 * Each entry is -1 if this character is empty, or a non-negative
104 * integer if the character contains a branch line. The value of
105 * the integer indicates the target position for this branch line.
106 * (I.e., this array maps the current column positions to their
107 * desired positions.)
109 * The maximum capacity of this array is always
110 * sizeof(int) * 2 * column_capacity.
112 int *mapping;
114 * A temporary array for computing the next mapping state
115 * while we are outputting a mapping line. This is stored as part
116 * of the git_graph simply so we don't have to allocate a new
117 * temporary array each time we have to output a collapsing line.
119 int *new_mapping;
122 struct git_graph *graph_init(void)
124 struct git_graph *graph = xmalloc(sizeof(struct git_graph));
125 graph->commit = NULL;
126 graph->num_parents = 0;
127 graph->expansion_row = 0;
128 graph->state = GRAPH_PADDING;
129 graph->num_columns = 0;
130 graph->num_new_columns = 0;
131 graph->mapping_size = 0;
134 * Allocate a reasonably large default number of columns
135 * We'll automatically grow columns later if we need more room.
137 graph->column_capacity = 30;
138 graph->columns = xmalloc(sizeof(struct column) *
139 graph->column_capacity);
140 graph->new_columns = xmalloc(sizeof(struct column) *
141 graph->column_capacity);
142 graph->mapping = xmalloc(sizeof(int) * 2 * graph->column_capacity);
143 graph->new_mapping = xmalloc(sizeof(int) * 2 * graph->column_capacity);
145 return graph;
148 void graph_release(struct git_graph *graph)
150 free(graph->columns);
151 free(graph->new_columns);
152 free(graph->mapping);
153 free(graph);
156 static void graph_ensure_capacity(struct git_graph *graph, int num_columns)
158 if (graph->column_capacity >= num_columns)
159 return;
161 do {
162 graph->column_capacity *= 2;
163 } while (graph->column_capacity < num_columns);
165 graph->columns = xrealloc(graph->columns,
166 sizeof(struct column) *
167 graph->column_capacity);
168 graph->new_columns = xrealloc(graph->new_columns,
169 sizeof(struct column) *
170 graph->column_capacity);
171 graph->mapping = xrealloc(graph->mapping,
172 sizeof(int) * 2 * graph->column_capacity);
173 graph->new_mapping = xrealloc(graph->new_mapping,
174 sizeof(int) * 2 * graph->column_capacity);
177 static void graph_insert_into_new_columns(struct git_graph *graph,
178 struct commit *commit,
179 int *mapping_index)
181 int i;
184 * Ignore uinteresting and pruned commits
186 if (commit->object.flags & (UNINTERESTING | TREESAME))
187 return;
190 * If the commit is already in the new_columns list, we don't need to
191 * add it. Just update the mapping correctly.
193 for (i = 0; i < graph->num_new_columns; i++) {
194 if (graph->new_columns[i].commit == commit) {
195 graph->mapping[*mapping_index] = i;
196 *mapping_index += 2;
197 return;
202 * This commit isn't already in new_columns. Add it.
204 graph->new_columns[graph->num_new_columns].commit = commit;
205 graph->mapping[*mapping_index] = graph->num_new_columns;
206 *mapping_index += 2;
207 graph->num_new_columns++;
210 static void graph_update_columns(struct git_graph *graph)
212 struct commit_list *parent;
213 struct column *tmp_columns;
214 int max_new_columns;
215 int mapping_idx;
216 int i, seen_this;
219 * Swap graph->columns with graph->new_columns
220 * graph->columns contains the state for the previous commit,
221 * and new_columns now contains the state for our commit.
223 * We'll re-use the old columns array as storage to compute the new
224 * columns list for the commit after this one.
226 tmp_columns = graph->columns;
227 graph->columns = graph->new_columns;
228 graph->num_columns = graph->num_new_columns;
230 graph->new_columns = tmp_columns;
231 graph->num_new_columns = 0;
234 * Now update new_columns and mapping with the information for the
235 * commit after this one.
237 * First, make sure we have enough room. At most, there will
238 * be graph->num_columns + graph->num_parents columns for the next
239 * commit.
241 max_new_columns = graph->num_columns + graph->num_parents;
242 graph_ensure_capacity(graph, max_new_columns);
245 * Clear out graph->mapping
247 graph->mapping_size = 2 * max_new_columns;
248 for (i = 0; i < graph->mapping_size; i++)
249 graph->mapping[i] = -1;
252 * Populate graph->new_columns and graph->mapping
254 * Some of the parents of this commit may already be in
255 * graph->columns. If so, graph->new_columns should only contain a
256 * single entry for each such commit. graph->mapping should
257 * contain information about where each current branch line is
258 * supposed to end up after the collapsing is performed.
260 seen_this = 0;
261 mapping_idx = 0;
262 for (i = 0; i <= graph->num_columns; i++) {
263 struct commit *col_commit;
264 if (i == graph->num_columns) {
265 if (seen_this)
266 break;
267 col_commit = graph->commit;
268 } else {
269 col_commit = graph->columns[i].commit;
272 if (col_commit == graph->commit) {
273 seen_this = 1;
274 for (parent = graph->commit->parents;
275 parent;
276 parent = parent->next) {
277 graph_insert_into_new_columns(graph,
278 parent->item,
279 &mapping_idx);
281 } else {
282 graph_insert_into_new_columns(graph, col_commit,
283 &mapping_idx);
288 * Shrink mapping_size to be the minimum necessary
290 while (graph->mapping_size > 1 &&
291 graph->mapping[graph->mapping_size - 1] < 0)
292 graph->mapping_size--;
295 void graph_update(struct git_graph *graph, struct commit *commit)
297 struct commit_list *parent;
300 * Set the new commit
302 graph->commit = commit;
305 * Count how many parents this commit has
307 graph->num_parents = 0;
308 for (parent = commit->parents; parent; parent = parent->next)
309 graph->num_parents++;
312 * Call graph_update_columns() to update
313 * columns, new_columns, and mapping.
315 graph_update_columns(graph);
317 graph->expansion_row = 0;
320 * Update graph->state.
322 * If the previous commit didn't get to the GRAPH_PADDING state,
323 * it never finished its output. Goto GRAPH_SKIP, to print out
324 * a line to indicate that portion of the graph is missing.
326 * Otherwise, if there are 3 or more parents, we need to print
327 * extra rows before the commit, to expand the branch lines around
328 * it and make room for it.
330 * If there are less than 3 parents, we can immediately print the
331 * commit line.
333 if (graph->state != GRAPH_PADDING)
334 graph->state = GRAPH_SKIP;
335 else if (graph->num_parents >= 3)
336 graph->state = GRAPH_PRE_COMMIT;
337 else
338 graph->state = GRAPH_COMMIT;
341 static int graph_is_mapping_correct(struct git_graph *graph)
343 int i;
346 * The mapping is up to date if each entry is at its target,
347 * or is 1 greater than its target.
348 * (If it is 1 greater than the target, '/' will be printed, so it
349 * will look correct on the next row.)
351 for (i = 0; i < graph->mapping_size; i++) {
352 int target = graph->mapping[i];
353 if (target < 0)
354 continue;
355 if (target == (i / 2))
356 continue;
357 return 0;
360 return 1;
363 static void graph_pad_horizontally(struct git_graph *graph, struct strbuf *sb)
366 * Add additional spaces to the end of the strbuf, so that all
367 * lines for a particular commit have the same width.
369 * This way, fields printed to the right of the graph will remain
370 * aligned for the entire commit.
372 * This computation results in 3 extra space to the right in most
373 * cases, but only 1 extra space if the commit doesn't have any
374 * children that have already been displayed in the graph (i.e.,
375 * if the current commit isn't in graph->columns).
377 size_t extra;
378 size_t final_width = graph->num_columns + graph->num_parents;
379 if (graph->num_parents < 1)
380 final_width++;
381 final_width *= 2;
383 if (sb->len >= final_width)
384 return;
386 extra = final_width - sb->len;
387 strbuf_addf(sb, "%*s", (int) extra, "");
390 static void graph_output_padding_line(struct git_graph *graph,
391 struct strbuf *sb)
393 int i;
396 * We could conceivable be called with a NULL commit
397 * if our caller has a bug, and invokes graph_next_line()
398 * immediately after graph_init(), without first calling
399 * graph_update(). Return without outputting anything in this
400 * case.
402 if (!graph->commit)
403 return;
406 * Output a padding row, that leaves all branch lines unchanged
408 for (i = 0; i < graph->num_new_columns; i++) {
409 strbuf_addstr(sb, "| ");
412 graph_pad_horizontally(graph, sb);
415 static void graph_output_skip_line(struct git_graph *graph, struct strbuf *sb)
418 * Output an ellipsis to indicate that a portion
419 * of the graph is missing.
421 strbuf_addstr(sb, "...");
422 graph_pad_horizontally(graph, sb);
424 if (graph->num_parents >= 3)
425 graph->state = GRAPH_PRE_COMMIT;
426 else
427 graph->state = GRAPH_COMMIT;
430 static void graph_output_pre_commit_line(struct git_graph *graph,
431 struct strbuf *sb)
433 int num_expansion_rows;
434 int i, seen_this;
437 * This function formats a row that increases the space around a commit
438 * with multiple parents, to make room for it. It should only be
439 * called when there are 3 or more parents.
441 * We need 2 extra rows for every parent over 2.
443 assert(graph->num_parents >= 3);
444 num_expansion_rows = (graph->num_parents - 2) * 2;
447 * graph->expansion_row tracks the current expansion row we are on.
448 * It should be in the range [0, num_expansion_rows - 1]
450 assert(0 <= graph->expansion_row &&
451 graph->expansion_row < num_expansion_rows);
454 * Output the row
456 seen_this = 0;
457 for (i = 0; i < graph->num_columns; i++) {
458 struct column *col = &graph->columns[i];
459 if (col->commit == graph->commit) {
460 seen_this = 1;
461 strbuf_addf(sb, "| %*s", graph->expansion_row, "");
462 } else if (seen_this) {
463 strbuf_addstr(sb, "\\ ");
464 } else {
465 strbuf_addstr(sb, "| ");
469 graph_pad_horizontally(graph, sb);
472 * Increment graph->expansion_row,
473 * and move to state GRAPH_COMMIT if necessary
475 graph->expansion_row++;
476 if (graph->expansion_row >= num_expansion_rows)
477 graph->state = GRAPH_COMMIT;
480 void graph_output_commit_line(struct git_graph *graph, struct strbuf *sb)
482 int seen_this = 0;
483 int i, j;
486 * Output the row containing this commit
487 * Iterate up to and including graph->num_columns,
488 * since the current commit may not be in any of the existing
489 * columns. (This happens when the current commit doesn't have any
490 * children that we have already processed.)
492 seen_this = 0;
493 for (i = 0; i <= graph->num_columns; i++) {
494 struct commit *col_commit;
495 if (i == graph->num_columns) {
496 if (seen_this)
497 break;
498 col_commit = graph->commit;
499 } else {
500 col_commit = graph->columns[i].commit;
503 if (col_commit == graph->commit) {
504 seen_this = 1;
505 if (graph->num_parents > 1)
506 strbuf_addch(sb, 'M');
507 else
508 strbuf_addch(sb, '*');
510 if (graph->num_parents < 2)
511 strbuf_addch(sb, ' ');
512 else if (graph->num_parents == 2)
513 strbuf_addstr(sb, " ");
514 else {
515 int num_dashes =
516 ((graph->num_parents - 2) * 2) - 1;
517 for (j = 0; j < num_dashes; j++)
518 strbuf_addch(sb, '-');
519 strbuf_addstr(sb, ". ");
521 } else if (seen_this && (graph->num_parents > 1)) {
522 strbuf_addstr(sb, "\\ ");
523 } else {
524 strbuf_addstr(sb, "| ");
528 graph_pad_horizontally(graph, sb);
531 * Update graph->state
533 if (graph->num_parents > 1)
534 graph->state = GRAPH_POST_MERGE;
535 else if (graph_is_mapping_correct(graph))
536 graph->state = GRAPH_PADDING;
537 else
538 graph->state = GRAPH_COLLAPSING;
541 void graph_output_post_merge_line(struct git_graph *graph, struct strbuf *sb)
543 int seen_this = 0;
544 int i, j;
547 * Output the post-merge row
549 for (i = 0; i <= graph->num_columns; i++) {
550 struct commit *col_commit;
551 if (i == graph->num_columns) {
552 if (seen_this)
553 break;
554 col_commit = graph->commit;
555 } else {
556 col_commit = graph->columns[i].commit;
559 if (col_commit == graph->commit) {
560 seen_this = 1;
561 strbuf_addch(sb, '|');
562 for (j = 0; j < graph->num_parents - 1; j++)
563 strbuf_addstr(sb, "\\ ");
564 if (graph->num_parents == 2)
565 strbuf_addch(sb, ' ');
566 } else if (seen_this && (graph->num_parents > 2)) {
567 strbuf_addstr(sb, "\\ ");
568 } else {
569 strbuf_addstr(sb, "| ");
573 graph_pad_horizontally(graph, sb);
576 * Update graph->state
578 if (graph_is_mapping_correct(graph))
579 graph->state = GRAPH_PADDING;
580 else
581 graph->state = GRAPH_COLLAPSING;
584 void graph_output_collapsing_line(struct git_graph *graph, struct strbuf *sb)
586 int i;
587 int *tmp_mapping;
590 * Clear out the new_mapping array
592 for (i = 0; i < graph->mapping_size; i++)
593 graph->new_mapping[i] = -1;
595 for (i = 0; i < graph->mapping_size; i++) {
596 int target = graph->mapping[i];
597 if (target < 0)
598 continue;
601 * Since update_columns() always inserts the leftmost
602 * column first, each branch's target location should
603 * always be either its current location or to the left of
604 * its current location.
606 * We never have to move branches to the right. This makes
607 * the graph much more legible, since whenever branches
608 * cross, only one is moving directions.
610 assert(target * 2 <= i);
612 if (target * 2 == i) {
614 * This column is already in the
615 * correct place
617 assert(graph->new_mapping[i] == -1);
618 graph->new_mapping[i] = target;
619 } else if (graph->new_mapping[i - 1] < 0) {
621 * Nothing is to the left.
622 * Move to the left by one
624 graph->new_mapping[i - 1] = target;
625 } else if (graph->new_mapping[i - 1] == target) {
627 * There is a branch line to our left
628 * already, and it is our target. We
629 * combine with this line, since we share
630 * the same parent commit.
632 * We don't have to add anything to the
633 * output or new_mapping, since the
634 * existing branch line has already taken
635 * care of it.
637 } else {
639 * There is a branch line to our left,
640 * but it isn't our target. We need to
641 * cross over it.
643 * The space just to the left of this
644 * branch should always be empty.
646 assert(graph->new_mapping[i - 1] > target);
647 assert(graph->new_mapping[i - 2] < 0);
648 graph->new_mapping[i - 2] = target;
653 * The new mapping may be 1 smaller than the old mapping
655 if (graph->new_mapping[graph->mapping_size - 1] < 0)
656 graph->mapping_size--;
659 * Output out a line based on the new mapping info
661 for (i = 0; i < graph->mapping_size; i++) {
662 int target = graph->new_mapping[i];
663 if (target < 0)
664 strbuf_addch(sb, ' ');
665 else if (target * 2 == i)
666 strbuf_addch(sb, '|');
667 else
668 strbuf_addch(sb, '/');
671 graph_pad_horizontally(graph, sb);
674 * Swap mapping and new_mapping
676 tmp_mapping = graph->mapping;
677 graph->mapping = graph->new_mapping;
678 graph->new_mapping = tmp_mapping;
681 * If graph->mapping indicates that all of the branch lines
682 * are already in the correct positions, we are done.
683 * Otherwise, we need to collapse some branch lines together.
685 if (graph_is_mapping_correct(graph))
686 graph->state = GRAPH_PADDING;
689 int graph_next_line(struct git_graph *graph, struct strbuf *sb)
691 switch (graph->state) {
692 case GRAPH_PADDING:
693 graph_output_padding_line(graph, sb);
694 return 0;
695 case GRAPH_SKIP:
696 graph_output_skip_line(graph, sb);
697 return 0;
698 case GRAPH_PRE_COMMIT:
699 graph_output_pre_commit_line(graph, sb);
700 return 0;
701 case GRAPH_COMMIT:
702 graph_output_commit_line(graph, sb);
703 return 1;
704 case GRAPH_POST_MERGE:
705 graph_output_post_merge_line(graph, sb);
706 return 0;
707 case GRAPH_COLLAPSING:
708 graph_output_collapsing_line(graph, sb);
709 return 0;
712 assert(0);
713 return 0;
716 void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
718 int i, j;
720 if (graph->state != GRAPH_COMMIT) {
721 graph_next_line(graph, sb);
722 return;
726 * Output the row containing this commit
727 * Iterate up to and including graph->num_columns,
728 * since the current commit may not be in any of the existing
729 * columns. (This happens when the current commit doesn't have any
730 * children that we have already processed.)
732 for (i = 0; i < graph->num_columns; i++) {
733 struct commit *col_commit = graph->columns[i].commit;
734 if (col_commit == graph->commit) {
735 strbuf_addch(sb, '|');
737 if (graph->num_parents < 3)
738 strbuf_addch(sb, ' ');
739 else {
740 int num_spaces = ((graph->num_parents - 2) * 2);
741 for (j = 0; j < num_spaces; j++)
742 strbuf_addch(sb, ' ');
744 } else {
745 strbuf_addstr(sb, "| ");
749 graph_pad_horizontally(graph, sb);
752 int graph_is_commit_finished(struct git_graph const *graph)
754 return (graph->state == GRAPH_PADDING);
757 void graph_show_commit(struct git_graph *graph)
759 struct strbuf msgbuf;
760 int shown_commit_line = 0;
762 if (!graph)
763 return;
765 strbuf_init(&msgbuf, 0);
767 while (!shown_commit_line) {
768 shown_commit_line = graph_next_line(graph, &msgbuf);
769 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
770 if (!shown_commit_line)
771 putchar('\n');
772 strbuf_setlen(&msgbuf, 0);
775 strbuf_release(&msgbuf);
778 void graph_show_oneline(struct git_graph *graph)
780 struct strbuf msgbuf;
782 if (!graph)
783 return;
785 strbuf_init(&msgbuf, 0);
786 graph_next_line(graph, &msgbuf);
787 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
788 strbuf_release(&msgbuf);
791 void graph_show_padding(struct git_graph *graph)
793 struct strbuf msgbuf;
795 if (!graph)
796 return;
798 strbuf_init(&msgbuf, 0);
799 graph_padding_line(graph, &msgbuf);
800 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
801 strbuf_release(&msgbuf);
804 int graph_show_remainder(struct git_graph *graph)
806 struct strbuf msgbuf;
807 int shown = 0;
809 if (!graph)
810 return 0;
812 if (graph_is_commit_finished(graph))
813 return 0;
815 strbuf_init(&msgbuf, 0);
816 for (;;) {
817 graph_next_line(graph, &msgbuf);
818 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
819 strbuf_setlen(&msgbuf, 0);
820 shown = 1;
822 if (!graph_is_commit_finished(graph))
823 putchar('\n');
824 else
825 break;
827 strbuf_release(&msgbuf);
829 return shown;
833 void graph_show_strbuf(struct git_graph *graph, struct strbuf const *sb)
835 char *p;
837 if (!graph) {
838 fwrite(sb->buf, sizeof(char), sb->len, stdout);
839 return;
843 * Print the strbuf line by line,
844 * and display the graph info before each line but the first.
846 p = sb->buf;
847 while (p) {
848 size_t len;
849 char *next_p = strchr(p, '\n');
850 if (next_p) {
851 next_p++;
852 len = next_p - p;
853 } else {
854 len = (sb->buf + sb->len) - p;
856 fwrite(p, sizeof(char), len, stdout);
857 if (next_p && *next_p != '\0')
858 graph_show_oneline(graph);
859 p = next_p;
863 void graph_show_commit_msg(struct git_graph *graph,
864 struct strbuf const *sb)
866 int newline_terminated;
868 if (!graph) {
870 * If there's no graph, just print the message buffer.
872 * The message buffer for CMIT_FMT_ONELINE and
873 * CMIT_FMT_USERFORMAT are already missing a terminating
874 * newline. All of the other formats should have it.
876 fwrite(sb->buf, sizeof(char), sb->len, stdout);
877 return;
880 newline_terminated = (sb->len && sb->buf[sb->len - 1] == '\n');
883 * Show the commit message
885 graph_show_strbuf(graph, sb);
888 * If there is more output needed for this commit, show it now
890 if (!graph_is_commit_finished(graph)) {
892 * If sb doesn't have a terminating newline, print one now,
893 * so we can start the remainder of the graph output on a
894 * new line.
896 if (!newline_terminated)
897 putchar('\n');
899 graph_show_remainder(graph);
902 * If sb ends with a newline, our output should too.
904 if (newline_terminated)
905 putchar('\n');