git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH v5 00/11] port tag.c to use ref-filter APIs
@ 2015-07-27  7:26 Karthik Nayak
  2015-07-27  7:27 ` [PATCH v5 01/11] ref-filter: introduce 'ref_formatting_state' Karthik Nayak
  0 siblings, 1 reply; 38+ messages in thread
From: Karthik Nayak @ 2015-07-27  7:26 UTC (permalink / raw)
  To: Git; +Cc: Christian Couder, Matthieu Moy, Junio C Hamano

This is part of my GSoC project to unify git tag -l, git branch -l,
git for-each-ref.  This patch series is continued from: Git (next)
https://github.com/git/git/commit/bf5418f49ff0cebc6e5ce04ad1417e1a47c81b61

Version 4 can be found here
http://article.gmane.org/gmane.comp.version-control.git/274579

Changes in this version (interdiff at the end):
* Change the way ref_formatting_state is used and the sequence of its
usage in the
series.
* Separate the introduction of ref_formatting_state from its usage.
* Removed the inefficient removal of path in match_pattern().
* Improve coding style in the implementation of sorting based on version.
* Changed the tests for sorting based on version, to use
'for-each-ref' rather than 'tag.c'
which already has a working sort based on version.
* Other small grammatical changes.
* Change the atom name from 'align' to 'padright'.

[PATCH v5 01/11] ref-filter: introduce 'ref_formatting_state'
[PATCH v5 02/11] ref-filter: make `color` use `ref_formatting_state`
[PATCH v5 03/11] ref-filter: add option to pad atoms to the right
[PATCH v5 04/11] ref-filter: add option to filter only tags
[PATCH v5 05/11] ref-filter: support printing N lines from tag
[PATCH v5 06/11] ref-filter: add support to sort by version
[PATCH v5 07/11] ref-filter: add option to match literal pattern
[PATCH v5 08/11] tag.c: use 'ref-filter' data structures
[PATCH v5 09/11] tag.c: use 'ref-filter' APIs
[PATCH v5 10/11] tag.c: implement '--format' option
[PATCH v5 11/11] tag.c: implement '--merged' and '--no-merged'

Interdiff:

diff --git a/Documentation/git-for-each-ref.txt
b/Documentation/git-for-each-ref.txt
index 224dc8c..2b60aee 100644
--- a/Documentation/git-for-each-ref.txt
+++ b/Documentation/git-for-each-ref.txt
@@ -127,6 +127,12 @@ color::
     Change output color.  Followed by `:<colorname>`, where names
     are described in `color.branch.*`.

+padright::
+    Pad succeeding atom to the right. Followed by `:<value>`,
+    where `value` states the total length of atom including the
+    padding. If the `value` is greater than the atom length, then
+    no padding is performed.
+
 In addition to the above, for commit and tag objects, the header
 field names (`tree`, `parent`, `object`, `type`, and `tag`) can
 be used to specify the value in the header field.
diff --git a/builtin/tag.c b/builtin/tag.c
index 8a7f684..fc01117 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -41,13 +41,11 @@ static int list_tags(struct ref_filter *filter,
struct ref_sorting *sorting, con
         filter->lines = 0;

     if (filter->lines)
-        format = "%(align:16)%(refname:short)";
+        format = "%(padright:16)%(refname:short)";
     else if (!format)
         format = "%(refname:short)";

     verify_ref_format(format);
-    if (!sorting)
-        sorting = ref_default_sorting();
     filter_refs(&array, filter, FILTER_REFS_TAGS);
     ref_array_sort(sorting, &array);

@@ -134,7 +132,7 @@ static int parse_sorting_string(const char *arg,
struct ref_sorting **sorting_ta
     if (*arg == '-') {
         s->reverse = 1;
         arg++;
-     }
+    }
     if (skip_prefix(arg, "version:", &arg) ||
         skip_prefix(arg, "v:", &arg))
         s->version = 1;
diff --git a/ref-filter.c b/ref-filter.c
index c686c03..597b189 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -56,7 +56,7 @@ static struct {
     { "flag" },
     { "HEAD" },
     { "color" },
-    { "align" },
+    { "padright" },
 };

 /*
@@ -601,8 +601,7 @@ static inline char *copy_advance(char *dst, const char *src)
 /*
  * Parse the object referred by ref, and grab needed value.
  */
-static void populate_value(struct ref_formatting_state *state,
-               struct ref_array_item *ref)
+static void populate_value(struct ref_array_item *ref)
 {
     void *buf;
     struct object *obj;
@@ -664,8 +663,9 @@ static void populate_value(struct
ref_formatting_state *state,

             if (color_parse(name + 6, color) < 0)
                 die(_("unable to parse format"));
-            state->color = xstrdup(color);
+            v->s = xstrdup(color);
             v->pseudo_atom = 1;
+            v->color = 1;
             continue;
         } else if (!strcmp(name, "flag")) {
             char buf[256], *cp = buf;
@@ -693,17 +693,17 @@ static void populate_value(struct
ref_formatting_state *state,
             else
                 v->s = " ";
             continue;
-        } else if (starts_with(name, "align:")) {
+        } else if (starts_with(name, "padright:")) {
             const char *valp = NULL;

-            skip_prefix(name, "align:", &valp);
+            skip_prefix(name, "padright:", &valp);
             if (!valp[0])
-                die(_("no value given with 'align:'"));
-            if (strtoul_ui(valp, 10, &state->pad_to_right))
-                die(_("positive integer expected after ':' in align:%u\n"),
-                    state->pad_to_right);
+                die(_("no value given with 'padright:'"));
+            if (strtoul_ui(valp, 10, (unsigned int *)&v->ul))
+                die(_("positive integer expected after ':' in padright:%u\n"),
+                    (unsigned int)v->ul);
             v->pseudo_atom = 1;
-            v->s = "";
+            v->pad_to_right = 1;
             continue;
         } else
             continue;
@@ -827,15 +827,10 @@ static void populate_value(struct
ref_formatting_state *state,
  * Given a ref, return the value for the atom.  This lazily gets value
  * out of the object by calling populate value.
  */
-static void get_ref_atom_value(struct ref_formatting_state *state,
-                   struct ref_array_item *ref, int atom, struct atom_value **v)
+static void get_ref_atom_value(struct ref_array_item *ref, int atom,
struct atom_value **v)
 {
-    /*
-     * If the atom is a pseudo_atom then we re-populate the value
-     * into the ref_formatting_state stucture.
-     */
-    if (!ref->value || ref->value[atom].pseudo_atom) {
-        populate_value(state, ref);
+    if (!ref->value) {
+        populate_value(ref);
         fill_missing_values(ref->value);
     }
     *v = &ref->value[atom];
@@ -957,17 +952,18 @@ static int commit_contains(struct ref_filter
*filter, struct commit *commit)
  */
 static int match_pattern(const char **patterns, const char *refname)
 {
+    /*
+     * When no '--format' option is given we need to skip the prefix
+     * for matching refs of tags and branches.
+     */
+    if (skip_prefix(refname, "refs/tags/", &refname))
+        ;
+    else if (skip_prefix(refname, "refs/heads/", &refname))
+        ;
+    else if (skip_prefix(refname, "refs/remotes/", &refname))
+        ;
+
     for (; *patterns; patterns++) {
-        /*
-         * When no '--format' option is given we need to skip the prefix
-         * for listing tags and branches.
-         */
-        if (!starts_with(*pattern, "refs/tags/"))
-            skip_prefix(refname, "refs/tags/", &refname);
-        else if (!starts_with(*pattern, "refs/heads/"))
-            skip_prefix(refname, "refs/heads/", &refname);
-        else if (!starts_with(refname, "refs/remotes/"))
-            skip_prefix(refname, "refs/remotes/", &refname);
         if (!wildmatch(*patterns, refname, 0, NULL))
             return 1;
     }
@@ -978,7 +974,7 @@ static int match_pattern(const char **patterns,
const char *refname)
  * Return 1 if the refname matches one of the patterns, otherwise 0.
  * A pattern can be path prefix (e.g. a refname "refs/heads/master"
  * matches a pattern "refs/heads/") or a wildcard (e.g. the same ref
- * matches "refs/heads/m*", too).
+ * matches "refs/heads/m*",too).
  */
 static int match_name_as_path(const char **pattern, const char *refname)
 {
@@ -1210,22 +1206,21 @@ static int cmp_ref_sorting(struct ref_sorting
*s, struct ref_array_item *a, stru
     struct atom_value *va, *vb;
     int cmp;
     cmp_type cmp_type = used_atom_type[s->atom];
-    struct ref_formatting_state state;

-    memset(&state, 0, sizeof(state));
-
-    get_ref_atom_value(&state, a, s->atom, &va);
-    get_ref_atom_value(&state, b, s->atom, &vb);
+    get_ref_atom_value(a, s->atom, &va);
+    get_ref_atom_value(b, s->atom, &vb);
     if (s->version)
         cmp = versioncmp(va->s, vb->s);
     else if (cmp_type == FIELD_STR)
         cmp = strcmp(va->s, vb->s);
-    else if (va->ul < vb->ul)
-        cmp = -1;
-    else if (va->ul == vb->ul)
-        cmp = 0;
-    else
-        cmp = 1;
+    else {
+        if (va->ul < vb->ul)
+            cmp = -1;
+        else if (va->ul == vb->ul)
+            cmp = 0;
+        else
+            cmp = 1;
+    }

     return (s->reverse) ? -cmp : cmp;
 }
@@ -1251,20 +1246,20 @@ void ref_array_sort(struct ref_sorting
*sorting, struct ref_array *array)
     qsort(array->items, array->nr, sizeof(struct ref_array_item *),
compare_refs);
 }

-static void ref_formatting(struct ref_formatting_state *state, struct
atom_value *v,
-               struct strbuf *value)
+static void ref_formatting(struct ref_formatting_state *state,
+               struct atom_value *v, struct strbuf *value)
 {
     if (state->color) {
         strbuf_addstr(value, state->color);
-        free((void *)state->color);
+        free(state->color);
         state->color = NULL;
     }
     if (state->pad_to_right) {
         if (!is_utf8(v->s))
             strbuf_addf(value, "%-*s", state->pad_to_right, v->s);
         else {
-            int len = strlen(v->s) - utf8_strwidth(v->s);
-            strbuf_addf(value, "%-*s", state->pad_to_right + len, v->s);
+            int utf8_compensation = strlen(v->s) - utf8_strwidth(v->s);
+            strbuf_addf(value, "%-*s", state->pad_to_right +
utf8_compensation, v->s);
         }
         return;
     }
@@ -1276,8 +1271,6 @@ static void print_value(struct
ref_formatting_state *state, struct atom_value *v
     struct strbuf value = STRBUF_INIT;
     struct strbuf formatted = STRBUF_INIT;

-    if (v->pseudo_atom)
-        return;
     ref_formatting(state, v, &value);

     switch (state->quote_style) {
@@ -1341,6 +1334,15 @@ static void emit(const char *cp, const char *ep)
     }
 }

+static void apply_pseudo_state(struct ref_formatting_state *state,
+                   struct atom_value *v)
+{
+    if (v->color)
+        state->color = (char *)v->s;
+    if (v->pad_to_right)
+        state->pad_to_right = v->ul;
+}
+
 /*
  * If 'lines' is greater than 0, print that many lines from the given
  * object_id 'oid'.
@@ -1399,9 +1401,11 @@ void show_ref_array_item(struct ref_array_item
*info, const char *format,
         ep = strchr(sp, ')');
         if (cp < sp)
             emit(cp, sp);
-        get_ref_atom_value(&state, info,
-                   parse_ref_filter_atom(sp + 2, ep), &atomv);
-        print_value(&state, atomv);
+        get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
+        if (atomv->pseudo_atom)
+            apply_pseudo_state(&state, atomv);
+        else
+            print_value(&state, atomv);
     }
     if (*cp) {
         sp = cp + strlen(cp);
@@ -1413,8 +1417,7 @@ void show_ref_array_item(struct ref_array_item
*info, const char *format,

         if (color_parse("reset", color) < 0)
             die("BUG: couldn't parse 'reset' as a color");
-        resetv.s = "";
-        state.color = xstrdup(color);
+        resetv.s = color;
         print_value(&state, &resetv);
     }
     if (lines > 0) {
diff --git a/ref-filter.h b/ref-filter.h
index f1933e0..a27745f 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -20,7 +20,15 @@
 struct atom_value {
     const char *s;
     unsigned long ul; /* used for sorting when not FIELD_STR */
-    unsigned int pseudo_atom : 1; /*  atoms which aren't placeholders
for ref attributes */
+    unsigned int pseudo_atom : 1, /*  atoms which aren't placeholders
for ref attributes */
+        color : 1,
+        pad_to_right : 1;
+};
+
+struct ref_formatting_state {
+    int quote_style;
+    char *color;
+    unsigned int pad_to_right;
 };

 struct ref_sorting {
@@ -30,12 +38,6 @@ struct ref_sorting {
         version : 1;
 };

-struct ref_formatting_state {
-    unsigned int pad_to_right; /*pad atoms to the right*/
-    int quote_style;
-    const char *color;
-};
-
 struct ref_array_item {
     unsigned char objectname[20];
     int flag;
diff --git a/t/t6302-for-each-ref-filter.sh b/t/t6302-for-each-ref-filter.sh
index 5017032..de872db 100755
--- a/t/t6302-for-each-ref-filter.sh
+++ b/t/t6302-for-each-ref-filter.sh
@@ -81,6 +81,22 @@ test_expect_success 'filtering with --contains' '
     test_cmp expect actual
 '

+test_expect_success 'padding to the right using `padright`' '
+    cat >expect <<-\EOF &&
+    refs/heads/master        |
+    refs/heads/side          |
+    refs/odd/spot            |
+    refs/tags/double-tag     |
+    refs/tags/four           |
+    refs/tags/one            |
+    refs/tags/signed-tag     |
+    refs/tags/three          |
+    refs/tags/two            |
+    EOF
+    git for-each-ref --format="%(padright:25)%(refname)|" >actual &&
+    test_cmp expect actual
+'
+
 test_expect_success 'setup for version sort' '
     test_commit foo1.3 &&
     test_commit foo1.6 &&
@@ -88,7 +104,7 @@ test_expect_success 'setup for version sort' '
 '

 test_expect_success 'version sort' '
-    git tag -l --sort=version:refname | grep "foo" >actual &&
+    git for-each-ref --sort=version:refname
--format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
     cat >expect <<-\EOF &&
     foo1.3
     foo1.6
@@ -98,7 +114,7 @@ test_expect_success 'version sort' '
 '

 test_expect_success 'version sort (shortened)' '
-    git tag -l --sort=v:refname | grep "foo" >actual &&
+    git for-each-ref --sort=v:refname --format="%(refname:short)"
refs/tags/ | grep "foo" >actual &&
     cat >expect <<-\EOF &&
     foo1.3
     foo1.6
@@ -108,7 +124,7 @@ test_expect_success 'version sort (shortened)' '
 '

 test_expect_success 'reverse version sort' '
-    git tag -l --sort=-version:refname | grep "foo" >actual &&
+    git for-each-ref --sort=-version:refname
--format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
     cat >expect <<-\EOF &&
     foo1.10
     foo1.6
diff --git a/v3-0001-ref-filter-add-option-to-align-atoms-to-the-left.patch
b/v3-0001-ref-filter-add-option-to-align-atoms-to-the-left.patch
deleted file mode 100644
index 350acae..0000000
--- a/v3-0001-ref-filter-add-option-to-align-atoms-to-the-left.patch
+++ /dev/null
@@ -1,124 +0,0 @@
-From 3a07ca1f56f74ca54b7f3c30e3dfd9fe2fed1cc3 Mon Sep 17 00:00:00 2001
-From: Karthik Nayak <karthik.188@gmail.com>
-Date: Wed, 10 Jun 2015 17:19:55 +0530
-Subject: [PATCH v3 1/9] ref-filter: add option to align atoms to the left
-
-Add a new atom "align" and support %(align:X) where X is a number.
-This will align the preceeding atom value to the left followed by
-spaces for a total length of X characters. If X is less than the item
-size, the entire atom value is printed.
-
-Helped-by: Duy Nguyen <pclouds@gmail.com>
-Mentored-by: Christian Couder <christian.couder@gmail.com>
-Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>
-Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
----
- ref-filter.c | 41 +++++++++++++++++++++++++++++++++++++++--
- ref-filter.h |  1 +
- 2 files changed, 40 insertions(+), 2 deletions(-)
-
-diff --git a/ref-filter.c b/ref-filter.c
-index 7561727..93f59aa 100644
---- a/ref-filter.c
-+++ b/ref-filter.c
-@@ -10,6 +10,8 @@
- #include "quote.h"
- #include "ref-filter.h"
- #include "revision.h"
-+#include "utf8.h"
-+#include "git-compat-util.h"
-
- typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
-
-@@ -53,6 +55,7 @@ static struct {
-     { "flag" },
-     { "HEAD" },
-     { "color" },
-+    { "align" },
- };
-
- /*
-@@ -620,7 +623,7 @@ static void populate_value(struct ref_array_item *ref)
-         const char *name = used_atom[i];
-         struct atom_value *v = &ref->value[i];
-         int deref = 0;
--        const char *refname;
-+        const char *refname = NULL;
-         const char *formatp;
-         struct branch *branch = NULL;
-
-@@ -687,6 +690,17 @@ static void populate_value(struct ref_array_item *ref)
-             else
-                 v->s = " ";
-             continue;
-+        } else if (starts_with(name, "align:")) {
-+            const char *valp = NULL;
-+
-+            skip_prefix(name, "align:", &valp);
-+            if (!valp[0])
-+                die(_("no value given with 'align:'"));
-+            strtoul_ui(valp, 10, &ref->align_value);
-+            if (ref->align_value < 1)
-+                die(_("value should be greater than zero:
align:%u"), ref->align_value);
-+            v->s = "";
-+            continue;
-         } else
-             continue;
-
-@@ -1254,17 +1268,40 @@ static void emit(const char *cp, const char *ep)
-     }
- }
-
-+static void assign_formating(struct ref_array_item *ref, int
parsed_atom, struct atom_value *v)
-+{
-+    if (ref->align_value && !starts_with(used_atom[parsed_atom], "align")) {
-+        unsigned int len = 0;
-+
-+        if (*v->s)
-+            len = utf8_strwidth(v->s);
-+        if (ref->align_value > len) {
-+            struct strbuf buf = STRBUF_INIT;
-+            if (*v->s)
-+                strbuf_addstr(&buf, v->s);
-+            if (*v->s && v->s[0] == '\0')
-+                free((char *)v->s);
-+            strbuf_addchars(&buf, ' ', ref->align_value - len);
-+            v->s = strbuf_detach(&buf, NULL);
-+        }
-+        ref->align_value = 0;
-+    }
-+}
-+
- void show_ref_array_item(struct ref_array_item *info, const char
*format, int quote_style)
- {
-     const char *cp, *sp, *ep;
-
-     for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
-         struct atom_value *atomv;
-+        int parsed_atom;
-
-         ep = strchr(sp, ')');
-         if (cp < sp)
-             emit(cp, sp);
--        get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
-+        parsed_atom = parse_ref_filter_atom(sp + 2, ep);
-+        get_ref_atom_value(info, parsed_atom, &atomv);
-+        assign_formating(info, parsed_atom, atomv);
-         print_value(atomv, quote_style);
-     }
-     if (*cp) {
-diff --git a/ref-filter.h b/ref-filter.h
-index 6bf27d8..12ffbc5 100644
---- a/ref-filter.h
-+++ b/ref-filter.h
-@@ -30,6 +30,7 @@ struct ref_sorting {
- struct ref_array_item {
-     unsigned char objectname[20];
-     int flag;
-+    unsigned int align_value;
-     const char *symref;
-     struct commit *commit;
-     struct atom_value *value;
---
-2.4.6
-

Thanks to everyone for suggestions on the previous version.

-- 
Regards,
Karthik Nayak

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [PATCH v5 01/11] ref-filter: introduce 'ref_formatting_state'
  2015-07-27  7:26 [PATCH v5 00/11] port tag.c to use ref-filter APIs Karthik Nayak
@ 2015-07-27  7:27 ` Karthik Nayak
  2015-07-27  7:27   ` [PATCH v5 02/11] ref-filter: make `color` use `ref_formatting_state` Karthik Nayak
                     ` (10 more replies)
  0 siblings, 11 replies; 38+ messages in thread
