Merge branch 'ps/graph-lane-limit' into seen

The graph output from commands like "git log --graph" can now be
limited to a specified number of lanes, preventing overly wide output
in repositories with many branches.

* ps/graph-lane-limit:
  graph: add truncation mark to capped lanes
  graph: add --graph-lane-limit option
  graph: limit the graph width to a hard-coded max
This commit is contained in:
Junio C Hamano
2026-04-09 15:11:46 -07:00
5 changed files with 311 additions and 24 deletions

View File

@@ -1259,6 +1259,12 @@ This implies the `--topo-order` option by default, but the
in between them in that case. If _<barrier>_ is specified, it
is the string that will be shown instead of the default one.
`--graph-lane-limit=<n>`::
When `--graph` is used, limit the number of graph lanes to be shown.
Lanes over the limit are replaced with a truncation mark '~'.
By default it is set to 0 (no limit), zero and negative values
are ignored and treated as no limit.
ifdef::git-rev-list[]
`--count`::
Print a number stating how many commits would have been

178
graph.c
View File

@@ -317,6 +317,15 @@ struct git_graph {
struct strbuf prefix_buf;
};
static inline int graph_needs_truncation(struct git_graph *graph, int lane)
{
int max = graph->revs->graph_max_lanes;
/*
* Ignore values <= 0, meaning no limit.
*/
return max > 0 && lane >= max;
}
static const char *diff_output_prefix_callback(struct diff_options *opt, void *data)
{
struct git_graph *graph = data;
@@ -696,6 +705,19 @@ static void graph_update_columns(struct git_graph *graph)
}
}
/*
* If graph_max_lanes is set, cap the width
*/
if (graph->revs->graph_max_lanes > 0) {
/*
* width of "| " per lanes plus truncation mark "~ ".
* Allow commits from merges to align to the merged lane.
*/
int max_width = graph->revs->graph_max_lanes * 2 + 2;
if (graph->width > max_width)
graph->width = max_width;
}
/*
* Shrink mapping_size to be the minimum necessary
*/
@@ -846,6 +868,10 @@ static void graph_output_padding_line(struct git_graph *graph,
* Output a padding row, that leaves all branch lines unchanged
*/
for (i = 0; i < graph->num_new_columns; i++) {
if (graph_needs_truncation(graph, i)) {
graph_line_addstr(line, "~ ");
break;
}
graph_line_write_column(line, &graph->new_columns[i], '|');
graph_line_addch(line, ' ');
}
@@ -903,6 +929,9 @@ static void graph_output_pre_commit_line(struct git_graph *graph,
seen_this = 1;
graph_line_write_column(line, col, '|');
graph_line_addchars(line, ' ', graph->expansion_row);
} else if (seen_this && graph_needs_truncation(graph, i)) {
graph_line_addstr(line, "~ ");
break;
} else if (seen_this && (graph->expansion_row == 0)) {
/*
* This is the first line of the pre-commit output.
@@ -994,6 +1023,16 @@ static void graph_draw_octopus_merge(struct git_graph *graph, struct graph_line
col = &graph->new_columns[j];
graph_line_write_column(line, col, '-');
/*
* Commit is at commit_index, each iteration move one lane to
* the right from the commit.
*/
if (graph_needs_truncation(graph, graph->commit_index + 1 + i)) {
graph_line_addstr(line, "~ ");
break;
}
graph_line_write_column(line, col, (i == dashed_parents - 1) ? '.' : '-');
}
@@ -1028,8 +1067,17 @@ static void graph_output_commit_line(struct git_graph *graph, struct graph_line
seen_this = 1;
graph_output_commit_char(graph, line);
if (graph_needs_truncation(graph, i)) {
graph_line_addch(line, ' ');
break;
}
if (graph->num_parents > 2)
graph_draw_octopus_merge(graph, line);
} else if (graph_needs_truncation(graph, i)) {
graph_line_addstr(line, "~ ");
seen_this = 1;
break;
} else if (seen_this && (graph->edges_added > 1)) {
graph_line_write_column(line, col, '\\');
} else if (seen_this && (graph->edges_added == 1)) {
@@ -1065,13 +1113,46 @@ static void graph_output_commit_line(struct git_graph *graph, struct graph_line
/*
* Update graph->state
*
* If the commit is a merge and the first parent is in a visible lane,
* then the GRAPH_POST_MERGE is needed to draw the merge lane.
*
* If the commit is over the truncation limit, but the first parent is on
* a visible lane, then we still need the merge lane but truncated.
*
* If both commit and first parent are over the truncation limit, then
* there's no need to draw the merge lane because it would work as a
* padding lane.
*/
if (graph->num_parents > 1)
graph_update_state(graph, GRAPH_POST_MERGE);
else if (graph_is_mapping_correct(graph))
if (graph->num_parents > 1) {
if (!graph_needs_truncation(graph, graph->commit_index)) {
graph_update_state(graph, GRAPH_POST_MERGE);
} else {
struct commit_list *p = first_interesting_parent(graph);
int lane;
/*
* graph->num_parents are found using first_interesting_parent
* and next_interesting_parent so it can't be a scenario
* where num_parents > 1 and there are no interesting parents
*/
if (!p)
BUG("num_parents > 1 but no interesting parent");
lane = graph_find_new_column_by_commit(graph, p->item);
if (!graph_needs_truncation(graph, lane))
graph_update_state(graph, GRAPH_POST_MERGE);
else if (graph_is_mapping_correct(graph))
graph_update_state(graph, GRAPH_PADDING);
else
graph_update_state(graph, GRAPH_COLLAPSING);
}
} else if (graph_is_mapping_correct(graph)) {
graph_update_state(graph, GRAPH_PADDING);
else
} else {
graph_update_state(graph, GRAPH_COLLAPSING);
}
}
static const char merge_chars[] = {'/', '|', '\\'};
@@ -1109,6 +1190,7 @@ static void graph_output_post_merge_line(struct git_graph *graph, struct graph_l
int par_column;
int idx = graph->merge_layout;
char c;
int truncated = 0;
seen_this = 1;
for (j = 0; j < graph->num_parents; j++) {
@@ -1117,23 +1199,56 @@ static void graph_output_post_merge_line(struct git_graph *graph, struct graph_l
c = merge_chars[idx];
graph_line_write_column(line, &graph->new_columns[par_column], c);
/*
* j counts parents, it needs to be halved to be
* comparable with i. Don't truncate if there are
* no more lanes to print (end of the lane)
*/
if (graph_needs_truncation(graph, j / 2 + i) &&
j / 2 + i <= graph->num_columns) {
if ((j + i * 2) % 2 != 0)
graph_line_addch(line, ' ');
graph_line_addstr(line, "~ ");
truncated = 1;
break;
}
if (idx == 2) {
if (graph->edges_added > 0 || j < graph->num_parents - 1)
/*
* Check if the next lane needs truncation
* to avoid having the padding doubled
*/
if (graph_needs_truncation(graph, (j + 1) / 2 + i) &&
j < graph->num_parents - 1) {
graph_line_addstr(line, "~ ");
truncated = 1;
break;
} else if (graph->edges_added > 0 || j < graph->num_parents - 1)
graph_line_addch(line, ' ');
} else {
idx++;
}
parents = next_interesting_parent(graph, parents);
}
if (truncated)
break;
if (graph->edges_added == 0)
graph_line_addch(line, ' ');
} else if (graph_needs_truncation(graph, i)) {
graph_line_addstr(line, "~ ");
break;
} else if (seen_this) {
if (graph->edges_added > 0)
graph_line_write_column(line, col, '\\');
else
graph_line_write_column(line, col, '|');
graph_line_addch(line, ' ');
/*
* If it's between two lanes and next would be truncated,
* don't add space padding.
*/
if (!graph_needs_truncation(graph, i + 1))
graph_line_addch(line, ' ');
} else {
graph_line_write_column(line, col, '|');
if (graph->merge_layout != 0 || i != graph->commit_index - 1) {
@@ -1164,6 +1279,7 @@ static void graph_output_collapsing_line(struct git_graph *graph, struct graph_l
short used_horizontal = 0;
int horizontal_edge = -1;
int horizontal_edge_target = -1;
int truncated = 0;
/*
* Swap the mapping and old_mapping arrays
@@ -1279,26 +1395,35 @@ static void graph_output_collapsing_line(struct git_graph *graph, struct graph_l
*/
for (i = 0; i < graph->mapping_size; i++) {
int target = graph->mapping[i];
if (target < 0)
graph_line_addch(line, ' ');
else if (target * 2 == i)
graph_line_write_column(line, &graph->new_columns[target], '|');
else if (target == horizontal_edge_target &&
i != horizontal_edge - 1) {
/*
* Set the mappings for all but the
* first segment to -1 so that they
* won't continue into the next line.
*/
if (i != (target * 2)+3)
graph->mapping[i] = -1;
used_horizontal = 1;
graph_line_write_column(line, &graph->new_columns[target], '_');
if (!truncated && graph_needs_truncation(graph, i / 2)) {
graph_line_addstr(line, "~ ");
truncated = 1;
}
if (target < 0) {
if (!truncated)
graph_line_addch(line, ' ');
} else if (target * 2 == i) {
if (!truncated)
graph_line_write_column(line, &graph->new_columns[target], '|');
} else if (target == horizontal_edge_target &&
i != horizontal_edge - 1) {
/*
* Set the mappings for all but the
* first segment to -1 so that they
* won't continue into the next line.
*/
if (i != (target * 2)+3)
graph->mapping[i] = -1;
used_horizontal = 1;
if (!truncated)
graph_line_write_column(line, &graph->new_columns[target], '_');
} else {
if (used_horizontal && i < horizontal_edge)
graph->mapping[i] = -1;
graph_line_write_column(line, &graph->new_columns[target], '/');
if (!truncated)
graph_line_write_column(line, &graph->new_columns[target], '/');
}
}
@@ -1372,6 +1497,11 @@ static void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
for (i = 0; i < graph->num_columns; i++) {
struct column *col = &graph->columns[i];
if (graph_needs_truncation(graph, i)) {
graph_line_addstr(&line, "~ ");
break;
}
graph_line_write_column(&line, col, '|');
if (col->commit == graph->commit && graph->num_parents > 2) {

View File

@@ -2606,6 +2606,8 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
} else if (!strcmp(arg, "--no-graph")) {
graph_clear(revs->graph);
revs->graph = NULL;
} else if (skip_prefix(arg, "--graph-lane-limit=", &optarg)) {
revs->graph_max_lanes = parse_count(optarg);
} else if (!strcmp(arg, "--encode-email-headers")) {
revs->encode_email_headers = 1;
} else if (!strcmp(arg, "--no-encode-email-headers")) {
@@ -3175,6 +3177,10 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
if (revs->no_walk && revs->graph)
die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
if (revs->graph_max_lanes > 0 && !revs->graph)
die(_("the option '%s' requires '%s'"), "--graph-lane-limit", "--graph");
if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");

View File

@@ -305,6 +305,7 @@ struct rev_info {
/* Display history graph */
struct git_graph *graph;
int graph_max_lanes;
/* special limits */
int skip_count;

View File

@@ -370,4 +370,148 @@ test_expect_success 'log --graph with multiple tips' '
EOF
'
test_expect_success 'log --graph --graph-lane-limit=2 limited to two lanes' '
check_graph --graph-lane-limit=2 M_7 <<-\EOF
*-. 7_M4
|\ \
| | * 7_G
| | * 7_F
| * ~ 7_E
| * ~ 7_D
* | ~ 7_C
| |/
|/|
* | 7_B
|/
* 7_A
EOF
'
test_expect_success 'log --graph --graph-lane-limit=1 truncate mid octopus merge' '
check_graph --graph-lane-limit=1 M_7 <<-\EOF
*-~ 7_M4
|\~
| ~ 7_G
| ~ 7_F
| * 7_E
| * 7_D
* ~ 7_C
| ~
|/~
* ~ 7_B
|/
* 7_A
EOF
'
test_expect_success 'log --graph --graph-lane-limit=3 limited to three lanes' '
check_graph --graph-lane-limit=3 M_1 M_3 M_5 M_7 <<-\EOF
* 7_M1
|\
| | * 7_M2
| | |\
| | | * 7_H
| | | ~ 7_M3
| | | ~ 7_J
| | | ~ 7_I
| | | ~ 7_M4
| |_|_~
|/| | ~
| | |_~
| |/| ~
| | | ~
| | |/~
| | * ~ 7_G
| | | ~
| | |/~
| | * ~ 7_F
| * | ~ 7_E
| | |/~
| |/| ~
| * | ~ 7_D
| | |/
| |/|
* | | 7_C
| |/
|/|
* | 7_B
|/
* 7_A
EOF
'
test_expect_success 'log --graph --graph-lane-limit=6 check if it only shows first of 3 parent merge' '
check_graph --graph-lane-limit=6 M_1 M_3 M_5 M_7 <<-\EOF
* 7_M1
|\
| | * 7_M2
| | |\
| | | * 7_H
| | | | * 7_M3
| | | | |\
| | | | | * 7_J
| | | | * | 7_I
| | | | | | * 7_M4
| |_|_|_|_|/~
|/| | | | |/~
| | |_|_|/| ~
| |/| | | |/
| | | |_|/|
| | |/| | |
| | * | | | 7_G
| | | |_|/
| | |/| |
| | * | | 7_F
| * | | | 7_E
| | |/ /
| |/| |
| * | | 7_D
| | |/
| |/|
* | | 7_C
| |/
|/|
* | 7_B
|/
* 7_A
EOF
'
test_expect_success 'log --graph --graph-lane-limit=7 check if it shows all 3 parent merge' '
check_graph --graph-lane-limit=7 M_1 M_3 M_5 M_7 <<-\EOF
* 7_M1
|\
| | * 7_M2
| | |\
| | | * 7_H
| | | | * 7_M3
| | | | |\
| | | | | * 7_J
| | | | * | 7_I
| | | | | | * 7_M4
| |_|_|_|_|/|\
|/| | | | |/ /
| | |_|_|/| /
| |/| | | |/
| | | |_|/|
| | |/| | |
| | * | | | 7_G
| | | |_|/
| | |/| |
| | * | | 7_F
| * | | | 7_E
| | |/ /
| |/| |
| * | | 7_D
| | |/
| |/|
* | | 7_C
| |/
|/|
* | 7_B
|/
* 7_A
EOF
'
test_done