git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH 6/9] for-each-ref: add %(color:...)
Date: Sun, 19 May 2013 17:27:12 +0700	[thread overview]
Message-ID: <1368959235-27777-7-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1368959235-27777-1-git-send-email-pclouds@gmail.com>


Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 branch.h               | 11 +++++++++++
 builtin/branch.c       | 12 ++----------
 builtin/for-each-ref.c | 34 +++++++++++++++++++++++++++++++++-
 3 files changed, 46 insertions(+), 11 deletions(-)

diff --git a/branch.h b/branch.h
index 64173ab..076babf 100644
--- a/branch.h
+++ b/branch.h
@@ -52,4 +52,15 @@ extern void install_branch_config(int flag, const char *local, const char *origi
  */
 extern int read_branch_desc(struct strbuf *, const char *branch_name);
 
+enum color_branch {
+	BRANCH_COLOR_RESET = 0,
+	BRANCH_COLOR_PLAIN = 1,
+	BRANCH_COLOR_REMOTE = 2,
+	BRANCH_COLOR_LOCAL = 3,
+	BRANCH_COLOR_CURRENT = 4,
+	BRANCH_COLOR_UPSTREAM = 5
+};
+extern int git_branch_config(const char *var, const char *value, void *cb);
+extern const char *branch_get_color(enum color_branch ix);
+
 #endif
diff --git a/builtin/branch.c b/builtin/branch.c
index 7d084da..1aa282c 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -43,14 +43,6 @@ static char branch_colors[][COLOR_MAXLEN] = {
 	GIT_COLOR_GREEN,	/* CURRENT */
 	GIT_COLOR_BLUE,		/* UPSTREAM */
 };
-enum color_branch {
-	BRANCH_COLOR_RESET = 0,
-	BRANCH_COLOR_PLAIN = 1,
-	BRANCH_COLOR_REMOTE = 2,
-	BRANCH_COLOR_LOCAL = 3,
-	BRANCH_COLOR_CURRENT = 4,
-	BRANCH_COLOR_UPSTREAM = 5
-};
 
 static enum merge_filter {
 	NO_FILTER = 0,
@@ -79,7 +71,7 @@ static int parse_branch_color_slot(const char *var, int ofs)
 	return -1;
 }
 
-static int git_branch_config(const char *var, const char *value, void *cb)
+int git_branch_config(const char *var, const char *value, void *cb)
 {
 	if (!prefixcmp(var, "column."))
 		return git_column_config(var, value, "branch", &colopts);
@@ -99,7 +91,7 @@ static int git_branch_config(const char *var, const char *value, void *cb)
 	return git_color_default_config(var, value, cb);
 }
 
-static const char *branch_get_color(enum color_branch ix)
+const char *branch_get_color(enum color_branch ix)
 {
 	if (want_color(branch_use_color))
 		return branch_colors[ix];
diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index b10d48a..db5c211 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -9,6 +9,8 @@
 #include "quote.h"
 #include "parse-options.h"
 #include "remote.h"
+#include "color.h"
+#include "branch.h"
 
 /* Quoting styles */
 #define QUOTE_NONE 0
@@ -36,6 +38,7 @@ struct refinfo {
 	int flag;
 	const char *symref;
 	struct atom_value *value;
+	int auto_color;
 };
 
 static struct {
@@ -78,6 +81,7 @@ static struct {
 	{ "current" },
 	{ "tracking" },
 	{ "tracking:upstream" },
+	{ "color" },
 };
 
 /*
@@ -707,6 +711,21 @@ static void populate_value(struct refinfo *ref)
 			v->s = sb.buf;
 			continue;
 		}
+		else if (!prefixcmp(name, "color:")) {
+			const char *color = name + 6;
+			ref->auto_color = 0;
+			if (!prefixcmp(color, "red"))
+				v->s = GIT_COLOR_RED;
+			else if (!prefixcmp(color, "green"))
+				v->s = GIT_COLOR_GREEN;
+			else if (!prefixcmp(color, "blue"))
+				v->s = GIT_COLOR_BLUE;
+			else if (!prefixcmp(color, "reset"))
+				v->s = GIT_COLOR_RESET;
+			else if (!prefixcmp(color, "auto"))
+				ref->auto_color = 1;
+			continue;
+		}
 		else
 			continue;
 
@@ -730,6 +749,19 @@ static void populate_value(struct refinfo *ref)
 			sprintf(s, "%s^{}", refname);
 			v->s = s;
 		}
+
+		if (ref->auto_color) {
+			if (!head) {
+				unsigned char sha1[20];
+				head = resolve_refdup("HEAD", sha1, 0, NULL);
+			}
+			if (strcmp(head, "HEAD") && !strcmp(ref->refname, head)) {
+				struct strbuf sb = STRBUF_INIT;
+				strbuf_addstr(&sb, branch_get_color(BRANCH_COLOR_CURRENT));
+				strbuf_addstr(&sb, v->s);
+				v->s = sb.buf;
+			}
+		}
 	}
 
 	for (i = 0; i < used_atom_cnt; i++) {
@@ -1071,7 +1103,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 	sort_atom_limit = used_atom_cnt;
 
 	/* for warn_ambiguous_refs */
-	git_config(git_default_config, NULL);
+	git_config(git_branch_config, NULL);
 
 	memset(&cbdata, 0, sizeof(cbdata));
 	cbdata.grab_pattern = argv;
-- 
1.8.2.83.gc99314b

  parent reply	other threads:[~2013-05-19 10:27 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-19 10:27 [PATCH/WIP 0/9] for-each-ref format improvements Nguyễn Thái Ngọc Duy
2013-05-19 10:27 ` [PATCH 1/9] quote.c: make sq_quote_print a slight wrapper of sq_quote_buf Nguyễn Thái Ngọc Duy
2013-05-19 11:39   ` Felipe Contreras
2013-05-19 10:27 ` [PATCH 2/9] for-each-ref: convert to use *_quote_buf instead of _quote_print Nguyễn Thái Ngọc Duy
2013-05-19 11:39   ` Felipe Contreras
2013-05-19 10:27 ` [PATCH 3/9] for-each-ref: avoid printing each element directly to stdout Nguyễn Thái Ngọc Duy
2013-05-19 11:17   ` Ramkumar Ramachandra
2013-05-19 11:58     ` Duy Nguyen
2013-05-19 10:27 ` [PATCH 4/9] for-each-ref: add %(current) for current branch marker Nguyễn Thái Ngọc Duy
2013-05-19 11:14   ` Ramkumar Ramachandra
2013-05-19 10:27 ` [PATCH 5/9] for-each-ref: add %(tracking[:upstream]) for tracking info Nguyễn Thái Ngọc Duy
2013-05-19 11:18   ` Ramkumar Ramachandra
2013-05-19 11:38     ` Felipe Contreras
2013-05-19 11:43       ` Duy Nguyen
2013-05-19 11:48         ` Ramkumar Ramachandra
2013-05-19 12:03           ` Felipe Contreras
2013-05-19 10:27 ` Nguyễn Thái Ngọc Duy [this message]
2013-05-19 11:25   ` [PATCH 6/9] for-each-ref: add %(color:...) Ramkumar Ramachandra
2013-05-19 10:27 ` [PATCH 7/9] for-each-ref: prepoplulate all atoms before show_ref() Nguyễn Thái Ngọc Duy
2013-05-19 10:27 ` [PATCH 8/9] for-each-ref: merge show_ref into show_refs Nguyễn Thái Ngọc Duy
2013-05-19 10:27 ` [PATCH 9/9] for-each-ref: support %(...:aligned) for left alignment Nguyễn Thái Ngọc Duy
2013-05-19 11:32   ` Ramkumar Ramachandra
2013-05-19 11:41     ` Duy Nguyen
2013-05-19 11:11 ` [PATCH/WIP 0/9] for-each-ref format improvements Ramkumar Ramachandra
2013-05-19 11:36   ` Felipe Contreras
2013-05-19 11:54   ` Duy Nguyen
2013-05-19 12:08     ` Ramkumar Ramachandra
2013-05-19 12:28       ` Duy Nguyen
2013-05-19 13:07         ` Ramkumar Ramachandra
2013-05-20  4:29 ` Junio C Hamano
2013-05-21 17:21 ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1368959235-27777-7-git-send-email-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).