From: Karthik Nayak @ 2015-07-27  7:27 UTC (permalink / raw)
  To: git; +Cc: christian.couder, Matthieu.Moy, gitster, Karthik Nayak,
	Karthik Nayak

Introduce 'ref_formatting' structure to hold values of pseudo atoms
which help only in formatting. This will eventually be used by atoms
like `color` and the `padright` atom which will be introduced in a
later patch.

Helped-by: Junio C Hamano <gitster@pobox.com>
Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 ref-filter.c | 46 +++++++++++++++++++++++++++++++---------------
 ref-filter.h |  5 +++++
 2 files changed, 36 insertions(+), 15 deletions(-)

diff --git a/ref-filter.c b/ref-filter.c
index 7561727..a3625e8 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -10,6 +10,8 @@
 #include "quote.h"
 #include "ref-filter.h"
 #include "revision.h"
+#include "utf8.h"
+#include "git-compat-util.h"
 
 typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
 
@@ -620,7 +622,7 @@ static void populate_value(struct ref_array_item *ref)
 		const char *name = used_atom[i];
 		struct atom_value *v = &ref->value[i];
 		int deref = 0;
-		const char *refname;
+		const char *refname = NULL;
 		const char *formatp;
 		struct branch *branch = NULL;
 
@@ -1190,30 +1192,40 @@ void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
 	qsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs);
 }
 
-static void print_value(struct atom_value *v, int quote_style)
+static void ref_formatting(struct ref_formatting_state *state,
+			   struct atom_value *v, struct strbuf *value)
 {
-	struct strbuf sb = STRBUF_INIT;
-	switch (quote_style) {
+	strbuf_addf(value, "%s", v->s);
+}
+
+static void print_value(struct ref_formatting_state *state, struct atom_value *v)
+{
+	struct strbuf value = STRBUF_INIT;
+	struct strbuf formatted = STRBUF_INIT;
+
+	ref_formatting(state, v, &value);
+
+	switch (state->quote_style) {
 	case QUOTE_NONE:
-		fputs(v->s, stdout);
+		fputs(value.buf, stdout);
 		break;
 	case QUOTE_SHELL:
-		sq_quote_buf(&sb, v->s);
+		sq_quote_buf(&formatted, value.buf);
 		break;
 	case QUOTE_PERL:
-		perl_quote_buf(&sb, v->s);
+		perl_quote_buf(&formatted, value.buf);
 		break;
 	case QUOTE_PYTHON:
-		python_quote_buf(&sb, v->s);
+		python_quote_buf(&formatted, value.buf);
 		break;
 	case QUOTE_TCL:
-		tcl_quote_buf(&sb, v->s);
+		tcl_quote_buf(&formatted, value.buf);
 		break;
 	}
-	if (quote_style != QUOTE_NONE) {
-		fputs(sb.buf, stdout);
-		strbuf_release(&sb);
-	}
+	if (state->quote_style != QUOTE_NONE)
+		fputs(formatted.buf, stdout);
+	strbuf_release(&formatted);
+	strbuf_release(&value);
 }
 
 static int hex1(char ch)
@@ -1257,6 +1269,10 @@ static void emit(const char *cp, const char *ep)
 void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
 {
 	const char *cp, *sp, *ep;
+	struct ref_formatting_state state;
+
+	memset(&state, 0, sizeof(state));
+	state.quote_style = quote_style;
 
 	for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
 		struct atom_value *atomv;
@@ -1265,7 +1281,7 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu
 		if (cp < sp)
 			emit(cp, sp);
 		get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
-		print_value(atomv, quote_style);
+		print_value(&state, atomv);
 	}
 	if (*cp) {
 		sp = cp + strlen(cp);
@@ -1278,7 +1294,7 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu
 		if (color_parse("reset", color) < 0)
 			die("BUG: couldn't parse 'reset' as a color");
 		resetv.s = color;
-		print_value(&resetv, quote_style);
+		print_value(&state, &resetv);
 	}
 	putchar('\n');
 }
diff --git a/ref-filter.h b/ref-filter.h
index 6bf27d8..f24e3ef 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -19,6 +19,11 @@
 struct atom_value {
 	const char *s;
 	unsigned long ul; /* used for sorting when not FIELD_STR */
+	unsigned int pseudo_atom : 1; /*  atoms which aren't placeholders for ref attributes */
+};
+
+struct ref_formatting_state {
+	int quote_style;
 };
 
 struct ref_sorting {
-- 
2.4.6

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [PATCH v5 02/11] ref-filter: make `color` use `ref_formatting_state`
  2015-07-27  7:27 ` [PATCH v5 01/11] ref-filter: introduce 'ref_formatting_state' Karthik Nayak
@ 2015-07-27  7:27   ` Karthik Nayak
  2015-07-27 12:47     ` Matthieu Moy
  2015-07-27 13:06     ` Matthieu Moy
  2015-07-27  7:27   ` [PATCH v5 03/11] ref-filter: add option to pad atoms to the right Karthik Nayak
                     ` (9 subsequent siblings)
  10 siblings, 2 replies; 38+ messages in thread
From: Karthik Nayak @ 2015-07-27  7:27 UTC (permalink / raw)
  To: git; +Cc: christian.couder, Matthieu.Moy, gitster, Karthik Nayak

Make the `color` atom a pseudo atom and ensure that it uses
`ref_formatting_state`.
---
 ref-filter.c | 19 ++++++++++++++++++-
 ref-filter.h |  4 +++-
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/ref-filter.c b/ref-filter.c
index a3625e8..cc25c85 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -662,6 +662,8 @@ static void populate_value(struct ref_array_item *ref)
 			if (color_parse(name + 6, color) < 0)
 				die(_("unable to parse format"));
 			v->s = xstrdup(color);
+			v->pseudo_atom = 1;
+			v->color = 1;
 			continue;
 		} else if (!strcmp(name, "flag")) {
 			char buf[256], *cp = buf;
@@ -1195,6 +1197,11 @@ void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
 static void ref_formatting(struct ref_formatting_state *state,
 			   struct atom_value *v, struct strbuf *value)
 {
+	if (state->color) {
+		strbuf_addstr(value, state->color);
+		free(state->color);
+		state->color = NULL;
+	}
 	strbuf_addf(value, "%s", v->s);
 }
 
@@ -1266,6 +1273,13 @@ static void emit(const char *cp, const char *ep)
 	}
 }
 
