All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v6 0/10] port tag.c to use ref-filter APIs
@ 2015-07-28  6:32 Karthik Nayak
  2015-07-28  6:33 ` [PATCH v6 01/10] ref-filter: introduce 'ref_formatting_state' Karthik Nayak
                   ` (2 more replies)
  0 siblings, 3 replies; 43+ messages in thread
From: Karthik Nayak @ 2015-07-28  6:32 UTC (permalink / raw)
  To: Git; +Cc: Matthieu Moy, Christian Couder, 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 5 can be found here:
http://article.gmane.org/gmane.comp.version-control.git/274650

Changes in v6:
* Change the flow of commits, introduce rest_formatting_state.
* Rename
ref_formatting -> apply_formatting_state
apply_pseudo_state -> store_formatting_state
* Remove the patch for making color a pseudo atom, and rename
pseudo_atom to modifier_atom.
* Small grammatical changes.

Side Note: --format="%(padright:X)" applies to the next available atom
and not to the next span. I find this more accurate as I don't see why
we'd want to pad something of known length. But its up for discussion
:D

Interdiff:
diff --git a/ref-filter.c b/ref-filter.c
index 597b189..0a34924 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -664,8 +664,6 @@ 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;
@@ -702,7 +700,7 @@ static void populate_value(struct ref_array_item *ref)
             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->modifier_atom = 1;
             v->pad_to_right = 1;
             continue;
         } else
@@ -973,8 +971,8 @@ 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 a pattern "refs/heads/" but not "refs/heads/m") or a
+ * wildcard (e.g. the same ref matches "refs/heads/m*", too).
  */
 static int match_name_as_path(const char **pattern, const char *refname)
 {
@@ -1246,14 +1244,9 @@ 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 apply_formatting_state(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;
-    }
     if (state->pad_to_right) {
         if (!is_utf8(v->s))
             strbuf_addf(value, "%-*s", state->pad_to_right, v->s);
@@ -1263,15 +1256,22 @@ static void ref_formatting(struct
ref_formatting_state *state,
         }
         return;
     }
-    strbuf_addf(value, "%s", v->s);
+
+    strbuf_addstr(value, v->s);
 }

-static void print_value(struct ref_formatting_state *state, struct
atom_value *v)
+static void print_value(struct atom_value *v, struct
ref_formatting_state *state)
 {
     struct strbuf value = STRBUF_INIT;
     struct strbuf formatted = STRBUF_INIT;

-    ref_formatting(state, v, &value);
+    /*
+     * Some (pesudo) atoms 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.
+     */
+    apply_formatting_state(state, v, &value);

     switch (state->quote_style) {
     case QUOTE_NONE:
@@ -1292,8 +1292,8 @@ static void print_value(struct
ref_formatting_state *state, struct atom_value *v
     }
     if (state->quote_style != QUOTE_NONE)
         fputs(formatted.buf, stdout);
-    strbuf_release(&formatted);
     strbuf_release(&value);
+    strbuf_release(&formatted);
 }

 static int hex1(char ch)
@@ -1334,13 +1334,18 @@ static void emit(const char *cp, const char *ep)
     }
 }

-static void apply_pseudo_state(struct ref_formatting_state *state,
-                   struct atom_value *v)
+static void store_formatting_state(struct ref_formatting_state *state,
+                   struct atom_value *atomv)
 {
-    if (v->color)
-        state->color = (char *)v->s;
-    if (v->pad_to_right)
-        state->pad_to_right = v->ul;
+    if (atomv->pad_to_right)
+        state->pad_to_right = atomv->ul;
+}
+
+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;
 }

 /*
@@ -1402,10 +1407,12 @@ void show_ref_array_item(struct ref_array_item
*info, const char *format,
         if (cp < sp)
             emit(cp, sp);
         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 (atomv->modifier_atom)
+            store_formatting_state(&state, atomv);
+        else {
+            print_value(atomv, &state);
+            reset_formatting_state(&state);
+        }
     }
     if (*cp) {
         sp = cp + strlen(cp);
@@ -1418,7 +1425,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 = color;
-        print_value(&state, &resetv);
+        print_value(&resetv, &state);
     }
     if (lines > 0) {
         struct object_id oid;
diff --git a/ref-filter.h b/ref-filter.h
index a27745f..fcf469e 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -20,14 +20,12 @@
 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,
+    unsigned int modifier_atom : 1, /*  atoms which act as modifiers
for the next atom */
         pad_to_right : 1;
 };

 struct ref_formatting_state {
     int quote_style;
-    char *color;
     unsigned int pad_to_right;
 };

