{"thread":{"id":"40244","subject":"[PATCH v15 01/13] ref-filter: move `struct atom_value` to ref-filter.c","startedAt":"2015-09-01T18:26:21Z","lastAt":"2015-09-04T18:02:13Z","messageCount":58,"participants":["Karthik Nayak","Junio C Hamano","Matthieu Moy","Eric Sunshine"],"isPatch":true,"patchVersion":15,"patchTotal":13},"messages":[{"id":"269124","messageId":"1441131994-13508-1-git-send-email-Karthik.188@gmail.com","threadId":"40244","inReplyTo":null,"subject":"[PATCH v15 00/13] port builtin/tag.c to use ref-filter APIs.","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-01T18:26:21Z","receivedAt":"2015-09-01T18:26:21Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"The previous iteration of this series is found here:\nhttp://article.gmane.org/gmane.comp.version-control.git/276779\n\nChanges in this version:\n* Make %(contents:lines=X) use existing ref-filter code rather\nthan reply completely on the code borrowed from tag.c.\n* Make struct align and struct contents static variables inside\natom_value and put them inside an union.\n* Since there was some redundant code movement, I moved most of\nthe code to the top. (Caused a noisy interdiff but should be more\nbenifital in the long run).\n* Move the error checking of `align` into the block for processing\nthe align atom.\n* Remove introduction of format_ref_array_item(), probably re-introduce\nit if/when required.\n* More tests added for %(contents:lines=X) and made them cleaner to read.\n* Only the second element of the stack performs quoting at the end, all\nelements after it perform no quoting themselves and rely on the quoting\nperformed by element two.\n\nKarthik Nayak (13):\n  ref-filter: move `struct atom_value` to ref-filter.c\n  ref-filter: introduce ref_formatting_state and ref_formatting_stack\n  utf8: add function to align a string into given strbuf\n  ref-filter: introduce handler function for each atom\n  ref-filter: implement an `align` atom\n  ref-filter: add option to filter out tags, branches and remotes\n  ref-filter: add support for %(contents:lines=X)\n  ref-filter: add support to sort by version\n  ref-filter: add option to match literal pattern\n  tag.c: use 'ref-filter' data structures\n  tag.c: use 'ref-filter' APIs\n  tag.c: implement '--format' option\n  tag.c: implement '--merged' and '--no-merged' options\n\n Documentation/git-for-each-ref.txt |  15 +-\n Documentation/git-tag.txt          |  27 ++-\n builtin/for-each-ref.c             |   1 +\n builtin/tag.c                      | 368 +++++++----------------------------\n ref-filter.c                       | 382 ++++++++++++++++++++++++++++++++-----\n ref-filter.h                       |  25 ++-\n refs.c                             |   9 +\n refs.h                             |   1 +\n t/t6302-for-each-ref-filter.sh     | 173 +++++++++++++++++\n t/t7004-tag.sh                     |  47 ++++-\n utf8.c                             |  21 ++\n utf8.h                             |  15 ++\n 12 files changed, 710 insertions(+), 374 deletions(-)\n\nInterdiff between v14 and v15:\n\ndiff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt\nindex 1b48b95..d039f40 100644\n--- a/Documentation/git-for-each-ref.txt\n+++ b/Documentation/git-for-each-ref.txt\n@@ -133,8 +133,8 @@ align::\n \t`<position>` is either left, right or middle and `<width>` is\n \tthe total length of the content with alignment. If the\n \tcontents length is more than the width then no alignment is\n-\tperformed. If used with '--quote' everything in between %(align:..)\n-\tand %(end) is quoted.\n+\tperformed. If used with '--quote' everything in between\n+\t%(align:..)  and %(end) is quoted.\n \n In addition to the above, for commit and tag objects, the header\n field names (`tree`, `parent`, `object`, `type`, and `tag`) can\n@@ -148,8 +148,8 @@ The complete message in a commit and tag object is `contents`.\n Its first line is `contents:subject`, where subject is the concatenation\n of all lines of the commit message up to the first blank line.  The next\n line is 'contents:body', where body is all of the lines after the first\n-blank line.  Finally, the optional GPG signature is `contents:signature`.\n-The first `N` lines of the object is obtained using `contents:lines=N`.\n+blank line.  The optional GPG signature is `contents:signature`.  The\n+first `N` lines of the message is obtained using `contents:lines=N`.\n \n For sorting purposes, fields with numeric values sort in numeric\n order (`objectsize`, `authordate`, `committerdate`, `taggerdate`).\ndiff --git a/ref-filter.c b/ref-filter.c\nindex f268cd7..b37d57a 100644\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -61,6 +61,8 @@ static struct {\n \t{ \"contents:lines\" },\n };\n \n+#define REF_FORMATTING_STATE_INIT  { 0, NULL }\n+\n struct align {\n \talign_type position;\n \tunsigned int width;\n@@ -71,8 +73,6 @@ struct contents {\n \tstruct object_id oid;\n };\n \n-#define REF_FORMATTING_STATE_INIT  { 0, NULL }\n-\n struct ref_formatting_stack {\n \tstruct ref_formatting_stack *prev;\n \tstruct strbuf output;\n@@ -87,8 +87,10 @@ struct ref_formatting_state {\n \n struct atom_value {\n \tconst char *s;\n-\tstruct align *align;\n-\tstruct contents *contents;\n+\tunion {\n+\t\tstruct align align;\n+\t\tstruct contents contents;\n+\t} u;\n \tvoid (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);\n \tunsigned long ul; /* used for sorting when not FIELD_STR */\n };\n@@ -183,6 +185,30 @@ static void quote_formatting(struct strbuf *s, const char *str, int quote_style)\n \t}\n }\n \n+static void align_handler(struct ref_formatting_stack *stack)\n+{\n+\tstruct align *align = (struct align *)stack->cb_data;\n+\tstruct strbuf s = STRBUF_INIT;\n+\n+\tstrbuf_utf8_align(&s, align->position, align->width, stack->output.buf);\n+\tstrbuf_swap(&stack->output, &s);\n+\tstrbuf_release(&s);\n+}\n+\n+static void append_atom(struct atom_value *v, struct ref_formatting_state *state)\n+{\n+\t/*\n+\t * Quote formatting is only done when the stack has a single\n+\t * element. Otherwise quote formatting is done on the\n+\t * element's entire output strbuf when the %(end) atom is\n+\t * encountered.\n+\t */\n+\tif (!state->stack->prev)\n+\t\tquote_formatting(&state->stack->output, v->s, state->quote_style);\n+\telse\n+\t\tstrbuf_addstr(&state->stack->output, v->s);\n+}\n+\n static void push_stack_element(struct ref_formatting_stack **stack)\n {\n \tstruct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack));\n@@ -204,6 +230,37 @@ static void pop_stack_element(struct ref_formatting_stack **stack)\n \t*stack = prev;\n }\n \n+static void align_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)\n+{\n+\tstruct ref_formatting_stack *new;\n+\n+\tpush_stack_element(&state->stack);\n+\tnew = state->stack;\n+\tnew->at_end = align_handler;\n+\tnew->cb_data = &atomv->u.align;\n+}\n+\n+static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)\n+{\n+\tstruct ref_formatting_stack *current = state->stack;\n+\tstruct strbuf s = STRBUF_INIT;\n+\n+\tif (!current->at_end)\n+\t\tdie(_(\"format: `end` atom used without a supporting atom\"));\n+\tcurrent->at_end(current);\n+\n+\t/*\n+\t * Perform quote formatting when the stack element is that of\n+\t * a modifier atom and right above the first stack element.\n+\t */\n+\tif (!state->stack->prev->prev) {\n+\t\tquote_formatting(&s, current->output.buf, state->quote_style);\n+\t\tstrbuf_swap(&current->output, &s);\n+\t}\n+\tstrbuf_release(&s);\n+\tpop_stack_element(&state->stack);\n+}\n+\n /*\n  * In a format string, find the next occurrence of %(atom).\n  */\n@@ -580,34 +637,20 @@ static void find_subpos(const char *buf, unsigned long sz,\n \n /*\n  * If 'lines' is greater than 0, append that many lines from the given\n- * object_id 'oid' to the given strbuf.\n+ * 'buf' of length 'size' to the given strbuf.\n  */\n-static void append_tag_lines(struct strbuf *out, const struct object_id *oid, int lines)\n+static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)\n {\n \tint i;\n-\tunsigned long size;\n-\tenum object_type type;\n-\tchar *buf, *sp, *eol;\n+\tconst char *sp, *eol;\n \tsize_t len;\n \n-\tbuf = read_sha1_file(oid->hash, &type, &size);\n-\tif (!buf)\n-\t\tdie_errno(\"unable to read object %s\", oid_to_hex(oid));\n-\tif (type != OBJ_COMMIT && type != OBJ_TAG)\n-\t\tgoto free_return;\n-\tif (!size)\n-\t\tdie(\"an empty %s object %s?\",\n-\t\t    typename(type), oid_to_hex(oid));\n-\n-\t/* skip header */\n-\tsp = strstr(buf, \"\\n\\n\");\n-\tif (!sp)\n-\t\tgoto free_return;\n-\n-\t/* only take up to \"lines\" lines, and strip the signature from a tag */\n-\tif (type == OBJ_TAG)\n-\t\tsize = parse_signature(buf, size);\n-\tfor (i = 0, sp += 2; i < lines && sp < buf + size; i++) {\n+\tif ((sp = strstr(buf, \"\\n\\n\")) && (sp <= buf + size))\n+\t\tsize += 2;\n+\n+\tsp = buf;\n+\n+\tfor (i = 0; i < lines && sp < buf + size; i++) {\n \t\tif (i)\n \t\t\tstrbuf_addstr(out, \"\\n    \");\n \t\teol = memchr(sp, '\\n', size - (sp - buf));\n@@ -617,20 +660,6 @@ static void append_tag_lines(struct strbuf *out, const struct object_id *oid, in\n \t\t\tbreak;\n \t\tsp = eol + 1;\n \t}\n-free_return:\n-\tfree(buf);\n-}\n-\n-static void contents_lines_handler(struct atom_value *atomv, struct ref_formatting_state *state)\n-{\n-\tstruct contents *contents = (struct contents *)atomv->contents;\n-\tstruct strbuf s = STRBUF_INIT;\n-\n-\tappend_tag_lines(&s, &contents->oid, contents->lines);\n-\tquote_formatting(&state->stack->output, s.buf, state->quote_style);\n-\tstrbuf_release(&s);\n-\n-\tfree(contents);\n }\n \n /* See grab_values */\n@@ -675,13 +704,11 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj\n \t\telse if (!strcmp(name, \"contents\"))\n \t\t\tv->s = xstrdup(subpos);\n \t\telse if (skip_prefix(name, \"contents:lines=\", &valp)) {\n-\t\t\tstruct contents *contents = xmalloc(sizeof(struct contents));\n-\n-\t\t\tif (strtoul_ui(valp, 10, &contents->lines))\n-\t\t\t\tdie(_(\"positive width expected align:%s\"), valp);\n-\t\t\thashcpy(contents->oid.hash, obj->sha1);\n-\t\t\tv->handler = contents_lines_handler;\n-\t\t\tv->contents = contents;\n+\t\t\tstruct strbuf s = STRBUF_INIT;\n+\t\t\tif (strtoul_ui(valp, 10, &v->u.contents.lines))\n+\t\t\t\tdie(_(\"positive width expected contents:lines=%s\"), valp);\n+\t\t\tappend_lines(&s, subpos, sublen + bodylen - siglen, v->u.contents.lines);\n+\t\t\tv->s = strbuf_detach(&s, NULL);\n \t\t}\n \t}\n }\n@@ -740,63 +767,6 @@ static inline char *copy_advance(char *dst, const char *src)\n \treturn dst;\n }\n \n-static void align_handler(struct ref_formatting_stack *stack)\n-{\n-\tstruct align *align = (struct align *)stack->cb_data;\n-\tstruct strbuf s = STRBUF_INIT;\n-\n-\tstrbuf_utf8_align(&s, align->position, align->width, stack->output.buf);\n-\tstrbuf_swap(&stack->output, &s);\n-\tstrbuf_release(&s);\n-\tfree(align);\n-}\n-\n-static void align_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)\n-{\n-\tstruct ref_formatting_stack *new;\n-\n-\tpush_stack_element(&state->stack);\n-\tnew = state->stack;\n-\tnew->at_end = align_handler;\n-\tnew->cb_data = atomv->align;\n-}\n-\n-static void append_atom(struct atom_value *v, struct ref_formatting_state *state)\n-{\n-\t/*\n-\t * Quote formatting is only done when the stack has a single\n-\t * element. Otherwise quote formatting is done on the\n-\t * element's entire output strbuf when the %(end) atom is\n-\t * encountered.\n-\t */\n-\tif (!state->stack->prev)\n-\t\tquote_formatting(&state->stack->output, v->s, state->quote_style);\n-\telse\n-\t\tstrbuf_addstr(&state->stack->output, v->s);\n-}\n-\n-static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)\n-{\n-\tstruct ref_formatting_stack *current = state->stack;\n-\tstruct strbuf s = STRBUF_INIT;\n-\n-\tif (!current->at_end)\n-\t\tdie(_(\"format: `end` atom used without a supporting atom\"));\n-\tcurrent->at_end(current);\n-\t/*\n-\t * Whenever we have more than one stack element that means we\n-\t * are using a certain modifier atom. In that case we need to\n-\t * perform quote formatting.\n-\t */\n-\tif (state->stack->prev) {\n-\t\tquote_formatting(&s, current->output.buf, state->quote_style);\n-\t\tstrbuf_reset(&current->output);\n-\t\tstrbuf_addbuf(&current->output, &s);\n-\t}\n-\tstrbuf_release(&s);\n-\tpop_stack_element(&state->stack);\n-}\n-\n /*\n  * Parse the object referred by ref, and grab needed value.\n  */\n@@ -893,21 +863,27 @@ static void populate_value(struct ref_array_item *ref)\n \t\t\telse\n \t\t\t\tv->s = \" \";\n \t\t\tcontinue;\n-\t\t} else if (!strcmp(name, \"align\"))\n-\t\t\tdie(_(\"format: incomplete use of the `align` atom\"));\n-\t\telse if (skip_prefix(name, \"align:\", &valp)) {\n-\t\t\tstruct align *align = xmalloc(sizeof(struct align));\n-\t\t\tstruct strbuf **s = strbuf_split_str(valp, ',', 0);\n+\t\t} else if (skip_prefix(name, \"align\", &valp)) {\n+\t\t\tstruct align *align = &v->u.align;\n+\t\t\tstruct strbuf **s;\n+\n+\t\t\tif (valp[0] != ':')\n+\t\t\t\tdie(_(\"format: usage %%(align:<width>,<position>)\"));\n+\t\t\telse\n+\t\t\t\tvalp++;\n+\n+\t\t\ts = strbuf_split_str(valp, ',', 0);\n \n \t\t\t/* If the position is given trim the ',' from the first strbuf */\n \t\t\tif (s[1])\n-\t\t\t\tstrbuf_remove(s[0], s[0]->len - 1, 1);\n+\t\t\t\tstrbuf_setlen(s[0], s[0]->len - 1);\n \n \t\t\tif (strtoul_ui(s[0]->buf, 10, &align->width))\n \t\t\t\tdie(_(\"positive width expected align:%s\"), s[0]->buf);\n \n-\t\t\t/* If no position is given, default to ALIGN_LEFT */\n-\t\t\tif (!s[1] || !strcmp(s[1]->buf, \"left\"))\n+\t\t\tif (!s[1])\n+\t\t\t\talign->position = ALIGN_LEFT;\n+\t\t\telse if (!strcmp(s[1]->buf, \"left\"))\n \t\t\t\talign->position = ALIGN_LEFT;\n \t\t\telse if (!strcmp(s[1]->buf, \"right\"))\n \t\t\t\talign->position = ALIGN_RIGHT;\n@@ -918,7 +894,6 @@ static void populate_value(struct ref_array_item *ref)\n \n \t\t\tstrbuf_list_free(s);\n \n-\t\t\tv->align = align;\n \t\t\tv->handler = align_atom_handler;\n \t\t\tcontinue;\n \t\t} else if (!strcmp(name, \"end\")) {\n@@ -1554,10 +1529,10 @@ static void append_literal(const char *cp, const char *ep, struct ref_formatting\n \t}\n }\n \n-void format_ref_array_item(struct strbuf *out, struct ref_array_item *info,\n-\t\t\t   const char *format, int quote_style)\n+void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)\n {\n \tconst char *cp, *sp, *ep;\n+\tstruct strbuf *final_buf;\n \tstruct ref_formatting_state state = REF_FORMATTING_STATE_INIT;\n \n \tstate.quote_style = quote_style;\n@@ -1587,17 +1562,10 @@ void format_ref_array_item(struct strbuf *out, struct ref_array_item *info,\n \t}\n \tif (state.stack->prev)\n \t\tdie(_(\"format: `end` atom missing\"));\n-\tstrbuf_addbuf(out, &state.stack->output);\n+\tfinal_buf = &state.stack->output;\n+\tfwrite(final_buf->buf, 1, final_buf->len, stdout);\n \tpop_stack_element(&state.stack);\n-}\n-\n-void show_ref_array_item(struct ref_array_item *item, const char *format, unsigned int quote_style)\n-{\n-\tstruct strbuf out = STRBUF_INIT;\n-\tformat_ref_array_item(&out, item, format, quote_style);\n-\tfwrite(out.buf, out.len, 1, stdout);\n-\tprintf(\"\\n\");\n-\tstrbuf_release(&out);\n+\tputchar('\\n');\n }\n \n /*  If no sorting option is given, use refname to sort as default */\ndiff --git a/ref-filter.h b/ref-filter.h\nindex 179944c..a5cfa5e 100644\n--- a/ref-filter.h\n+++ b/ref-filter.h\n@@ -94,11 +94,8 @@ int parse_ref_filter_atom(const char *atom, const char *ep);\n int verify_ref_format(const char *format);\n /*  Sort the given ref_array as per the ref_sorting provided */\n void ref_array_sort(struct ref_sorting *sort, struct ref_array *array);\n-/*  Format the ref as per given format and quote_style and store it into the strbuf */\n-void format_ref_array_item(struct strbuf *out, struct ref_array_item *info,\n-\t\t\t   const char *format, int quote_style);\n-/*  Wrapper around format_ref_array_item() which prints the given ref_array_item */\n-void show_ref_array_item(struct ref_array_item *item, const char *format, unsigned int quote_style);\n+/*  Print the ref using the given format and quote_style */\n+void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style);\n /*  Callback function for parsing the sort option */\n int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset);\n /*  Default sort option based on refname */\ndiff --git a/t/t6302-for-each-ref-filter.sh b/t/t6302-for-each-ref-filter.sh\nindex 8f18f86..d95b781 100755\n--- a/t/t6302-for-each-ref-filter.sh\n+++ b/t/t6302-for-each-ref-filter.sh\n@@ -166,22 +166,58 @@ test_expect_success 'nested alignment' '\n \ttest_cmp expect actual\n '\n \n-test_expect_success 'check `%(contents:lines=X)`' '\n+test_expect_success 'check `%(contents:lines=1)`' '\n \tcat >expect <<-\\EOF &&\n-\tmaster three\n-\tside four\n-\todd/spot three\n-\tdouble-tag Annonated doubly\n-\tfour four\n-\tone one\n-\tsigned-tag A signed tag message\n-\tthree three\n-\ttwo two\n-\tEOF\n-\tgit for-each-ref --format=\"%(refname:short) %(contents:lines=1)\" >actual &&\n+\tmaster |three\n+\tside |four\n+\todd/spot |three\n+\tdouble-tag |Annonated doubly\n+\tfour |four\n+\tone |one\n+\tsigned-tag |A signed tag message\n+\tthree |three\n+\ttwo |two\n+\tEOF\n+\tgit for-each-ref --format=\"%(refname:short) |%(contents:lines=1)\" >actual &&\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'check `%(contents:lines=0)`' '\n+\tcat >expect <<-\\EOF &&\n+\tmaster |\n+\tside |\n+\todd/spot |\n+\tdouble-tag |\n+\tfour |\n+\tone |\n+\tsigned-tag |\n+\tthree |\n+\ttwo |\n+\tEOF\n+\tgit for-each-ref --format=\"%(refname:short) |%(contents:lines=0)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'check `%(contents:lines=99999)`' '\n+\tcat >expect <<-\\EOF &&\n+\tmaster |three\n+\tside |four\n+\todd/spot |three\n+\tdouble-tag |Annonated doubly\n+\tfour |four\n+\tone |one\n+\tsigned-tag |A signed tag message\n+\tthree |three\n+\ttwo |two\n+\tEOF\n+\tgit for-each-ref --format=\"%(refname:short) |%(contents:lines=99999)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success '`%(contents:lines=-1)` should fail' '\n+\ttest_must_fail git for-each-ref --format=\"%(refname:short) |%(contents:lines=-1)\"\n+'\n+\n test_expect_success 'setup for version sort' '\n \ttest_commit foo1.3 &&\n \ttest_commit foo1.6 &&\n\n-- \n2.5.0\n"},{"id":"269123","messageId":"1441131994-13508-2-git-send-email-Karthik.188@gmail.com","threadId":"40244","inReplyTo":"1441131994-13508-1-git-send-email-Karthik.188@gmail.com","subject":"[PATCH v15 01/13] ref-filter: move `struct atom_value` to ref-filter.c","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-01T18:26:22Z","receivedAt":"2015-09-01T18:26:22Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Since atom_value is only required for the internal working of\nref-filter it doesn't belong in the public header.\n\nHelped-by: Eric Sunshine <sunshine@sunshineco.com>\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n ref-filter.c | 5 +++++\n ref-filter.h | 5 +----\n 2 files changed, 6 insertions(+), 4 deletions(-)\n\ndiff --git a/ref-filter.c b/ref-filter.c\nindex 46963a5..e53c77e 100644\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -55,6 +55,11 @@ static struct {\n \t{ \"color\" },\n };\n \n+struct atom_value {\n+\tconst char *s;\n+\tunsigned long ul; /* used for sorting when not FIELD_STR */\n+};\n+\n /*\n  * An atom is a valid field atom listed above, possibly prefixed with\n  * a \"*\" to denote deref_tag().\ndiff --git a/ref-filter.h b/ref-filter.h\nindex 6bf27d8..45026d0 100644\n--- a/ref-filter.h\n+++ b/ref-filter.h\n@@ -16,10 +16,7 @@\n #define FILTER_REFS_INCLUDE_BROKEN 0x1\n #define FILTER_REFS_ALL 0x2\n \n-struct atom_value {\n-\tconst char *s;\n-\tunsigned long ul; /* used for sorting when not FIELD_STR */\n-};\n+struct atom_value;\n \n struct ref_sorting {\n \tstruct ref_sorting *next;\n-- \n2.5.0\n"},{"id":"269125","messageId":"1441131994-13508-3-git-send-email-Karthik.188@gmail.com","threadId":"40244","inReplyTo":"1441131994-13508-1-git-send-email-Karthik.188@gmail.com","subject":"[PATCH v15 02/13] ref-filter: introduce ref_formatting_state and ref_formatting_stack","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-01T18:26:23Z","receivedAt":"2015-09-01T18:26:23Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Introduce ref_formatting_state which will hold the formatted output\nstrbuf instead of directly printing to stdout. This will help us in\ncreating modifier atoms which modify the format specified before\nprinting to stdout.\n\nImplement a stack machinery for ref_formatting_state, this allows us\nto push and pop elements onto the stack. Whenever we pop an element\nfrom the stack, the strbuf from that element is appended to the strbuf\nof the next element on the stack, this will allow us to support\nnesting of modifier atoms.\n\nRename some functions to reflect the changes made:\nprint_value() -> append_atom()\nemit()        -> append_literal()\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n ref-filter.c | 78 +++++++++++++++++++++++++++++++++++++++++++++---------------\n 1 file changed, 59 insertions(+), 19 deletions(-)\n\ndiff --git a/ref-filter.c b/ref-filter.c\nindex e53c77e..432cea0 100644\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -55,6 +55,18 @@ static struct {\n \t{ \"color\" },\n };\n \n+#define REF_FORMATTING_STATE_INIT  { 0, NULL }\n+\n+struct ref_formatting_stack {\n+\tstruct ref_formatting_stack *prev;\n+\tstruct strbuf output;\n+};\n+\n+struct ref_formatting_state {\n+\tint quote_style;\n+\tstruct ref_formatting_stack *stack;\n+};\n+\n struct atom_value {\n \tconst char *s;\n \tunsigned long ul; /* used for sorting when not FIELD_STR */\n@@ -129,6 +141,27 @@ int parse_ref_filter_atom(const char *atom, const char *ep)\n \treturn at;\n }\n \n+static void push_stack_element(struct ref_formatting_stack **stack)\n+{\n+\tstruct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack));\n+\n+\tstrbuf_init(&s->output, 0);\n+\ts->prev = *stack;\n+\t*stack = s;\n+}\n+\n+static void pop_stack_element(struct ref_formatting_stack **stack)\n+{\n+\tstruct ref_formatting_stack *current = *stack;\n+\tstruct ref_formatting_stack *prev = current->prev;\n+\n+\tif (prev)\n+\t\tstrbuf_addbuf(&prev->output, &current->output);\n+\tstrbuf_release(&current->output);\n+\tfree(current);\n+\t*stack = prev;\n+}\n+\n /*\n  * In a format string, find the next occurrence of %(atom).\n  */\n@@ -1195,30 +1228,27 @@ void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)\n \tqsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs);\n }\n \n-static void print_value(struct atom_value *v, int quote_style)\n+static void append_atom(struct atom_value *v, struct ref_formatting_state *state)\n {\n-\tstruct strbuf sb = STRBUF_INIT;\n-\tswitch (quote_style) {\n+\tstruct strbuf *s = &state->stack->output;\n+\n+\tswitch (state->quote_style) {\n \tcase QUOTE_NONE:\n-\t\tfputs(v->s, stdout);\n+\t\tstrbuf_addstr(s, v->s);\n \t\tbreak;\n \tcase QUOTE_SHELL:\n-\t\tsq_quote_buf(&sb, v->s);\n+\t\tsq_quote_buf(s, v->s);\n \t\tbreak;\n \tcase QUOTE_PERL:\n-\t\tperl_quote_buf(&sb, v->s);\n+\t\tperl_quote_buf(s, v->s);\n \t\tbreak;\n \tcase QUOTE_PYTHON:\n-\t\tpython_quote_buf(&sb, v->s);\n+\t\tpython_quote_buf(s, v->s);\n \t\tbreak;\n \tcase QUOTE_TCL:\n-\t\ttcl_quote_buf(&sb, v->s);\n+\t\ttcl_quote_buf(s, v->s);\n \t\tbreak;\n \t}\n-\tif (quote_style != QUOTE_NONE) {\n-\t\tfputs(sb.buf, stdout);\n-\t\tstrbuf_release(&sb);\n-\t}\n }\n \n static int hex1(char ch)\n@@ -1239,8 +1269,10 @@ static int hex2(const char *cp)\n \t\treturn -1;\n }\n \n-static void emit(const char *cp, const char *ep)\n+static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state)\n {\n+\tstruct strbuf *s = &state->stack->output;\n+\n \twhile (*cp && (!ep || cp < ep)) {\n \t\tif (*cp == '%') {\n \t\t\tif (cp[1] == '%')\n@@ -1248,13 +1280,13 @@ static void emit(const char *cp, const char *ep)\n \t\t\telse {\n \t\t\t\tint ch = hex2(cp + 1);\n \t\t\t\tif (0 <= ch) {\n-\t\t\t\t\tputchar(ch);\n+\t\t\t\t\tstrbuf_addch(s, ch);\n \t\t\t\t\tcp += 3;\n \t\t\t\t\tcontinue;\n \t\t\t\t}\n \t\t\t}\n \t\t}\n-\t\tputchar(*cp);\n+\t\tstrbuf_addch(s, *cp);\n \t\tcp++;\n \t}\n }\n@@ -1262,19 +1294,24 @@ static void emit(const char *cp, const char *ep)\n void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)\n {\n \tconst char *cp, *sp, *ep;\n+\tstruct strbuf *final_buf;\n+\tstruct ref_formatting_state state = REF_FORMATTING_STATE_INIT;\n+\n+\tstate.quote_style = quote_style;\n+\tpush_stack_element(&state.stack);\n \n \tfor (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {\n \t\tstruct atom_value *atomv;\n \n \t\tep = strchr(sp, ')');\n \t\tif (cp < sp)\n-\t\t\temit(cp, sp);\n+\t\t\tappend_literal(cp, sp, &state);\n \t\tget_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);\n-\t\tprint_value(atomv, quote_style);\n+\t\tappend_atom(atomv, &state);\n \t}\n \tif (*cp) {\n \t\tsp = cp + strlen(cp);\n-\t\temit(cp, sp);\n+\t\tappend_literal(cp, sp, &state);\n \t}\n \tif (need_color_reset_at_eol) {\n \t\tstruct atom_value resetv;\n@@ -1283,8 +1320,11 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu\n \t\tif (color_parse(\"reset\", color) < 0)\n \t\t\tdie(\"BUG: couldn't parse 'reset' as a color\");\n \t\tresetv.s = color;\n-\t\tprint_value(&resetv, quote_style);\n+\t\tappend_atom(&resetv, &state);\n \t}\n+\tfinal_buf = &state.stack->output;\n+\tfwrite(final_buf->buf, 1, final_buf->len, stdout);\n+\tpop_stack_element(&state.stack);\n \tputchar('\\n');\n }\n \n-- \n2.5.0\n"},{"id":"269127","messageId":"1441131994-13508-4-git-send-email-Karthik.188@gmail.com","threadId":"40244","inReplyTo":"1441131994-13508-1-git-send-email-Karthik.188@gmail.com","subject":"[PATCH v15 03/13] utf8: add function to align a string into given strbuf","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-01T18:26:24Z","receivedAt":"2015-09-01T18:26:24Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Add strbuf_utf8_align() which will align a given string into a strbuf\nas per given align_type and width. If the width is greater than the\nstring length then no alignment is performed.\n\nHelped-by: Eric Sunshine <sunshine@sunshineco.com>\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n utf8.c | 21 +++++++++++++++++++++\n utf8.h | 15 +++++++++++++++\n 2 files changed, 36 insertions(+)\n\ndiff --git a/utf8.c b/utf8.c\nindex 28e6d76..00e10c8 100644\n--- a/utf8.c\n+++ b/utf8.c\n@@ -644,3 +644,24 @@ int skip_utf8_bom(char **text, size_t len)\n \t*text += strlen(utf8_bom);\n \treturn 1;\n }\n+\n+void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,\n+\t\t       const char *s)\n+{\n+\tint slen = strlen(s);\n+\tint display_len = utf8_strnwidth(s, slen, 0);\n+\tint utf8_compensation = slen - display_len;\n+\n+\tif (display_len >= width) {\n+\t\tstrbuf_addstr(buf, s);\n+\t\treturn;\n+\t}\n+\n+\tif (position == ALIGN_LEFT)\n+\t\tstrbuf_addf(buf, \"%-*s\", width + utf8_compensation, s);\n+\telse if (position == ALIGN_MIDDLE) {\n+\t\tint left = (width - display_len) / 2;\n+\t\tstrbuf_addf(buf, \"%*s%-*s\", left, \"\", width - left + utf8_compensation, s);\n+\t} else if (position == ALIGN_RIGHT)\n+\t\tstrbuf_addf(buf, \"%*s\", width + utf8_compensation, s);\n+}\ndiff --git a/utf8.h b/utf8.h\nindex 5a9e94b..7930b44 100644\n--- a/utf8.h\n+++ b/utf8.h\n@@ -55,4 +55,19 @@ int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding);\n  */\n int is_hfs_dotgit(const char *path);\n \n+typedef enum {\n+\tALIGN_LEFT,\n+\tALIGN_MIDDLE,\n+\tALIGN_RIGHT\n+} align_type;\n+\n+/*\n+ * Align the string given and store it into a strbuf as per the\n+ * 'position' and 'width'. If the given string length is larger than\n+ * 'width' than then the input string is not truncated and no\n+ * alignment is done.\n+ */\n+void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,\n+\t\t       const char *s);\n+\n #endif\n-- \n2.5.0\n"},{"id":"269126","messageId":"1441131994-13508-5-git-send-email-Karthik.188@gmail.com","threadId":"40244","inReplyTo":"1441131994-13508-1-git-send-email-Karthik.188@gmail.com","subject":"[PATCH v15 04/13] ref-filter: introduce handler function for each atom","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-01T18:26:25Z","receivedAt":"2015-09-01T18:26:25Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Introduce a handler function for each atom, which is called when the\natom is processed in show_ref_array_item().\n\nIn this context make append_atom() as the default handler function and\nextract quote_formatting() out of append_atom(). Bump this to the top.\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n ref-filter.c | 54 ++++++++++++++++++++++++++++++------------------------\n 1 file changed, 30 insertions(+), 24 deletions(-)\n\ndiff --git a/ref-filter.c b/ref-filter.c\nindex 432cea0..a993216 100644\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -69,6 +69,7 @@ struct ref_formatting_state {\n \n struct atom_value {\n \tconst char *s;\n+\tvoid (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);\n \tunsigned long ul; /* used for sorting when not FIELD_STR */\n };\n \n@@ -141,6 +142,32 @@ int parse_ref_filter_atom(const char *atom, const char *ep)\n \treturn at;\n }\n \n+static void quote_formatting(struct strbuf *s, const char *str, int quote_style)\n+{\n+\tswitch (quote_style) {\n+\tcase QUOTE_NONE:\n+\t\tstrbuf_addstr(s, str);\n+\t\tbreak;\n+\tcase QUOTE_SHELL:\n+\t\tsq_quote_buf(s, str);\n+\t\tbreak;\n+\tcase QUOTE_PERL:\n+\t\tperl_quote_buf(s, str);\n+\t\tbreak;\n+\tcase QUOTE_PYTHON:\n+\t\tpython_quote_buf(s, str);\n+\t\tbreak;\n+\tcase QUOTE_TCL:\n+\t\ttcl_quote_buf(s, str);\n+\t\tbreak;\n+\t}\n+}\n+\n+static void append_atom(struct atom_value *v, struct ref_formatting_state *state)\n+{\n+\tquote_formatting(&state->stack->output, v->s, state->quote_style);\n+}\n+\n static void push_stack_element(struct ref_formatting_stack **stack)\n {\n \tstruct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack));\n@@ -662,6 +689,8 @@ static void populate_value(struct ref_array_item *ref)\n \t\tconst char *formatp;\n \t\tstruct branch *branch = NULL;\n \n+\t\tv->handler = append_atom;\n+\n \t\tif (*name == '*') {\n \t\t\tderef = 1;\n \t\t\tname++;\n@@ -1228,29 +1257,6 @@ void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)\n \tqsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs);\n }\n \n-static void append_atom(struct atom_value *v, struct ref_formatting_state *state)\n-{\n-\tstruct strbuf *s = &state->stack->output;\n-\n-\tswitch (state->quote_style) {\n-\tcase QUOTE_NONE:\n-\t\tstrbuf_addstr(s, v->s);\n-\t\tbreak;\n-\tcase QUOTE_SHELL:\n-\t\tsq_quote_buf(s, v->s);\n-\t\tbreak;\n-\tcase QUOTE_PERL:\n-\t\tperl_quote_buf(s, v->s);\n-\t\tbreak;\n-\tcase QUOTE_PYTHON:\n-\t\tpython_quote_buf(s, v->s);\n-\t\tbreak;\n-\tcase QUOTE_TCL:\n-\t\ttcl_quote_buf(s, v->s);\n-\t\tbreak;\n-\t}\n-}\n-\n static int hex1(char ch)\n {\n \tif ('0' <= ch && ch <= '9')\n@@ -1307,7 +1313,7 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu\n \t\tif (cp < sp)\n \t\t\tappend_literal(cp, sp, &state);\n \t\tget_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);\n-\t\tappend_atom(atomv, &state);\n+\t\tatomv->handler(atomv, &state);\n \t}\n \tif (*cp) {\n \t\tsp = cp + strlen(cp);\n-- \n2.5.0\n"},{"id":"269128","messageId":"1441131994-13508-6-git-send-email-Karthik.188@gmail.com","threadId":"40244","inReplyTo":"1441131994-13508-1-git-send-email-Karthik.188@gmail.com","subject":"[PATCH v15 05/13] ref-filter: implement an `align` atom","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-01T18:26:26Z","receivedAt":"2015-09-01T18:26:26Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Implement an `align` atom which left-, middle-, or right-aligns the\ncontent between %(align:..) and %(end).\n\nIt is followed by `:<width>,<position>`, where the `<position>` is\neither left, right or middle and `<width>` is the size of the area\ninto which the content will be placed. If the content between\n%(align:) and %(end) is more than the width then no alignment is\nperformed. e.g. to align a refname atom to the middle with a total\nwidth of 40 we can do: --format=\"%(align:middle,40)%(refname)%(end)\".\n\nWe have an `at_end` function for each element of the stack which is to\nbe called when the `end` atom is encountered. Using this we implement\nthe aling_handler() for the `align` atom, this aligns the final strbuf\nby calling `strbuf_utf8_align()` from utf8.c.\n\nEnsure that quote formatting is performed on the whole of\n%(align)...%(end) rather than individual atoms inside.  We skip quote\nformatting for individual atoms when the current stack element is\nhandling an %(align) atom and perform quote formatting at the end when\nwe encounter the %(end) atom.\n\nAdd documentation and tests for the same.\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n Documentation/git-for-each-ref.txt |   9 ++++\n ref-filter.c                       | 102 ++++++++++++++++++++++++++++++++++++-\n t/t6302-for-each-ref-filter.sh     |  85 +++++++++++++++++++++++++++++++\n 3 files changed, 195 insertions(+), 1 deletion(-)\n\ndiff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt\nindex e49d578..cac3128 100644\n--- a/Documentation/git-for-each-ref.txt\n+++ b/Documentation/git-for-each-ref.txt\n@@ -127,6 +127,15 @@ color::\n \tChange output color.  Followed by `:<colorname>`, where names\n \tare described in `color.branch.*`.\n \n+align::\n+\tLeft-, middle-, or right-align the content between %(align:..)\n+\tand %(end). Followed by `:<width>,<position>`, where the\n+\t`<position>` is either left, right or middle and `<width>` is\n+\tthe total length of the content with alignment. If the\n+\tcontents length is more than the width then no alignment is\n+\tperformed. If used with '--quote' everything in between\n+\t%(align:..)  and %(end) is quoted.\n+\n In addition to the above, for commit and tag objects, the header\n field names (`tree`, `parent`, `object`, `type`, and `tag`) can\n be used to specify the value in the header field.\ndiff --git a/ref-filter.c b/ref-filter.c\nindex a993216..3a5f0a7 100644\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -10,6 +10,7 @@\n #include \"quote.h\"\n #include \"ref-filter.h\"\n #include \"revision.h\"\n+#include \"utf8.h\"\n \n typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;\n \n@@ -53,13 +54,22 @@ static struct {\n \t{ \"flag\" },\n \t{ \"HEAD\" },\n \t{ \"color\" },\n+\t{ \"align\" },\n+\t{ \"end\" },\n };\n \n #define REF_FORMATTING_STATE_INIT  { 0, NULL }\n \n+struct align {\n+\talign_type position;\n+\tunsigned int width;\n+};\n+\n struct ref_formatting_stack {\n \tstruct ref_formatting_stack *prev;\n \tstruct strbuf output;\n+\tvoid (*at_end)(struct ref_formatting_stack *stack);\n+\tvoid *cb_data;\n };\n \n struct ref_formatting_state {\n@@ -69,6 +79,7 @@ struct ref_formatting_state {\n \n struct atom_value {\n \tconst char *s;\n+\tstruct align align;\n \tvoid (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);\n \tunsigned long ul; /* used for sorting when not FIELD_STR */\n };\n@@ -163,9 +174,28 @@ static void quote_formatting(struct strbuf *s, const char *str, int quote_style)\n \t}\n }\n \n+static void align_handler(struct ref_formatting_stack *stack)\n+{\n+\tstruct align *align = (struct align *)stack->cb_data;\n+\tstruct strbuf s = STRBUF_INIT;\n+\n+\tstrbuf_utf8_align(&s, align->position, align->width, stack->output.buf);\n+\tstrbuf_swap(&stack->output, &s);\n+\tstrbuf_release(&s);\n+}\n+\n static void append_atom(struct atom_value *v, struct ref_formatting_state *state)\n {\n-\tquote_formatting(&state->stack->output, v->s, state->quote_style);\n+\t/*\n+\t * Quote formatting is only done when the stack has a single\n+\t * element. Otherwise quote formatting is done on the\n+\t * element's entire output strbuf when the %(end) atom is\n+\t * encountered.\n+\t */\n+\tif (!state->stack->prev)\n+\t\tquote_formatting(&state->stack->output, v->s, state->quote_style);\n+\telse\n+\t\tstrbuf_addstr(&state->stack->output, v->s);\n }\n \n static void push_stack_element(struct ref_formatting_stack **stack)\n@@ -189,6 +219,37 @@ static void pop_stack_element(struct ref_formatting_stack **stack)\n \t*stack = prev;\n }\n \n+static void align_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)\n+{\n+\tstruct ref_formatting_stack *new;\n+\n+\tpush_stack_element(&state->stack);\n+\tnew = state->stack;\n+\tnew->at_end = align_handler;\n+\tnew->cb_data = &atomv->align;\n+}\n+\n+static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)\n+{\n+\tstruct ref_formatting_stack *current = state->stack;\n+\tstruct strbuf s = STRBUF_INIT;\n+\n+\tif (!current->at_end)\n+\t\tdie(_(\"format: `end` atom used without a supporting atom\"));\n+\tcurrent->at_end(current);\n+\n+\t/*\n+\t * Perform quote formatting when the stack element is that of\n+\t * a modifier atom and right above the first stack element.\n+\t */\n+\tif (!state->stack->prev->prev) {\n+\t\tquote_formatting(&s, current->output.buf, state->quote_style);\n+\t\tstrbuf_swap(&current->output, &s);\n+\t}\n+\tstrbuf_release(&s);\n+\tpop_stack_element(&state->stack);\n+}\n+\n /*\n  * In a format string, find the next occurrence of %(atom).\n  */\n@@ -687,6 +748,7 @@ static void populate_value(struct ref_array_item *ref)\n \t\tint deref = 0;\n \t\tconst char *refname;\n \t\tconst char *formatp;\n+\t\tconst char *valp;\n \t\tstruct branch *branch = NULL;\n \n \t\tv->handler = append_atom;\n@@ -754,6 +816,42 @@ static void populate_value(struct ref_array_item *ref)\n \t\t\telse\n \t\t\t\tv->s = \" \";\n \t\t\tcontinue;\n+\t\t} else if (skip_prefix(name, \"align\", &valp)) {\n+\t\t\tstruct align *align = &v->align;\n+\t\t\tstruct strbuf **s;\n+\n+\t\t\tif (valp[0] != ':')\n+\t\t\t\tdie(_(\"format: usage %%(align:<width>,<position>)\"));\n+\t\t\telse\n+\t\t\t\tvalp++;\n+\n+\t\t\ts = strbuf_split_str(valp, ',', 0);\n+\n+\t\t\t/* If the position is given trim the ',' from the first strbuf */\n+\t\t\tif (s[1])\n+\t\t\t\tstrbuf_setlen(s[0], s[0]->len - 1);\n+\n+\t\t\tif (strtoul_ui(s[0]->buf, 10, &align->width))\n+\t\t\t\tdie(_(\"positive width expected align:%s\"), s[0]->buf);\n+\n+\t\t\tif (!s[1])\n+\t\t\t\talign->position = ALIGN_LEFT;\n+\t\t\telse if (!strcmp(s[1]->buf, \"left\"))\n+\t\t\t\talign->position = ALIGN_LEFT;\n+\t\t\telse if (!strcmp(s[1]->buf, \"right\"))\n+\t\t\t\talign->position = ALIGN_RIGHT;\n+\t\t\telse if (!strcmp(s[1]->buf, \"middle\"))\n+\t\t\t\talign->position = ALIGN_MIDDLE;\n+\t\t\telse\n+\t\t\t\tdie(_(\"improper format entered align:%s\"), s[1]->buf);\n+\n+\t\t\tstrbuf_list_free(s);\n+\n+\t\t\tv->handler = align_atom_handler;\n+\t\t\tcontinue;\n+\t\t} else if (!strcmp(name, \"end\")) {\n+\t\t\tv->handler = end_atom_handler;\n+\t\t\tcontinue;\n \t\t} else\n \t\t\tcontinue;\n \n@@ -1328,6 +1426,8 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu\n \t\tresetv.s = color;\n \t\tappend_atom(&resetv, &state);\n \t}\n+\tif (state.stack->prev)\n+\t\tdie(_(\"format: `end` atom missing\"));\n \tfinal_buf = &state.stack->output;\n \tfwrite(final_buf->buf, 1, final_buf->len, stdout);\n \tpop_stack_element(&state.stack);\ndiff --git a/t/t6302-for-each-ref-filter.sh b/t/t6302-for-each-ref-filter.sh\nindex 505a360..cef7a41 100755\n--- a/t/t6302-for-each-ref-filter.sh\n+++ b/t/t6302-for-each-ref-filter.sh\n@@ -81,4 +81,89 @@ test_expect_success 'filtering with --contains' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'left alignment' '\n+\tcat >expect <<-\\EOF &&\n+\trefname is refs/heads/master  |refs/heads/master\n+\trefname is refs/heads/side    |refs/heads/side\n+\trefname is refs/odd/spot      |refs/odd/spot\n+\trefname is refs/tags/double-tag|refs/tags/double-tag\n+\trefname is refs/tags/four     |refs/tags/four\n+\trefname is refs/tags/one      |refs/tags/one\n+\trefname is refs/tags/signed-tag|refs/tags/signed-tag\n+\trefname is refs/tags/three    |refs/tags/three\n+\trefname is refs/tags/two      |refs/tags/two\n+\tEOF\n+\tgit for-each-ref --format=\"%(align:30,left)refname is %(refname)%(end)|%(refname)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'middle alignment' '\n+\tcat >expect <<-\\EOF &&\n+\t| refname is refs/heads/master |refs/heads/master\n+\t|  refname is refs/heads/side  |refs/heads/side\n+\t|   refname is refs/odd/spot   |refs/odd/spot\n+\t|refname is refs/tags/double-tag|refs/tags/double-tag\n+\t|  refname is refs/tags/four   |refs/tags/four\n+\t|   refname is refs/tags/one   |refs/tags/one\n+\t|refname is refs/tags/signed-tag|refs/tags/signed-tag\n+\t|  refname is refs/tags/three  |refs/tags/three\n+\t|   refname is refs/tags/two   |refs/tags/two\n+\tEOF\n+\tgit for-each-ref --format=\"|%(align:30,middle)refname is %(refname)%(end)|%(refname)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'right alignment' '\n+\tcat >expect <<-\\EOF &&\n+\t|  refname is refs/heads/master|refs/heads/master\n+\t|    refname is refs/heads/side|refs/heads/side\n+\t|      refname is refs/odd/spot|refs/odd/spot\n+\t|refname is refs/tags/double-tag|refs/tags/double-tag\n+\t|     refname is refs/tags/four|refs/tags/four\n+\t|      refname is refs/tags/one|refs/tags/one\n+\t|refname is refs/tags/signed-tag|refs/tags/signed-tag\n+\t|    refname is refs/tags/three|refs/tags/three\n+\t|      refname is refs/tags/two|refs/tags/two\n+\tEOF\n+\tgit for-each-ref --format=\"|%(align:30,right)refname is %(refname)%(end)|%(refname)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+# Everything in between the %(align)...%(end) atom must be quoted, hence we test this by\n+# introducing single quote's in %(align)...%(end), which must not be escaped.\n+\n+sq=\"'\"\n+\n+test_expect_success 'alignment with format quote' '\n+\tcat >expect <<-EOF &&\n+\trefname is ${sq}           ${sq}\\\\${sq}${sq}master${sq}\\\\${sq}${sq}           ${sq}|\n+\trefname is ${sq}            ${sq}\\\\${sq}${sq}side${sq}\\\\${sq}${sq}            ${sq}|\n+\trefname is ${sq}          ${sq}\\\\${sq}${sq}odd/spot${sq}\\\\${sq}${sq}          ${sq}|\n+\trefname is ${sq}         ${sq}\\\\${sq}${sq}double-tag${sq}\\\\${sq}${sq}         ${sq}|\n+\trefname is ${sq}            ${sq}\\\\${sq}${sq}four${sq}\\\\${sq}${sq}            ${sq}|\n+\trefname is ${sq}            ${sq}\\\\${sq}${sq}one${sq}\\\\${sq}${sq}             ${sq}|\n+\trefname is ${sq}         ${sq}\\\\${sq}${sq}signed-tag${sq}\\\\${sq}${sq}         ${sq}|\n+\trefname is ${sq}           ${sq}\\\\${sq}${sq}three${sq}\\\\${sq}${sq}            ${sq}|\n+\trefname is ${sq}            ${sq}\\\\${sq}${sq}two${sq}\\\\${sq}${sq}             ${sq}|\n+\tEOF\n+\tgit for-each-ref --shell --format=\"refname is %(align:30,middle)${sq}%(refname:short)${sq}%(end)|\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'nested alignment' '\n+\tcat >expect <<-\\EOF &&\n+\t|         master               |\n+\t|           side               |\n+\t|       odd/spot               |\n+\t|     double-tag               |\n+\t|           four               |\n+\t|            one               |\n+\t|     signed-tag               |\n+\t|          three               |\n+\t|            two               |\n+\tEOF\n+\tgit for-each-ref --format=\"|%(align:30,left)%(align:15,right)%(refname:short)%(end)%(end)|\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_done\n-- \n2.5.0\n"},{"id":"269129","messageId":"1441131994-13508-7-git-send-email-Karthik.188@gmail.com","threadId":"40244","inReplyTo":"1441131994-13508-1-git-send-email-Karthik.188@gmail.com","subject":"[PATCH v15 06/13] ref-filter: add option to filter out tags, branches and remotes","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-01T18:26:27Z","receivedAt":"2015-09-01T18:26:27Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"From: Karthik Nayak <karthik.188@gmail.com>\n\nAdd a function called 'for_each_fullref_in()' to refs.{c,h} which\niterates through each ref for the given path without trimming the path\nand also accounting for broken refs, if mentioned.\n\nAdd 'filter_ref_kind()' in ref-filter.c to check the kind of ref being\nhandled and return the kind to 'ref_filter_handler()', where we\ndiscard refs which we do not need and assign the kind to needed refs.\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n ref-filter.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-----\n ref-filter.h | 13 +++++++++++--\n refs.c       |  9 +++++++++\n refs.h       |  1 +\n 4 files changed, 79 insertions(+), 7 deletions(-)\n\ndiff --git a/ref-filter.c b/ref-filter.c\nindex 3a5f0a7..430c80b 100644\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -1163,6 +1163,34 @@ static struct ref_array_item *new_ref_array_item(const char *refname,\n \treturn ref;\n }\n \n+static int filter_ref_kind(struct ref_filter *filter, const char *refname)\n+{\n+\tunsigned int i;\n+\n+\tstatic struct {\n+\t\tconst char *prefix;\n+\t\tunsigned int kind;\n+\t} ref_kind[] = {\n+\t\t{ \"refs/heads/\" , FILTER_REFS_BRANCHES },\n+\t\t{ \"refs/remotes/\" , FILTER_REFS_REMOTES },\n+\t\t{ \"refs/tags/\", FILTER_REFS_TAGS}\n+\t};\n+\n+\tif (filter->kind == FILTER_REFS_BRANCHES ||\n+\t    filter->kind == FILTER_REFS_REMOTES ||\n+\t    filter->kind == FILTER_REFS_TAGS)\n+\t\treturn filter->kind;\n+\telse if (!strcmp(refname, \"HEAD\"))\n+\t\treturn FILTER_REFS_DETACHED_HEAD;\n+\n+\tfor (i = 0; i < ARRAY_SIZE(ref_kind); i++) {\n+\t\tif (starts_with(refname, ref_kind[i].prefix))\n+\t\t\treturn ref_kind[i].kind;\n+\t}\n+\n+\treturn FILTER_REFS_OTHERS;\n+}\n+\n /*\n  * A call-back given to for_each_ref().  Filter refs and keep them for\n  * later object processing.\n@@ -1173,6 +1201,7 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,\n \tstruct ref_filter *filter = ref_cbdata->filter;\n \tstruct ref_array_item *ref;\n \tstruct commit *commit = NULL;\n+\tunsigned int kind;\n \n \tif (flag & REF_BAD_NAME) {\n \t\twarning(\"ignoring ref with broken name %s\", refname);\n@@ -1184,6 +1213,15 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,\n \t\treturn 0;\n \t}\n \n+\t/*\n+\t * Get the current ref kind. If we're filtering tags, remotes or local branches\n+\t * only then the current ref-kind is nothing but filter->kind and filter_ref_kind()\n+\t * will only return that value.\n+\t */\n+\tkind = filter_ref_kind(filter, refname);\n+\tif (!(kind & filter->kind))\n+\t\treturn 0;\n+\n \tif (*filter->name_patterns && !match_name_as_path(filter->name_patterns, refname))\n \t\treturn 0;\n \n@@ -1215,6 +1253,7 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,\n \n \tREALLOC_ARRAY(ref_cbdata->array->items, ref_cbdata->array->nr + 1);\n \tref_cbdata->array->items[ref_cbdata->array->nr++] = ref;\n+\tref->kind = kind;\n \treturn 0;\n }\n \n@@ -1291,17 +1330,31 @@ int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int\n {\n \tstruct ref_filter_cbdata ref_cbdata;\n \tint ret = 0;\n+\tunsigned int broken = 0;\n \n \tref_cbdata.array = array;\n \tref_cbdata.filter = filter;\n \n+\tif (type & FILTER_REFS_INCLUDE_BROKEN)\n+\t\tbroken = 1;\n+\tfilter->kind = type & FILTER_REFS_KIND_MASK;\n+\n \t/*  Simple per-ref filtering */\n-\tif (type & (FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN))\n-\t\tret = for_each_rawref(ref_filter_handler, &ref_cbdata);\n-\telse if (type & FILTER_REFS_ALL)\n-\t\tret = for_each_ref(ref_filter_handler, &ref_cbdata);\n-\telse if (type)\n+\tif (!filter->kind)\n \t\tdie(\"filter_refs: invalid type\");\n+\telse {\n+\t\tif (filter->kind == FILTER_REFS_BRANCHES)\n+\t\t\tret = for_each_fullref_in(\"refs/heads/\", ref_filter_handler, &ref_cbdata, broken);\n+\t\telse if (filter->kind == FILTER_REFS_REMOTES)\n+\t\t\tret = for_each_fullref_in(\"refs/remotes/\", ref_filter_handler, &ref_cbdata, broken);\n+\t\telse if (filter->kind == FILTER_REFS_TAGS)\n+\t\t\tret = for_each_fullref_in(\"refs/tags/\", ref_filter_handler, &ref_cbdata, broken);\n+\t\telse if (filter->kind & FILTER_REFS_ALL)\n+\t\t\tret = for_each_fullref_in(\"\", ref_filter_handler, &ref_cbdata, broken);\n+\t\tif (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))\n+\t\t\thead_ref(ref_filter_handler, &ref_cbdata);\n+\t}\n+\n \n \t/*  Filters that need revision walking */\n \tif (filter->merge_commit)\ndiff --git a/ref-filter.h b/ref-filter.h\nindex 45026d0..0913ba9 100644\n--- a/ref-filter.h\n+++ b/ref-filter.h\n@@ -13,8 +13,15 @@\n #define QUOTE_PYTHON 4\n #define QUOTE_TCL 8\n \n-#define FILTER_REFS_INCLUDE_BROKEN 0x1\n-#define FILTER_REFS_ALL 0x2\n+#define FILTER_REFS_INCLUDE_BROKEN 0x0001\n+#define FILTER_REFS_TAGS           0x0002\n+#define FILTER_REFS_BRANCHES       0x0004\n+#define FILTER_REFS_REMOTES        0x0008\n+#define FILTER_REFS_OTHERS         0x0010\n+#define FILTER_REFS_ALL            (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \\\n+\t\t\t\t    FILTER_REFS_REMOTES | FILTER_REFS_OTHERS)\n+#define FILTER_REFS_DETACHED_HEAD  0x0020\n+#define FILTER_REFS_KIND_MASK      (FILTER_REFS_ALL | FILTER_REFS_DETACHED_HEAD)\n \n struct atom_value;\n \n@@ -27,6 +34,7 @@ struct ref_sorting {\n struct ref_array_item {\n \tunsigned char objectname[20];\n \tint flag;\n+\tunsigned int kind;\n \tconst char *symref;\n \tstruct commit *commit;\n \tstruct atom_value *value;\n@@ -51,6 +59,7 @@ struct ref_filter {\n \tstruct commit *merge_commit;\n \n \tunsigned int with_commit_tag_algo : 1;\n+\tunsigned int kind;\n };\n \n struct ref_filter_cbdata {\ndiff --git a/refs.c b/refs.c\nindex 4e15f60..a9469c2 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2108,6 +2108,15 @@ int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)\n \treturn do_for_each_ref(&ref_cache, prefix, fn, strlen(prefix), 0, cb_data);\n }\n \n+int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken)\n+{\n+\tunsigned int flag = 0;\n+\n+\tif (broken)\n+\t\tflag = DO_FOR_EACH_INCLUDE_BROKEN;\n+\treturn do_for_each_ref(&ref_cache, prefix, fn, 0, flag, cb_data);\n+}\n+\n int for_each_ref_in_submodule(const char *submodule, const char *prefix,\n \t\teach_ref_fn fn, void *cb_data)\n {\ndiff --git a/refs.h b/refs.h\nindex e9a5f32..6d30c98 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -173,6 +173,7 @@ typedef int each_ref_fn(const char *refname,\n extern int head_ref(each_ref_fn fn, void *cb_data);\n extern int for_each_ref(each_ref_fn fn, void *cb_data);\n extern int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data);\n+extern int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken);\n extern int for_each_tag_ref(each_ref_fn fn, void *cb_data);\n extern int for_each_branch_ref(each_ref_fn fn, void *cb_data);\n extern int for_each_remote_ref(each_ref_fn fn, void *cb_data);\n-- \n2.5.0\n"},{"id":"269130","messageId":"1441131994-13508-8-git-send-email-Karthik.188@gmail.com","threadId":"40244","inReplyTo":"1441131994-13508-1-git-send-email-Karthik.188@gmail.com","subject":"[PATCH v15 07/13] ref-filter: add support for %(contents:lines=X)","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-01T18:26:28Z","receivedAt":"2015-09-01T18:26:28Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"In 'tag.c' we can print N lines from the annotation of the tag using\nthe '-n<num>' option. Copy code from 'tag.c' to 'ref-filter' and\nmodify it to support appending of N lines from the annotation of tags\nto the given strbuf.\n\nImplement %(contents:lines=X) where X lines of the given object are\nobtained.\n\nAdd documentation and test for the same.\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n Documentation/git-for-each-ref.txt |  3 ++-\n builtin/tag.c                      |  4 +++\n ref-filter.c                       | 53 +++++++++++++++++++++++++++++++++++---\n ref-filter.h                       |  3 ++-\n t/t6302-for-each-ref-filter.sh     | 52 +++++++++++++++++++++++++++++++++++++\n 5 files changed, 109 insertions(+), 6 deletions(-)\n\ndiff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt\nindex cac3128..98eb027 100644\n--- a/Documentation/git-for-each-ref.txt\n+++ b/Documentation/git-for-each-ref.txt\n@@ -148,7 +148,8 @@ The complete message in a commit and tag object is `contents`.\n Its first line is `contents:subject`, where subject is the concatenation\n of all lines of the commit message up to the first blank line.  The next\n line is 'contents:body', where body is all of the lines after the first\n-blank line.  Finally, the optional GPG signature is `contents:signature`.\n+blank line.  The optional GPG signature is `contents:signature`.  The\n+first `N` lines of the message is obtained using `contents:lines=N`.\n \n For sorting purposes, fields with numeric values sort in numeric\n order (`objectsize`, `authordate`, `committerdate`, `taggerdate`).\ndiff --git a/builtin/tag.c b/builtin/tag.c\nindex 471d6b1..b0bc1c5 100644\n--- a/builtin/tag.c\n+++ b/builtin/tag.c\n@@ -185,6 +185,10 @@ static enum contains_result contains(struct commit *candidate,\n \treturn contains_test(candidate, want);\n }\n \n+/*\n+ * Currently modified and used in ref-filter as append_lines(), will\n+ * eventually be removed as we port tag.c to use ref-filter APIs.\n+ */\n static void show_tag_lines(const struct object_id *oid, int lines)\n {\n \tint i;\ndiff --git a/ref-filter.c b/ref-filter.c\nindex 430c80b..0e58fee 100644\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -56,6 +56,7 @@ static struct {\n \t{ \"color\" },\n \t{ \"align\" },\n \t{ \"end\" },\n+\t{ \"contents:lines\" },\n };\n \n #define REF_FORMATTING_STATE_INIT  { 0, NULL }\n@@ -65,6 +66,11 @@ struct align {\n \tunsigned int width;\n };\n \n+struct contents {\n+\tunsigned int lines;\n+\tstruct object_id oid;\n+};\n+\n struct ref_formatting_stack {\n \tstruct ref_formatting_stack *prev;\n \tstruct strbuf output;\n@@ -79,7 +85,10 @@ struct ref_formatting_state {\n \n struct atom_value {\n \tconst char *s;\n-\tstruct align align;\n+\tunion {\n+\t\tstruct align align;\n+\t\tstruct contents contents;\n+\t} u;\n \tvoid (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);\n \tunsigned long ul; /* used for sorting when not FIELD_STR */\n };\n@@ -226,7 +235,7 @@ static void align_atom_handler(struct atom_value *atomv, struct ref_formatting_s\n \tpush_stack_element(&state->stack);\n \tnew = state->stack;\n \tnew->at_end = align_handler;\n-\tnew->cb_data = &atomv->align;\n+\tnew->cb_data = &atomv->u.align;\n }\n \n static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)\n@@ -624,6 +633,33 @@ static void find_subpos(const char *buf, unsigned long sz,\n \t*nonsiglen = *sig - buf;\n }\n \n+/*\n+ * If 'lines' is greater than 0, append that many lines from the given\n+ * 'buf' of length 'size' to the given strbuf.\n+ */\n+static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)\n+{\n+\tint i;\n+\tconst char *sp, *eol;\n+\tsize_t len;\n+\n+\tif ((sp = strstr(buf, \"\\n\\n\")) && (sp <= buf + size))\n+\t\tsize += 2;\n+\n+\tsp = buf;\n+\n+\tfor (i = 0; i < lines && sp < buf + size; i++) {\n+\t\tif (i)\n+\t\t\tstrbuf_addstr(out, \"\\n    \");\n+\t\teol = memchr(sp, '\\n', size - (sp - buf));\n+\t\tlen = eol ? eol - sp : size - (sp - buf);\n+\t\tstrbuf_add(out, sp, len);\n+\t\tif (!eol)\n+\t\t\tbreak;\n+\t\tsp = eol + 1;\n+\t}\n+}\n+\n /* See grab_values */\n static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)\n {\n@@ -634,6 +670,7 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj\n \tfor (i = 0; i < used_atom_cnt; i++) {\n \t\tconst char *name = used_atom[i];\n \t\tstruct atom_value *v = &val[i];\n+\t\tconst char *valp = NULL;\n \t\tif (!!deref != (*name == '*'))\n \t\t\tcontinue;\n \t\tif (deref)\n@@ -643,7 +680,8 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj\n \t\t    strcmp(name, \"contents\") &&\n \t\t    strcmp(name, \"contents:subject\") &&\n \t\t    strcmp(name, \"contents:body\") &&\n-\t\t    strcmp(name, \"contents:signature\"))\n+\t\t    strcmp(name, \"contents:signature\") &&\n+\t\t    !starts_with(name, \"contents:lines=\"))\n \t\t\tcontinue;\n \t\tif (!subpos)\n \t\t\tfind_subpos(buf, sz,\n@@ -663,6 +701,13 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj\n \t\t\tv->s = xmemdupz(sigpos, siglen);\n \t\telse if (!strcmp(name, \"contents\"))\n \t\t\tv->s = xstrdup(subpos);\n+\t\telse if (skip_prefix(name, \"contents:lines=\", &valp)) {\n+\t\t\tstruct strbuf s = STRBUF_INIT;\n+\t\t\tif (strtoul_ui(valp, 10, &v->u.contents.lines))\n+\t\t\t\tdie(_(\"positive width expected contents:lines=%s\"), valp);\n+\t\t\tappend_lines(&s, subpos, sublen + bodylen - siglen, v->u.contents.lines);\n+\t\t\tv->s = strbuf_detach(&s, NULL);\n+\t\t}\n \t}\n }\n \n@@ -817,7 +862,7 @@ static void populate_value(struct ref_array_item *ref)\n \t\t\t\tv->s = \" \";\n \t\t\tcontinue;\n \t\t} else if (skip_prefix(name, \"align\", &valp)) {\n-\t\t\tstruct align *align = &v->align;\n+\t\t\tstruct align *align = &v->u.align;\n \t\t\tstruct strbuf **s;\n \n \t\t\tif (valp[0] != ':')\ndiff --git a/ref-filter.h b/ref-filter.h\nindex 0913ba9..ab76b22 100644\n--- a/ref-filter.h\n+++ b/ref-filter.h\n@@ -59,7 +59,8 @@ struct ref_filter {\n \tstruct commit *merge_commit;\n \n \tunsigned int with_commit_tag_algo : 1;\n-\tunsigned int kind;\n+\tunsigned int kind,\n+\t\tlines;\n };\n \n struct ref_filter_cbdata {\ndiff --git a/t/t6302-for-each-ref-filter.sh b/t/t6302-for-each-ref-filter.sh\nindex cef7a41..75da32f 100755\n--- a/t/t6302-for-each-ref-filter.sh\n+++ b/t/t6302-for-each-ref-filter.sh\n@@ -166,4 +166,56 @@ test_expect_success 'nested alignment' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'check `%(contents:lines=1)`' '\n+\tcat >expect <<-\\EOF &&\n+\tmaster |three\n+\tside |four\n+\todd/spot |three\n+\tdouble-tag |Annonated doubly\n+\tfour |four\n+\tone |one\n+\tsigned-tag |A signed tag message\n+\tthree |three\n+\ttwo |two\n+\tEOF\n+\tgit for-each-ref --format=\"%(refname:short) |%(contents:lines=1)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'check `%(contents:lines=0)`' '\n+\tcat >expect <<-\\EOF &&\n+\tmaster |\n+\tside |\n+\todd/spot |\n+\tdouble-tag |\n+\tfour |\n+\tone |\n+\tsigned-tag |\n+\tthree |\n+\ttwo |\n+\tEOF\n+\tgit for-each-ref --format=\"%(refname:short) |%(contents:lines=0)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'check `%(contents:lines=99999)`' '\n+\tcat >expect <<-\\EOF &&\n+\tmaster |three\n+\tside |four\n+\todd/spot |three\n+\tdouble-tag |Annonated doubly\n+\tfour |four\n+\tone |one\n+\tsigned-tag |A signed tag message\n+\tthree |three\n+\ttwo |two\n+\tEOF\n+\tgit for-each-ref --format=\"%(refname:short) |%(contents:lines=99999)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success '`%(contents:lines=-1)` should fail' '\n+\ttest_must_fail git for-each-ref --format=\"%(refname:short) |%(contents:lines=-1)\"\n+'\n+\n test_done\n-- \n2.5.0\n"},{"id":"269131","messageId":"1441131994-13508-9-git-send-email-Karthik.188@gmail.com","threadId":"40244","inReplyTo":"1441131994-13508-1-git-send-email-Karthik.188@gmail.com","subject":"[PATCH v15 08/13] ref-filter: add support to sort by version","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-01T18:26:29Z","receivedAt":"2015-09-01T18:26:29Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"From: Karthik Nayak <karthik.188@gmail.com>\n\nAdd support to sort by version using the \"v:refname\" and\n\"version:refname\" option. This is achieved by using the 'versioncmp()'\nfunction as the comparing function for qsort.\n\nThis option is included to support sorting by versions in `git tag -l`\nwhich will eventually be ported to use ref-filter APIs.\n\nAdd documentation and tests for the same.\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n Documentation/git-for-each-ref.txt |  3 +++\n ref-filter.c                       | 15 ++++++++++-----\n ref-filter.h                       |  3 ++-\n t/t6302-for-each-ref-filter.sh     | 36 ++++++++++++++++++++++++++++++++++++\n 4 files changed, 51 insertions(+), 6 deletions(-)\n\ndiff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt\nindex 98eb027..d039f40 100644\n--- a/Documentation/git-for-each-ref.txt\n+++ b/Documentation/git-for-each-ref.txt\n@@ -155,6 +155,9 @@ For sorting purposes, fields with numeric values sort in numeric\n order (`objectsize`, `authordate`, `committerdate`, `taggerdate`).\n All other fields are used to sort in their byte-value order.\n \n+There is also an option to sort by versions, this can be done by using\n+the fieldname `version:refname` or its alias `v:refname`.\n+\n In any case, a field name that refers to a field inapplicable to\n the object referred by the ref does not cause an error.  It\n returns an empty string instead.\ndiff --git a/ref-filter.c b/ref-filter.c\nindex 0e58fee..a545fd4 100644\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -11,6 +11,8 @@\n #include \"ref-filter.h\"\n #include \"revision.h\"\n #include \"utf8.h\"\n+#include \"git-compat-util.h\"\n+#include \"version.h\"\n \n typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;\n \n@@ -1416,19 +1418,19 @@ static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, stru\n \n \tget_ref_atom_value(a, s->atom, &va);\n \tget_ref_atom_value(b, s->atom, &vb);\n-\tswitch (cmp_type) {\n-\tcase FIELD_STR:\n+\tif (s->version)\n+\t\tcmp = versioncmp(va->s, vb->s);\n+\telse if (cmp_type == FIELD_STR)\n \t\tcmp = strcmp(va->s, vb->s);\n-\t\tbreak;\n-\tdefault:\n+\telse {\n \t\tif (va->ul < vb->ul)\n \t\t\tcmp = -1;\n \t\telse if (va->ul == vb->ul)\n \t\t\tcmp = 0;\n \t\telse\n \t\t\tcmp = 1;\n-\t\tbreak;\n \t}\n+\n \treturn (s->reverse) ? -cmp : cmp;\n }\n \n@@ -1561,6 +1563,9 @@ int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)\n \t\ts->reverse = 1;\n \t\targ++;\n \t}\n+\tif (skip_prefix(arg, \"version:\", &arg) ||\n+\t    skip_prefix(arg, \"v:\", &arg))\n+\t\ts->version = 1;\n \tlen = strlen(arg);\n \ts->atom = parse_ref_filter_atom(arg, arg+len);\n \treturn 0;\ndiff --git a/ref-filter.h b/ref-filter.h\nindex ab76b22..ef25b6e 100644\n--- a/ref-filter.h\n+++ b/ref-filter.h\n@@ -28,7 +28,8 @@ struct atom_value;\n struct ref_sorting {\n \tstruct ref_sorting *next;\n \tint atom; /* index into used_atom array (internal) */\n-\tunsigned reverse : 1;\n+\tunsigned reverse : 1,\n+\t\tversion : 1;\n };\n \n struct ref_array_item {\ndiff --git a/t/t6302-for-each-ref-filter.sh b/t/t6302-for-each-ref-filter.sh\nindex 75da32f..d95b781 100755\n--- a/t/t6302-for-each-ref-filter.sh\n+++ b/t/t6302-for-each-ref-filter.sh\n@@ -218,4 +218,40 @@ test_expect_success '`%(contents:lines=-1)` should fail' '\n \ttest_must_fail git for-each-ref --format=\"%(refname:short) |%(contents:lines=-1)\"\n '\n \n+test_expect_success 'setup for version sort' '\n+\ttest_commit foo1.3 &&\n+\ttest_commit foo1.6 &&\n+\ttest_commit foo1.10\n+'\n+\n+test_expect_success 'version sort' '\n+\tgit for-each-ref --sort=version:refname --format=\"%(refname:short)\" refs/tags/ | grep \"foo\" >actual &&\n+\tcat >expect <<-\\EOF &&\n+\tfoo1.3\n+\tfoo1.6\n+\tfoo1.10\n+\tEOF\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'version sort (shortened)' '\n+\tgit for-each-ref --sort=v:refname --format=\"%(refname:short)\" refs/tags/ | grep \"foo\" >actual &&\n+\tcat >expect <<-\\EOF &&\n+\tfoo1.3\n+\tfoo1.6\n+\tfoo1.10\n+\tEOF\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'reverse version sort' '\n+\tgit for-each-ref --sort=-version:refname --format=\"%(refname:short)\" refs/tags/ | grep \"foo\" >actual &&\n+\tcat >expect <<-\\EOF &&\n+\tfoo1.10\n+\tfoo1.6\n+\tfoo1.3\n+\tEOF\n+\ttest_cmp expect actual\n+'\n+\n test_done\n-- \n2.5.0\n"},{"id":"269132","messageId":"1441131994-13508-10-git-send-email-Karthik.188@gmail.com","threadId":"40244","inReplyTo":"1441131994-13508-1-git-send-email-Karthik.188@gmail.com","subject":"[PATCH v15 09/13] ref-filter: add option to match literal pattern","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-01T18:26:30Z","receivedAt":"2015-09-01T18:26:30Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"From: Karthik Nayak <karthik.188@gmail.com>\n\nSince 'ref-filter' only has an option to match path names add an\noption for plain fnmatch pattern-matching.\n\nThis is to support the pattern matching options which are used in `git\ntag -l` and `git branch -l` where we can match patterns like `git tag\n-l foo*` which would match all tags which has a \"foo*\" pattern.\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n builtin/for-each-ref.c |  1 +\n ref-filter.c           | 40 +++++++++++++++++++++++++++++++++++++---\n ref-filter.h           |  3 ++-\n 3 files changed, 40 insertions(+), 4 deletions(-)\n\ndiff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\nindex 40f343b..4e9f6c2 100644\n--- a/builtin/for-each-ref.c\n+++ b/builtin/for-each-ref.c\n@@ -68,6 +68,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)\n \tgit_config(git_default_config, NULL);\n \n \tfilter.name_patterns = argv;\n+\tfilter.match_as_path = 1;\n \tfilter_refs(&array, &filter, FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN);\n \tref_array_sort(sorting, &array);\n \ndiff --git a/ref-filter.c b/ref-filter.c\nindex a545fd4..b37d57a 100644\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -1140,9 +1140,33 @@ static int commit_contains(struct ref_filter *filter, struct commit *commit)\n \n /*\n  * Return 1 if the refname matches one of the patterns, otherwise 0.\n+ * A pattern can be a literal prefix (e.g. a refname \"refs/heads/master\"\n+ * matches a pattern \"refs/heads/mas\") or a wildcard (e.g. the same ref\n+ * matches \"refs/heads/mas*\", too).\n+ */\n+static int match_pattern(const char **patterns, const char *refname)\n+{\n+\t/*\n+\t * When no '--format' option is given we need to skip the prefix\n+\t * for matching refs of tags and branches.\n+\t */\n+\t(void)(skip_prefix(refname, \"refs/tags/\", &refname) ||\n+\t       skip_prefix(refname, \"refs/heads/\", &refname) ||\n+\t       skip_prefix(refname, \"refs/remotes/\", &refname) ||\n+\t       skip_prefix(refname, \"refs/\", &refname));\n+\n+\tfor (; *patterns; patterns++) {\n+\t\tif (!wildmatch(*patterns, refname, 0, NULL))\n+\t\t\treturn 1;\n+\t}\n+\treturn 0;\n+}\n+\n+/*\n+ * Return 1 if the refname matches one of the patterns, otherwise 0.\n  * A pattern can be path prefix (e.g. a refname \"refs/heads/master\"\n- * matches a pattern \"refs/heads/\") or a wildcard (e.g. the same ref\n- * matches \"refs/heads/m*\",too).\n+ * matches a pattern \"refs/heads/\" but not \"refs/heads/m\") or a\n+ * wildcard (e.g. the same ref matches \"refs/heads/m*\", too).\n  */\n static int match_name_as_path(const char **pattern, const char *refname)\n {\n@@ -1163,6 +1187,16 @@ static int match_name_as_path(const char **pattern, const char *refname)\n \treturn 0;\n }\n \n+/* Return 1 if the refname matches one of the patterns, otherwise 0. */\n+static int filter_pattern_match(struct ref_filter *filter, const char *refname)\n+{\n+\tif (!*filter->name_patterns)\n+\t\treturn 1; /* No pattern always matches */\n+\tif (filter->match_as_path)\n+\t\treturn match_name_as_path(filter->name_patterns, refname);\n+\treturn match_pattern(filter->name_patterns, refname);\n+}\n+\n /*\n  * Given a ref (sha1, refname), check if the ref belongs to the array\n  * of sha1s. If the given ref is a tag, check if the given tag points\n@@ -1269,7 +1303,7 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,\n \tif (!(kind & filter->kind))\n \t\treturn 0;\n \n-\tif (*filter->name_patterns && !match_name_as_path(filter->name_patterns, refname))\n+\tif (!filter_pattern_match(filter, refname))\n \t\treturn 0;\n \n \tif (filter->points_at.nr && !match_points_at(&filter->points_at, oid->hash, refname))\ndiff --git a/ref-filter.h b/ref-filter.h\nindex ef25b6e..a5cfa5e 100644\n--- a/ref-filter.h\n+++ b/ref-filter.h\n@@ -59,7 +59,8 @@ struct ref_filter {\n \t} merge;\n \tstruct commit *merge_commit;\n \n-\tunsigned int with_commit_tag_algo : 1;\n+\tunsigned int with_commit_tag_algo : 1,\n+\t\tmatch_as_path : 1;\n \tunsigned int kind,\n \t\tlines;\n };\n-- \n2.5.0\n"},{"id":"269133","messageId":"1441131994-13508-11-git-send-email-Karthik.188@gmail.com","threadId":"40244","inReplyTo":"1441131994-13508-1-git-send-email-Karthik.188@gmail.com","subject":"[PATCH v15 10/13] tag.c: use 'ref-filter' data structures","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-01T18:26:31Z","receivedAt":"2015-09-01T18:26:31Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"From: Karthik Nayak <karthik.188@gmail.com>\n\nMake 'tag.c' use 'ref-filter' data structures and make changes to\nsupport the new data structures. This is a part of the process\nof porting 'tag.c' to use 'ref-filter' APIs.\n\nThis is a temporary step before porting 'tag.c' to use 'ref-filter'\ncompletely. As this is a temporary step, most of the code\nintroduced here will be removed when 'tag.c' is ported over to use\n'ref-filter' APIs.\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n builtin/tag.c | 106 +++++++++++++++++++++++++++++++---------------------------\n 1 file changed, 57 insertions(+), 49 deletions(-)\n\ndiff --git a/builtin/tag.c b/builtin/tag.c\nindex b0bc1c5..fe66f7b 100644\n--- a/builtin/tag.c\n+++ b/builtin/tag.c\n@@ -17,6 +17,7 @@\n #include \"gpg-interface.h\"\n #include \"sha1-array.h\"\n #include \"column.h\"\n+#include \"ref-filter.h\"\n \n static const char * const git_tag_usage[] = {\n \tN_(\"git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>]\"),\n@@ -34,15 +35,6 @@ static const char * const git_tag_usage[] = {\n \n static int tag_sort;\n \n-struct tag_filter {\n-\tconst char **patterns;\n-\tint lines;\n-\tint sort;\n-\tstruct string_list tags;\n-\tstruct commit_list *with_commit;\n-};\n-\n-static struct sha1_array points_at;\n static unsigned int colopts;\n \n static int match_pattern(const char **patterns, const char *ref)\n@@ -61,19 +53,20 @@ static int match_pattern(const char **patterns, const char *ref)\n  * removed as we port tag.c to use the ref-filter APIs.\n  */\n static const unsigned char *match_points_at(const char *refname,\n-\t\t\t\t\t    const unsigned char *sha1)\n+\t\t\t\t\t    const unsigned char *sha1,\n+\t\t\t\t\t    struct sha1_array *points_at)\n {\n \tconst unsigned char *tagged_sha1 = NULL;\n \tstruct object *obj;\n \n-\tif (sha1_array_lookup(&points_at, sha1) >= 0)\n+\tif (sha1_array_lookup(points_at, sha1) >= 0)\n \t\treturn sha1;\n \tobj = parse_object(sha1);\n \tif (!obj)\n \t\tdie(_(\"malformed object at '%s'\"), refname);\n \tif (obj->type == OBJ_TAG)\n \t\ttagged_sha1 = ((struct tag *)obj)->tagged->sha1;\n-\tif (tagged_sha1 && sha1_array_lookup(&points_at, tagged_sha1) >= 0)\n+\tif (tagged_sha1 && sha1_array_lookup(points_at, tagged_sha1) >= 0)\n \t\treturn tagged_sha1;\n \treturn NULL;\n }\n@@ -228,12 +221,24 @@ free_return:\n \tfree(buf);\n }\n \n+static void ref_array_append(struct ref_array *array, const char *refname)\n+{\n+\tsize_t len = strlen(refname);\n+\tstruct ref_array_item *ref = xcalloc(1, sizeof(struct ref_array_item) + len + 1);\n+\tmemcpy(ref->refname, refname, len);\n+\tref->refname[len] = '\\0';\n+\tREALLOC_ARRAY(array->items, array->nr + 1);\n+\tarray->items[array->nr++] = ref;\n+}\n+\n static int show_reference(const char *refname, const struct object_id *oid,\n \t\t\t  int flag, void *cb_data)\n {\n-\tstruct tag_filter *filter = cb_data;\n+\tstruct ref_filter_cbdata *data = cb_data;\n+\tstruct ref_array *array = data->array;\n+\tstruct ref_filter *filter = data->filter;\n \n-\tif (match_pattern(filter->patterns, refname)) {\n+\tif (match_pattern(filter->name_patterns, refname)) {\n \t\tif (filter->with_commit) {\n \t\t\tstruct commit *commit;\n \n@@ -244,12 +249,12 @@ static int show_reference(const char *refname, const struct object_id *oid,\n \t\t\t\treturn 0;\n \t\t}\n \n-\t\tif (points_at.nr && !match_points_at(refname, oid->hash))\n+\t\tif (filter->points_at.nr && !match_points_at(refname, oid->hash, &filter->points_at))\n \t\t\treturn 0;\n \n \t\tif (!filter->lines) {\n-\t\t\tif (filter->sort)\n-\t\t\t\tstring_list_append(&filter->tags, refname);\n+\t\t\tif (tag_sort)\n+\t\t\t\tref_array_append(array, refname);\n \t\t\telse\n \t\t\t\tprintf(\"%s\\n\", refname);\n \t\t\treturn 0;\n@@ -264,36 +269,36 @@ static int show_reference(const char *refname, const struct object_id *oid,\n \n static int sort_by_version(const void *a_, const void *b_)\n {\n-\tconst struct string_list_item *a = a_;\n-\tconst struct string_list_item *b = b_;\n-\treturn versioncmp(a->string, b->string);\n+\tconst struct ref_array_item *a = *((struct ref_array_item **)a_);\n+\tconst struct ref_array_item *b = *((struct ref_array_item **)b_);\n+\treturn versioncmp(a->refname, b->refname);\n }\n \n-static int list_tags(const char **patterns, int lines,\n-\t\t     struct commit_list *with_commit, int sort)\n+static int list_tags(struct ref_filter *filter, int sort)\n {\n-\tstruct tag_filter filter;\n+\tstruct ref_array array;\n+\tstruct ref_filter_cbdata data;\n+\n+\tmemset(&array, 0, sizeof(array));\n+\tdata.array = &array;\n+\tdata.filter = filter;\n \n-\tfilter.patterns = patterns;\n-\tfilter.lines = lines;\n-\tfilter.sort = sort;\n-\tfilter.with_commit = with_commit;\n-\tmemset(&filter.tags, 0, sizeof(filter.tags));\n-\tfilter.tags.strdup_strings = 1;\n+\tif (filter->lines == -1)\n+\t\tfilter->lines = 0;\n \n-\tfor_each_tag_ref(show_reference, (void *)&filter);\n+\tfor_each_tag_ref(show_reference, &data);\n \tif (sort) {\n \t\tint i;\n \t\tif ((sort & SORT_MASK) == VERCMP_SORT)\n-\t\t\tqsort(filter.tags.items, filter.tags.nr,\n-\t\t\t      sizeof(struct string_list_item), sort_by_version);\n+\t\t\tqsort(array.items, array.nr,\n+\t\t\t      sizeof(struct ref_array_item *), sort_by_version);\n \t\tif (sort & REVERSE_SORT)\n-\t\t\tfor (i = filter.tags.nr - 1; i >= 0; i--)\n-\t\t\t\tprintf(\"%s\\n\", filter.tags.items[i].string);\n+\t\t\tfor (i = array.nr - 1; i >= 0; i--)\n+\t\t\t\tprintf(\"%s\\n\", array.items[i]->refname);\n \t\telse\n-\t\t\tfor (i = 0; i < filter.tags.nr; i++)\n-\t\t\t\tprintf(\"%s\\n\", filter.tags.items[i].string);\n-\t\tstring_list_clear(&filter.tags, 0);\n+\t\t\tfor (i = 0; i < array.nr; i++)\n+\t\t\t\tprintf(\"%s\\n\", array.items[i]->refname);\n+\t\tref_array_clear(&array);\n \t}\n \treturn 0;\n }\n@@ -574,17 +579,17 @@ int cmd_tag(int argc, const char **argv, const char *prefix)\n \tconst char *object_ref, *tag;\n \tstruct create_tag_options opt;\n \tchar *cleanup_arg = NULL;\n-\tint annotate = 0, force = 0, lines = -1;\n \tint create_reflog = 0;\n+\tint annotate = 0, force = 0;\n \tint cmdmode = 0;\n \tconst char *msgfile = NULL, *keyid = NULL;\n \tstruct msg_arg msg = { 0, STRBUF_INIT };\n-\tstruct commit_list *with_commit = NULL;\n \tstruct ref_transaction *transaction;\n \tstruct strbuf err = STRBUF_INIT;\n+\tstruct ref_filter filter;\n \tstruct option options[] = {\n \t\tOPT_CMDMODE('l', \"list\", &cmdmode, N_(\"list tag names\"), 'l'),\n-\t\t{ OPTION_INTEGER, 'n', NULL, &lines, N_(\"n\"),\n+\t\t{ OPTION_INTEGER, 'n', NULL, &filter.lines, N_(\"n\"),\n \t\t\t\tN_(\"print <n> lines of each tag message\"),\n \t\t\t\tPARSE_OPT_OPTARG, NULL, 1 },\n \t\tOPT_CMDMODE('d', \"delete\", &cmdmode, N_(\"delete tags\"), 'd'),\n@@ -606,14 +611,14 @@ int cmd_tag(int argc, const char **argv, const char *prefix)\n \n \t\tOPT_GROUP(N_(\"Tag listing options\")),\n \t\tOPT_COLUMN(0, \"column\", &colopts, N_(\"show tag list in columns\")),\n-\t\tOPT_CONTAINS(&with_commit, N_(\"print only tags that contain the commit\")),\n-\t\tOPT_WITH(&with_commit, N_(\"print only tags that contain the commit\")),\n+\t\tOPT_CONTAINS(&filter.with_commit, N_(\"print only tags that contain the commit\")),\n+\t\tOPT_WITH(&filter.with_commit, N_(\"print only tags that contain the commit\")),\n \t\t{\n \t\t\tOPTION_CALLBACK, 0, \"sort\", &tag_sort, N_(\"type\"), N_(\"sort tags\"),\n \t\t\tPARSE_OPT_NONEG, parse_opt_sort\n \t\t},\n \t\t{\n-\t\t\tOPTION_CALLBACK, 0, \"points-at\", &points_at, N_(\"object\"),\n+\t\t\tOPTION_CALLBACK, 0, \"points-at\", &filter.points_at, N_(\"object\"),\n \t\t\tN_(\"print only tags of the object\"), 0, parse_opt_object_name\n \t\t},\n \t\tOPT_END()\n@@ -622,6 +627,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)\n \tgit_config(git_tag_config, NULL);\n \n \tmemset(&opt, 0, sizeof(opt));\n+\tmemset(&filter, 0, sizeof(filter));\n+\tfilter.lines = -1;\n \n \targc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);\n \n@@ -638,7 +645,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)\n \t\tusage_with_options(git_tag_usage, options);\n \n \tfinalize_colopts(&colopts, -1);\n-\tif (cmdmode == 'l' && lines != -1) {\n+\tif (cmdmode == 'l' && filter.lines != -1) {\n \t\tif (explicitly_enable_column(colopts))\n \t\t\tdie(_(\"--column and -n are incompatible\"));\n \t\tcolopts = 0;\n@@ -651,18 +658,19 @@ int cmd_tag(int argc, const char **argv, const char *prefix)\n \t\t\tcopts.padding = 2;\n \t\t\trun_column_filter(colopts, &copts);\n \t\t}\n-\t\tif (lines != -1 && tag_sort)\n+\t\tif (filter.lines != -1 && tag_sort)\n \t\t\tdie(_(\"--sort and -n are incompatible\"));\n-\t\tret = list_tags(argv, lines == -1 ? 0 : lines, with_commit, tag_sort);\n+\t\tfilter.name_patterns = argv;\n+\t\tret = list_tags(&filter, tag_sort);\n \t\tif (column_active(colopts))\n \t\t\tstop_column_filter();\n \t\treturn ret;\n \t}\n-\tif (lines != -1)\n+\tif (filter.lines != -1)\n \t\tdie(_(\"-n option is only allowed with -l.\"));\n-\tif (with_commit)\n+\tif (filter.with_commit)\n \t\tdie(_(\"--contains option is only allowed with -l.\"));\n-\tif (points_at.nr)\n+\tif (filter.points_at.nr)\n \t\tdie(_(\"--points-at option is only allowed with -l.\"));\n \tif (cmdmode == 'd')\n \t\treturn for_each_tag_name(argv, delete_tag);\n-- \n2.5.0\n"},{"id":"269134","messageId":"1441131994-13508-12-git-send-email-Karthik.188@gmail.com","threadId":"40244","inReplyTo":"1441131994-13508-1-git-send-email-Karthik.188@gmail.com","subject":"[PATCH v15 11/13] tag.c: use 'ref-filter' APIs","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-01T18:26:32Z","receivedAt":"2015-09-01T18:26:32Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"From: Karthik Nayak <karthik.188@gmail.com>\n\nMake 'tag.c' use 'ref-filter' APIs for iterating through refs, sorting\nand printing of refs. This removes most of the code used in 'tag.c'\nreplacing it with calls to the 'ref-filter' library.\n\nMake 'tag.c' use the 'filter_refs()' function provided by 'ref-filter'\nto filter out tags based on the options set.\n\nFor printing tags we use 'show_ref_array_item()' function provided by\n'ref-filter'.\n\nWe improve the sorting option provided by 'tag.c' by using the sorting\noptions provided by 'ref-filter'. This causes the test 'invalid sort\nparameter on command line' in t7004 to fail, as 'ref-filter' throws an\nerror for all sorting fields which are incorrect. The test is changed\nto reflect the same.\n\nModify documentation for the same.\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n Documentation/git-tag.txt |  16 ++-\n builtin/tag.c             | 344 ++++++----------------------------------------\n t/t7004-tag.sh            |   8 +-\n 3 files changed, 52 insertions(+), 316 deletions(-)\n\ndiff --git a/Documentation/git-tag.txt b/Documentation/git-tag.txt\nindex 84f6496..3ac4a96 100644\n--- a/Documentation/git-tag.txt\n+++ b/Documentation/git-tag.txt\n@@ -13,7 +13,7 @@ SYNOPSIS\n \t<tagname> [<commit> | <object>]\n 'git tag' -d <tagname>...\n 'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>]\n-\t[--column[=<options>] | --no-column] [--create-reflog] [<pattern>...]\n+\t[--column[=<options>] | --no-column] [--create-reflog] [--sort=<key>] [<pattern>...]\n 'git tag' -v <tagname>...\n \n DESCRIPTION\n@@ -94,14 +94,16 @@ OPTIONS\n \tusing fnmatch(3)).  Multiple patterns may be given; if any of\n \tthem matches, the tag is shown.\n \n---sort=<type>::\n-\tSort in a specific order. Supported type is \"refname\"\n-\t(lexicographic order), \"version:refname\" or \"v:refname\" (tag\n+--sort=<key>::\n+\tSort based on the key given.  Prefix `-` to sort in\n+\tdescending order of the value. You may use the --sort=<key> option\n+\tmultiple times, in which case the last key becomes the primary\n+\tkey. Also supports \"version:refname\" or \"v:refname\" (tag\n \tnames are treated as versions). The \"version:refname\" sort\n \torder can also be affected by the\n-\t\"versionsort.prereleaseSuffix\" configuration variable. Prepend\n-\t\"-\" to reverse sort order. When this option is not given, the\n-\tsort order defaults to the value configured for the 'tag.sort'\n+\t\"versionsort.prereleaseSuffix\" configuration variable.\n+\tThe keys supported are the same as those in `git for-each-ref`.\n+\tSort order defaults to the value configured for the 'tag.sort'\n \tvariable if it exists, or lexicographic order otherwise. See\n \tlinkgit:git-config[1].\n \ndiff --git a/builtin/tag.c b/builtin/tag.c\nindex fe66f7b..2d348f4 100644\n--- a/builtin/tag.c\n+++ b/builtin/tag.c\n@@ -28,278 +28,34 @@ static const char * const git_tag_usage[] = {\n \tNULL\n };\n \n-#define STRCMP_SORT     0\t/* must be zero */\n-#define VERCMP_SORT     1\n-#define SORT_MASK       0x7fff\n-#define REVERSE_SORT    0x8000\n-\n-static int tag_sort;\n-\n static unsigned int colopts;\n \n-static int match_pattern(const char **patterns, const char *ref)\n-{\n-\t/* no pattern means match everything */\n-\tif (!*patterns)\n-\t\treturn 1;\n-\tfor (; *patterns; patterns++)\n-\t\tif (!wildmatch(*patterns, ref, 0, NULL))\n-\t\t\treturn 1;\n-\treturn 0;\n-}\n-\n-/*\n- * This is currently duplicated in ref-filter.c, and will eventually be\n- * removed as we port tag.c to use the ref-filter APIs.\n- */\n-static const unsigned char *match_points_at(const char *refname,\n-\t\t\t\t\t    const unsigned char *sha1,\n-\t\t\t\t\t    struct sha1_array *points_at)\n-{\n-\tconst unsigned char *tagged_sha1 = NULL;\n-\tstruct object *obj;\n-\n-\tif (sha1_array_lookup(points_at, sha1) >= 0)\n-\t\treturn sha1;\n-\tobj = parse_object(sha1);\n-\tif (!obj)\n-\t\tdie(_(\"malformed object at '%s'\"), refname);\n-\tif (obj->type == OBJ_TAG)\n-\t\ttagged_sha1 = ((struct tag *)obj)->tagged->sha1;\n-\tif (tagged_sha1 && sha1_array_lookup(points_at, tagged_sha1) >= 0)\n-\t\treturn tagged_sha1;\n-\treturn NULL;\n-}\n-\n-static int in_commit_list(const struct commit_list *want, struct commit *c)\n-{\n-\tfor (; want; want = want->next)\n-\t\tif (!hashcmp(want->item->object.sha1, c->object.sha1))\n-\t\t\treturn 1;\n-\treturn 0;\n-}\n-\n-/*\n- * The entire code segment for supporting the --contains option has been\n- * copied over to ref-filter.{c,h}. This will be deleted evetually when\n- * we port tag.c to use ref-filter APIs.\n- */\n-enum contains_result {\n-\tCONTAINS_UNKNOWN = -1,\n-\tCONTAINS_NO = 0,\n-\tCONTAINS_YES = 1\n-};\n-\n-/*\n- * Test whether the candidate or one of its parents is contained in the list.\n- * Do not recurse to find out, though, but return -1 if inconclusive.\n- */\n-static enum contains_result contains_test(struct commit *candidate,\n-\t\t\t    const struct commit_list *want)\n-{\n-\t/* was it previously marked as containing a want commit? */\n-\tif (candidate->object.flags & TMP_MARK)\n-\t\treturn 1;\n-\t/* or marked as not possibly containing a want commit? */\n-\tif (candidate->object.flags & UNINTERESTING)\n-\t\treturn 0;\n-\t/* or are we it? */\n-\tif (in_commit_list(want, candidate)) {\n-\t\tcandidate->object.flags |= TMP_MARK;\n-\t\treturn 1;\n-\t}\n-\n-\tif (parse_commit(candidate) < 0)\n-\t\treturn 0;\n-\n-\treturn -1;\n-}\n-\n-/*\n- * Mimicking the real stack, this stack lives on the heap, avoiding stack\n- * overflows.\n- *\n- * At each recursion step, the stack items points to the commits whose\n- * ancestors are to be inspected.\n- */\n-struct stack {\n-\tint nr, alloc;\n-\tstruct stack_entry {\n-\t\tstruct commit *commit;\n-\t\tstruct commit_list *parents;\n-\t} *stack;\n-};\n-\n-static void push_to_stack(struct commit *candidate, struct stack *stack)\n-{\n-\tint index = stack->nr++;\n-\tALLOC_GROW(stack->stack, stack->nr, stack->alloc);\n-\tstack->stack[index].commit = candidate;\n-\tstack->stack[index].parents = candidate->parents;\n-}\n-\n-static enum contains_result contains(struct commit *candidate,\n-\t\tconst struct commit_list *want)\n-{\n-\tstruct stack stack = { 0, 0, NULL };\n-\tint result = contains_test(candidate, want);\n-\n-\tif (result != CONTAINS_UNKNOWN)\n-\t\treturn result;\n-\n-\tpush_to_stack(candidate, &stack);\n-\twhile (stack.nr) {\n-\t\tstruct stack_entry *entry = &stack.stack[stack.nr - 1];\n-\t\tstruct commit *commit = entry->commit;\n-\t\tstruct commit_list *parents = entry->parents;\n-\n-\t\tif (!parents) {\n-\t\t\tcommit->object.flags |= UNINTERESTING;\n-\t\t\tstack.nr--;\n-\t\t}\n-\t\t/*\n-\t\t * If we just popped the stack, parents->item has been marked,\n-\t\t * therefore contains_test will return a meaningful 0 or 1.\n-\t\t */\n-\t\telse switch (contains_test(parents->item, want)) {\n-\t\tcase CONTAINS_YES:\n-\t\t\tcommit->object.flags |= TMP_MARK;\n-\t\t\tstack.nr--;\n-\t\t\tbreak;\n-\t\tcase CONTAINS_NO:\n-\t\t\tentry->parents = parents->next;\n-\t\t\tbreak;\n-\t\tcase CONTAINS_UNKNOWN:\n-\t\t\tpush_to_stack(parents->item, &stack);\n-\t\t\tbreak;\n-\t\t}\n-\t}\n-\tfree(stack.stack);\n-\treturn contains_test(candidate, want);\n-}\n-\n-/*\n- * Currently modified and used in ref-filter as append_lines(), will\n- * eventually be removed as we port tag.c to use ref-filter APIs.\n- */\n-static void show_tag_lines(const struct object_id *oid, int lines)\n-{\n-\tint i;\n-\tunsigned long size;\n-\tenum object_type type;\n-\tchar *buf, *sp, *eol;\n-\tsize_t len;\n-\n-\tbuf = read_sha1_file(oid->hash, &type, &size);\n-\tif (!buf)\n-\t\tdie_errno(\"unable to read object %s\", oid_to_hex(oid));\n-\tif (type != OBJ_COMMIT && type != OBJ_TAG)\n-\t\tgoto free_return;\n-\tif (!size)\n-\t\tdie(\"an empty %s object %s?\",\n-\t\t    typename(type), oid_to_hex(oid));\n-\n-\t/* skip header */\n-\tsp = strstr(buf, \"\\n\\n\");\n-\tif (!sp)\n-\t\tgoto free_return;\n-\n-\t/* only take up to \"lines\" lines, and strip the signature from a tag */\n-\tif (type == OBJ_TAG)\n-\t\tsize = parse_signature(buf, size);\n-\tfor (i = 0, sp += 2; i < lines && sp < buf + size; i++) {\n-\t\tif (i)\n-\t\t\tprintf(\"\\n    \");\n-\t\teol = memchr(sp, '\\n', size - (sp - buf));\n-\t\tlen = eol ? eol - sp : size - (sp - buf);\n-\t\tfwrite(sp, len, 1, stdout);\n-\t\tif (!eol)\n-\t\t\tbreak;\n-\t\tsp = eol + 1;\n-\t}\n-free_return:\n-\tfree(buf);\n-}\n-\n-static void ref_array_append(struct ref_array *array, const char *refname)\n-{\n-\tsize_t len = strlen(refname);\n-\tstruct ref_array_item *ref = xcalloc(1, sizeof(struct ref_array_item) + len + 1);\n-\tmemcpy(ref->refname, refname, len);\n-\tref->refname[len] = '\\0';\n-\tREALLOC_ARRAY(array->items, array->nr + 1);\n-\tarray->items[array->nr++] = ref;\n-}\n-\n-static int show_reference(const char *refname, const struct object_id *oid,\n-\t\t\t  int flag, void *cb_data)\n-{\n-\tstruct ref_filter_cbdata *data = cb_data;\n-\tstruct ref_array *array = data->array;\n-\tstruct ref_filter *filter = data->filter;\n-\n-\tif (match_pattern(filter->name_patterns, refname)) {\n-\t\tif (filter->with_commit) {\n-\t\t\tstruct commit *commit;\n-\n-\t\t\tcommit = lookup_commit_reference_gently(oid->hash, 1);\n-\t\t\tif (!commit)\n-\t\t\t\treturn 0;\n-\t\t\tif (!contains(commit, filter->with_commit))\n-\t\t\t\treturn 0;\n-\t\t}\n-\n-\t\tif (filter->points_at.nr && !match_points_at(refname, oid->hash, &filter->points_at))\n-\t\t\treturn 0;\n-\n-\t\tif (!filter->lines) {\n-\t\t\tif (tag_sort)\n-\t\t\t\tref_array_append(array, refname);\n-\t\t\telse\n-\t\t\t\tprintf(\"%s\\n\", refname);\n-\t\t\treturn 0;\n-\t\t}\n-\t\tprintf(\"%-15s \", refname);\n-\t\tshow_tag_lines(oid, filter->lines);\n-\t\tputchar('\\n');\n-\t}\n-\n-\treturn 0;\n-}\n-\n-static int sort_by_version(const void *a_, const void *b_)\n-{\n-\tconst struct ref_array_item *a = *((struct ref_array_item **)a_);\n-\tconst struct ref_array_item *b = *((struct ref_array_item **)b_);\n-\treturn versioncmp(a->refname, b->refname);\n-}\n-\n-static int list_tags(struct ref_filter *filter, int sort)\n+static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting)\n {\n \tstruct ref_array array;\n-\tstruct ref_filter_cbdata data;\n+\tchar *format, *to_free = NULL;\n+\tint i;\n \n \tmemset(&array, 0, sizeof(array));\n-\tdata.array = &array;\n-\tdata.filter = filter;\n \n \tif (filter->lines == -1)\n \t\tfilter->lines = 0;\n \n-\tfor_each_tag_ref(show_reference, &data);\n-\tif (sort) {\n-\t\tint i;\n-\t\tif ((sort & SORT_MASK) == VERCMP_SORT)\n-\t\t\tqsort(array.items, array.nr,\n-\t\t\t      sizeof(struct ref_array_item *), sort_by_version);\n-\t\tif (sort & REVERSE_SORT)\n-\t\t\tfor (i = array.nr - 1; i >= 0; i--)\n-\t\t\t\tprintf(\"%s\\n\", array.items[i]->refname);\n-\t\telse\n-\t\t\tfor (i = 0; i < array.nr; i++)\n-\t\t\t\tprintf(\"%s\\n\", array.items[i]->refname);\n-\t\tref_array_clear(&array);\n-\t}\n+\tif (filter->lines)\n+\t\tformat = to_free = xstrfmt(\"%%(align:15,left)%%(refname:short)%%(end) %%(contents:lines=%d)\",\n+\t\t\t\t\t   filter->lines);\n+\telse\n+\t\tformat = \"%(refname:short)\";\n+\n+\tverify_ref_format(format);\n+\tfilter_refs(&array, filter, FILTER_REFS_TAGS);\n+\tref_array_sort(sorting, &array);\n+\n+\tfor (i = 0; i < array.nr; i++)\n+\t\tshow_ref_array_item(array.items[i], format, 0);\n+\tref_array_clear(&array);\n+\tfree(to_free);\n+\n \treturn 0;\n }\n \n@@ -366,35 +122,26 @@ static const char tag_template_nocleanup[] =\n \t\"Lines starting with '%c' will be kept; you may remove them\"\n \t\" yourself if you want to.\\n\");\n \n-/*\n- * Parse a sort string, and return 0 if parsed successfully. Will return\n- * non-zero when the sort string does not parse into a known type. If var is\n- * given, the error message becomes a warning and includes information about\n- * the configuration value.\n- */\n-static int parse_sort_string(const char *var, const char *arg, int *sort)\n+/* Parse arg given and add it the ref_sorting array */\n+static int parse_sorting_string(const char *arg, struct ref_sorting **sorting_tail)\n {\n-\tint type = 0, flags = 0;\n-\n-\tif (skip_prefix(arg, \"-\", &arg))\n-\t\tflags |= REVERSE_SORT;\n+\tstruct ref_sorting *s;\n+\tint len;\n \n-\tif (skip_prefix(arg, \"version:\", &arg) || skip_prefix(arg, \"v:\", &arg))\n-\t\ttype = VERCMP_SORT;\n-\telse\n-\t\ttype = STRCMP_SORT;\n+\ts = xcalloc(1, sizeof(*s));\n+\ts->next = *sorting_tail;\n+\t*sorting_tail = s;\n \n-\tif (strcmp(arg, \"refname\")) {\n-\t\tif (!var)\n-\t\t\treturn error(_(\"unsupported sort specification '%s'\"), arg);\n-\t\telse {\n-\t\t\twarning(_(\"unsupported sort specification '%s' in variable '%s'\"),\n-\t\t\t\tvar, arg);\n-\t\t\treturn -1;\n-\t\t}\n+\tif (*arg == '-') {\n+\t\ts->reverse = 1;\n+\t\targ++;\n \t}\n+\tif (skip_prefix(arg, \"version:\", &arg) ||\n+\t    skip_prefix(arg, \"v:\", &arg))\n+\t\ts->version = 1;\n \n-\t*sort = (type | flags);\n+\tlen = strlen(arg);\n+\ts->atom = parse_ref_filter_atom(arg, arg+len);\n \n \treturn 0;\n }\n@@ -402,11 +149,12 @@ static int parse_sort_string(const char *var, const char *arg, int *sort)\n static int git_tag_config(const char *var, const char *value, void *cb)\n {\n \tint status;\n+\tstruct ref_sorting **sorting_tail = (struct ref_sorting **)cb;\n \n \tif (!strcmp(var, \"tag.sort\")) {\n \t\tif (!value)\n \t\t\treturn config_error_nonbool(var);\n-\t\tparse_sort_string(var, value, &tag_sort);\n+\t\tparse_sorting_string(value, sorting_tail);\n \t\treturn 0;\n \t}\n \n@@ -564,13 +312,6 @@ static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)\n \treturn check_refname_format(sb->buf, 0);\n }\n \n-static int parse_opt_sort(const struct option *opt, const char *arg, int unset)\n-{\n-\tint *sort = opt->value;\n-\n-\treturn parse_sort_string(NULL, arg, sort);\n-}\n-\n int cmd_tag(int argc, const char **argv, const char *prefix)\n {\n \tstruct strbuf buf = STRBUF_INIT;\n@@ -587,6 +328,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)\n \tstruct ref_transaction *transaction;\n \tstruct strbuf err = STRBUF_INIT;\n \tstruct ref_filter filter;\n+\tstatic struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;\n \tstruct option options[] = {\n \t\tOPT_CMDMODE('l', \"list\", &cmdmode, N_(\"list tag names\"), 'l'),\n \t\t{ OPTION_INTEGER, 'n', NULL, &filter.lines, N_(\"n\"),\n@@ -613,10 +355,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)\n \t\tOPT_COLUMN(0, \"column\", &colopts, N_(\"show tag list in columns\")),\n \t\tOPT_CONTAINS(&filter.with_commit, N_(\"print only tags that contain the commit\")),\n \t\tOPT_WITH(&filter.with_commit, N_(\"print only tags that contain the commit\")),\n-\t\t{\n-\t\t\tOPTION_CALLBACK, 0, \"sort\", &tag_sort, N_(\"type\"), N_(\"sort tags\"),\n-\t\t\tPARSE_OPT_NONEG, parse_opt_sort\n-\t\t},\n+\t\tOPT_CALLBACK(0 , \"sort\", sorting_tail, N_(\"key\"),\n+\t\t\t     N_(\"field name to sort on\"), &parse_opt_ref_sorting),\n \t\t{\n \t\t\tOPTION_CALLBACK, 0, \"points-at\", &filter.points_at, N_(\"object\"),\n \t\t\tN_(\"print only tags of the object\"), 0, parse_opt_object_name\n@@ -624,7 +364,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)\n \t\tOPT_END()\n \t};\n \n-\tgit_config(git_tag_config, NULL);\n+\tgit_config(git_tag_config, sorting_tail);\n \n \tmemset(&opt, 0, sizeof(opt));\n \tmemset(&filter, 0, sizeof(filter));\n@@ -650,6 +390,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)\n \t\t\tdie(_(\"--column and -n are incompatible\"));\n \t\tcolopts = 0;\n \t}\n+\tif (!sorting)\n+\t\tsorting = ref_default_sorting();\n \tif (cmdmode == 'l') {\n \t\tint ret;\n \t\tif (column_active(colopts)) {\n@@ -658,10 +400,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)\n \t\t\tcopts.padding = 2;\n \t\t\trun_column_filter(colopts, &copts);\n \t\t}\n-\t\tif (filter.lines != -1 && tag_sort)\n-\t\t\tdie(_(\"--sort and -n are incompatible\"));\n \t\tfilter.name_patterns = argv;\n-\t\tret = list_tags(&filter, tag_sort);\n+\t\tret = list_tags(&filter, sorting);\n \t\tif (column_active(colopts))\n \t\t\tstop_column_filter();\n \t\treturn ret;\ndiff --git a/t/t7004-tag.sh b/t/t7004-tag.sh\nindex d31788c..84153ef 100755\n--- a/t/t7004-tag.sh\n+++ b/t/t7004-tag.sh\n@@ -1462,13 +1462,7 @@ test_expect_success 'invalid sort parameter on command line' '\n \n test_expect_success 'invalid sort parameter in configuratoin' '\n \tgit config tag.sort \"v:notvalid\" &&\n-\tgit tag -l \"foo*\" >actual &&\n-\tcat >expect <<-\\EOF &&\n-\tfoo1.10\n-\tfoo1.3\n-\tfoo1.6\n-\tEOF\n-\ttest_cmp expect actual\n+\ttest_must_fail git tag -l \"foo*\"\n '\n \n test_expect_success 'version sort with prerelease reordering' '\n-- \n2.5.0\n"},{"id":"269135","messageId":"1441131994-13508-13-git-send-email-Karthik.188@gmail.com","threadId":"40244","inReplyTo":"1441131994-13508-1-git-send-email-Karthik.188@gmail.com","subject":"[PATCH v15 12/13] tag.c: implement '--format' option","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-01T18:26:33Z","receivedAt":"2015-09-01T18:26:33Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Implement the '--format' option provided by 'ref-filter'.\nThis lets the user list tags as per desired format similar\nto the implementation in 'git for-each-ref'.\n\nAdd tests and documentation for the same.\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n Documentation/git-tag.txt |  8 +++++++-\n builtin/tag.c             | 22 +++++++++++++---------\n t/t7004-tag.sh            | 12 ++++++++++++\n 3 files changed, 32 insertions(+), 10 deletions(-)\n\ndiff --git a/Documentation/git-tag.txt b/Documentation/git-tag.txt\nindex 3ac4a96..0c7f4e6 100644\n--- a/Documentation/git-tag.txt\n+++ b/Documentation/git-tag.txt\n@@ -13,7 +13,8 @@ SYNOPSIS\n \t<tagname> [<commit> | <object>]\n 'git tag' -d <tagname>...\n 'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>]\n-\t[--column[=<options>] | --no-column] [--create-reflog] [--sort=<key>] [<pattern>...]\n+\t[--column[=<options>] | --no-column] [--create-reflog] [--sort=<key>]\n+\t[--format=<format>] [<pattern>...]\n 'git tag' -v <tagname>...\n \n DESCRIPTION\n@@ -158,6 +159,11 @@ This option is only applicable when listing tags without annotation lines.\n \tThe object that the new tag will refer to, usually a commit.\n \tDefaults to HEAD.\n \n+<format>::\n+\tA string that interpolates `%(fieldname)` from the object\n+\tpointed at by a ref being shown.  The format is the same as\n+\tthat of linkgit:git-for-each-ref[1].  When unspecified,\n+\tdefaults to `%(refname:short)`.\n \n CONFIGURATION\n -------------\ndiff --git a/builtin/tag.c b/builtin/tag.c\nindex 2d348f4..dc4494d 100644\n--- a/builtin/tag.c\n+++ b/builtin/tag.c\n@@ -23,17 +23,17 @@ static const char * const git_tag_usage[] = {\n \tN_(\"git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>]\"),\n \tN_(\"git tag -d <tagname>...\"),\n \tN_(\"git tag -l [-n[<num>]] [--contains <commit>] [--points-at <object>]\"\n-\t\t\"\\n\\t\\t[<pattern>...]\"),\n+\t\t\"\\n\\t\\t[--format=<format>] [<pattern>...]\"),\n \tN_(\"git tag -v <tagname>...\"),\n \tNULL\n };\n \n static unsigned int colopts;\n \n-static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting)\n+static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, const char *format)\n {\n \tstruct ref_array array;\n-\tchar *format, *to_free = NULL;\n+\tchar *to_free = NULL;\n \tint i;\n \n \tmemset(&array, 0, sizeof(array));\n@@ -41,11 +41,13 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting)\n \tif (filter->lines == -1)\n \t\tfilter->lines = 0;\n \n-\tif (filter->lines)\n-\t\tformat = to_free = xstrfmt(\"%%(align:15,left)%%(refname:short)%%(end) %%(contents:lines=%d)\",\n-\t\t\t\t\t   filter->lines);\n-\telse\n-\t\tformat = \"%(refname:short)\";\n+\tif (!format) {\n+\t\tif (filter->lines)\n+\t\t\tformat = to_free = xstrfmt(\"%%(align:15,left)%%(refname:short)%%(end) %%(contents:lines=%d)\",\n+\t\t\t\t\t\t   filter->lines);\n+\t\telse\n+\t\t\tformat = \"%(refname:short)\";\n+\t}\n \n \tverify_ref_format(format);\n \tfilter_refs(&array, filter, FILTER_REFS_TAGS);\n@@ -329,6 +331,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)\n \tstruct strbuf err = STRBUF_INIT;\n \tstruct ref_filter filter;\n \tstatic struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;\n+\tconst char *format = NULL;\n \tstruct option options[] = {\n \t\tOPT_CMDMODE('l', \"list\", &cmdmode, N_(\"list tag names\"), 'l'),\n \t\t{ OPTION_INTEGER, 'n', NULL, &filter.lines, N_(\"n\"),\n@@ -361,6 +364,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)\n \t\t\tOPTION_CALLBACK, 0, \"points-at\", &filter.points_at, N_(\"object\"),\n \t\t\tN_(\"print only tags of the object\"), 0, parse_opt_object_name\n \t\t},\n+\t\tOPT_STRING(  0 , \"format\", &format, N_(\"format\"), N_(\"format to use for the output\")),\n \t\tOPT_END()\n \t};\n \n@@ -401,7 +405,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)\n \t\t\trun_column_filter(colopts, &copts);\n \t\t}\n \t\tfilter.name_patterns = argv;\n-\t\tret = list_tags(&filter, sorting);\n+\t\tret = list_tags(&filter, sorting, format);\n \t\tif (column_active(colopts))\n \t\t\tstop_column_filter();\n \t\treturn ret;\ndiff --git a/t/t7004-tag.sh b/t/t7004-tag.sh\nindex 84153ef..8987fb1 100755\n--- a/t/t7004-tag.sh\n+++ b/t/t7004-tag.sh\n@@ -1519,4 +1519,16 @@ EOF\"\n \ttest_cmp expect actual\n '\n \n+test_expect_success '--format should list tags as per format given' '\n+\tcat >expect <<-\\EOF &&\n+\trefname : refs/tags/foo1.10\n+\trefname : refs/tags/foo1.3\n+\trefname : refs/tags/foo1.6\n+\trefname : refs/tags/foo1.6-rc1\n+\trefname : refs/tags/foo1.6-rc2\n+\tEOF\n+\tgit tag -l --format=\"refname : %(refname)\" \"foo*\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_done\n-- \n2.5.0\n"},{"id":"269136","messageId":"1441131994-13508-14-git-send-email-Karthik.188@gmail.com","threadId":"40244","inReplyTo":"1441131994-13508-1-git-send-email-Karthik.188@gmail.com","subject":"[PATCH v15 13/13] tag.c: implement '--merged' and '--no-merged' options","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-01T18:26:34Z","receivedAt":"2015-09-01T18:26:34Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Use 'ref-filter' APIs to implement the '--merged' and '--no-merged'\noptions into 'tag.c'. The '--merged' option lets the user to only list\ntags merged into the named commit. The '--no-merged' option lets the\nuser to only list tags not merged into the named commit.  If no object\nis provided it assumes HEAD as the object.\n\nAdd documentation and tests for the same.\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n Documentation/git-tag.txt |  7 ++++++-\n builtin/tag.c             |  6 +++++-\n t/t7004-tag.sh            | 27 +++++++++++++++++++++++++++\n 3 files changed, 38 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/git-tag.txt b/Documentation/git-tag.txt\nindex 0c7f4e6..3803bf7 100644\n--- a/Documentation/git-tag.txt\n+++ b/Documentation/git-tag.txt\n@@ -14,7 +14,7 @@ SYNOPSIS\n 'git tag' -d <tagname>...\n 'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>]\n \t[--column[=<options>] | --no-column] [--create-reflog] [--sort=<key>]\n-\t[--format=<format>] [<pattern>...]\n+\t[--format=<format>] [--[no-]merged [<commit>]] [<pattern>...]\n 'git tag' -v <tagname>...\n \n DESCRIPTION\n@@ -165,6 +165,11 @@ This option is only applicable when listing tags without annotation lines.\n \tthat of linkgit:git-for-each-ref[1].  When unspecified,\n \tdefaults to `%(refname:short)`.\n \n+--[no-]merged [<commit>]::\n+\tOnly list tags whose tips are reachable, or not reachable\n+\tif '--no-merged' is used, from the specified commit ('HEAD'\n+\tif not specified).\n+\n CONFIGURATION\n -------------\n By default, 'git tag' in sign-with-default mode (-s) will use your\ndiff --git a/builtin/tag.c b/builtin/tag.c\nindex dc4494d..9fa1400 100644\n--- a/builtin/tag.c\n+++ b/builtin/tag.c\n@@ -23,7 +23,7 @@ static const char * const git_tag_usage[] = {\n \tN_(\"git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>]\"),\n \tN_(\"git tag -d <tagname>...\"),\n \tN_(\"git tag -l [-n[<num>]] [--contains <commit>] [--points-at <object>]\"\n-\t\t\"\\n\\t\\t[--format=<format>] [<pattern>...]\"),\n+\t\t\"\\n\\t\\t[--format=<format>] [--[no-]merged [<commit>]] [<pattern>...]\"),\n \tN_(\"git tag -v <tagname>...\"),\n \tNULL\n };\n@@ -358,6 +358,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)\n \t\tOPT_COLUMN(0, \"column\", &colopts, N_(\"show tag list in columns\")),\n \t\tOPT_CONTAINS(&filter.with_commit, N_(\"print only tags that contain the commit\")),\n \t\tOPT_WITH(&filter.with_commit, N_(\"print only tags that contain the commit\")),\n+\t\tOPT_MERGED(&filter, N_(\"print only tags that are merged\")),\n+\t\tOPT_NO_MERGED(&filter, N_(\"print only tags that are not merged\")),\n \t\tOPT_CALLBACK(0 , \"sort\", sorting_tail, N_(\"key\"),\n \t\t\t     N_(\"field name to sort on\"), &parse_opt_ref_sorting),\n \t\t{\n@@ -416,6 +418,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)\n \t\tdie(_(\"--contains option is only allowed with -l.\"));\n \tif (filter.points_at.nr)\n \t\tdie(_(\"--points-at option is only allowed with -l.\"));\n+\tif (filter.merge_commit)\n+\t\tdie(_(\"--merged and --no-merged option are only allowed with -l\"));\n \tif (cmdmode == 'd')\n \t\treturn for_each_tag_name(argv, delete_tag);\n \tif (cmdmode == 'v')\ndiff --git a/t/t7004-tag.sh b/t/t7004-tag.sh\nindex 8987fb1..3dd2f51 100755\n--- a/t/t7004-tag.sh\n+++ b/t/t7004-tag.sh\n@@ -1531,4 +1531,31 @@ test_expect_success '--format should list tags as per format given' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'setup --merged test tags' '\n+\tgit tag mergetest-1 HEAD~2 &&\n+\tgit tag mergetest-2 HEAD~1 &&\n+\tgit tag mergetest-3 HEAD\n+'\n+\n+test_expect_success '--merged cannot be used in non-list mode' '\n+\ttest_must_fail git tag --merged=mergetest-2 foo\n+'\n+\n+test_expect_success '--merged shows merged tags' '\n+\tcat >expect <<-\\EOF &&\n+\tmergetest-1\n+\tmergetest-2\n+\tEOF\n+\tgit tag -l --merged=mergetest-2 mergetest-* >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success '--no-merged show unmerged tags' '\n+\tcat >expect <<-\\EOF &&\n+\tmergetest-3\n+\tEOF\n+\tgit tag -l --no-merged=mergetest-2 mergetest-* >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_done\n-- \n2.5.0\n"},{"id":"269153","messageId":"xmqqegihrg6t.fsf@gitster.mtv.corp.google.com","threadId":"40244","inReplyTo":"1441131994-13508-6-git-send-email-Karthik.188@gmail.com","subject":"Re: [PATCH v15 05/13] ref-filter: implement an `align` atom","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-09-01T21:19:06Z","receivedAt":"2015-09-01T21:19:06Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Karthik Nayak <karthik.188@gmail.com> writes:\n\n> We have an `at_end` function for each element of the stack which is to\n> be called when the `end` atom is encountered. Using this we implement\n> the aling_handler() for the `align` atom, this aligns the final strbuf\n\nalign_handler().\n\n>  struct ref_formatting_stack {\n>  \tstruct ref_formatting_stack *prev;\n>  \tstruct strbuf output;\n> +\tvoid (*at_end)(struct ref_formatting_stack *stack);\n> +\tvoid *cb_data;\n>  };\n\ns/cb_data/at_end_data/ or something, as this is not really about a\nfunction callback.  Imagine a fictional future where you add a new\nfunctions at_middle---the readers cannot tell what cb_data is about\nat that point.\n\n> +static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)\n> +{\n> +\tstruct ref_formatting_stack *current = state->stack;\n> +\tstruct strbuf s = STRBUF_INIT;\n> +\n> +\tif (!current->at_end)\n> +\t\tdie(_(\"format: `end` atom used without a supporting atom\"));\n\nNot a show-stopper, but we may need some wordsmithing for \"a\nsupporting atom\" here; an end-user would not know what it is.\n\n> +\tcurrent->at_end(current);\n> +\n> +\t/*\n> +\t * Perform quote formatting when the stack element is that of\n> +\t * a modifier atom and right above the first stack element.\n> +\t */\n> +\tif (!state->stack->prev->prev) {\n> +\t\tquote_formatting(&s, current->output.buf, state->quote_style);\n> +\t\tstrbuf_swap(&current->output, &s);\n> +\t}\n> +\tstrbuf_release(&s);\n> +\tpop_stack_element(&state->stack);\n> +}\n\nNice.\n\n> @@ -687,6 +748,7 @@ static void populate_value(struct ref_array_item *ref)\n>  \t\tint deref = 0;\n>  \t\tconst char *refname;\n>  \t\tconst char *formatp;\n> +\t\tconst char *valp;\n>  \t\tstruct branch *branch = NULL;\n>  \n>  \t\tv->handler = append_atom;\n> @@ -754,6 +816,42 @@ static void populate_value(struct ref_array_item *ref)\n>  \t\t\telse\n>  \t\t\t\tv->s = \" \";\n>  \t\t\tcontinue;\n> +\t\t} else if (skip_prefix(name, \"align\", &valp)) {\n\nThis looked as if you are willing to take %(align) in addition to\n%(align:...), but...\n\n> +\t\t\tstruct align *align = &v->align;\n> +\t\t\tstruct strbuf **s;\n> +\n> +\t\t\tif (valp[0] != ':')\n> +\t\t\t\tdie(_(\"format: usage %%(align:<width>,<position>)\"));\n\n... apparently that is not what is happening.  Why not skip \"align:\"\nwith colon as the prefix, then?\n\n> +\t\t\telse\n> +\t\t\t\tvalp++;\n> +\t\t\ts = strbuf_split_str(valp, ',', 0);\n> +\n> +\t\t\t/* If the position is given trim the ',' from the first strbuf */\n> +\t\t\tif (s[1])\n> +\t\t\t\tstrbuf_setlen(s[0], s[0]->len - 1);\n> +\n> +\t\t\tif (strtoul_ui(s[0]->buf, 10, &align->width))\n> +\t\t\t\tdie(_(\"positive width expected align:%s\"), s[0]->buf);\n> +\n> +\t\t\tif (!s[1])\n> +\t\t\t\talign->position = ALIGN_LEFT;\n> +\t\t\telse if (!strcmp(s[1]->buf, \"left\"))\n> +\t\t\t\talign->position = ALIGN_LEFT;\n> +\t\t\telse if (!strcmp(s[1]->buf, \"right\"))\n> +\t\t\t\talign->position = ALIGN_RIGHT;\n> +\t\t\telse if (!strcmp(s[1]->buf, \"middle\"))\n> +\t\t\t\talign->position = ALIGN_MIDDLE;\n> +\t\t\telse\n> +\t\t\t\tdie(_(\"improper format entered align:%s\"), s[1]->buf);\n\nThis does not reject %(align:40,left,junk), no?  Before \"s[1] does\nnot exist so default to left align\", you would want\n\n\tif (s[2])\n\t\tdie(\"align:width,position followed by garbage: ,%s\", s[2]->buf);\n\nI have a few observations; these are not necessarily we would want\nto change in the scope of this series, though.\n\n - The design of strbuf_split_buf API feels screwy.  A variant of\n   this function that strips the terminator at the end would be what\n   most callers would want.  Granted, leaving the terminator in the\n   resulting buffer does let the caller tell if the input ended with\n   an incomplete line that lacked the final terminator, but for all\n   s[i] for 0 <= i < N-1 where s[N] is the first element that is\n   NULL, they must end with the terminator---otherwise the elements\n   would not have split into the array in the first place.  \"By\n   keeping the terminator, you can tell which one of the possible\n   terminators was used\" could be a valid rationale for the API if\n   the function allowed more than one terminators, but that does not\n   apply here, either.\n\n - I would have expected the above code to look more like this:\n\n\twidth = -1; position = ALIGN_LEFT;\n\ts = strbuf_split_str(valp, ',', 0);\n\twhile (*s) {\n\t\tif (s[1])\n                \tstrbuf_setlen(*s, *s->len - 1);\n\t\tif (!strtoul_ui(*s->buf, 10, &width))\n                \t; /* parsed width successfully */\n                else if (!strcmp(*s->buf, \"left\"))\n                \tposition = ALIGN_LEFT;\n\t\telse if ...\n\t\telse\n                \tdie(\"unknown parameter: %s\", *s->buf);\n\t\ts++;\t\t\t\n\t}\n\tif (width < 0)\n\t\t... perhaps set to the default width, or\n                ... call die() complaining that you did not see\n                ... an explicit width specified\n\n   Doing the code that way, it would be more obvious that a way to\n   extend the parser to accept forms like\n\n\t%(align:width=40,position=left)\n\n   is by adding \"keyword=value\" parser before the fallbacks for\n   short-hand, i.e. \"if looks like number\" and everything else.\n        \n"},{"id":"269154","messageId":"xmqqa8t5rfng.fsf@gitster.mtv.corp.google.com","threadId":"40244","inReplyTo":"1441131994-13508-7-git-send-email-Karthik.188@gmail.com","subject":"Re: [PATCH v15 06/13] ref-filter: add option to filter out tags, branches and remotes","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-09-01T21:30:43Z","receivedAt":"2015-09-01T21:30:43Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Karthik Nayak <karthik.188@gmail.com> writes:\n\n> +\tif (!filter->kind)\n>  \t\tdie(\"filter_refs: invalid type\");\n> +\telse {\n> +\t\tif (filter->kind == FILTER_REFS_BRANCHES)\n> +\t\t\tret = for_each_fullref_in(\"refs/heads/\", ref_filter_handler, &ref_cbdata, broken);\n> +\t\telse if (filter->kind == FILTER_REFS_REMOTES)\n> +\t\t\tret = for_each_fullref_in(\"refs/remotes/\", ref_filter_handler, &ref_cbdata, broken);\n> +\t\telse if (filter->kind == FILTER_REFS_TAGS)\n> +\t\t\tret = for_each_fullref_in(\"refs/tags/\", ref_filter_handler, &ref_cbdata, broken);\n> +\t\telse if (filter->kind & FILTER_REFS_ALL)\n> +\t\t\tret = for_each_fullref_in(\"\", ref_filter_handler, &ref_cbdata, broken);\n\nThis if/else if/else if/ cascade and ...\n\n> +\t\tif (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))\n> +\t\t\thead_ref(ref_filter_handler, &ref_cbdata);\n> +\t}\n> +\n>  \n>  \t/*  Filters that need revision walking */\n>  \tif (filter->merge_commit)\n> diff --git a/ref-filter.h b/ref-filter.h\n> index 45026d0..0913ba9 100644\n> --- a/ref-filter.h\n> +++ b/ref-filter.h\n> @@ -13,8 +13,15 @@\n>  #define QUOTE_PYTHON 4\n>  #define QUOTE_TCL 8\n>  \n> -#define FILTER_REFS_INCLUDE_BROKEN 0x1\n> -#define FILTER_REFS_ALL 0x2\n> +#define FILTER_REFS_INCLUDE_BROKEN 0x0001\n> +#define FILTER_REFS_TAGS           0x0002\n> +#define FILTER_REFS_BRANCHES       0x0004\n> +#define FILTER_REFS_REMOTES        0x0008\n> +#define FILTER_REFS_OTHERS         0x0010\n\n... the bit assignment here conceptually do not mesh well with each\nother.  These bits look as if I can ask for both tags and branches\nby passing 0x0006, and if the code above were\n\n\tempty the result set;\n\tif (filter->kind & FILTER_REFS_BRANCHES)\n        \tadd in things from refs/heads/ to the result set;\n\tif (filter->kind & FILTER_REFS_TAGS)                \n        \tadd in things from refs/tags/ to the result set;\n\t...\n\nwithout \"else if\", that would conceptually make sense.\n\nAlternatively, if the code does not (and will not ever) support such\nan arbitrary mixing of bits and intends to only allow \"one of\nBRANCHES, REMOTES, TAGS or ALL, and no other choice\", then it is\nwrong to pretend as if they can be mixed by defining the constant\nwith values with non-overlapping bit patterns.  If you defined these\nconstants as\n\n#define FILTER_REFS_TAGS           100\n#define FILTER_REFS_BRANCHES       101\n#define FILTER_REFS_REMOTES        102\n#define FILTER_REFS_OTHERS         103\n\nthen nobody would be think that the function can collect both tags\nand branches by passing FILTER_REFS_TAGS|FILTER_REFS_BRANCHES.\n\nThe former, i.e. keep the bits distinct and allow them to be OR'ed\ntogether by updating the code to allow such callers, would be more\npreferrable, of course.\n"},{"id":"269185","messageId":"CAOLa=ZQQia58_9c+KDh9mZ_UdcpUn96yuPOdABYvs1gno04CmQ@mail.gmail.com","threadId":"40244","inReplyTo":"xmqqa8t5rfng.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v15 06/13] ref-filter: add option to filter out tags, branches and remotes","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-02T01:27:48Z","receivedAt":"2015-09-02T01:27:48Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"On Wed, Sep 2, 2015 at 3:00 AM, Junio C Hamano <gitster@pobox.com> wrote:\n> Karthik Nayak <karthik.188@gmail.com> writes:\n>\n>> +     if (!filter->kind)\n>>               die(\"filter_refs: invalid type\");\n>> +     else {\n>> +             if (filter->kind == FILTER_REFS_BRANCHES)\n>> +                     ret = for_each_fullref_in(\"refs/heads/\", ref_filter_handler, &ref_cbdata, broken);\n>> +             else if (filter->kind == FILTER_REFS_REMOTES)\n>> +                     ret = for_each_fullref_in(\"refs/remotes/\", ref_filter_handler, &ref_cbdata, broken);\n>> +             else if (filter->kind == FILTER_REFS_TAGS)\n>> +                     ret = for_each_fullref_in(\"refs/tags/\", ref_filter_handler, &ref_cbdata, broken);\n>> +             else if (filter->kind & FILTER_REFS_ALL)\n>> +                     ret = for_each_fullref_in(\"\", ref_filter_handler, &ref_cbdata, broken);\n>\n> This if/else if/else if/ cascade and ...\n\nDid you notice the \"==\" for others and \"&\" for the ALL?\n\n>\n>> +             if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))\n>> +                     head_ref(ref_filter_handler, &ref_cbdata);\n>> +     }\n>> +\n>>\n>>       /*  Filters that need revision walking */\n>>       if (filter->merge_commit)\n>> diff --git a/ref-filter.h b/ref-filter.h\n>> index 45026d0..0913ba9 100644\n>> --- a/ref-filter.h\n>> +++ b/ref-filter.h\n>> @@ -13,8 +13,15 @@\n>>  #define QUOTE_PYTHON 4\n>>  #define QUOTE_TCL 8\n>>\n>> -#define FILTER_REFS_INCLUDE_BROKEN 0x1\n>> -#define FILTER_REFS_ALL 0x2\n>> +#define FILTER_REFS_INCLUDE_BROKEN 0x0001\n>> +#define FILTER_REFS_TAGS           0x0002\n>> +#define FILTER_REFS_BRANCHES       0x0004\n>> +#define FILTER_REFS_REMOTES        0x0008\n>> +#define FILTER_REFS_OTHERS         0x0010\n>\n> ... the bit assignment here conceptually do not mesh well with each\n> other.  These bits look as if I can ask for both tags and branches\n> by passing 0x0006, and if the code above were\n>\n>         empty the result set;\n>         if (filter->kind & FILTER_REFS_BRANCHES)\n>                 add in things from refs/heads/ to the result set;\n>         if (filter->kind & FILTER_REFS_TAGS)\n>                 add in things from refs/tags/ to the result set;\n>         ...\n>\n> without \"else if\", that would conceptually make sense.\n>\n> Alternatively, if the code does not (and will not ever) support such\n> an arbitrary mixing of bits and intends to only allow \"one of\n> BRANCHES, REMOTES, TAGS or ALL, and no other choice\", then it is\n> wrong to pretend as if they can be mixed by defining the constant\n> with values with non-overlapping bit patterns.  If you defined these\n> constants as\n\nHmm, The code does support mixing if you see, whenever we mix and match\nthese bits (consider 0x0006) we satisfy the ` else if (filter->kind &\nFILTER_REFS_ALL)`\ncondition. Which would then go through the entire set of refs and pick\nonly refs we\nneed using filter_ref_kind(). This may seem a little confusing but this avoids\nref type filtering when we do not mix bits up.\n\n>\n> #define FILTER_REFS_TAGS           100\n> #define FILTER_REFS_BRANCHES       101\n> #define FILTER_REFS_REMOTES        102\n> #define FILTER_REFS_OTHERS         103\n>\n> then nobody would be think that the function can collect both tags\n> and branches by passing FILTER_REFS_TAGS|FILTER_REFS_BRANCHES.\n>\n> The former, i.e. keep the bits distinct and allow them to be OR'ed\n> together by updating the code to allow such callers, would be more\n> preferrable, of course.\n>\n\nJust to confirm, I even changed the function call of for-each-ref to\n    filter_refs(&array, &filter, FILTER_REFS_BRANCHES |\nFILTER_REFS_TAGS | FILTER_REFS_INCLUDE_BROKEN);\nand it printed only \"refs/heads/\" and \"refs/tags/\".\nThanks :)\n\n-- \nRegards,\nKarthik Nayak\n"},{"id":"269187","messageId":"xmqqoahlpibt.fsf@gitster.mtv.corp.google.com","threadId":"40244","inReplyTo":"CAOLa=ZQQia58_9c+KDh9mZ_UdcpUn96yuPOdABYvs1gno04CmQ@mail.gmail.com","subject":"Re: [PATCH v15 06/13] ref-filter: add option to filter out tags, branches and remotes","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-09-02T04:15:50Z","receivedAt":"2015-09-02T04:15:50Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Karthik Nayak <karthik.188@gmail.com> writes:\n\n>>> +             if (filter->kind == FILTER_REFS_BRANCHES)\n>>> + ret = for_each_fullref_in(\"refs/heads/\", ref_filter_handler,\n>>> &ref_cbdata, broken);\n>>> +             else if (filter->kind == FILTER_REFS_REMOTES)\n>>> + ret = for_each_fullref_in(\"refs/remotes/\", ref_filter_handler,\n>>> &ref_cbdata, broken);\n>>> +             else if (filter->kind == FILTER_REFS_TAGS)\n>>> + ret = for_each_fullref_in(\"refs/tags/\", ref_filter_handler,\n>>> &ref_cbdata, broken);\n>>> +             else if (filter->kind & FILTER_REFS_ALL)\n>>> + ret = for_each_fullref_in(\"\", ref_filter_handler, &ref_cbdata,\n>>> broken);\n>>\n>> This if/else if/else if/ cascade and ...\n>\n> Did you notice the \"==\" for others and \"&\" for the ALL?\n\nI didn't.  Thanks for pointing it out.\n\nSo the point of the earlier part of the cascade is to optimize for\ncommon cases?  If that is the case, it probably deserves some\ncommenting.  I also suspect that a table-based control might be\neasier to maintain, but that kind of change might fall into the\ncategory of premature optimization.\n"},{"id":"269194","messageId":"vpq613tusat.fsf@anie.imag.fr","threadId":"40244","inReplyTo":"1441131994-13508-6-git-send-email-Karthik.188@gmail.com","subject":"Re: [PATCH v15 05/13] ref-filter: implement an `align` atom","fromName":"Matthieu Moy","fromEmail":"matthieu.moy@grenoble-inp.fr","sentAt":"2015-09-02T08:41:30Z","receivedAt":"2015-09-02T08:41:30Z","isPatch":true,"sender":{"key":"matthieu.moy@grenoble-inp.fr","avatar":"https://gravatar.com/avatar/72c8a2705971a25dfaff23cece15130d405685845d911aedd5667ace277f3fc5?d=mp&s=160"},"body":"Karthik Nayak <karthik.188@gmail.com> writes:\n\n> +test_expect_success 'alignment with format quote' '\n> +\tcat >expect <<-EOF &&\n> +\trefname is ${sq}           ${sq}\\\\${sq}${sq}master${sq}\\\\${sq}${sq}           ${sq}|\n> +\trefname is ${sq}            ${sq}\\\\${sq}${sq}side${sq}\\\\${sq}${sq}            ${sq}|\n> +\trefname is ${sq}          ${sq}\\\\${sq}${sq}odd/spot${sq}\\\\${sq}${sq}          ${sq}|\n> +\trefname is ${sq}         ${sq}\\\\${sq}${sq}double-tag${sq}\\\\${sq}${sq}         ${sq}|\n> +\trefname is ${sq}            ${sq}\\\\${sq}${sq}four${sq}\\\\${sq}${sq}            ${sq}|\n> +\trefname is ${sq}            ${sq}\\\\${sq}${sq}one${sq}\\\\${sq}${sq}             ${sq}|\n> +\trefname is ${sq}         ${sq}\\\\${sq}${sq}signed-tag${sq}\\\\${sq}${sq}         ${sq}|\n> +\trefname is ${sq}           ${sq}\\\\${sq}${sq}three${sq}\\\\${sq}${sq}            ${sq}|\n> +\trefname is ${sq}            ${sq}\\\\${sq}${sq}two${sq}\\\\${sq}${sq}             ${sq}|\n> +\tEOF\n> +\tgit for-each-ref --shell --format=\"refname is %(align:30,middle)${sq}%(refname:short)${sq}%(end)|\" >actual &&\n> +\ttest_cmp expect actual\n> +'\n\nSomeone (Eric IIRC) suggested using double-quotes around the last\nargument of test_expect_success. Even though I'm the one who suggested\nthis ${sq}, I have to agree with this suggestion. The result looks like\n\ntest_expect_success 'alignment with quote' \"\n\tcat >expect <<-\\EOF &&\n\trefname is '               '\\\\'''master'\\\\''\n\t...\n\"\n\nBecause you used \"\" at the toplevel, ' is not a special character\nanymore. You do have to be careful with \\\\ though, but adding before EOF\nas I did should do the trick. Untested.\n\nYou don't have test for nested alignment with quotes. I think it\ndeserves to be tested, if only to cast in stone that the current\nbehavior is your intention.\n\nPerhaps just adding --shell to the test below would be OK to avoid the\nproliferation of tests :\n\n> +test_expect_success 'nested alignment' '\n> +\tcat >expect <<-\\EOF &&\n> +\t|         master               |\n> +\t|           side               |\n> +\t|       odd/spot               |\n> +\t|     double-tag               |\n> +\t|           four               |\n> +\t|            one               |\n> +\t|     signed-tag               |\n> +\t|          three               |\n> +\t|            two               |\n> +\tEOF\n> +\tgit for-each-ref --format=\"|%(align:30,left)%(align:15,right)%(refname:short)%(end)%(end)|\" >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n>  test_done\n\n-- \nMatthieu Moy\nhttp://www-verimag.imag.fr/~moy/\n"},{"id":"269195","messageId":"vpqvbbttdjm.fsf@anie.imag.fr","threadId":"40244","inReplyTo":"1441131994-13508-6-git-send-email-Karthik.188@gmail.com","subject":"Re: [PATCH v15 05/13] ref-filter: implement an `align` atom","fromName":"Matthieu Moy","fromEmail":"matthieu.moy@grenoble-inp.fr","sentAt":"2015-09-02T08:45:33Z","receivedAt":"2015-09-02T08:45:33Z","isPatch":true,"sender":{"key":"matthieu.moy@grenoble-inp.fr","avatar":"https://gravatar.com/avatar/72c8a2705971a25dfaff23cece15130d405685845d911aedd5667ace277f3fc5?d=mp&s=160"},"body":"Karthik Nayak <karthik.188@gmail.com> writes:\n\n> --- a/ref-filter.c\n> +++ b/ref-filter.c\n\n> @@ -163,9 +174,28 @@ static void quote_formatting(struct strbuf *s, const char *str, int quote_style)\n>  \t}\n>  }\n>  \n> +static void align_handler(struct ref_formatting_stack *stack)\n\nPerhaps name it end_align_handler, to make the difference with\nalign_atom_handler clear.\n\nAlso, I think moving this function to be right next to\nalign_atom_handler in the code would make this more readable.\n\n> @@ -754,6 +816,42 @@ static void populate_value(struct ref_array_item *ref)\n>  \t\t\telse\n>  \t\t\t\tv->s = \" \";\n>  \t\t\tcontinue;\n> +\t\t} else if (skip_prefix(name, \"align\", &valp)) {\n> +\t\t\tstruct align *align = &v->align;\n> +\t\t\tstruct strbuf **s;\n> +\n> +\t\t\tif (valp[0] != ':')\n> +\t\t\t\tdie(_(\"format: usage %%(align:<width>,<position>)\"));\n> +\t\t\telse\n> +\t\t\t\tvalp++;\n\nI agree with Junio that skip_prefix(name, \"align:\" ...) is simpler for\nthe same thing.\n\n> +\tif (state.stack->prev)\n> +\t\tdie(_(\"format: `end` atom missing\"));\n\nPerhaps spell it %(end) instead of just end.\n\n-- \nMatthieu Moy\nhttp://www-verimag.imag.fr/~moy/\n"},{"id":"269196","messageId":"vpq8u8ptcjq.fsf@anie.imag.fr","threadId":"40244","inReplyTo":"1441131994-13508-8-git-send-email-Karthik.188@gmail.com","subject":"Re: [PATCH v15 07/13] ref-filter: add support for %(contents:lines=X)","fromName":"Matthieu Moy","fromEmail":"matthieu.moy@grenoble-inp.fr","sentAt":"2015-09-02T09:07:05Z","receivedAt":"2015-09-02T09:07:05Z","isPatch":true,"sender":{"key":"matthieu.moy@grenoble-inp.fr","avatar":"https://gravatar.com/avatar/72c8a2705971a25dfaff23cece15130d405685845d911aedd5667ace277f3fc5?d=mp&s=160"},"body":"Karthik Nayak <karthik.188@gmail.com> writes:\n\n> --- a/builtin/tag.c\n> +++ b/builtin/tag.c\n> @@ -185,6 +185,10 @@ static enum contains_result contains(struct commit *candidate,\n>  \treturn contains_test(candidate, want);\n>  }\n>  \n> +/*\n> + * Currently modified and used in ref-filter as append_lines(), will\n> + * eventually be removed as we port tag.c to use ref-filter APIs.\n> + */\n>  static void show_tag_lines(const struct object_id *oid, int lines)\n\nI would rather have one \"cut and paste\" patch followed by a \"modify and\nuse\" patch for review.\n\nAs-is, reading the patch doesn't tell me what change you did.\n\nThat said, I did get this information in the interdiff, so I won't\ninsist on that.\n\n> +static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)\n> +{\n> +\tint i;\n> +\tconst char *sp, *eol;\n> +\tsize_t len;\n> +\n> +\tif ((sp = strstr(buf, \"\\n\\n\")) && (sp <= buf + size))\n> +\t\tsize += 2;\n\nWhy is this \"size += 2\" needed?\n\n-- \nMatthieu Moy\nhttp://www-verimag.imag.fr/~moy/\n"},{"id":"269197","messageId":"vpq4mjdtcfr.fsf@anie.imag.fr","threadId":"40244","inReplyTo":"1441131994-13508-12-git-send-email-Karthik.188@gmail.com","subject":"Re: [PATCH v15 11/13] tag.c: use 'ref-filter' APIs","fromName":"Matthieu Moy","fromEmail":"matthieu.moy@grenoble-inp.fr","sentAt":"2015-09-02T09:09:28Z","receivedAt":"2015-09-02T09:09:28Z","isPatch":true,"sender":{"key":"matthieu.moy@grenoble-inp.fr","avatar":"https://gravatar.com/avatar/72c8a2705971a25dfaff23cece15130d405685845d911aedd5667ace277f3fc5?d=mp&s=160"},"body":"Karthik Nayak <karthik.188@gmail.com> writes:\n\n> +\tif (filter->lines)\n> +\t\tformat = to_free = xstrfmt(\"%%(align:15,left)%%(refname:short)%%(end) %%(contents:lines=%d)\",\n> +\t\t\t\t\t   filter->lines);\n> +\telse\n> +\t\tformat = \"%(refname:short)\";\n> +\n> +\tverify_ref_format(format);\n> +\tfilter_refs(&array, filter, FILTER_REFS_TAGS);\n> +\tref_array_sort(sorting, &array);\n> +\n> +\tfor (i = 0; i < array.nr; i++)\n> +\t\tshow_ref_array_item(array.items[i], format, 0);\n\nNice :-).\n\n(I'd cut the string argument to xstrfmt after \"%(end)\" to avoid long\nline)\n\n-- \nMatthieu Moy\nhttp://www-verimag.imag.fr/~moy/\n"},{"id":"269198","messageId":"CAOLa=ZQ0nyaQmvE3suT6hh0jO3s_iYxFLjSuLUY4STneMFJf9Q@mail.gmail.com","threadId":"40244","inReplyTo":"xmqqegihrg6t.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v15 05/13] ref-filter: implement an `align` atom","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-02T11:51:06Z","receivedAt":"2015-09-02T11:51:06Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"On Wed, Sep 2, 2015 at 2:49 AM, Junio C Hamano <gitster@pobox.com> wrote:\n> Karthik Nayak <karthik.188@gmail.com> writes:\n>\n>> We have an `at_end` function for each element of the stack which is to\n>> be called when the `end` atom is encountered. Using this we implement\n>> the aling_handler() for the `align` atom, this aligns the final strbuf\n>\n> align_handler().\n\nWill change.\n\n>\n>>  struct ref_formatting_stack {\n>>       struct ref_formatting_stack *prev;\n>>       struct strbuf output;\n>> +     void (*at_end)(struct ref_formatting_stack *stack);\n>> +     void *cb_data;\n>>  };\n>\n> s/cb_data/at_end_data/ or something, as this is not really about a\n> function callback.  Imagine a fictional future where you add a new\n> functions at_middle---the readers cannot tell what cb_data is about\n> at that point.\n>\n\nMakes sense will change.\n\n>> +static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)\n>> +{\n>> +     struct ref_formatting_stack *current = state->stack;\n>> +     struct strbuf s = STRBUF_INIT;\n>> +\n>> +     if (!current->at_end)\n>> +             die(_(\"format: `end` atom used without a supporting atom\"));\n>\n> Not a show-stopper, but we may need some wordsmithing for \"a\n> supporting atom\" here; an end-user would not know what it is.\n>\n\nProbably something like \"format: `end` atom should only be\nused with modifier atoms\".\n\n>> +     current->at_end(current);\n>> +\n>> +     /*\n>> +      * Perform quote formatting when the stack element is that of\n>> +      * a modifier atom and right above the first stack element.\n>> +      */\n>> +     if (!state->stack->prev->prev) {\n>> +             quote_formatting(&s, current->output.buf, state->quote_style);\n>> +             strbuf_swap(&current->output, &s);\n>> +     }\n>> +     strbuf_release(&s);\n>> +     pop_stack_element(&state->stack);\n>> +}\n>\n> Nice.\n>\n>> @@ -687,6 +748,7 @@ static void populate_value(struct ref_array_item *ref)\n>>               int deref = 0;\n>>               const char *refname;\n>>               const char *formatp;\n>> +             const char *valp;\n>>               struct branch *branch = NULL;\n>>\n>>               v->handler = append_atom;\n>> @@ -754,6 +816,42 @@ static void populate_value(struct ref_array_item *ref)\n>>                       else\n>>                               v->s = \" \";\n>>                       continue;\n>> +             } else if (skip_prefix(name, \"align\", &valp)) {\n>\n> This looked as if you are willing to take %(align) in addition to\n> %(align:...), but...\n>\n>> +                     struct align *align = &v->align;\n>> +                     struct strbuf **s;\n>> +\n>> +                     if (valp[0] != ':')\n>> +                             die(_(\"format: usage %%(align:<width>,<position>)\"));\n>\n> ... apparently that is not what is happening.  Why not skip \"align:\"\n> with colon as the prefix, then?\n>\n\nCause we wanted to provide an error for usage of \"%(ailgn)\" without any\nsubvalues as such.\n\n>> +                     else\n>> +                             valp++;\n>> +                     s = strbuf_split_str(valp, ',', 0);\n>> +\n>> +                     /* If the position is given trim the ',' from the first strbuf */\n>> +                     if (s[1])\n>> +                             strbuf_setlen(s[0], s[0]->len - 1);\n>> +\n>> +                     if (strtoul_ui(s[0]->buf, 10, &align->width))\n>> +                             die(_(\"positive width expected align:%s\"), s[0]->buf);\n>> +\n>> +                     if (!s[1])\n>> +                             align->position = ALIGN_LEFT;\n>> +                     else if (!strcmp(s[1]->buf, \"left\"))\n>> +                             align->position = ALIGN_LEFT;\n>> +                     else if (!strcmp(s[1]->buf, \"right\"))\n>> +                             align->position = ALIGN_RIGHT;\n>> +                     else if (!strcmp(s[1]->buf, \"middle\"))\n>> +                             align->position = ALIGN_MIDDLE;\n>> +                     else\n>> +                             die(_(\"improper format entered align:%s\"), s[1]->buf);\n>\n> This does not reject %(align:40,left,junk), no?  Before \"s[1] does\n> not exist so default to left align\", you would want\n>\n>         if (s[2])\n>                 die(\"align:width,position followed by garbage: ,%s\", s[2]->buf);\n>\n\nYea we should probably do that.\n\n> I have a few observations; these are not necessarily we would want\n> to change in the scope of this series, though.\n>\n>  - The design of strbuf_split_buf API feels screwy.  A variant of\n>    this function that strips the terminator at the end would be what\n>    most callers would want.  Granted, leaving the terminator in the\n>    resulting buffer does let the caller tell if the input ended with\n>    an incomplete line that lacked the final terminator, but for all\n>    s[i] for 0 <= i < N-1 where s[N] is the first element that is\n>    NULL, they must end with the terminator---otherwise the elements\n>    would not have split into the array in the first place.  \"By\n>    keeping the terminator, you can tell which one of the possible\n>    terminators was used\" could be a valid rationale for the API if\n>    the function allowed more than one terminators, but that does not\n>    apply here, either.\n>\n>  - I would have expected the above code to look more like this:\n>\n>         width = -1; position = ALIGN_LEFT;\n>         s = strbuf_split_str(valp, ',', 0);\n>         while (*s) {\n>                 if (s[1])\n>                         strbuf_setlen(*s, *s->len - 1);\n>                 if (!strtoul_ui(*s->buf, 10, &width))\n>                         ; /* parsed width successfully */\n>                 else if (!strcmp(*s->buf, \"left\"))\n>                         position = ALIGN_LEFT;\n>                 else if ...\n>                 else\n>                         die(\"unknown parameter: %s\", *s->buf);\n>                 s++;\n>         }\n>         if (width < 0)\n>                 ... perhaps set to the default width, or\n>                 ... call die() complaining that you did not see\n>                 ... an explicit width specified\n>\n>    Doing the code that way, it would be more obvious that a way to\n>    extend the parser to accept forms like\n>\n>         %(align:width=40,position=left)\n>\n>    is by adding \"keyword=value\" parser before the fallbacks for\n>    short-hand, i.e. \"if looks like number\" and everything else.\n>\n\nI'll keep this in mind, probably work on this later after this series is done.\nThanks\n\n-- \nRegards,\nKarthik Nayak\n"},{"id":"269199","messageId":"CAOLa=ZSRZHWMRh759dGVd4hHebpnnCA60TZoXoY-iqPa=t-e0Q@mail.gmail.com","threadId":"40244","inReplyTo":"xmqqoahlpibt.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v15 06/13] ref-filter: add option to filter out tags, branches and remotes","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-02T12:48:35Z","receivedAt":"2015-09-02T12:48:35Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"On Wed, Sep 2, 2015 at 9:45 AM, Junio C Hamano <gitster@pobox.com> wrote:\n> Karthik Nayak <karthik.188@gmail.com> writes:\n>\n>>>> +             if (filter->kind == FILTER_REFS_BRANCHES)\n>>>> + ret = for_each_fullref_in(\"refs/heads/\", ref_filter_handler,\n>>>> &ref_cbdata, broken);\n>>>> +             else if (filter->kind == FILTER_REFS_REMOTES)\n>>>> + ret = for_each_fullref_in(\"refs/remotes/\", ref_filter_handler,\n>>>> &ref_cbdata, broken);\n>>>> +             else if (filter->kind == FILTER_REFS_TAGS)\n>>>> + ret = for_each_fullref_in(\"refs/tags/\", ref_filter_handler,\n>>>> &ref_cbdata, broken);\n>>>> +             else if (filter->kind & FILTER_REFS_ALL)\n>>>> + ret = for_each_fullref_in(\"\", ref_filter_handler, &ref_cbdata,\n>>>> broken);\n>>>\n>>> This if/else if/else if/ cascade and ...\n>>\n>> Did you notice the \"==\" for others and \"&\" for the ALL?\n>\n> I didn't.  Thanks for pointing it out.\n>\n> So the point of the earlier part of the cascade is to optimize for\n> common cases?  If that is the case, it probably deserves some\n> commenting.  I also suspect that a table-based control might be\n> easier to maintain, but that kind of change might fall into the\n> category of premature optimization.\n\nYes, thats the point.\nWill add a comment, thanks :)\n\n-- \nRegards,\nKarthik Nayak\n"},{"id":"269200","messageId":"CAOLa=ZRTDAD1wi8=Nz6uWfgS=Nsowf9CkDFsv+BC7b3L9w9Z=Q@mail.gmail.com","threadId":"40244","inReplyTo":"vpq613tusat.fsf@anie.imag.fr","subject":"Re: [PATCH v15 05/13] ref-filter: implement an `align` atom","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-02T12:51:29Z","receivedAt":"2015-09-02T12:51:29Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"On Wed, Sep 2, 2015 at 2:11 PM, Matthieu Moy\n<Matthieu.Moy@grenoble-inp.fr> wrote:\n> Karthik Nayak <karthik.188@gmail.com> writes:\n>\n>> +test_expect_success 'alignment with format quote' '\n>> +     cat >expect <<-EOF &&\n>> +     refname is ${sq}           ${sq}\\\\${sq}${sq}master${sq}\\\\${sq}${sq}           ${sq}|\n>> +     refname is ${sq}            ${sq}\\\\${sq}${sq}side${sq}\\\\${sq}${sq}            ${sq}|\n>> +     refname is ${sq}          ${sq}\\\\${sq}${sq}odd/spot${sq}\\\\${sq}${sq}          ${sq}|\n>> +     refname is ${sq}         ${sq}\\\\${sq}${sq}double-tag${sq}\\\\${sq}${sq}         ${sq}|\n>> +     refname is ${sq}            ${sq}\\\\${sq}${sq}four${sq}\\\\${sq}${sq}            ${sq}|\n>> +     refname is ${sq}            ${sq}\\\\${sq}${sq}one${sq}\\\\${sq}${sq}             ${sq}|\n>> +     refname is ${sq}         ${sq}\\\\${sq}${sq}signed-tag${sq}\\\\${sq}${sq}         ${sq}|\n>> +     refname is ${sq}           ${sq}\\\\${sq}${sq}three${sq}\\\\${sq}${sq}            ${sq}|\n>> +     refname is ${sq}            ${sq}\\\\${sq}${sq}two${sq}\\\\${sq}${sq}             ${sq}|\n>> +     EOF\n>> +     git for-each-ref --shell --format=\"refname is %(align:30,middle)${sq}%(refname:short)${sq}%(end)|\" >actual &&\n>> +     test_cmp expect actual\n>> +'\n>\n> Someone (Eric IIRC) suggested using double-quotes around the last\n> argument of test_expect_success. Even though I'm the one who suggested\n> this ${sq}, I have to agree with this suggestion. The result looks like\n>\n> test_expect_success 'alignment with quote' \"\n>         cat >expect <<-\\EOF &&\n>         refname is '               '\\\\'''master'\\\\''\n>         ...\n> \"\n>\n> Because you used \"\" at the toplevel, ' is not a special character\n> anymore. You do have to be careful with \\\\ though, but adding before EOF\n> as I did should do the trick. Untested.\n>\n\nOops! I missed that out, thanks for bringing that up again, will do it.\n\n> You don't have test for nested alignment with quotes. I think it\n> deserves to be tested, if only to cast in stone that the current\n> behavior is your intention.\n>\n> Perhaps just adding --shell to the test below would be OK to avoid the\n> proliferation of tests :\n>\n\nMakes sense will add it to the following test.\n\n>> +test_expect_success 'nested alignment' '\n>> +     cat >expect <<-\\EOF &&\n>> +     |         master               |\n>> +     |           side               |\n>> +     |       odd/spot               |\n>> +     |     double-tag               |\n>> +     |           four               |\n>> +     |            one               |\n>> +     |     signed-tag               |\n>> +     |          three               |\n>> +     |            two               |\n>> +     EOF\n>> +     git for-each-ref --format=\"|%(align:30,left)%(align:15,right)%(refname:short)%(end)%(end)|\" >actual &&\n>> +     test_cmp expect actual\n>> +'\n>> +\n>>  test_done\n>\n> --\n> Matthieu Moy\n> http://www-verimag.imag.fr/~moy/\n\n\n\n-- \nRegards,\nKarthik Nayak\n"},{"id":"269201","messageId":"CAOLa=ZQxWO6+_N6TKgZ09HVGiNNDy29p6kU56Y2TX8Y+kYTcsA@mail.gmail.com","threadId":"40244","inReplyTo":"vpqvbbttdjm.fsf@anie.imag.fr","subject":"Re: [PATCH v15 05/13] ref-filter: implement an `align` atom","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-02T13:12:20Z","receivedAt":"2015-09-02T13:12:20Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"On Wed, Sep 2, 2015 at 2:15 PM, Matthieu Moy\n<Matthieu.Moy@grenoble-inp.fr> wrote:\n> Karthik Nayak <karthik.188@gmail.com> writes:\n>\n>> --- a/ref-filter.c\n>> +++ b/ref-filter.c\n>\n>> @@ -163,9 +174,28 @@ static void quote_formatting(struct strbuf *s, const char *str, int quote_style)\n>>       }\n>>  }\n>>\n>> +static void align_handler(struct ref_formatting_stack *stack)\n>\n> Perhaps name it end_align_handler, to make the difference with\n> align_atom_handler clear.\n>\n> Also, I think moving this function to be right next to\n> align_atom_handler in the code would make this more readable.\n>\n\nwill do.\n\n>> @@ -754,6 +816,42 @@ static void populate_value(struct ref_array_item *ref)\n>>                       else\n>>                               v->s = \" \";\n>>                       continue;\n>> +             } else if (skip_prefix(name, \"align\", &valp)) {\n>> +                     struct align *align = &v->align;\n>> +                     struct strbuf **s;\n>> +\n>> +                     if (valp[0] != ':')\n>> +                             die(_(\"format: usage %%(align:<width>,<position>)\"));\n>> +                     else\n>> +                             valp++;\n>\n> I agree with Junio that skip_prefix(name, \"align:\" ...) is simpler for\n> the same thing.\n\nCorrect me if mistaken, but Junio wanted the first skip_prefix to\ncheck for \"align:\" rather than \"align\", but that would mean we don't have\nthe error printing.\n\nAre you sure we want to skip that?\n\n>\n>> +     if (state.stack->prev)\n>> +             die(_(\"format: `end` atom missing\"));\n>\n> Perhaps spell it %(end) instead of just end.\n>\n\nYes, will do\n\n-- \nRegards,\nKarthik Nayak\n"},{"id":"269202","messageId":"CAOLa=ZTB_uMvKjU=f9UgmvOmeZD4YtFL_Gv1+VvZAgDV-6WYDA@mail.gmail.com","threadId":"40244","inReplyTo":"vpq8u8ptcjq.fsf@anie.imag.fr","subject":"Re: [PATCH v15 07/13] ref-filter: add support for %(contents:lines=X)","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-02T14:16:59Z","receivedAt":"2015-09-02T14:16:59Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"On Wed, Sep 2, 2015 at 2:37 PM, Matthieu Moy\n<Matthieu.Moy@grenoble-inp.fr> wrote:\n> Karthik Nayak <karthik.188@gmail.com> writes:\n>\n>> --- a/builtin/tag.c\n>> +++ b/builtin/tag.c\n>> @@ -185,6 +185,10 @@ static enum contains_result contains(struct commit *candidate,\n>>       return contains_test(candidate, want);\n>>  }\n>>\n>> +/*\n>> + * Currently modified and used in ref-filter as append_lines(), will\n>> + * eventually be removed as we port tag.c to use ref-filter APIs.\n>> + */\n>>  static void show_tag_lines(const struct object_id *oid, int lines)\n>\n> I would rather have one \"cut and paste\" patch followed by a \"modify and\n> use\" patch for review.\n>\n> As-is, reading the patch doesn't tell me what change you did.\n>\n> That said, I did get this information in the interdiff, so I won't\n> insist on that.\n\nIts only borrowed slightly, so I don't really see the need for this.\nBut if you insist, we could do that .\n\n>\n>> +static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)\n>> +{\n>> +     int i;\n>> +     const char *sp, *eol;\n>> +     size_t len;\n>> +\n>> +     if ((sp = strstr(buf, \"\\n\\n\")) && (sp <= buf + size))\n>> +             size += 2;\n>\n> Why is this \"size += 2\" needed?\n>\n\nWe pass size as \"sublen + bodylen - siglen\" if there exists a \"\\n\\n\"\nbetween sublen and bodylen this is not accounted for. hence we\nadd 2 here.\n\n-- \nRegards,\nKarthik Nayak\n"},{"id":"269205","messageId":"xmqqd1y0q30g.fsf@gitster.mtv.corp.google.com","threadId":"40244","inReplyTo":"CAOLa=ZQ0nyaQmvE3suT6hh0jO3s_iYxFLjSuLUY4STneMFJf9Q@mail.gmail.com","subject":"Re: [PATCH v15 05/13] ref-filter: implement an `align` atom","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-09-02T15:01:19Z","receivedAt":"2015-09-02T15:01:19Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Karthik Nayak <karthik.188@gmail.com> writes:\n\n>>> +             die(_(\"format: `end` atom used without a supporting atom\"));\n>>\n>> Not a show-stopper, but we may need some wordsmithing for \"a\n>> supporting atom\" here; an end-user would not know what it is.\n>\n> Probably something like \"format: `end` atom should only be\n> used with modifier atoms\".\n\nBetween \"supporting\" and \"modifier\" I do not see much difference,\nthough.\n\n>>> +             } else if (skip_prefix(name, \"align\", &valp)) {\n>>\n>> This looked as if you are willing to take %(align) in addition to\n>> %(align:...), but...\n>>\n>>> +                     struct align *align = &v->align;\n>>> +                     struct strbuf **s;\n>>> +\n>>> +                     if (valp[0] != ':')\n>>> +                             die(_(\"format: usage %%(align:<width>,<position>)\"));\n>>\n>> ... apparently that is not what is happening.  Why not skip \"align:\"\n>> with colon as the prefix, then?\n>\n> Cause we wanted to provide an error for usage of \"%(ailgn)\" without any\n> subvalues as such.\n\nWouldn't it be something that would be caught in the same codepath\nas what catches %(unrecognized) in the format string?\n"},{"id":"269206","messageId":"CAOLa=ZQLG_HB0trodXPCb9n4x2FuH7h7-+KJYLEdNZSpd0PHsg@mail.gmail.com","threadId":"40244","inReplyTo":"xmqqd1y0q30g.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v15 05/13] ref-filter: implement an `align` atom","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-02T15:05:11Z","receivedAt":"2015-09-02T15:05:11Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"On Wed, Sep 2, 2015 at 8:31 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> Karthik Nayak <karthik.188@gmail.com> writes:\n>\n>>>> +             die(_(\"format: `end` atom used without a supporting atom\"));\n>>>\n>>> Not a show-stopper, but we may need some wordsmithing for \"a\n>>> supporting atom\" here; an end-user would not know what it is.\n>>\n>> Probably something like \"format: `end` atom should only be\n>> used with modifier atoms\".\n>\n> Between \"supporting\" and \"modifier\" I do not see much difference,\n> though.\n>\n\nI don't see how we could provide a better message, as %(end) atom\nwould be common to various atoms eventually.\n\n>>>> +             } else if (skip_prefix(name, \"align\", &valp)) {\n>>>\n>>> This looked as if you are willing to take %(align) in addition to\n>>> %(align:...), but...\n>>>\n>>>> +                     struct align *align = &v->align;\n>>>> +                     struct strbuf **s;\n>>>> +\n>>>> +                     if (valp[0] != ':')\n>>>> +                             die(_(\"format: usage %%(align:<width>,<position>)\"));\n>>>\n>>> ... apparently that is not what is happening.  Why not skip \"align:\"\n>>> with colon as the prefix, then?\n>>\n>> Cause we wanted to provide an error for usage of \"%(ailgn)\" without any\n>> subvalues as such.\n>\n> Wouldn't it be something that would be caught in the same codepath\n> as what catches %(unrecognized) in the format string?\n\nNo, since \"align\" is defined as an atom, in the valid_atom struct.\nChanging it to \"align:\" would work, but that seems a little inconsistent\nwith the other atoms.\n\nHence --format=\"%(align)foo%(end)\" would just result in the\n\"format: `end` atom used without a supporting atom\" error being\ndisplayed, hence confusing the user even more.\n\n-- \nRegards,\nKarthik Nayak\n"},{"id":"269207","messageId":"xmqq8u8oq2lq.fsf@gitster.mtv.corp.google.com","threadId":"40244","inReplyTo":"1441131994-13508-12-git-send-email-Karthik.188@gmail.com","subject":"Re: [PATCH v15 11/13] tag.c: use 'ref-filter' APIs","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-09-02T15:10:09Z","receivedAt":"2015-09-02T15:10:09Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Karthik Nayak <karthik.188@gmail.com> writes:\n\n> +\tif (filter->lines)\n> +\t\tformat = to_free = xstrfmt(\"%%(align:15,left)%%(refname:short)%%(end) %%(contents:lines=%d)\",\n> +\t\t\t\t\t   filter->lines);\n\nI recall hearing that you were more in favor of\n\n\t\"%(align:16)%(refname:short) %(end)%(contents:lines=4)\"\n\nsomewhere in the earlier discussions?\n"},{"id":"269209","messageId":"CAOLa=ZQXgVXSB9FqhXnc5npOrW8HVj97i0XhNeM-k-MJHR_xfw@mail.gmail.com","threadId":"40244","inReplyTo":"xmqq8u8oq2lq.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v15 11/13] tag.c: use 'ref-filter' APIs","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-02T15:40:15Z","receivedAt":"2015-09-02T15:40:15Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"On Wed, Sep 2, 2015 at 8:40 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> Karthik Nayak <karthik.188@gmail.com> writes:\n>\n>> +     if (filter->lines)\n>> +             format = to_free = xstrfmt(\"%%(align:15,left)%%(refname:short)%%(end) %%(contents:lines=%d)\",\n>> +                                        filter->lines);\n>\n> I recall hearing that you were more in favor of\n>\n>         \"%(align:16)%(refname:short) %(end)%(contents:lines=4)\"\n>\n> somewhere in the earlier discussions?\n\nI did, but Matthieu suggested that this would be better!\nCan't find a link to that.\n\n-- \nRegards,\nKarthik Nayak\n"},{"id":"269211","messageId":"xmqqy4goomdk.fsf@gitster.mtv.corp.google.com","threadId":"40244","inReplyTo":"CAOLa=ZQLG_HB0trodXPCb9n4x2FuH7h7-+KJYLEdNZSpd0PHsg@mail.gmail.com","subject":"Re: [PATCH v15 05/13] ref-filter: implement an `align` atom","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-09-02T15:45:59Z","receivedAt":"2015-09-02T15:45:59Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Karthik Nayak <karthik.188@gmail.com> writes:\n\n> On Wed, Sep 2, 2015 at 8:31 PM, Junio C Hamano <gitster@pobox.com> wrote:\n>> Karthik Nayak <karthik.188@gmail.com> writes:\n>>\n>>>>> +             die(_(\"format: `end` atom used without a supporting atom\"));\n>>>>\n>>>> Not a show-stopper, but we may need some wordsmithing for \"a\n>>>> supporting atom\" here; an end-user would not know what it is.\n>>>\n>>> Probably something like \"format: `end` atom should only be\n>>> used with modifier atoms\".\n>>\n>> Between \"supporting\" and \"modifier\" I do not see much difference,\n>> though.\n>\n> I don't see how we could provide a better message, as %(end) atom\n> would be common to various atoms eventually.\n\nI said \"not a show-stopper\" without giving a suggestion exactly\nbecause I didn't (and I still don't) think either you or I can come\nup with a good wording ;-).  That is why the message was Cc'ed to\nthe list for others to comment.\n\n>>> Cause we wanted to provide an error for usage of \"%(ailgn)\" without any\n>>> subvalues as such.\n>>\n>> Wouldn't it be something that would be caught in the same codepath\n>> as what catches %(unrecognized) in the format string?\n\nOne potential issue you have with prefix matching with \"align\" is\nthat you can never have a different atom whose name happens to begin\nwith that substring.  I think the best behaviour is for the higher\nlevel parser to recognize %(ATOM) and %(ATOM:anything) and nothing\nelse for all ATOM in the valid atoms registry.  That would prevent\n%(ATOMfoo) from being handled as part of handling ATOM.\n\nAnd make the code consider %(ATOM) form as a short-hand for %(ATOM:)\nthat does not have customizations.  Conceptually %(refname) is a\n%(refname:default).\n\nFor an atom like 'align' that does not have a reasonable default,\nthe parser for 'align' can notice that a required customization is\nmissing and give an error that is specific to 'align'.\n\nSo all calls in your code of the from\n\n\t... else if (skip_prefix(name, \"align\", &val)) {\n        \t...\n\nshould become a call to helper that does more than skip_prefix().\n\nint match_atom_name(const char *name, const char *atom_name, char **val)\n{\n\tchar *body\n\n       \tif (!skip_prefix(name, atom_name, &body))\n        \treturn 0; /* doesn't even begin with \"align\" */\n\tif (!body[0]) {\n        \t*val = NULL; /* %(align) and no customization */\n                return 1;\n\t}\n\tif (body[0] != ':')\n        \treturn 0; /* \"alignfoo\" is not \"align\" or \"align:...\" */\n\t*val = body + 1; /* \"align:val\" */\n        return 1;\n}\n"},{"id":"269213","messageId":"vpqsi6wom6q.fsf@anie.imag.fr","threadId":"40244","inReplyTo":"xmqqd1y0q30g.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v15 05/13] ref-filter: implement an `align` atom","fromName":"Matthieu Moy","fromEmail":"matthieu.moy@grenoble-inp.fr","sentAt":"2015-09-02T15:50:05Z","receivedAt":"2015-09-02T15:50:05Z","isPatch":true,"sender":{"key":"matthieu.moy@grenoble-inp.fr","avatar":"https://gravatar.com/avatar/72c8a2705971a25dfaff23cece15130d405685845d911aedd5667ace277f3fc5?d=mp&s=160"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> Karthik Nayak <karthik.188@gmail.com> writes:\n>\n>>>> +             } else if (skip_prefix(name, \"align\", &valp)) {\n>>>\n>>> This looked as if you are willing to take %(align) in addition to\n>>> %(align:...), but...\n>>>\n>>>> +                     struct align *align = &v->align;\n>>>> +                     struct strbuf **s;\n>>>> +\n>>>> +                     if (valp[0] != ':')\n>>>> +                             die(_(\"format: usage %%(align:<width>,<position>)\"));\n>>>\n>>> ... apparently that is not what is happening.  Why not skip \"align:\"\n>>> with colon as the prefix, then?\n>>\n>> Cause we wanted to provide an error for usage of \"%(ailgn)\" without any\n>> subvalues as such.\n>\n> Wouldn't it be something that would be caught in the same codepath\n> as what catches %(unrecognized) in the format string?\n\nAfter thinking about it, I agree with Karthik: if we get the same\ncodepath to complain about %(nosuchatom) and %(align), then we won't be\nable to provide an accurate error message. Or we would need to re-parse\nthe atom, notice that it's one we know about, i.e. redo what we're\nalready doing here.\n\n-- \nMatthieu Moy\nhttp://www-verimag.imag.fr/~moy/\n"},{"id":"269214","messageId":"vpqoahkom5a.fsf@anie.imag.fr","threadId":"40244","inReplyTo":"CAOLa=ZQxWO6+_N6TKgZ09HVGiNNDy29p6kU56Y2TX8Y+kYTcsA@mail.gmail.com","subject":"Re: [PATCH v15 05/13] ref-filter: implement an `align` atom","fromName":"Matthieu Moy","fromEmail":"matthieu.moy@grenoble-inp.fr","sentAt":"2015-09-02T15:50:57Z","receivedAt":"2015-09-02T15:50:57Z","isPatch":true,"sender":{"key":"matthieu.moy@grenoble-inp.fr","avatar":"https://gravatar.com/avatar/72c8a2705971a25dfaff23cece15130d405685845d911aedd5667ace277f3fc5?d=mp&s=160"},"body":"Karthik Nayak <karthik.188@gmail.com> writes:\n\n> On Wed, Sep 2, 2015 at 2:15 PM, Matthieu Moy\n>>> @@ -754,6 +816,42 @@ static void populate_value(struct ref_array_item *ref)\n>>>                       else\n>>>                               v->s = \" \";\n>>>                       continue;\n>>> +             } else if (skip_prefix(name, \"align\", &valp)) {\n>>> +                     struct align *align = &v->align;\n>>> +                     struct strbuf **s;\n>>> +\n>>> +                     if (valp[0] != ':')\n>>> +                             die(_(\"format: usage %%(align:<width>,<position>)\"));\n>>> +                     else\n>>> +                             valp++;\n>>\n>> I agree with Junio that skip_prefix(name, \"align:\" ...) is simpler for\n>> the same thing.\n>\n> Correct me if mistaken, but Junio wanted the first skip_prefix to\n> check for \"align:\" rather than \"align\", but that would mean we don't have\n> the error printing.\n>\n> Are you sure we want to skip that?\n\nSee my other message: you convinced me ;-).\n\n-- \nMatthieu Moy\nhttp://www-verimag.imag.fr/~moy/\n"},{"id":"269215","messageId":"CAOLa=ZQTKuHH4ktvTdCRM49cinTSeCbhd8ngVzcJh_eBHGk-iw@mail.gmail.com","threadId":"40244","inReplyTo":"xmqqy4goomdk.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v15 05/13] ref-filter: implement an `align` atom","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-02T16:09:18Z","receivedAt":"2015-09-02T16:09:18Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"On Wed, Sep 2, 2015 at 9:15 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> Karthik Nayak <karthik.188@gmail.com> writes:\n>\n>> On Wed, Sep 2, 2015 at 8:31 PM, Junio C Hamano <gitster@pobox.com> wrote:\n>>> Karthik Nayak <karthik.188@gmail.com> writes:\n>>>\n>>>>>> +             die(_(\"format: `end` atom used without a supporting atom\"));\n>>>>>\n>>>>> Not a show-stopper, but we may need some wordsmithing for \"a\n>>>>> supporting atom\" here; an end-user would not know what it is.\n>>>>\n>>>> Probably something like \"format: `end` atom should only be\n>>>> used with modifier atoms\".\n>>>\n>>> Between \"supporting\" and \"modifier\" I do not see much difference,\n>>> though.\n>>\n>> I don't see how we could provide a better message, as %(end) atom\n>> would be common to various atoms eventually.\n>\n> I said \"not a show-stopper\" without giving a suggestion exactly\n> because I didn't (and I still don't) think either you or I can come\n> up with a good wording ;-).  That is why the message was Cc'ed to\n> the list for others to comment.\n\nHaha okay.\n\n>\n>>>> Cause we wanted to provide an error for usage of \"%(ailgn)\" without any\n>>>> subvalues as such.\n>>>\n>>> Wouldn't it be something that would be caught in the same codepath\n>>> as what catches %(unrecognized) in the format string?\n>\n> One potential issue you have with prefix matching with \"align\" is\n> that you can never have a different atom whose name happens to begin\n> with that substring.  I think the best behaviour is for the higher\n> level parser to recognize %(ATOM) and %(ATOM:anything) and nothing\n> else for all ATOM in the valid atoms registry.  That would prevent\n> %(ATOMfoo) from being handled as part of handling ATOM.\n>\n> And make the code consider %(ATOM) form as a short-hand for %(ATOM:)\n> that does not have customizations.  Conceptually %(refname) is a\n> %(refname:default).\n>\n> For an atom like 'align' that does not have a reasonable default,\n> the parser for 'align' can notice that a required customization is\n> missing and give an error that is specific to 'align'.\n>\n> So all calls in your code of the from\n>\n>         ... else if (skip_prefix(name, \"align\", &val)) {\n>                 ...\n>\n> should become a call to helper that does more than skip_prefix().\n>\n\nThis is what Eric suggested and we agreed on working on a general\nsolution to this problem eventually, seems like you beat me to it :)\n\nWill incorporate this :)\n\n> int match_atom_name(const char *name, const char *atom_name, char **val)\n> {\n>         char *body\n>\n>         if (!skip_prefix(name, atom_name, &body))\n>                 return 0; /* doesn't even begin with \"align\" */\n>         if (!body[0]) {\n>                 *val = NULL; /* %(align) and no customization */\n>                 return 1;\n>         }\n>         if (body[0] != ':')\n>                 return 0; /* \"alignfoo\" is not \"align\" or \"align:...\" */\n>         *val = body + 1; /* \"align:val\" */\n>         return 1;\n> }\n\nThanks.\n\n-- \nRegards,\nKarthik Nayak\n"},{"id":"269216","messageId":"vpq7fo8ol7i.fsf@anie.imag.fr","threadId":"40244","inReplyTo":"CAOLa=ZTB_uMvKjU=f9UgmvOmeZD4YtFL_Gv1+VvZAgDV-6WYDA@mail.gmail.com","subject":"Re: [PATCH v15 07/13] ref-filter: add support for %(contents:lines=X)","fromName":"Matthieu Moy","fromEmail":"matthieu.moy@grenoble-inp.fr","sentAt":"2015-09-02T16:11:13Z","receivedAt":"2015-09-02T16:11:13Z","isPatch":true,"sender":{"key":"matthieu.moy@grenoble-inp.fr","avatar":"https://gravatar.com/avatar/72c8a2705971a25dfaff23cece15130d405685845d911aedd5667ace277f3fc5?d=mp&s=160"},"body":"Karthik Nayak <karthik.188@gmail.com> writes:\n\n> On Wed, Sep 2, 2015 at 2:37 PM, Matthieu Moy\n> <Matthieu.Moy@grenoble-inp.fr> wrote:\n>> Karthik Nayak <karthik.188@gmail.com> writes:\n>>\n>>> --- a/builtin/tag.c\n>>> +++ b/builtin/tag.c\n>>> @@ -185,6 +185,10 @@ static enum contains_result contains(struct commit *candidate,\n>>>       return contains_test(candidate, want);\n>>>  }\n>>>\n>>> +/*\n>>> + * Currently modified and used in ref-filter as append_lines(), will\n>>> + * eventually be removed as we port tag.c to use ref-filter APIs.\n>>> + */\n>>>  static void show_tag_lines(const struct object_id *oid, int lines)\n>>\n>> I would rather have one \"cut and paste\" patch followed by a \"modify and\n>> use\" patch for review.\n>>\n>> As-is, reading the patch doesn't tell me what change you did.\n>>\n>> That said, I did get this information in the interdiff, so I won't\n>> insist on that.\n>\n> Its only borrowed slightly, so I don't really see the need for this.\n> But if you insist, we could do that .\n\nAs you prefer.\n\nPerhaps just adapt the comment to say \"Currently redundant with\nref-filter'.c's append_line ...\", but that's not important.\n\n>>> +static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)\n>>> +{\n>>> +     int i;\n>>> +     const char *sp, *eol;\n>>> +     size_t len;\n>>> +\n>>> +     if ((sp = strstr(buf, \"\\n\\n\")) && (sp <= buf + size))\n>>> +             size += 2;\n>>\n>> Why is this \"size += 2\" needed?\n>>\n>\n> We pass size as \"sublen + bodylen - siglen\" if there exists a \"\\n\\n\"\n> between sublen and bodylen this is not accounted for. hence we\n> add 2 here.\n\nThat's too much magic for uncommented code. If this is really needed,\nthen thes explanations should go in a comment, and I think this logic\nshould be moved out of append_lines (if you read the comment above, the\nfunction, it is actually lying about what the function does).\n\nI think you can simplify this: you know where the buffer ends (bodypos +\nbodylen) and where it starts (subpos), so you know the size: bodypos +\nbodylen - subpos.\n\nIOW, I think you can apply this:\n\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -645,9 +645,6 @@ static void append_lines(struct strbuf *out, const char *buf, unsigned long size\n \tconst char *sp, *eol;\n \tsize_t len;\n \n-\tif ((sp = strstr(buf, \"\\n\\n\")) && (sp <= buf + size))\n-\t\tsize += 2;\n-\n \tsp = buf;\n \n \tfor (i = 0; i < lines && sp < buf + size; i++) {\n@@ -707,7 +704,7 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj\n \t\t\tstruct strbuf s = STRBUF_INIT;\n \t\t\tif (strtoul_ui(valp, 10, &v->u.contents.lines))\n \t\t\t\tdie(_(\"positive width expected contents:lines=%s\"), valp);\n-\t\t\tappend_lines(&s, subpos, sublen + bodylen - siglen, v->u.contents.lines);\n+\t\t\tappend_lines(&s, subpos, bodypos + bodylen - subpos, v->u.contents.lines);\n \t\t\tv->s = strbuf_detach(&s, NULL);\n \t\t}\n \t}\n\n(half-tested only)\n\n-- \nMatthieu Moy\nhttp://www-verimag.imag.fr/~moy/\n"},{"id":"269217","messageId":"vpq37ywol3m.fsf@anie.imag.fr","threadId":"40244","inReplyTo":"CAOLa=ZQXgVXSB9FqhXnc5npOrW8HVj97i0XhNeM-k-MJHR_xfw@mail.gmail.com","subject":"Re: [PATCH v15 11/13] tag.c: use 'ref-filter' APIs","fromName":"Matthieu Moy","fromEmail":"matthieu.moy@grenoble-inp.fr","sentAt":"2015-09-02T16:13:33Z","receivedAt":"2015-09-02T16:13:33Z","isPatch":true,"sender":{"key":"matthieu.moy@grenoble-inp.fr","avatar":"https://gravatar.com/avatar/72c8a2705971a25dfaff23cece15130d405685845d911aedd5667ace277f3fc5?d=mp&s=160"},"body":"Karthik Nayak <karthik.188@gmail.com> writes:\n\n> On Wed, Sep 2, 2015 at 8:40 PM, Junio C Hamano <gitster@pobox.com> wrote:\n>> Karthik Nayak <karthik.188@gmail.com> writes:\n>>\n>>> +     if (filter->lines)\n>>> +             format = to_free = xstrfmt(\"%%(align:15,left)%%(refname:short)%%(end) %%(contents:lines=%d)\",\n>>> +                                        filter->lines);\n>>\n>> I recall hearing that you were more in favor of\n>>\n>>         \"%(align:16)%(refname:short) %(end)%(contents:lines=4)\"\n>>\n>> somewhere in the earlier discussions?\n>\n> I did, but Matthieu suggested that this would be better!\n> Can't find a link to that.\n\nIt was off-list. I don't have strong preference. My argument was:\n\n> -            format = \"%(align:16,left)%(refname:short)%(end)\";\n> +            format = \"%(align:16,left)%(refname:short) %(end)\";\n\nI actually found the other more readable, as it reads \"display the\nrefname aligned on 15 columns, and then a space\", while this one reads\nas \"append a space to the refname, then align the result\", which is\nequivalent but requires more effort to understand.\n\nBut it's not important, keep this version if you prefer it like this.\n\n-- \nMatthieu Moy\nhttp://www-verimag.imag.fr/~moy/\n"},{"id":"269219","messageId":"xmqqpp20ojpf.fsf@gitster.mtv.corp.google.com","threadId":"40244","inReplyTo":"vpq37ywol3m.fsf@anie.imag.fr","subject":"Re: [PATCH v15 11/13] tag.c: use 'ref-filter' APIs","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-09-02T16:43:40Z","receivedAt":"2015-09-02T16:43:40Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:\n\n> I actually found the other more readable, as it reads \"display the\n> refname aligned on 15 columns, and then a space\",\n\nFWIW, I too find the \"15-col followed by a space\" easier to\nunderstand.  I was merely being curious when/why Karthik changed\npreference, not objecting to the actual choice.\n"},{"id":"269220","messageId":"vpqio7slpbd.fsf@anie.imag.fr","threadId":"40244","inReplyTo":"xmqqy4goomdk.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v15 05/13] ref-filter: implement an `align` atom","fromName":"Matthieu Moy","fromEmail":"matthieu.moy@grenoble-inp.fr","sentAt":"2015-09-02T17:10:46Z","receivedAt":"2015-09-02T17:10:46Z","isPatch":true,"sender":{"key":"matthieu.moy@grenoble-inp.fr","avatar":"https://gravatar.com/avatar/72c8a2705971a25dfaff23cece15130d405685845d911aedd5667ace277f3fc5?d=mp&s=160"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> Karthik Nayak <karthik.188@gmail.com> writes:\n>\n>> On Wed, Sep 2, 2015 at 8:31 PM, Junio C Hamano <gitster@pobox.com> wrote:\n>>> Karthik Nayak <karthik.188@gmail.com> writes:\n>>>\n>>>>>> +             die(_(\"format: `end` atom used without a supporting atom\"));\n>>>>>\n>>>>> Not a show-stopper, but we may need some wordsmithing for \"a\n>>>>> supporting atom\" here; an end-user would not know what it is.\n>>>>\n>>>> Probably something like \"format: `end` atom should only be\n>>>> used with modifier atoms\".\n>>>\n>>> Between \"supporting\" and \"modifier\" I do not see much difference,\n>>> though.\n>>\n>> I don't see how we could provide a better message, as %(end) atom\n>> would be common to various atoms eventually.\n>\n> I said \"not a show-stopper\" without giving a suggestion exactly\n> because I didn't (and I still don't) think either you or I can come\n> up with a good wording ;-).  That is why the message was Cc'ed to\n> the list for others to comment.\n\nI don't really have a better proposal either. What we really mean is\n\"%(end) requires an atom that requires to be paired with %(end)\", but\nthat wouldn't really help. I prefer \"supporting\" to \"modifier\":\nTo me, %(color:red) can be called a \"modifier\" by I wouldn't call %(if)\na modifier. \"Supporting\" is vague, but less misleading to me.\n\nPerhaps \"corresponding\"? (not convinced myself ...)\n\n-- \nMatthieu Moy\nhttp://www-verimag.imag.fr/~moy/\n"},{"id":"269221","messageId":"xmqqfv2wohly.fsf@gitster.mtv.corp.google.com","threadId":"40244","inReplyTo":"vpqio7slpbd.fsf@anie.imag.fr","subject":"Re: [PATCH v15 05/13] ref-filter: implement an `align` atom","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-09-02T17:28:57Z","receivedAt":"2015-09-02T17:28:57Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:\n\n> Junio C Hamano <gitster@pobox.com> writes:\n>\n>> Karthik Nayak <karthik.188@gmail.com> writes:\n>>\n>>> On Wed, Sep 2, 2015 at 8:31 PM, Junio C Hamano <gitster@pobox.com> wrote:\n>>>> Karthik Nayak <karthik.188@gmail.com> writes:\n>>>>\n>>>>>>> +             die(_(\"format: `end` atom used without a supporting atom\"));\n>>>>>>\n>>>>>> Not a show-stopper, but we may need some wordsmithing for \"a\n>>>>>> supporting atom\" here; an end-user would not know what it is.\n>>>>>\n>>>>> Probably something like \"format: `end` atom should only be\n>>>>> used with modifier atoms\".\n>>>>\n>>>> Between \"supporting\" and \"modifier\" I do not see much difference,\n>>>> though.\n>>>\n>>> I don't see how we could provide a better message, as %(end) atom\n>>> would be common to various atoms eventually.\n>>\n>> I said \"not a show-stopper\" without giving a suggestion exactly\n>> because I didn't (and I still don't) think either you or I can come\n>> up with a good wording ;-).  That is why the message was Cc'ed to\n>> the list for others to comment.\n>\n> I don't really have a better proposal either. What we really mean is\n> \"%(end) requires an atom that requires to be paired with %(end)\", but\n> that wouldn't really help. I prefer \"supporting\" to \"modifier\":\n> To me, %(color:red) can be called a \"modifier\" by I wouldn't call %(if)\n> a modifier. \"Supporting\" is vague, but less misleading to me.\n>\n> Perhaps \"corresponding\"? (not convinced myself ...)\n\nYeah, it is like an open and a close parentheses that form a\nmatching pair.  \"%(end) without a corresponding atom\" (implying\n\"that opened the environment the %(end) attempts to close\")?\n\nWe'd need to define what an atom is (or \"supporting atom\" for that\nmatter) and explain how nesting works in the documentation anyway,\nand I'd expect we would gain definitions of a few terms we can use\nin this error message.\n"},{"id":"269312","messageId":"CAOLa=ZQCDjo8_h7G1GpUOCs_9X8=phQqxXN-s=XjhBK-DLJVVg@mail.gmail.com","threadId":"40244","inReplyTo":"xmqqfv2wohly.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v15 05/13] ref-filter: implement an `align` atom","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-03T13:30:18Z","receivedAt":"2015-09-03T13:30:18Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"On Wed, Sep 2, 2015 at 10:58 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:\n>\n>> Junio C Hamano <gitster@pobox.com> writes:\n>>\n>>> Karthik Nayak <karthik.188@gmail.com> writes:\n>>>\n>>>> On Wed, Sep 2, 2015 at 8:31 PM, Junio C Hamano <gitster@pobox.com> wrote:\n>>>>> Karthik Nayak <karthik.188@gmail.com> writes:\n>>>>>\n>>>>>>>> +             die(_(\"format: `end` atom used without a supporting atom\"));\n>>>>>>>\n>>>>>>> Not a show-stopper, but we may need some wordsmithing for \"a\n>>>>>>> supporting atom\" here; an end-user would not know what it is.\n>>>>>>\n>>>>>> Probably something like \"format: `end` atom should only be\n>>>>>> used with modifier atoms\".\n>>>>>\n>>>>> Between \"supporting\" and \"modifier\" I do not see much difference,\n>>>>> though.\n>>>>\n>>>> I don't see how we could provide a better message, as %(end) atom\n>>>> would be common to various atoms eventually.\n>>>\n>>> I said \"not a show-stopper\" without giving a suggestion exactly\n>>> because I didn't (and I still don't) think either you or I can come\n>>> up with a good wording ;-).  That is why the message was Cc'ed to\n>>> the list for others to comment.\n>>\n>> I don't really have a better proposal either. What we really mean is\n>> \"%(end) requires an atom that requires to be paired with %(end)\", but\n>> that wouldn't really help. I prefer \"supporting\" to \"modifier\":\n>> To me, %(color:red) can be called a \"modifier\" by I wouldn't call %(if)\n>> a modifier. \"Supporting\" is vague, but less misleading to me.\n>>\n>> Perhaps \"corresponding\"? (not convinced myself ...)\n>\n> Yeah, it is like an open and a close parentheses that form a\n> matching pair.  \"%(end) without a corresponding atom\" (implying\n> \"that opened the environment the %(end) attempts to close\")?\n>\n> We'd need to define what an atom is (or \"supporting atom\" for that\n> matter) and explain how nesting works in the documentation anyway,\n> and I'd expect we would gain definitions of a few terms we can use\n> in this error message.\n>\n\nThen I'll just change it to corresponding for now, and probably go back to it\nafter the series? Maybe work on some documentation at the end of the series.\n\n-- \nRegards,\nKarthik Nayak\n"},{"id":"269313","messageId":"CAOLa=ZSprm90wFcC1-0GOiUmSS0XRmV-gfEg0CYUN45A15CYLw@mail.gmail.com","threadId":"40244","inReplyTo":"xmqqpp20ojpf.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v15 11/13] tag.c: use 'ref-filter' APIs","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-03T13:32:15Z","receivedAt":"2015-09-03T13:32:15Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"On Wed, Sep 2, 2015 at 10:13 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:\n>\n>> I actually found the other more readable, as it reads \"display the\n>> refname aligned on 15 columns, and then a space\",\n>\n> FWIW, I too find the \"15-col followed by a space\" easier to\n> understand.  I was merely being curious when/why Karthik changed\n> preference, not objecting to the actual choice.\n>\n\nI'm surprised you recall that from memory :D\n\n-- \nRegards,\nKarthik Nayak\n"},{"id":"269314","messageId":"CAOLa=ZRiGg1snfsi_mxQYq-wV6Zz4cTkxum47dRowp1XopQejw@mail.gmail.com","threadId":"40244","inReplyTo":"vpq7fo8ol7i.fsf@anie.imag.fr","subject":"Re: [PATCH v15 07/13] ref-filter: add support for %(contents:lines=X)","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-03T13:34:25Z","receivedAt":"2015-09-03T13:34:25Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"On Wed, Sep 2, 2015 at 9:41 PM, Matthieu Moy\n<Matthieu.Moy@grenoble-inp.fr> wrote:\n> Karthik Nayak <karthik.188@gmail.com> writes:\n>\n>> On Wed, Sep 2, 2015 at 2:37 PM, Matthieu Moy\n>> <Matthieu.Moy@grenoble-inp.fr> wrote:\n>>> Karthik Nayak <karthik.188@gmail.com> writes:\n>>>\n>>>> --- a/builtin/tag.c\n>>>> +++ b/builtin/tag.c\n>>>> @@ -185,6 +185,10 @@ static enum contains_result contains(struct commit *candidate,\n>>>>       return contains_test(candidate, want);\n>>>>  }\n>>>>\n>>>> +/*\n>>>> + * Currently modified and used in ref-filter as append_lines(), will\n>>>> + * eventually be removed as we port tag.c to use ref-filter APIs.\n>>>> + */\n>>>>  static void show_tag_lines(const struct object_id *oid, int lines)\n>>>\n>>> I would rather have one \"cut and paste\" patch followed by a \"modify and\n>>> use\" patch for review.\n>>>\n>>> As-is, reading the patch doesn't tell me what change you did.\n>>>\n>>> That said, I did get this information in the interdiff, so I won't\n>>> insist on that.\n>>\n>> Its only borrowed slightly, so I don't really see the need for this.\n>> But if you insist, we could do that .\n>\n> As you prefer.\n>\n> Perhaps just adapt the comment to say \"Currently redundant with\n> ref-filter'.c's append_line ...\", but that's not important.\n\n\"Currently modified and used in ref-filter as append_lines()\" seems better\nas only a part of this is used.\n\n>\n>>>> +static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)\n>>>> +{\n>>>> +     int i;\n>>>> +     const char *sp, *eol;\n>>>> +     size_t len;\n>>>> +\n>>>> +     if ((sp = strstr(buf, \"\\n\\n\")) && (sp <= buf + size))\n>>>> +             size += 2;\n>>>\n>>> Why is this \"size += 2\" needed?\n>>>\n>>\n>> We pass size as \"sublen + bodylen - siglen\" if there exists a \"\\n\\n\"\n>> between sublen and bodylen this is not accounted for. hence we\n>> add 2 here.\n>\n> That's too much magic for uncommented code. If this is really needed,\n> then thes explanations should go in a comment, and I think this logic\n> should be moved out of append_lines (if you read the comment above, the\n> function, it is actually lying about what the function does).\n>\n> I think you can simplify this: you know where the buffer ends (bodypos +\n> bodylen) and where it starts (subpos), so you know the size: bodypos +\n> bodylen - subpos.\n>\n> IOW, I think you can apply this:\n>\n> --- a/ref-filter.c\n> +++ b/ref-filter.c\n> @@ -645,9 +645,6 @@ static void append_lines(struct strbuf *out, const char *buf, unsigned long size\n>         const char *sp, *eol;\n>         size_t len;\n>\n> -       if ((sp = strstr(buf, \"\\n\\n\")) && (sp <= buf + size))\n> -               size += 2;\n> -\n>         sp = buf;\n>\n>         for (i = 0; i < lines && sp < buf + size; i++) {\n> @@ -707,7 +704,7 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj\n>                         struct strbuf s = STRBUF_INIT;\n>                         if (strtoul_ui(valp, 10, &v->u.contents.lines))\n>                                 die(_(\"positive width expected contents:lines=%s\"), valp);\n> -                       append_lines(&s, subpos, sublen + bodylen - siglen, v->u.contents.lines);\n> +                       append_lines(&s, subpos, bodypos + bodylen - subpos, v->u.contents.lines);\n>                         v->s = strbuf_detach(&s, NULL);\n>                 }\n>         }\n>\n> (half-tested only)\n\nIt is important, without it we'll be missing characters at the end.\n\nI'll try what you suggested. Looks good, will test :)\n\n-- \nRegards,\nKarthik Nayak\n"},{"id":"269315","messageId":"CAOLa=ZRSbixzcNyF_s2E+oHMJsG7SuKizb5WHHk5Dmj-OKEetw@mail.gmail.com","threadId":"40244","inReplyTo":"CAOLa=ZRiGg1snfsi_mxQYq-wV6Zz4cTkxum47dRowp1XopQejw@mail.gmail.com","subject":"Re: [PATCH v15 07/13] ref-filter: add support for %(contents:lines=X)","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-03T13:49:03Z","receivedAt":"2015-09-03T13:49:03Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"On Thu, Sep 3, 2015 at 7:04 PM, Karthik Nayak <karthik.188@gmail.com> wrote:\n>>                         struct strbuf s = STRBUF_INIT;\n>>                         if (strtoul_ui(valp, 10, &v->u.contents.lines))\n>>                                 die(_(\"positive width expected contents:lines=%s\"), valp);\n>> -                       append_lines(&s, subpos, sublen + bodylen - siglen, v->u.contents.lines);\n>> +                       append_lines(&s, subpos, bodypos + bodylen - subpos, v->u.contents.lines);\n>>                         v->s = strbuf_detach(&s, NULL);\n>>                 }\n>>         }\n\nappend_lines(&s, subpos, bodylen + bodypos - subpos - siglen,\nv->u.contents.lines);\n\nWe need to eliminate the signature if existing also.\n\n-- \nRegards,\nKarthik Nayak\n"},{"id":"269317","messageId":"CAPig+cRfYow-wBvorX44E4ROH=nvQdS=3zBaEVVbQZf86JFELw@mail.gmail.com","threadId":"40244","inReplyTo":"1441131994-13508-6-git-send-email-Karthik.188@gmail.com","subject":"Re: [PATCH v15 05/13] ref-filter: implement an `align` atom","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2015-09-03T14:12:23Z","receivedAt":"2015-09-03T14:12:23Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Tue, Sep 1, 2015 at 2:26 PM, Karthik Nayak <karthik.188@gmail.com> wrote:\n> Implement an `align` atom which left-, middle-, or right-aligns the\n> content between %(align:..) and %(end).\n\nSpell this either %(align:) or %(align:...) with three dots, not two.\nI, personally, think %(align:) is sufficient.\n\n> It is followed by `:<width>,<position>`, where the `<position>` is\n> either left, right or middle and `<width>` is the size of the area\n> into which the content will be placed. If the content between\n> %(align:) and %(end) is more than the width then no alignment is\n> performed. e.g. to align a refname atom to the middle with a total\n> width of 40 we can do: --format=\"%(align:middle,40)%(refname)%(end)\".\n>\n> We have an `at_end` function for each element of the stack which is to\n> be called when the `end` atom is encountered. Using this we implement\n> the aling_handler() for the `align` atom, this aligns the final strbuf\n> by calling `strbuf_utf8_align()` from utf8.c.\n>\n> Ensure that quote formatting is performed on the whole of\n> %(align)...%(end) rather than individual atoms inside.  We skip quote\n\nAdd colon: %(align:)\n\n> formatting for individual atoms when the current stack element is\n> handling an %(align) atom and perform quote formatting at the end when\n\n%(align:)\n\n> we encounter the %(end) atom.\n>\n> Add documentation and tests for the same.\n>\n> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>\n> ---\n> diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt\n> index e49d578..cac3128 100644\n> --- a/Documentation/git-for-each-ref.txt\n> +++ b/Documentation/git-for-each-ref.txt\n> @@ -127,6 +127,15 @@ color::\n>         Change output color.  Followed by `:<colorname>`, where names\n>         are described in `color.branch.*`.\n>\n> +align::\n> +       Left-, middle-, or right-align the content between %(align:..)\n\n%(align:)\n\n> +       and %(end). Followed by `:<width>,<position>`, where the\n> +       `<position>` is either left, right or middle and `<width>` is\n> +       the total length of the content with alignment. If the\n> +       contents length is more than the width then no alignment is\n> +       performed. If used with '--quote' everything in between\n> +       %(align:..)  and %(end) is quoted.\n\n%(align:)\nAlso drop the extra space before \"and\": s/\\s+and/ and/\n\n>  In addition to the above, for commit and tag objects, the header\n>  field names (`tree`, `parent`, `object`, `type`, and `tag`) can\n>  be used to specify the value in the header field.\n"},{"id":"269318","messageId":"CAPig+cRJG7t1M-FyrB84UG4Ar_NBW3J+wFvFeLg599sAEAweHw@mail.gmail.com","threadId":"40244","inReplyTo":"1441131994-13508-8-git-send-email-Karthik.188@gmail.com","subject":"Re: [PATCH v15 07/13] ref-filter: add support for %(contents:lines=X)","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2015-09-03T14:39:29Z","receivedAt":"2015-09-03T14:39:29Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Tue, Sep 1, 2015 at 2:26 PM, Karthik Nayak <karthik.188@gmail.com> wrote:\n> In 'tag.c' we can print N lines from the annotation of the tag using\n> the '-n<num>' option. Copy code from 'tag.c' to 'ref-filter' and\n> modify it to support appending of N lines from the annotation of tags\n> to the given strbuf.\n>\n> Implement %(contents:lines=X) where X lines of the given object are\n> obtained.\n>\n> Add documentation and test for the same.\n>\n> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>\n> ---\n>  struct atom_value {\n>         const char *s;\n> -       struct align align;\n> +       union {\n> +               struct align align;\n> +               struct contents contents;\n> +       } u;\n>         void (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);\n>         unsigned long ul; /* used for sorting when not FIELD_STR */\n>  };\n> @@ -226,7 +235,7 @@ static void align_atom_handler(struct atom_value *atomv, struct ref_formatting_s\n>         push_stack_element(&state->stack);\n>         new = state->stack;\n>         new->at_end = align_handler;\n> -       new->cb_data = &atomv->align;\n> +       new->cb_data = &atomv->u.align;\n\nYou could declare atom_value with the union from the start, even if it\nhas only a single member ('align', at first). Doing so would make this\npatch less noisy and wouldn't distract the reader with these seemingly\nunrelated changes.\n\n>  }\n>\n>  static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)\n> @@ -624,6 +633,33 @@ static void find_subpos(const char *buf, unsigned long sz,\n>         *nonsiglen = *sig - buf;\n>  }\n>\n> +/*\n> + * If 'lines' is greater than 0, append that many lines from the given\n> + * 'buf' of length 'size' to the given strbuf.\n> + */\n> +static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)\n> +{\n> +       int i;\n> +       const char *sp, *eol;\n> +       size_t len;\n> +\n> +       if ((sp = strstr(buf, \"\\n\\n\")) && (sp <= buf + size))\n> +               size += 2;\n\nAside from the +2 which Matthieu already questioned, this code has a\nmuch more serious problem. strstr() assumes that 'buf' is\nNUL-terminated, however, the fact that buf's size is also being passed\nto the function, implies that it may not be NUL-terminated.\nConsequently, strstr() could zip well past the end of 'buf', trying to\nconsult memory not part of 'buf', which is a violation of the C\nstandard. Even with the band-aid (sp <= buf + size) which tries to\ndetect this situation, it's still a violation, and could crash if\nstrstr() attempts to consult memory which hasn't even been allocated\nto the process or is otherwise protected in some fashion.\n\nIt's possible that 'buf' may actually always be NUL-terminated, but\nthis code does not convey that fact. If it is known to be\nNUL-terminated, then it is safe to use strstr(), however, that should\nbe shown more clearly either by revising the code or adding an\nappropriate comment.\n\n> +       sp = buf;\n> +\n> +       for (i = 0; i < lines && sp < buf + size; i++) {\n> +               if (i)\n> +                       strbuf_addstr(out, \"\\n    \");\n> +               eol = memchr(sp, '\\n', size - (sp - buf));\n> +               len = eol ? eol - sp : size - (sp - buf);\n> +               strbuf_add(out, sp, len);\n> +               if (!eol)\n> +                       break;\n> +               sp = eol + 1;\n> +       }\n> +}\n> @@ -663,6 +701,13 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj\n>                         v->s = xmemdupz(sigpos, siglen);\n>                 else if (!strcmp(name, \"contents\"))\n>                         v->s = xstrdup(subpos);\n> +               else if (skip_prefix(name, \"contents:lines=\", &valp)) {\n> +                       struct strbuf s = STRBUF_INIT;\n> +                       if (strtoul_ui(valp, 10, &v->u.contents.lines))\n> +                               die(_(\"positive width expected contents:lines=%s\"), valp);\n\nThis error message is still too tightly coupled to %(align:), from\nwhich it was copied. \"width\"?\n\n> +                       append_lines(&s, subpos, sublen + bodylen - siglen, v->u.contents.lines);\n> +                       v->s = strbuf_detach(&s, NULL);\n> +               }\n>         }\n>  }\n"},{"id":"269319","messageId":"CAPig+cS9mBNhCm2Ea9jkAK7ME9U1QJ=xYgF_zWzThTVsZCHmdQ@mail.gmail.com","threadId":"40244","inReplyTo":"CAPig+cRJG7t1M-FyrB84UG4Ar_NBW3J+wFvFeLg599sAEAweHw@mail.gmail.com","subject":"Re: [PATCH v15 07/13] ref-filter: add support for %(contents:lines=X)","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2015-09-03T14:47:10Z","receivedAt":"2015-09-03T14:47:10Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Thu, Sep 3, 2015 at 10:39 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n> On Tue, Sep 1, 2015 at 2:26 PM, Karthik Nayak <karthik.188@gmail.com> wrote:\n>> +static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)\n>> +{\n>> +       if ((sp = strstr(buf, \"\\n\\n\")) && (sp <= buf + size))\n>> +               size += 2;\n>\n> Aside from the +2 which Matthieu already questioned, this code has a\n> much more serious problem. strstr() assumes that 'buf' is\n> NUL-terminated, however, the fact that buf's size is also being passed\n> to the function, implies that it may not be NUL-terminated.\n> Consequently, strstr() could zip well past the end of 'buf', trying to\n> consult memory not part of 'buf', which is a violation of the C\n> standard. Even with the band-aid (sp <= buf + size) which tries to\n> detect this situation, it's still a violation, and could crash if\n> strstr() attempts to consult memory which hasn't even been allocated\n> to the process or is otherwise protected in some fashion.\n>\n> It's possible that 'buf' may actually always be NUL-terminated, but\n> this code does not convey that fact. If it is known to be\n> NUL-terminated, then it is safe to use strstr(), however, that should\n> be shown more clearly either by revising the code or adding an\n> appropriate comment.\n\nAlso, if 'buf' is indeed unconditionally NUL-terminated, then the (sp\n<= buf + size) check is wasted code since the result of strstr() will\nalways be either NULL or pointing somewhere within the NUL-terminated\nstring.\n"},{"id":"269320","messageId":"vpq8u8nh85k.fsf@anie.imag.fr","threadId":"40244","inReplyTo":"CAOLa=ZRSbixzcNyF_s2E+oHMJsG7SuKizb5WHHk5Dmj-OKEetw@mail.gmail.com","subject":"Re: [PATCH v15 07/13] ref-filter: add support for %(contents:lines=X)","fromName":"Matthieu Moy","fromEmail":"matthieu.moy@grenoble-inp.fr","sentAt":"2015-09-03T14:47:19Z","receivedAt":"2015-09-03T14:47:19Z","isPatch":true,"sender":{"key":"matthieu.moy@grenoble-inp.fr","avatar":"https://gravatar.com/avatar/72c8a2705971a25dfaff23cece15130d405685845d911aedd5667ace277f3fc5?d=mp&s=160"},"body":"Karthik Nayak <karthik.188@gmail.com> writes:\n\n> On Thu, Sep 3, 2015 at 7:04 PM, Karthik Nayak <karthik.188@gmail.com> wrote:\n>>>                         struct strbuf s = STRBUF_INIT;\n>>>                         if (strtoul_ui(valp, 10, &v->u.contents.lines))\n>>>                                 die(_(\"positive width expected contents:lines=%s\"), valp);\n>>> -                       append_lines(&s, subpos, sublen + bodylen - siglen, v->u.contents.lines);\n>>> +                       append_lines(&s, subpos, bodypos + bodylen - subpos, v->u.contents.lines);\n>>>                         v->s = strbuf_detach(&s, NULL);\n>>>                 }\n>>>         }\n>\n> append_lines(&s, subpos, bodylen + bodypos - subpos - siglen,\n> v->u.contents.lines);\n>\n> We need to eliminate the signature if existing also.\n\nIndeed. I thought body did not include the signature.\n\nI'd write it as\n\n  bodylen + bodypos - siglen - subpos\n\nor even\n\n  char *contents_end = bodylen + bodypos - siglen;\n  ...\n  append_lines(&s, subpos, contents_end - subpos, ...);\n\nto make it self-explanatory.\n\n-- \nMatthieu Moy\nhttp://www-verimag.imag.fr/~moy/\n"},{"id":"269321","messageId":"vpqwpw7fsxt.fsf@anie.imag.fr","threadId":"40244","inReplyTo":"CAPig+cRJG7t1M-FyrB84UG4Ar_NBW3J+wFvFeLg599sAEAweHw@mail.gmail.com","subject":"Re: [PATCH v15 07/13] ref-filter: add support for %(contents:lines=X)","fromName":"Matthieu Moy","fromEmail":"matthieu.moy@grenoble-inp.fr","sentAt":"2015-09-03T15:01:18Z","receivedAt":"2015-09-03T15:01:18Z","isPatch":true,"sender":{"key":"matthieu.moy@grenoble-inp.fr","avatar":"https://gravatar.com/avatar/72c8a2705971a25dfaff23cece15130d405685845d911aedd5667ace277f3fc5?d=mp&s=160"},"body":"Eric Sunshine <sunshine@sunshineco.com> writes:\n\n>> @@ -624,6 +633,33 @@ static void find_subpos(const char *buf, unsigned long sz,\n>>         *nonsiglen = *sig - buf;\n>>  }\n>>\n>> +/*\n>> + * If 'lines' is greater than 0, append that many lines from the given\n>> + * 'buf' of length 'size' to the given strbuf.\n>> + */\n>> +static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)\n>> +{\n>> +       int i;\n>> +       const char *sp, *eol;\n>> +       size_t len;\n>> +\n>> +       if ((sp = strstr(buf, \"\\n\\n\")) && (sp <= buf + size))\n>> +               size += 2;\n>\n> Aside from the +2 which Matthieu already questioned, this code has a\n> much more serious problem. strstr() assumes that 'buf' is\n> NUL-terminated, however, the fact that buf's size is also being passed\n> to the function, implies that it may not be NUL-terminated.\n\nIf Karthik applies my suggestion, then the strstr would go away. I think\nthe code would be correct even on non-null-terminated strings.\n\nActually, we're already making the assumption that the buffer for the\nwhole tag object is null-terminated (and contains no '\\0') for\n%(contents):\n\n\t\telse if (!strcmp(name, \"contents\"))\n\t\t\tv->s = xstrdup(subpos);\n\n(But I agree that even if the assumption is correct, it should be made\nexplicit if it remains a precondition of append_lines).\n\n-- \nMatthieu Moy\nhttp://www-verimag.imag.fr/~moy/\n"},{"id":"269322","messageId":"vpqoahjfsrf.fsf@anie.imag.fr","threadId":"40244","inReplyTo":"CAPig+cS9mBNhCm2Ea9jkAK7ME9U1QJ=xYgF_zWzThTVsZCHmdQ@mail.gmail.com","subject":"Re: [PATCH v15 07/13] ref-filter: add support for %(contents:lines=X)","fromName":"Matthieu Moy","fromEmail":"matthieu.moy@grenoble-inp.fr","sentAt":"2015-09-03T15:05:08Z","receivedAt":"2015-09-03T15:05:08Z","isPatch":true,"sender":{"key":"matthieu.moy@grenoble-inp.fr","avatar":"https://gravatar.com/avatar/72c8a2705971a25dfaff23cece15130d405685845d911aedd5667ace277f3fc5?d=mp&s=160"},"body":"Eric Sunshine <sunshine@sunshineco.com> writes:\n\n> Also, if 'buf' is indeed unconditionally NUL-terminated, then the (sp\n> <= buf + size) check is wasted code since the result of strstr() will\n> always be either NULL or pointing somewhere within the NUL-terminated\n> string.\n\nThe null character is there, but after the signature. buf + size points\nbefore the signature.\n\nAnyway, all this should go away if Karthik applies my suggestion, which\nI like even more after this discussion ;-).\n\n-- \nMatthieu Moy\nhttp://www-verimag.imag.fr/~moy/\n"},{"id":"269327","messageId":"CAOLa=ZT=VEHWokfdBdRPsNj8VgBkyvV7GdacPaRgnMZOQ-G4qg@mail.gmail.com","threadId":"40244","inReplyTo":"CAPig+cRfYow-wBvorX44E4ROH=nvQdS=3zBaEVVbQZf86JFELw@mail.gmail.com","subject":"Re: [PATCH v15 05/13] ref-filter: implement an `align` atom","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-03T16:01:04Z","receivedAt":"2015-09-03T16:01:04Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"On Thu, Sep 3, 2015 at 7:42 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n> On Tue, Sep 1, 2015 at 2:26 PM, Karthik Nayak <karthik.188@gmail.com> wrote:\n>> Implement an `align` atom which left-, middle-, or right-aligns the\n>> content between %(align:..) and %(end).\n>\n> Spell this either %(align:) or %(align:...) with three dots, not two.\n> I, personally, think %(align:) is sufficient.\n>\n\nWill change.\n\n>> It is followed by `:<width>,<position>`, where the `<position>` is\n>> either left, right or middle and `<width>` is the size of the area\n>> into which the content will be placed. If the content between\n>> %(align:) and %(end) is more than the width then no alignment is\n>> performed. e.g. to align a refname atom to the middle with a total\n>> width of 40 we can do: --format=\"%(align:middle,40)%(refname)%(end)\".\n>>\n>> We have an `at_end` function for each element of the stack which is to\n>> be called when the `end` atom is encountered. Using this we implement\n>> the aling_handler() for the `align` atom, this aligns the final strbuf\n>> by calling `strbuf_utf8_align()` from utf8.c.\n>>\n>> Ensure that quote formatting is performed on the whole of\n>> %(align)...%(end) rather than individual atoms inside.  We skip quote\n>\n> Add colon: %(align:)\n>\n\nOkay.\n\n>> formatting for individual atoms when the current stack element is\n>> handling an %(align) atom and perform quote formatting at the end when\n>\n> %(align:)\n>\n\nwill do.\n\n>> we encounter the %(end) atom.\n>>\n>> Add documentation and tests for the same.\n>>\n>> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>\n>> ---\n>> diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt\n>> index e49d578..cac3128 100644\n>> --- a/Documentation/git-for-each-ref.txt\n>> +++ b/Documentation/git-for-each-ref.txt\n>> @@ -127,6 +127,15 @@ color::\n>>         Change output color.  Followed by `:<colorname>`, where names\n>>         are described in `color.branch.*`.\n>>\n>> +align::\n>> +       Left-, middle-, or right-align the content between %(align:..)\n>\n> %(align:)\n>\n\nnoted.\n\n>> +       and %(end). Followed by `:<width>,<position>`, where the\n>> +       `<position>` is either left, right or middle and `<width>` is\n>> +       the total length of the content with alignment. If the\n>> +       contents length is more than the width then no alignment is\n>> +       performed. If used with '--quote' everything in between\n>> +       %(align:..)  and %(end) is quoted.\n>\n> %(align:)\n> Also drop the extra space before \"and\": s/\\s+and/ and/\n>\n\nNoted, thanks for the review.\n\n-- \nRegards,\nKarthik Nayak\n"},{"id":"269328","messageId":"CAOLa=ZS05Ysvfz9gf59wkdAqiSGcwVd25CvRV-DFFj9qPf_PyA@mail.gmail.com","threadId":"40244","inReplyTo":"CAPig+cRJG7t1M-FyrB84UG4Ar_NBW3J+wFvFeLg599sAEAweHw@mail.gmail.com","subject":"Re: [PATCH v15 07/13] ref-filter: add support for %(contents:lines=X)","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-03T16:03:36Z","receivedAt":"2015-09-03T16:03:36Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"On Thu, Sep 3, 2015 at 8:09 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n> On Tue, Sep 1, 2015 at 2:26 PM, Karthik Nayak <karthik.188@gmail.com> wrote:\n>> In 'tag.c' we can print N lines from the annotation of the tag using\n>> the '-n<num>' option. Copy code from 'tag.c' to 'ref-filter' and\n>> modify it to support appending of N lines from the annotation of tags\n>> to the given strbuf.\n>>\n>> Implement %(contents:lines=X) where X lines of the given object are\n>> obtained.\n>>\n>> Add documentation and test for the same.\n>>\n>> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>\n>> ---\n>>  struct atom_value {\n>>         const char *s;\n>> -       struct align align;\n>> +       union {\n>> +               struct align align;\n>> +               struct contents contents;\n>> +       } u;\n>>         void (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);\n>>         unsigned long ul; /* used for sorting when not FIELD_STR */\n>>  };\n>> @@ -226,7 +235,7 @@ static void align_atom_handler(struct atom_value *atomv, struct ref_formatting_s\n>>         push_stack_element(&state->stack);\n>>         new = state->stack;\n>>         new->at_end = align_handler;\n>> -       new->cb_data = &atomv->align;\n>> +       new->cb_data = &atomv->u.align;\n>\n> You could declare atom_value with the union from the start, even if it\n> has only a single member ('align', at first). Doing so would make this\n> patch less noisy and wouldn't distract the reader with these seemingly\n> unrelated changes.\n>\n\nWill do.\n\n>>  }\n>>\n>>  static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)\n>> @@ -624,6 +633,33 @@ static void find_subpos(const char *buf, unsigned long sz,\n>>         *nonsiglen = *sig - buf;\n>>  }\n>>\n>> +/*\n>> + * If 'lines' is greater than 0, append that many lines from the given\n>> + * 'buf' of length 'size' to the given strbuf.\n>> + */\n>> +static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)\n>> +{\n>> +       int i;\n>> +       const char *sp, *eol;\n>> +       size_t len;\n>> +\n>> +       if ((sp = strstr(buf, \"\\n\\n\")) && (sp <= buf + size))\n>> +               size += 2;\n>\n> Aside from the +2 which Matthieu already questioned, this code has a\n> much more serious problem. strstr() assumes that 'buf' is\n> NUL-terminated, however, the fact that buf's size is also being passed\n> to the function, implies that it may not be NUL-terminated.\n> Consequently, strstr() could zip well past the end of 'buf', trying to\n> consult memory not part of 'buf', which is a violation of the C\n> standard. Even with the band-aid (sp <= buf + size) which tries to\n> detect this situation, it's still a violation, and could crash if\n> strstr() attempts to consult memory which hasn't even been allocated\n> to the process or is otherwise protected in some fashion.\n>\n> It's possible that 'buf' may actually always be NUL-terminated, but\n> this code does not convey that fact. If it is known to be\n> NUL-terminated, then it is safe to use strstr(), however, that should\n> be shown more clearly either by revising the code or adding an\n> appropriate comment.\n>\n\nAlthough it is NUL terminated, you do have a point, I hope you checked out\nMatthieu's suggestion of removing that snippet of code and calculating size\nin the function call itself.\n\n>> +       sp = buf;\n>> +\n>> +       for (i = 0; i < lines && sp < buf + size; i++) {\n>> +               if (i)\n>> +                       strbuf_addstr(out, \"\\n    \");\n>> +               eol = memchr(sp, '\\n', size - (sp - buf));\n>> +               len = eol ? eol - sp : size - (sp - buf);\n>> +               strbuf_add(out, sp, len);\n>> +               if (!eol)\n>> +                       break;\n>> +               sp = eol + 1;\n>> +       }\n>> +}\n>> @@ -663,6 +701,13 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj\n>>                         v->s = xmemdupz(sigpos, siglen);\n>>                 else if (!strcmp(name, \"contents\"))\n>>                         v->s = xstrdup(subpos);\n>> +               else if (skip_prefix(name, \"contents:lines=\", &valp)) {\n>> +                       struct strbuf s = STRBUF_INIT;\n>> +                       if (strtoul_ui(valp, 10, &v->u.contents.lines))\n>> +                               die(_(\"positive width expected contents:lines=%s\"), valp);\n>\n> This error message is still too tightly coupled to %(align:), from\n> which it was copied. \"width\"?\n>\n\nWill change it, thanks.\n\n-- \nRegards,\nKarthik Nayak\n"},{"id":"269329","messageId":"CAOLa=ZQzggAmuAe5_9pjPvqjHquvJtkaKNM5Cbk_ZdQ-cVVWwA@mail.gmail.com","threadId":"40244","inReplyTo":"vpqoahjfsrf.fsf@anie.imag.fr","subject":"Re: [PATCH v15 07/13] ref-filter: add support for %(contents:lines=X)","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-03T16:04:45Z","receivedAt":"2015-09-03T16:04:45Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"On Thu, Sep 3, 2015 at 8:35 PM, Matthieu Moy\n<Matthieu.Moy@grenoble-inp.fr> wrote:\n> Eric Sunshine <sunshine@sunshineco.com> writes:\n>\n>> Also, if 'buf' is indeed unconditionally NUL-terminated, then the (sp\n>> <= buf + size) check is wasted code since the result of strstr() will\n>> always be either NULL or pointing somewhere within the NUL-terminated\n>> string.\n>\n> The null character is there, but after the signature. buf + size points\n> before the signature.\n>\n> Anyway, all this should go away if Karthik applies my suggestion, which\n> I like even more after this discussion ;-).\n\nYes, your suggestion seems like a good way to go.\n\n-- \nRegards,\nKarthik Nayak\n"},{"id":"269330","messageId":"CAOLa=ZTndOz5M-3dErpKG5FHxa31ccgvQB+3AK2gQotNDkTF8Q@mail.gmail.com","threadId":"40244","inReplyTo":"vpq8u8nh85k.fsf@anie.imag.fr","subject":"Re: [PATCH v15 07/13] ref-filter: add support for %(contents:lines=X)","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-03T16:05:36Z","receivedAt":"2015-09-03T16:05:36Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"On Thu, Sep 3, 2015 at 8:17 PM, Matthieu Moy\n<Matthieu.Moy@grenoble-inp.fr> wrote:\n> Karthik Nayak <karthik.188@gmail.com> writes:\n>\n>> On Thu, Sep 3, 2015 at 7:04 PM, Karthik Nayak <karthik.188@gmail.com> wrote:\n>>>>                         struct strbuf s = STRBUF_INIT;\n>>>>                         if (strtoul_ui(valp, 10, &v->u.contents.lines))\n>>>>                                 die(_(\"positive width expected contents:lines=%s\"), valp);\n>>>> -                       append_lines(&s, subpos, sublen + bodylen - siglen, v->u.contents.lines);\n>>>> +                       append_lines(&s, subpos, bodypos + bodylen - subpos, v->u.contents.lines);\n>>>>                         v->s = strbuf_detach(&s, NULL);\n>>>>                 }\n>>>>         }\n>>\n>> append_lines(&s, subpos, bodylen + bodypos - subpos - siglen,\n>> v->u.contents.lines);\n>>\n>> We need to eliminate the signature if existing also.\n>\n> Indeed. I thought body did not include the signature.\n>\n> I'd write it as\n>\n>   bodylen + bodypos - siglen - subpos\n>\n> or even\n>\n>   char *contents_end = bodylen + bodypos - siglen;\n>   ...\n>   append_lines(&s, subpos, contents_end - subpos, ...);\n>\n> to make it self-explanatory.\n>\n\nI was thinking of adding a comment but a self explanatory comment\nseems like a good idea. Thanks.\n\n-- \nRegards,\nKarthik Nayak\n"},{"id":"269331","messageId":"xmqqtwrblbdy.fsf@gitster.mtv.corp.google.com","threadId":"40244","inReplyTo":"CAPig+cRfYow-wBvorX44E4ROH=nvQdS=3zBaEVVbQZf86JFELw@mail.gmail.com","subject":"Re: [PATCH v15 05/13] ref-filter: implement an `align` atom","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-09-03T16:23:53Z","receivedAt":"2015-09-03T16:23:53Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Eric Sunshine <sunshine@sunshineco.com> writes:\n\n> On Tue, Sep 1, 2015 at 2:26 PM, Karthik Nayak <karthik.188@gmail.com> wrote:\n>> Implement an `align` atom which left-, middle-, or right-aligns the\n>> content between %(align:..) and %(end).\n>\n> Spell this either %(align:) or %(align:...) with three dots, not two.\n> I, personally, think %(align:) is sufficient.\n\nI agree with you that double-dot does not signal \"some things are\nellided here\" to a usual reader.\n\nI actually think consistent use of %(align:...) is needed, simply\nbecause my knee-jerk reaction to \"%(align:)\" was \"huh?  where does\nthe need for the trailing colon come from?\", not \"ah, you try to\nimply that there must be something more by having just a colon\nthere\".\n"},{"id":"269332","messageId":"xmqqpp1zlb79.fsf@gitster.mtv.corp.google.com","threadId":"40244","inReplyTo":"CAPig+cS9mBNhCm2Ea9jkAK7ME9U1QJ=xYgF_zWzThTVsZCHmdQ@mail.gmail.com","subject":"Re: [PATCH v15 07/13] ref-filter: add support for %(contents:lines=X)","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-09-03T16:27:54Z","receivedAt":"2015-09-03T16:27:54Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Eric Sunshine <sunshine@sunshineco.com> writes:\n\n> Also, if 'buf' is indeed unconditionally NUL-terminated, then the (sp\n> <= buf + size) check is wasted code since the result of strstr() will\n> always be either NULL or pointing somewhere within the NUL-terminated\n> string.\n\nA caller can give a buf that is NUL terminated but specify that the\nonly early part of the buffer to be used by giving you a shorter\nsize, no?  In such a case, strstr() is safe in the sense that it is\nguaranteed not to go on forever, but you need to verify the location\nof the string it found is within the bounds.\n"},{"id":"269412","messageId":"CAOLa=ZT8uAWp=janWUX5iBKZZ7cj-j2Z7OynYxhJ+v4THZJZXw@mail.gmail.com","threadId":"40244","inReplyTo":"xmqqpp1zlb79.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v15 07/13] ref-filter: add support for %(contents:lines=X)","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-04T12:35:36Z","receivedAt":"2015-09-04T12:35:36Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"On Thu, Sep 3, 2015 at 9:57 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> Eric Sunshine <sunshine@sunshineco.com> writes:\n>\n>> Also, if 'buf' is indeed unconditionally NUL-terminated, then the (sp\n>> <= buf + size) check is wasted code since the result of strstr() will\n>> always be either NULL or pointing somewhere within the NUL-terminated\n>> string.\n>\n> A caller can give a buf that is NUL terminated but specify that the\n> only early part of the buffer to be used by giving you a shorter\n> size, no?  In such a case, strstr() is safe in the sense that it is\n> guaranteed not to go on forever, but you need to verify the location\n> of the string it found is within the bounds.\n\nThat was the idea behind this, but will stick to Matthieu's suggestion.\n\n-- \nRegards,\nKarthik Nayak\n"},{"id":"269435","messageId":"CAOLa=ZTx_CG_RrHHDLadWmx5UvuE9GMaf4nwcWr880vQ-eM3Pg@mail.gmail.com","threadId":"40244","inReplyTo":"xmqqtwrblbdy.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v15 05/13] ref-filter: implement an `align` atom","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-09-04T18:02:13Z","receivedAt":"2015-09-04T18:02:13Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"On Thu, Sep 3, 2015 at 9:53 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> Eric Sunshine <sunshine@sunshineco.com> writes:\n>\n>> On Tue, Sep 1, 2015 at 2:26 PM, Karthik Nayak <karthik.188@gmail.com> wrote:\n>>> Implement an `align` atom which left-, middle-, or right-aligns the\n>>> content between %(align:..) and %(end).\n>>\n>> Spell this either %(align:) or %(align:...) with three dots, not two.\n>> I, personally, think %(align:) is sufficient.\n>\n> I agree with you that double-dot does not signal \"some things are\n> ellided here\" to a usual reader.\n>\n> I actually think consistent use of %(align:...) is needed, simply\n> because my knee-jerk reaction to \"%(align:)\" was \"huh?  where does\n> the need for the trailing colon come from?\", not \"ah, you try to\n> imply that there must be something more by having just a colon\n> there\".\n>\n\nYeah, it makes more sense to keep the triple dot.\n\n-- \nRegards,\nKarthik Nayak\n"}]}