+static void apply_pseudo_state(struct ref_formatting_state *state,
+			       struct atom_value *v)
+{
+	if (v->color)
+		state->color = (char *)v->s;
+}
+
 void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
 {
 	const char *cp, *sp, *ep;
@@ -1281,7 +1295,10 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu
 		if (cp < sp)
 			emit(cp, sp);
 		get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
-		print_value(&state, atomv);
+		if (atomv->pseudo_atom)
+			apply_pseudo_state(&state, atomv);
+		else
+			print_value(&state, atomv);
 	}
 	if (*cp) {
 		sp = cp + strlen(cp);
diff --git a/ref-filter.h b/ref-filter.h
index f24e3ef..7687879 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -19,11 +19,13 @@
 struct atom_value {
 	const char *s;
 	unsigned long ul; /* used for sorting when not FIELD_STR */
-	unsigned int pseudo_atom : 1; /*  atoms which aren't placeholders for ref attributes */
+	unsigned int pseudo_atom : 1, /*  atoms which aren't placeholders for ref attributes */
+		color : 1;
 };
 
 struct ref_formatting_state {
 	int quote_style;
+	char *color;
 };
 
 struct ref_sorting {
-- 
2.4.6

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [PATCH v5 03/11] ref-filter: add option to pad atoms to the right
  2015-07-27  7:27 ` [PATCH v5 01/11] ref-filter: introduce 'ref_formatting_state' Karthik Nayak
  2015-07-27  7:27   ` [PATCH v5 02/11] ref-filter: make `color` use `ref_formatting_state` Karthik Nayak
@ 2015-07-27  7:27   ` Karthik Nayak
  2015-07-27 12:50     ` Matthieu Moy
  2015-07-28 21:41     ` Eric Sunshine
  2015-07-27  7:27   ` [PATCH v5 04/11] ref-filter: add option to filter only tags Karthik Nayak
                     ` (8 subsequent siblings)
  10 siblings, 2 replies; 38+ messages in thread
From: Karthik Nayak @ 2015-07-27  7:27 UTC (permalink / raw)
  To: git; +Cc: christian.couder, Matthieu.Moy, gitster, Karthik Nayak,
	Karthik Nayak

Add a new atom "padright" and support %(padright:X) where X is a
number.  This will align the succeeding atom value to the left
followed by spaces for a total length of X characters. If X is less
than the item size, the entire atom value is printed.

Add tests and documentation for the same.

Helped-by: Duy Nguyen <pclouds@gmail.com>
Helped-by: Junio C Hamano <gitster@pobox.com>
Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 Documentation/git-for-each-ref.txt |  6 ++++++
 ref-filter.c                       | 24 ++++++++++++++++++++++++
 ref-filter.h                       |  4 +++-
 t/t6302-for-each-ref-filter.sh     | 16 ++++++++++++++++
 4 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt
index e49d578..45dd7f8 100644
--- a/Documentation/git-for-each-ref.txt
+++ b/Documentation/git-for-each-ref.txt
@@ -127,6 +127,12 @@ color::
 	Change output color.  Followed by `:<colorname>`, where names
 	are described in `color.branch.*`.
 
+padright::
+	Pad succeeding atom to the right. Followed by `:<value>`,
+	where `value` states the total length of atom including the
+	padding. If the `value` is greater than the atom length, then
+	no padding is performed.
+
 In addition to the above, for commit and tag objects, the header
 field names (`tree`, `parent`, `object`, `type`, and `tag`) can
 be used to specify the value in the header field.
diff --git a/ref-filter.c b/ref-filter.c
index cc25c85..7ab34be 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -55,6 +55,7 @@ static struct {
 	{ "flag" },
 	{ "HEAD" },
 	{ "color" },
+	{ "padright" },
 };
 
 /*
@@ -691,6 +692,18 @@ static void populate_value(struct ref_array_item *ref)
 			else
 				v->s = " ";
 			continue;
+		} else if (starts_with(name, "padright:")) {
+			const char *valp = NULL;
+
+			skip_prefix(name, "padright:", &valp);
+			if (!valp[0])
+				die(_("no value given with 'padright:'"));
+			if (strtoul_ui(valp, 10, (unsigned int *)&v->ul))
+				die(_("positive integer expected after ':' in padright:%u\n"),
+				    (unsigned int)v->ul);
+			v->pseudo_atom = 1;
+			v->pad_to_right = 1;
+			continue;
 		} else
 			continue;
 
@@ -1202,6 +1215,15 @@ static void ref_formatting(struct ref_formatting_state *state,
 		free(state->color);
 		state->color = NULL;
 	}
+	if (state->pad_to_right) {
+		if (!is_utf8(v->s))
+			strbuf_addf(value, "%-*s", state->pad_to_right, v->s);
+		else {
+			int utf8_compensation = strlen(v->s) - utf8_strwidth(v->s);
+			strbuf_addf(value, "%-*s", state->pad_to_right + utf8_compensation, v->s);
+		}
+		return;
+	}
 	strbuf_addf(value, "%s", v->s);
 }
 
@@ -1278,6 +1300,8 @@ static void apply_pseudo_state(struct ref_formatting_state *state,
 {
 	if (v->color)
 		state->color = (char *)v->s;
+	if (v->pad_to_right)
+		state->pad_to_right = v->ul;
 }
 
 void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
diff --git a/ref-filter.h b/ref-filter.h
index 7687879..63b8175 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -20,12 +20,14 @@ struct atom_value {
 	const char *s;
 	unsigned long ul; /* used for sorting when not FIELD_STR */
 	unsigned int pseudo_atom : 1, /*  atoms which aren't placeholders for ref attributes */
-		color : 1;
+		color : 1,
+		pad_to_right : 1;
 };
 
 struct ref_formatting_state {
 	int quote_style;
 	char *color;
+	unsigned int pad_to_right;
 };
 
 struct ref_sorting {
diff --git a/t/t6302-for-each-ref-filter.sh b/t/t6302-for-each-ref-filter.sh
index 505a360..daaa27a 100755
--- a/t/t6302-for-each-ref-filter.sh
+++ b/t/t6302-for-each-ref-filter.sh
@@ -81,4 +81,20 @@ test_expect_success 'filtering with --contains' '
 	test_cmp expect actual
 '
 
+test_expect_success 'padding to the right using `padright`' '
+	cat >expect <<-\EOF &&
+	refs/heads/master        |
+	refs/heads/side          |
+	refs/odd/spot            |
+	refs/tags/double-tag     |
+	refs/tags/four           |
+	refs/tags/one            |
+	refs/tags/signed-tag     |
+	refs/tags/three          |
+	refs/tags/two            |
+	EOF
+	git for-each-ref --format="%(padright:25)%(refname)|" >actual &&
+	test_cmp expect actual
+'
+
 test_done
-- 
2.4.6

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [PATCH v5 04/11] ref-filter: add option to filter only tags
  2015-07-27  7:27 ` [PATCH v5 01/11] ref-filter: introduce 'ref_formatting_state' Karthik Nayak
  2015-07-27  7:27   ` [PATCH v5 02/11] ref-filter: make `color` use `ref_formatting_state` Karthik Nayak
  2015-07-27  7:27   ` [PATCH v5 03/11] ref-filter: add option to pad atoms to the right Karthik Nayak
@ 2015-07-27  7:27   ` Karthik Nayak
  2015-07-27  7:27   ` [PATCH v5 05/11] ref-filter: support printing N lines from tag annotation Karthik Nayak
                     ` (7 subsequent siblings)
  10 siblings, 0 replies; 38+ messages in thread
From: Karthik Nayak @ 2015-07-27  7:27 UTC (permalink / raw)
  To: git; +Cc: christian.couder, Matthieu.Moy, gitster, Karthik Nayak

From: Karthik Nayak <karthik.188@gmail.com>

Add a functions called 'for_each_tag_ref_fullpath()' to refs.{c,h}
which iterates through each tag ref without trimming the path.

Add an option in 'filter_refs()' to use 'for_each_tag_ref_fullpath()'
and filter refs. This type checking is done by adding a
'FILTER_REFS_TAGS' in 'ref-filter.h'

Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 ref-filter.c | 2 ++
 ref-filter.h | 1 +
 refs.c       | 5 +++++
 refs.h       | 1 +
 4 files changed, 9 insertions(+)

diff --git a/ref-filter.c b/ref-filter.c
index 7ab34be..6e01b44 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -1152,6 +1152,8 @@ int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int
 		ret = for_each_rawref(ref_filter_handler, &ref_cbdata);
 	else if (type & FILTER_REFS_ALL)
 		ret = for_each_ref(ref_filter_handler, &ref_cbdata);
+	else if (type & FILTER_REFS_TAGS)
+		ret = for_each_tag_ref_fullpath(ref_filter_handler, &ref_cbdata);
 	else if (type)
 		die("filter_refs: invalid type");
 
diff --git a/ref-filter.h b/ref-filter.h
index 63b8175..62ad979 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -15,6 +15,7 @@
 
 #define FILTER_REFS_INCLUDE_BROKEN 0x1
 #define FILTER_REFS_ALL 0x2
+#define FILTER_REFS_TAGS 0x4
 
 struct atom_value {
 	const char *s;
diff --git a/refs.c b/refs.c
index 0b96ece..23ce483 100644
--- a/refs.c
+++ b/refs.c
@@ -2108,6 +2108,11 @@ int for_each_tag_ref(each_ref_fn fn, void *cb_data)
 	return for_each_ref_in("refs/tags/", fn, cb_data);
 }
 
+int for_each_tag_ref_fullpath(each_ref_fn fn, void *cb_data)
+{
+	return do_for_each_ref(&ref_cache, "refs/tags/", fn, 0, 0, cb_data);
+}
+
 int for_each_tag_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
 {
 	return for_each_ref_in_submodule(submodule, "refs/tags/", fn, cb_data);
diff --git a/refs.h b/refs.h
index e4e46c3..9eee2de 100644
--- a/refs.h
+++ b/refs.h
@@ -174,6 +174,7 @@ extern int head_ref(each_ref_fn fn, void *cb_data);
 extern int for_each_ref(each_ref_fn fn, void *cb_data);
 extern int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data);
 extern int for_each_tag_ref(each_ref_fn fn, void *cb_data);
+extern int for_each_tag_ref_fullpath(each_ref_fn fn, void *cb_data);
 extern int for_each_branch_ref(each_ref_fn fn, void *cb_data);
 extern int for_each_remote_ref(each_ref_fn fn, void *cb_data);
 extern int for_each_replace_ref(each_ref_fn fn, void *cb_data);
-- 
2.4.6

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [PATCH v5 05/11] ref-filter: support printing N lines from tag annotation
  2015-07-27  7:27 ` [PATCH v5 01/11] ref-filter: introduce 'ref_formatting_state' Karthik Nayak
                     ` (2 preceding siblings ...)
  2015-07-27  7:27   ` [PATCH v5 04/11] ref-filter: add option to filter only tags Karthik Nayak
@ 2015-07-27  7:27   ` Karthik Nayak
  2015-07-27  7:27   ` [PATCH v5 06/11] ref-filter: add support to sort by version Karthik Nayak
                     ` (6 subsequent siblings)
  10 siblings, 0 replies; 38+ messages in thread
From: Karthik Nayak @ 2015-07-27  7:27 UTC (permalink / raw)
  To: git; +Cc: christian.couder, Matthieu.Moy, gitster, Karthik Nayak

From: Karthik Nayak <karthik.188@gmail.com>

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

Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 builtin/for-each-ref.c |  2 +-
 builtin/tag.c          |  4 ++++
 ref-filter.c           | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-
 ref-filter.h           |  9 +++++++--
 4 files changed, 62 insertions(+), 4 deletions(-)

diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index 40f343b..e4a4f8a 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -74,7 +74,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 	if (!maxcount || array.nr < maxcount)
 		maxcount = array.nr;
 	for (i = 0; i < maxcount; i++)
-		show_ref_array_item(array.items[i], format, quote_style);
+		show_ref_array_item(array.items[i], format, quote_style, 0);
 	ref_array_clear(&array);
 	return 0;
 }
diff --git a/builtin/tag.c b/builtin/tag.c
index 471d6b1..0fc7557 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -185,6 +185,10 @@ static enum contains_result contains(struct commit *candidate,
 	return contains_test(candidate, want);
 }
 
+/*
+ * Currently duplicated in ref-filter, will eventually be removed as
+ * we port tag.c to use ref-filter APIs.
+ */
 static void show_tag_lines(const struct object_id *oid, int lines)
 {
 	int i;
diff --git a/ref-filter.c b/ref-filter.c
index 6e01b44..1605252 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -1306,7 +1306,51 @@ static void apply_pseudo_state(struct ref_formatting_state *state,
 		state->pad_to_right = v->ul;
 }
 
-void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
+/*
+ * If 'lines' is greater than 0, print that many lines from the given
+ * object_id 'oid'.
+ */
+static void show_tag_lines(const struct object_id *oid, int lines)
+{
+	int i;
+	unsigned long size;
+	enum object_type type;
+	char *buf, *sp, *eol;
+	size_t len;
+
+	buf = read_sha1_file(oid->hash, &type, &size);
+	if (!buf)
+		die_errno("unable to read object %s", oid_to_hex(oid));
+	if (type != OBJ_COMMIT && type != OBJ_TAG)
+		goto free_return;
+	if (!size)
+		die("an empty %s object %s?",
+		    typename(type), oid_to_hex(oid));
+
+	/* skip header */
+	sp = strstr(buf, "\n\n");
+	if (!sp)
+		goto free_return;
+
+	/* only take up to "lines" lines, and strip the signature from a tag */
+	if (type == OBJ_TAG)
+		size = parse_signature(buf, size);
+	for (i = 0, sp += 2; i < lines && sp < buf + size; i++) {
+		if (i)
+			printf("\n    ");
+		eol = memchr(sp, '\n', size - (sp - buf));
+		len = eol ? eol - sp : size - (sp - buf);
+		fwrite(sp, len, 1, stdout);
+		if (!eol)
+			break;
+		sp = eol + 1;
+	}
+free_return:
+	free(buf);
+}
+
+void show_ref_array_item(struct ref_array_item *info, const char *format,
+			 int quote_style, unsigned int lines)
 {
 	const char *cp, *sp, *ep;
 	struct ref_formatting_state state;
@@ -1339,6 +1383,11 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu
 		resetv.s = color;
 		print_value(&state, &resetv);
 	}
+	if (lines > 0) {
+		struct object_id oid;
+		hashcpy(oid.hash, info->objectname);
+		show_tag_lines(&oid, lines);
+	}
 	putchar('\n');
 }
 
diff --git a/ref-filter.h b/ref-filter.h
index 62ad979..570fc3a 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -64,6 +64,7 @@ struct ref_filter {
 	struct commit *merge_commit;
 
 	unsigned int with_commit_tag_algo : 1;
+	unsigned int lines;
 };
 
 struct ref_filter_cbdata {
@@ -95,8 +96,12 @@ int parse_ref_filter_atom(const char *atom, const char *ep);
 int verify_ref_format(const char *format);
 /*  Sort the given ref_array as per the ref_sorting provided */
 void ref_array_sort(struct ref_sorting *sort, struct ref_array *array);
-/*  Print the ref using the given format and quote_style */
-void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style);
+/*
+ * Print the ref using the given format and quote_style. If 'lines' > 0,
+ * print that many lines of the the given ref.
+ */
+void show_ref_array_item(struct ref_array_item *info, const char *format,
+			 int quote_style, unsigned int lines);
 /*  Callback function for parsing the sort option */
 int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset);
 /*  Default sort option based on refname */
-- 
2.4.6

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [PATCH v5 06/11] ref-filter: add support to sort by version
  2015-07-27  7:27 ` [PATCH v5 01/11] ref-filter: introduce 'ref_formatting_state' Karthik Nayak
                     ` (3 preceding siblings ...)
  2015-07-27  7:27   ` [PATCH v5 05/11] ref-filter: support printing N lines from tag annotation Karthik Nayak
@ 2015-07-27  7:27   ` Karthik Nayak
  2015-07-27  7:27   ` [PATCH v5 07/11] ref-filter: add option to match literal pattern Karthik Nayak
                     ` (5 subsequent siblings)
  10 siblings, 0 replies; 38+ messages in thread
From: Karthik Nayak @ 2015-07-27  7:27 UTC (permalink / raw)
  To: git; +Cc: christian.couder, Matthieu.Moy, gitster, Karthik Nayak

From: Karthik Nayak <karthik.188@gmail.com>

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

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

Add documentation and tests for the same.

Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 Documentation/git-for-each-ref.txt |  3 +++
 ref-filter.c                       | 14 +++++++++-----
 ref-filter.h                       |  3 ++-
 t/t6302-for-each-ref-filter.sh     | 36 ++++++++++++++++++++++++++++++++++++
 4 files changed, 50 insertions(+), 6 deletions(-)

diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt
index 45dd7f8..2b60aee 100644
--- a/Documentation/git-for-each-ref.txt
+++ b/Documentation/git-for-each-ref.txt
@@ -151,6 +151,9 @@ For sorting purposes, fields with numeric values sort in numeric
 order (`objectsize`, `authordate`, `committerdate`, `taggerdate`).
 All other fields are used to sort in their byte-value order.
 
+There is also an option to sort by versions, this can be done by using
+the fieldname `version:refname` or in short `v:refname`.
+
 In any case, a field name that refers to a field inapplicable to
 the object referred by the ref does not cause an error.  It
 returns an empty string instead.
diff --git a/ref-filter.c b/ref-filter.c
index 1605252..26eb26c 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -12,6 +12,7 @@
 #include "revision.h"
 #include "utf8.h"
 #include "git-compat-util.h"
+#include "version.h"
 
 typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
 
@@ -1172,19 +1173,19 @@ static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, stru
 
 	get_ref_atom_value(a, s->atom, &va);
 	get_ref_atom_value(b, s->atom, &vb);
-	switch (cmp_type) {
-	case FIELD_STR:
+	if (s->version)
+		cmp = versioncmp(va->s, vb->s);
+	else if (cmp_type == FIELD_STR)
 		cmp = strcmp(va->s, vb->s);
-		break;
-	default:
+	else {
 		if (va->ul < vb->ul)
 			cmp = -1;
 		else if (va->ul == vb->ul)
 			cmp = 0;
 		else
 			cmp = 1;
-		break;
 	}
+
 	return (s->reverse) ? -cmp : cmp;
 }
 
@@ -1420,6 +1421,9 @@ int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
 		s->reverse = 1;
 		arg++;
 	}
+	if (skip_prefix(arg, "version:", &arg) ||
+	    skip_prefix(arg, "v:", &arg))
+		s->version = 1;
 	len = strlen(arg);
 	s->atom = parse_ref_filter_atom(arg, arg+len);
 	return 0;
diff --git a/ref-filter.h b/ref-filter.h
index 570fc3a..5406721 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -34,7 +34,8 @@ struct ref_formatting_state {
 struct ref_sorting {
 	struct ref_sorting *next;
 	int atom; /* index into used_atom array (internal) */
-	unsigned reverse : 1;
+	unsigned reverse : 1,
+		version : 1;
 };
 
 struct ref_array_item {
diff --git a/t/t6302-for-each-ref-filter.sh b/t/t6302-for-each-ref-filter.sh
index daaa27a..de872db 100755
--- a/t/t6302-for-each-ref-filter.sh
+++ b/t/t6302-for-each-ref-filter.sh
@@ -97,4 +97,40 @@ test_expect_success 'padding to the right using `padright`' '
 	test_cmp expect actual
 '
 
+test_expect_success 'setup for version sort' '
+	test_commit foo1.3 &&
+	test_commit foo1.6 &&
+	test_commit foo1.10
+'
+
+test_expect_success 'version sort' '
+	git for-each-ref --sort=version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
+	cat >expect <<-\EOF &&
+	foo1.3
+	foo1.6
+	foo1.10
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'version sort (shortened)' '
+	git for-each-ref --sort=v:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
+	cat >expect <<-\EOF &&
+	foo1.3
+	foo1.6
+	foo1.10
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'reverse version sort' '
+	git for-each-ref --sort=-version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
+	cat >expect <<-\EOF &&
+	foo1.10
+	foo1.6
+	foo1.3
+	EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
2.4.6

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [PATCH v5 07/11] ref-filter: add option to match literal pattern
  2015-07-27  7:27 ` [PATCH v5 01/11] ref-filter: introduce 'ref_formatting_state' Karthik Nayak
                     ` (4 preceding siblings ...)
  2015-07-27  7:27   ` [PATCH v5 06/11] ref-filter: add support to sort by version Karthik Nayak
@ 2015-07-27  7:27   ` Karthik Nayak
  2015-07-27 12:54     ` Matthieu Moy
  2015-07-28 21:49     ` Eric Sunshine
  2015-07-27  7:27   ` [PATCH v5 08/11] tag.c: use 'ref-filter' data structures Karthik Nayak
                     ` (4 subsequent siblings)
  10 siblings, 2 replies; 38+ messages in thread
From: Karthik Nayak @ 2015-07-27  7:27 UTC (permalink / raw)
  To: git; +Cc: christian.couder, Matthieu.Moy, gitster, Karthik Nayak

From: Karthik Nayak <karthik.188@gmail.com>

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

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

Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 builtin/for-each-ref.c |  1 +
 ref-filter.c           | 38 +++++++++++++++++++++++++++++++++++++-
 ref-filter.h           |  3 ++-
 3 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index e4a4f8a..3ad6a64 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -68,6 +68,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 	git_config(git_default_config, NULL);
 
 	filter.name_patterns = argv;
+	filter.match_as_path = 1;
 	filter_refs(&array, &filter, FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN);
 	ref_array_sort(sorting, &array);
 
diff --git a/ref-filter.c b/ref-filter.c
index 26eb26c..597b189 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -946,6 +946,32 @@ static int commit_contains(struct ref_filter *filter, struct commit *commit)
 
 /*
  * Return 1 if the refname matches one of the patterns, otherwise 0.
+ * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
+ * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
+ * matches "refs/heads/mas*", too).
+ */
+static int match_pattern(const char **patterns, const char *refname)
+{
+	/*
+	 * When no '--format' option is given we need to skip the prefix
+	 * for matching refs of tags and branches.
+	 */
+	if (skip_prefix(refname, "refs/tags/", &refname))
+		;
+	else if (skip_prefix(refname, "refs/heads/", &refname))
+		;
+	else if (skip_prefix(refname, "refs/remotes/", &refname))
+		;
+
+	for (; *patterns; patterns++) {
+		if (!wildmatch(*patterns, refname, 0, NULL))
+			return 1;
+	}
+	return 0;
+}
+
+/*
+ * Return 1 if the refname matches one of the patterns, otherwise 0.
  * A pattern can be path prefix (e.g. a refname "refs/heads/master"
  * matches a pattern "refs/heads/") or a wildcard (e.g. the same ref
  * matches "refs/heads/m*",too).
@@ -969,6 +995,16 @@ static int match_name_as_path(const char **pattern, const char *refname)
 	return 0;
 }
 
+/* Return 1 if the refname matches one of the patterns, otherwise 0. */
+static int filter_pattern_match(struct ref_filter *filter, const char *refname)
+{
+	if (!*filter->name_patterns)
+		return 1; /* No pattern always matches */
+	if (filter->match_as_path)
+		return match_name_as_path(filter->name_patterns, refname);
+	return match_pattern(filter->name_patterns, refname);
+}
+
 /*
  * Given a ref (sha1, refname), check if the ref belongs to the array
  * of sha1s. If the given ref is a tag, check if the given tag points
@@ -1037,7 +1073,7 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
 		return 0;
 	}
 
-	if (*filter->name_patterns && !match_name_as_path(filter->name_patterns, refname))
+	if (!filter_pattern_match(filter, refname))
 		return 0;
 
 	if (filter->points_at.nr && !match_points_at(&filter->points_at, oid->hash, refname))
diff --git a/ref-filter.h b/ref-filter.h
index 5406721..a27745f 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -64,7 +64,8 @@ struct ref_filter {
 	} merge;
 	struct commit *merge_commit;
 
-	unsigned int with_commit_tag_algo : 1;
+	unsigned int with_commit_tag_algo : 1,
+		match_as_path : 1;
 	unsigned int lines;
 };
 
-- 
2.4.6

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [PATCH v5 08/11] tag.c: use 'ref-filter' data structures
  2015-07-27  7:27 ` [PATCH v5 01/11] ref-filter: introduce 'ref_formatting_state' Karthik Nayak
                     ` (5 preceding siblings ...)
  2015-07-27  7:27   ` [PATCH v5 07/11] ref-filter: add option to match literal pattern Karthik Nayak
@ 2015-07-27  7:27   ` Karthik Nayak
  2015-07-27  7:27   ` [PATCH v5 09/11] tag.c: use 'ref-filter' APIs Karthik Nayak
                     ` (3 subsequent siblings)
  10 siblings, 0 replies; 38+ messages in thread
From: Karthik Nayak @ 2015-07-27  7:27 UTC (permalink / raw)
  To: git; +Cc: christian.couder, Matthieu.Moy, gitster, Karthik Nayak

From: Karthik Nayak <karthik.188@gmail.com>

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

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

Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 builtin/tag.c | 106 +++++++++++++++++++++++++++++++---------------------------
 1 file changed, 57 insertions(+), 49 deletions(-)

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

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [PATCH v5 09/11] tag.c: use 'ref-filter' APIs
  2015-07-27  7:27 ` [PATCH v5 01/11] ref-filter: introduce 'ref_formatting_state' Karthik Nayak
                     ` (6 preceding siblings ...)
  2015-07-27  7:27   ` [PATCH v5 08/11] tag.c: use 'ref-filter' data structures Karthik Nayak
@ 2015-07-27  7:27   ` Karthik Nayak
  2015-07-27  7:27   ` [PATCH v5 10/11] tag.c: implement '--format' option Karthik Nayak
                     ` (2 subsequent siblings)
  10 siblings, 0 replies; 38+ messages in thread
From: Karthik Nayak @ 2015-07-27  7:27 UTC (permalink / raw)
  To: git; +Cc: christian.couder, Matthieu.Moy, gitster, Karthik Nayak

From: Karthik Nayak <karthik.188@gmail.com>

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

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

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

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

Modify documentation for the same.

Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 Documentation/git-tag.txt |  16 ++-
 builtin/tag.c             | 342 ++++++----------------------------------------
 t/t7004-tag.sh            |   8 +-
 3 files changed, 50 insertions(+), 316 deletions(-)

diff --git a/Documentation/git-tag.txt b/Documentation/git-tag.txt
index 84f6496..3ac4a96 100644
--- a/Documentation/git-tag.txt
+++ b/Documentation/git-tag.txt
@@ -13,7 +13,7 @@ SYNOPSIS
 	<tagname> [<commit> | <object>]
 'git tag' -d <tagname>...
 'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>]
-	[--column[=<options>] | --no-column] [--create-reflog] [<pattern>...]
+	[--column[=<options>] | --no-column] [--create-reflog] [--sort=<key>] [<pattern>...]
 'git tag' -v <tagname>...
 
 DESCRIPTION
@@ -94,14 +94,16 @@ OPTIONS
 	using fnmatch(3)).  Multiple patterns may be given; if any of
 	them matches, the tag is shown.
 
---sort=<type>::
-	Sort in a specific order. Supported type is "refname"
-	(lexicographic order), "version:refname" or "v:refname" (tag
+--sort=<key>::
+	Sort based on the key given.  Prefix `-` to sort in
+	descending order of the value. You may use the --sort=<key> option
+	multiple times, in which case the last key becomes the primary
+	key. Also supports "version:refname" or "v:refname" (tag
 	names are treated as versions). The "version:refname" sort
 	order can also be affected by the
-	"versionsort.prereleaseSuffix" configuration variable. Prepend
-	"-" to reverse sort order. When this option is not given, the
-	sort order defaults to the value configured for the 'tag.sort'
+	"versionsort.prereleaseSuffix" configuration variable.
+	The keys supported are the same as those in `git for-each-ref`.
+	Sort order defaults to the value configured for the 'tag.sort'
 	variable if it exists, or lexicographic order otherwise. See
 	linkgit:git-config[1].
 
diff --git a/builtin/tag.c b/builtin/tag.c
index e96bae2..1e8d1b2 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -28,278 +28,32 @@ static const char * const git_tag_usage[] = {
 	NULL
 };
 
-#define STRCMP_SORT     0	/* must be zero */
-#define VERCMP_SORT     1
-#define SORT_MASK       0x7fff
-#define REVERSE_SORT    0x8000
-
-static int tag_sort;
-
 static unsigned int colopts;
 
-static int match_pattern(const char **patterns, const char *ref)
-{
-	/* no pattern means match everything */
-	if (!*patterns)
-		return 1;
-	for (; *patterns; patterns++)
-		if (!wildmatch(*patterns, ref, 0, NULL))
-			return 1;
-	return 0;
-}
-
-/*
- * This is currently duplicated in ref-filter.c, and will eventually be
- * removed as we port tag.c to use the ref-filter APIs.
- */
-static const unsigned char *match_points_at(const char *refname,
-					    const unsigned char *sha1,
-					    struct sha1_array *points_at)
-{
-	const unsigned char *tagged_sha1 = NULL;
-	struct object *obj;
-
-	if (sha1_array_lookup(points_at, sha1) >= 0)
-		return sha1;
-	obj = parse_object(sha1);
-	if (!obj)
-		die(_("malformed object at '%s'"), refname);
-	if (obj->type == OBJ_TAG)
-		tagged_sha1 = ((struct tag *)obj)->tagged->sha1;
-	if (tagged_sha1 && sha1_array_lookup(points_at, tagged_sha1) >= 0)
-		return tagged_sha1;
-	return NULL;
-}
-
-static int in_commit_list(const struct commit_list *want, struct commit *c)
-{
-	for (; want; want = want->next)
-		if (!hashcmp(want->item->object.sha1, c->object.sha1))
-			return 1;
-	return 0;
-}
-
-/*
- * The entire code segment for supporting the --contains option has been
- * copied over to ref-filter.{c,h}. This will be deleted evetually when
- * we port tag.c to use ref-filter APIs.
- */
-enum contains_result {
-	CONTAINS_UNKNOWN = -1,
-	CONTAINS_NO = 0,
-	CONTAINS_YES = 1
-};
-
-/*
- * Test whether the candidate or one of its parents is contained in the list.
- * Do not recurse to find out, though, but return -1 if inconclusive.
- */
-static enum contains_result contains_test(struct commit *candidate,
-			    const struct commit_list *want)
-{
-	/* was it previously marked as containing a want commit? */
-	if (candidate->object.flags & TMP_MARK)
-		return 1;
-	/* or marked as not possibly containing a want commit? */
-	if (candidate->object.flags & UNINTERESTING)
-		return 0;
-	/* or are we it? */
-	if (in_commit_list(want, candidate)) {
-		candidate->object.flags |= TMP_MARK;
-		return 1;
-	}
-
-	if (parse_commit(candidate) < 0)
-		return 0;
-
-	return -1;
-}
-
-/*
- * Mimicking the real stack, this stack lives on the heap, avoiding stack
- * overflows.
- *
- * At each recursion step, the stack items points to the commits whose
- * ancestors are to be inspected.
- */
-struct stack {
-	int nr, alloc;
-	struct stack_entry {
-		struct commit *commit;
-		struct commit_list *parents;
-	} *stack;
-};
-
-static void push_to_stack(struct commit *candidate, struct stack *stack)
-{
-	int index = stack->nr++;
-	ALLOC_GROW(stack->stack, stack->nr, stack->alloc);
-	stack->stack[index].commit = candidate;
-	stack->stack[index].parents = candidate->parents;
-}
-
-static enum contains_result contains(struct commit *candidate,
-		const struct commit_list *want)
-{
-	struct stack stack = { 0, 0, NULL };
-	int result = contains_test(candidate, want);
-
-	if (result != CONTAINS_UNKNOWN)
-		return result;
-
-	push_to_stack(candidate, &stack);
-	while (stack.nr) {
-		struct stack_entry *entry = &stack.stack[stack.nr - 1];
-		struct commit *commit = entry->commit;
-		struct commit_list *parents = entry->parents;
-
-		if (!parents) {
-			commit->object.flags |= UNINTERESTING;
-			stack.nr--;
-		}
-		/*
-		 * If we just popped the stack, parents->item has been marked,
-		 * therefore contains_test will return a meaningful 0 or 1.
-		 */
-		else switch (contains_test(parents->item, want)) {
-		case CONTAINS_YES:
-			commit->object.flags |= TMP_MARK;
-			stack.nr--;
-			break;
-		case CONTAINS_NO:
-			entry->parents = parents->next;
-			break;
-		case CONTAINS_UNKNOWN:
-			push_to_stack(parents->item, &stack);
-			break;
-		}
-	}
-	free(stack.stack);
-	return contains_test(candidate, want);
-}
-
-/*
- * Currently duplicated in ref-filter, will eventually be removed as
- * we port tag.c to use ref-filter APIs.
- */
-static void show_tag_lines(const struct object_id *oid, int lines)
-{
-	int i;
-	unsigned long size;
-	enum object_type type;
-	char *buf, *sp, *eol;
-	size_t len;
-
-	buf = read_sha1_file(oid->hash, &type, &size);
-	if (!buf)
-		die_errno("unable to read object %s", oid_to_hex(oid));
-	if (type != OBJ_COMMIT && type != OBJ_TAG)
-		goto free_return;
-	if (!size)
-		die("an empty %s object %s?",
-		    typename(type), oid_to_hex(oid));
-
-	/* skip header */
-	sp = strstr(buf, "\n\n");
-	if (!sp)
-		goto free_return;
-
-	/* only take up to "lines" lines, and strip the signature from a tag */
-	if (type == OBJ_TAG)
-		size = parse_signature(buf, size);
-	for (i = 0, sp += 2; i < lines && sp < buf + size; i++) {
-		if (i)
-			printf("\n    ");
-		eol = memchr(sp, '\n', size - (sp - buf));
-		len = eol ? eol - sp : size - (sp - buf);
-		fwrite(sp, len, 1, stdout);
-		if (!eol)
-			break;
-		sp = eol + 1;
-	}
-free_return:
-	free(buf);
-}
-
-static void ref_array_append(struct ref_array *array, const char *refname)
-{
-	size_t len = strlen(refname);
-	struct ref_array_item *ref = xcalloc(1, sizeof(struct ref_array_item) + len + 1);
-	memcpy(ref->refname, refname, len);
-	ref->refname[len] = '\0';
-	REALLOC_ARRAY(array->items, array->nr + 1);
-	array->items[array->nr++] = ref;
-}
-
-static int show_reference(const char *refname, const struct object_id *oid,
-			  int flag, void *cb_data)
-{
-	struct ref_filter_cbdata *data = cb_data;
-	struct ref_array *array = data->array;
-	struct ref_filter *filter = data->filter;
-
-	if (match_pattern(filter->name_patterns, refname)) {
-		if (filter->with_commit) {
-			struct commit *commit;
-
-			commit = lookup_commit_reference_gently(oid->hash, 1);
-			if (!commit)
-				return 0;
-			if (!contains(commit, filter->with_commit))
-				return 0;
-		}
-
-		if (filter->points_at.nr && !match_points_at(refname, oid->hash, &filter->points_at))
-			return 0;
-
-		if (!filter->lines) {
-			if (tag_sort)
-				ref_array_append(array, refname);
-			else
-				printf("%s\n", refname);
-			return 0;
-		}
-		printf("%-15s ", refname);
-		show_tag_lines(oid, filter->lines);
-		putchar('\n');
-	}
-
-	return 0;
-}
-
-static int sort_by_version(const void *a_, const void *b_)
-{
-	const struct ref_array_item *a = *((struct ref_array_item **)a_);
-	const struct ref_array_item *b = *((struct ref_array_item **)b_);
-	return versioncmp(a->refname, b->refname);
-}
-
-static int list_tags(struct ref_filter *filter, int sort)
+static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting)
 {
 	struct ref_array array;
-	struct ref_filter_cbdata data;
+	char *format;
+	int i;
 
 	memset(&array, 0, sizeof(array));
-	data.array = &array;
-	data.filter = filter;
 
 	if (filter->lines == -1)
 		filter->lines = 0;
 
-	for_each_tag_ref(show_reference, &data);
-	if (sort) {
-		int i;
-		if ((sort & SORT_MASK) == VERCMP_SORT)
-			qsort(array.items, array.nr,
-			      sizeof(struct ref_array_item *), sort_by_version);
-		if (sort & REVERSE_SORT)
-			for (i = array.nr - 1; i >= 0; i--)
-				printf("%s\n", array.items[i]->refname);
-		else
-			for (i = 0; i < array.nr; i++)
-				printf("%s\n", array.items[i]->refname);
-		ref_array_clear(&array);
-	}
+	if (filter->lines)
+		format = "%(padright:16)%(refname:short)";
+	else
+		format = "%(refname:short)";
+
+	verify_ref_format(format);
+	filter_refs(&array, filter, FILTER_REFS_TAGS);
+	ref_array_sort(sorting, &array);
+
+	for (i = 0; i < array.nr; i++)
+		show_ref_array_item(array.items[i], format, QUOTE_NONE, filter->lines);
+	ref_array_clear(&array);
+
 	return 0;
 }
 
@@ -366,35 +120,26 @@ static const char tag_template_nocleanup[] =
 	"Lines starting with '%c' will be kept; you may remove them"
 	" yourself if you want to.\n");
 
-/*
- * Parse a sort string, and return 0 if parsed successfully. Will return
- * non-zero when the sort string does not parse into a known type. If var is
- * given, the error message becomes a warning and includes information about
- * the configuration value.
- */
-static int parse_sort_string(const char *var, const char *arg, int *sort)
+/* Parse arg given and add it the ref_sorting array */
+static int parse_sorting_string(const char *arg, struct ref_sorting **sorting_tail)
 {
-	int type = 0, flags = 0;
-
-	if (skip_prefix(arg, "-", &arg))
-		flags |= REVERSE_SORT;
+	struct ref_sorting *s;
+	int len;
 
-	if (skip_prefix(arg, "version:", &arg) || skip_prefix(arg, "v:", &arg))
-		type = VERCMP_SORT;
-	else
-		type = STRCMP_SORT;
+	s = xcalloc(1, sizeof(*s));
+	s->next = *sorting_tail;
+	*sorting_tail = s;
 
-	if (strcmp(arg, "refname")) {
-		if (!var)
-			return error(_("unsupported sort specification '%s'"), arg);
-		else {
-			warning(_("unsupported sort specification '%s' in variable '%s'"),
-				var, arg);
-			return -1;
-		}
+	if (*arg == '-') {
+		s->reverse = 1;
+		arg++;
 	}
+	if (skip_prefix(arg, "version:", &arg) ||
+	    skip_prefix(arg, "v:", &arg))
+		s->version = 1;
 
-	*sort = (type | flags);
+	len = strlen(arg);
+	s->atom = parse_ref_filter_atom(arg, arg+len);
 
 	return 0;
 }
@@ -402,11 +147,12 @@ static int parse_sort_string(const char *var, const char *arg, int *sort)
 static int git_tag_config(const char *var, const char *value, void *cb)
 {
 	int status;
+	struct ref_sorting **sorting_tail = (struct ref_sorting **)cb;
 
 	if (!strcmp(var, "tag.sort")) {
 		if (!value)
 			return config_error_nonbool(var);
-		parse_sort_string(var, value, &tag_sort);
+		parse_sorting_string(value, sorting_tail);
 		return 0;
 	}
 
@@ -564,13 +310,6 @@ static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
 	return check_refname_format(sb->buf, 0);
 }
 
-static int parse_opt_sort(const struct option *opt, const char *arg, int unset)
-{
-	int *sort = opt->value;
-
-	return parse_sort_string(NULL, arg, sort);
-}
-
 int cmd_tag(int argc, const char **argv, const char *prefix)
 {
 	struct strbuf buf = STRBUF_INIT;
@@ -587,6 +326,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 	struct ref_transaction *transaction;
 	struct strbuf err = STRBUF_INIT;
 	struct ref_filter filter;
+	static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
 	struct option options[] = {
 		OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
 		{ OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"),
@@ -613,10 +353,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
 		OPT_CONTAINS(&filter.with_commit, N_("print only tags that contain the commit")),
 		OPT_WITH(&filter.with_commit, N_("print only tags that contain the commit")),
-		{
-			OPTION_CALLBACK, 0, "sort", &tag_sort, N_("type"), N_("sort tags"),
-			PARSE_OPT_NONEG, parse_opt_sort
-		},
+		OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
+			     N_("field name to sort on"), &parse_opt_ref_sorting),
 		{
 			OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
 			N_("print only tags of the object"), 0, parse_opt_object_name
@@ -624,7 +362,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		OPT_END()
 	};
 
-	git_config(git_tag_config, NULL);
+	git_config(git_tag_config, sorting_tail);
 
 	memset(&opt, 0, sizeof(opt));
 	memset(&filter, 0, sizeof(filter));
@@ -650,6 +388,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 			die(_("--column and -n are incompatible"));
 		colopts = 0;
 	}
+	if (!sorting)
+		sorting = ref_default_sorting();
 	if (cmdmode == 'l') {
 		int ret;
 		if (column_active(colopts)) {
@@ -658,10 +398,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 			copts.padding = 2;
 			run_column_filter(colopts, &copts);
 		}
-		if (filter.lines != -1 && tag_sort)
-			die(_("--sort and -n are incompatible"));
 		filter.name_patterns = argv;
-		ret = list_tags(&filter, tag_sort);
+		ret = list_tags(&filter, sorting);
 		if (column_active(colopts))
 			stop_column_filter();
 		return ret;
diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
index d31788c..1f066aa 100755
--- a/t/t7004-tag.sh
+++ b/t/t7004-tag.sh
@@ -1462,13 +1462,7 @@ test_expect_success 'invalid sort parameter on command line' '
 
 test_expect_success 'invalid sort parameter in configuratoin' '
 	git config tag.sort "v:notvalid" &&
-	git tag -l "foo*" >actual &&
-	cat >expect <<-\EOF &&
-	foo1.10
-	foo1.3
-	foo1.6
-	EOF
-	test_cmp expect actual
+	test_must_fail git tag -l "foo*" >actual
 '
 
 test_expect_success 'version sort with prerelease reordering' '
-- 
2.4.6

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [PATCH v5 10/11] tag.c: implement '--format' option
  2015-07-27  7:27 ` [PATCH v5 01/11] ref-filter: introduce 'ref_formatting_state' Karthik Nayak
                     ` (7 preceding siblings ...)
  2015-07-27  7:27   ` [PATCH v5 09/11] tag.c: use 'ref-filter' APIs Karthik Nayak
@ 2015-07-27  7:27   ` Karthik Nayak
  2015-07-27  7:27   ` [PATCH v5 11/11] tag.c: implement '--merged' and '--no-merged' options Karthik Nayak
  2015-07-27 12:42   ` [PATCH v5 01/11] ref-filter: introduce 'ref_formatting_state' Matthieu Moy
  10 siblings, 0 replies; 38+ messages in thread
From: Karthik Nayak @ 2015-07-27  7:27 UTC (permalink / raw)
  To: git; +Cc: christian.couder, Matthieu.Moy, gitster, Karthik Nayak

From: Karthik Nayak <karthik.188@gmail.com>

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

Add tests and documentation for the same.

Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 Documentation/git-tag.txt | 15 ++++++++++++++-
 builtin/tag.c             | 11 +++++++----
 t/t7004-tag.sh            | 16 ++++++++++++++++
 3 files changed, 37 insertions(+), 5 deletions(-)

diff --git a/Documentation/git-tag.txt b/Documentation/git-tag.txt
index 3ac4a96..75703c5 100644
--- a/Documentation/git-tag.txt
+++ b/Documentation/git-tag.txt
@@ -13,7 +13,8 @@ SYNOPSIS
 	<tagname> [<commit> | <object>]
 'git tag' -d <tagname>...
 'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>]
-	[--column[=<options>] | --no-column] [--create-reflog] [--sort=<key>] [<pattern>...]
+	[--column[=<options>] | --no-column] [--create-reflog] [--sort=<key>]
+	[--format=<format>] [<pattern>...]
 'git tag' -v <tagname>...
 
 DESCRIPTION
@@ -158,6 +159,18 @@ This option is only applicable when listing tags without annotation lines.
 	The object that the new tag will refer to, usually a commit.
 	Defaults to HEAD.
 
+<format>::
+	A string that interpolates `%(fieldname)` from the object
+	pointed at by a ref being shown.  If `fieldname` is prefixed
+	with an asterisk (`*`) and the ref points at a tag object, the
+	value for the field in the object tag refers is used.  When
+	unspecified, defaults to `%(refname:short)`.  It also
+	interpolates `%%` to `%`, and `%xx` where `xx` are hex digits
+	interpolates to character with hex code `xx`; for example
+	`%00` interpolates to `\0` (NUL), `%09` to `\t` (TAB) and
+	`%0a` to `\n` (LF).  The fields are same as those in `git
+	for-each-ref`.
+
 
 CONFIGURATION
 -------------
diff --git a/builtin/tag.c b/builtin/tag.c
index 1e8d1b2..7de49c4 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -30,10 +30,9 @@ static const char * const git_tag_usage[] = {
 
 static unsigned int colopts;
 
-static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting)
+static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, const char *format)
 {
 	struct ref_array array;
-	char *format;
 	int i;
 
 	memset(&array, 0, sizeof(array));
@@ -43,7 +42,7 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting)
 
 	if (filter->lines)
 		format = "%(padright:16)%(refname:short)";
-	else
+	else if (!format)
 		format = "%(refname:short)";
 
 	verify_ref_format(format);
@@ -327,6 +326,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 	struct strbuf err = STRBUF_INIT;
 	struct ref_filter filter;
 	static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
+	const char *format = NULL;
 	struct option options[] = {
 		OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
 		{ OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"),
@@ -359,6 +359,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 			OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
 			N_("print only tags of the object"), 0, parse_opt_object_name
 		},
+		OPT_STRING(  0 , "format", &format, N_("format"), N_("format to use for the output")),
 		OPT_END()
 	};
 
@@ -398,8 +399,10 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 			copts.padding = 2;
 			run_column_filter(colopts, &copts);
 		}
+		if (format && (filter.lines != -1))
+			die(_("--format and -n are incompatible"));
 		filter.name_patterns = argv;
-		ret = list_tags(&filter, sorting);
+		ret = list_tags(&filter, sorting, format);
 		if (column_active(colopts))
 			stop_column_filter();
 		return ret;
diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
index 1f066aa..1809011 100755
--- a/t/t7004-tag.sh
+++ b/t/t7004-tag.sh
@@ -1519,4 +1519,20 @@ EOF"
 	test_cmp expect actual
 '
 
+test_expect_success '--format cannot be used with -n' '
+	test_must_fail git tag -l -n4 --format="%(refname)"
+'
+
+test_expect_success '--format should list tags as per format given' '
+	cat >expect <<-\EOF &&
+	refname : refs/tags/foo1.10
+	refname : refs/tags/foo1.3
+	refname : refs/tags/foo1.6
+	refname : refs/tags/foo1.6-rc1
+	refname : refs/tags/foo1.6-rc2
+	EOF
+	git tag -l --format="refname : %(refname)" "foo*" >actual &&
+	test_cmp expect actual
+'
+
 test_done
-- 
2.4.6

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [PATCH v5 11/11] tag.c: implement '--merged' and '--no-merged' options
  2015-07-27  7:27 ` [PATCH v5 01/11] ref-filter: introduce 'ref_formatting_state' Karthik Nayak
                     ` (8 preceding siblings ...)
  2015-07-27  7:27   ` [PATCH v5 10/11] tag.c: implement '--format' option Karthik Nayak
@ 2015-07-27  7:27   ` Karthik Nayak
  2015-07-27 12:42   ` [PATCH v5 01/11] ref-filter: introduce 'ref_formatting_state' Matthieu Moy
  10 siblings, 0 replies; 38+ messages in thread
From: Karthik Nayak @ 2015-07-27  7:27 UTC (permalink / raw)
  To: git; +Cc: christian.couder, Matthieu.Moy, gitster, Karthik Nayak

From: Karthik Nayak <karthik.188@gmail.com>

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

Add documentation and tests for the same.

Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 Documentation/git-tag.txt |  7 ++++++-
 builtin/tag.c             |  6 +++++-
 t/t7004-tag.sh            | 27 +++++++++++++++++++++++++++
 3 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/Documentation/git-tag.txt b/Documentation/git-tag.txt
index 75703c5..c2785d9 100644
--- a/Documentation/git-tag.txt
+++ b/Documentation/git-tag.txt
@@ -14,7 +14,7 @@ SYNOPSIS
 'git tag' -d <tagname>...
 'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>]
 	[--column[=<options>] | --no-column] [--create-reflog] [--sort=<key>]
-	[--format=<format>] [<pattern>...]
+	[--format=<format>] [--[no-]merged [<commit>]] [<pattern>...]
 'git tag' -v <tagname>...
 
 DESCRIPTION
@@ -171,6 +171,11 @@ This option is only applicable when listing tags without annotation lines.
 	`%0a` to `\n` (LF).  The fields are same as those in `git
 	for-each-ref`.
 
+--[no-]merged [<commit>]::
+	Only list tags whose tips are reachable, or not reachable
+	if '--no-merged' is used, from the specified commit ('HEAD'
+	if not specified).
+
 
 CONFIGURATION
 -------------
diff --git a/builtin/tag.c b/builtin/tag.c
index 7de49c4..fc01117 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -23,7 +23,7 @@ static const char * const git_tag_usage[] = {
 	N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>]"),
 	N_("git tag -d <tagname>..."),
 	N_("git tag -l [-n[<num>]] [--contains <commit>] [--points-at <object>]"
-		"\n\t\t[<pattern>...]"),
+		"\n\t\t[--[no-]merged [<commit>]] [<pattern>...]"),
 	N_("git tag -v <tagname>..."),
 	NULL
 };
