Skip to contentSkip to conversation

for-each-ref format improvements

PATCH In review Nguyễn Thái Ngọc Duy 4 participants 31 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

cover for-each-ref format improvements

The purpose of this series is to make "for-each-ref --format" powerful enough to display what "branch -v" and "branch -vv" do so that we could get rid of those display code and use for-each-ref code instead.

The benefits are clear: share more code, branch can also borrow --sort and --format from for-each-ref, which should satisty users who are not happy with what -v and -vv provides.

This version has not gotten there yet. I just want to post quick and dirty changes and try to address some design issues.

Originally I wanted to introduce --pretty with git-log's pretty syntax to for-each-ref, deprecating --format. But because --format has to be there forever, we cannot remove its code, and the code change to make pretty code ready for displaying refs may not be small. So I went with enhancing --format syntax instead.

This series introduces:

- %(current), which either shows "*" if the ref is pointed by HEAD or a space. Junio called it %(headness). I don't like that. I don't like %(current) either but we have to start somewhere. Name suggestion? %(marker)??

- %(tracking[:upstream]) gives us the exact output that branch -v[v] does. %(upstream) does not include []. We can't change its semantics.

- %(color:...) is pretty much the same as %C family in pretty code. I haven't added code for %(color:foo) == %C(foo) yet. There's a potential ambiguity here: %C(red) == %Cred or %C(red)??

- %(...:aligned) to do left aligning. I'm not entirely sure about this. We might be able to share code with %>, %< and %>< from pretty.c. But we need improvements there too because in for-each-ref case, we could calculate column width but %< would require the user to specify the width.

Do people expect fancy layout with for-each-ref (and branch)? If so we might need to have %(align) or something instead of the simple left alignment case in %(...:aligned)

- We may need an equivalent of the space following % in pretty format. If the specifier produces something, then prepend a space, otherwise produce nothing. Do it like %C( tracking) vs %C(tracking)??

You can try this after applying the series, which should give you the about close to 'branch -v'. %(tracking) coloring does not work though.

git for-each-ref --format='%(current) %(color:auto)%(refname:short:aligned)%(color:reset) %%(objectname:short) %(tracking) %(subject)' 'refs/heads/*'

Nguyễn Thái Ngọc Duy (9):
quote.c: make sq_quote_print a slight wrapper of sq_quote_buf
for-each-ref: convert to use *_quote_buf instead of _quote_print
for-each-ref: avoid printing each element directly to stdout
for-each-ref: add %(current) for current branch marker
for-each-ref: add %(tracking[:upstream]) for tracking info
for-each-ref: add %(color:...)
for-each-ref: prepoplulate all atoms before show_ref()
for-each-ref: merge show_ref into show_refs
for-each-ref: support %(...:aligned) for left alignment

 branch.h               |  11 ++++
 builtin/branch.c       |  16 ++----
 builtin/for-each-ref.c | 146 ++++++++++++++++++++++++++++++++++++++++++-------
 quote.c                |  61 +++++++++------------
 quote.h                |   6 +-
 5 files changed, 170 insertions(+), 70 deletions(-)
Signature · 1 line
1.8.2.83.gc99314b
#1 lore raw

PATCH 1/9 quote.c: make sq_quote_print a slight wrapper of sq_quote_buf

---
 quote.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)
quote.c +4 -13
index 911229f..c67f5d3 100644
@@ -44,19 +44,10 @@ void sq_quote_buf(struct strbuf *dst, const char *src)
 
 void sq_quote_print(FILE *stream, const char *src)
 {
-	char c;
-
-	fputc('\'', stream);
-	while ((c = *src++)) {
-		if (need_bs_quote(c)) {
-			fputs("'\\", stream);
-			fputc(c, stream);
-			fputc('\'', stream);
-		} else {
-			fputc(c, stream);
-		}
-	}
-	fputc('\'', stream);
+	struct strbuf sb = STRBUF_INIT;
+	sq_quote_buf(&sb, src);
+	fputs(sb.buf, stream);
+	strbuf_release(&sb);
 }
 
 void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
Signature · 1 line
1.8.2.83.gc99314b
#2 lore raw

Re: [PATCH 1/9] quote.c: make sq_quote_print a slight wrapper of sq_quote_buf

Quoted 5 lines · Nguyễn Thái Ngọc Duy
---
 quote.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)

No functional changes I suppose.

Signature · 1 line
Felipe Contreras
#3 lore raw

PATCH 2/9 for-each-ref: convert to use *_quote_buf instead of _quote_print

In reply to Nguyễn Thái Ngọc Duy
---
 builtin/for-each-ref.c | 14 ++++++++++----
 quote.c                | 44 ++++++++++++++++++++++----------------------
 quote.h                |  6 +++---
 3 files changed, 35 insertions(+), 29 deletions(-)
