]> git.ipfire.org Git - thirdparty/git.git/commitdiff
log --graph: customize the graph lines with config log.graphColors
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Thu, 19 Jan 2017 11:41:23 +0000 (18:41 +0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 24 Jan 2017 02:32:11 +0000 (18:32 -0800)
If you have a 256 colors terminal (or one with true color support), then
the predefined 12 colors seem limited. On the other hand, you don't want
to draw graph lines with every single color in this mode because the two
colors could look extremely similar. This option allows you to hand pick
the colors you want.

Even with standard terminal, if your background color is neither black
or white, then the graph line may match your background and become
hidden. You can exclude your background color (or simply the colors you
hate) with this.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/config.txt
graph.c
t/t4202-log.sh

index 0bcb6790d6efbb2a812bc90b6f64e323cb3ec393..33a007b52ea16dbbee964a7702dd8d8104c7a3eb 100644 (file)
@@ -2003,6 +2003,10 @@ log.follow::
        i.e. it cannot be used to follow multiple files and does not work well
        on non-linear history.
 
+log.graphColors::
+       A list of colors, separated by commas, that can be used to draw
+       history lines in `git log --graph`.
+
 log.showRoot::
        If true, the initial commit will be shown as a big creation event.
        This is equivalent to a diff against an empty tree.
diff --git a/graph.c b/graph.c
index dd1720148dc51740c96da0f4b17b59b97994abac..00aeee36d8a048d56ab686b136cc68bc111f9929 100644 (file)
--- a/graph.c
+++ b/graph.c
@@ -4,6 +4,7 @@
 #include "graph.h"
 #include "diff.h"
 #include "revision.h"
+#include "argv-array.h"
 
 /* Internal API */
 
@@ -62,6 +63,26 @@ enum graph_state {
 static const char **column_colors;
 static unsigned short column_colors_max;
 
+static void parse_graph_colors_config(struct argv_array *colors, const char *string)
+{
+       const char *end, *start;
+
+       start = string;
+       end = string + strlen(string);
+       while (start < end) {
+               const char *comma = strchrnul(start, ',');
+               char color[COLOR_MAXLEN];
+
+               if (!color_parse_mem(start, comma - start, color))
+                       argv_array_push(colors, color);
+               else
+                       warning(_("ignore invalid color '%.*s' in log.graphColors"),
+                               (int)(comma - start), start);
+               start = comma + 1;
+       }
+       argv_array_push(colors, GIT_COLOR_RESET);
+}
+
 void graph_set_column_colors(const char **colors, unsigned short colors_max)
 {
        column_colors = colors;
@@ -207,9 +228,22 @@ struct git_graph *graph_init(struct rev_info *opt)
 {
        struct git_graph *graph = xmalloc(sizeof(struct git_graph));
 
-       if (!column_colors)
-               graph_set_column_colors(column_colors_ansi,
-                                       column_colors_ansi_max);
+       if (!column_colors) {
+               char *string;
+               if (git_config_get_string("log.graphcolors", &string)) {
+                       /* not configured -- use default */
+                       graph_set_column_colors(column_colors_ansi,
+                                               column_colors_ansi_max);
+               } else {
+                       static struct argv_array custom_colors = ARGV_ARRAY_INIT;
+                       argv_array_clear(&custom_colors);
+                       parse_graph_colors_config(&custom_colors, string);
+                       free(string);
+                       /* graph_set_column_colors takes a max-index, not a count */
+                       graph_set_column_colors(custom_colors.argv,
+                                               custom_colors.argc - 1);
+               }
+       }
 
        graph->commit = NULL;
        graph->revs = opt;
index e2db47c36e09e3580c53867909cd3e20bdf320f5..0aeabed96dacab05c1f47ad2dae49de089547b65 100755 (executable)
@@ -313,6 +313,28 @@ test_expect_success 'log --graph with merge' '
        test_cmp expect actual
 '
 
+cat > expect.colors <<\EOF
+*   Merge branch 'side'
+<BLUE>|<RESET><CYAN>\<RESET>
+<BLUE>|<RESET> * side-2
+<BLUE>|<RESET> * side-1
+* <CYAN>|<RESET> Second
+* <CYAN>|<RESET> sixth
+* <CYAN>|<RESET> fifth
+* <CYAN>|<RESET> fourth
+<CYAN>|<RESET><CYAN>/<RESET>
+* third
+* second
+* initial
+EOF
+
+test_expect_success 'log --graph with merge with log.graphColors' '
+       test_config log.graphColors ",, blue,invalid-color, cyan, red  , " &&
+       git log --color=always --graph --date-order --pretty=tformat:%s |
+               test_decode_color | sed "s/ *\$//" >actual &&
+       test_cmp expect.colors actual
+'
+
 test_expect_success 'log --raw --graph -m with merge' '
        git log --raw --graph --oneline -m master | head -n 500 >actual &&
        grep "initial" actual