@@ -353,6 +353,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
 		OPT_CONTAINS(&filter.with_commit, N_("print only tags that contain the commit")),
 		OPT_WITH(&filter.with_commit, N_("print only tags that contain the commit")),
+		OPT_MERGED(&filter, N_("print only tags that are merged")),
+		OPT_NO_MERGED(&filter, N_("print only tags that are not merged")),
 		OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
 			     N_("field name to sort on"), &parse_opt_ref_sorting),
 		{
@@ -413,6 +415,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		die(_("--contains option is only allowed with -l."));
 	if (filter.points_at.nr)
 		die(_("--points-at option is only allowed with -l."));
+	if (filter.merge_commit)
+		die(_("--merged and --no-merged option are only allowed with -l"));
 	if (cmdmode == 'd')
 		return for_each_tag_name(argv, delete_tag);
 	if (cmdmode == 'v')
diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
index 1809011..5b73539 100755
--- a/t/t7004-tag.sh
+++ b/t/t7004-tag.sh
@@ -1535,4 +1535,31 @@ test_expect_success '--format should list tags as per format given' '
 	test_cmp expect actual
 '
 
+test_expect_success 'setup --merged test tags' '
+	git tag mergetest-1 HEAD~2 &&
+	git tag mergetest-2 HEAD~1 &&
+	git tag mergetest-3 HEAD
+'
+
+test_expect_success '--merged cannot be used in non-list mode' '
+	test_must_fail git tag --merged=mergetest-2 foo
+'
+
+test_expect_success '--merged shows merged tags' '
+	cat >expect <<-\EOF &&
+	mergetest-1
+	mergetest-2
+	EOF
+	git tag -l --merged=mergetest-2 mergetest-* >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success '--no-merged show unmerged tags' '
+	cat >expect <<-\EOF &&
+	mergetest-3
+	EOF
+	git tag -l --no-merged=mergetest-2 mergetest-* >actual &&
+	test_cmp expect actual
+'
+
 test_done
-- 
2.4.6

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 01/11] ref-filter: introduce 'ref_formatting_state'
  2015-07-27  7:27 ` [PATCH v5 01/11] ref-filter: introduce 'ref_formatting_state' Karthik Nayak
                     ` (9 preceding siblings ...)
  2015-07-27  7:27   ` [PATCH v5 11/11] tag.c: implement '--merged' and '--no-merged' options Karthik Nayak
@ 2015-07-27 12:42   ` Matthieu Moy
  2015-07-27 15:28     ` Karthik Nayak
  10 siblings, 1 reply; 38+ messages in thread
From: Matthieu Moy @ 2015-07-27 12:42 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: git, christian.couder, gitster

Karthik Nayak <karthik.188@gmail.com> writes:

> +static void ref_formatting(struct ref_formatting_state *state,
> +			   struct atom_value *v, struct strbuf *value)
>  {
> -	struct strbuf sb = STRBUF_INIT;
> -	switch (quote_style) {
> +	strbuf_addf(value, "%s", v->s);
> +}

You're taking 'state' as argument, but you're not using it in the
function for now. Perhaps add a temporary comment like:

static void ref_formatting(...)
{
	/* Formatting according to 'state' will be applied here */
	strbuf_addf(...)
}

Or perhaps it's OK like this.

> -static void print_value(struct atom_value *v, int quote_style)
> +static void print_value(struct ref_formatting_state *state, struct atom_value *v)

Changing the position of the v parameter makes the patch a bit harder to
read. I would have written in this order:

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

So the patch reads as "encapsulate quote_style in a struct" more
straightforwardly.

> @@ -1257,6 +1269,10 @@ static void emit(const char *cp, const char *ep)
>  void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
>  {
>  	const char *cp, *sp, *ep;
> +	struct ref_formatting_state state;

I still found it a bit hard to read, and I would have appreciated a
comment here, like

	/*
	 * Some (pseudo) atom have no immediate side effect, but only
	 * affect the next atom. Store the relevant information from 	
	 * these atoms in the 'state' variable for use when displaying
	 * the next atom.
	 */

With this in mind, it becomes more obvious that you also need to reset
the state after using it, which you forgot to do. See:

$ ./git for-each-ref --format '%(padright:30)|%(refname)|%(refname)|' refs/tags/v2.4.\*
|refs/tags/v2.4.0              |refs/tags/v2.4.0              |
|refs/tags/v2.4.0-rc0          |refs/tags/v2.4.0-rc0          |
|refs/tags/v2.4.0-rc1          |refs/tags/v2.4.0-rc1          |
|refs/tags/v2.4.0-rc2          |refs/tags/v2.4.0-rc2          |
|refs/tags/v2.4.0-rc3          |refs/tags/v2.4.0-rc3          |
|refs/tags/v2.4.1              |refs/tags/v2.4.1              |
|refs/tags/v2.4.2              |refs/tags/v2.4.2              |
|refs/tags/v2.4.3              |refs/tags/v2.4.3              |
|refs/tags/v2.4.4              |refs/tags/v2.4.4              |
|refs/tags/v2.4.5              |refs/tags/v2.4.5              |
|refs/tags/v2.4.6              |refs/tags/v2.4.6              |

I think only the first column should have padding, not the second. You
can fix this with a patch like this:

--- a/ref-filter.c
+++ b/ref-filter.c
@@ -1431,6 +1431,14 @@ static void apply_pseudo_state(struct ref_formatting_state *state,
                state->ifexists = v->s;
 }
 
+static void reset_formatting_state(struct ref_formatting_state *state)
+{
+       int quote_style = state->quote_style;
+       memset(state, 0, sizeof(*state));
+       state->quote_style = quote_style;
+}
+
+
 /*
  * If 'lines' is greater than 0, print that many lines from the given
  * object_id 'oid'.
@@ -1492,8 +1500,11 @@ void show_ref_array_item(struct ref_array_item *info, const char *format,
                get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
                if (atomv->pseudo_atom)
                        apply_pseudo_state(&state, atomv);
-               else
+               else {
                        print_value(&state, atomv);
+                       reset_formatting_state(&state);
+               }
+
        }
        if (*cp) {
                sp = cp + strlen(cp);

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 02/11] ref-filter: make `color` use `ref_formatting_state`
  2015-07-27  7:27   ` [PATCH v5 02/11] ref-filter: make `color` use `ref_formatting_state` Karthik Nayak