builtin/for-each-ref.c +10 -4
index 7f059c3..14151b4 100644
@@ -867,24 +867,30 @@ static void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs
 static void print_value(struct refinfo *ref, int atom, int quote_style)
 {
 	struct atom_value *v;
+	struct strbuf sb_buf = STRBUF_INIT;
+	struct strbuf *sb = &sb_buf;
 	get_value(ref, atom, &v);
 	switch (quote_style) {
 	case QUOTE_NONE:
 		fputs(v->s, stdout);
 		break;
 	case QUOTE_SHELL:
-		sq_quote_print(stdout, v->s);
+		sq_quote_buf(sb, v->s);
 		break;
 	case QUOTE_PERL:
-		perl_quote_print(stdout, v->s);
+		perl_quote_buf(sb, v->s);
 		break;
 	case QUOTE_PYTHON:
-		python_quote_print(stdout, v->s);
+		python_quote_buf(sb, v->s);
 		break;
 	case QUOTE_TCL:
-		tcl_quote_print(stdout, v->s);
+		tcl_quote_buf(sb, v->s);
 		break;
 	}
+	if (quote_style != QUOTE_NONE) {
+		fputs(sb->buf, stdout);
+		strbuf_release(sb);
+	}
 }
 
 static int hex1(char ch)
quote.c +22 -22

94 lines not on this page · raw message

quote.h +3 -3
index 133155a..ed06df5 100644
@@ -69,8 +69,8 @@ extern char *quote_path_relative(const char *in, int len,
 			  struct strbuf *out, const char *prefix);
 
 /* quoting as a string literal for other languages */
-extern void perl_quote_print(FILE *stream, const char *src);
-extern void python_quote_print(FILE *stream, const char *src);
-extern void tcl_quote_print(FILE *stream, const char *src);
+extern void perl_quote_buf(struct strbuf *sb, const char *src);
+extern void python_quote_buf(struct strbuf *sb, const char *src);
+extern void tcl_quote_buf(struct strbuf *sb, const char *src);
 
 #endif
Signature · 1 line
1.8.2.83.gc99314b
#4 lore raw

Re: [PATCH 2/9] for-each-ref: convert to use *_quote_buf instead of _quote_print

Quoted 7 lines · Nguyễn Thái Ngọc Duy
---
 builtin/for-each-ref.c | 14 ++++++++++----
 quote.c                | 44 ++++++++++++++++++++++----------------------
 quote.h                |  6 +++---
 3 files changed, 35 insertions(+), 29 deletions(-)

No functional changes I suppose.

Signature · 1 line
Felipe Contreras
#5 lore raw

PATCH 3/9 for-each-ref: avoid printing each element directly to stdout

In reply to Nguyễn Thái Ngọc Duy
---
 builtin/for-each-ref.c | 48 +++++++++++++++++++++++++++++-------------------
 1 file changed, 29 insertions(+), 19 deletions(-)
builtin/for-each-ref.c +29 -19
index 14151b4..08d4eb1 100644
@@ -864,15 +864,14 @@ static void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs
 	qsort(refs, num_refs, sizeof(struct refinfo *), compare_refs);
 }
 
-static void print_value(struct refinfo *ref, int atom, int quote_style)
+static void print_value(struct strbuf *sb, struct refinfo *ref,
+			int atom, int quote_style)
 {
 	struct atom_value *v;
-	struct strbuf sb_buf = STRBUF_INIT;
-	struct strbuf *sb = &sb_buf;
 	get_value(ref, atom, &v);
 	switch (quote_style) {
 	case QUOTE_NONE:
-		fputs(v->s, stdout);
+		strbuf_addstr(sb, v->s);
 		break;
 	case QUOTE_SHELL:
 		sq_quote_buf(sb, v->s);
@@ -887,10 +886,6 @@ static void print_value(struct refinfo *ref, int atom, int quote_style)
 		tcl_quote_buf(sb, v->s);
 		break;
 	}
-	if (quote_style != QUOTE_NONE) {
-		fputs(sb->buf, stdout);
-		strbuf_release(sb);
-	}
 }
 
 static int hex1(char ch)
@@ -911,7 +906,7 @@ static int hex2(const char *cp)
 		return -1;
 }
 
-static void emit(const char *cp, const char *ep)
+static void emit(struct strbuf *sb, const char *cp, const char *ep)
 {
 	while (*cp && (!ep || cp < ep)) {
 		if (*cp == '%') {
@@ -920,32 +915,47 @@ static void emit(const char *cp, const char *ep)
 			else {
 				int ch = hex2(cp + 1);
 				if (0 <= ch) {
-					putchar(ch);
+					strbuf_addch(sb, ch);
 					cp += 3;
 					continue;
 				}
 			}
 		}
-		putchar(*cp);
+		strbuf_addch(sb, *cp);
 		cp++;
 	}
 }
 
-static void show_ref(struct refinfo *info, const char *format, int quote_style)
+static void show_ref(struct strbuf *sb, struct refinfo *info,
+		     const char *format, int quote_style)
 {
 	const char *cp, *sp, *ep;
 
 	for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
 		ep = strchr(sp, ')');
 		if (cp < sp)
-			emit(cp, sp);
-		print_value(info, parse_atom(sp + 2, ep), quote_style);
+			emit(sb, cp, sp);
+		print_value(sb, info, parse_atom(sp + 2, ep), quote_style);
 	}
 	if (*cp) {
 		sp = cp + strlen(cp);
-		emit(cp, sp);
+		emit(sb, cp, sp);
+	}
+	strbuf_addch(sb, '\n');
+}
+
+static void show_refs(struct refinfo **refs, int maxcount,
+		      const char *format, int quote_style)
+{
+	struct strbuf sb = STRBUF_INIT;
+	int i;
+
+	for (i = 0; i < maxcount; i++) {
+		strbuf_reset(&sb);
+		show_ref(&sb, refs[i], format, quote_style);
+		fputs(sb.buf, stdout);
 	}
-	putchar('\n');
+	strbuf_release(&sb);
 }
 
 static struct ref_sort *default_sort(void)
