log: fix coloring of certain octopus merge shapes

For octopus merges where the first parent edge immediately merges into
the next column to the left, the number of columns should be one less
than the usual case.

First parent to the left case:

| *-.
| |\ \
|/ / /

The usual case:

| *-.
| |\ \
| | | *

Also refactor the code to iterate over columns rather than dashes,
building from an initial patch suggested by Jeff King.

Signed-off-by: Noam Postavsky <npostavs@users.sourceforge.net>
Reviewed-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Noam Postavsky 2018-09-01 20:07:16 -04:00 committed by Junio C Hamano
parent d0832b2847
commit 04005834ed
2 changed files with 144 additions and 14 deletions

56
graph.c
View file

@ -846,27 +846,55 @@ static void graph_output_commit_char(struct git_graph *graph, struct strbuf *sb)
} }
/* /*
* Draw an octopus merge and return the number of characters written. * Draw the horizontal dashes of an octopus merge and return the number of
* characters written.
*/ */
static int graph_draw_octopus_merge(struct git_graph *graph, static int graph_draw_octopus_merge(struct git_graph *graph,
struct strbuf *sb) struct strbuf *sb)
{ {
/* /*
* Here dashless_commits represents the number of parents * Here dashless_parents represents the number of parents which don't
* which don't need to have dashes (because their edges fit * need to have dashes (the edges labeled "0" and "1"). And
* neatly under the commit). * dashful_parents are the remaining ones.
*
* | *---.
* | |\ \ \
* | | | | |
* x 0 1 2 3
*
*/ */
const int dashless_commits = 2; const int dashless_parents = 2;
int col_num, i; int dashful_parents = graph->num_parents - dashless_parents;
int num_dashes =
((graph->num_parents - dashless_commits) * 2) - 1; /*
for (i = 0; i < num_dashes; i++) { * Usually, we add one new column for each parent (like the diagram
col_num = (i / 2) + dashless_commits + graph->commit_index; * above) but sometimes the first parent goes into an existing column,
strbuf_write_column(sb, &graph->new_columns[col_num], '-'); * like this:
*
* | *---.
* | |\ \ \
* |/ / / /
* x 0 1 2
*
* In which case the number of parents will be one greater than the
* number of added columns.
*/
int added_cols = (graph->num_new_columns - graph->num_columns);
int parent_in_old_cols = graph->num_parents - added_cols;
/*
* In both cases, commit_index corresponds to the edge labeled "0".
*/
int first_col = graph->commit_index + dashless_parents
- parent_in_old_cols;
int i;
for (i = 0; i < dashful_parents; i++) {
strbuf_write_column(sb, &graph->new_columns[i+first_col], '-');
strbuf_write_column(sb, &graph->new_columns[i+first_col],
i == dashful_parents-1 ? '.' : '-');
} }
col_num = (i / 2) + dashless_commits + graph->commit_index; return 2 * dashful_parents;
strbuf_write_column(sb, &graph->new_columns[col_num], '.');
return num_dashes + 1;
} }
static void graph_output_commit_line(struct git_graph *graph, struct strbuf *sb) static void graph_output_commit_line(struct git_graph *graph, struct strbuf *sb)

102
t/t4214-log-graph-octopus.sh Executable file
View file

@ -0,0 +1,102 @@
#!/bin/sh
test_description='git log --graph of skewed left octopus merge.'
. ./test-lib.sh
test_expect_success 'set up merge history' '
cat >expect.uncolored <<-\EOF &&
* left
| *---. octopus-merge
| |\ \ \
|/ / / /
| | | * 4
| | * | 3
| | |/
| * | 2
| |/
* | 1
|/
* initial
EOF
cat >expect.colors <<-\EOF &&
* left
<RED>|<RESET> *<BLUE>-<RESET><BLUE>-<RESET><MAGENTA>-<RESET><MAGENTA>.<RESET> octopus-merge
<RED>|<RESET> <RED>|<RESET><YELLOW>\<RESET> <BLUE>\<RESET> <MAGENTA>\<RESET>
<RED>|<RESET><RED>/<RESET> <YELLOW>/<RESET> <BLUE>/<RESET> <MAGENTA>/<RESET>
<RED>|<RESET> <YELLOW>|<RESET> <BLUE>|<RESET> * 4
<RED>|<RESET> <YELLOW>|<RESET> * <MAGENTA>|<RESET> 3
<RED>|<RESET> <YELLOW>|<RESET> <MAGENTA>|<RESET><MAGENTA>/<RESET>
<RED>|<RESET> * <MAGENTA>|<RESET> 2
<RED>|<RESET> <MAGENTA>|<RESET><MAGENTA>/<RESET>
* <MAGENTA>|<RESET> 1
<MAGENTA>|<RESET><MAGENTA>/<RESET>
* initial
EOF
test_commit initial &&
for i in 1 2 3 4 ; do
git checkout master -b $i || return $?
# Make tag name different from branch name, to avoid
# ambiguity error when calling checkout.
test_commit $i $i $i tag$i || return $?
done &&
git checkout 1 -b merge &&
test_tick &&
git merge -m octopus-merge 1 2 3 4 &&
git checkout 1 -b L &&
test_commit left
'
test_expect_success 'log --graph with tricky octopus merge with colors' '
test_config log.graphColors red,green,yellow,blue,magenta,cyan &&
git log --color=always --graph --date-order --pretty=tformat:%s --all >actual.colors.raw &&
test_decode_color <actual.colors.raw | sed "s/ *\$//" >actual.colors &&
test_cmp expect.colors actual.colors
'
test_expect_success 'log --graph with tricky octopus merge, no color' '
git log --color=never --graph --date-order --pretty=tformat:%s --all >actual.raw &&
sed "s/ *\$//" actual.raw >actual &&
test_cmp expect.uncolored actual
'
# Repeat the previous two tests with "normal" octopus merge (i.e.,
# without the first parent skewing to the "left" branch column).
test_expect_success 'log --graph with normal octopus merge, no color' '
cat >expect.uncolored <<-\EOF &&
*---. octopus-merge
|\ \ \
| | | * 4
| | * | 3
| | |/
| * | 2
| |/
* | 1
|/
* initial
EOF
git log --color=never --graph --date-order --pretty=tformat:%s merge >actual.raw &&
sed "s/ *\$//" actual.raw >actual &&
test_cmp expect.uncolored actual
'
test_expect_success 'log --graph with normal octopus merge with colors' '
cat >expect.colors <<-\EOF &&
*<YELLOW>-<RESET><YELLOW>-<RESET><BLUE>-<RESET><BLUE>.<RESET> octopus-merge
<RED>|<RESET><GREEN>\<RESET> <YELLOW>\<RESET> <BLUE>\<RESET>
<RED>|<RESET> <GREEN>|<RESET> <YELLOW>|<RESET> * 4
<RED>|<RESET> <GREEN>|<RESET> * <BLUE>|<RESET> 3
<RED>|<RESET> <GREEN>|<RESET> <BLUE>|<RESET><BLUE>/<RESET>
<RED>|<RESET> * <BLUE>|<RESET> 2
<RED>|<RESET> <BLUE>|<RESET><BLUE>/<RESET>
* <BLUE>|<RESET> 1
<BLUE>|<RESET><BLUE>/<RESET>
* initial
EOF
test_config log.graphColors red,green,yellow,blue,magenta,cyan &&
git log --color=always --graph --date-order --pretty=tformat:%s merge >actual.colors.raw &&
test_decode_color <actual.colors.raw | sed "s/ *\$//" >actual.colors &&
test_cmp expect.colors actual.colors
'
test_done