@ 2015-07-27 12:47     ` Matthieu Moy
  2015-07-27 14:28       ` Junio C Hamano
  2015-07-27 16:02       ` Karthik Nayak
  2015-07-27 13:06     ` Matthieu Moy
  1 sibling, 2 replies; 38+ messages in thread
From: Matthieu Moy @ 2015-07-27 12:47 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: git, christian.couder, gitster

Karthik Nayak <karthik.188@gmail.com> writes:

> Make the `color` atom a pseudo atom and ensure that it uses
> `ref_formatting_state`.

Actually, I think this is an incorrect change.

Previously, %(color:...) was taking effect immediately, and after this
patch, it takes effect starting from the next atom.

Try this:

git for-each-ref --format '%(color:red)red %(color:reset)normal'

Before your patch, I get 'red' as red, and 'normal' as normal. After
your patch, I get no color at all, since %(color:...) just stores
information that is never used because I have no real atom.

> --- a/ref-filter.h
> +++ b/ref-filter.h
> @@ -19,11 +19,13 @@
>  struct atom_value {
>  	const char *s;
>  	unsigned long ul; /* used for sorting when not FIELD_STR */
> -	unsigned int pseudo_atom : 1; /*  atoms which aren't placeholders for ref attributes */
> +	unsigned int pseudo_atom : 1, /*  atoms which aren't placeholders for ref attributes */
> +		color : 1;
>  };

As a consequence of the remark above, I think the name and comment of
the field are misleading. There are 3 kinds of atoms:

* Placeholders for ref attributes

* Atoms that take action immediately, but that are not ref attributes
  like %(color)

