[RFC/PATCH] Colorize commit decorations

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Use different color for each type of ref (local, remote, tags, HEAD,
stash). This will make the decorations and their type stand out more and
easier to distinguish in 'git log --decorate'.

Currently all the different type of decorations are shown in the same
color as the commit id, which is not that easy to spot.
---
If there are no strong objections to this patch I'll look into the remaining
work if needed:

	1. config.color.log.decorate.<slot>
	2. See the FIXME part in this patch (most of the work here was copied
           from diff coloring).

 commit.h   |    1 +
 decorate.c |   18 ++++++++++++++++++
 decorate.h |   14 ++++++++++++++
 log-tree.c |   35 +++++++++++++++++++++++++++--------
 4 files changed, 60 insertions(+), 8 deletions(-)

diff --git a/commit.h b/commit.h
index 6ef88dc..a49cfe3 100644
--- a/commit.h
+++ b/commit.h
@@ -28,6 +28,7 @@ extern const char *commit_type;
 extern struct decoration name_decoration;
 struct name_decoration {
 	struct name_decoration *next;
+	enum decoration_type type;
 	char name[1];
 };
 
diff --git a/decorate.c b/decorate.c
index 2f8a63e..8444867 100644
--- a/decorate.c
+++ b/decorate.c
@@ -5,6 +5,24 @@
 #include "cache.h"
 #include "object.h"
 #include "decorate.h"
+#include "color.h"
+
+static char decoration_colors[][COLOR_MAXLEN] = {
+	GIT_COLOR_RESET,	/* DECORATION_NONE */
+	GIT_COLOR_BOLD_GREEN,	/* DECORATION_REF_LOCAL */
+	GIT_COLOR_BOLD_RED,	/* DECORATION_REF_REMOTE */
+	GIT_COLOR_BOLD_YELLOW,	/* DECORATION_TAG */
+	GIT_COLOR_BOLD_MAGENTA,	/* DECORATION_STASH */
+	GIT_COLOR_BOLD_CYAN	/* DECORATION_HEAD */
+};
+
+const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
+{
+	if (decorate_use_color) {
+		return decoration_colors[ix];
+	}
+	return "";
+}
 
 static unsigned int hash_obj(const struct object *obj, unsigned int n)
 {
diff --git a/decorate.h b/decorate.h
index e732804..7d6e7e5 100644
--- a/decorate.h
+++ b/decorate.h
@@ -12,6 +12,20 @@ struct decoration {
 	struct object_decoration *hash;
 };
 
+enum decoration_type {
+	DECORATION_NONE = 0,
+	DECORATION_REF_LOCAL,
+	DECORATION_REF_REMOTE,
+	DECORATION_TAG,
+	DECORATION_STASH,
+	DECORATION_HEAD
+};
+
+const char *decorate_get_color(int diff_use_color, enum decoration_type ix);
+/* FIXME DIFF_OPT_TST -> DECORATE_OPT_TST? */
+#define decorate_get_color_opt(o, ix) \
+	decorate_get_color(DIFF_OPT_TST((o), COLOR_DIFF), ix)
+
 extern void *add_decoration(struct decoration *n, const struct object *obj, void *decoration);
 extern void *lookup_decoration(struct decoration *n, const struct object *obj);
 
diff --git a/log-tree.c b/log-tree.c
index d3ae969..485ce3c 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -10,29 +10,41 @@
 
 struct decoration name_decoration = { "object names" };
 
-static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
+static void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
 {
-	int plen = strlen(prefix);
 	int nlen = strlen(name);
-	struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
-	memcpy(res->name, prefix, plen);
-	memcpy(res->name + plen, name, nlen + 1);
+	struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + nlen);
+	memcpy(res->name, name, nlen + 1);
+	res->type = type;
 	res->next = add_decoration(&name_decoration, obj, res);
 }
 
 static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
 {
 	struct object *obj = parse_object(sha1);
+	enum decoration_type type = DECORATION_NONE;
 	if (!obj)
 		return 0;
+
+	if (!prefixcmp(refname, "refs/heads"))
+		type = DECORATION_REF_LOCAL;
+	else if (!prefixcmp(refname, "refs/remotes"))
+		type = DECORATION_REF_REMOTE;
+	else if (!prefixcmp(refname, "refs/tags"))
+		type = DECORATION_TAG;
+	else if (!prefixcmp(refname, "refs/stash"))
+		type = DECORATION_STASH;
+	else if (!prefixcmp(refname, "HEAD"))
+		type = DECORATION_HEAD;
+
 	if (!cb_data || *(int *)cb_data == DECORATE_SHORT_REFS)
 		refname = prettify_refname(refname);
-	add_name_decoration("", refname, obj);
+	add_name_decoration(type, refname, obj);
 	while (obj->type == OBJ_TAG) {
 		obj = ((struct tag *)obj)->tagged;
 		if (!obj)
 			break;
-		add_name_decoration("tag: ", refname, obj);
+		add_name_decoration(DECORATION_TAG, refname, obj);
 	}
 	return 0;
 }
@@ -60,6 +72,7 @@ void show_decorations(struct rev_info *opt, struct commit *commit)
 {
 	const char *prefix;
 	struct name_decoration *decoration;
+	const char *color_commit = diff_get_color_opt(&opt->diffopt, DIFF_COMMIT);
 
 	if (opt->show_source && commit->util)
 		printf("\t%s", (char *) commit->util);
@@ -70,7 +83,13 @@ void show_decorations(struct rev_info *opt, struct commit *commit)
 		return;
 	prefix = " (";
 	while (decoration) {
-		printf("%s%s", prefix, decoration->name);
+		printf("%s", prefix);
+		fputs(decorate_get_color_opt(&opt->diffopt, decoration->type), stdout);
+		if (decoration->type == DECORATION_TAG)
+			fputs("tag: ", stdout);
+		printf("%s", decoration->name);
+		fputs(GIT_COLOR_RESET, stdout);
+		fputs(color_commit, stdout);
 		prefix = ", ";
 		decoration = decoration->next;
 	}
-- 
1.7.1.245.g7c42e.dirty

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]