Skip to contentSkip to conversation

Port tag.c to use ref-filter.c

PATCH Superseded Karthik Nayak 5 participants 52 messages Jun 15, 2016 Documentation

v14 of 17 Superseded Latest is v17 →

lore
Share & tools
lore
#0 lore raw

v14 cover Port tag.c to use ref-filter.c

This is part of porting tag.c to ref-filter APIs. Version 13 can be found:
thread.gmane.org/gmane.comp.version-control.git/276363

Changes in this version:
* Introduce format_ref_array_item() and make show_ref_array_item() a wrapper around the same.
* Introduce %(contents:lines=X) which gets the first X lines from a given object.
* Change code in 05/13 to make the code neater and consistent.
* %(align) without arguments fails now.
* Add test for nested alignment.
* We perform quoting on each layer of nested alignment.

Karthik Nayak (13):
ref-filter: move struct atom_value to ref-filter.c
ref-filter: introduce ref_formatting_state and ref_formatting_stack
utf8: add function to align a string into given strbuf
ref-filter: implement an align atom
ref-filter: add option to filter out tags, branches and remotes
ref-filter: introduce format_ref_array_item()
ref-filter: add support for %(contents:lines=X)
ref-filter: add support to sort by version
ref-filter: add option to match literal pattern
tag.c: use 'ref-filter' data structures
tag.c: use 'ref-filter' APIs
tag.c: implement '--format' option
tag.c: implement '--merged' and '--no-merged' options

 Documentation/git-for-each-ref.txt |  13 ++
 Documentation/git-tag.txt          |  27 ++-
 builtin/for-each-ref.c             |   1 +
 builtin/tag.c                      | 368 ++++++--------------------------
 ref-filter.c                       | 418 ++++++++++++++++++++++++++++++++-----
 ref-filter.h                       |  32 ++-
 refs.c                             |   9 +
 refs.h                             |   1 +
 t/t6302-for-each-ref-filter.sh     | 137 ++++++++++++
 t/t7004-tag.sh                     |  47 ++++-
 utf8.c                             |  21 ++
 utf8.h                             |  15 ++
 12 files changed, 712 insertions(+), 377 deletions(-)

Interdiff between version 13 and version 14

Documentation/git-for-each-ref.txt +1 -0
index 06d468e..1b48b95 100644
@@ -149,6 +149,7 @@ Its first line is `contents:subject`, where subject is the concatenation
 of all lines of the commit message up to the first blank line.  The next
 line is 'contents:body', where body is all of the lines after the first
 blank line.  Finally, the optional GPG signature is `contents:signature`.
+The first `N` lines of the object is obtained using `contents:lines=N`.
 
 For sorting purposes, fields with numeric values sort in numeric
 order (`objectsize`, `authordate`, `committerdate`, `taggerdate`).
builtin/for-each-ref.c +1 -1
index 3ad6a64..4e9f6c2 100644
@@ -75,7 +75,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 	if (!maxcount || array.nr < maxcount)
 		maxcount = array.nr;
 	for (i = 0; i < maxcount; i++)
-		show_ref_array_item(array.items[i], format, quote_style, 0);
+		show_ref_array_item(array.items[i], format, quote_style);
 	ref_array_clear(&array);
 	return 0;
 }
builtin/tag.c +5 -2
index bbbcaed..9fa1400 100644
@@ -33,6 +33,7 @@ static unsigned int colopts;
 static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, const char *format)
 {
 	struct ref_array array;
+	char *to_free = NULL;
 	int i;
 
 	memset(&array, 0, sizeof(array));
@@ -42,7 +43,8 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, con
 
 	if (!format) {
 		if (filter->lines)
-			format = "%(align:16,left)%(refname:short)%(end)";
+			format = to_free = xstrfmt("%%(align:15,left)%%(refname:short)%%(end) %%(contents:lines=%d)",
+						   filter->lines);
 		else
 			format = "%(refname:short)";
 	}
@@ -52,8 +54,9 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, con
 	ref_array_sort(sorting, &array);
 
 	for (i = 0; i < array.nr; i++)
-		show_ref_array_item(array.items[i], format, QUOTE_NONE, filter->lines);
+		show_ref_array_item(array.items[i], format, 0);
 	ref_array_clear(&array);
+	free(to_free);
 
 	return 0;
 }
ref-filter.c +157 -123

408 lines not on this page · raw message

ref-filter.h +6 -6
index 8241066..179944c 100644
@@ -21,6 +21,7 @@
 #define FILTER_REFS_ALL            (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \
 				    FILTER_REFS_REMOTES | FILTER_REFS_OTHERS)
 #define FILTER_REFS_DETACHED_HEAD  0x0020
+#define FILTER_REFS_KIND_MASK      (FILTER_REFS_ALL | FILTER_REFS_DETACHED_HEAD)
 
 struct atom_value;
 
@@ -93,12 +94,11 @@ int parse_ref_filter_atom(const char *atom, const char *ep);
 int verify_ref_format(const char *format);
 /*  Sort the given ref_array as per the ref_sorting provided */
 void ref_array_sort(struct ref_sorting *sort, struct ref_array *array);
-/*
- * Print the ref using the given format and quote_style. If 'lines' > 0,
- * print that many lines of the the given ref.
- */
-void show_ref_array_item(struct ref_array_item *info, const char *format,
-			 int quote_style, unsigned int lines);
+/*  Format the ref as per given format and quote_style and store it into the strbuf */
+void format_ref_array_item(struct strbuf *out, struct ref_array_item *info,
+			   const char *format, int quote_style);
+/*  Wrapper around format_ref_array_item() which prints the given ref_array_item */
+void show_ref_array_item(struct ref_array_item *item, const char *format, unsigned int quote_style);
 /*  Callback function for parsing the sort option */
 int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset);
 /*  Default sort option based on refname */
refs.c +9 -9
index 3266617..a9469c2 100644
@@ -2108,6 +2108,15 @@ int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
 	return do_for_each_ref(&ref_cache, prefix, fn, strlen(prefix), 0, cb_data);
 }
 
+int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken)
+{
+	unsigned int flag = 0;
+
+	if (broken)
+		flag = DO_FOR_EACH_INCLUDE_BROKEN;
+	return do_for_each_ref(&ref_cache, prefix, fn, 0, flag, cb_data);
+}
+
 int for_each_ref_in_submodule(const char *submodule, const char *prefix,
 		each_ref_fn fn, void *cb_data)
 {
@@ -2150,15 +2159,6 @@ int for_each_replace_ref(each_ref_fn fn, void *cb_data)
 			       strlen(git_replace_ref_base), 0, cb_data);
 }
 
-int for_each_reftype_fullpath(each_ref_fn fn, char *type, unsigned int broken, void *cb_data)
-{
-	unsigned int flag = 0;
-
-	if (broken)
-		flag = DO_FOR_EACH_INCLUDE_BROKEN;
-	return do_for_each_ref(&ref_cache, type, fn, 0, flag, cb_data);
-}
-
 int head_ref_namespaced(each_ref_fn fn, void *cb_data)
 {
 	struct strbuf buf = STRBUF_INIT;
refs.h +1 -1
index 6e913ee..6d30c98 100644
@@ -173,13 +173,13 @@ typedef int each_ref_fn(const char *refname,
 extern int head_ref(each_ref_fn fn, void *cb_data);
 extern int for_each_ref(each_ref_fn fn, void *cb_data);
 extern int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data);
+extern int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken);
 extern int for_each_tag_ref(each_ref_fn fn, void *cb_data);
 extern int for_each_branch_ref(each_ref_fn fn, void *cb_data);
 extern int for_each_remote_ref(each_ref_fn fn, void *cb_data);
 extern int for_each_replace_ref(each_ref_fn fn, void *cb_data);
 extern int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data);
 extern int for_each_glob_ref_in(each_ref_fn fn, const char *pattern, const char *prefix, void *cb_data);
-extern int for_each_reftype_fullpath(each_ref_fn fn, char *type, unsigned int broken, void *cb_data);
 
 extern int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
 extern int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
t/t6302-for-each-ref-filter.sh +32 -0
index 38c99c9..8f18f86 100755
@@ -150,6 +150,38 @@ test_expect_success 'alignment with format quote' '
 	test_cmp expect actual
 '
 
+test_expect_success 'nested alignment' '
+	cat >expect <<-\EOF &&
+	|         master               |
+	|           side               |
+	|       odd/spot               |
+	|     double-tag               |
+	|           four               |
+	|            one               |
+	|     signed-tag               |
+	|          three               |
+	|            two               |
+	EOF
+	git for-each-ref --format="|%(align:30,left)%(align:15,right)%(refname:short)%(end)%(end)|" >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'check `%(contents:lines=X)`' '
+	cat >expect <<-\EOF &&
+	master three
+	side four
+	odd/spot three
+	double-tag Annonated doubly
+	four four
+	one one
+	signed-tag A signed tag message
+	three three
+	two two
+	EOF
+	git for-each-ref --format="%(refname:short) %(contents:lines=1)" >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'setup for version sort' '
 	test_commit foo1.3 &&
 	test_commit foo1.6 &&
Signature · 1 line
2.5.0
#1 lore raw

PATCH v14 1/13 ref-filter: move `struct atom_value` to ref-filter.c

Since atom_value is only required for the internal working of ref-filter it doesn't belong in the public header.

---
 ref-filter.c | 5 +++++
 ref-filter.h | 5 +----
 2 files changed, 6 insertions(+), 4 deletions(-)
ref-filter.c +5 -0
index 46963a5..e53c77e 100644
@@ -55,6 +55,11 @@ static struct {
 	{ "color" },
 };
 
+struct atom_value {
+	const char *s;
+	unsigned long ul; /* used for sorting when not FIELD_STR */
+};
+
 /*
  * An atom is a valid field atom listed above, possibly prefixed with
  * a "*" to denote deref_tag().
ref-filter.h +1 -4
index 6bf27d8..45026d0 100644
@@ -16,10 +16,7 @@
 #define FILTER_REFS_INCLUDE_BROKEN 0x1
 #define FILTER_REFS_ALL 0x2
 
-struct atom_value {
-	const char *s;
-	unsigned long ul; /* used for sorting when not FIELD_STR */
-};
+struct atom_value;
 
 struct ref_sorting {
 	struct ref_sorting *next;
Signature · 1 line
2.5.0
#2 lore raw

PATCH v14 2/13 ref-filter: introduce ref_formatting_state and ref_formatting_stack

In reply to Karthik Nayak

Introduce ref_formatting_state which will hold the formatted output strbuf instead of directly printing to stdout. This will help us in creating modifier atoms which modify the format specified before printing to stdout.

Implement a stack machinery for ref_formatting_state, this allows us to push and pop elements onto the stack. Whenever we pop an element from the stack, the strbuf from that element is appended to the strbuf of the next element on the stack, this will allow us to support nesting of modifier atoms.

Rename some functions to reflect the changes made:
print_value() -> append_atom()
emit() -> append_literal()

---
 ref-filter.c | 78 +++++++++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 59 insertions(+), 19 deletions(-)
ref-filter.c +59 -19
index e53c77e..432cea0 100644
@@ -55,6 +55,18 @@ static struct {
 	{ "color" },
 };
 
+#define REF_FORMATTING_STATE_INIT  { 0, NULL }
+
+struct ref_formatting_stack {
+	struct ref_formatting_stack *prev;
+	struct strbuf output;
+};
+
+struct ref_formatting_state {
+	int quote_style;
+	struct ref_formatting_stack *stack;
+};
+
 struct atom_value {
 	const char *s;
 	unsigned long ul; /* used for sorting when not FIELD_STR */
@@ -129,6 +141,27 @@ int parse_ref_filter_atom(const char *atom, const char *ep)
 	return at;
 }
 
+static void push_stack_element(struct ref_formatting_stack **stack)
+{
+	struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack));
+
+	strbuf_init(&s->output, 0);
+	s->prev = *stack;
+	*stack = s;
+}
+
+static void pop_stack_element(struct ref_formatting_stack **stack)
+{
+	struct ref_formatting_stack *current = *stack;
+	struct ref_formatting_stack *prev = current->prev;
+
+	if (prev)
+		strbuf_addbuf(&prev->output, &current->output);
+	strbuf_release(&current->output);
+	free(current);
+	*stack = prev;
+}
+
 /*
  * In a format string, find the next occurrence of %(atom).
  */
@@ -1195,30 +1228,27 @@ void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
 	qsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs);
 }
 
-static void print_value(struct atom_value *v, int quote_style)
+static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
 {
-	struct strbuf sb = STRBUF_INIT;
-	switch (quote_style) {
+	struct strbuf *s = &state->stack->output;
+
+	switch (state->quote_style) {
 	case QUOTE_NONE:
-		fputs(v->s, stdout);
+		strbuf_addstr(s, v->s);
 		break;
 	case QUOTE_SHELL:
-		sq_quote_buf(&sb, v->s);
+		sq_quote_buf(s, v->s);
 		break;
 	case QUOTE_PERL:
-		perl_quote_buf(&sb, v->s);
+		perl_quote_buf(s, v->s);
 		break;
 	case QUOTE_PYTHON:
-		python_quote_buf(&sb, v->s);
+		python_quote_buf(s, v->s);
 		break;
 	case QUOTE_TCL:
-		tcl_quote_buf(&sb, v->s);
+		tcl_quote_buf(s, v->s);
 		break;
 	}
-	if (quote_style != QUOTE_NONE) {
-		fputs(sb.buf, stdout);
-		strbuf_release(&sb);
-	}
 }
 
 static int hex1(char ch)
@@ -1239,8 +1269,10 @@ static int hex2(const char *cp)
 		return -1;
 }
 
-static void emit(const char *cp, const char *ep)
+static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state)
 {
+	struct strbuf *s = &state->stack->output;
+
 	while (*cp && (!ep || cp < ep)) {
 		if (*cp == '%') {
 			if (cp[1] == '%')
@@ -1248,13 +1280,13 @@ static void emit(const char *cp, const char *ep)
 			else {
 				int ch = hex2(cp + 1);
 				if (0 <= ch) {
-					putchar(ch);
+					strbuf_addch(s, ch);
 					cp += 3;
 					continue;
 				}
 			}
 		}
-		putchar(*cp);
+		strbuf_addch(s, *cp);
 		cp++;
 	}
 }
@@ -1262,19 +1294,24 @@ static void emit(const char *cp, const char *ep)
 void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
 {
 	const char *cp, *sp, *ep;
+	struct strbuf *final_buf;
+	struct ref_formatting_state state = REF_FORMATTING_STATE_INIT;
+
+	state.quote_style = quote_style;
+	push_stack_element(&state.stack);
 
 	for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
 		struct atom_value *atomv;
 
 		ep = strchr(sp, ')');
 		if (cp < sp)
-			emit(cp, sp);
+			append_literal(cp, sp, &state);
 		get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
-		print_value(atomv, quote_style);
+		append_atom(atomv, &state);
 	}
 	if (*cp) {
 		sp = cp + strlen(cp);
-		emit(cp, sp);
+		append_literal(cp, sp, &state);
 	}
 	if (need_color_reset_at_eol) {
 		struct atom_value resetv;
@@ -1283,8 +1320,11 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu
 		if (color_parse("reset", color) < 0)
 			die("BUG: couldn't parse 'reset' as a color");
 		resetv.s = color;
-		print_value(&resetv, quote_style);
+		append_atom(&resetv, &state);
 	}
+	final_buf = &state.stack->output;
+	fwrite(final_buf->buf, 1, final_buf->len, stdout);
+	pop_stack_element(&state.stack);
 	putchar('\n');
 }
 
Signature · 1 line
2.5.0
#3 lore raw

PATCH v14 3/13 utf8: add function to align a string into given strbuf

In reply to Karthik Nayak

Add strbuf_utf8_align() which will align a given string into a strbuf as per given align_type and width. If the width is greater than the string length then no alignment is performed.

---
 utf8.c | 21 +++++++++++++++++++++
 utf8.h | 15 +++++++++++++++
 2 files changed, 36 insertions(+)
utf8.c +21 -0
index 28e6d76..00e10c8 100644
@@ -644,3 +644,24 @@ int skip_utf8_bom(char **text, size_t len)
 	*text += strlen(utf8_bom);
 	return 1;
 }
+
+void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,
+		       const char *s)
+{
+	int slen = strlen(s);
+	int display_len = utf8_strnwidth(s, slen, 0);
+	int utf8_compensation = slen - display_len;
+
+	if (display_len >= width) {
+		strbuf_addstr(buf, s);
+		return;
+	}
+
+	if (position == ALIGN_LEFT)
+		strbuf_addf(buf, "%-*s", width + utf8_compensation, s);
+	else if (position == ALIGN_MIDDLE) {
+		int left = (width - display_len) / 2;
+		strbuf_addf(buf, "%*s%-*s", left, "", width - left + utf8_compensation, s);
+	} else if (position == ALIGN_RIGHT)
+		strbuf_addf(buf, "%*s", width + utf8_compensation, s);
+}
utf8.h +15 -0
index 5a9e94b..7930b44 100644
@@ -55,4 +55,19 @@ int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding);
  */
 int is_hfs_dotgit(const char *path);
 
+typedef enum {
+	ALIGN_LEFT,
+	ALIGN_MIDDLE,
+	ALIGN_RIGHT
+} align_type;
+
+/*
+ * Align the string given and store it into a strbuf as per the
+ * 'position' and 'width'. If the given string length is larger than
+ * 'width' than then the input string is not truncated and no
+ * alignment is done.
+ */
+void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,
+		       const char *s);
+
 #endif
Signature · 1 line
2.5.0
#4 lore raw

Re: [PATCH v14 03/13] utf8: add function to align a string into given strbuf

utf8.h int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding); · line 62

Quoted 13 lines · Karthik Nayak
utf8.h +5 -0
index 5a9e94b..7930b44 100644
@@ -55,4 +55,19 @@ int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding);
  */
 int is_hfs_dotgit(const char *path);
 
+typedef enum {
+	ALIGN_LEFT,
+	ALIGN_MIDDLE,
+	ALIGN_RIGHT
+} align_type;

should this be called strbuf_align_type ?

And is there a reason why the is in utf.c and not in stbuf.c ?

(I know that there is a lot of strbuf in utf8.c, but I hadn't managed to send a patch to move everything into strbuf.c and make utf8.c un-aware of all strbub-business)

#5 lore raw

Re: [PATCH v14 03/13] utf8: add function to align a string into given strbuf

Quoted 9 lines · Torsten Bögershausen
Quoted 8 lines · Karthik Nayak
utf8.h +0 -0
index 5a9e94b..7930b44 100644
@@ -55,4 +55,19 @@ int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding);
  */
 int is_hfs_dotgit(const char *path);
 
Quoted 7 lines

utf8.h int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding); · line 62

Quoted 5 lines
utf8.h +5 -0
+typedef enum {
+     ALIGN_LEFT,
+     ALIGN_MIDDLE,
+     ALIGN_RIGHT
+} align_type;

should this be called strbuf_align_type ?

align_type seemed descriptive and unique enough.

Quoted 5 lines

And is there a reason why the is in utf.c and not in stbuf.c ?