diff --git a/t/t6302-for-each-ref-filter.sh b/t/t6302-for-each-ref-filter.sh
index de872db..68688a9 100755
--- a/t/t6302-for-each-ref-filter.sh
+++ b/t/t6302-for-each-ref-filter.sh
@@ -83,17 +83,17 @@ test_expect_success 'filtering with --contains' '

 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            |
+    refs/heads/master|refs/heads/master        |refs/heads/master|
+    refs/heads/side|refs/heads/side          |refs/heads/side|
+    refs/odd/spot|refs/odd/spot            |refs/odd/spot|
+    refs/tags/double-tag|refs/tags/double-tag     |refs/tags/double-tag|
+    refs/tags/four|refs/tags/four           |refs/tags/four|
+    refs/tags/one|refs/tags/one            |refs/tags/one|
+    refs/tags/signed-tag|refs/tags/signed-tag     |refs/tags/signed-tag|
+    refs/tags/three|refs/tags/three          |refs/tags/three|
+    refs/tags/two|refs/tags/two            |refs/tags/two|
     EOF
-    git for-each-ref --format="%(padright:25)%(refname)|" >actual &&
+    git for-each-ref
--format="%(refname)%(padright:25)|%(refname)|%(refname)|" >actual &&
     test_cmp expect actual
 '

-- 
Regards,
Karthik Nayak

^ permalink raw reply related	[flat|nested] 43+ messages in thread
* [PATCH v7 0/11] port tag.c to use ref-filter APIs
@ 2015-07-30 15:34 Karthik Nayak
  2015-07-30 15:48 ` [PATCH v7 01/11] ref-filter: introduce 'ref_formatting_state' Karthik Nayak
  0 siblings, 1 reply; 43+ messages in thread
From: Karthik Nayak @ 2015-07-30 15:34 UTC (permalink / raw)
  To: Git; +Cc: Matthieu Moy, Christian Couder, 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 6 can be found here:
http://article.gmane.org/gmane.comp.version-control.git/274726

Changes in this version:
* ref_formatting_state now works on emit() also, hence succeeding
strings and atoms are affected.
* Make color a modifier_atom like in v5.
* Add test for "padright" when its value is lesser than the value of the atom.
* Other small changes.

Interdiff:

diff --git a/Documentation/git-for-each-ref.txt
b/Documentation/git-for-each-ref.txt
index 2b60aee..bcf319a 100644
--- a/Documentation/git-for-each-ref.txt
+++ b/Documentation/git-for-each-ref.txt
@@ -128,10 +128,10 @@ color::
     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.
+    Pad succeeding atom or string to the right. Followed by
+    `:<value>`, where `value` states the total length of atom or
+    string including the padding. If the `value` is lesser than
+    the atom or string 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
@@ -152,7 +152,7 @@ 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`.
+the fieldname `version:refname` or its alias `v:refname`.

 In any case, a field name that refers to a field inapplicable to
 the object referred by the ref does not cause an error.  It
diff --git a/ref-filter.c b/ref-filter.c
index 0a34924..65d168e 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -624,7 +624,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 = NULL;
+        const char *refname;
         const char *formatp;
         struct branch *branch = NULL;

@@ -664,6 +664,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->color = 1;
+            v->modifier_atom = 1;
             continue;
         } else if (!strcmp(name, "flag")) {
             char buf[256], *cp = buf;
@@ -954,12 +956,9 @@ 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))
-        ;
+    (void)(skip_prefix(refname, "refs/tags/", &refname) ||
+           skip_prefix(refname, "refs/heads/", &refname) ||
+           skip_prefix(refname, "refs/remotes/", &refname));

     for (; *patterns; patterns++) {
         if (!wildmatch(*patterns, refname, 0, NULL))
@@ -1245,19 +1244,24 @@ void ref_array_sort(struct ref_sorting
*sorting, struct ref_array *array)
 }

 static void apply_formatting_state(struct ref_formatting_state *state,
-                   struct atom_value *v, struct strbuf *value)
+                   const char *buf, struct strbuf *value)
 {
+    if (state->color) {
+        strbuf_addstr(value, 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);
+        if (!is_utf8(buf))
+            strbuf_addf(value, "%-*s", state->pad_to_right, buf);
         else {
-            int utf8_compensation = strlen(v->s) - utf8_strwidth(v->s);
-            strbuf_addf(value, "%-*s", state->pad_to_right +
utf8_compensation, v->s);
+            int utf8_compensation = strlen(buf) - utf8_strwidth(buf);
+            strbuf_addf(value, "%-*s", state->pad_to_right +
utf8_compensation, buf);
         }
+        state->pad_to_right = 0;
         return;
     }

-    strbuf_addstr(value, v->s);
+    strbuf_addstr(value, buf);
 }

 static void print_value(struct atom_value *v, struct
ref_formatting_state *state)
@@ -1265,13 +1269,7 @@ static void print_value(struct atom_value *v,
struct ref_formatting_state *state
     struct strbuf value = STRBUF_INIT;
     struct strbuf formatted = STRBUF_INIT;

-    /*
-     * Some (pesudo) atoms 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.
-     */
-    apply_formatting_state(state, v, &value);
+    apply_formatting_state(state, v->s, &value);

     switch (state->quote_style) {
     case QUOTE_NONE:
@@ -1314,8 +1312,12 @@ static int hex2(const char *cp)
         return -1;
 }

-static void emit(const char *cp, const char *ep)
+static void emit(const char *cp, const char *ep,
+         struct ref_formatting_state *state)
 {
+    struct strbuf value = STRBUF_INIT;
+    struct strbuf format = STRBUF_INIT;
+
     while (*cp && (!ep || cp < ep)) {
         if (*cp == '%') {
             if (cp[1] == '%')
@@ -1329,25 +1331,24 @@ static void emit(const char *cp, const char *ep)
                 }
             }
         }
-        putchar(*cp);
+        strbuf_addch(&value, *cp);
         cp++;
     }
