{"thread":{"id":"24131","subject":"[RFC/PATCH] Colorize commit decorations","startedAt":"2010-06-16T16:57:30Z","lastAt":"2010-06-16T16:57:30Z","messageCount":1,"participants":["Nazri Ramliy"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"143825","messageId":"1276707450-19396-1-git-send-email-ayiehere@gmail.com","threadId":"24131","inReplyTo":null,"subject":"[RFC/PATCH] Colorize commit decorations","fromName":"Nazri Ramliy","fromEmail":"ayiehere@gmail.com","sentAt":"2010-06-16T16:57:30Z","receivedAt":"2010-06-16T16:57:30Z","isPatch":true,"sender":{"key":"ayiehere@gmail.com","avatar":"https://avatars.githubusercontent.com/u/164756?v=4"},"body":"Use different color for each type of ref (local, remote, tags, HEAD,\nstash). This will make the decorations and their type stand out more and\neasier to distinguish in 'git log --decorate'.\n\nCurrently all the different type of decorations are shown in the same\ncolor as the commit id, which is not that easy to spot.\n---\nIf there are no strong objections to this patch I'll look into the remaining\nwork if needed:\n\n\t1. config.color.log.decorate.<slot>\n\t2. See the FIXME part in this patch (most of the work here was copied\n           from diff coloring).\n\n commit.h   |    1 +\n decorate.c |   18 ++++++++++++++++++\n decorate.h |   14 ++++++++++++++\n log-tree.c |   35 +++++++++++++++++++++++++++--------\n 4 files changed, 60 insertions(+), 8 deletions(-)\n\ndiff --git a/commit.h b/commit.h\nindex 6ef88dc..a49cfe3 100644\n--- a/commit.h\n+++ b/commit.h\n@@ -28,6 +28,7 @@ extern const char *commit_type;\n extern struct decoration name_decoration;\n struct name_decoration {\n \tstruct name_decoration *next;\n+\tenum decoration_type type;\n \tchar name[1];\n };\n \ndiff --git a/decorate.c b/decorate.c\nindex 2f8a63e..8444867 100644\n--- a/decorate.c\n+++ b/decorate.c\n@@ -5,6 +5,24 @@\n #include \"cache.h\"\n #include \"object.h\"\n #include \"decorate.h\"\n+#include \"color.h\"\n+\n+static char decoration_colors[][COLOR_MAXLEN] = {\n+\tGIT_COLOR_RESET,\t/* DECORATION_NONE */\n+\tGIT_COLOR_BOLD_GREEN,\t/* DECORATION_REF_LOCAL */\n+\tGIT_COLOR_BOLD_RED,\t/* DECORATION_REF_REMOTE */\n+\tGIT_COLOR_BOLD_YELLOW,\t/* DECORATION_TAG */\n+\tGIT_COLOR_BOLD_MAGENTA,\t/* DECORATION_STASH */\n+\tGIT_COLOR_BOLD_CYAN\t/* DECORATION_HEAD */\n+};\n+\n+const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)\n+{\n+\tif (decorate_use_color) {\n+\t\treturn decoration_colors[ix];\n+\t}\n+\treturn \"\";\n+}\n \n static unsigned int hash_obj(const struct object *obj, unsigned int n)\n {\ndiff --git a/decorate.h b/decorate.h\nindex e732804..7d6e7e5 100644\n--- a/decorate.h\n+++ b/decorate.h\n@@ -12,6 +12,20 @@ struct decoration {\n \tstruct object_decoration *hash;\n };\n \n+enum decoration_type {\n+\tDECORATION_NONE = 0,\n+\tDECORATION_REF_LOCAL,\n+\tDECORATION_REF_REMOTE,\n+\tDECORATION_TAG,\n+\tDECORATION_STASH,\n+\tDECORATION_HEAD\n+};\n+\n+const char *decorate_get_color(int diff_use_color, enum decoration_type ix);\n+/* FIXME DIFF_OPT_TST -> DECORATE_OPT_TST? */\n+#define decorate_get_color_opt(o, ix) \\\n+\tdecorate_get_color(DIFF_OPT_TST((o), COLOR_DIFF), ix)\n+\n extern void *add_decoration(struct decoration *n, const struct object *obj, void *decoration);\n extern void *lookup_decoration(struct decoration *n, const struct object *obj);\n \ndiff --git a/log-tree.c b/log-tree.c\nindex d3ae969..485ce3c 100644\n--- a/log-tree.c\n+++ b/log-tree.c\n@@ -10,29 +10,41 @@\n \n struct decoration name_decoration = { \"object names\" };\n \n-static void add_name_decoration(const char *prefix, const char *name, struct object *obj)\n+static void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)\n {\n-\tint plen = strlen(prefix);\n \tint nlen = strlen(name);\n-\tstruct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);\n-\tmemcpy(res->name, prefix, plen);\n-\tmemcpy(res->name + plen, name, nlen + 1);\n+\tstruct name_decoration *res = xmalloc(sizeof(struct name_decoration) + nlen);\n+\tmemcpy(res->name, name, nlen + 1);\n+\tres->type = type;\n \tres->next = add_decoration(&name_decoration, obj, res);\n }\n \n static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)\n {\n \tstruct object *obj = parse_object(sha1);\n+\tenum decoration_type type = DECORATION_NONE;\n \tif (!obj)\n \t\treturn 0;\n+\n+\tif (!prefixcmp(refname, \"refs/heads\"))\n+\t\ttype = DECORATION_REF_LOCAL;\n+\telse if (!prefixcmp(refname, \"refs/remotes\"))\n+\t\ttype = DECORATION_REF_REMOTE;\n+\telse if (!prefixcmp(refname, \"refs/tags\"))\n+\t\ttype = DECORATION_TAG;\n+\telse if (!prefixcmp(refname, \"refs/stash\"))\n+\t\ttype = DECORATION_STASH;\n+\telse if (!prefixcmp(refname, \"HEAD\"))\n+\t\ttype = DECORATION_HEAD;\n+\n \tif (!cb_data || *(int *)cb_data == DECORATE_SHORT_REFS)\n \t\trefname = prettify_refname(refname);\n-\tadd_name_decoration(\"\", refname, obj);\n+\tadd_name_decoration(type, refname, obj);\n \twhile (obj->type == OBJ_TAG) {\n \t\tobj = ((struct tag *)obj)->tagged;\n \t\tif (!obj)\n \t\t\tbreak;\n-\t\tadd_name_decoration(\"tag: \", refname, obj);\n+\t\tadd_name_decoration(DECORATION_TAG, refname, obj);\n \t}\n \treturn 0;\n }\n@@ -60,6 +72,7 @@ void show_decorations(struct rev_info *opt, struct commit *commit)\n {\n \tconst char *prefix;\n \tstruct name_decoration *decoration;\n+\tconst char *color_commit = diff_get_color_opt(&opt->diffopt, DIFF_COMMIT);\n \n \tif (opt->show_source && commit->util)\n \t\tprintf(\"\\t%s\", (char *) commit->util);\n@@ -70,7 +83,13 @@ void show_decorations(struct rev_info *opt, struct commit *commit)\n \t\treturn;\n \tprefix = \" (\";\n \twhile (decoration) {\n-\t\tprintf(\"%s%s\", prefix, decoration->name);\n+\t\tprintf(\"%s\", prefix);\n+\t\tfputs(decorate_get_color_opt(&opt->diffopt, decoration->type), stdout);\n+\t\tif (decoration->type == DECORATION_TAG)\n+\t\t\tfputs(\"tag: \", stdout);\n+\t\tprintf(\"%s\", decoration->name);\n+\t\tfputs(GIT_COLOR_RESET, stdout);\n+\t\tfputs(color_commit, stdout);\n \t\tprefix = \", \";\n \t\tdecoration = decoration->next;\n \t}\n-- \n1.7.1.245.g7c42e.dirty\n"}]}