* Atoms that only act as modifiers for the next atom. Perhaps they could
  be called "prefix atoms" or "modifier atoms".

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 03/11] ref-filter: add option to pad atoms to the right
  2015-07-27  7:27   ` [PATCH v5 03/11] ref-filter: add option to pad atoms to the right Karthik Nayak
@ 2015-07-27 12:50     ` Matthieu Moy
  2015-07-27 15:45       ` Junio C Hamano
  2015-07-27 18:43       ` Karthik Nayak
  2015-07-28 21:41     ` Eric Sunshine
  1 sibling, 2 replies; 38+ messages in thread
From: Matthieu Moy @ 2015-07-27 12:50 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: git, christian.couder, gitster

Karthik Nayak <karthik.188@gmail.com> writes:

> --- a/t/t6302-for-each-ref-filter.sh
> +++ b/t/t6302-for-each-ref-filter.sh
> @@ -81,4 +81,20 @@ test_expect_success 'filtering with --contains' '
>  	test_cmp expect actual
>  '
>  
> +test_expect_success 'padding to the right using `padright`' '
> +	cat >expect <<-\EOF &&
> +	refs/heads/master        |
> +	refs/heads/side          |
> +	refs/odd/spot            |
> +	refs/tags/double-tag     |
> +	refs/tags/four           |
> +	refs/tags/one            |
> +	refs/tags/signed-tag     |
> +	refs/tags/three          |
> +	refs/tags/two            |
> +	EOF
> +	git for-each-ref --format="%(padright:25)%(refname)|" >actual &&
> +	test_cmp expect actual
> +'
> +
>  test_done

See my remark on previous patch: this test is not sufficient. You do
not only need to check that %(padright) is taken into account, but you
also need to check that it is taken into account for the right atom and
only this one. I'd suggest

--format '%(refname)%(padright:25)|%(refname)|%(refname)|'

Only the middle column should be padded.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 07/11] ref-filter: add option to match literal pattern
  2015-07-27  7:27   ` [PATCH v5 07/11] ref-filter: add option to match literal pattern Karthik Nayak
@ 2015-07-27 12:54     ` Matthieu Moy
  2015-07-27 15:57       ` Karthik Nayak
  2015-07-28 21:49     ` Eric Sunshine
  1 sibling, 1 reply; 38+ messages in thread
From: Matthieu Moy @ 2015-07-27 12:54 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: git, christian.couder, gitster

Karthik Nayak <karthik.188@gmail.com> writes:

> --- a/ref-filter.c
> +++ b/ref-filter.c
> @@ -946,6 +946,32 @@ static int commit_contains(struct ref_filter *filter, struct commit *commit)

> +/*
> + * Return 1 if the refname matches one of the patterns, otherwise 0.
>   * A pattern can be path prefix (e.g. a refname "refs/heads/master"
>   * matches a pattern "refs/heads/") or a wildcard (e.g. the same ref
>   * matches "refs/heads/m*",too).

Nit: you used to s/,too/, too/ in the comment in a previous version.

I think I already suggested saying explicitly "... matches a pattern
"refs/heads/" but not "refs/heads/m")", but I won't insist on that. Just
a reminder in case you missed it.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 02/11] ref-filter: make `color` use `ref_formatting_state`
  2015-07-27  7:27   ` [PATCH v5 02/11] ref-filter: make `color` use `ref_formatting_state` Karthik Nayak
  2015-07-27 12:47     ` Matthieu Moy
@ 2015-07-27 13:06     ` Matthieu Moy
  2015-07-27 17:16       ` Karthik Nayak
  1 sibling, 1 reply; 38+ messages in thread
From: Matthieu Moy @ 2015-07-27 13:06 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: git, christian.couder, gitster

Karthik Nayak <karthik.188@gmail.com> writes:

> --- a/ref-filter.c
> +++ b/ref-filter.c
> @@ -1195,6 +1197,11 @@ void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
>  static void ref_formatting(struct ref_formatting_state *state,
>  			   struct atom_value *v, struct strbuf *value)
>  {
> +	if (state->color) {
> +		strbuf_addstr(value, state->color);
> +		free(state->color);
> +		state->color = NULL;
> +	}
>  	strbuf_addf(value, "%s", v->s);
>  }
>  
> @@ -1266,6 +1273,13 @@ static void emit(const char *cp, const char *ep)
>  	}
>  }
>  
> +static void apply_pseudo_state(struct ref_formatting_state *state,
> +			       struct atom_value *v)
> +{
> +	if (v->color)
> +		state->color = (char *)v->s;
> +}
> +
>  void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
>  {
>  	const char *cp, *sp, *ep;

It's not clear enough in the code and history that these these two
functions are symmetrical.

You can find better names. 'apply_pseudo_state' seems wrong it two ways:
it does not _apply_ the state, but it stores it. And it's a "pseudo-atom
related state", but not a "pseudo-state".

How about

ref_formatting -> apply_formatting_state
apply_pseudo_state -> store_formatting_state

?

Actually, I would even call these functions right from
show_ref_array_item, so that the result look like this:

		if (atomv->pseudo_atom)
			store_formatting_state(&state, atomv);
		else {
			apply_formatting_state(&state, atomv);
			reset_formatting_state(&state);
			print_value(&state, atomv);
		}

In the history, if you are to introduce a dumb version of ref_formatting
in PATCH 1, I think you should also introduce a dumb (actually, totally
empty) version of apply_pseudo_state. Then, further patches would just
add a few lines in each function, and ...

> @@ -1281,7 +1295,10 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu
>  		if (cp < sp)
>  			emit(cp, sp);
>  		get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
> -		print_value(&state, atomv);
> +		if (atomv->pseudo_atom)
> +			apply_pseudo_state(&state, atomv);
> +		else
> +			print_value(&state, atomv);
>  	}

... this hunk would belong to PATCH 1.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 02/11] ref-filter: make `color` use `ref_formatting_state`
  2015-07-27 12:47     ` Matthieu Moy
@ 2015-07-27 14:28       ` Junio C Hamano
  2015-07-27 17:23         ` Karthik Nayak
  2015-07-27 16:02       ` Karthik Nayak
  1 sibling, 1 reply; 38+ messages in thread
From: Junio C Hamano @ 2015-07-27 14:28 UTC (permalink / raw)
  To: Matthieu Moy; +Cc: Karthik Nayak, git, christian.couder

Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:

> Karthik Nayak <karthik.188@gmail.com> writes:
>
>> Make the `color` atom a pseudo atom and ensure that it uses
>> `ref_formatting_state`.
>
> Actually, I think this is an incorrect change.
>
> Previously, %(color:...) was taking effect immediately, and after this
> patch, it takes effect starting from the next atom.
> ...
> As a consequence of the remark above, I think the name and comment of
> the field are misleading. There are 3 kinds of atoms:
>
> * Placeholders for ref attributes
>
> * Atoms that take action immediately, but that are not ref attributes
>   like %(color)
>
> * Atoms that only act as modifiers for the next atom. Perhaps they could
>   be called "prefix atoms" or "modifier atoms".

My fault.

I briefly thought that it may be simpler to treat %(color) just as a
different way to express a literal that cannot be typed from the
keyboard, but your "different kind of atom" is a much better way to
think about it.

What is necessary is that, just like the updated "print_value()"
knows about the formatting state, "emit()" needs to be told about
the same formatting state.  Some of these "state affecting" atoms
may take effect on what is output by "emit()" (e.g. "color" is
obviously an example, the hypotethical one that counts the current
output column and uses that knowledge to "align" the output to
certain columns, instead of "right pad to make the next column
30-columns wide" one, which is in this series, is another).

Thanks for finding this, and Karthik, sorry for an incomplete
suggestion based on a faulty thinking.

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 01/11] ref-filter: introduce 'ref_formatting_state'
  2015-07-27 12:42   ` [PATCH v5 01/11] ref-filter: introduce 'ref_formatting_state' Matthieu Moy
@ 2015-07-27 15:28     ` Karthik Nayak
  0 siblings, 0 replies; 38+ messages in thread
From: Karthik Nayak @ 2015-07-27 15:28 UTC (permalink / raw)
  To: Matthieu Moy; +Cc: Git, Christian Couder, Junio C Hamano

On Mon, Jul 27, 2015 at 6:12 PM, Matthieu Moy
<Matthieu.Moy@grenoble-inp.fr> wrote:
> Karthik Nayak <karthik.188@gmail.com> writes:
>
>> +static void ref_formatting(struct ref_formatting_state *state,
>> +                        struct atom_value *v, struct strbuf *value)
>>  {
>> -     struct strbuf sb = STRBUF_INIT;
>> -     switch (quote_style) {
>> +     strbuf_addf(value, "%s", v->s);
>> +}
>
> You're taking 'state' as argument, but you're not using it in the
> function for now. Perhaps add a temporary comment like:
>
> static void ref_formatting(...)
> {
>         /* Formatting according to 'state' will be applied here */
>         strbuf_addf(...)
> }
>
> Or perhaps it's OK like this.

I thought it'd be OK since it doesn't have any adverse effect, but I added
the comment you suggested nonetheless.

>
>> -static void print_value(struct atom_value *v, int quote_style)
>> +static void print_value(struct ref_formatting_state *state, struct atom_value *v)
>
> Changing the position of the v parameter makes the patch a bit harder to
> read. I would have written in this order:
>
> static void print_value(struct atom_value *v, struct ref_formatting_state *state)
>
> So the patch reads as "encapsulate quote_style in a struct" more
> straightforwardly.
>

I need to be more careful about things like this, thanks.

>> @@ -1257,6 +1269,10 @@ static void emit(const char *cp, const char *ep)
>>  void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
>>  {
>>       const char *cp, *sp, *ep;
>> +     struct ref_formatting_state state;
>
> I still found it a bit hard to read, and I would have appreciated a
> comment here, like
>
>         /*
>          * Some (pseudo) atom have no immediate side effect, but only
>          * affect the next atom. Store the relevant information from
>          * these atoms in the 'state' variable for use when displaying
>          * the next atom.
>          */
>

This seems good, will add this.

> With this in mind, it becomes more obvious that you also need to reset
> the state after using it, which you forgot to do. See:
>
> $ ./git for-each-ref --format '%(padright:30)|%(refname)|%(refname)|' refs/tags/v2.4.\*
> |refs/tags/v2.4.0              |refs/tags/v2.4.0              |
> |refs/tags/v2.4.0-rc0          |refs/tags/v2.4.0-rc0          |
> |refs/tags/v2.4.0-rc1          |refs/tags/v2.4.0-rc1          |
> |refs/tags/v2.4.0-rc2          |refs/tags/v2.4.0-rc2          |
> |refs/tags/v2.4.0-rc3          |refs/tags/v2.4.0-rc3          |
> |refs/tags/v2.4.1              |refs/tags/v2.4.1              |
> |refs/tags/v2.4.2              |refs/tags/v2.4.2              |
> |refs/tags/v2.4.3              |refs/tags/v2.4.3              |
> |refs/tags/v2.4.4              |refs/tags/v2.4.4              |
> |refs/tags/v2.4.5              |refs/tags/v2.4.5              |
> |refs/tags/v2.4.6              |refs/tags/v2.4.6              |
>
> I think only the first column should have padding, not the second. You
> can fix this with a patch like this:
>
> --- a/ref-filter.c
> +++ b/ref-filter.c
> @@ -1431,6 +1431,14 @@ static void apply_pseudo_state(struct ref_formatting_state *state,
>                 state->ifexists = v->s;
>  }
>
> +static void reset_formatting_state(struct ref_formatting_state *state)
> +{
> +       int quote_style = state->quote_style;
> +       memset(state, 0, sizeof(*state));
> +       state->quote_style = quote_style;
> +}
> +
> +
>  /*
>   * If 'lines' is greater than 0, print that many lines from the given
>   * object_id 'oid'.
> @@ -1492,8 +1500,11 @@ void show_ref_array_item(struct ref_array_item *info, const char *format,
>                 get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
>                 if (atomv->pseudo_atom)
>                         apply_pseudo_state(&state, atomv);
> -               else
> +               else {
>                         print_value(&state, atomv);
> +                       reset_formatting_state(&state);
> +               }
> +
>         }
>         if (*cp) {
>                 sp = cp + strlen(cp);
>

If you saw the last patch series, i had them reset individually, I
missed that by mistake.
But what you're doing seems good, will integrate this. thanks :D

-- 
Regards,
Karthik Nayak

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 03/11] ref-filter: add option to pad atoms to the right
  2015-07-27 12:50     ` Matthieu Moy
@ 2015-07-27 15:45       ` Junio C Hamano
  2015-07-27 15:54         ` Matthieu Moy
  2015-07-27 18:43       ` Karthik Nayak
  1 sibling, 1 reply; 38+ messages in thread
From: Junio C Hamano @ 2015-07-27 15:45 UTC (permalink / raw)
  To: Matthieu Moy; +Cc: Karthik Nayak, git, christian.couder

Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:

> See my remark on previous patch: this test is not sufficient. You do
> not only need to check that %(padright) is taken into account, but you
> also need to check that it is taken into account for the right atom and
> only this one. I'd suggest
>
> --format '%(refname)%(padright:25)|%(refname)|%(refname)|'
>
> Only the middle column should be padded.

This actually raises an interesting point.  It is equally valid to
view that format string as requesting to pad the first "|" with 24
spaces; in other words, %(padright:N) would apply to the next span,
be it a literal string up to the next %(atom), or the %(atom) that
comes immediately after it.

The thing is, the above _might_ be an OK way to ask the middle
refname to be padded, but I somehow find it very unnatural and
unintuitive to expect that this:

	--format '%(padright:25)A Very Long Irrelevancy%(refname)'

would do nothing to "A Very Long Irrelevancy" and affects the
refname that comes much later in the format string.

Or we could simply forbid certain atoms followed by an non-empty
literal as an error.

My preference between the three is "%(padright:4)", etc. to apply to
the "next span", but I can live with "it is an error to pad-right
a far-away %(atom)".

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 03/11] ref-filter: add option to pad atoms to the right
  2015-07-27 15:45       ` Junio C Hamano
@ 2015-07-27 15:54         ` Matthieu Moy
  2015-07-27 18:42           ` Karthik Nayak
  2015-07-27 18:56           ` Junio C Hamano
  0 siblings, 2 replies; 38+ messages in thread
From: Matthieu Moy @ 2015-07-27 15:54 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Karthik Nayak, git, christian.couder

Junio C Hamano <gitster@pobox.com> writes:

> Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:
>
>> See my remark on previous patch: this test is not sufficient. You do
>> not only need to check that %(padright) is taken into account, but you
>> also need to check that it is taken into account for the right atom and
>> only this one. I'd suggest
>>
>> --format '%(refname)%(padright:25)|%(refname)|%(refname)|'
>>
>> Only the middle column should be padded.
>
> This actually raises an interesting point.  It is equally valid to
> view that format string as requesting to pad the first "|" with 24
> spaces; in other words, %(padright:N) would apply to the next span,
> be it a literal string up to the next %(atom), or the %(atom) that
> comes immediately after it.

For an arbitrary %(modifier), I'd agree, but in the particular case of
%(padright), I think it only makes sense if applied to something of
variable length.

> The thing is, the above _might_ be an OK way to ask the middle
> refname to be padded, but I somehow find it very unnatural and
> unintuitive to expect that this:
>
> 	--format '%(padright:25)A Very Long Irrelevancy%(refname)'

Yes, but on the other hand we already have:

  git log --format='%<|(50)A very long irrevlevancy|%an|'

that pads/truncate %an. So, consistancy would dictate that Karthik's
version is the right one.

> My preference between the three is "%(padright:4)", etc. to apply to
> the "next span", but I can live with "it is an error to pad-right
> a far-away %(atom)".

I think there are valid argument for the 3 versions. I'm fine with any
of them, as long as there's a test that shows which one is picked.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 07/11] ref-filter: add option to match literal pattern
  2015-07-27 12:54     ` Matthieu Moy
@ 2015-07-27 15:57       ` Karthik Nayak
  2015-07-27 15:59         ` Karthik Nayak
  0 siblings, 1 reply; 38+ messages in thread
From: Karthik Nayak @ 2015-07-27 15:57 UTC (permalink / raw)
  To: Matthieu Moy; +Cc: Git, Christian Couder, Junio C Hamano