@@ -988,7 +998,7 @@ static char const * const for_each_ref_usage[] = {
 
 int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 {
-	int i, num_refs;
+	int num_refs;
 	const char *format = "%(objectname) %(objecttype)\t%(refname)";
 	struct ref_sort *sort = NULL, **sort_tail = &sort;
 	int maxcount = 0, quote_style = 0;
@@ -1042,7 +1052,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 
 	if (!maxcount || num_refs < maxcount)
 		maxcount = num_refs;
-	for (i = 0; i < maxcount; i++)
-		show_ref(refs[i], format, quote_style);
+
+	show_refs(refs, maxcount, format, quote_style);
 	return 0;
 }
Signature · 1 line
1.8.2.83.gc99314b
#6 lore raw

Re: [PATCH 3/9] for-each-ref: avoid printing each element directly to stdout

Quoted 1 line

[PATCH 3/9] for-each-ref: avoid printing each element directly to stdout

Why did you do this? Atoms are designed to be independent of each other. I'll keep reading: might find out in a later patch.

author
#7 lore raw

Re: [PATCH 3/9] for-each-ref: avoid printing each element directly to stdout

Quoted 4 lines · Ramkumar Ramachandra
Quoted 1 line

[PATCH 3/9] for-each-ref: avoid printing each element directly to stdout

Why did you do this? Atoms are designed to be independent of each other. I'll keep reading: might find out in a later patch.

Sorry for the lack of explanation in the message. I wanted to discuss about syntax more than code. As you see later down the series, we'll need to process an atom for all refs, then output all refs in the end. We can't do that wihout buffering.
--
Duy

#8 lore raw

PATCH 4/9 for-each-ref: add %(current) for current branch marker

In reply to Nguyễn Thái Ngọc Duy
---
 builtin/for-each-ref.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
builtin/for-each-ref.c +13 -0
index 08d4eb1..498d703 100644
@@ -75,6 +75,7 @@ static struct {
 	{ "upstream" },
 	{ "symref" },
 	{ "flag" },
+	{ "current" },
 };
 
 /*
@@ -90,6 +91,7 @@ static struct {
 static const char **used_atom;
 static cmp_type *used_atom_type;
 static int used_atom_cnt, sort_atom_limit, need_tagged, need_symref;
+static const char *head;
 
 /*
  * Used to parse format string and sort specifiers
@@ -676,6 +678,17 @@ static void populate_value(struct refinfo *ref)
 			}
 			continue;
 		}
+		else if (!strcmp(name, "current")) {
+			if (!head) {
+				unsigned char sha1[20];
+				head = resolve_refdup("HEAD", sha1, 0, NULL);
+			}
+			if (strcmp(head, "HEAD") && !strcmp(ref->refname, head))
+				v->s = "*";
+			else
+				v->s = " ";
+			continue;
+		}
 		else
 			continue;
 
Signature · 1 line
1.8.2.83.gc99314b
#9 lore raw

Re: [PATCH 4/9] for-each-ref: add %(current) for current branch marker

builtin/for-each-ref.c static struct { · line 79

Quoted 10 lines · Nguyễn Thái Ngọc Duy
builtin/for-each-ref.c +1 -0
index 08d4eb1..498d703 100644
@@ -75,6 +75,7 @@ static struct {
        { "upstream" },
        { "symref" },
        { "flag" },
+       { "current" },
 };

So you've implemented it as another atom. Good.

#10 lore raw

PATCH 5/9 for-each-ref: add %(tracking[:upstream]) for tracking info

In reply to Nguyễn Thái Ngọc Duy
---
 builtin/branch.c       |  4 ++--
 builtin/for-each-ref.c | 18 ++++++++++++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)
builtin/branch.c +2 -2
index 0836890..7d084da 100644
@@ -416,8 +416,8 @@ static int ref_cmp(const void *r1, const void *r2)
 	return strcmp(c1->name, c2->name);
 }
 
-static void fill_tracking_info(struct strbuf *stat, const char *branch_name,
-		int show_upstream_ref)
+void fill_tracking_info(struct strbuf *stat, const char *branch_name,
+			int show_upstream_ref)
 {
 	int ours, theirs;
 	char *ref = NULL;
builtin/for-each-ref.c +18 -0
index 498d703..b10d48a 100644
@@ -76,6 +76,8 @@ static struct {
 	{ "symref" },
 	{ "flag" },
 	{ "current" },
+	{ "tracking" },
+	{ "tracking:upstream" },
 };
 
 /*
@@ -615,6 +617,8 @@ static inline char *copy_advance(char *dst, const char *src)
 	return dst;
 }
 
+extern void fill_tracking_info(struct strbuf *stat, const char *branch_name,
+			       int show_upstream_ref);
 /*
  * Parse the object referred by ref, and grab needed value.
  */
@@ -689,6 +693,20 @@ static void populate_value(struct refinfo *ref)
 				v->s = " ";
 			continue;
 		}
+		else if (!strcmp(name, "tracking") &&
+			 !prefixcmp(ref->refname, "refs/heads/")) {
+			struct strbuf sb = STRBUF_INIT;
+			fill_tracking_info(&sb, ref->refname + 11, 0);
+			v->s = sb.buf;
+			continue;
+		}
+		else if (!strcmp(name, "tracking:upstream") &&
+			 !prefixcmp(ref->refname, "refs/heads/")) {
+			struct strbuf sb = STRBUF_INIT;
+			fill_tracking_info(&sb, ref->refname + 11, 1);
+			v->s = sb.buf;
+			continue;
+		}
 		else
 			continue;
 