+    apply_formatting_state(state, value.buf, &format);
+    fputs(format.buf, stdout);
+    strbuf_release(&format);
+    strbuf_release(&value);
 }

 static void store_formatting_state(struct ref_formatting_state *state,
                    struct atom_value *atomv)
 {
+    if (atomv->color)
+        state->color = atomv->s;
     if (atomv->pad_to_right)
         state->pad_to_right = atomv->ul;
 }

-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'.
@@ -1405,18 +1406,16 @@ void show_ref_array_item(struct ref_array_item
*info, const char *format,

         ep = strchr(sp, ')');
         if (cp < sp)
-            emit(cp, sp);
+            emit(cp, sp, &state);
         get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
         if (atomv->modifier_atom)
             store_formatting_state(&state, atomv);
-        else {
+        else
             print_value(atomv, &state);
-            reset_formatting_state(&state);
-        }
     }
     if (*cp) {
         sp = cp + strlen(cp);
-        emit(cp, sp);
+        emit(cp, sp, &state);
     }
     if (need_color_reset_at_eol) {
         struct atom_value resetv;
diff --git a/ref-filter.h b/ref-filter.h
index fcf469e..b50a036 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -21,12 +21,14 @@ struct atom_value {
     const char *s;
     unsigned long ul; /* used for sorting when not FIELD_STR */
     unsigned int modifier_atom : 1, /*  atoms which act as modifiers
for the next atom */
+        color : 1,
         pad_to_right : 1;
 };

 struct ref_formatting_state {
     int quote_style;
     unsigned int pad_to_right;
+    const char *color;
 };

 struct ref_sorting {
diff --git a/t/t6302-for-each-ref-filter.sh b/t/t6302-for-each-ref-filter.sh
index 68688a9..b9ebb3d 100755
--- a/t/t6302-for-each-ref-filter.sh
+++ b/t/t6302-for-each-ref-filter.sh
@@ -83,17 +83,33 @@ test_expect_success 'filtering with --contains' '

 test_expect_success 'padding to the right using `padright`' '
     cat >expect <<-\EOF &&
-    refs/heads/master|refs/heads/master        |refs/heads/master|
-    refs/heads/side|refs/heads/side          |refs/heads/side|
-    refs/odd/spot|refs/odd/spot            |refs/odd/spot|
-    refs/tags/double-tag|refs/tags/double-tag     |refs/tags/double-tag|
-    refs/tags/four|refs/tags/four           |refs/tags/four|
-    refs/tags/one|refs/tags/one            |refs/tags/one|
-    refs/tags/signed-tag|refs/tags/signed-tag     |refs/tags/signed-tag|
-    refs/tags/three|refs/tags/three          |refs/tags/three|
-    refs/tags/two|refs/tags/two            |refs/tags/two|
+    master|    master    |
+    side|    side      |
+    odd/spot|    odd/spot  |
+    double-tag|    double-tag|
+    four|    four      |
+    one|    one       |
+    signed-tag|    signed-tag|
+    three|    three     |
+    two|    two       |
     EOF
-    git for-each-ref
--format="%(refname)%(padright:25)|%(refname)|%(refname)|" >actual &&
+    git for-each-ref
--format="%(refname:short)%(padright:5)|%(padright:10)%(refname:short)|"
>actual &&
+    test_cmp expect actual
+'
+
+test_expect_success 'no padding when `padright` length is smaller
than atom length' '
+    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:5)%(refname)|" >actual &&
     test_cmp expect actual
 '

Thanks to Everyone for suggestions and tips :)