On Mon, Jul 27, 2015 at 6:24 PM, Matthieu Moy
<Matthieu.Moy@grenoble-inp.fr> wrote:
> Karthik Nayak <karthik.188@gmail.com> writes:
>
>> --- a/ref-filter.c
>> +++ b/ref-filter.c
>> @@ -946,6 +946,32 @@ static int commit_contains(struct ref_filter *filter, struct commit *commit)
>
>> +/*
>> + * Return 1 if the refname matches one of the patterns, otherwise 0.
>>   * A pattern can be path prefix (e.g. a refname "refs/heads/master"
>>   * matches a pattern "refs/heads/") or a wildcard (e.g. the same ref
>>   * matches "refs/heads/m*",too).
>
> Nit: you used to s/,too/, too/ in the comment in a previous version.
>

That's carried over from a previous patch, ill change it.

> I think I already suggested saying explicitly "... matches a pattern
> "refs/heads/" but not "refs/heads/m")", but I won't insist on that. Just
> a reminder in case you missed it.
>

Sorry, I missed that out. Thanks for reminding.

-- 
Regards,
Karthik Nayak

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 07/11] ref-filter: add option to match literal pattern
  2015-07-27 15:57       ` Karthik Nayak
@ 2015-07-27 15:59         ` Karthik Nayak
  2015-07-27 16:06           ` Matthieu Moy
  0 siblings, 1 reply; 38+ messages in thread
From: Karthik Nayak @ 2015-07-27 15:59 UTC (permalink / raw)
  To: Matthieu Moy; +Cc: Git, Christian Couder, Junio C Hamano

On Mon, Jul 27, 2015 at 9:27 PM, Karthik Nayak <karthik.188@gmail.com> wrote:
> On Mon, Jul 27, 2015 at 6:24 PM, Matthieu Moy
> <Matthieu.Moy@grenoble-inp.fr> wrote:
>> Karthik Nayak <karthik.188@gmail.com> writes:
>>
>>> --- a/ref-filter.c
>>> +++ b/ref-filter.c
>>> @@ -946,6 +946,32 @@ static int commit_contains(struct ref_filter *filter, struct commit *commit)
>>
>>> +/*
>>> + * Return 1 if the refname matches one of the patterns, otherwise 0.
>>>   * A pattern can be path prefix (e.g. a refname "refs/heads/master"
>>>   * matches a pattern "refs/heads/") or a wildcard (e.g. the same ref
>>>   * matches "refs/heads/m*",too).
>>
>> Nit: you used to s/,too/, too/ in the comment in a previous version.
>>
>
> That's carried over from a previous patch, ill change it.
>
>> I think I already suggested saying explicitly "... matches a pattern
>> "refs/heads/" but not "refs/heads/m")", but I won't insist on that. Just
>> a reminder in case you missed it.
>>
>
> Sorry, I missed that out. Thanks for reminding.
>

If I remember right, I didn't change that cause It didn't pertain to
this commit.
I forgot to mention it in your previous mail.

-- 
Regards,
Karthik Nayak

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 02/11] ref-filter: make `color` use `ref_formatting_state`
  2015-07-27 12:47     ` Matthieu Moy
  2015-07-27 14:28       ` Junio C Hamano
@ 2015-07-27 16:02       ` Karthik Nayak
  1 sibling, 0 replies; 38+ messages in thread
From: Karthik Nayak @ 2015-07-27 16:02 UTC (permalink / raw)
  To: Matthieu Moy; +Cc: Git, Christian Couder, Junio C Hamano

On Mon, Jul 27, 2015 at 6:17 PM, Matthieu Moy
<Matthieu.Moy@grenoble-inp.fr> wrote:
> Karthik Nayak <karthik.188@gmail.com> writes:
>
>> Make the `color` atom a pseudo atom and ensure that it uses
>> `ref_formatting_state`.
>
> Actually, I think this is an incorrect change.
>
> Previously, %(color:...) was taking effect immediately, and after this
> patch, it takes effect starting from the next atom.
>
> Try this:
>
> git for-each-ref --format '%(color:red)red %(color:reset)normal'
>
> Before your patch, I get 'red' as red, and 'normal' as normal. After
> your patch, I get no color at all, since %(color:...) just stores
> information that is never used because I have no real atom.
>
>> --- a/ref-filter.h
>> +++ b/ref-filter.h
>> @@ -19,11 +19,13 @@
>>  struct atom_value {
>>       const char *s;
>>       unsigned long ul; /* used for sorting when not FIELD_STR */
>> -     unsigned int pseudo_atom : 1; /*  atoms which aren't placeholders for ref attributes */
>> +     unsigned int pseudo_atom : 1, /*  atoms which aren't placeholders for ref attributes */
>> +             color : 1;
>>  };
>
> As a consequence of the remark above, I think the name and comment of
> the field are misleading. There are 3 kinds of atoms:
>
> * Placeholders for ref attributes
>
> * Atoms that take action immediately, but that are not ref attributes
>   like %(color)
>
> * Atoms that only act as modifiers for the next atom. Perhaps they could
>   be called "prefix atoms" or "modifier atoms".
>

Thats right, Junio and I missed out the second part and overlapped it
over with the
third part. Thanks for seeing it through.

-- 
Regards,
Karthik Nayak

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 07/11] ref-filter: add option to match literal pattern
  2015-07-27 15:59         ` Karthik Nayak
@ 2015-07-27 16:06           ` Matthieu Moy
  2015-07-27 16:48             ` Karthik Nayak
  0 siblings, 1 reply; 38+ messages in thread
From: Matthieu Moy @ 2015-07-27 16:06 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: Git, Christian Couder, Junio C Hamano

Karthik Nayak <karthik.188@gmail.com> writes:

> On Mon, Jul 27, 2015 at 9:27 PM, Karthik Nayak <karthik.188@gmail.com> wrote:
>> On Mon, Jul 27, 2015 at 6:24 PM, Matthieu Moy
>> <Matthieu.Moy@grenoble-inp.fr> wrote:
>>> Karthik Nayak <karthik.188@gmail.com> writes:
>>>
>>>> --- a/ref-filter.c
>>>> +++ b/ref-filter.c
>>>> @@ -946,6 +946,32 @@ static int commit_contains(struct ref_filter *filter, struct commit *commit)
>>>
>>>> +/*
>>>> + * Return 1 if the refname matches one of the patterns, otherwise 0.
>>>>   * A pattern can be path prefix (e.g. a refname "refs/heads/master"
>>>>   * matches a pattern "refs/heads/") or a wildcard (e.g. the same ref
>>>>   * matches "refs/heads/m*",too).
>>>
>>> Nit: you used to s/,too/, too/ in the comment in a previous version.
>>>
>>
>> That's carried over from a previous patch, ill change it.
>>
>>> I think I already suggested saying explicitly "... matches a pattern
>>> "refs/heads/" but not "refs/heads/m")", but I won't insist on that. Just
>>> a reminder in case you missed it.
>>>
>>
>> Sorry, I missed that out. Thanks for reminding.
>>
>
> If I remember right, I didn't change that cause It didn't pertain to
> this commit. I forgot to mention it in your previous mail.

The "but not "refs/heads/m"" part makes sense in this patch to document
explicitly the difference with the other function. But again, it's just
a suggestion, you chose whether to apply it or not.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 07/11] ref-filter: add option to match literal pattern
  2015-07-27 16:06           ` Matthieu Moy
@ 2015-07-27 16:48             ` Karthik Nayak
  0 siblings, 0 replies; 38+ messages in thread
From: Karthik Nayak @ 2015-07-27 16:48 UTC (permalink / raw)
  To: Matthieu Moy; +Cc: Git, Christian Couder, Junio C Hamano

On Mon, Jul 27, 2015 at 9:36 PM, Matthieu Moy
<Matthieu.Moy@grenoble-inp.fr> wrote:
> Karthik Nayak <karthik.188@gmail.com> writes:
>
>> On Mon, Jul 27, 2015 at 9:27 PM, Karthik Nayak <karthik.188@gmail.com> wrote:
>>> On Mon, Jul 27, 2015 at 6:24 PM, Matthieu Moy
>>> <Matthieu.Moy@grenoble-inp.fr> wrote:
>>>> Karthik Nayak <karthik.188@gmail.com> writes:
>>>>
>>>>> --- a/ref-filter.c
>>>>> +++ b/ref-filter.c
>>>>> @@ -946,6 +946,32 @@ static int commit_contains(struct ref_filter *filter, struct commit *commit)
>>>>
>>>>> +/*
>>>>> + * Return 1 if the refname matches one of the patterns, otherwise 0.
>>>>>   * A pattern can be path prefix (e.g. a refname "refs/heads/master"
>>>>>   * matches a pattern "refs/heads/") or a wildcard (e.g. the same ref
>>>>>   * matches "refs/heads/m*",too).
>>>>
>>>> Nit: you used to s/,too/, too/ in the comment in a previous version.
>>>>
>>>
>>> That's carried over from a previous patch, ill change it.
>>>
>>>> I think I already suggested saying explicitly "... matches a pattern
>>>> "refs/heads/" but not "refs/heads/m")", but I won't insist on that. Just
>>>> a reminder in case you missed it.
>>>>
>>>
>>> Sorry, I missed that out. Thanks for reminding.
>>>
>>
>> If I remember right, I didn't change that cause It didn't pertain to
>> this commit. I forgot to mention it in your previous mail.
>
> The "but not "refs/heads/m"" part makes sense in this patch to document
> explicitly the difference with the other function. But again, it's just
> a suggestion, you chose whether to apply it or not.
>

I think it's a valid add-on, will include it :)

-- 
Regards,
Karthik Nayak

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 02/11] ref-filter: make `color` use `ref_formatting_state`
  2015-07-27 13:06     ` Matthieu Moy
@ 2015-07-27 17:16       ` Karthik Nayak
  0 siblings, 0 replies; 38+ messages in thread
From: Karthik Nayak @ 2015-07-27 17:16 UTC (permalink / raw)
  To: Matthieu Moy; +Cc: Git, Christian Couder, Junio C Hamano

On Mon, Jul 27, 2015 at 6:36 PM, Matthieu Moy
<Matthieu.Moy@grenoble-inp.fr> wrote:
> Karthik Nayak <karthik.188@gmail.com> writes:
>
>> --- a/ref-filter.c
>> +++ b/ref-filter.c
>> @@ -1195,6 +1197,11 @@ void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
>>  static void ref_formatting(struct ref_formatting_state *state,
>>                          struct atom_value *v, struct strbuf *value)
>>  {
>> +     if (state->color) {
>> +             strbuf_addstr(value, state->color);
>> +             free(state->color);
>> +             state->color = NULL;
>> +     }
>>       strbuf_addf(value, "%s", v->s);
>>  }
>>
>> @@ -1266,6 +1273,13 @@ static void emit(const char *cp, const char *ep)
>>       }
>>  }
>>
>> +static void apply_pseudo_state(struct ref_formatting_state *state,
>> +                            struct atom_value *v)
>> +{
>> +     if (v->color)
>> +             state->color = (char *)v->s;
>> +}
>> +
>>  void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
>>  {
>>       const char *cp, *sp, *ep;
>
> It's not clear enough in the code and history that these these two
> functions are symmetrical.
>
> You can find better names. 'apply_pseudo_state' seems wrong it two ways:
> it does not _apply_ the state, but it stores it. And it's a "pseudo-atom
> related state", but not a "pseudo-state".
>
> How about
>
> ref_formatting -> apply_formatting_state
> apply_pseudo_state -> store_formatting_state
>
> ?

Yes, your suggested naming scheme is better. Ill adopt this.

>
> Actually, I would even call these functions right from
> show_ref_array_item, so that the result look like this:
>
>                 if (atomv->pseudo_atom)
>                         store_formatting_state(&state, atomv);
>                 else {
>                         apply_formatting_state(&state, atomv);
>                         reset_formatting_state(&state);
>                         print_value(&state, atomv);
>                 }

This would eliminate that extra strbuf in print_value() wouldn't it,
but this would also mean
that we replace the value in atomv to hold the new formatted value.
Sounds good to me.
Thanks :)

>
> In the history, if you are to introduce a dumb version of ref_formatting
> in PATCH 1, I think you should also introduce a dumb (actually, totally
> empty) version of apply_pseudo_state. Then, further patches would just
> add a few lines in each function, and ...
>
>> @@ -1281,7 +1295,10 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu
>>               if (cp < sp)
>>                       emit(cp, sp);
>>               get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
>> -             print_value(&state, atomv);
>> +             if (atomv->pseudo_atom)
>> +                     apply_pseudo_state(&state, atomv);
>> +             else
>> +                     print_value(&state, atomv);
>>       }
>
> ... this hunk would belong to PATCH 1.
>

I'll add a placeholder for this in the PATCH 1. Thanks :D

-- 
Regards,
Karthik Nayak

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 02/11] ref-filter: make `color` use `ref_formatting_state`
  2015-07-27 14:28       ` Junio C Hamano
@ 2015-07-27 17:23         ` Karthik Nayak
  0 siblings, 0 replies; 38+ messages in thread
From: Karthik Nayak @ 2015-07-27 17:23 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Matthieu Moy, Git, Christian Couder

On Mon, Jul 27, 2015 at 7:58 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:
>
>> Karthik Nayak <karthik.188@gmail.com> writes:
>>
>>> Make the `color` atom a pseudo atom and ensure that it uses
>>> `ref_formatting_state`.
>>
>> Actually, I think this is an incorrect change.
>>
>> Previously, %(color:...) was taking effect immediately, and after this
>> patch, it takes effect starting from the next atom.
>> ...
>> As a consequence of the remark above, I think the name and comment of
>> the field are misleading. There are 3 kinds of atoms:
>>
>> * Placeholders for ref attributes
>>
>> * Atoms that take action immediately, but that are not ref attributes
>>   like %(color)
>>
>> * Atoms that only act as modifiers for the next atom. Perhaps they could
>>   be called "prefix atoms" or "modifier atoms".
>
> My fault.
>
> I briefly thought that it may be simpler to treat %(color) just as a
> different way to express a literal that cannot be typed from the
> keyboard, but your "different kind of atom" is a much better way to
> think about it.

Yes even I agree with this.

>
> What is necessary is that, just like the updated "print_value()"
> knows about the formatting state, "emit()" needs to be told about
> the same formatting state.  Some of these "state affecting" atoms
> may take effect on what is output by "emit()" (e.g. "color" is
> obviously an example, the hypotethical one that counts the current
> output column and uses that knowledge to "align" the output to
> certain columns, instead of "right pad to make the next column
> 30-columns wide" one, which is in this series, is another).

This depends on whether these modifier atoms should effect only succeeding
atoms or any string. Since there is a conversation about this topic on the other
patch, let's continue this discussion there.

>
> Thanks for finding this, and Karthik, sorry for an incomplete
> suggestion based on a faulty thinking.

I don't see why you have to be sorry, you were trying to help :)

-- 
Regards,
Karthik Nayak

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 03/11] ref-filter: add option to pad atoms to the right
  2015-07-27 15:54         ` Matthieu Moy
@ 2015-07-27 18:42           ` Karthik Nayak
  2015-07-27 18:47             ` Matthieu Moy
  2015-07-27 18:56           ` Junio C Hamano
  1 sibling, 1 reply; 38+ messages in thread
From: Karthik Nayak @ 2015-07-27 18:42 UTC (permalink / raw)
  To: Matthieu Moy; +Cc: Junio C Hamano, Git, Christian Couder

On Mon, Jul 27, 2015 at 9:24 PM, Matthieu Moy
<Matthieu.Moy@grenoble-inp.fr> wrote:
> Junio C Hamano <gitster@pobox.com> writes:
>
>> Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:
>>
>>> See my remark on previous patch: this test is not sufficient. You do
>>> not only need to check that %(padright) is taken into account, but you
>>> also need to check that it is taken into account for the right atom and
>>> only this one. I'd suggest
>>>
>>> --format '%(refname)%(padright:25)|%(refname)|%(refname)|'
>>>
>>> Only the middle column should be padded.
>>
>> This actually raises an interesting point.  It is equally valid to
>> view that format string as requesting to pad the first "|" with 24
>> spaces; in other words, %(padright:N) would apply to the next span,
>> be it a literal string up to the next %(atom), or the %(atom) that
>> comes immediately after it.
>
> For an arbitrary %(modifier), I'd agree, but in the particular case of
> %(padright), I think it only makes sense if applied to something of
> variable length
>

I agree with Matthieu here, Other than atom values, any user defined string
would be of known size, hence padding for such things would make no sense.

>> The thing is, the above _might_ be an OK way to ask the middle
>> refname to be padded, but I somehow find it very unnatural and
>> unintuitive to expect that this:
>>
>>       --format '%(padright:25)A Very Long Irrelevancy%(refname)'
>
> Yes, but on the other hand we already have:
>
>   git log --format='%<|(50)A very long irrevlevancy|%an|'
>
> that pads/truncate %an. So, consistancy would dictate that Karthik's
> version is the right one.

Sorry but I didn't understand what you're trying to say here, Matthieu.

>
>> My preference between the three is "%(padright:4)", etc. to apply to
>> the "next span", but I can live with "it is an error to pad-right
>> a far-away %(atom)".
>
> I think there are valid argument for the 3 versions. I'm fine with any
> of them, as long as there's a test that shows which one is picked.
>

Makes sense, thanks both of you :)

-- 
Regards,
Karthik Nayak

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 03/11] ref-filter: add option to pad atoms to the right
  2015-07-27 12:50     ` Matthieu Moy
  2015-07-27 15:45       ` Junio C Hamano
@ 2015-07-27 18:43       ` Karthik Nayak
  1 sibling, 0 replies; 38+ messages in thread
From: Karthik Nayak @ 2015-07-27 18:43 UTC (permalink / raw)
  To: Matthieu Moy; +Cc: Git, Christian Couder, Junio C Hamano

On Mon, Jul 27, 2015 at 6:20 PM, Matthieu Moy
<Matthieu.Moy@grenoble-inp.fr> wrote:
> u
> also need to check that it is taken into account for the right atom and
> only this one. I'd suggest
>
> --format '%(refname)%(padright:25)|%(refname)|%(refname)|'

I guess this is more accurate, Thanks.