Signature · 1 line
1.8.2.83.gc99314b
#11 lore raw

Re: [PATCH 5/9] for-each-ref: add %(tracking[:upstream]) for tracking info

builtin/for-each-ref.c static struct { · line 81

Quoted 11 lines · Nguyễn Thái Ngọc Duy
builtin/for-each-ref.c +2 -0
index 498d703..b10d48a 100644
@@ -76,6 +76,8 @@ static struct {
        { "symref" },
        { "flag" },
        { "current" },
+       { "tracking" },
+       { "tracking:upstream" },
 };

You just threw the upstream atom (and "upstream:short") out the window :|

#12 lore raw

Re: [PATCH 5/9] for-each-ref: add %(tracking[:upstream]) for tracking info

Quoted 14 lines · Ramkumar Ramachandra

builtin/for-each-ref.c static struct { · line 81

Quoted 11 lines · Nguyễn Thái Ngọc Duy
builtin/for-each-ref.c +2 -0
index 498d703..b10d48a 100644
@@ -76,6 +76,8 @@ static struct {
        { "symref" },
        { "flag" },
        { "current" },
+       { "tracking" },
+       { "tracking:upstream" },
 };

You just threw the upstream atom (and "upstream:short") out the window :|

Huh? Those don't print the tracking information, do they? "tracking:upstream" prints the upstream, but other things as well I suppose.

Signature · 1 line
Felipe Contreras
author
#13 lore raw

Re: [PATCH 5/9] for-each-ref: add %(tracking[:upstream]) for tracking info

Quoted 17 lines · Felipe Contreras
Quoted 14 lines · Ramkumar Ramachandra

builtin/for-each-ref.c static struct { · line 81

Quoted 11 lines · Nguyễn Thái Ngọc Duy
builtin/for-each-ref.c +2 -0
index 498d703..b10d48a 100644
@@ -76,6 +76,8 @@ static struct {
        { "symref" },
        { "flag" },
        { "current" },
+       { "tracking" },
+       { "tracking:upstream" },
 };

You just threw the upstream atom (and "upstream:short") out the window :|

Quoted 3 lines

Huh? Those don't print the tracking information, do they? "tracking:upstream" prints the upstream, but other things as well I suppose.

Exactly. I already explained why %(upstream) can't be used in 00/09. "tracking" may not be perfect. Somebody might want "tracking:upstream:short". It does not look quite nice. --
Duy

#14 lore raw

Re: [PATCH 5/9] for-each-ref: add %(tracking[:upstream]) for tracking info

Quoted 3 lines · Duy Nguyen

Exactly. I already explained why %(upstream) can't be used in 00/09. "tracking" may not be perfect. Somebody might want "tracking:upstream:short". It does not look quite nice.

Which is why I suggested keeping upstream, upstream:short, and introducing upstream:diff and upstream:shortdiff (or :tracking if you prefer that) in [0/9].

#15 lore raw

Re: [PATCH 5/9] for-each-ref: add %(tracking[:upstream]) for tracking info

Quoted 8 lines · Ramkumar Ramachandra
Quoted 3 lines · Duy Nguyen

Exactly. I already explained why %(upstream) can't be used in 00/09. "tracking" may not be perfect. Somebody might want "tracking:upstream:short". It does not look quite nice.

Which is why I suggested keeping upstream, upstream:short, and introducing upstream:diff and upstream:shortdiff (or :tracking if you prefer that) in [0/9].

Yeah, but there won't be any upstream in %(tracking). Besides, if we manage to get downstream, we could do %(tracking:downstream). I think %(tracking) and %(short:tracking) make sense.

Signature · 1 line
Felipe Contreras
#16 lore raw

PATCH 6/9 for-each-ref: add %(color:...)

In reply to Nguyễn Thái Ngọc Duy
---
 branch.h               | 11 +++++++++++
 builtin/branch.c       | 12 ++----------
 builtin/for-each-ref.c | 34 +++++++++++++++++++++++++++++++++-
 3 files changed, 46 insertions(+), 11 deletions(-)
branch.h +11 -0
index 64173ab..076babf 100644
@@ -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
builtin/branch.c +2 -10
index 7d084da..1aa282c 100644
@@ -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];
builtin/for-each-ref.c +33 -1
index b10d48a..db5c211 100644
@@ -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;
Signature · 1 line
1.8.2.83.gc99314b
#17 lore raw

Re: [PATCH 6/9] for-each-ref: add %(color:...)

builtin/for-each-ref.c static struct { · line 85

Quoted 10 lines · Nguyễn Thái Ngọc Duy
builtin/for-each-ref.c +1 -0
index b10d48a..db5c211 100644
@@ -78,6 +81,7 @@ static struct {
        { "current" },
        { "tracking" },
        { "tracking:upstream" },
+       { "color" },
 };

No, I intentionally did not make color an atom in $gmane/224692. It doesn't print any information about the branch, like the other atoms
do: have it augment the final printing.

Quoted 10 lines
builtin/for-each-ref.c +5 -0
 /*
@@ -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;

builtin/for-each-ref.c static void populate_value(struct refinfo *ref) · line 728

Quoted 10 lines
builtin/for-each-ref.c +10 -0
+                       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;
+               }

So I can't have %(color:yellow)? :(
Why are you parsing it here when you can use color_parse_name()?

#18 lore raw

PATCH 7/9 for-each-ref: prepoplulate all atoms before show_ref()

In reply to Nguyễn Thái Ngọc Duy

By the time show_ref() is called, atom values for all refs are ready. This can be taken advantage of later.

---
 builtin/for-each-ref.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
builtin/for-each-ref.c +14 -5
index db5c211..a9d189c 100644
@@ -1010,15 +1010,24 @@ static void show_ref(struct strbuf *sb, struct refinfo *info,
 static void show_refs(struct refinfo **refs, int maxcount,
 		      const char *format, int quote_style)
 {
-	struct strbuf sb = STRBUF_INIT;
+	struct strbuf *sb;
 	int i;
 
+	sb = xmalloc(sizeof(*sb) * maxcount);
 	for (i = 0; i < maxcount; i++) {
-		strbuf_reset(&sb);
-		show_ref(&sb, refs[i], format, quote_style);
-		fputs(sb.buf, stdout);
+		strbuf_init(sb + i, 256);
+		if (!refs[i]->value) {
+			populate_value(refs[i]);
+			fill_missing_values(refs[i]->value);
+		}
+	}
+
+	for (i = 0; i < maxcount; i++) {
+		show_ref(sb + i, refs[i], format, quote_style);
+		fputs(sb[i].buf, stdout);
+		strbuf_release(sb + i);
 	}
-	strbuf_release(&sb);
+	free(sb);
 }
 
 static struct ref_sort *default_sort(void)
Signature · 1 line
1.8.2.83.gc99314b
#19 lore raw

PATCH 8/9 for-each-ref: merge show_ref into show_refs

In reply to Nguyễn Thái Ngọc Duy
---
 builtin/for-each-ref.c | 39 +++++++++++++++++++--------------------
 1 file changed, 19 insertions(+), 20 deletions(-)
builtin/for-each-ref.c +19 -20
index a9d189c..1390da8 100644
@@ -989,29 +989,12 @@ static void emit(struct strbuf *sb, const char *cp, const char *ep)
 	}
 }
 
-static void show_ref(struct strbuf *sb, struct refinfo *info,
-		     const char *format, int quote_style)
-{
-	const char *cp, *sp, *ep;
-
-	for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
-		ep = strchr(sp, ')');
-		if (cp < sp)
-			emit(sb, cp, sp);
-		print_value(sb, info, parse_atom(sp + 2, ep), quote_style);
-	}
-	if (*cp) {
-		sp = cp + strlen(cp);
-		emit(sb, cp, sp);
-	}
-	strbuf_addch(sb, '\n');
-}
-
 static void show_refs(struct refinfo **refs, int maxcount,
 		      const char *format, int quote_style)
 {
 	struct strbuf *sb;
-	int i;
+	const char *cp, *sp, *ep;
+	int i, atom;
 
 	sb = xmalloc(sizeof(*sb) * maxcount);
 	for (i = 0; i < maxcount; i++) {
@@ -1022,8 +1005,24 @@ static void show_refs(struct refinfo **refs, int maxcount,
 		}
 	}
 
+	for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
+		ep = strchr(sp, ')');
+		if (cp < sp) {
+			for (i = 0; i < maxcount; i++)
+				emit(sb + i, cp, sp);
+		}
+		atom = parse_atom(sp + 2, ep);
+		for (i = 0; i < maxcount; i++)
+			print_value(sb + i, refs[i], atom, quote_style);
+	}
+	if (*cp) {
+		sp = cp + strlen(cp);
+		for (i = 0; i < maxcount; i++)
+			emit(sb + i, cp, sp);
+	}
+
 	for (i = 0; i < maxcount; i++) {
-		show_ref(sb + i, refs[i], format, quote_style);
+		strbuf_addch(sb + i, '\n');
 		fputs(sb[i].buf, stdout);
 		strbuf_release(sb + i);
 	}
Signature · 1 line
1.8.2.83.gc99314b
#20 lore raw

PATCH 9/9 for-each-ref: support %(...:aligned) for left alignment

In reply to Nguyễn Thái Ngọc Duy
---
 builtin/for-each-ref.c | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)
builtin/for-each-ref.c +22 -3
index 1390da8..3240ca0 100644
@@ -11,6 +11,7 @@
 #include "remote.h"
 #include "color.h"
 #include "branch.h"
+#include "utf8.h"
 
 /* Quoting styles */
 #define QUOTE_NONE 0
@@ -733,7 +734,7 @@ static void populate_value(struct refinfo *ref)
 		/* look for "short" refname format */
 		if (formatp) {
 			formatp++;
-			if (!strcmp(formatp, "short"))
+			if (!prefixcmp(formatp, "short"))
 				refname = shorten_unambiguous_ref(refname,
 						      warn_ambiguous_refs);
 			else
@@ -994,7 +995,7 @@ static void show_refs(struct refinfo **refs, int maxcount,
 {
 	struct strbuf *sb;
 	const char *cp, *sp, *ep;
-	int i, atom;
+	int i, atom, aligned, max_length, len;
 
 	sb = xmalloc(sizeof(*sb) * maxcount);
 	for (i = 0; i < maxcount; i++) {
@@ -1012,8 +1013,26 @@ static void show_refs(struct refinfo **refs, int maxcount,
 				emit(sb + i, cp, sp);
 		}
 		atom = parse_atom(sp + 2, ep);
-		for (i = 0; i < maxcount; i++)
+		aligned = !suffixcmp(used_atom[atom], ":aligned");
+		for (i = 0, max_length = 0; aligned && i < maxcount; i++) {
+			struct atom_value *v;
+			get_value(refs[i], atom, &v);
+			len = utf8_strnwidth(v->s, -1, 1);
+			if (len > max_length)
+				max_length = len;
+		}
+		for (i = 0; i < maxcount; i++) {
+			int old_len = sb[i].len;
 			print_value(sb + i, refs[i], atom, quote_style);
+			if (aligned) {
+				len = max_length -
+					utf8_strnwidth(sb[i].buf + old_len, -1, 1);
+				while (len) {
+					strbuf_addch(sb + i, ' ');
+					len--;
+				}
+			}
+		}
 	}
 	if (*cp) {
 		sp = cp + strlen(cp);
Signature · 1 line
1.8.2.83.gc99314b
#21 lore raw

Re: [PATCH 9/9] for-each-ref: support %(...:aligned) for left alignment

I don't think [7/9] and [8/9] belong in this series. Let's see how you've used it in :aligned.

Quoted 6 lines · Nguyễn Thái Ngọc Duy
builtin/for-each-ref.c +0 -0
index 1390da8..3240ca0 100644
@@ -1012,8 +1013,26 @@ static void show_refs(struct refinfo **refs, int maxcount,
                                emit(sb + i, cp, sp);

builtin/for-each-ref.c static void show_refs(struct refinfo **refs, int maxcount, · line 1022

Quoted 10 lines
builtin/for-each-ref.c +7 -1
                }
                atom = parse_atom(sp + 2, ep);
-               for (i = 0; i < maxcount; i++)
+               aligned = !suffixcmp(used_atom[atom], ":aligned");
+               for (i = 0, max_length = 0; aligned && i < maxcount; i++) {
+                       struct atom_value *v;
+                       get_value(refs[i], atom, &v);
+                       len = utf8_strnwidth(v->s, -1, 1);
+                       if (len > max_length)
+                               max_length = len;

Why?! Why are you denying me the pleasure of using %<, %<|, %>, %>|, %>>, %>>|, %<>, and %<>| that you invented in pretty? The code is already there: you just have to hook it up.

author
#22 lore raw

Re: [PATCH 9/9] for-each-ref: support %(...:aligned) for left alignment

Quoted 16 lines · Ramkumar Ramachandra

I don't think [7/9] and [8/9] belong in this series. Let's see how you've used it in :aligned.

Quoted 12 lines · Nguyễn Thái Ngọc Duy
builtin/for-each-ref.c +3 -1
index 1390da8..3240ca0 100644
@@ -1012,8 +1013,26 @@ static void show_refs(struct refinfo **refs, int maxcount,
                                emit(sb + i, cp, sp);
                }
                atom = parse_atom(sp + 2, ep);
-               for (i = 0; i < maxcount; i++)
+               aligned = !suffixcmp(used_atom[atom], ":aligned");
+               for (i = 0, max_length = 0; aligned && i < maxcount; i++) {
+                       struct atom_value *v;
Quoted 8 lines

builtin/for-each-ref.c static void show_refs(struct refinfo **refs, int maxcount, · line 1022

Quoted 4 lines
builtin/for-each-ref.c +4 -0
+                       get_value(refs[i], atom, &v);
+                       len = utf8_strnwidth(v->s, -1, 1);
+                       if (len > max_length)
+                               max_length = len;

Why?! Why are you denying me the pleasure of using %<, %<|, %>, %>|, %>>, %>>|, %<>, and %<>| that you invented in pretty? The code is already there: you just have to hook it up.

Because for-each-ref only understands %(...) not %<|, i.e. % followed by a (. We need more changes in for-each-ref code to make it accept %<|, I think. Also %<|(N) needs "N". In case of "branch -v" that should be calculated automatically, so we need a syntax to say "align to the left, use the smallest width that fits all". I guess "%<|(*)"? --
Duy

#23 lore raw
In reply to Nguyễn Thái Ngọc Duy
Quoted 3 lines · Nguyễn Thái Ngọc Duy

The purpose of this series is to make "for-each-ref --format" powerful enough to display what "branch -v" and "branch -vv" do so that we could get rid of those display code and use for-each-ref code instead.

Damn, you beat me to it. I just introduced color, and was working on alignment. See $gmane/224692.

Yes, I think this is the direction we should be taking. Poorly thought-out stuff like -v and -vv should be deprecated.

Quoted 6 lines

This series introduces:

- %(current), which either shows "*" if the ref is pointed by HEAD or a space. Junio called it %(headness). I don't like that. I don't like %(current) either but we have to start somewhere. Name suggestion? %(marker)??

How about %(HEAD)?

Quoted 3 lines

- %(tracking[:upstream]) gives us the exact output that branch -v[v] does. %(upstream) does not include []. We can't change its semantics.

There's already an atom called "upstream", and "upstream:short" works.
Why not introduce "upstream:diff" for "[ahead x, behind y]" and
"upstream:shortdiff" for "<>" (like in the prompt)?

Quoted 3 lines

- %(color:...) is pretty much the same as %C family in pretty code. I haven't added code for %(color:foo) == %C(foo) yet. There's a potential ambiguity here: %C(red) == %Cred or %C(red)??

I'd vote for dropping %C<name> altogether and just go with %C(<name>).
Why do we need %(color:<name>) at all?

Quoted 5 lines

- %(...:aligned) to do left aligning. I'm not entirely sure about this. We might be able to share code with %>, %< and %>< from pretty.c. But we need improvements there too because in for-each-ref case, we could calculate column width but %< would require the user to specify the width.

Yeah, I think we should go with the %> and %< you introduced in pretty.c. Yes, I want to be able to specify width.

Quoted 3 lines

Do people expect fancy layout with for-each-ref (and branch)? If so we might need to have %(align) or something instead of the simple left alignment case in %(...:aligned)

Why should we deviate from the pretty case? What is different here?

Quoted 4 lines

- We may need an equivalent of the space following % in pretty format. If the specifier produces something, then prepend a space, otherwise produce nothing. Do it like %C( tracking) vs %C(tracking)??

Yeah, sounds good.

Quoted 2 lines

You can try this after applying the series, which should give you the about close to 'branch -v'. %(tracking) coloring does not work though.

Why doesn't %(tracking) coloring work?

Quoted 1 line

Nguyễn Thái Ngọc Duy (9):

I'll have a look at this.

Also, I think it'll help to have a --pretty="format:<string>" equivalent to --format="<string>" so that we can introduce pretty names like oneline, short, medium, full. We can eventually deprecate --format for consistency.

author
#24 lore raw
Quoted 7 lines · Ramkumar Ramachandra
Quoted 3 lines · Nguyễn Thái Ngọc Duy

The purpose of this series is to make "for-each-ref --format" powerful enough to display what "branch -v" and "branch -vv" do so that we could get rid of those display code and use for-each-ref code instead.

Damn, you beat me to it. I just introduced color, and was working on alignment. See $gmane/224692.

Hmm.. I missed that mail (or I wouldn't have worked on this already). Do you want to take over?

Quoted 7 lines
Quoted 3 lines

- %(tracking[:upstream]) gives us the exact output that branch -v[v] does. %(upstream) does not include []. We can't change its semantics.

There's already an atom called "upstream", and "upstream:short" works.
Why not introduce "upstream:diff" for "[ahead x, behind y]" and
"upstream:shortdiff" for "<>" (like in the prompt)?

"branch -vv" shows [upstream: ahead x, behind y]. We need a syntax to cover that too.

Quoted 6 lines
Quoted 3 lines

- %(color:...) is pretty much the same as %C family in pretty code. I haven't added code for %(color:foo) == %C(foo) yet. There's a potential ambiguity here: %C(red) == %Cred or %C(red)??

I'd vote for dropping %C<name> altogether and just go with %C(<name>).
Why do we need %(color:<name>) at all?

pretty and for-each-ref format seem to be on the opposite: one is terse, one verbose. Unless you are going to introduce a lot of new specifiers (and in the worst case, bring all pretty specifiers over, unify underlying code), I think we should stick with %(xx) convention.

Quoted 8 lines
Quoted 5 lines

- %(...:aligned) to do left aligning. I'm not entirely sure about this. We might be able to share code with %>, %< and %>< from pretty.c. But we need improvements there too because in for-each-ref case, we could calculate column width but %< would require the user to specify the width.

Yeah, I think we should go with the %> and %< you introduced in pretty.c. Yes, I want to be able to specify width.

I still think we should follow %(...), e.g. %(left:N), %(right:N) as equivalent of %< and %>...

Quoted 5 lines
Quoted 3 lines

Do people expect fancy layout with for-each-ref (and branch)? If so we might need to have %(align) or something instead of the simple left alignment case in %(...:aligned)

Why should we deviate from the pretty case? What is different here?

Laziness plays a big factor :) So again, you want to take over? ;)

Quoted 11 lines
Quoted 4 lines

- We may need an equivalent of the space following % in pretty format. If the specifier produces something, then prepend a space, otherwise produce nothing. Do it like %C( tracking) vs %C(tracking)??

Yeah, sounds good.

Quoted 2 lines

You can try this after applying the series, which should give you the about close to 'branch -v'. %(tracking) coloring does not work though.

Why doesn't %(tracking) coloring work?

it uses builtin/branch.c:branch_use_color. Eventually fill_tracking_info() should be moved to for-each-ref.c and pass branch_use_color in as an argument. But for now, I just leave it broken.
--
Duy

#25 lore raw
Quoted 2 lines · Duy Nguyen

Hmm.. I missed that mail (or I wouldn't have worked on this already). Do you want to take over?

Oh, we can collaborate on one beautiful series :)

Quoted 2 lines

"branch -vv" shows [upstream: ahead x, behind y]. We need a syntax to cover that too.

Can't we construct that using [%(upstream:short): %(upstream:diff)]? It's nothing fundamental.

Quoted 4 lines

pretty and for-each-ref format seem to be on the opposite: one is terse, one verbose. Unless you are going to introduce a lot of new specifiers (and in the worst case, bring all pretty specifiers over, unify underlying code), I think we should stick with %(xx) convention.

We can stick to using the existing %(...) atoms: there's no need to go as far as %an versus %aN. The atoms cannot be consistent with pretty-formats anyway, because pretty-formats has completely different atoms. For the _new_ stuff like color and alignment, we can be consistent with pretty-formats, no?

Quoted 3 lines
Quoted 1 line

Why should we deviate from the pretty case? What is different here?

Laziness plays a big factor :) So again, you want to take over? ;)

It's just a matter of modifying the parsing/printing layer, instead of introducing new atoms in the current parser. Doesn't $gmane/224692 demonstrate that the former can, in fact, be easier?

Quoted 4 lines

it uses builtin/branch.c:branch_use_color. Eventually fill_tracking_info() should be moved to for-each-ref.c and pass branch_use_color in as an argument. But for now, I just leave it broken.

Auto-color can come later: it's not that urgent. What's more important is that we give users the flexibility to set their own colors now.

Can you give me a pull URL to your series, so we can start collaborating? Mine's at gh:artagnon/git (branch: hot-branch).

author
#26 lore raw
Quoted 5 lines · Ramkumar Ramachandra
Quoted 2 lines

"branch -vv" shows [upstream: ahead x, behind y]. We need a syntax to cover that too.

Can't we construct that using [%(upstream:short): %(upstream:diff)]? It's nothing fundamental.

If there is no upstream, [] should not be shown. We don't have conditional specifiers so [] should be produced by one of the specifiers. But yes it's nothing fundamental.

Quoted 10 lines
Quoted 4 lines

pretty and for-each-ref format seem to be on the opposite: one is terse, one verbose. Unless you are going to introduce a lot of new specifiers (and in the worst case, bring all pretty specifiers over, unify underlying code), I think we should stick with %(xx) convention.

We can stick to using the existing %(...) atoms: there's no need to go as far as %an versus %aN. The atoms cannot be consistent with pretty-formats anyway, because pretty-formats has completely different atoms. For the _new_ stuff like color and alignment, we can be consistent with pretty-formats, no?

I don't think you can easily borrow parsing code from pretty-formats (but I may be wrong). Anyway new stuff with new syntax would look alien in for-each-ref format lines. Either we bring --pretty to for-each-ref, leaving all for-each-ref atoms behind in --format, or we should follow %(..) convention if we add new stuff to --format.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Why should we deviate from the pretty case? What is different here?

Laziness plays a big factor :) So again, you want to take over? ;)

It's just a matter of modifying the parsing/printing layer, instead of introducing new atoms in the current parser. Doesn't $gmane/224692 demonstrate that the former can, in fact, be easier?

Yes it looks so.

Quoted 6 lines
Quoted 5 lines

it uses builtin/branch.c:branch_use_color. Eventually fill_tracking_info() should be moved to for-each-ref.c and pass branch_use_color in as an argument. But for now, I just leave it broken.

Quoted 6 lines

Auto-color can come later: it's not that urgent. What's more important is that we give users the flexibility to set their own colors now.

Can you give me a pull URL to your series, so we can start collaborating? Mine's at gh:artagnon/git (branch: hot-branch).

https://github.com/pclouds/git/tree/for-each-ref-pretty

I merged 07/09 and 08/09 together as they should be one. --
Duy

#27 lore raw
Quoted 5 lines · Duy Nguyen

I don't think you can easily borrow parsing code from pretty-formats (but I may be wrong). Anyway new stuff with new syntax would look alien in for-each-ref format lines. Either we bring --pretty to for-each-ref, leaving all for-each-ref atoms behind in --format, or we should follow %(..) convention if we add new stuff to --format.

Why so extremist? pretty-formats has %(...), %C(...) as well as %..., so why shouldn't we? Our format is undocumented, and I doubt anyone even uses it; we're not breaking anyone's expectations. I'm just saying that our format can be a little reminiscent of pretty-formats, nothing more. There's no need to borrow parsing code: we can do it ourselves, I think. There is no need to go to the other extreme and throw out the existing --format and start out with a --pretty from scratch either: the current code isn't so bad that we can't build on top of it. Sure, we can eventually deprecate --format and move to --pretty for consistency (but that's a long-term goal).

#28 lore raw
In reply to Ramkumar Ramachandra
Quoted 2 lines · Ramkumar Ramachandra

Yes, I think this is the direction we should be taking. Poorly thought-out stuff like -v and -vv should be deprecated.

Of course not. They are useful and user-friendly.

The only question is what should be the format by default.

Quoted 4 lines

Also, I think it'll help to have a --pretty="format:<string>" equivalent to --format="<string>" so that we can introduce pretty names like oneline, short, medium, full. We can eventually deprecate --format for consistency.

I don't see the point of --pretty. I think there should be shortcuts, something like --hash for the SHA1s only, and --names for the refs only, or something like that.

Signature · 1 line
Felipe Contreras
#29 lore raw
In reply to Nguyễn Thái Ngọc Duy
Quoted 2 lines · Nguyễn Thái Ngọc Duy

Originally I wanted to introduce --pretty with git-log's pretty syntax to for-each-ref, deprecating --format.

If you are going to unify the two mechanisms, I think the "--format" option of "for-each-ref" needs to become a superset of what the "--pretty" option of "log/rev-list" can express and shared between the two. The former has to handle anything that can be at the tip of a ref, including commits, so it needs to be able to do anything the latter can.

#30 lore raw
In reply to Nguyễn Thái Ngọc Duy
Quoted 6 lines · Nguyễn Thái Ngọc Duy

This series introduces:

- %(current), which either shows "*" if the ref is pointed by HEAD or a space. Junio called it %(headness). I don't like that. I don't like %(current) either but we have to start somewhere. Name suggestion? %(marker)??

"marker" will paint us into a corner we cannot get out of when we want to mark refs with different criteria later. At least "current" tells you what kind of marker we are talking about and is 1000 times better.