-- 
Regards,
Karthik Nayak

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

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

Thread overview: 43+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-28  6:32 [PATCH v6 0/10] port tag.c to use ref-filter APIs Karthik Nayak
2015-07-28  6:33 ` [PATCH v6 01/10] ref-filter: introduce 'ref_formatting_state' Karthik Nayak
2015-07-28  6:33   ` [PATCH v6 02/10] ref-filter: add option to pad atoms to the right Karthik Nayak
2015-07-29 19:29     ` Eric Sunshine
2015-07-30 10:18       ` Karthik Nayak
2015-07-28  6:33   ` [PATCH v6 03/10] ref-filter: add option to filter only tags Karthik Nayak
2015-07-28  6:33   ` [PATCH v6 04/10] ref-filter: support printing N lines from tag annotation Karthik Nayak
2015-07-28  6:33   ` [PATCH v6 05/10] ref-filter: add support to sort by version Karthik Nayak
2015-07-29 19:34     ` Eric Sunshine
2015-07-30 10:23       ` Karthik Nayak
2015-07-28  6:33   ` [PATCH v6 06/10] ref-filter: add option to match literal pattern Karthik Nayak
2015-07-28  6:33   ` [PATCH v6 07/10] tag.c: use 'ref-filter' data structures Karthik Nayak
2015-07-28  6:33   ` [PATCH v6 08/10] tag.c: use 'ref-filter' APIs Karthik Nayak
2015-07-28  6:33   ` [PATCH v6 09/10] tag.c: implement '--format' option Karthik Nayak
2015-07-28  6:33   ` [PATCH v6 10/10] tag.c: implement '--merged' and '--no-merged' options Karthik Nayak
2015-07-28  7:26   ` [PATCH v6 01/10] ref-filter: introduce 'ref_formatting_state' Matthieu Moy
2015-07-29 15:56     ` Karthik Nayak
2015-07-29 16:04       ` Matthieu Moy
2015-07-29 16:10         ` Karthik Nayak
2015-07-29 16:35           ` Matthieu Moy
2015-07-29 19:19   ` Eric Sunshine
2015-07-29 19:50     ` Junio C Hamano
2015-07-29 19:54     ` Junio C Hamano
2015-07-30  9:18       ` Karthik Nayak
2015-07-30  9:25       ` Matthieu Moy
2015-07-29 21:34     ` Matthieu Moy
2015-07-30  6:53     ` Karthik Nayak
2015-07-29 19:27 ` [PATCH v6 0/10] port tag.c to use ref-filter APIs Eric Sunshine
2015-07-29 20:29   ` Junio C Hamano
2015-07-30  9:44     ` Matthieu Moy
2015-07-30 10:13       ` Karthik Nayak
2015-07-30 15:35 ` [PATCH v7 01/11] ref-filter: introduce 'ref_formatting_state' Karthik Nayak
2015-07-30 15:35   ` [PATCH v7 02/11] ref-filter: make `color` use `ref_formatting_state` Karthik Nayak
2015-07-30 15:35   ` [PATCH v7 03/11] ref-filter: add option to pad atoms to the right Karthik Nayak
2015-07-30 15:35   ` [PATCH v7 04/11] ref-filter: add option to filter only tags Karthik Nayak
2015-07-30 15:35   ` [PATCH v7 05/11] ref-filter: support printing N lines from tag annotation Karthik Nayak
2015-07-30 15:35   ` [PATCH v7 06/11] ref-filter: add support to sort by version Karthik Nayak
2015-07-30 15:35   ` [PATCH v7 07/11] ref-filter: add option to match literal pattern Karthik Nayak
2015-07-30 15:35   ` [PATCH v7 08/11] tag.c: use 'ref-filter' data structures Karthik Nayak
2015-07-30 15:35   ` [PATCH v7 09/11] tag.c: use 'ref-filter' APIs Karthik Nayak
2015-07-30 15:35   ` [PATCH v7 10/11] tag.c: implement '--format' option Karthik Nayak
2015-07-30 15:35   ` [PATCH v7 11/11] tag.c: implement '--merged' and '--no-merged' options Karthik Nayak
2015-07-30 15:34 [PATCH v7 0/11] port tag.c to use ref-filter APIs Karthik Nayak
2015-07-30 15:48 ` [PATCH v7 01/11] ref-filter: introduce 'ref_formatting_state' Karthik Nayak
2015-07-30 15:48   ` [PATCH v7 08/11] tag.c: use 'ref-filter' data structures Karthik Nayak

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.