#include "cache.h"
#include "commit.h"
#include "color.h"
#include "graph.h"
#include "diff.h"
#include "revision.h"
/* Internal API */
/*
* Output the next line for a graph.
* This formats the next graph line into the specified strbuf. It is not
* terminated with a newline.
*
* Returns 1 if the line includes the current commit, and 0 otherwise.
* graph_next_line() will return 1 exactly once for each time
* graph_update() is called.
*/
static int graph_next_line(struct git_graph *graph, struct strbuf *sb);
/*
* Output a padding line in the graph.
* This is similar to graph_next_line(). However, it is guaranteed to
* never print the current commit line. Instead, if the commit line is
* next, it will simply output a line of vertical padding, extending the
* branch lines downwards, but leaving them otherwise unchanged.
*/
static void graph_padding_line(struct git_graph *graph, struct strbuf *sb);
/*
* Print a strbuf to stdout. If the graph is non-NULL, all lines but the
* first will be prefixed with the graph output.
*
* If the strbuf ends with a newline, the output will end after this
* newline. A new graph line will not be printed after the final newline.
* If the strbuf is empty, no output will be printed.
*
* Since the first line will not include the graph output, the caller is
* responsible for printing this line's graph (perhaps via
* graph_show_commit() or graph_show_oneline()) before calling
* graph_show_strbuf().
*/
static void graph_show_strbuf(struct git_graph *graph, struct strbuf const *sb);
/*
* TODO:
* - Limit the number of columns, similar to the way gitk does.
* If we reach more than a specified number of columns, omit
* sections of some columns.
*/
struct column {
/*
* The parent commit of this column.
*/
struct commit *commit;
/*
* The color to (optionally) print this column in. This is an
* index into column_colors.
*/
unsigned short color;
};
enum graph_state {
GRAPH_PADDING,
GRAPH_SKIP,
GRAPH_PRE_COMMIT,
GRAPH_COMMIT,
GRAPH_POST_MERGE,
GRAPH_COLLAPSING
};
/*
* The list of available column colors.
*/
static char column_colors[][COLOR_MAXLEN] = {
GIT_COLOR_RED,
GIT_COLOR_GREEN,
GIT_COLOR_YELLOW,
GIT_COLOR_BLUE,
GIT_COLOR_MAGENTA,
GIT_COLOR_CYAN,
GIT_COLOR_BOLD_RED,
GIT_COLOR_BOLD_GREEN,
GIT_COLOR_BOLD_YELLOW,
GIT_COLOR_BOLD_BLUE,
GIT_COLOR_BOLD_MAGENTA,
GIT_COLOR_BOLD_CYAN,
};
#define COLUMN_COLORS_MAX (ARRAY_SIZE(column_colors))
static const char *column_get_color_code(const struct column *c)
{
return column_colors[c->color];
}
static void strbuf_write_column(struct strbuf *sb, const struct column *c,
char col_char)
{
if (c->color < COLUMN_COLORS_MAX)
strbuf_addstr(sb, column_get_color_code(c));
strbuf_addch(sb, col_char);
if (c->color < COLUMN_COLORS_MAX)
strbuf_addstr(sb, GIT_COLOR_RESET);
}
struct git_graph {
/*
* The commit currently being processed
*/
struct commit *commit;
/* The rev-info used for the current traversal */
struct rev_info *revs;
/*
* The number of interesting parents that this commit has.
*
* Note that this is not the same as the actual number of parents.
* This count excludes parents that won't be printed in the graph
* output, as determined by graph_is_interesting().
*/
int num_parents;
/*
* The width of the graph output for this commit.
* All rows for this commit are padded to this width, so that
* messages printed after the graph output are aligned.
*/
int width;
/*
* The next expansion row to print
* when state is GRAPH_PRE_COMMIT
*/
int expansion_row;
/*
* The current output state.
* This tells us what kind of line graph_next_line() should output.
|