(I know that there is a lot of strbuf in utf8.c, but I hadn't managed to send a patch to move everything into strbuf.c and make utf8.c un-aware of all strbub-business)

This was based on Eric's suggestions.
http://article.gmane.org/gmane.comp.version-control.git/275456

It makes sense also, since rather than acting on a strbuf, this is more of just utilizing an strbuf to provide the result. whereas the real work is of alignment.

Signature · 2 lines
Regards,
Karthik Nayak
#6 lore raw

PATCH v14 4/13 ref-filter: implement an `align` atom

In reply to Karthik Nayak

Implement an align atom which left-, middle-, or right-aligns the content between %(align:..) and %(end).

It is followed by :<width>,<position>, where the <position> is either left, right or middle and <width> is the size of the area into which the content will be placed. If the content between %(align:) and %(end) is more than the width then no alignment is performed. e.g. to align a refname atom to the middle with a total width of 40 we can do: --format="%(align:middle,40)%(refname)%(end)".

We now have a handler() for each atom_value which will be called when that atom_value is being parsed, and similarly an at_end function for each element of the stack which is to be called when the end atom is encountered. Using this we implement the align atom which aligns the given strbuf by calling strbuf_utf8_align() from utf8.c.

Extract perform_quote_formatting() from append_atom(). Given a string a quote_value and a strbuf, perform_quote_formatting() formats the string based on the quote_value and stores it into the strbuf.

Ensure that quote formatting is performed on the whole of %(align)...%(end) rather than individual atoms. We do this by skipping individual quote formatting for atoms whenever the stack has more than one element, and performing formatting for the entire stack element when the %(end) atoms is encountered.

Add documentation and tests for the same.

---
 Documentation/git-for-each-ref.txt |   9 +++
 ref-filter.c                       | 151 +++++++++++++++++++++++++++++++------
 t/t6302-for-each-ref-filter.sh     |  85 +++++++++++++++++++++
 3 files changed, 221 insertions(+), 24 deletions(-)
Documentation/git-for-each-ref.txt +9 -0
index e49d578..943975d 100644
@@ -127,6 +127,15 @@ color::
 	Change output color.  Followed by `:<colorname>`, where names
 	are described in `color.branch.*`.
 
+align::
+	Left-, middle-, or right-align the content between %(align:..)
+	and %(end). Followed by `:<width>,<position>`, where the
+	`<position>` is either left, right or middle and `<width>` is
+	the total length of the content with alignment. If the
+	contents length is more than the width then no alignment is
+	performed. If used with '--quote' everything in between %(align:..)
+	and %(end) is quoted.
+
 In addition to the above, for commit and tag objects, the header
 field names (`tree`, `parent`, `object`, `type`, and `tag`) can
 be used to specify the value in the header field.
ref-filter.c +127 -24

213 lines not on this page · raw message

t/t6302-for-each-ref-filter.sh +85 -0

89 lines not on this page · raw message

Signature · 1 line
2.5.0
#7 lore raw

Re: [PATCH v14 04/13] ref-filter: implement an `align` atom

Quoted 21 lines · Karthik Nayak

Implement an align atom which left-, middle-, or right-aligns the content between %(align:..) and %(end).

It is followed by :<width>,<position>, where the <position> is either left, right or middle and <width> is the size of the area into which the content will be placed. If the content between %(align:) and %(end) is more than the width then no alignment is performed. e.g. to align a refname atom to the middle with a total width of 40 we can do: --format="%(align:middle,40)%(refname)%(end)".

We now have a handler() for each atom_value which will be called when that atom_value is being parsed, and similarly an at_end function for each element of the stack which is to be called when the end atom is encountered. Using this we implement the align atom which aligns the given strbuf by calling strbuf_utf8_align() from utf8.c.

Extract perform_quote_formatting() from append_atom(). Given a string a quote_value and a strbuf, perform_quote_formatting() formats the string based on the quote_value and stores it into the strbuf.

Quoted 5 lines

Ensure that quote formatting is performed on the whole of %(align)...%(end) rather than individual atoms. We do this by skipping individual quote formatting for atoms whenever the stack has more than one element, and performing formatting for the entire stack element when the %(end) atoms is encountered.

This patch seems to be conflating three distinct changes:

1. adding %(align:) atom
2. extracting quoting logic to a separate function
3. quoting top-level %(align:) but not contained atoms

In fact, #3 seems too specially tied to %(align:)...%(end). One might expect that the logic for determining when to quote should be independent of any particular atom, which suggests that this logic is being handled at the wrong level, and that %(align:) shouldn't have to know anything about quoting. I'd have thought the quoting logic would naturally accompany the introduction of the formatting state stack mechanism in patch 2/13, and that that would generically work with all atoms, including %(align:) and whatever new ones are added in the future.

But, I may not have been following the quoting discussion closely, so perhaps these observations are incorrect? See more below regarding %(if:).

Quoted 23 lines

Add documentation and tests for the same.

---

ref-filter.c +7 -0
index 432cea0..21c8b5f 100644
@@ -53,6 +54,13 @@ static struct {
        { "flag" },
        { "HEAD" },
        { "color" },
+       { "align" },
+       { "end" },
+};
+
+struct align {
+       align_type position;
+       unsigned int width;
 };
 
 #define REF_FORMATTING_STATE_INIT  { 0, NULL }

ref-filter.c struct ref_formatting_state { · line 82

Quoted 4 lines
ref-filter.c +1 -0
@@ -69,6 +79,8 @@ struct ref_formatting_state {
 
 struct atom_value {
        const char *s;
+       struct align *align;

Why does 'align' need to be heap-allocated rather than just being a direct member of 'atom_value'? Does 'align' need to exist beyond the lifetime of its 'atom_value'? If not, making it a direct member might simplify resource management (no need to free it).

Quoted 5 lines
ref-filter.c +1 -0
+       void (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);
        unsigned long ul; /* used for sorting when not FIELD_STR */
 };
 

ref-filter.c static inline char *copy_advance(char *dst, const char *src) · line 652

Quoted 9 lines
ref-filter.c +9 -0
@@ -632,6 +644,84 @@ static inline char *copy_advance(char *dst, const char *src)
+static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
+{
+       /*
+        * Quote formatting is only done when the stack has a single
+        * element. Otherwise quote formatting is done on the
+        * element's entire output strbuf when the %(end) atom is
+        * encountered.
+        */
+       if (!state->stack->prev)

With the disclaimer that I wasn't following the quoting discussion
closely: Is this condition going to be sufficient for all cases, such as an %(if:) atom? That is, if you have:

    %(if:notempty)%(bloop)%(then) --option=%(bloop)%(end)

isn't the intention that, %(bloop) within the %(then) section should be quoted but not the literal "--option="?

The condition `!state->stack->prev' would be insufficient to handle this if %(if:) pushes one or more states onto the stack, no? This implies that you might want an explicit flag for enabling/disabling quoting rather than relying upon the state of the stack, and that individual atom handlers would control that flag.

Or, am I misunderstanding due to not having followed the discussion closely?

Quoted 12 lines
ref-filter.c +12 -0
+               quote_formatting(&state->stack->output, v->s, state->quote_style);
+       else
+               strbuf_addstr(&state->stack->output, v->s);
+}
+
+static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
+{
+       struct ref_formatting_stack *current = state->stack;
+       struct strbuf s = STRBUF_INIT;
+
+       if (!current->at_end)
+               die(_("format: `end` atom used without a supporting atom"));

ref-filter.c static inline char *copy_advance(char *dst, const char *src) · line 719

Quoted 10 lines
ref-filter.c +10 -0
+       current->at_end(current);
+       /*
+        * Whenever we have more than one stack element that means we
+        * are using a certain modifier atom. In that case we need to
+        * perform quote formatting.
+        */
+       if (state->stack->prev) {
+               quote_formatting(&s, current->output.buf, state->quote_style);
+               strbuf_reset(&current->output);
+               strbuf_addbuf(&current->output, &s);

strbuf_swap() can replace the above two lines.

Quoted 9 lines
ref-filter.c +5 -0
+       }
+       strbuf_release(&s);
+       pop_stack_element(&state->stack);
+}
+
 /*
  * Parse the object referred by ref, and grab needed value.
  */

ref-filter.c static void populate_value(struct ref_array_item *ref) · line 822

Quoted 5 lines
ref-filter.c +2 -0
@@ -725,6 +818,37 @@ static void populate_value(struct ref_array_item *ref)
                        else
                                v->s = " ";
                        continue;
+               } else if (!strcmp(name, "align"))
+                       die(_("format: incomplete use of the `align` atom"));

Why does %(align) get flagged as a malformation of %(align:), whereas %(color) does not get flagged as a malformation of %(color:)? Why does one deserve special treatment but not the other?

ref-filter.c static void populate_value(struct ref_array_item *ref) · line 829

Quoted 7 lines
ref-filter.c +7 -0
+               else if (skip_prefix(name, "align:", &valp)) {
+                       struct align *align = xmalloc(sizeof(struct align));
+                       struct strbuf **s = strbuf_split_str(valp, ',', 0);
+
+                       /* If the position is given trim the ',' from the first strbuf */
+                       if (s[1])
+                               strbuf_remove(s[0], s[0]->len - 1, 1);

This is a truncation operation, which may be more idiomatically stated as:

    strbuf_setlen(s[0], s[0]->len - 1);

ref-filter.c static void populate_value(struct ref_array_item *ref) · line 836

Quoted 7 lines
ref-filter.c +7 -0
+
+                       if (strtoul_ui(s[0]->buf, 10, &align->width))
+                               die(_("positive width expected align:%s"), s[0]->buf);
+
+                       /* If no position is given, default to ALIGN_LEFT */
+                       if (!s[1] || !strcmp(s[1]->buf, "left"))
+                               align->position = ALIGN_LEFT;

If you structured the code like this:

    if (!s[1])
        align->position = ALIGN_LEFT;
    else if (!strcmp(s[1]->buf, "left"))
        align->position = ALIGN_LEFT;
    else if ...

then the comment about "default" would become unnecessary, and it would be easier to change the default in the future (if the need ever arose), however, this is a very minor point, and I don't care strongly.

Quoted 19 lines
ref-filter.c +6 -0
+                       else if (!strcmp(s[1]->buf, "right"))
+                               align->position = ALIGN_RIGHT;
+                       else if (!strcmp(s[1]->buf, "middle"))
+                               align->position = ALIGN_MIDDLE;
+                       else
+                               die(_("improper format entered align:%s"), s[1]->buf);
t/t6302-for-each-ref-filter.sh +8 -0
index 505a360..cef7a41 100755
@@ -81,4 +81,89 @@ test_expect_success 'filtering with --contains' '
+# Everything in between the %(align)...%(end) atom must be quoted, hence we test this by
+# introducing single quote's in %(align)...%(end), which must not be escaped.
+
+sq="'"
+
+test_expect_success 'alignment with format quote' '
+       cat >expect <<-EOF &&
+       refname is ${sq}           ${sq}\\${sq}${sq}master${sq}\\${sq}${sq}           ${sq}|

t/t6302-for-each-ref-filter.sh test_expect_success 'filtering with --contains' ' · line 98

Quoted 10 lines
t/t6302-for-each-ref-filter.sh +10 -0
+       refname is ${sq}            ${sq}\\${sq}${sq}side${sq}\\${sq}${sq}            ${sq}|
+       refname is ${sq}          ${sq}\\${sq}${sq}odd/spot${sq}\\${sq}${sq}          ${sq}|
+       refname is ${sq}         ${sq}\\${sq}${sq}double-tag${sq}\\${sq}${sq}         ${sq}|
+       refname is ${sq}            ${sq}\\${sq}${sq}four${sq}\\${sq}${sq}            ${sq}|
+       refname is ${sq}            ${sq}\\${sq}${sq}one${sq}\\${sq}${sq}             ${sq}|
+       refname is ${sq}         ${sq}\\${sq}${sq}signed-tag${sq}\\${sq}${sq}         ${sq}|
+       refname is ${sq}           ${sq}\\${sq}${sq}three${sq}\\${sq}${sq}            ${sq}|
+       refname is ${sq}            ${sq}\\${sq}${sq}two${sq}\\${sq}${sq}             ${sq}|
+       EOF
+       git for-each-ref --shell --format="refname is %(align:30,middle)${sq}%(refname:short)${sq}%(end)|" >actual &&

This can be much less noisy if you change the second argument of test_expect_success to use double-quote rather than single-quote. That would allow you to use single-quote directly in the test and expected output rather than having to use ${sq} indirection.

test_expect_success 'alignment with format quote' "
cat >expect <<-\EOF &&
refname is ' '''master''' '|
...
EOF
git for-each-ref --shell --format='refname ...
\\'%(rename:short)\\'...|'

    "

or something.

Quoted 2 lines
t/t6302-for-each-ref-filter.sh +2 -0
+       test_cmp expect actual
+'
#26 lore raw

PATCH v14 5/13 ref-filter: add option to filter out tags, branches and remotes

In reply to Karthik Nayak

From: Karthik Nayak <[email protected]>

Add a function called 'for_each_fullref_in()' to refs.{c,h} which iterates through each ref for the given path without trimming the path and also accounting for broken refs, if mentioned.

Add 'filter_ref_kind()' in ref-filter.c to check the kind of ref being handled and return the kind to 'ref_filter_handler()', where we discard refs which we do not need and assign the kind to needed refs.

---
 ref-filter.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 ref-filter.h | 13 +++++++++++--
 refs.c       |  9 +++++++++
 refs.h       |  1 +
 4 files changed, 79 insertions(+), 7 deletions(-)
ref-filter.c +58 -5
index 21c8b5f..5d4f93d 100644
@@ -1160,6 +1160,34 @@ static struct ref_array_item *new_ref_array_item(const char *refname,
 	return ref;
 }
 
+static int filter_ref_kind(struct ref_filter *filter, const char *refname)
+{
+	unsigned int i;
+
+	static struct {
+		const char *prefix;
+		unsigned int kind;
+	} ref_kind[] = {
+		{ "refs/heads/" , FILTER_REFS_BRANCHES },
+		{ "refs/remotes/" , FILTER_REFS_REMOTES },
+		{ "refs/tags/", FILTER_REFS_TAGS}
+	};
+
+	if (filter->kind == FILTER_REFS_BRANCHES ||
+	    filter->kind == FILTER_REFS_REMOTES ||
+	    filter->kind == FILTER_REFS_TAGS)
+		return filter->kind;
+	else if (!strcmp(refname, "HEAD"))
+		return FILTER_REFS_DETACHED_HEAD;
+
+	for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
+		if (starts_with(refname, ref_kind[i].prefix))
+			return ref_kind[i].kind;
+	}
+
+	return FILTER_REFS_OTHERS;
+}
+
 /*
  * A call-back given to for_each_ref().  Filter refs and keep them for
  * later object processing.
@@ -1170,6 +1198,7 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
 	struct ref_filter *filter = ref_cbdata->filter;
 	struct ref_array_item *ref;
 	struct commit *commit = NULL;
+	unsigned int kind;
 
 	if (flag & REF_BAD_NAME) {
 		warning("ignoring ref with broken name %s", refname);
@@ -1181,6 +1210,15 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
 		return 0;
 	}
 
+	/*
+	 * Get the current ref kind. If we're filtering tags, remotes or local branches
+	 * only then the current ref-kind is nothing but filter->kind and filter_ref_kind()
+	 * will only return that value.
+	 */
+	kind = filter_ref_kind(filter, refname);
+	if (!(kind & filter->kind))
+		return 0;
+
 	if (*filter->name_patterns && !match_name_as_path(filter->name_patterns, refname))
 		return 0;
 
@@ -1212,6 +1250,7 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
 
 	REALLOC_ARRAY(ref_cbdata->array->items, ref_cbdata->array->nr + 1);
 	ref_cbdata->array->items[ref_cbdata->array->nr++] = ref;
+	ref->kind = kind;
 	return 0;
 }
 
@@ -1288,17 +1327,31 @@ int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int
 {
 	struct ref_filter_cbdata ref_cbdata;
 	int ret = 0;
+	unsigned int broken = 0;
 
 	ref_cbdata.array = array;
 	ref_cbdata.filter = filter;
 
+	if (type & FILTER_REFS_INCLUDE_BROKEN)
+		broken = 1;
+	filter->kind = type & FILTER_REFS_KIND_MASK;
+
 	/*  Simple per-ref filtering */
-	if (type & (FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN))
-		ret = for_each_rawref(ref_filter_handler, &ref_cbdata);
-	else if (type & FILTER_REFS_ALL)
-		ret = for_each_ref(ref_filter_handler, &ref_cbdata);
-	else if (type)
+	if (!filter->kind)
 		die("filter_refs: invalid type");
+	else {
+		if (filter->kind == FILTER_REFS_BRANCHES)
+			ret = for_each_fullref_in("refs/heads/", ref_filter_handler, &ref_cbdata, broken);
+		else if (filter->kind == FILTER_REFS_REMOTES)
+			ret = for_each_fullref_in("refs/remotes/", ref_filter_handler, &ref_cbdata, broken);
+		else if (filter->kind == FILTER_REFS_TAGS)
+			ret = for_each_fullref_in("refs/tags/", ref_filter_handler, &ref_cbdata, broken);
+		else if (filter->kind & FILTER_REFS_ALL)
+			ret = for_each_fullref_in("", ref_filter_handler, &ref_cbdata, broken);
+		if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))
+			head_ref(ref_filter_handler, &ref_cbdata);
+	}
+
 
 	/*  Filters that need revision walking */
 	if (filter->merge_commit)
ref-filter.h +11 -2
index 45026d0..0913ba9 100644
@@ -13,8 +13,15 @@
 #define QUOTE_PYTHON 4
 #define QUOTE_TCL 8
 
-#define FILTER_REFS_INCLUDE_BROKEN 0x1
-#define FILTER_REFS_ALL 0x2
+#define FILTER_REFS_INCLUDE_BROKEN 0x0001
+#define FILTER_REFS_TAGS           0x0002
+#define FILTER_REFS_BRANCHES       0x0004
+#define FILTER_REFS_REMOTES        0x0008
+#define FILTER_REFS_OTHERS         0x0010
+#define FILTER_REFS_ALL            (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \
+				    FILTER_REFS_REMOTES | FILTER_REFS_OTHERS)
+#define FILTER_REFS_DETACHED_HEAD  0x0020
+#define FILTER_REFS_KIND_MASK      (FILTER_REFS_ALL | FILTER_REFS_DETACHED_HEAD)
 
 struct atom_value;
 
@@ -27,6 +34,7 @@ struct ref_sorting {
 struct ref_array_item {
 	unsigned char objectname[20];
 	int flag;
+	unsigned int kind;
 	const char *symref;
 	struct commit *commit;
 	struct atom_value *value;
@@ -51,6 +59,7 @@ struct ref_filter {
 	struct commit *merge_commit;
 
 	unsigned int with_commit_tag_algo : 1;
+	unsigned int kind;
 };
 
 struct ref_filter_cbdata {
refs.c +9 -0
index 4e15f60..a9469c2 100644
@@ -2108,6 +2108,15 @@ int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
 	return do_for_each_ref(&ref_cache, prefix, fn, strlen(prefix), 0, cb_data);
 }
 
+int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken)
+{
+	unsigned int flag = 0;
+
+	if (broken)
+		flag = DO_FOR_EACH_INCLUDE_BROKEN;
+	return do_for_each_ref(&ref_cache, prefix, fn, 0, flag, cb_data);
+}
+
 int for_each_ref_in_submodule(const char *submodule, const char *prefix,
 		each_ref_fn fn, void *cb_data)
 {
refs.h +1 -0
index e9a5f32..6d30c98 100644
@@ -173,6 +173,7 @@ typedef int each_ref_fn(const char *refname,
 extern int head_ref(each_ref_fn fn, void *cb_data);
 extern int for_each_ref(each_ref_fn fn, void *cb_data);
 extern int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data);
+extern int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken);
 extern int for_each_tag_ref(each_ref_fn fn, void *cb_data);
 extern int for_each_branch_ref(each_ref_fn fn, void *cb_data);
 extern int for_each_remote_ref(each_ref_fn fn, void *cb_data);
Signature · 1 line
2.5.0
#27 lore raw

Re: [PATCH v14 05/13] ref-filter: add option to filter out tags, branches and remotes

Quoted 15 lines · Karthik Nayak

Add a function called 'for_each_fullref_in()' to refs.{c,h} which iterates through each ref for the given path without trimming the path and also accounting for broken refs, if mentioned.

Add 'filter_ref_kind()' in ref-filter.c to check the kind of ref being handled and return the kind to 'ref_filter_handler()', where we discard refs which we do not need and assign the kind to needed refs.

---

refs.c +0 -0
index 4e15f60..a9469c2 100644

refs.c int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data) · line 2111

Quoted 4 lines
refs.c +1 -0
@@ -2108,6 +2108,15 @@ int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
        return do_for_each_ref(&ref_cache, prefix, fn, strlen(prefix), 0, cb_data);
 }
 
+int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken)

Booleans such as 'broken' are typically declared 'int' in this codebase, rather than 'unsigned int'.

Quoted 23 lines
refs.c +8 -0
+{
+       unsigned int flag = 0;
+
+       if (broken)
+               flag = DO_FOR_EACH_INCLUDE_BROKEN;
+       return do_for_each_ref(&ref_cache, prefix, fn, 0, flag, cb_data);
+}
+
 int for_each_ref_in_submodule(const char *submodule, const char *prefix,
                each_ref_fn fn, void *cb_data)
 {
refs.h +1 -0
index e9a5f32..6d30c98 100644
@@ -173,6 +173,7 @@ typedef int each_ref_fn(const char *refname,
 extern int head_ref(each_ref_fn fn, void *cb_data);
 extern int for_each_ref(each_ref_fn fn, void *cb_data);
 extern int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data);
+extern int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken);
 extern int for_each_tag_ref(each_ref_fn fn, void *cb_data);
 extern int for_each_branch_ref(each_ref_fn fn, void *cb_data);
 extern int for_each_remote_ref(each_ref_fn fn, void *cb_data);
#30 lore raw

PATCH v14 6/13 ref-filter: introduce format_ref_array_item()

In reply to Karthik Nayak

Create format_ref_array_item() out of show_ref_array_item(). This will store the output format for the given ref_array_item into the provided strbuf. Make show_ref_array_item() a wrapper around this to print the given ref_array_item with linefeed.

---
 ref-filter.c | 59 +++++++++++++++++++++++++++++++++--------------------------
 ref-filter.h |  7 +++++--
 2 files changed, 38 insertions(+), 28 deletions(-)
ref-filter.c +33 -26
index 5d4f93d..1e6754a 100644
@@ -153,6 +153,27 @@ int parse_ref_filter_atom(const char *atom, const char *ep)
 	return at;
 }
 
+static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
+{
+	switch (quote_style) {
+	case QUOTE_NONE:
+		strbuf_addstr(s, str);
+		break;
+	case QUOTE_SHELL:
+		sq_quote_buf(s, str);
+		break;
+	case QUOTE_PERL:
+		perl_quote_buf(s, str);
+		break;
+	case QUOTE_PYTHON:
+		python_quote_buf(s, str);
+		break;
+	case QUOTE_TCL:
+		tcl_quote_buf(s, str);
+		break;
+	}
+}
+
 static void push_stack_element(struct ref_formatting_stack **stack)
 {
 	struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack));
@@ -665,27 +686,6 @@ static void align_atom_handler(struct atom_value *atomv, struct ref_formatting_s
 	new->cb_data = atomv->align;
 }
 
-static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
-{
-	switch (quote_style) {
-	case QUOTE_NONE:
-		strbuf_addstr(s, str);
-		break;
-	case QUOTE_SHELL:
-		sq_quote_buf(s, str);
-		break;
-	case QUOTE_PERL:
-		perl_quote_buf(s, str);
-		break;
-	case QUOTE_PYTHON:
-		python_quote_buf(s, str);
-		break;
-	case QUOTE_TCL:
-		tcl_quote_buf(s, str);
-		break;
-	}
-}
-
 static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
 {
 	/*
@@ -1445,10 +1445,10 @@ static void append_literal(const char *cp, const char *ep, struct ref_formatting
 	}
 }
 
-void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
+void format_ref_array_item(struct strbuf *out, struct ref_array_item *info,
+			   const char *format, int quote_style)
 {
 	const char *cp, *sp, *ep;
-	struct strbuf *final_buf;
 	struct ref_formatting_state state = REF_FORMATTING_STATE_INIT;
 
 	state.quote_style = quote_style;
@@ -1478,10 +1478,17 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu
 	}
 	if (state.stack->prev)
 		die(_("format: `end` atom missing"));
-	final_buf = &state.stack->output;
-	fwrite(final_buf->buf, 1, final_buf->len, stdout);
+	strbuf_addbuf(out, &state.stack->output);
 	pop_stack_element(&state.stack);
-	putchar('\n');
+}
+
+void show_ref_array_item(struct ref_array_item *item, const char *format, unsigned int quote_style)
+{
+	struct strbuf out = STRBUF_INIT;
+	format_ref_array_item(&out, item, format, quote_style);
+	fwrite(out.buf, out.len, 1, stdout);
+	printf("\n");
+	strbuf_release(&out);
 }
 
 /*  If no sorting option is given, use refname to sort as default */
ref-filter.h +5 -2
index 0913ba9..082bee5 100644
@@ -91,8 +91,11 @@ int parse_ref_filter_atom(const char *atom, const char *ep);
 int verify_ref_format(const char *format);
 /*  Sort the given ref_array as per the ref_sorting provided */
 void ref_array_sort(struct ref_sorting *sort, struct ref_array *array);
-/*  Print the ref using the given format and quote_style */
-void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style);
+/*  Format the ref as per given format and quote_style and store it into the strbuf */
+void format_ref_array_item(struct strbuf *out, struct ref_array_item *info,
+			   const char *format, int quote_style);
+/*  Wrapper around format_ref_array_item() which prints the given ref_array_item */
+void show_ref_array_item(struct ref_array_item *item, const char *format, unsigned int quote_style);
 /*  Callback function for parsing the sort option */
 int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset);
 /*  Default sort option based on refname */
Signature · 1 line
2.5.0
#31 lore raw

Re: [PATCH v14 06/13] ref-filter: introduce format_ref_array_item()

Quoted 4 lines · Karthik Nayak

Create format_ref_array_item() out of show_ref_array_item(). This will store the output format for the given ref_array_item into the provided strbuf. Make show_ref_array_item() a wrapper around this to print the given ref_array_item with linefeed.

Perhaps you could explain why this change is a good idea, such as that a future patch, for <fill-in-the-blank> reason, will need the formatting capability of format_ref_array_item() but not the printing with newline done by show_ref_array_item().

Quoted 20 lines

---

ref-filter.c +10 -0
index 5d4f93d..1e6754a 100644
@@ -153,6 +153,27 @@ int parse_ref_filter_atom(const char *atom, const char *ep)
        return at;
 }
 
+static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
+{
+       switch (quote_style) {
+       case QUOTE_NONE:
+               strbuf_addstr(s, str);
+               break;
+       case QUOTE_SHELL:
+               sq_quote_buf(s, str);
+               break;
+       case QUOTE_PERL:

ref-filter.c int parse_ref_filter_atom(const char *atom, const char *ep) · line 175

Quoted 10 lines
ref-filter.c +10 -0
+               perl_quote_buf(s, str);
+               break;
+       case QUOTE_PYTHON:
+               python_quote_buf(s, str);
+               break;
+       case QUOTE_TCL:
+               tcl_quote_buf(s, str);
+               break;
+       }
+}

This code was already relocated once in patch 4/13, and is now being relocated again in 6/13. If you instead place the code at the final desired location in 4/13, then this patch will become less noisy.

More below.

Quoted 38 lines
ref-filter.c +1 -23
 static void push_stack_element(struct ref_formatting_stack **stack)
 {
        struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack));
@@ -665,27 +686,6 @@ static void align_atom_handler(struct atom_value *atomv, struct ref_formatting_s
        new->cb_data = atomv->align;
 }
 
-static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
-{
-       switch (quote_style) {
-       case QUOTE_NONE:
-               strbuf_addstr(s, str);
-               break;
-       case QUOTE_SHELL:
-               sq_quote_buf(s, str);
-               break;
-       case QUOTE_PERL:
-               perl_quote_buf(s, str);
-               break;
-       case QUOTE_PYTHON:
-               python_quote_buf(s, str);
-               break;
-       case QUOTE_TCL:
-               tcl_quote_buf(s, str);
-               break;
-       }
-}
-
 static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
 {
        /*
@@ -1478,10 +1478,17 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu
        }
        if (state.stack->prev)
                die(_("format: `end` atom missing"));
-       final_buf = &state.stack->output;
-       fwrite(final_buf->buf, 1, final_buf->len, stdout);
+       strbuf_addbuf(out, &state.stack->output);

ref-filter.c void show_ref_array_item(struct ref_array_item *info, const char *format, int qu · line 1490

Quoted 10 lines
ref-filter.c +8 -1
        pop_stack_element(&state.stack);
-       putchar('\n');
+}
+
+void show_ref_array_item(struct ref_array_item *item, const char *format, unsigned int quote_style)
+{
+       struct strbuf out = STRBUF_INIT;
+       format_ref_array_item(&out, item, format, quote_style);
+       fwrite(out.buf, out.len, 1, stdout);
+       printf("\n");

putchar('\n');

Quoted 4 lines
ref-filter.c +1 -0
+       strbuf_release(&out);
 }
 
 /*  If no sorting option is given, use refname to sort as default */
#32 lore raw

Re: [PATCH v14 06/13] ref-filter: introduce format_ref_array_item()

Quoted 11 lines · Eric Sunshine
Quoted 4 lines · Karthik Nayak

Create format_ref_array_item() out of show_ref_array_item(). This will store the output format for the given ref_array_item into the provided strbuf. Make show_ref_array_item() a wrapper around this to print the given ref_array_item with linefeed.

Perhaps you could explain why this change is a good idea, such as that a future patch, for <fill-in-the-blank> reason, will need the formatting capability of format_ref_array_item() but not the printing with newline done by show_ref_array_item().

Thinking along these lines, it's aim was to use with printing lines, but since that
is done with %(contents:lines=X) it might not be useful here, I see it being used with
future branch.c printing, but maybe it would make more sense to introduce it there
in that series.

Quoted 92 lines
Quoted 20 lines

---

ref-filter.c +10 -0
index 5d4f93d..1e6754a 100644
@@ -153,6 +153,27 @@ int parse_ref_filter_atom(const char *atom, const char *ep)
        return at;
 }
 
+static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
+{
+       switch (quote_style) {
+       case QUOTE_NONE:
+               strbuf_addstr(s, str);
+               break;
+       case QUOTE_SHELL:
+               sq_quote_buf(s, str);
+               break;
+       case QUOTE_PERL:

ref-filter.c int parse_ref_filter_atom(const char *atom, const char *ep) · line 175

Quoted 10 lines
ref-filter.c +10 -0
+               perl_quote_buf(s, str);
+               break;
+       case QUOTE_PYTHON:
+               python_quote_buf(s, str);
+               break;
+       case QUOTE_TCL:
+               tcl_quote_buf(s, str);
+               break;
+       }
+}

This code was already relocated once in patch 4/13, and is now being relocated again in 6/13. If you instead place the code at the final desired location in 4/13, then this patch will become less noisy.

More below.

Quoted 38 lines
ref-filter.c +1 -23
 static void push_stack_element(struct ref_formatting_stack **stack)
 {
        struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack));
@@ -665,27 +686,6 @@ static void align_atom_handler(struct atom_value *atomv, struct ref_formatting_s
        new->cb_data = atomv->align;
 }
 
-static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
-{
-       switch (quote_style) {
-       case QUOTE_NONE:
-               strbuf_addstr(s, str);
-               break;
-       case QUOTE_SHELL:
-               sq_quote_buf(s, str);
-               break;
-       case QUOTE_PERL:
-               perl_quote_buf(s, str);
-               break;
-       case QUOTE_PYTHON:
-               python_quote_buf(s, str);
-               break;
-       case QUOTE_TCL:
-               tcl_quote_buf(s, str);
-               break;
-       }
-}
-
 static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
 {
        /*
@@ -1478,10 +1478,17 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu
        }
        if (state.stack->prev)
                die(_("format: `end` atom missing"));
-       final_buf = &state.stack->output;
-       fwrite(final_buf->buf, 1, final_buf->len, stdout);
+       strbuf_addbuf(out, &state.stack->output);

ref-filter.c void show_ref_array_item(struct ref_array_item *info, const char *format, int qu · line 1490

Quoted 10 lines
ref-filter.c +8 -1
        pop_stack_element(&state.stack);
-       putchar('\n');
+}
+
+void show_ref_array_item(struct ref_array_item *item, const char *format, unsigned int quote_style)
+{
+       struct strbuf out = STRBUF_INIT;
+       format_ref_array_item(&out, item, format, quote_style);
+       fwrite(out.buf, out.len, 1, stdout);
+       printf("\n");

putchar('\n');

Quoted 4 lines
ref-filter.c +1 -0
+       strbuf_release(&out);
 }
 
 /*  If no sorting option is given, use refname to sort as default */
Signature · 2 lines
Regards,
Karthik Nayak
#33 lore raw

Re: [PATCH v14 06/13] ref-filter: introduce format_ref_array_item()

In reply to Eric Sunshine
Quoted 11 lines · Eric Sunshine
Quoted 4 lines · Karthik Nayak

Create format_ref_array_item() out of show_ref_array_item(). This will store the output format for the given ref_array_item into the provided strbuf. Make show_ref_array_item() a wrapper around this to print the given ref_array_item with linefeed.

Perhaps you could explain why this change is a good idea, such as that a future patch, for <fill-in-the-blank> reason, will need the formatting capability of format_ref_array_item() but not the printing with newline done by show_ref_array_item().

Yeah sure.

Quoted 26 lines
Quoted 26 lines

---

ref-filter.c +16 -0
index 5d4f93d..1e6754a 100644
@@ -153,6 +153,27 @@ int parse_ref_filter_atom(const char *atom, const char *ep)
        return at;
 }
 
+static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
+{
+       switch (quote_style) {
+       case QUOTE_NONE:
+               strbuf_addstr(s, str);
+               break;
+       case QUOTE_SHELL:
+               sq_quote_buf(s, str);
+               break;
+       case QUOTE_PERL:
+               perl_quote_buf(s, str);
+               break;
+       case QUOTE_PYTHON:
+               python_quote_buf(s, str);
+               break;
+       case QUOTE_TCL:
Quoted 9 lines

ref-filter.c int parse_ref_filter_atom(const char *atom, const char *ep) · line 175

Quoted 4 lines
ref-filter.c +4 -0
+               tcl_quote_buf(s, str);
+               break;
+       }
+}

This code was already relocated once in patch 4/13, and is now being relocated again in 6/13. If you instead place the code at the final desired location in 4/13, then this patch will become less noisy.

Will do.

Quoted 44 lines

More below.

Quoted 42 lines
ref-filter.c +3 -24
 static void push_stack_element(struct ref_formatting_stack **stack)
 {
        struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack));
@@ -665,27 +686,6 @@ static void align_atom_handler(struct atom_value *atomv, struct ref_formatting_s
        new->cb_data = atomv->align;
 }
 
-static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
-{
-       switch (quote_style) {
-       case QUOTE_NONE:
-               strbuf_addstr(s, str);
-               break;
-       case QUOTE_SHELL:
-               sq_quote_buf(s, str);
-               break;
-       case QUOTE_PERL:
-               perl_quote_buf(s, str);
-               break;
-       case QUOTE_PYTHON:
-               python_quote_buf(s, str);
-               break;
-       case QUOTE_TCL:
-               tcl_quote_buf(s, str);
-               break;
-       }
-}
-
 static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
 {
        /*
@@ -1478,10 +1478,17 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu
        }
        if (state.stack->prev)
                die(_("format: `end` atom missing"));
-       final_buf = &state.stack->output;
-       fwrite(final_buf->buf, 1, final_buf->len, stdout);
+       strbuf_addbuf(out, &state.stack->output);
        pop_stack_element(&state.stack);
-       putchar('\n');
+}
+
Quoted 8 lines

ref-filter.c void show_ref_array_item(struct ref_array_item *info, const char *format, int qu · line 1490

Quoted 6 lines
ref-filter.c +6 -0
+void show_ref_array_item(struct ref_array_item *item, const char *format, unsigned int quote_style)
+{
+       struct strbuf out = STRBUF_INIT;
+       format_ref_array_item(&out, item, format, quote_style);
+       fwrite(out.buf, out.len, 1, stdout);
+       printf("\n");

putchar('\n');

Thanks for the review.

Signature · 2 lines
Regards,
Karthik Nayak
#34 lore raw

PATCH v14 7/13 ref-filter: add support for %(contents:lines=X)

In reply to Karthik Nayak

In 'tag.c' we can print N lines from the annotation of the tag using the '-n<num>' option. Copy code from 'tag.c' to 'ref-filter' and modify it to support appending of N lines from the annotation of tags to the given strbuf.

Implement %(contents:lines=X) where X lines of the given object are obtained.

Add documentation and test for the same.

---
 Documentation/git-for-each-ref.txt |  1 +
 builtin/tag.c                      |  4 ++
 ref-filter.c                       | 75 +++++++++++++++++++++++++++++++++++++-
 ref-filter.h                       |  3 +-
 t/t6302-for-each-ref-filter.sh     | 16 ++++++++
 5 files changed, 97 insertions(+), 2 deletions(-)
Documentation/git-for-each-ref.txt +1 -0
index 943975d..7ca1fe4 100644
@@ -149,6 +149,7 @@ Its first line is `contents:subject`, where subject is the concatenation
 of all lines of the commit message up to the first blank line.  The next
 line is 'contents:body', where body is all of the lines after the first
 blank line.  Finally, the optional GPG signature is `contents:signature`.
+The first `N` lines of the object is obtained using `contents:lines=N`.
 
 For sorting purposes, fields with numeric values sort in numeric
 order (`objectsize`, `authordate`, `committerdate`, `taggerdate`).
builtin/tag.c +4 -0
index 471d6b1..0fc7557 100644
@@ -185,6 +185,10 @@ static enum contains_result contains(struct commit *candidate,
 	return contains_test(candidate, want);
 }
 
+/*
+ * Currently duplicated in ref-filter, will eventually be removed as
+ * we port tag.c to use ref-filter APIs.
+ */
 static void show_tag_lines(const struct object_id *oid, int lines)
 {
 	int i;
ref-filter.c +74 -1

117 lines not on this page · raw message

ref-filter.h +2 -1
index 082bee5..3759424 100644
@@ -59,7 +59,8 @@ struct ref_filter {
 	struct commit *merge_commit;
 
 	unsigned int with_commit_tag_algo : 1;
-	unsigned int kind;
+	unsigned int kind,
+		lines;
 };
 
 struct ref_filter_cbdata {
t/t6302-for-each-ref-filter.sh +16 -0
index cef7a41..0277498 100755
@@ -166,4 +166,20 @@ test_expect_success 'nested alignment' '
 	test_cmp expect actual
 '
 
+test_expect_success 'check `%(contents:lines=X)`' '
+	cat >expect <<-\EOF &&
+	master three
+	side four
+	odd/spot three
+	double-tag Annonated doubly
+	four four
+	one one
+	signed-tag A signed tag message
+	three three
+	two two
+	EOF
+	git for-each-ref --format="%(refname:short) %(contents:lines=1)" >actual &&
+	test_cmp expect actual
+'
+
 test_done
Signature · 1 line
2.5.0
#41 lore raw

PATCH v14 8/13 ref-filter: add support to sort by version

In reply to Karthik Nayak

From: Karthik Nayak <[email protected]>

Add support to sort by version using the "v:refname" and "version:refname" option. This is achieved by using the 'versioncmp()' function as the comparing function for qsort.

This option is included to support sorting by versions in git tag -l which will eventually be ported to use ref-filter APIs.

Add documentation and tests for the same.

---
 Documentation/git-for-each-ref.txt |  3 +++
 ref-filter.c                       | 15 ++++++++++-----
 ref-filter.h                       |  3 ++-
 t/t6302-for-each-ref-filter.sh     | 36 ++++++++++++++++++++++++++++++++++++
 4 files changed, 51 insertions(+), 6 deletions(-)
Documentation/git-for-each-ref.txt +3 -0
index 7ca1fe4..1b48b95 100644
@@ -155,6 +155,9 @@ For sorting purposes, fields with numeric values sort in numeric
 order (`objectsize`, `authordate`, `committerdate`, `taggerdate`).
 All other fields are used to sort in their byte-value order.
 
+There is also an option to sort by versions, this can be done by using
+the fieldname `version:refname` or its alias `v:refname`.
+
 In any case, a field name that refers to a field inapplicable to
 the object referred by the ref does not cause an error.  It
 returns an empty string instead.
ref-filter.c +10 -5
index ece7a23..434f2c9 100644
@@ -11,6 +11,8 @@
 #include "ref-filter.h"
 #include "revision.h"
 #include "utf8.h"
+#include "git-compat-util.h"
+#include "version.h"
 
 typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
 
@@ -1441,19 +1443,19 @@ static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, stru
 
 	get_ref_atom_value(a, s->atom, &va);
 	get_ref_atom_value(b, s->atom, &vb);
-	switch (cmp_type) {
-	case FIELD_STR:
+	if (s->version)
+		cmp = versioncmp(va->s, vb->s);
+	else if (cmp_type == FIELD_STR)
 		cmp = strcmp(va->s, vb->s);
-		break;
-	default:
+	else {
 		if (va->ul < vb->ul)
 			cmp = -1;
 		else if (va->ul == vb->ul)
 			cmp = 0;
 		else
 			cmp = 1;
-		break;
 	}
+
 	return (s->reverse) ? -cmp : cmp;
 }
 
@@ -1593,6 +1595,9 @@ int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
 		s->reverse = 1;
 		arg++;
 	}
+	if (skip_prefix(arg, "version:", &arg) ||
+	    skip_prefix(arg, "v:", &arg))
+		s->version = 1;
 	len = strlen(arg);
 	s->atom = parse_ref_filter_atom(arg, arg+len);
 	return 0;
ref-filter.h +2 -1
index 3759424..1282d70 100644
@@ -28,7 +28,8 @@ struct atom_value;
 struct ref_sorting {
 	struct ref_sorting *next;
 	int atom; /* index into used_atom array (internal) */
-	unsigned reverse : 1;
+	unsigned reverse : 1,
+		version : 1;
 };
 
 struct ref_array_item {
t/t6302-for-each-ref-filter.sh +36 -0
index 0277498..8f18f86 100755
@@ -182,4 +182,40 @@ test_expect_success 'check `%(contents:lines=X)`' '
 	test_cmp expect actual
 '
 
+test_expect_success 'setup for version sort' '
+	test_commit foo1.3 &&
+	test_commit foo1.6 &&
+	test_commit foo1.10
+'
+
+test_expect_success 'version sort' '
+	git for-each-ref --sort=version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
+	cat >expect <<-\EOF &&
+	foo1.3
+	foo1.6
+	foo1.10
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'version sort (shortened)' '
+	git for-each-ref --sort=v:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
+	cat >expect <<-\EOF &&
+	foo1.3
+	foo1.6
+	foo1.10
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'reverse version sort' '
+	git for-each-ref --sort=-version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
+	cat >expect <<-\EOF &&
+	foo1.10
+	foo1.6
+	foo1.3
+	EOF
+	test_cmp expect actual
+'
+
 test_done
Signature · 1 line
2.5.0
#42 lore raw

PATCH v14 9/13 ref-filter: add option to match literal pattern

In reply to Karthik Nayak

From: Karthik Nayak <[email protected]>

Since 'ref-filter' only has an option to match path names add an option for plain fnmatch pattern-matching.

This is to support the pattern matching options which are used in git tag -l and git branch -l where we can match patterns like git tag -l foo* which would match all tags which has a "foo*" pattern.

---
 builtin/for-each-ref.c |  1 +
 ref-filter.c           | 40 +++++++++++++++++++++++++++++++++++++---
 ref-filter.h           |  3 ++-
 3 files changed, 40 insertions(+), 4 deletions(-)
builtin/for-each-ref.c +1 -0
index 40f343b..4e9f6c2 100644
@@ -68,6 +68,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 	git_config(git_default_config, NULL);
 
 	filter.name_patterns = argv;
+	filter.match_as_path = 1;
 	filter_refs(&array, &filter, FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN);
 	ref_array_sort(sorting, &array);
 
ref-filter.c +37 -3
index 434f2c9..f268cd7 100644
@@ -1165,9 +1165,33 @@ static int commit_contains(struct ref_filter *filter, struct commit *commit)
 
 /*
  * Return 1 if the refname matches one of the patterns, otherwise 0.
+ * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
+ * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
+ * matches "refs/heads/mas*", too).
+ */
+static int match_pattern(const char **patterns, const char *refname)
+{
+	/*
+	 * When no '--format' option is given we need to skip the prefix
+	 * for matching refs of tags and branches.
+	 */
+	(void)(skip_prefix(refname, "refs/tags/", &refname) ||
+	       skip_prefix(refname, "refs/heads/", &refname) ||
+	       skip_prefix(refname, "refs/remotes/", &refname) ||
+	       skip_prefix(refname, "refs/", &refname));
+
+	for (; *patterns; patterns++) {
+		if (!wildmatch(*patterns, refname, 0, NULL))
+			return 1;
+	}
+	return 0;
+}
+
+/*
+ * Return 1 if the refname matches one of the patterns, otherwise 0.
  * A pattern can be path prefix (e.g. a refname "refs/heads/master"
- * matches a pattern "refs/heads/") or a wildcard (e.g. the same ref
- * matches "refs/heads/m*",too).
+ * matches a pattern "refs/heads/" but not "refs/heads/m") or a
+ * wildcard (e.g. the same ref matches "refs/heads/m*", too).
  */
 static int match_name_as_path(const char **pattern, const char *refname)
 {
@@ -1188,6 +1212,16 @@ static int match_name_as_path(const char **pattern, const char *refname)
 	return 0;
 }
 
+/* Return 1 if the refname matches one of the patterns, otherwise 0. */
+static int filter_pattern_match(struct ref_filter *filter, const char *refname)
+{
+	if (!*filter->name_patterns)
+		return 1; /* No pattern always matches */
+	if (filter->match_as_path)
+		return match_name_as_path(filter->name_patterns, refname);
+	return match_pattern(filter->name_patterns, refname);
+}
+
 /*
  * Given a ref (sha1, refname), check if the ref belongs to the array
  * of sha1s. If the given ref is a tag, check if the given tag points
@@ -1294,7 +1328,7 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
 	if (!(kind & filter->kind))
 		return 0;
 
-	if (*filter->name_patterns && !match_name_as_path(filter->name_patterns, refname))
+	if (!filter_pattern_match(filter, refname))
 		return 0;
 
 	if (filter->points_at.nr && !match_points_at(&filter->points_at, oid->hash, refname))
ref-filter.h +2 -1
index 1282d70..179944c 100644
@@ -59,7 +59,8 @@ struct ref_filter {
 	} merge;
 	struct commit *merge_commit;
 
-	unsigned int with_commit_tag_algo : 1;
+	unsigned int with_commit_tag_algo : 1,
+		match_as_path : 1;
 	unsigned int kind,
 		lines;
 };
Signature · 1 line
2.5.0
#43 lore raw

PATCH v14 10/13 tag.c: use 'ref-filter' data structures

In reply to Karthik Nayak

From: Karthik Nayak <[email protected]>

Make 'tag.c' use 'ref-filter' data structures and make changes to support the new data structures. This is a part of the process of porting 'tag.c' to use 'ref-filter' APIs.

This is a temporary step before porting 'tag.c' to use 'ref-filter' completely. As this is a temporary step, most of the code introduced here will be removed when 'tag.c' is ported over to use 'ref-filter' APIs.

---
 builtin/tag.c | 106 +++++++++++++++++++++++++++++++---------------------------
 1 file changed, 57 insertions(+), 49 deletions(-)
builtin/tag.c +57 -49
index 0fc7557..e96bae2 100644
@@ -17,6 +17,7 @@
 #include "gpg-interface.h"
 #include "sha1-array.h"
 #include "column.h"
+#include "ref-filter.h"
 
 static const char * const git_tag_usage[] = {
 	N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>]"),
@@ -34,15 +35,6 @@ static const char * const git_tag_usage[] = {
 
 static int tag_sort;
 
-struct tag_filter {
-	const char **patterns;
-	int lines;
-	int sort;
-	struct string_list tags;
-	struct commit_list *with_commit;
-};
-
-static struct sha1_array points_at;
 static unsigned int colopts;
 
 static int match_pattern(const char **patterns, const char *ref)
@@ -61,19 +53,20 @@ static int match_pattern(const char **patterns, const char *ref)
  * removed as we port tag.c to use the ref-filter APIs.
  */
 static const unsigned char *match_points_at(const char *refname,
-					    const unsigned char *sha1)
+					    const unsigned char *sha1,
+					    struct sha1_array *points_at)
 {
 	const unsigned char *tagged_sha1 = NULL;
 	struct object *obj;
 
-	if (sha1_array_lookup(&points_at, sha1) >= 0)
+	if (sha1_array_lookup(points_at, sha1) >= 0)
 		return sha1;
 	obj = parse_object(sha1);
 	if (!obj)
 		die(_("malformed object at '%s'"), refname);
 	if (obj->type == OBJ_TAG)
 		tagged_sha1 = ((struct tag *)obj)->tagged->sha1;
-	if (tagged_sha1 && sha1_array_lookup(&points_at, tagged_sha1) >= 0)
+	if (tagged_sha1 && sha1_array_lookup(points_at, tagged_sha1) >= 0)
 		return tagged_sha1;
 	return NULL;
 }
@@ -228,12 +221,24 @@ free_return:
 	free(buf);
 }
 
+static void ref_array_append(struct ref_array *array, const char *refname)
+{
+	size_t len = strlen(refname);
+	struct ref_array_item *ref = xcalloc(1, sizeof(struct ref_array_item) + len + 1);
+	memcpy(ref->refname, refname, len);
+	ref->refname[len] = '\0';
+	REALLOC_ARRAY(array->items, array->nr + 1);
+	array->items[array->nr++] = ref;
+}
+
 static int show_reference(const char *refname, const struct object_id *oid,
 			  int flag, void *cb_data)
 {
-	struct tag_filter *filter = cb_data;
+	struct ref_filter_cbdata *data = cb_data;
+	struct ref_array *array = data->array;
+	struct ref_filter *filter = data->filter;
 
-	if (match_pattern(filter->patterns, refname)) {
+	if (match_pattern(filter->name_patterns, refname)) {
 		if (filter->with_commit) {
 			struct commit *commit;
 
@@ -244,12 +249,12 @@ static int show_reference(const char *refname, const struct object_id *oid,
 				return 0;
 		}
 
-		if (points_at.nr && !match_points_at(refname, oid->hash))
+		if (filter->points_at.nr && !match_points_at(refname, oid->hash, &filter->points_at))
 			return 0;
 
 		if (!filter->lines) {
-			if (filter->sort)
-				string_list_append(&filter->tags, refname);
+			if (tag_sort)
+				ref_array_append(array, refname);
 			else
 				printf("%s\n", refname);
 			return 0;
@@ -264,36 +269,36 @@ static int show_reference(const char *refname, const struct object_id *oid,
 
 static int sort_by_version(const void *a_, const void *b_)
 {
-	const struct string_list_item *a = a_;
-	const struct string_list_item *b = b_;
-	return versioncmp(a->string, b->string);
+	const struct ref_array_item *a = *((struct ref_array_item **)a_);
+	const struct ref_array_item *b = *((struct ref_array_item **)b_);
+	return versioncmp(a->refname, b->refname);
 }
 
-static int list_tags(const char **patterns, int lines,
-		     struct commit_list *with_commit, int sort)
+static int list_tags(struct ref_filter *filter, int sort)
 {
-	struct tag_filter filter;
+	struct ref_array array;
+	struct ref_filter_cbdata data;
+
+	memset(&array, 0, sizeof(array));
+	data.array = &array;
+	data.filter = filter;
 
-	filter.patterns = patterns;
-	filter.lines = lines;
-	filter.sort = sort;
-	filter.with_commit = with_commit;
-	memset(&filter.tags, 0, sizeof(filter.tags));
-	filter.tags.strdup_strings = 1;
+	if (filter->lines == -1)
+		filter->lines = 0;
 
-	for_each_tag_ref(show_reference, (void *)&filter);
+	for_each_tag_ref(show_reference, &data);
 	if (sort) {
 		int i;
 		if ((sort & SORT_MASK) == VERCMP_SORT)
-			qsort(filter.tags.items, filter.tags.nr,
-			      sizeof(struct string_list_item), sort_by_version);
+			qsort(array.items, array.nr,
+			      sizeof(struct ref_array_item *), sort_by_version);
 		if (sort & REVERSE_SORT)
-			for (i = filter.tags.nr - 1; i >= 0; i--)
-				printf("%s\n", filter.tags.items[i].string);
+			for (i = array.nr - 1; i >= 0; i--)
+				printf("%s\n", array.items[i]->refname);
 		else
-			for (i = 0; i < filter.tags.nr; i++)
-				printf("%s\n", filter.tags.items[i].string);
-		string_list_clear(&filter.tags, 0);
+			for (i = 0; i < array.nr; i++)
+				printf("%s\n", array.items[i]->refname);
+		ref_array_clear(&array);
 	}
 	return 0;
 }
@@ -574,17 +579,17 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 	const char *object_ref, *tag;
 	struct create_tag_options opt;
 	char *cleanup_arg = NULL;
-	int annotate = 0, force = 0, lines = -1;
 	int create_reflog = 0;
+	int annotate = 0, force = 0;
 	int cmdmode = 0;
 	const char *msgfile = NULL, *keyid = NULL;
 	struct msg_arg msg = { 0, STRBUF_INIT };
-	struct commit_list *with_commit = NULL;
 	struct ref_transaction *transaction;
 	struct strbuf err = STRBUF_INIT;
+	struct ref_filter filter;
 	struct option options[] = {
 		OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
-		{ OPTION_INTEGER, 'n', NULL, &lines, N_("n"),
+		{ OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"),
 				N_("print <n> lines of each tag message"),
 				PARSE_OPT_OPTARG, NULL, 1 },
 		OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'),
@@ -606,14 +611,14 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 
 		OPT_GROUP(N_("Tag listing options")),
 		OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
-		OPT_CONTAINS(&with_commit, N_("print only tags that contain the commit")),
-		OPT_WITH(&with_commit, N_("print only tags that contain the commit")),
+		OPT_CONTAINS(&filter.with_commit, N_("print only tags that contain the commit")),
+		OPT_WITH(&filter.with_commit, N_("print only tags that contain the commit")),
 		{
 			OPTION_CALLBACK, 0, "sort", &tag_sort, N_("type"), N_("sort tags"),
 			PARSE_OPT_NONEG, parse_opt_sort
 		},
 		{
-			OPTION_CALLBACK, 0, "points-at", &points_at, N_("object"),
+			OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
 			N_("print only tags of the object"), 0, parse_opt_object_name
 		},
 		OPT_END()
@@ -622,6 +627,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 	git_config(git_tag_config, NULL);
 
 	memset(&opt, 0, sizeof(opt));
+	memset(&filter, 0, sizeof(filter));
+	filter.lines = -1;
 
 	argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
 
@@ -638,7 +645,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		usage_with_options(git_tag_usage, options);
 
 	finalize_colopts(&colopts, -1);
-	if (cmdmode == 'l' && lines != -1) {
+	if (cmdmode == 'l' && filter.lines != -1) {
 		if (explicitly_enable_column(colopts))
 			die(_("--column and -n are incompatible"));
 		colopts = 0;
@@ -651,18 +658,19 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 			copts.padding = 2;
 			run_column_filter(colopts, &copts);
 		}
-		if (lines != -1 && tag_sort)
+		if (filter.lines != -1 && tag_sort)
 			die(_("--sort and -n are incompatible"));
-		ret = list_tags(argv, lines == -1 ? 0 : lines, with_commit, tag_sort);
+		filter.name_patterns = argv;
+		ret = list_tags(&filter, tag_sort);
 		if (column_active(colopts))
 			stop_column_filter();
 		return ret;
 	}
-	if (lines != -1)
+	if (filter.lines != -1)
 		die(_("-n option is only allowed with -l."));
-	if (with_commit)
+	if (filter.with_commit)
 		die(_("--contains option is only allowed with -l."));
-	if (points_at.nr)
+	if (filter.points_at.nr)
 		die(_("--points-at option is only allowed with -l."));
 	if (cmdmode == 'd')
 		return for_each_tag_name(argv, delete_tag);
Signature · 1 line
2.5.0
#44 lore raw

PATCH v14 11/13 tag.c: use 'ref-filter' APIs

In reply to Karthik Nayak

From: Karthik Nayak <[email protected]>

Make 'tag.c' use 'ref-filter' APIs for iterating through refs, sorting and printing of refs. This removes most of the code used in 'tag.c' replacing it with calls to the 'ref-filter' library.

Make 'tag.c' use the 'filter_refs()' function provided by 'ref-filter' to filter out tags based on the options set.

For printing tags we use 'show_ref_array_item()' function provided by 'ref-filter'.

We improve the sorting option provided by 'tag.c' by using the sorting options provided by 'ref-filter'. This causes the test 'invalid sort parameter on command line' in t7004 to fail, as 'ref-filter' throws an error for all sorting fields which are incorrect. The test is changed to reflect the same.

Modify documentation for the same.

---
 Documentation/git-tag.txt |  16 ++-
 builtin/tag.c             | 344 ++++++----------------------------------------
 t/t7004-tag.sh            |   8 +-
 3 files changed, 52 insertions(+), 316 deletions(-)
Documentation/git-tag.txt +9 -7
index 84f6496..3ac4a96 100644
@@ -13,7 +13,7 @@ SYNOPSIS
 	<tagname> [<commit> | <object>]
 'git tag' -d <tagname>...
 'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>]
-	[--column[=<options>] | --no-column] [--create-reflog] [<pattern>...]
+	[--column[=<options>] | --no-column] [--create-reflog] [--sort=<key>] [<pattern>...]
 'git tag' -v <tagname>...
 
 DESCRIPTION
@@ -94,14 +94,16 @@ OPTIONS
 	using fnmatch(3)).  Multiple patterns may be given; if any of
 	them matches, the tag is shown.
 
---sort=<type>::
-	Sort in a specific order. Supported type is "refname"
-	(lexicographic order), "version:refname" or "v:refname" (tag
+--sort=<key>::
+	Sort based on the key given.  Prefix `-` to sort in
+	descending order of the value. You may use the --sort=<key> option
+	multiple times, in which case the last key becomes the primary
+	key. Also supports "version:refname" or "v:refname" (tag
 	names are treated as versions). The "version:refname" sort
 	order can also be affected by the
-	"versionsort.prereleaseSuffix" configuration variable. Prepend
-	"-" to reverse sort order. When this option is not given, the
-	sort order defaults to the value configured for the 'tag.sort'
+	"versionsort.prereleaseSuffix" configuration variable.
+	The keys supported are the same as those in `git for-each-ref`.
+	Sort order defaults to the value configured for the 'tag.sort'
 	variable if it exists, or lexicographic order otherwise. See
 	linkgit:git-config[1].
 
builtin/tag.c +42 -302

418 lines not on this page · raw message

t/t7004-tag.sh +1 -7
index d31788c..84153ef 100755
@@ -1462,13 +1462,7 @@ test_expect_success 'invalid sort parameter on command line' '
 
 test_expect_success 'invalid sort parameter in configuratoin' '
 	git config tag.sort "v:notvalid" &&
-	git tag -l "foo*" >actual &&
-	cat >expect <<-\EOF &&
-	foo1.10
-	foo1.3
-	foo1.6
-	EOF
-	test_cmp expect actual
+	test_must_fail git tag -l "foo*"
 '
 
 test_expect_success 'version sort with prerelease reordering' '
Signature · 1 line
2.5.0
#45 lore raw

PATCH v14 12/13 tag.c: implement '--format' option

In reply to Karthik Nayak

Implement the '--format' option provided by 'ref-filter'. This lets the user list tags as per desired format similar to the implementation in 'git for-each-ref'.

Add tests and documentation for the same.

---
 Documentation/git-tag.txt |  8 +++++++-
 builtin/tag.c             | 22 +++++++++++++---------
 t/t7004-tag.sh            | 12 ++++++++++++
 3 files changed, 32 insertions(+), 10 deletions(-)
Documentation/git-tag.txt +7 -1
index 3ac4a96..0c7f4e6 100644
@@ -13,7 +13,8 @@ SYNOPSIS
 	<tagname> [<commit> | <object>]
 'git tag' -d <tagname>...
 'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>]
-	[--column[=<options>] | --no-column] [--create-reflog] [--sort=<key>] [<pattern>...]
+	[--column[=<options>] | --no-column] [--create-reflog] [--sort=<key>]
+	[--format=<format>] [<pattern>...]
 'git tag' -v <tagname>...
 
 DESCRIPTION
@@ -158,6 +159,11 @@ This option is only applicable when listing tags without annotation lines.
 	The object that the new tag will refer to, usually a commit.
 	Defaults to HEAD.
 
+<format>::
+	A string that interpolates `%(fieldname)` from the object
+	pointed at by a ref being shown.  The format is the same as
+	that of linkgit:git-for-each-ref[1].  When unspecified,
+	defaults to `%(refname:short)`.
 
 CONFIGURATION
 -------------
builtin/tag.c +13 -9
index 2d348f4..dc4494d 100644
@@ -23,17 +23,17 @@ static const char * const git_tag_usage[] = {
 	N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>]"),
 	N_("git tag -d <tagname>..."),
 	N_("git tag -l [-n[<num>]] [--contains <commit>] [--points-at <object>]"
-		"\n\t\t[<pattern>...]"),
+		"\n\t\t[--format=<format>] [<pattern>...]"),
 	N_("git tag -v <tagname>..."),
 	NULL
 };
 
 static unsigned int colopts;
 
-static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting)
+static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, const char *format)
 {
 	struct ref_array array;
-	char *format, *to_free = NULL;
+	char *to_free = NULL;
 	int i;
 
 	memset(&array, 0, sizeof(array));
@@ -41,11 +41,13 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting)
 	if (filter->lines == -1)
 		filter->lines = 0;
 
-	if (filter->lines)
-		format = to_free = xstrfmt("%%(align:15,left)%%(refname:short)%%(end) %%(contents:lines=%d)",
-					   filter->lines);
-	else
-		format = "%(refname:short)";
+	if (!format) {
+		if (filter->lines)
+			format = to_free = xstrfmt("%%(align:15,left)%%(refname:short)%%(end) %%(contents:lines=%d)",
+						   filter->lines);
+		else
+			format = "%(refname:short)";
+	}
 
 	verify_ref_format(format);
 	filter_refs(&array, filter, FILTER_REFS_TAGS);
@@ -329,6 +331,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 	struct strbuf err = STRBUF_INIT;
 	struct ref_filter filter;
 	static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
+	const char *format = NULL;
 	struct option options[] = {
 		OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
 		{ OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"),
@@ -361,6 +364,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 			OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
 			N_("print only tags of the object"), 0, parse_opt_object_name
 		},
+		OPT_STRING(  0 , "format", &format, N_("format"), N_("format to use for the output")),
 		OPT_END()
 	};
 
@@ -401,7 +405,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 			run_column_filter(colopts, &copts);
 		}
 		filter.name_patterns = argv;
-		ret = list_tags(&filter, sorting);
+		ret = list_tags(&filter, sorting, format);
 		if (column_active(colopts))
 			stop_column_filter();
 		return ret;
t/t7004-tag.sh +12 -0
index 84153ef..8987fb1 100755
@@ -1519,4 +1519,16 @@ EOF"
 	test_cmp expect actual
 '
 
+test_expect_success '--format should list tags as per format given' '
+	cat >expect <<-\EOF &&
+	refname : refs/tags/foo1.10
+	refname : refs/tags/foo1.3
+	refname : refs/tags/foo1.6
+	refname : refs/tags/foo1.6-rc1
+	refname : refs/tags/foo1.6-rc2
+	EOF
+	git tag -l --format="refname : %(refname)" "foo*" >actual &&
+	test_cmp expect actual
+'
+
 test_done
Signature · 1 line
2.5.0
#46 lore raw

PATCH v14 13/13 tag.c: implement '--merged' and '--no-merged' options

In reply to Karthik Nayak

Use 'ref-filter' APIs to implement the '--merged' and '--no-merged' options into 'tag.c'. The '--merged' option lets the user to only list tags merged into the named commit. The '--no-merged' option lets the user to only list tags not merged into the named commit. If no object is provided it assumes HEAD as the object.

Add documentation and tests for the same.

---
 Documentation/git-tag.txt |  7 ++++++-
 builtin/tag.c             |  6 +++++-
 t/t7004-tag.sh            | 27 +++++++++++++++++++++++++++
 3 files changed, 38 insertions(+), 2 deletions(-)
Documentation/git-tag.txt +6 -1
index 0c7f4e6..3803bf7 100644
@@ -14,7 +14,7 @@ SYNOPSIS
 'git tag' -d <tagname>...
 'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>]
 	[--column[=<options>] | --no-column] [--create-reflog] [--sort=<key>]
-	[--format=<format>] [<pattern>...]
+	[--format=<format>] [--[no-]merged [<commit>]] [<pattern>...]
 'git tag' -v <tagname>...
 
 DESCRIPTION
@@ -165,6 +165,11 @@ This option is only applicable when listing tags without annotation lines.
 	that of linkgit:git-for-each-ref[1].  When unspecified,
 	defaults to `%(refname:short)`.
 
+--[no-]merged [<commit>]::
+	Only list tags whose tips are reachable, or not reachable
+	if '--no-merged' is used, from the specified commit ('HEAD'
+	if not specified).
+
 CONFIGURATION
 -------------
 By default, 'git tag' in sign-with-default mode (-s) will use your
builtin/tag.c +5 -1
index dc4494d..9fa1400 100644
@@ -23,7 +23,7 @@ static const char * const git_tag_usage[] = {
 	N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>]"),
 	N_("git tag -d <tagname>..."),
 	N_("git tag -l [-n[<num>]] [--contains <commit>] [--points-at <object>]"
-		"\n\t\t[--format=<format>] [<pattern>...]"),
+		"\n\t\t[--format=<format>] [--[no-]merged [<commit>]] [<pattern>...]"),
 	N_("git tag -v <tagname>..."),
 	NULL
 };
@@ -358,6 +358,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
 		OPT_CONTAINS(&filter.with_commit, N_("print only tags that contain the commit")),
 		OPT_WITH(&filter.with_commit, N_("print only tags that contain the commit")),
+		OPT_MERGED(&filter, N_("print only tags that are merged")),
+		OPT_NO_MERGED(&filter, N_("print only tags that are not merged")),
 		OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
 			     N_("field name to sort on"), &parse_opt_ref_sorting),
 		{
@@ -416,6 +418,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		die(_("--contains option is only allowed with -l."));
 	if (filter.points_at.nr)
 		die(_("--points-at option is only allowed with -l."));
+	if (filter.merge_commit)
+		die(_("--merged and --no-merged option are only allowed with -l"));
 	if (cmdmode == 'd')
 		return for_each_tag_name(argv, delete_tag);
 	if (cmdmode == 'v')
t/t7004-tag.sh +27 -0
index 8987fb1..3dd2f51 100755
@@ -1531,4 +1531,31 @@ test_expect_success '--format should list tags as per format given' '
 	test_cmp expect actual
 '
 
+test_expect_success 'setup --merged test tags' '
+	git tag mergetest-1 HEAD~2 &&
+	git tag mergetest-2 HEAD~1 &&
+	git tag mergetest-3 HEAD
+'
+
+test_expect_success '--merged cannot be used in non-list mode' '
+	test_must_fail git tag --merged=mergetest-2 foo
+'
+
+test_expect_success '--merged shows merged tags' '
+	cat >expect <<-\EOF &&
+	mergetest-1
+	mergetest-2
+	EOF
+	git tag -l --merged=mergetest-2 mergetest-* >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success '--no-merged show unmerged tags' '
+	cat >expect <<-\EOF &&
+	mergetest-3
+	EOF
+	git tag -l --no-merged=mergetest-2 mergetest-* >actual &&
+	test_cmp expect actual
+'
+
 test_done
Signature · 1 line
2.5.0
#8 lore raw

Re: [PATCH v14 04/13] ref-filter: implement an `align` atom

In reply to Eric Sunshine
Quoted 24 lines · Eric Sunshine
Quoted 24 lines

Add documentation and tests for the same.

---

ref-filter.c +7 -0
index 432cea0..21c8b5f 100644
@@ -53,6 +54,13 @@ static struct {
        { "flag" },
        { "HEAD" },
        { "color" },
+       { "align" },
+       { "end" },
+};
+
+struct align {
+       align_type position;
+       unsigned int width;
 };
 
 #define REF_FORMATTING_STATE_INIT  { 0, NULL }
@@ -69,6 +79,8 @@ struct ref_formatting_state {
 
Quoted 9 lines

ref-filter.c struct ref_formatting_state { · line 82

Quoted 3 lines
ref-filter.c +1 -0
 struct atom_value {
        const char *s;
+       struct align *align;

Why does 'align' need to be heap-allocated rather than just being a direct member of 'atom_value'? Does 'align' need to exist beyond the lifetime of its 'atom_value'? If not, making it a direct member might simplify resource management (no need to free it).

But it does, since we carry over the contents of align from atom_value to cb_data of ref_formatting_stack and that holds the value until we read the %(end)
atom hence it seemed like a better choice to allocate it on the heap

Signature · 2 lines
Regards,
Karthik Nayak
#9 lore raw

Re: [PATCH v14 04/13] ref-filter: implement an `align` atom

Quoted 18 lines · Karthik Nayak
Quoted 8 lines · Eric Sunshine
Quoted 8 lines
ref-filter.c +3 -0
+struct align {
+       align_type position;
+       unsigned int width;
 };
 
 #define REF_FORMATTING_STATE_INIT  { 0, NULL }
@@ -69,6 +79,8 @@ struct ref_formatting_state {
 
Quoted 8 lines

ref-filter.c struct ref_formatting_state { · line 82

Quoted 3 lines
ref-filter.c +1 -0
 struct atom_value {
        const char *s;
+       struct align *align;

Why does 'align' need to be heap-allocated rather than just being a direct member of 'atom_value'? Does 'align' need to exist beyond the lifetime of its 'atom_value'? If not, making it a direct member might simplify resource management (no need to free it).

Quoted 4 lines

But it does, since we carry over the contents of align from atom_value to cb_data of ref_formatting_stack and that holds the value until we read the %(end)
atom hence it seemed like a better choice to allocate it on the heap

So, you're saying that the 'atom_value' instance no longer exists at the point that processing of %(end) needs to access the alignment properties? If so, then heap allocation make sense. Thanks.

#10 lore raw

Re: [PATCH v14 04/13] ref-filter: implement an `align` atom

Quoted 19 lines · Eric Sunshine
Quoted 18 lines · Karthik Nayak
Quoted 16 lines · Eric Sunshine

ref-filter.c struct ref_formatting_state { · line 82

Quoted 11 lines
ref-filter.c +4 -0
+struct align {
+       align_type position;
+       unsigned int width;
 };
 
 #define REF_FORMATTING_STATE_INIT  { 0, NULL }
@@ -69,6 +79,8 @@ struct ref_formatting_state {
 
 struct atom_value {
        const char *s;
+       struct align *align;

Why does 'align' need to be heap-allocated rather than just being a direct member of 'atom_value'? Does 'align' need to exist beyond the lifetime of its 'atom_value'? If not, making it a direct member might simplify resource management (no need to free it).

Quoted 8 lines
Quoted 4 lines

But it does, since we carry over the contents of align from atom_value to cb_data of ref_formatting_stack and that holds the value until we read the %(end)
atom hence it seemed like a better choice to allocate it on the heap

So, you're saying that the 'atom_value' instance no longer exists at the point that processing of %(end) needs to access the alignment properties? If so, then heap allocation make sense. Thanks.

I was actually wrong there, if you see populate_value() the ref is filled with atoms
which aren't really deallocated, hence the atom_value remains with the ref in ref->value[atom]. where atom is obtained using parse_ref_filter_atom() hence it makes sense to make it static. Thanks

Signature · 2 lines
Regards,
Karthik Nayak
#11 lore raw

Re: [PATCH v14 04/13] ref-filter: implement an `align` atom

In reply to Eric Sunshine
Quoted 28 lines · Eric Sunshine
Quoted 21 lines · Karthik Nayak

Implement an align atom which left-, middle-, or right-aligns the content between %(align:..) and %(end).

It is followed by :<width>,<position>, where the <position> is either left, right or middle and <width> is the size of the area into which the content will be placed. If the content between %(align:) and %(end) is more than the width then no alignment is performed. e.g. to align a refname atom to the middle with a total width of 40 we can do: --format="%(align:middle,40)%(refname)%(end)".

We now have a handler() for each atom_value which will be called when that atom_value is being parsed, and similarly an at_end function for each element of the stack which is to be called when the end atom is encountered. Using this we implement the align atom which aligns the given strbuf by calling strbuf_utf8_align() from utf8.c.

Extract perform_quote_formatting() from append_atom(). Given a string a quote_value and a strbuf, perform_quote_formatting() formats the string based on the quote_value and stores it into the strbuf.

Quoted 5 lines

Ensure that quote formatting is performed on the whole of %(align)...%(end) rather than individual atoms. We do this by skipping individual quote formatting for atoms whenever the stack has more than one element, and performing formatting for the entire stack element when the %(end) atoms is encountered.

Quoted 6 lines

This patch seems to be conflating three distinct changes:

1. adding %(align:) atom
2. extracting quoting logic to a separate function
3. quoting top-level %(align:) but not contained atoms

I'll extract 2 to a separate commit.

Quoted 10 lines

In fact, #3 seems too specially tied to %(align:)...%(end). One might expect that the logic for determining when to quote should be independent of any particular atom, which suggests that this logic is being handled at the wrong level, and that %(align:) shouldn't have to know anything about quoting. I'd have thought the quoting logic would naturally accompany the introduction of the formatting state stack mechanism in patch 2/13, and that that would generically work with all atoms, including %(align:) and whatever new ones are added in the future.

Quoted 3 lines

But, I may not have been following the quoting discussion closely, so perhaps these observations are incorrect? See more below regarding %(if:).

You're right, more below.

Quoted 58 lines
Quoted 24 lines

Add documentation and tests for the same.

---

ref-filter.c +7 -0
index 432cea0..21c8b5f 100644
@@ -53,6 +54,13 @@ static struct {
        { "flag" },
        { "HEAD" },
        { "color" },
+       { "align" },
+       { "end" },
+};
+
+struct align {
+       align_type position;
+       unsigned int width;
 };
 
 #define REF_FORMATTING_STATE_INIT  { 0, NULL }

ref-filter.c struct ref_formatting_state { · line 82

Quoted 4 lines
ref-filter.c +1 -0
@@ -69,6 +79,8 @@ struct ref_formatting_state {
 
 struct atom_value {
        const char *s;
+       struct align *align;

Why does 'align' need to be heap-allocated rather than just being a direct member of 'atom_value'? Does 'align' need to exist beyond the lifetime of its 'atom_value'? If not, making it a direct member might simplify resource management (no need to free it).

Quoted 5 lines
ref-filter.c +1 -0
+       void (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);
        unsigned long ul; /* used for sorting when not FIELD_STR */
 };
 

ref-filter.c static inline char *copy_advance(char *dst, const char *src) · line 652

Quoted 9 lines
ref-filter.c +9 -0
@@ -632,6 +644,84 @@ static inline char *copy_advance(char *dst, const char *src)
+static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
+{
+       /*
+        * Quote formatting is only done when the stack has a single
+        * element. Otherwise quote formatting is done on the
+        * element's entire output strbuf when the %(end) atom is
+        * encountered.
+        */
+       if (!state->stack->prev)

With the disclaimer that I wasn't following the quoting discussion
closely: Is this condition going to be sufficient for all cases, such as an %(if:) atom? That is, if you have:

    %(if:notempty)%(bloop)%(then) --option=%(bloop)%(end)

isn't the intention that, %(bloop) within the %(then) section should be quoted but not the literal "--option="?

Quoted 8 lines

The condition `!state->stack->prev' would be insufficient to handle this if %(if:) pushes one or more states onto the stack, no? This implies that you might want an explicit flag for enabling/disabling quoting rather than relying upon the state of the stack, and that individual atom handlers would control that flag.

Or, am I misunderstanding due to not having followed the discussion closely?

Yes this wont be work for what you've said.

To sum up the discussion:
We didn't want atoms within the %(align)....%(end) to be quoted separately rather the whole aligned buffer to be quoted at the end.

But this does conflict with %(if)...%(end). So it makes sense to change it to only have checks for %(align) atom being used.

So probably:

static void append_atom(struct atom_value *v, struct ref_formatting_state *state)

{
    if (state->stack->at_end == align_handler)
        strbuf_addstr(&state->stack->output, v->s);
    else
        quote_formatting(&state->stack->output, v->s, state->quote_style);
}

and

static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)

{
    struct ref_formatting_stack *current = state->stack;
    struct strbuf s = STRBUF_INIT;

    if (!current->at_end)
        die(_("format: `end` atom used without a supporting atom"));
    current->at_end(current);
    /*
     * Whenever we have more than one stack element that means we
     * are using a align modifier atom. In that case we need to
     * perform quote formatting.
     */
    if (current->at_end == align_handler) {
        quote_formatting(&s, current->output.buf, state->quote_style);
        strbuf_reset(&current->output);
        strbuf_addbuf(&current->output, &s);
    }
    strbuf_release(&s);
    pop_stack_element(&state->stack);
}
Quoted 17 lines
Quoted 17 lines
ref-filter.c +16 -0
+               quote_formatting(&state->stack->output, v->s, state->quote_style);
+       else
+               strbuf_addstr(&state->stack->output, v->s);
+}
+
+static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
+{
+       struct ref_formatting_stack *current = state->stack;
+       struct strbuf s = STRBUF_INIT;
+
+       if (!current->at_end)
+               die(_("format: `end` atom used without a supporting atom"));
+       current->at_end(current);
+       /*
+        * Whenever we have more than one stack element that means we
+        * are using a certain modifier atom. In that case we need to
Quoted 9 lines

ref-filter.c static inline char *copy_advance(char *dst, const char *src) · line 719

Quoted 6 lines
ref-filter.c +6 -0
+        * perform quote formatting.
+        */
+       if (state->stack->prev) {
+               quote_formatting(&s, current->output.buf, state->quote_style);
+               strbuf_reset(&current->output);
+               strbuf_addbuf(&current->output, &s);

strbuf_swap() can replace the above two lines.

Yes, this makes sense.

Quoted 10 lines
Quoted 10 lines
ref-filter.c +5 -0
+       }
+       strbuf_release(&s);
+       pop_stack_element(&state->stack);
+}
+
 /*
  * Parse the object referred by ref, and grab needed value.
  */
@@ -725,6 +818,37 @@ static void populate_value(struct ref_array_item *ref)
                        else
Quoted 9 lines

ref-filter.c static void populate_value(struct ref_array_item *ref) · line 822

Quoted 4 lines
ref-filter.c +2 -0
                                v->s = " ";
                        continue;
+               } else if (!strcmp(name, "align"))
+                       die(_("format: incomplete use of the `align` atom"));

Why does %(align) get flagged as a malformation of %(align:), whereas %(color) does not get flagged as a malformation of %(color:)? Why does one deserve special treatment but not the other?

Didn't see that, I think its needed to add a check for both like :

else if (!strcmp(name, "align") || !strcmp(name, "color"))

            die(_("format: improper usage of %s atom"), name);

I had a look if any other atoms need a subvalue to operate, couldn't find any.

Quoted 8 lines

ref-filter.c static void populate_value(struct ref_array_item *ref) · line 829

Quoted 7 lines
ref-filter.c +7 -0
+               else if (skip_prefix(name, "align:", &valp)) {
+                       struct align *align = xmalloc(sizeof(struct align));
+                       struct strbuf **s = strbuf_split_str(valp, ',', 0);
+
+                       /* If the position is given trim the ',' from the first strbuf */
+                       if (s[1])
+                               strbuf_remove(s[0], s[0]->len - 1, 1);
Quoted 4 lines

This is a truncation operation, which may be more idiomatically stated as:

    strbuf_setlen(s[0], s[0]->len - 1);

Will do.

Quoted 16 lines

ref-filter.c static void populate_value(struct ref_array_item *ref) · line 836

Quoted 7 lines
ref-filter.c +7 -0
+
+                       if (strtoul_ui(s[0]->buf, 10, &align->width))
+                               die(_("positive width expected align:%s"), s[0]->buf);
+
+                       /* If no position is given, default to ALIGN_LEFT */
+                       if (!s[1] || !strcmp(s[1]->buf, "left"))
+                               align->position = ALIGN_LEFT;

If you structured the code like this:

    if (!s[1])
        align->position = ALIGN_LEFT;
    else if (!strcmp(s[1]->buf, "left"))
        align->position = ALIGN_LEFT;
    else if ...
Quoted 5 lines

then the comment about "default" would become unnecessary, and it would be easier to change the default in the future (if the need ever arose), however, this is a very minor point, and I don't care strongly.

Since there are other changes to be made, ill add this along.

Quoted 42 lines
Quoted 19 lines
ref-filter.c +6 -0
+                       else if (!strcmp(s[1]->buf, "right"))
+                               align->position = ALIGN_RIGHT;
+                       else if (!strcmp(s[1]->buf, "middle"))
+                               align->position = ALIGN_MIDDLE;
+                       else
+                               die(_("improper format entered align:%s"), s[1]->buf);
t/t6302-for-each-ref-filter.sh +8 -0
index 505a360..cef7a41 100755
@@ -81,4 +81,89 @@ test_expect_success 'filtering with --contains' '
+# Everything in between the %(align)...%(end) atom must be quoted, hence we test this by
+# introducing single quote's in %(align)...%(end), which must not be escaped.
+
+sq="'"
+
+test_expect_success 'alignment with format quote' '
+       cat >expect <<-EOF &&
+       refname is ${sq}           ${sq}\\${sq}${sq}master${sq}\\${sq}${sq}           ${sq}|

t/t6302-for-each-ref-filter.sh test_expect_success 'filtering with --contains' ' · line 98

Quoted 10 lines
t/t6302-for-each-ref-filter.sh +10 -0
+       refname is ${sq}            ${sq}\\${sq}${sq}side${sq}\\${sq}${sq}            ${sq}|
+       refname is ${sq}          ${sq}\\${sq}${sq}odd/spot${sq}\\${sq}${sq}          ${sq}|
+       refname is ${sq}         ${sq}\\${sq}${sq}double-tag${sq}\\${sq}${sq}         ${sq}|
+       refname is ${sq}            ${sq}\\${sq}${sq}four${sq}\\${sq}${sq}            ${sq}|
+       refname is ${sq}            ${sq}\\${sq}${sq}one${sq}\\${sq}${sq}             ${sq}|
+       refname is ${sq}         ${sq}\\${sq}${sq}signed-tag${sq}\\${sq}${sq}         ${sq}|
+       refname is ${sq}           ${sq}\\${sq}${sq}three${sq}\\${sq}${sq}            ${sq}|
+       refname is ${sq}            ${sq}\\${sq}${sq}two${sq}\\${sq}${sq}             ${sq}|
+       EOF
+       git for-each-ref --shell --format="refname is %(align:30,middle)${sq}%(refname:short)${sq}%(end)|" >actual &&

This can be much less noisy if you change the second argument of test_expect_success to use double-quote rather than single-quote. That would allow you to use single-quote directly in the test and expected output rather than having to use ${sq} indirection.

test_expect_success 'alignment with format quote' "
cat >expect <<-\EOF &&
refname is ' '''master''' '|
...
EOF
git for-each-ref --shell --format='refname ...
\\'%(rename:short)\\'...|'

Quoted 4 lines
    "

or something.

Will check it out, thanks for the review.

Signature · 2 lines
Regards,
Karthik Nayak
#12 lore raw

Re: [PATCH v14 04/13] ref-filter: implement an `align` atom

Quoted 29 lines · Karthik Nayak
Quoted 20 lines · Eric Sunshine

ref-filter.c static inline char *copy_advance(char *dst, const char *src) · line 697

Quoted 9 lines · Karthik Nayak
ref-filter.c +9 -0
+static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
+{
+       /*
+        * Quote formatting is only done when the stack has a single
+        * element. Otherwise quote formatting is done on the
+        * element's entire output strbuf when the %(end) atom is
+        * encountered.
+        */
+       if (!state->stack->prev)

With the disclaimer that I wasn't following the quoting discussion
closely: Is this condition going to be sufficient for all cases, such as an %(if:) atom? That is, if you have:

    %(if:notempty)%(bloop)%(then) --option=%(bloop)%(end)

isn't the intention that, %(bloop) within the %(then) section should be quoted but not the literal "--option="?

Quoted 7 lines

The condition `!state->stack->prev' would be insufficient to handle this if %(if:) pushes one or more states onto the stack, no? This implies that you might want an explicit flag for enabling/disabling quoting rather than relying upon the state of the stack, and that individual atom handlers would control that flag.

Or, am I misunderstanding due to not having followed the discussion closely?

Quoted 8 lines

Yes this wont be work for what you've said.

To sum up the discussion:
We didn't want atoms within the %(align)....%(end) to be quoted separately rather the whole aligned buffer to be quoted at the end.

But this does conflict with %(if)...%(end). So it makes sense to change it to only have checks for %(align) atom being used.

If I understand Junio's response correctly, then this doesn't sound correct either. Rather than imbuing only %(align:) with special quoting knowledge, it sounds like quoting should be handled generically for all top-level atoms and pseudo-atoms, including %(if:) and others which may come later.

(But perhaps I'm still misunderstanding...)

Quoted 6 lines

So probably:

static void append_atom(struct atom_value *v, struct ref_formatting_state *state)

{
    if (state->stack->at_end == align_handler)

This couples append_atom() far too tightly with the %(align:) atom. If you really need to do this sort of special-casing, then it probably would make more sense to have an explicit flag saying whether or not quoting should be done, rather than tying it specifically to the %(align:) atom.

However, (again, if I'm understanding Junio's response), your original `!state->stack->prev' condition might be sufficient after all.

Quoted 9 lines
        strbuf_addstr(&state->stack->output, v->s);
    else
        quote_formatting(&state->stack->output, v->s, state->quote_style);
}

and

static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)

Quoted 13 lines
{
    struct ref_formatting_stack *current = state->stack;
    struct strbuf s = STRBUF_INIT;

    if (!current->at_end)
        die(_("format: `end` atom used without a supporting atom"));
    current->at_end(current);
    /*
     * Whenever we have more than one stack element that means we
     * are using a align modifier atom. In that case we need to
     * perform quote formatting.
     */
    if (current->at_end == align_handler) {

Ditto about being too tightly coupled to %(align:). Such logic should likely be generic for any such atom.

Quoted 18 lines
        quote_formatting(&s, current->output.buf, state->quote_style);
        strbuf_reset(&current->output);
        strbuf_addbuf(&current->output, &s);
    }
    strbuf_release(&s);
    pop_stack_element(&state->stack);
}
Quoted 10 lines

ref-filter.c static void populate_value(struct ref_array_item *ref) · line 822

Quoted 6 lines
ref-filter.c +2 -0
@@ -725,6 +818,37 @@ static void populate_value(struct ref_array_item *ref)
                        else
                                v->s = " ";
                        continue;
+               } else if (!strcmp(name, "align"))
+                       die(_("format: incomplete use of the `align` atom"));

Why does %(align) get flagged as a malformation of %(align:), whereas %(color) does not get flagged as a malformation of %(color:)? Why does one deserve special treatment but not the other?

Quoted 7 lines

Didn't see that, I think its needed to add a check for both like :

else if (!strcmp(name, "align") || !strcmp(name, "color"))

            die(_("format: improper usage of %s atom"), name);

I had a look if any other atoms need a subvalue to operate, couldn't find any.

Hmm, I'm not convinced that either %(align) or %(color) need to be called out specially. What is the current behavior when these "malformations" or any other misspelled atoms are used? Does it error out? Does it simply ignore them and pass them through to the output unmolested?

#13 lore raw

Re: [PATCH v14 04/13] ref-filter: implement an `align` atom

Quoted 39 lines · Eric Sunshine
Quoted 29 lines · Karthik Nayak
Quoted 20 lines · Eric Sunshine

ref-filter.c static inline char *copy_advance(char *dst, const char *src) · line 697

Quoted 9 lines · Karthik Nayak
ref-filter.c +9 -0
+static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
+{
+       /*
+        * Quote formatting is only done when the stack has a single
+        * element. Otherwise quote formatting is done on the
+        * element's entire output strbuf when the %(end) atom is
+        * encountered.
+        */
+       if (!state->stack->prev)

With the disclaimer that I wasn't following the quoting discussion
closely: Is this condition going to be sufficient for all cases, such as an %(if:) atom? That is, if you have:

    %(if:notempty)%(bloop)%(then) --option=%(bloop)%(end)

isn't the intention that, %(bloop) within the %(then) section should be quoted but not the literal "--option="?

Quoted 7 lines

The condition `!state->stack->prev' would be insufficient to handle this if %(if:) pushes one or more states onto the stack, no? This implies that you might want an explicit flag for enabling/disabling quoting rather than relying upon the state of the stack, and that individual atom handlers would control that flag.

Or, am I misunderstanding due to not having followed the discussion closely?

Quoted 8 lines

Yes this wont be work for what you've said.

To sum up the discussion:
We didn't want atoms within the %(align)....%(end) to be quoted separately rather the whole aligned buffer to be quoted at the end.

But this does conflict with %(if)...%(end). So it makes sense to change it to only have checks for %(align) atom being used.

Quoted 8 lines

If I understand Junio's response correctly, then this doesn't sound correct either. Rather than imbuing only %(align:) with special quoting knowledge, it sounds like quoting should be handled generically for all top-level atoms and pseudo-atoms, including %(if:) and others which may come later.

(But perhaps I'm still misunderstanding...)

I was sure about how %(align)...%(end) was to be quoted at the end. From Junio's comment it seems all top level atoms need to be quoted at the end so with regards to this, the new changes aren't needed and the old changes will hold.

Quoted 7 lines
Quoted 6 lines

So probably:

static void append_atom(struct atom_value *v, struct ref_formatting_state *state)

{
    if (state->stack->at_end == align_handler)
Quoted 9 lines

This couples append_atom() far too tightly with the %(align:) atom. If you really need to do this sort of special-casing, then it probably would make more sense to have an explicit flag saying whether or not quoting should be done, rather than tying it specifically to the %(align:) atom.

However, (again, if I'm understanding Junio's response), your original `!state->stack->prev' condition might be sufficient after all.

same as above.

Quoted 23 lines
Quoted 9 lines
        strbuf_addstr(&state->stack->output, v->s);
    else
        quote_formatting(&state->stack->output, v->s, state->quote_style);
}

and

static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)

Quoted 13 lines
{
    struct ref_formatting_stack *current = state->stack;
    struct strbuf s = STRBUF_INIT;

    if (!current->at_end)
        die(_("format: `end` atom used without a supporting atom"));
    current->at_end(current);
    /*
     * Whenever we have more than one stack element that means we
     * are using a align modifier atom. In that case we need to
     * perform quote formatting.
     */
    if (current->at_end == align_handler) {
Quoted 3 lines

Ditto about being too tightly coupled to %(align:). Such logic should likely be generic for any such atom.

Shouldn't be needed, was mistaken, sticking to the old logic.

Quoted 23 lines
Quoted 23 lines
        quote_formatting(&s, current->output.buf, state->quote_style);
        strbuf_reset(&current->output);
        strbuf_addbuf(&current->output, &s);
    }
    strbuf_release(&s);
    pop_stack_element(&state->stack);
}
Quoted 10 lines

ref-filter.c static void populate_value(struct ref_array_item *ref) · line 822

Quoted 6 lines
ref-filter.c +2 -0
@@ -725,6 +818,37 @@ static void populate_value(struct ref_array_item *ref)
                        else
                                v->s = " ";
                        continue;
+               } else if (!strcmp(name, "align"))
+                       die(_("format: incomplete use of the `align` atom"));

Why does %(align) get flagged as a malformation of %(align:), whereas %(color) does not get flagged as a malformation of %(color:)? Why does one deserve special treatment but not the other?

Didn't see that, I think its needed to add a check for both like :

else if (!strcmp(name, "align") || !strcmp(name, "color"))

            die(_("format: improper usage of %s atom"), name);
Quoted 8 lines
Quoted 2 lines

I had a look if any other atoms need a subvalue to operate, couldn't find any.

Hmm, I'm not convinced that either %(align) or %(color) need to be called out specially. What is the current behavior when these "malformations" or any other misspelled atoms are used? Does it error out? Does it simply ignore them and pass them through to the output unmolested?

It just simply ignores them currently, which is kinda bad, as the user is given no
warning, and the atom is ineffective.

Signature · 2 lines
Regards,
Karthik Nayak
#20 lore raw

Re: [PATCH v14 04/13] ref-filter: implement an `align` atom

In reply to Eric Sunshine
Quoted 8 lines · Eric Sunshine

With the disclaimer that I wasn't following the quoting discussion
closely: Is this condition going to be sufficient for all cases, such as an %(if:) atom? That is, if you have:

    %(if:notempty)%(bloop)%(then) --option=%(bloop)%(end)

isn't the intention that, %(bloop) within the %(then) section should be quoted but not the literal "--option="?

I think you'll see that the intention of the above is to quote the entirty of the result of %(if...)...%(end) if you read the previous discussion. The "quoting" is used when you say you are making --format write a script in specified programming language, e.g.

	for-each-ref --shell --format='
        	a=%(atom) b=%(if...)...%(end)
		do interesting things using $a and $b here
	' | sh

You are correct to point out in the earlier part of your message I am responding to that %(align) is not special and any nested thing including %(if) will uniformly trigger the same "usually each atom is quoted separately, but with this opening atom, everything up to the matching end atom is evaluated first and then the result is quoted" logic.

#21 lore raw

Re: [PATCH v14 04/13] ref-filter: implement an `align` atom

Quoted 20 lines · Junio C Hamano
Quoted 8 lines · Eric Sunshine

With the disclaimer that I wasn't following the quoting discussion
closely: Is this condition going to be sufficient for all cases, such as an %(if:) atom? That is, if you have:

    %(if:notempty)%(bloop)%(then) --option=%(bloop)%(end)

isn't the intention that, %(bloop) within the %(then) section should be quoted but not the literal "--option="?

I think you'll see that the intention of the above is to quote the entirty of the result of %(if...)...%(end) if you read the previous discussion. The "quoting" is used when you say you are making --format write a script in specified programming language, e.g.

        for-each-ref --shell --format='
                a=%(atom) b=%(if...)...%(end)
                do interesting things using $a and $b here
        ' | sh
Quoted 6 lines

You are correct to point out in the earlier part of your message I am responding to that %(align) is not special and any nested thing including %(if) will uniformly trigger the same "usually each atom is quoted separately, but with this opening atom, everything up to the matching end atom is evaluated first and then the result is quoted" logic.

So, if I'm understanding correctly, the semantic behavior of the current patch seems to be more or less correct, but the implementation (and commit message) place perhaps too much emphasis on specializing quoting suppression only for %(align:), whereas it could/should be generalized?

I am a bit concerned about this code from end_atom_handler():

    /*
     * Whenever we have more than one stack element that means we
     * are using a certain modifier atom. In that case we need to
     * perform quote formatting.
     */
    if (state->stack->prev) {
        quote_formatting(&s, current->output.buf, state->quote_style);
        strbuf_reset(&current->output);
        strbuf_addbuf(&current->output, &s);
    }

Aren't both the comment and the condition backward? Shouldn't quoting be done only for the top-most state on the stack rather than every state other than the top-most? That is, shouldn't the condition be `!state->stack->prev' as it is in append_atom()?

#22 lore raw

Re: [PATCH v14 04/13] ref-filter: implement an `align` atom

Quoted 47 lines · Eric Sunshine
Quoted 20 lines · Junio C Hamano
Quoted 8 lines · Eric Sunshine

With the disclaimer that I wasn't following the quoting discussion
closely: Is this condition going to be sufficient for all cases, such as an %(if:) atom? That is, if you have:

    %(if:notempty)%(bloop)%(then) --option=%(bloop)%(end)

isn't the intention that, %(bloop) within the %(then) section should be quoted but not the literal "--option="?

I think you'll see that the intention of the above is to quote the entirty of the result of %(if...)...%(end) if you read the previous discussion. The "quoting" is used when you say you are making --format write a script in specified programming language, e.g.

        for-each-ref --shell --format='
                a=%(atom) b=%(if...)...%(end)
                do interesting things using $a and $b here
        ' | sh
Quoted 6 lines

You are correct to point out in the earlier part of your message I am responding to that %(align) is not special and any nested thing including %(if) will uniformly trigger the same "usually each atom is quoted separately, but with this opening atom, everything up to the matching end atom is evaluated first and then the result is quoted" logic.

So, if I'm understanding correctly, the semantic behavior of the current patch seems to be more or less correct, but the implementation (and commit message) place perhaps too much emphasis on specializing quoting suppression only for %(align:), whereas it could/should be generalized?

I am a bit concerned about this code from end_atom_handler():

    /*
     * Whenever we have more than one stack element that means we
     * are using a certain modifier atom. In that case we need to
     * perform quote formatting.
     */
    if (state->stack->prev) {
        quote_formatting(&s, current->output.buf, state->quote_style);
        strbuf_reset(&current->output);
        strbuf_addbuf(&current->output, &s);
    }
Quoted 4 lines

Aren't both the comment and the condition backward? Shouldn't quoting be done only for the top-most state on the stack rather than every state other than the top-most? That is, shouldn't the condition be `!state->stack->prev' as it is in append_atom()?

After seeing the example of quote usage given by Junio, yes you're right. !state->stack->prev is the way to go.

Signature · 2 lines
Regards,
Karthik Nayak
#23 lore raw

Re: [PATCH v14 04/13] ref-filter: implement an `align` atom

Quoted 48 lines · Karthik Nayak
Quoted 47 lines · Eric Sunshine
Quoted 20 lines · Junio C Hamano
Quoted 8 lines · Eric Sunshine

With the disclaimer that I wasn't following the quoting discussion
closely: Is this condition going to be sufficient for all cases, such as an %(if:) atom? That is, if you have:

    %(if:notempty)%(bloop)%(then) --option=%(bloop)%(end)

isn't the intention that, %(bloop) within the %(then) section should be quoted but not the literal "--option="?

I think you'll see that the intention of the above is to quote the entirty of the result of %(if...)...%(end) if you read the previous discussion. The "quoting" is used when you say you are making --format write a script in specified programming language, e.g.

        for-each-ref --shell --format='
                a=%(atom) b=%(if...)...%(end)
                do interesting things using $a and $b here
        ' | sh
Quoted 6 lines

You are correct to point out in the earlier part of your message I am responding to that %(align) is not special and any nested thing including %(if) will uniformly trigger the same "usually each atom is quoted separately, but with this opening atom, everything up to the matching end atom is evaluated first and then the result is quoted" logic.

So, if I'm understanding correctly, the semantic behavior of the current patch seems to be more or less correct, but the implementation (and commit message) place perhaps too much emphasis on specializing quoting suppression only for %(align:), whereas it could/should be generalized?

I am a bit concerned about this code from end_atom_handler():

    /*
     * Whenever we have more than one stack element that means we
     * are using a certain modifier atom. In that case we need to
     * perform quote formatting.
     */
    if (state->stack->prev) {
        quote_formatting(&s, current->output.buf, state->quote_style);
        strbuf_reset(&current->output);
        strbuf_addbuf(&current->output, &s);
    }
Quoted 8 lines
Quoted 4 lines

Aren't both the comment and the condition backward? Shouldn't quoting be done only for the top-most state on the stack rather than every state other than the top-most? That is, shouldn't the condition be `!state->stack->prev' as it is in append_atom()?

After seeing the example of quote usage given by Junio, yes you're right. !state->stack->prev is the way to go.

Also I think you mean !state->stack->prev->prev as we push a new element on the stack when an atom such as %(align) is encountered. And this quoting done at the end should be only for such atoms. Hence it should be !state->stack->prev->prev and not !state->stack->prev.

Signature · 2 lines
Regards,
Karthik Nayak
#24 lore raw

Re: [PATCH v14 04/13] ref-filter: implement an `align` atom

In reply to Karthik Nayak

Hi,

Just a remark on the way iterations are going on with this series: I do agree that each version gets better than the previous one, which is good. However, I have the feeling that we're turning a simple and easy to review series into a monster one (reading "v14 .../13" with a non-trivial interdiff is a bit scary for reviewers).

Karthik: I think you could (should?) have splitted the work again. You're integrating other people's idea in the series, and sometimes I think at some point, a better way would have been: "OK, good idea, I'll implement it in on top of this series" (and possibly implement it on top before you resend, to make sure that the series is ready to welcome the new feature). For example, %(contents:lines=X) is good, but could have waited for the next series IMHO. This way, you get a shorter series to converge faster (straightforward interdiff for the last iterations), and then reviewers can focus on the next, short, series.

The opposite is Zeno paradox kind of series, where you add something new every time you get close to getting merged, and you actually never reach a stable state ;-).

That said, this particular series was a tough one for this, so I'm not even sure my advice would have been applicable ^^.

Signature · 2 lines
Matthieu Moy
http://www-verimag.imag.fr/~moy/
#25 lore raw

Re: [PATCH v14 04/13] ref-filter: implement an `align` atom

Quoted 18 lines · Matthieu Moy

Hi,

Just a remark on the way iterations are going on with this series: I do agree that each version gets better than the previous one, which is good. However, I have the feeling that we're turning a simple and easy to review series into a monster one (reading "v14 .../13" with a non-trivial interdiff is a bit scary for reviewers).

Karthik: I think you could (should?) have splitted the work again. You're integrating other people's idea in the series, and sometimes I think at some point, a better way would have been: "OK, good idea, I'll implement it in on top of this series" (and possibly implement it on top before you resend, to make sure that the series is ready to welcome the new feature). For example, %(contents:lines=X) is good, but could have waited for the next series IMHO. This way, you get a shorter series to converge faster (straightforward interdiff for the last iterations), and then reviewers can focus on the next, short, series.

Quoted 7 lines

The opposite is Zeno paradox kind of series, where you add something new every time you get close to getting merged, and you actually never reach a stable state ;-).

That said, this particular series was a tough one for this, so I'm not even sure my advice would have been applicable ^^.

What you're saying also makes sense, This series has been changing based on the wonderful suggestions given by everyone on the list.

It has changed course and that's in a good way. But maybe you're right I'll probably stop it with the next iteration, which I've already worked on based on Eric's various suggestion.

So I'll push that soon, and more changes can be then added to the top of the series.

Thanks for the suggestion.

Signature · 2 lines
Regards,
Karthik Nayak
#28 lore raw

Re: [PATCH v14 05/13] ref-filter: add option to filter out tags, branches and remotes

In reply to Eric Sunshine
Quoted 19 lines · Eric Sunshine
Quoted 18 lines · Karthik Nayak

Add a function called 'for_each_fullref_in()' to refs.{c,h} which iterates through each ref for the given path without trimming the path and also accounting for broken refs, if mentioned.

Add 'filter_ref_kind()' in ref-filter.c to check the kind of ref being handled and return the kind to 'ref_filter_handler()', where we discard refs which we do not need and assign the kind to needed refs.

---

refs.c +0 -0
index 4e15f60..a9469c2 100644
@@ -2108,6 +2108,15 @@ int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
        return do_for_each_ref(&ref_cache, prefix, fn, strlen(prefix), 0, cb_data);
 }
 
Quoted 5 lines

refs.c int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data) · line 2111

Quoted 1 line
refs.c +1 -0
+int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken)

Booleans such as 'broken' are typically declared 'int' in this codebase, rather than 'unsigned int'.

But doesn't it make more sense to have it as unsigned, since its values are either 0 or 1?

Signature · 2 lines
Regards,
Karthik Nayak
#29 lore raw

Re: [PATCH v14 05/13] ref-filter: add option to filter out tags, branches and remotes

Quoted 9 lines · Karthik Nayak
Quoted 5 lines · Eric Sunshine
Quoted 1 line · Karthik Nayak

+int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken)

Booleans such as 'broken' are typically declared 'int' in this codebase, rather than 'unsigned int'.

But doesn't it make more sense to have it as unsigned, since its values are either 0 or 1?

In C, zero is false and any other value is true, so from that viewpoint, the type doesn't matter much. However, beside being raw instructions for the computer, code should ideally convey its intention as clearly as possible to other programmers. 'int' for boolean has been idiomatic since C's inception, thus is a good way to do so, whereas 'unsigned int' is typically used for magnitude or bit flags, thus is misleading.

#35 lore raw

Re: [PATCH v14 07/13] ref-filter: add support for %(contents:lines=X)

In reply to Karthik Nayak
Quoted 29 lines · Karthik Nayak

In 'tag.c' we can print N lines from the annotation of the tag using the '-n<num>' option. Copy code from 'tag.c' to 'ref-filter' and modify it to support appending of N lines from the annotation of tags to the given strbuf.

Implement %(contents:lines=X) where X lines of the given object are obtained.

Add documentation and test for the same.

---

builtin/tag.c +5 -0
index 471d6b1..0fc7557 100644
@@ -63,6 +64,11 @@ struct align {
        unsigned int width;
 };
 
+struct contents {
+       unsigned int lines;
+       struct object_id oid;
+};
+
 #define REF_FORMATTING_STATE_INIT  { 0, NULL }
 
 struct ref_formatting_stack {

builtin/tag.c struct ref_formatting_state { · line 89

Quoted 4 lines
builtin/tag.c +1 -0
@@ -80,6 +86,7 @@ struct ref_formatting_state {
 struct atom_value {
        const char *s;
        struct align *align;
+       struct contents *contents;

Same question as for 'align': Does 'contents' need to be heap-allocated because it must exist beyond the lifetime of 'atom_value'? If not, making it just a plain member of 'atom_value' would simplify memory management (no need to free it).

Also, will 'align' and 'contents' ever be used at the same time? If not, it might make sense to place them in a 'union' (not for the memory saving, but to make it clear to the reader that their use is mutually exclusive).

More below.

Quoted 39 lines
builtin/tag.c +32 -0
        void (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);
        unsigned long ul; /* used for sorting when not FIELD_STR */
 };
@@ -569,6 +576,61 @@ static void find_subpos(const char *buf, unsigned long sz,
        *nonsiglen = *sig - buf;
 }
 
+/*
+ * If 'lines' is greater than 0, append that many lines from the given
+ * object_id 'oid' to the given strbuf.
+ */
+static void append_tag_lines(struct strbuf *out, const struct object_id *oid, int lines)
+{
+       int i;
+       unsigned long size;
+       enum object_type type;
+       char *buf, *sp, *eol;
+       size_t len;
+
+       buf = read_sha1_file(oid->hash, &type, &size);
+       if (!buf)
+               die_errno("unable to read object %s", oid_to_hex(oid));
+       if (type != OBJ_COMMIT && type != OBJ_TAG)
+               goto free_return;
+       if (!size)
+               die("an empty %s object %s?",
+                   typename(type), oid_to_hex(oid));
+
+       /* skip header */
+       sp = strstr(buf, "\n\n");
+       if (!sp)
+               goto free_return;
+
+       /* only take up to "lines" lines, and strip the signature from a tag */
+       if (type == OBJ_TAG)
+               size = parse_signature(buf, size);
+       for (i = 0, sp += 2; i < lines && sp < buf + size; i++) {
+               if (i)
+                       strbuf_addstr(out, "\n    ");

builtin/tag.c static void find_subpos(const char *buf, unsigned long sz, · line 620

Quoted 10 lines
builtin/tag.c +10 -0
+               eol = memchr(sp, '\n', size - (sp - buf));
+               len = eol ? eol - sp : size - (sp - buf);
+               strbuf_add(out, sp, len);
+               if (!eol)
+                       break;
+               sp = eol + 1;
+       }
+free_return:
+       free(buf);
+}

I understand that you want to re-use this code from tag.c:show_tag_lines(), but (from a very cursory read) isn't this duplicating logic and processing already done elsewhere in ref-filter.c? More about this below.

builtin/tag.c static void find_subpos(const char *buf, unsigned long sz, · line 624

Quoted 4 lines
builtin/tag.c +4 -0
+
+static void contents_lines_handler(struct atom_value *atomv, struct ref_formatting_state *state)
+{
+       struct contents *contents = (struct contents *)atomv->contents;

Why is this cast needed?

Quoted 19 lines
builtin/tag.c +10 -1
+       struct strbuf s = STRBUF_INIT;
+
+       append_tag_lines(&s, &contents->oid, contents->lines);
+       quote_formatting(&state->stack->output, s.buf, state->quote_style);
+       strbuf_release(&s);
+
+       free(contents);
+}
@@ -588,7 +651,8 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
                    strcmp(name, "contents") &&
                    strcmp(name, "contents:subject") &&
                    strcmp(name, "contents:body") &&
-                   strcmp(name, "contents:signature"))
+                   strcmp(name, "contents:signature") &&
+                   !starts_with(name, "contents:lines="))
                        continue;
                if (!subpos)
                        find_subpos(buf, sz,

builtin/tag.c static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj · line 680

Quoted 9 lines
builtin/tag.c +6 -0
@@ -608,6 +672,15 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
                        v->s = xmemdupz(sigpos, siglen);
                else if (!strcmp(name, "contents"))
                        v->s = xstrdup(subpos);
+               else if (skip_prefix(name, "contents:lines=", &valp)) {
+                       struct contents *contents = xmalloc(sizeof(struct contents));
+
+                       if (strtoul_ui(valp, 10, &contents->lines))
+                               die(_("positive width expected align:%s"), valp);
+                       hashcpy(contents->oid.hash, obj->sha1);

The logic in append_tag_lines() which was copied from tag.c:show_tag_lines() goes through the effort of loading the object and parsing it, but hasn't the object already been loaded and parsed by the time you get to this point? Assuming I'm reading this correctly, wouldn't it make more sense to take advantage of the work already done loading and parsing the object rather than repeating it all inside append_tag_lines()?

Quoted 16 lines
builtin/tag.c +3 -0
+                       v->handler = contents_lines_handler;
+                       v->contents = contents;
+               }
        }
 }
t/t6302-for-each-ref-filter.sh +3 -0
index cef7a41..0277498 100755
@@ -166,4 +166,20 @@ test_expect_success 'nested alignment' '
        test_cmp expect actual
 '
 
+test_expect_success 'check `%(contents:lines=X)`' '
+       cat >expect <<-\EOF &&
+       master three

t/t6302-for-each-ref-filter.sh test_expect_success 'nested alignment' ' · line 181

Quoted 10 lines
t/t6302-for-each-ref-filter.sh +10 -0
+       side four
+       odd/spot three
+       double-tag Annonated doubly
+       four four
+       one one
+       signed-tag A signed tag message
+       three three
+       two two
+       EOF
+       git for-each-ref --format="%(refname:short) %(contents:lines=1)" >actual &&

Maybe also test some edge cases, such as line=0, lines=-1 (an invalid value), lines=2, lines=9999999 (a value larger than the number of lines in any object).

Quoted 6 lines
t/t6302-for-each-ref-filter.sh +3 -1
+       test_cmp expect actual
+'
+
 test_done
--

2.5.0

#36 lore raw

Re: [PATCH v14 07/13] ref-filter: add support for %(contents:lines=X)

Quoted 28 lines · Eric Sunshine
Quoted 27 lines · Karthik Nayak

In 'tag.c' we can print N lines from the annotation of the tag using the '-n<num>' option. Copy code from 'tag.c' to 'ref-filter' and modify it to support appending of N lines from the annotation of tags to the given strbuf.

Implement %(contents:lines=X) where X lines of the given object are obtained.

Add documentation and test for the same.

---

builtin/tag.c +5 -0
index 471d6b1..0fc7557 100644
@@ -63,6 +64,11 @@ struct align {
        unsigned int width;
 };
 
+struct contents {
+       unsigned int lines;
+       struct object_id oid;
+};
+
 #define REF_FORMATTING_STATE_INIT  { 0, NULL }
 
Quoted 12 lines

builtin/tag.c struct ref_formatting_state { · line 89

Quoted 6 lines
builtin/tag.c +1 -0
 struct ref_formatting_stack {
@@ -80,6 +86,7 @@ struct ref_formatting_state {
 struct atom_value {
        const char *s;
        struct align *align;
+       struct contents *contents;

Same question as for 'align': Does 'contents' need to be heap-allocated because it must exist beyond the lifetime of 'atom_value'? If not, making it just a plain member of 'atom_value' would simplify memory management (no need to free it).

In this context that makes sense, as the value is only needed for the current atom value.

Quoted 5 lines

Also, will 'align' and 'contents' ever be used at the same time? If not, it might make sense to place them in a 'union' (not for the memory saving, but to make it clear to the reader that their use is mutually exclusive).

Not quite sure if it needs to be mutually exclusive (isn't that up to the user?) But this can be done, as they're separate atoms and at a time only one of them is used.

Quoted 48 lines

More below.

Quoted 46 lines
builtin/tag.c +39 -0
        void (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);
        unsigned long ul; /* used for sorting when not FIELD_STR */
 };
@@ -569,6 +576,61 @@ static void find_subpos(const char *buf, unsigned long sz,
        *nonsiglen = *sig - buf;
 }
 
+/*
+ * If 'lines' is greater than 0, append that many lines from the given
+ * object_id 'oid' to the given strbuf.
+ */
+static void append_tag_lines(struct strbuf *out, const struct object_id *oid, int lines)
+{
+       int i;
+       unsigned long size;
+       enum object_type type;
+       char *buf, *sp, *eol;
+       size_t len;
+
+       buf = read_sha1_file(oid->hash, &type, &size);
+       if (!buf)
+               die_errno("unable to read object %s", oid_to_hex(oid));
+       if (type != OBJ_COMMIT && type != OBJ_TAG)
+               goto free_return;
+       if (!size)
+               die("an empty %s object %s?",
+                   typename(type), oid_to_hex(oid));
+
+       /* skip header */
+       sp = strstr(buf, "\n\n");
+       if (!sp)
+               goto free_return;
+
+       /* only take up to "lines" lines, and strip the signature from a tag */
+       if (type == OBJ_TAG)
+               size = parse_signature(buf, size);
+       for (i = 0, sp += 2; i < lines && sp < buf + size; i++) {
+               if (i)
+                       strbuf_addstr(out, "\n    ");
+               eol = memchr(sp, '\n', size - (sp - buf));
+               len = eol ? eol - sp : size - (sp - buf);
+               strbuf_add(out, sp, len);
+               if (!eol)
+                       break;
+               sp = eol + 1;
+       }
Quoted 9 lines

builtin/tag.c static void find_subpos(const char *buf, unsigned long sz, · line 620

Quoted 3 lines
builtin/tag.c +3 -0
+free_return:
+       free(buf);
+}

I understand that you want to re-use this code from tag.c:show_tag_lines(), but (from a very cursory read) isn't this duplicating logic and processing already done elsewhere in ref-filter.c? More about this below.

Replied below.

Quoted 7 lines

builtin/tag.c static void find_subpos(const char *buf, unsigned long sz, · line 624

Quoted 4 lines
builtin/tag.c +4 -0
+
+static void contents_lines_handler(struct atom_value *atomv, struct ref_formatting_state *state)
+{
+       struct contents *contents = (struct contents *)atomv->contents;

Why is this cast needed?

Will remove.

Quoted 29 lines
Quoted 19 lines
builtin/tag.c +10 -1
+       struct strbuf s = STRBUF_INIT;
+
+       append_tag_lines(&s, &contents->oid, contents->lines);
+       quote_formatting(&state->stack->output, s.buf, state->quote_style);
+       strbuf_release(&s);
+
+       free(contents);
+}
@@ -588,7 +651,8 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
                    strcmp(name, "contents") &&
                    strcmp(name, "contents:subject") &&
                    strcmp(name, "contents:body") &&
-                   strcmp(name, "contents:signature"))
+                   strcmp(name, "contents:signature") &&
+                   !starts_with(name, "contents:lines="))
                        continue;
                if (!subpos)
                        find_subpos(buf, sz,

builtin/tag.c static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj · line 680

Quoted 9 lines
builtin/tag.c +6 -0
@@ -608,6 +672,15 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
                        v->s = xmemdupz(sigpos, siglen);
                else if (!strcmp(name, "contents"))
                        v->s = xstrdup(subpos);
+               else if (skip_prefix(name, "contents:lines=", &valp)) {
+                       struct contents *contents = xmalloc(sizeof(struct contents));
+
+                       if (strtoul_ui(valp, 10, &contents->lines))
+                               die(_("positive width expected align:%s"), valp);
+                       hashcpy(contents->oid.hash, obj->sha1);
Quoted 8 lines

The logic in append_tag_lines() which was copied from tag.c:show_tag_lines() goes through the effort of loading the object and parsing it, but hasn't the object already been loaded and parsed by the time you get to this point? Assuming I'm reading this correctly, wouldn't it make more sense to take advantage of the work already done loading and parsing the object rather than repeating it all inside append_tag_lines()?

Makes sense, I'll work on this.

Quoted 22 lines
Quoted 22 lines
builtin/tag.c +3 -0
+                       v->handler = contents_lines_handler;
+                       v->contents = contents;
+               }
        }
 }
t/t6302-for-each-ref-filter.sh +9 -0
index cef7a41..0277498 100755
@@ -166,4 +166,20 @@ test_expect_success 'nested alignment' '
        test_cmp expect actual
 '
 
+test_expect_success 'check `%(contents:lines=X)`' '
+       cat >expect <<-\EOF &&
+       master three
+       side four
+       odd/spot three
+       double-tag Annonated doubly
+       four four
+       one one
+       signed-tag A signed tag message
Quoted 9 lines

t/t6302-for-each-ref-filter.sh test_expect_success 'nested alignment' ' · line 181

Quoted 4 lines
t/t6302-for-each-ref-filter.sh +4 -0
+       three three
+       two two
+       EOF
+       git for-each-ref --format="%(refname:short) %(contents:lines=1)" >actual &&

Maybe also test some edge cases, such as line=0, lines=-1 (an invalid value), lines=2, lines=9999999 (a value larger than the number of lines in any object).

Will do. Thanks for the review.

--
Regards,
Karthik Nayak

#37 lore raw

Re: [PATCH v14 07/13] ref-filter: add support for %(contents:lines=X)

Quoted 15 lines · Karthik Nayak
Quoted 10 lines · Eric Sunshine
Quoted 4 lines · Karthik Nayak
 struct atom_value {
        const char *s;
        struct align *align;

+ struct contents *contents;

Same question as for 'align': Does 'contents' need to be heap-allocated because it must exist beyond the lifetime of 'atom_value'? If not, making it just a plain member of 'atom_value' would simplify memory management (no need to free it).

In this context that makes sense, as the value is only needed for the current atom value.

Quoted 8 lines
Quoted 4 lines

Also, will 'align' and 'contents' ever be used at the same time? If not, it might make sense to place them in a 'union' (not for the memory saving, but to make it clear to the reader that their use is mutually exclusive).

Not quite sure if it needs to be mutually exclusive (isn't that up to the user?) But this can be done, as they're separate atoms and at a time only one of them is used.

I meant "mutually exclusive" in the sense of only one or the other of 'align' and 'contents' ever being used within a single 'atom_value' instance. (I wasn't referring to the user experience.)

#38 lore raw

Re: [PATCH v14 07/13] ref-filter: add support for %(contents:lines=X)

Quoted 4 lines · Eric Sunshine

I meant "mutually exclusive" in the sense of only one or the other of 'align' and 'contents' ever being used within a single 'atom_value' instance. (I wasn't referring to the user experience.)

Sorry for the confusion, we're on the same page :)

Signature · 2 lines
Regards,
Karthik Nayak
#39 lore raw

Re: [PATCH v14 07/13] ref-filter: add support for %(contents:lines=X)

In reply to Karthik Nayak
Quoted 13 lines · Karthik Nayak

In 'tag.c' we can print N lines from the annotation of the tag using the '-n<num>' option. Copy code from 'tag.c' to 'ref-filter' and modify it to support appending of N lines from the annotation of tags to the given strbuf.

Implement %(contents:lines=X) where X lines of the given object are obtained.

Add documentation and test for the same.

---

ref-filter.c static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj · line 679

Quoted 8 lines
ref-filter.c +5 -0
@@ -608,6 +672,15 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
                        v->s = xmemdupz(sigpos, siglen);
                else if (!strcmp(name, "contents"))
                        v->s = xstrdup(subpos);
+               else if (skip_prefix(name, "contents:lines=", &valp)) {
+                       struct contents *contents = xmalloc(sizeof(struct contents));
+
+                       if (strtoul_ui(valp, 10, &contents->lines))
+                               die(_("positive width expected align:%s"), valp);

I forgot to mention this when I reviewed the patch earlier[1], but you copied this error message a bit too literally from the %(align:) atom.

[1]: http://article.gmane.org/gmane.comp.version-control.git/276807

Quoted 6 lines
ref-filter.c +4 -0
+                       hashcpy(contents->oid.hash, obj->sha1);
+                       v->handler = contents_lines_handler;
+                       v->contents = contents;
+               }
        }
 }
#40 lore raw

Re: [PATCH v14 07/13] ref-filter: add support for %(contents:lines=X)

Quoted 26 lines · Eric Sunshine
Quoted 13 lines · Karthik Nayak

In 'tag.c' we can print N lines from the annotation of the tag using the '-n<num>' option. Copy code from 'tag.c' to 'ref-filter' and modify it to support appending of N lines from the annotation of tags to the given strbuf.

Implement %(contents:lines=X) where X lines of the given object are obtained.

Add documentation and test for the same.

---

ref-filter.c static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj · line 679

Quoted 8 lines
ref-filter.c +5 -0
@@ -608,6 +672,15 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
                        v->s = xmemdupz(sigpos, siglen);
                else if (!strcmp(name, "contents"))
                        v->s = xstrdup(subpos);
+               else if (skip_prefix(name, "contents:lines=", &valp)) {
+                       struct contents *contents = xmalloc(sizeof(struct contents));
+
+                       if (strtoul_ui(valp, 10, &contents->lines))
+                               die(_("positive width expected align:%s"), valp);

I forgot to mention this when I reviewed the patch earlier[1], but you copied this error message a bit too literally from the %(align:) atom.

Quoted 2 lines

[1]: http://article.gmane.org/gmane.comp.version-control.git/276807

I fixed that with your other suggestions, should have mentioned it. Thanks

Signature · 2 lines
Regards,
Karthik Nayak
#47 lore raw
In reply to Karthik Nayak
Quoted 1 line · Karthik Nayak

* We perform quoting on each layer of nested alignment.

I do not understand why.

For example, using the tip of karthik/exp on GitHub (on top of this series, d91419b (ref-filter: adopt get_head_description() from branch.c, 2015-08-23)):

git for-each-ref --shell \
--format 'x=%(if)foo%(then)%(align:10)XXX%(end)%(else) not foo %(end)'

I'd expect an output like:

x='XXX '

and instead I get:

x=''\''XXX '\'''

which assigns the value 'XXX ' (including the quotes) to $x. I do
not see a use-case for this (well, I could imagine one where we would later call eval "$x", that seems rather far-fetched).

I think the quoting should be:

1) When the stack contains only the initial element, quote individual atoms.

2) When the stack contains exactly two elements and encountering a %(end) or %(else), quote the entire strbuf of the 2nd level when appending to the 1st level.

3) When the stack contains more than two elements, perform no quoting at all. The quoting will be done later by #2.

I found a segfault while testing:

$ git for-each-ref --format 'x=%(if)%(align:10)%(end)%(then)%(align:10)XXX%(end)%(else)%(end)' --shell
zsh: segmentation fault
Signature · 2 lines
Matthieu Moy
http://www-verimag.imag.fr/~moy/
#48 lore raw
Quoted 30 lines · Matthieu Moy
Quoted 1 line · Karthik Nayak

* We perform quoting on each layer of nested alignment.

I do not understand why.

For example, using the tip of karthik/exp on GitHub (on top of this series, d91419b (ref-filter: adopt get_head_description() from branch.c, 2015-08-23)):

git for-each-ref --shell \
--format 'x=%(if)foo%(then)%(align:10)XXX%(end)%(else) not foo %(end)'

I'd expect an output like:

x='XXX '

and instead I get:

x=''\''XXX '\'''

which assigns the value 'XXX ' (including the quotes) to $x. I do
not see a use-case for this (well, I could imagine one where we would later call eval "$x", that seems rather far-fetched).

I think the quoting should be:

1) When the stack contains only the initial element, quote individual atoms.

Quoted 7 lines

2) When the stack contains exactly two elements and encountering a %(end) or %(else), quote the entire strbuf of the 2nd level when appending to the 1st level.

3) When the stack contains more than two elements, perform no quoting at all. The quoting will be done later by #2.

Yea, That's what Eric was saying, I even made changes which sum up to what you're saying :)

Quoted 5 lines

I found a segfault while testing:

$ git for-each-ref --format 'x=%(if)%(align:10)%(end)%(then)%(align:10)XXX%(end)%(else)%(end)' --shell
zsh: segmentation fault

I wouldn't worry about this ATM, I have made so many changes that the tip is barely changed to reflect those, though I'll have a look at it :)

Signature · 2 lines
Regards,
Karthik Nayak
#50 lore raw
In reply to Karthik Nayak

Documentation/git-for-each-ref.txt Its first line is `contents:subject`, where subject is the concatenation · line 152

Quoted 9 lines · Karthik Nayak
Documentation/git-for-each-ref.txt +1 -0
index 06d468e..1b48b95 100644
@@ -149,6 +149,7 @@ Its first line is `contents:subject`, where subject is the concatenation
 of all lines of the commit message up to the first blank line.  The next
 line is 'contents:body', where body is all of the lines after the first
 blank line.  Finally, the optional GPG signature is `contents:signature`.
+The first `N` lines of the object is obtained using `contents:lines=N`.

"Finally" in the last line of the context is no longer accurate.

Quoted 5 lines
Documentation/git-for-each-ref.txt +5 -0
+test_expect_success 'check `%(contents:lines=X)`' '
+	cat >expect <<-\EOF &&
+	master three
+	side four
+	odd/spot three

Documentation/git-for-each-ref.txt Its first line is `contents:subject`, where subject is the concatenation

Quoted 10 lines
Documentation/git-for-each-ref.txt +10 -0
+	double-tag Annonated doubly
+	four four
+	one one
+	signed-tag A signed tag message
+	three three
+	two two
+	EOF
+	git for-each-ref --format="%(refname:short) %(contents:lines=1)" >actual &&
+	test_cmp expect actual
+'

Nit: I would find it more readable with an actual separator (anything but a space) between %(refname) and %(contents).

Signature · 2 lines
Matthieu Moy
http://www-verimag.imag.fr/~moy/
#51 lore raw
Quoted 14 lines · Matthieu Moy

Documentation/git-for-each-ref.txt Its first line is `contents:subject`, where subject is the concatenation · line 152

Quoted 9 lines · Karthik Nayak
Documentation/git-for-each-ref.txt +1 -0
index 06d468e..1b48b95 100644
@@ -149,6 +149,7 @@ Its first line is `contents:subject`, where subject is the concatenation
 of all lines of the commit message up to the first blank line.  The next
 line is 'contents:body', where body is all of the lines after the first
 blank line.  Finally, the optional GPG signature is `contents:signature`.
+The first `N` lines of the object is obtained using `contents:lines=N`.

"Finally" in the last line of the context is no longer accurate.

Will remove that.

Quoted 16 lines
Quoted 5 lines
Documentation/git-for-each-ref.txt +5 -0
+test_expect_success 'check `%(contents:lines=X)`' '
+     cat >expect <<-\EOF &&
+     master three
+     side four
+     odd/spot three

Documentation/git-for-each-ref.txt Its first line is `contents:subject`, where subject is the concatenation

Quoted 10 lines
Documentation/git-for-each-ref.txt +10 -0
+     double-tag Annonated doubly
+     four four
+     one one
+     signed-tag A signed tag message
+     three three
+     two two
+     EOF
+     git for-each-ref --format="%(refname:short) %(contents:lines=1)" >actual &&
+     test_cmp expect actual
+'
Quoted 3 lines

Nit: I would find it more readable with an actual separator (anything but a space) between %(refname) and %(contents).

Will add.

Signature · 2 lines
Regards,
Karthik Nayak
#14 lore raw

Re: [PATCH v14 04/13] ref-filter: implement an `align` atom

In reply to Karthik Nayak
Quoted 19 lines · Karthik Nayak
Quoted 18 lines · Eric Sunshine
Quoted 16 lines · Karthik Nayak
Quoted 7 lines · Eric Sunshine

ref-filter.c static void populate_value(struct ref_array_item *ref) · line 822

Quoted 2 lines · Karthik Nayak
ref-filter.c +2 -0
+               } else if (!strcmp(name, "align"))
+                       die(_("format: incomplete use of the `align` atom"));

Why does %(align) get flagged as a malformation of %(align:), whereas %(color) does not get flagged as a malformation of %(color:)? Why does one deserve special treatment but not the other?

Didn't see that, I think its needed to add a check for both like :

else if (!strcmp(name, "align") || !strcmp(name, "color"))

            die(_("format: improper usage of %s atom"), name);

I had a look if any other atoms need a subvalue to operate, couldn't find any.

Quoted 8 lines
Quoted 5 lines

Hmm, I'm not convinced that either %(align) or %(color) need to be called out specially. What is the current behavior when these "malformations" or any other misspelled atoms are used? Does it error out? Does it simply ignore them and pass them through to the output unmolested?

It just simply ignores them currently, which is kinda bad, as the user is given no warning, and the atom is ineffective.

Warning about unrecognized atoms may indeed be a good idea, however, the current behavior isn't a huge problem since user discovers the error when the output fails to match his expectation. This behavior of ignoring unrecognized atoms predates your work, doesn't it? If so, it's probably not something you need to address in this series.

#15 lore raw

Re: [PATCH v14 04/13] ref-filter: implement an `align` atom

Quoted 26 lines · Eric Sunshine
Quoted 25 lines · Karthik Nayak
Quoted 23 lines · Eric Sunshine
Quoted 9 lines · Karthik Nayak
Quoted 7 lines · Eric Sunshine

ref-filter.c static void populate_value(struct ref_array_item *ref) · line 822

Quoted 2 lines · Karthik Nayak
ref-filter.c +2 -0
+               } else if (!strcmp(name, "align"))
+                       die(_("format: incomplete use of the `align` atom"));

Why does %(align) get flagged as a malformation of %(align:), whereas %(color) does not get flagged as a malformation of %(color:)? Why does one deserve special treatment but not the other?

Quoted 7 lines

Didn't see that, I think its needed to add a check for both like :

else if (!strcmp(name, "align") || !strcmp(name, "color"))

            die(_("format: improper usage of %s atom"), name);

I had a look if any other atoms need a subvalue to operate, couldn't find any.

Hmm, I'm not convinced that either %(align) or %(color) need to be called out specially. What is the current behavior when these "malformations" or any other misspelled atoms are used? Does it error out? Does it simply ignore them and pass them through to the output unmolested?

Quoted 6 lines
Quoted 2 lines

It just simply ignores them currently, which is kinda bad, as the user is given no warning, and the atom is ineffective.

Warning about unrecognized atoms may indeed be a good idea, however, the current behavior isn't a huge problem since user discovers the error when the output fails to match his expectation.

It's a bit worse than it seems: without this change, using --format '%(align)%(end)' results in Git complaining about %(end) without matching atom, which is really confusing since you do have a %(align) (I got it for real while testing a preliminary version).

Quoted 3 lines

This behavior of ignoring unrecognized atoms predates your work, doesn't it? If so, it's probably not something you need to address in this series.

I wouldn't insist in having it in the series, but now that it's here, I think we can keep it (if only to shorten the interdiff for the next iteration).

Signature · 2 lines
Matthieu Moy
http://www-verimag.imag.fr/~moy/
#16 lore raw

Re: [PATCH v14 04/13] ref-filter: implement an `align` atom

Quoted 39 lines · Matthieu Moy
Quoted 26 lines · Eric Sunshine
Quoted 25 lines · Karthik Nayak
Quoted 23 lines · Eric Sunshine
Quoted 9 lines · Karthik Nayak
Quoted 7 lines · Eric Sunshine

Review

Quoted 2 lines · Karthik Nayak
(quoted diff) +2 -0
+               } else if (!strcmp(name, "align"))
+                       die(_("format: incomplete use of the `align` atom"));

Why does %(align) get flagged as a malformation of %(align:), whereas %(color) does not get flagged as a malformation of %(color:)? Why does one deserve special treatment but not the other?

Quoted 7 lines

Didn't see that, I think its needed to add a check for both like :

else if (!strcmp(name, "align") || !strcmp(name, "color"))

            die(_("format: improper usage of %s atom"), name);

I had a look if any other atoms need a subvalue to operate, couldn't find any.

Hmm, I'm not convinced that either %(align) or %(color) need to be called out specially. What is the current behavior when these "malformations" or any other misspelled atoms are used? Does it error out? Does it simply ignore them and pass them through to the output unmolested?

Quoted 6 lines
Quoted 2 lines

It just simply ignores them currently, which is kinda bad, as the user is given no warning, and the atom is ineffective.

Warning about unrecognized atoms may indeed be a good idea, however, the current behavior isn't a huge problem since user discovers the error when the output fails to match his expectation.

It's a bit worse than it seems: without this change, using --format '%(align)%(end)' results in Git complaining about %(end) without matching atom, which is really confusing since you do have a %(align) (I got it for real while testing a preliminary version).

Quoted 7 lines
Quoted 3 lines

This behavior of ignoring unrecognized atoms predates your work, doesn't it? If so, it's probably not something you need to address in this series.

I wouldn't insist in having it in the series, but now that it's here, I think we can keep it (if only to shorten the interdiff for the next iteration).

The unstated subtext in my original question is that the approach taken by this patch of warning only about unrecognized %(align) is too special-case; if it's going to warn at all, it should do so generically for any unrecognized %(atom). Special-casing the warning about malformed %(align) sets a poor precedent; it's code which will eventually need to be removed anyhow when the generic warning mechanism is eventually implemented.

#17 lore raw

Re: [PATCH v14 04/13] ref-filter: implement an `align` atom

Quoted 21 lines · Eric Sunshine
Quoted 11 lines · Matthieu Moy
Quoted 4 lines · Eric Sunshine

Warning about unrecognized atoms may indeed be a good idea, however, the current behavior isn't a huge problem since user discovers the error when the output fails to match his expectation.

It's a bit worse than it seems: without this change, using --format '%(align)%(end)' results in Git complaining about %(end) without matching atom, which is really confusing since you do have a %(align) (I got it for real while testing a preliminary version).

Quoted 7 lines
Quoted 3 lines

This behavior of ignoring unrecognized atoms predates your work, doesn't it? If so, it's probably not something you need to address in this series.

I wouldn't insist in having it in the series, but now that it's here, I think we can keep it (if only to shorten the interdiff for the next iteration).

Quoted 7 lines

The unstated subtext in my original question is that the approach taken by this patch of warning only about unrecognized %(align) is too special-case; if it's going to warn at all, it should do so generically for any unrecognized %(atom). Special-casing the warning about malformed %(align) sets a poor precedent; it's code which will eventually need to be removed anyhow when the generic warning mechanism is eventually implemented.

Probably, I could just have a check within the align block and maybe build a general case later.

Like this:

 else if (skip_prefix(name, "align", &valp)) {
            struct align *align = &v->u.align;
            struct strbuf **s;

            if (valp[0] != ':')
                die(_("format: usage %%(align:<width>,<position>)"));
            else
                valp++;
            ......
            ........
}
Signature · 2 lines
Regards,
Karthik Nayak
#18 lore raw

Re: [PATCH v14 04/13] ref-filter: implement an `align` atom

Quoted 13 lines · Karthik Nayak

Like this:

 else if (skip_prefix(name, "align", &valp)) {
            struct align *align = &v->u.align;
            struct strbuf **s;

            if (valp[0] != ':')
                die(_("format: usage %%(align:<width>,<position>)"));
            else
                valp++;
            ......
            ........
}

Checking the string's correctness as you read it seems good to me, yes.

But as you say, you should make this a bit more generic, for example by putting your "if/else" in a helper function, that other atoms could use.

Signature · 2 lines
Matthieu Moy
http://www-verimag.imag.fr/~moy/
#19 lore raw

Re: [PATCH v14 04/13] ref-filter: implement an `align` atom

Quoted 16 lines · Matthieu Moy
Quoted 13 lines · Karthik Nayak

Like this:

 else if (skip_prefix(name, "align", &valp)) {
            struct align *align = &v->u.align;
            struct strbuf **s;

            if (valp[0] != ':')
                die(_("format: usage %%(align:<width>,<position>)"));
            else
                valp++;
            ......
            ........
}
Quoted 4 lines

Checking the string's correctness as you read it seems good to me, yes.

But as you say, you should make this a bit more generic, for example by putting your "if/else" in a helper function, that other atoms could use.

Okay then, I'll keep that in mind :)

Signature · 2 lines
Regards,
Karthik Nayak
#49 lore raw
In reply to Karthik Nayak
Quoted 45 lines · Karthik Nayak
Quoted 30 lines · Matthieu Moy
Quoted 1 line · Karthik Nayak

* We perform quoting on each layer of nested alignment.

I do not understand why.

For example, using the tip of karthik/exp on GitHub (on top of this series, d91419b (ref-filter: adopt get_head_description() from branch.c, 2015-08-23)):

git for-each-ref --shell \
--format 'x=%(if)foo%(then)%(align:10)XXX%(end)%(else) not foo %(end)'

I'd expect an output like:

x='XXX '

and instead I get:

x=''\''XXX '\'''

which assigns the value 'XXX ' (including the quotes) to $x. I do
not see a use-case for this (well, I could imagine one where we would later call eval "$x", that seems rather far-fetched).

I think the quoting should be:

1) When the stack contains only the initial element, quote individual atoms.

Quoted 7 lines

2) When the stack contains exactly two elements and encountering a %(end) or %(else), quote the entire strbuf of the 2nd level when appending to the 1st level.

3) When the stack contains more than two elements, perform no quoting at all. The quoting will be done later by #2.

Yea, That's what Eric was saying, I even made changes which sum up to what you're saying :)

Quoted 2 lines

I found a segfault while testing:

Quoted 8 lines
Quoted 3 lines
$ git for-each-ref --format 'x=%(if)%(align:10)%(end)%(then)%(align:10)XXX%(end)%(else)%(end)' --shell
zsh: segmentation fault

I wouldn't worry about this ATM, I have made so many changes that the tip is barely changed to reflect those, though I'll have a look at it :)

Was a dereferencing error, fixed it now :)

Signature · 2 lines
Regards,
Karthik Nayak