-- 
Regards,
Karthik Nayak

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 03/11] ref-filter: add option to pad atoms to the right
  2015-07-27 18:42           ` Karthik Nayak
@ 2015-07-27 18:47             ` Matthieu Moy
  2015-07-27 18:54               ` Karthik Nayak
  0 siblings, 1 reply; 38+ messages in thread
From: Matthieu Moy @ 2015-07-27 18:47 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: Junio C Hamano, Git, Christian Couder

Karthik Nayak <karthik.188@gmail.com> writes:

> On Mon, Jul 27, 2015 at 9:24 PM, Matthieu Moy
> <Matthieu.Moy@grenoble-inp.fr> wrote:
>> Yes, but on the other hand we already have:
>>
>>   git log --format='%<|(50)A very long irrevlevancy|%an|'
>>
>> that pads/truncate %an. So, consistancy would dictate that Karthik's
>> version is the right one.
>
> Sorry but I didn't understand what you're trying to say here, Matthieu.

The "git log" equivalent of %(padright:N) is %<|(N), and it behaves the
same way as your current implementation of %(padright) (except for the
missing reset in your v5).

So, if we want to be consistant with "git log", we should keep the
"apply to next atom, even if it's far away in the format string"
semantics.

Note that consistancy is not the only criterion for choice, so I'm not
saying we should absolutely do this, just that there's an argument in
favor of it.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 03/11] ref-filter: add option to pad atoms to the right
  2015-07-27 18:47             ` Matthieu Moy
@ 2015-07-27 18:54               ` Karthik Nayak
  0 siblings, 0 replies; 38+ messages in thread
From: Karthik Nayak @ 2015-07-27 18:54 UTC (permalink / raw)
  To: Matthieu Moy; +Cc: Junio C Hamano, Git, Christian Couder

On Tue, Jul 28, 2015 at 12:17 AM, Matthieu Moy
<Matthieu.Moy@grenoble-inp.fr> wrote:
> Karthik Nayak <karthik.188@gmail.com> writes:
>
>> On Mon, Jul 27, 2015 at 9:24 PM, Matthieu Moy
>> <Matthieu.Moy@grenoble-inp.fr> wrote:
>>> Yes, but on the other hand we already have:
>>>
>>>   git log --format='%<|(50)A very long irrevlevancy|%an|'
>>>
>>> that pads/truncate %an. So, consistancy would dictate that Karthik's
>>> version is the right one.
>>
>> Sorry but I didn't understand what you're trying to say here, Matthieu.
>
> The "git log" equivalent of %(padright:N) is %<|(N), and it behaves the
> same way as your current implementation of %(padright) (except for the
> missing reset in your v5).
>
> So, if we want to be consistant with "git log", we should keep the
> "apply to next atom, even if it's far away in the format string"
> semantics.
>
> Note that consistancy is not the only criterion for choice, so I'm not
> saying we should absolutely do this, just that there's an argument in
> favor of it.
>

I didn't know that, thanks, I think I'll let Junio make the call here :)

-- 
Regards,
Karthik Nayak

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 03/11] ref-filter: add option to pad atoms to the right
  2015-07-27 15:54         ` Matthieu Moy
  2015-07-27 18:42           ` Karthik Nayak
@ 2015-07-27 18:56           ` Junio C Hamano
  1 sibling, 0 replies; 38+ messages in thread
From: Junio C Hamano @ 2015-07-27 18:56 UTC (permalink / raw)
  To: Matthieu Moy; +Cc: Karthik Nayak, git, christian.couder

Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:

> Yes, but on the other hand we already have:
>
>   git log --format='%<|(50)A very long irrevlevancy|%an|'
>
> that pads/truncate %an. So, consistancy would dictate that Karthik's
> version is the right one.

Interesting.  Although that %<(50) looks simply a bug to me which we
may want to fix.

I wonder if there is a good reason why the above should not be
spelled as --format="A very long irrelevancy%<(50)%an".

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 03/11] ref-filter: add option to pad atoms to the right
  2015-07-27  7:27   ` [PATCH v5 03/11] ref-filter: add option to pad atoms to the right Karthik Nayak
  2015-07-27 12:50     ` Matthieu Moy
@ 2015-07-28 21:41     ` Eric Sunshine
  2015-07-29 16:16       ` Karthik Nayak
  1 sibling, 1 reply; 38+ messages in thread
From: Eric Sunshine @ 2015-07-28 21:41 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: Git List, Christian Couder, Matthieu Moy, Junio C Hamano

On Mon, Jul 27, 2015 at 3:27 AM, Karthik Nayak <karthik.188@gmail.com> wrote:
> Add a new atom "padright" and support %(padright:X) where X is a
> number.  This will align the succeeding atom value to the left
> followed by spaces for a total length of X characters. If X is less
> than the item size, the entire atom value is printed.
>
> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
> ---
> diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt
> index e49d578..45dd7f8 100644
> --- a/Documentation/git-for-each-ref.txt
> +++ b/Documentation/git-for-each-ref.txt
> @@ -127,6 +127,12 @@ color::
>         Change output color.  Followed by `:<colorname>`, where names
>         are described in `color.branch.*`.
>
> +padright::
> +       Pad succeeding atom to the right. Followed by `:<value>`,
> +       where `value` states the total length of atom including the
> +       padding. If the `value` is greater than the atom length, then
> +       no padding is performed.

Isn't this backward? Don't you mean

    ... If the atom length is greater than `value`, then
    no padding is performed.

?

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 07/11] ref-filter: add option to match literal pattern
  2015-07-27  7:27   ` [PATCH v5 07/11] ref-filter: add option to match literal pattern Karthik Nayak
  2015-07-27 12:54     ` Matthieu Moy
@ 2015-07-28 21:49     ` Eric Sunshine
  2015-07-29 16:17       ` Karthik Nayak
  2015-07-30 11:21       ` Karthik Nayak
  1 sibling, 2 replies; 38+ messages in thread
From: Eric Sunshine @ 2015-07-28 21:49 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: Git List, Christian Couder, Matthieu Moy, Junio C Hamano

On Mon, Jul 27, 2015 at 3:27 AM, Karthik Nayak <karthik.188@gmail.com> wrote:
> From: Karthik Nayak <karthik.188@gmail.com>
>
> Since 'ref-filter' only has an option to match path names add an
> option for plain fnmatch pattern-matching.
>
> This is to support the pattern matching options which are used in `git
> tag -l` and `git branch -l` where we can match patterns like `git tag
> -l foo*` which would match all tags which has a "foo*" pattern.
>
> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
> ---
> diff --git a/ref-filter.c b/ref-filter.c
> index 26eb26c..597b189 100644
> --- a/ref-filter.c
> +++ b/ref-filter.c
> @@ -946,6 +946,32 @@ static int commit_contains(struct ref_filter *filter, struct commit *commit)
>
>  /*
>   * Return 1 if the refname matches one of the patterns, otherwise 0.
> + * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
> + * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
> + * matches "refs/heads/mas*", too).
> + */
> +static int match_pattern(const char **patterns, const char *refname)
> +{
> +       /*
> +        * When no '--format' option is given we need to skip the prefix
> +        * for matching refs of tags and branches.
> +        */
> +       if (skip_prefix(refname, "refs/tags/", &refname))
> +               ;
> +       else if (skip_prefix(refname, "refs/heads/", &refname))
> +               ;
> +       else if (skip_prefix(refname, "refs/remotes/", &refname))
> +               ;

Or, more concisely:

    skip_prefix(refname, "refs/tags/", &refname) ||
    skip_prefix(refname, "refs/heads/", &refname) ||
    skip_prefix(refname, "refs/remotes/", &refname);

since || short-circuits. No need for the 'if' or cascading 'else if's.

> +       for (; *patterns; patterns++) {
> +               if (!wildmatch(*patterns, refname, 0, NULL))
> +                       return 1;
> +       }
> +       return 0;
> +}

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 03/11] ref-filter: add option to pad atoms to the right
  2015-07-28 21:41     ` Eric Sunshine
@ 2015-07-29 16:16       ` Karthik Nayak
  0 siblings, 0 replies; 38+ messages in thread
From: Karthik Nayak @ 2015-07-29 16:16 UTC (permalink / raw)
  To: Eric Sunshine; +Cc: Git List, Christian Couder, Matthieu Moy, Junio C Hamano

On Wed, Jul 29, 2015 at 3:11 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Mon, Jul 27, 2015 at 3:27 AM, Karthik Nayak <karthik.188@gmail.com> wrote:
>> Add a new atom "padright" and support %(padright:X) where X is a
>> number.  This will align the succeeding atom value to the left
>> followed by spaces for a total length of X characters. If X is less
>> than the item size, the entire atom value is printed.
>>
>> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
>> ---
>> diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt
>> index e49d578..45dd7f8 100644
>> --- a/Documentation/git-for-each-ref.txt
>> +++ b/Documentation/git-for-each-ref.txt
>> @@ -127,6 +127,12 @@ color::
>>         Change output color.  Followed by `:<colorname>`, where names
>>         are described in `color.branch.*`.
>>
>> +padright::
>> +       Pad succeeding atom to the right. Followed by `:<value>`,
>> +       where `value` states the total length of atom including the
>> +       padding. If the `value` is greater than the atom length, then
>> +       no padding is performed.
>
> Isn't this backward? Don't you mean
>
>     ... If the atom length is greater than `value`, then
>     no padding is performed.
>
> ?

Yes, silly mistake.

-- 
Regards,
Karthik Nayak

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 07/11] ref-filter: add option to match literal pattern
  2015-07-28 21:49     ` Eric Sunshine
@ 2015-07-29 16:17       ` Karthik Nayak
  2015-07-30 11:21       ` Karthik Nayak
  1 sibling, 0 replies; 38+ messages in thread
From: Karthik Nayak @ 2015-07-29 16:17 UTC (permalink / raw)
  To: Eric Sunshine; +Cc: Git List, Christian Couder, Matthieu Moy, Junio C Hamano

On Wed, Jul 29, 2015 at 3:19 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Mon, Jul 27, 2015 at 3:27 AM, Karthik Nayak <karthik.188@gmail.com> wrote:
>> From: Karthik Nayak <karthik.188@gmail.com>
>>
>> Since 'ref-filter' only has an option to match path names add an
>> option for plain fnmatch pattern-matching.
>>
>> This is to support the pattern matching options which are used in `git
>> tag -l` and `git branch -l` where we can match patterns like `git tag
>> -l foo*` which would match all tags which has a "foo*" pattern.
>>
>> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
>> ---
>> diff --git a/ref-filter.c b/ref-filter.c
>> index 26eb26c..597b189 100644
>> --- a/ref-filter.c
>> +++ b/ref-filter.c
>> @@ -946,6 +946,32 @@ static int commit_contains(struct ref_filter *filter, struct commit *commit)
>>
>>  /*
>>   * Return 1 if the refname matches one of the patterns, otherwise 0.
>> + * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
>> + * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
>> + * matches "refs/heads/mas*", too).
>> + */
>> +static int match_pattern(const char **patterns, const char *refname)
>> +{
>> +       /*
>> +        * When no '--format' option is given we need to skip the prefix
>> +        * for matching refs of tags and branches.
>> +        */
>> +       if (skip_prefix(refname, "refs/tags/", &refname))
>> +               ;
>> +       else if (skip_prefix(refname, "refs/heads/", &refname))
>> +               ;
>> +       else if (skip_prefix(refname, "refs/remotes/", &refname))
>> +               ;
>
> Or, more concisely:
>
>     skip_prefix(refname, "refs/tags/", &refname) ||
>     skip_prefix(refname, "refs/heads/", &refname) ||
>     skip_prefix(refname, "refs/remotes/", &refname);
>
> since || short-circuits. No need for the 'if' or cascading 'else if's.
>
>> +       for (; *patterns; patterns++) {
>> +               if (!wildmatch(*patterns, refname, 0, NULL))
>> +                       return 1;
>> +       }
>> +       return 0;
>> +}

Looks better thanks :)

-- 
Regards,
Karthik Nayak

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [PATCH v5 07/11] ref-filter: add option to match literal pattern
  2015-07-28 21:49     ` Eric Sunshine
  2015-07-29 16:17       ` Karthik Nayak
@ 2015-07-30 11:21       ` Karthik Nayak
  1 sibling, 0 replies; 38+ messages in thread
From: Karthik Nayak @ 2015-07-30 11:21 UTC (permalink / raw)
  To: Eric Sunshine; +Cc: Git List, Christian Couder, Matthieu Moy, Junio C Hamano

On Wed, Jul 29, 2015 at 3:19 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Mon, Jul 27, 2015 at 3:27 AM, Karthik Nayak <karthik.188@gmail.com> wrote:
>> From: Karthik Nayak <karthik.188@gmail.com>
>>
>> Since 'ref-filter' only has an option to match path names add an
>> option for plain fnmatch pattern-matching.
>>
>> This is to support the pattern matching options which are used in `git
>> tag -l` and `git branch -l` where we can match patterns like `git tag
>> -l foo*` which would match all tags which has a "foo*" pattern.
>>
>> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
>> ---
>> diff --git a/ref-filter.c b/ref-filter.c
>> index 26eb26c..597b189 100644
>> --- a/ref-filter.c
>> +++ b/ref-filter.c
>> @@ -946,6 +946,32 @@ static int commit_contains(struct ref_filter *filter, struct commit *commit)
>>
>>  /*
>>   * Return 1 if the refname matches one of the patterns, otherwise 0.
>> + * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
>> + * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
>> + * matches "refs/heads/mas*", too).
>> + */
>> +static int match_pattern(const char **patterns, const char *refname)
>> +{
>> +       /*
>> +        * When no '--format' option is given we need to skip the prefix
>> +        * for matching refs of tags and branches.
>> +        */
>> +       if (skip_prefix(refname, "refs/tags/", &refname))
>> +               ;
>> +       else if (skip_prefix(refname, "refs/heads/", &refname))
>> +               ;
>> +       else if (skip_prefix(refname, "refs/remotes/", &refname))
>> +               ;
>
> Or, more concisely:
>
>     skip_prefix(refname, "refs/tags/", &refname) ||
>     skip_prefix(refname, "refs/heads/", &refname) ||
>     skip_prefix(refname, "refs/remotes/", &refname);
>

Gives a "warning: value computed is not used [-Wunused-value]"

so I typecasted the output as:

      (void)(skip_prefix(refname, "refs/tags/", &refname) ||
                skip_prefix(refname, "refs/heads/", &refname) ||
                skip_prefix(refname, "refs/remotes/", &refname));

Just wondering if that's alright.

-- 
Regards,
Karthik Nayak

^ permalink raw reply	[flat|nested] 38+ messages in thread

end of thread, other threads:[~2015-07-30 11:21 UTC | newest]

Thread overview: 38+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-27  7:26 [PATCH v5 00/11] port tag.c to use ref-filter APIs Karthik Nayak
2015-07-27  7:27 ` [PATCH v5 01/11] ref-filter: introduce 'ref_formatting_state' Karthik Nayak
2015-07-27  7:27   ` [PATCH v5 02/11] ref-filter: make `color` use `ref_formatting_state` Karthik Nayak
2015-07-27 12:47     ` Matthieu Moy
2015-07-27 14:28       ` Junio C Hamano
2015-07-27 17:23         ` Karthik Nayak
2015-07-27 16:02       ` Karthik Nayak
2015-07-27 13:06     ` Matthieu Moy
2015-07-27 17:16       ` Karthik Nayak
2015-07-27  7:27   ` [PATCH v5 03/11] ref-filter: add option to pad atoms to the right Karthik Nayak
2015-07-27 12:50     ` Matthieu Moy
2015-07-27 15:45       ` Junio C Hamano
2015-07-27 15:54         ` Matthieu Moy
2015-07-27 18:42           ` Karthik Nayak
2015-07-27 18:47             ` Matthieu Moy
2015-07-27 18:54               ` Karthik Nayak
2015-07-27 18:56           ` Junio C Hamano
2015-07-27 18:43       ` Karthik Nayak
2015-07-28 21:41     ` Eric Sunshine
2015-07-29 16:16       ` Karthik Nayak
2015-07-27  7:27   ` [PATCH v5 04/11] ref-filter: add option to filter only tags Karthik Nayak
2015-07-27  7:27   ` [PATCH v5 05/11] ref-filter: support printing N lines from tag annotation Karthik Nayak
2015-07-27  7:27   ` [PATCH v5 06/11] ref-filter: add support to sort by version Karthik Nayak
2015-07-27  7:27   ` [PATCH v5 07/11] ref-filter: add option to match literal pattern Karthik Nayak
2015-07-27 12:54     ` Matthieu Moy
2015-07-27 15:57       ` Karthik Nayak
2015-07-27 15:59         ` Karthik Nayak
2015-07-27 16:06           ` Matthieu Moy
2015-07-27 16:48             ` Karthik Nayak
2015-07-28 21:49     ` Eric Sunshine
2015-07-29 16:17       ` Karthik Nayak
2015-07-30 11:21       ` Karthik Nayak
2015-07-27  7:27   ` [PATCH v5 08/11] tag.c: use 'ref-filter' data structures Karthik Nayak
2015-07-27  7:27   ` [PATCH v5 09/11] tag.c: use 'ref-filter' APIs Karthik Nayak
2015-07-27  7:27   ` [PATCH v5 10/11] tag.c: implement '--format' option Karthik Nayak
2015-07-27  7:27   ` [PATCH v5 11/11] tag.c: implement '--merged' and '--no-merged' options Karthik Nayak
2015-07-27 12:42   ` [PATCH v5 01/11] ref-filter: introduce 'ref_formatting_state' Matthieu Moy
2015-07-27 15:28     ` Karthik Nayak

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).