port tag.c to use ref-filter APIs
v6 cover port tag.c to use ref-filter APIs
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.
* Renameref_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:
ref-filter.c +1 -3
@@ -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;ref-filter.h +0 -1
@@ -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;};
t/t6302-for-each-ref-filter.sh +10 -11
@@ -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 '
Signature · 2 lines
Regards, Karthik Nayak
PATCH v6 1/10 ref-filter: introduce 'ref_formatting_state'
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.
--- ref-filter.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++------------- ref-filter.h | 5 +++++ 2 files changed, 61 insertions(+), 15 deletions(-)
ref-filter.c +56 -15
@@ -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,47 @@ 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 apply_formatting_state (struct ref_formatting_state *state, +struct atom_value *v,struct strbuf *value) { -struct strbuf sb =STRBUF_INIT ; -switch (quote_style) { +/* Eventually we'll formatt based on the ref_formatting_state */ +strbuf_addstr (value, v->s); +} + +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); + +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 (&value); +strbuf_release (&formatted); }static int hex1 (char ch)
@@ -1254,9 +1273,26 @@ static void emit(const char *cp, const char *ep) } } +static void store_formatting_state (struct ref_formatting_state *state, +struct atom_value *atomv) +{ +/* Here the 'ref_formatting_state' variable will be filled */ +} + +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; +} +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 +1301,12 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int quif (cp < sp)emit (cp, sp);get_ref_atom_value (info,parse_ref_filter_atom (sp +2 , ep), &atomv); -print_value (atomv, quote_style); +if (atomv->modifier_atom) +store_formatting_state (&state, atomv); +else { +print_value (atomv, &state); +reset_formatting_state (&state); + } }if (*cp) { sp = cp +strlen (cp);
@@ -1278,7 +1319,7 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int quif (color_parse ("reset" , color) <0 )die ("BUG: couldn't parse 'reset' as a color" ); resetv.s = color; -print_value (&resetv, quote_style); +print_value (&resetv, &state); }putchar ('\n' ); }
ref-filter.h +5 -0
@@ -19,6 +19,11 @@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 */ +}; + +struct ref_formatting_state { +int quote_style; };struct ref_sorting {
Signature · 1 line
2.4.6
PATCH v6 2/10 ref-filter: add option to pad atoms to the right
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.
--- Documentation/git-for-each-ref.txt | 6 ++++++ ref-filter.c | 27 +++++++++++++++++++++++++-- ref-filter.h | 4 +++- t/t6302-for-each-ref-filter.sh | 16 ++++++++++++++++ 4 files changed, 50 insertions(+), 3 deletions(-)
Documentation/git-for-each-ref.txt +6 -0
@@ -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.
ref-filter.c +25 -2
@@ -55,6 +55,7 @@ static struct { { "flag" }, { "HEAD" }, { "color" }, + { "padright" }, };/*
@@ -689,6 +690,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->modifier_atom =1 ; + v->pad_to_right =1 ; +continue ; }else continue ;
@@ -1195,7 +1208,16 @@ 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) { -/* Eventually we'll formatt based on the ref_formatting_state */ +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_addstr (value, v->s); }
@@ -1276,7 +1298,8 @@ static void emit(const char *cp, const char *ep)static void store_formatting_state (struct ref_formatting_state *state,struct atom_value *atomv) { -/* Here the 'ref_formatting_state' variable will be filled */ +if (atomv->pad_to_right) + state->pad_to_right = atomv->ul; }static void reset_formatting_state (struct ref_formatting_state *state)
ref-filter.h +3 -1
@@ -19,11 +19,13 @@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 */ +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; +unsigned int pad_to_right; };struct ref_sorting {
t/t6302-for-each-ref-filter.sh +16 -0
@@ -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/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="%(refname)%(padright:25)|%(refname)|%(refname)|" >actual && + test_cmp expect actual +' + test_done
Signature · 1 line
2.4.6
Re: [PATCH v6 02/10] ref-filter: add option to pad atoms to the right
Quoted 20 lines · 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.
Signed-off-by Karthik Nayak <[email protected]>---
t/t6302-for-each-ref-filter.sh +3
-0@@ -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/master |refs/heads/master|
t/t6302-for-each-ref-filter.sh test_expect_success 'filtering with --contains' ' · line 96
Quoted 10 lines
t/t6302-for-each-ref-filter.sh +10
-0+ 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="%(refname)%(padright:25)|%(refname)|%(refname)|" >actual &&
This fails to test the important case when the atom length is greater than the padright value (in which case no padding should be done, and the atom text should extend beyond the 'padright' column).
Quoted 6 lines
t/t6302-for-each-ref-filter.sh +3
-1+ test_cmp expect actual +' + test_done --2.4.6
Re: [PATCH v6 02/10] ref-filter: add option to pad atoms to the right
Quoted 27 lines · Eric Sunshine
Quoted 26 lines · 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.
Signed-off-by Karthik Nayak <[email protected]>---
t/t6302-for-each-ref-filter.sh +9
-0@@ -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/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|
Quoted 9 lines
t/t6302-for-each-ref-filter.sh test_expect_success 'filtering with --contains' ' · line 96
Quoted 4 lines
t/t6302-for-each-ref-filter.sh +4
-0+ refs/tags/three|refs/tags/three |refs/tags/three| + refs/tags/two|refs/tags/two |refs/tags/two| + EOF + git for-each-ref --format="%(refname)%(padright:25)|%(refname)|%(refname)|" >actual &&This fails to test the important case when the atom length is greater than the padright value (in which case no padding should be done, and the atom text should extend beyond the 'padright' column).
Will add a test for the same, thanks :)
Signature · 2 lines
Regards, Karthik Nayak
PATCH v6 3/10 ref-filter: add option to filter only tags
In reply to Karthik NayakFrom: Karthik Nayak <[email protected]>
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'
--- ref-filter.c | 2 ++ ref-filter.h | 1 + refs.c | 5 +++++ refs.h | 1 + 4 files changed, 9 insertions(+)
ref-filter.c +2 -0
@@ -1150,6 +1150,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");
ref-filter.h +1 -0
@@ -15,6 +15,7 @@#define FILTER_REFS_INCLUDE_BROKEN 0x1#define FILTER_REFS_ALL 0x2 +#define FILTER_REFS_TAGS 0x4struct atom_value {const char *s;
refs.c +5 -0
@@ -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);
refs.h +1 -0
@@ -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);
Signature · 1 line
2.4.6
PATCH v6 4/10 ref-filter: support printing N lines from tag annotation
In reply to Karthik NayakFrom: Karthik Nayak <[email protected]>
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.
--- 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(-)
builtin/for-each-ref.c +1 -1
@@ -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 ; }
builtin/tag.c +4 -0
@@ -185,6 +185,10 @@ static enum contains_result contains(struct commit *candidate,return contains_test (candidate, want); } +/* +* Currently duplicated in ref-filter, will eventually be removed as +* we port tag.c to use ref-filter APIs. +*/ static void show_tag_lines (const struct object_id *oid,int lines) {int i;
ref-filter.c +50 -1
63 lines not on this page · raw message
ref-filter.h +7 -2
@@ -62,6 +62,7 @@ struct ref_filter {struct commit *merge_commit; unsigned int with_commit_tag_algo : 1; +unsigned int lines; };struct ref_filter_cbdata {
@@ -93,8 +94,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 */
Signature · 1 line
2.4.6
PATCH v6 5/10 ref-filter: add support to sort by version
In reply to Karthik NayakFrom: Karthik Nayak <[email protected]>
Add support to sort by version using the "v:refname" and "version:refname" option. This is achieved by using the 'versioncmp()' function as the comparing function for qsort.
This option is included to support sorting by versions in git tag -l which will eventaully be ported to use ref-filter APIs.
Add documentation and tests for the same.
--- 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(-)
Documentation/git-for-each-ref.txt +3 -0
@@ -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.
ref-filter.c +9 -5
@@ -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 ;
@@ -1170,19 +1171,19 @@ static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, struget_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; }
@@ -1427,6 +1428,9 @@ int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset) s->reverse =1 ; arg++; } +if (skip_prefix (arg,"version:" , &arg) || +skip_prefix (arg,"v:" , &arg)) + s->version =1 ; len =strlen (arg); s->atom =parse_ref_filter_atom (arg, arg+len);return 0 ;
ref-filter.h +2 -1
@@ -32,7 +32,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 {
t/t6302-for-each-ref-filter.sh +36 -0
@@ -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
Signature · 1 line
2.4.6
Re: [PATCH v6 05/10] ref-filter: add support to sort by version
Quoted 16 lines · Karthik Nayak
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 -lwhich will eventaully be ported to use ref-filter APIs.Add documentation and tests for the same.
Signed-off-by Karthik Nayak <[email protected]>---
Documentation/git-for-each-ref.txt +0
-0
Documentation/git-for-each-ref.txt For sorting purposes, fields with numeric values sort in numeric · line 155
Quoted 5 lines
Documentation/git-for-each-ref.txt +2
-0@@ -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`.
Nit: "or in short" reads a bit oddly. Perhaps:
...version:refname or its alias v:refname.
or
...version:refname or the short-form v:refname.
(I rather prefer the "alias" alternative.)
Quoted 16 lines
Documentation/git-for-each-ref.txt +0
-0In 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.t/t6302-for-each-ref-filter.sh +8
-0@@ -97,4 +97,40 @@ test_expect_success 'padding to the right using `padright`' ' +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
t/t6302-for-each-ref-filter.sh test_expect_success 'padding to the right using `padright`' ' · line 114
Quoted 10 lines
t/t6302-for-each-ref-filter.sh +10
-0+' + +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
Nit: In the earlier review when I suggested using "v:refname" for one of the tests in order to exercise it (in addition to "version:refname"), I didn't mean that you had to add another (identical) test but rather that you could have one of the existing tests use "v:refname". (Not a big deal. You can leave this as is if you like. I just wanted to clarify.)
Quoted 20 lines
t/t6302-for-each-ref-filter.sh +12
-1+' + +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
--
To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH v6 05/10] ref-filter: add support to sort by version
Quoted 25 lines · Eric Sunshine
Quoted 16 lines · Karthik Nayak
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 -lwhich will eventaully be ported to use ref-filter APIs.Add documentation and tests for the same.
Signed-off-by Karthik Nayak <[email protected]>---
Documentation/git-for-each-ref.txt +0
-0Documentation/git-for-each-ref.txt For sorting purposes, fields with numeric values sort in numeric · line 155
Quoted 5 lines
Documentation/git-for-each-ref.txt +2
-0@@ -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`.Nit: "or in short" reads a bit oddly. Perhaps:
Quoted 8 lines
...
version:refnameor its aliasv:refname.or
...
version:refnameor the short-formv:refname.(I rather prefer the "alias" alternative.)
Thanks! will change.
Quoted 25 lines
Quoted 25 lines
Documentation/git-for-each-ref.txt +0
-0In 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.t/t6302-for-each-ref-filter.sh +17
-0@@ -97,4 +97,40 @@ test_expect_success 'padding to the right using `padright`' ' +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
Quoted 9 lines
t/t6302-for-each-ref-filter.sh test_expect_success 'padding to the right using `padright`' ' · line 114
Quoted 1 line
t/t6302-for-each-ref-filter.sh +1
-0+ test_cmp expect actualNit: In the earlier review when I suggested using "v:refname" for one of the tests in order to exercise it (in addition to "version:refname"), I didn't mean that you had to add another (identical) test but rather that you could have one of the existing tests use "v:refname". (Not a big deal. You can leave this as is if you like. I just wanted to clarify.)
Ah! Thats ok, this seems to explain the commit entirely showing that "v:refname" and "version:refname" are the same thing.
Signature · 2 lines
Regards, Karthik Nayak
PATCH v6 6/10 ref-filter: add option to match literal pattern
In reply to Karthik NayakFrom: Karthik Nayak <[email protected]>
Since 'ref-filter' only has an option to match path names add an option for plain fnmatch pattern-matching.
This is to support the pattern matching options which are used in git tag -l and git branch -l where we can match patterns like git tag -l foo* which would match all tags which has a "foo*" pattern.
--- builtin/for-each-ref.c | 1 + ref-filter.c | 42 +++++++++++++++++++++++++++++++++++++++--- ref-filter.h | 3 ++- 3 files changed, 42 insertions(+), 4 deletions(-)
builtin/for-each-ref.c +1 -0
@@ -68,6 +68,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)git_config (git_default_config,NULL ); filter.name_patterns = argv; + filter.match_as_path =1 ;filter_refs (&array, &filter,FILTER_REFS_ALL |FILTER_REFS_INCLUDE_BROKEN );ref_array_sort (sorting, &array);
ref-filter.c +39 -3
61 lines not on this page · raw message
ref-filter.h +2 -1
@@ -62,7 +62,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; };
Signature · 1 line
2.4.6
PATCH v6 7/10 tag.c: use 'ref-filter' data structures
In reply to Karthik NayakFrom: Karthik Nayak <[email protected]>
Make 'tag.c' use 'ref-filter' data structures and make changes to support the new data structures. This is a part of the process of porting 'tag.c' to use 'ref-filter' APIs.
This is a temporary step before porting 'tag.c' to use 'ref-filter' completely. As this is a temporary step, most of the code introduced here will be removed when 'tag.c' is ported over to use 'ref-filter' APIs
--- builtin/tag.c | 106 +++++++++++++++++++++++++++++++--------------------------- 1 file changed, 57 insertions(+), 49 deletions(-)
builtin/tag.c +57 -49
@@ -17,6 +17,7 @@#include "gpg-interface.h" #include "sha1-array.h" #include "column.h" +#include "ref-filter.h" static const char *const git_tag_usage[] = {N_ ("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>]" ),
@@ -34,15 +35,6 @@ static const char * const git_tag_usage[] = {static int tag_sort; -struct tag_filter { -const char **patterns; -int lines; -int sort; -struct string_list tags; -struct commit_list *with_commit; -}; - -static struct sha1_array points_at;static unsigned int colopts;static int match_pattern (const char **patterns,const char *ref)
@@ -61,19 +53,20 @@ static int match_pattern(const char **patterns, const char *ref)* removed as we port tag.c to use the ref-filter APIs. */ static const unsigned char *match_points_at (const char *refname, -const unsigned char *sha1) +const unsigned char *sha1, +struct sha1_array *points_at) {const unsigned char *tagged_sha1 =NULL ;struct object *obj; -if (sha1_array_lookup (&points_at, sha1) >=0 ) +if (sha1_array_lookup (points_at, sha1) >=0 )return sha1; obj =parse_object (sha1);if (!obj)die (_ ("malformed object at '%s'" ), refname);if (obj->type ==OBJ_TAG ) tagged_sha1 = ((struct tag *)obj)->tagged->sha1; -if (tagged_sha1 &&sha1_array_lookup (&points_at, tagged_sha1) >=0 ) +if (tagged_sha1 &&sha1_array_lookup (points_at, tagged_sha1) >=0 )return tagged_sha1;return NULL ; }
@@ -228,12 +221,24 @@ free_return:free (buf); } +static void ref_array_append (struct ref_array *array,const char *refname) +{ +size_t len =strlen (refname); +struct ref_array_item *ref =xcalloc (1 ,sizeof (struct ref_array_item ) + len +1 ); +memcpy (ref->refname, refname, len); + ref->refname[len] ='\0' ; +REALLOC_ARRAY (array->items, array->nr +1 ); + array->items[array->nr++] = ref; +} +static int show_reference (const char *refname,const struct object_id *oid,int flag,void *cb_data) { -struct tag_filter *filter = cb_data; +struct ref_filter_cbdata *data = cb_data; +struct ref_array *array = data->array; +struct ref_filter *filter = data->filter; -if (match_pattern (filter->patterns, refname)) { +if (match_pattern (filter->name_patterns, refname)) {if (filter->with_commit) {struct commit *commit;
@@ -244,12 +249,12 @@ static int show_reference(const char *refname, const struct object_id *oid,return 0 ; } -if (points_at.nr && !match_points_at (refname, oid->hash)) +if (filter->points_at.nr && !match_points_at (refname, oid->hash, &filter->points_at))return 0 ;if (!filter->lines) { -if (filter->sort) -string_list_append (&filter->tags, refname); +if (tag_sort) +ref_array_append (array, refname);else printf ("%s\n" , refname);return 0 ;
@@ -264,36 +269,36 @@ static int show_reference(const char *refname, const struct object_id *oid,static int sort_by_version (const void *a_,const void *b_) { -const struct string_list_item *a = a_; -const struct string_list_item *b = b_; -return versioncmp (a->string, b->string); +const struct ref_array_item *a = *((struct ref_array_item **)a_); +const struct ref_array_item *b = *((struct ref_array_item **)b_); +return versioncmp (a->refname, b->refname); } -static int list_tags (const char **patterns,int lines, -struct commit_list *with_commit,int sort) +static int list_tags (struct ref_filter *filter,int sort) { -struct tag_filter filter; +struct ref_array array; +struct ref_filter_cbdata data; + +memset (&array,0 ,sizeof (array)); + data.array = &array; + data.filter = filter; - filter.patterns = patterns; - filter.lines = lines; - filter.sort = sort; - filter.with_commit = with_commit; -memset (&filter.tags,0 ,sizeof (filter.tags)); - filter.tags.strdup_strings =1 ; +if (filter->lines ==-1 ) + filter->lines =0 ; -for_each_tag_ref (show_reference, (void *)&filter); +for_each_tag_ref (show_reference, &data);if (sort) {int i;if ((sort &SORT_MASK ) ==VERCMP_SORT ) -qsort (filter.tags.items, filter.tags.nr, -sizeof (struct string_list_item ), sort_by_version); +qsort (array.items, array.nr, +sizeof (struct ref_array_item *), sort_by_version);if (sort &REVERSE_SORT ) -for (i = filter.tags.nr -1 ; i >=0 ; i--) -printf ("%s\n" , filter.tags.items[i].string); +for (i = array.nr -1 ; i >=0 ; i--) +printf ("%s\n" , array.items[i]->refname);else -for (i =0 ; i < filter.tags.nr; i++) -printf ("%s\n" , filter.tags.items[i].string); -string_list_clear (&filter.tags,0 ); +for (i =0 ; i < array.nr; i++) +printf ("%s\n" , array.items[i]->refname); +ref_array_clear (&array); }return 0 ; }
@@ -574,17 +579,17 @@ int cmd_tag(int argc, const char **argv, const char *prefix)const char *object_ref, *tag;struct create_tag_options opt;char *cleanup_arg =NULL ; -int annotate =0 , force =0 , lines =-1 ;int create_reflog =0 ; +int annotate =0 , force =0 ;int cmdmode =0 ;const char *msgfile =NULL , *keyid =NULL ;struct msg_arg msg = {0 ,STRBUF_INIT }; -struct commit_list *with_commit =NULL ;struct ref_transaction *transaction;struct strbuf err =STRBUF_INIT ; +struct ref_filter filter;struct option options[] = {OPT_CMDMODE ('l' ,"list" , &cmdmode,N_ ("list tag names" ),'l' ), - {OPTION_INTEGER ,'n' ,NULL , &lines,N_ ("n" ), + {OPTION_INTEGER ,'n' ,NULL , &filter.lines,N_ ("n" ),N_ ("print <n> lines of each tag message" ),PARSE_OPT_OPTARG ,NULL ,1 },OPT_CMDMODE ('d' ,"delete" , &cmdmode,N_ ("delete tags" ),'d' ),
@@ -606,14 +611,14 @@ int cmd_tag(int argc, const char **argv, const char *prefix)OPT_GROUP (N_ ("Tag listing options" )),OPT_COLUMN (0 ,"column" , &colopts,N_ ("show tag list in columns" )), -OPT_CONTAINS (&with_commit,N_ ("print only tags that contain the commit" )), -OPT_WITH (&with_commit,N_ ("print only tags that contain the commit" )), +OPT_CONTAINS (&filter.with_commit,N_ ("print only tags that contain the commit" )), +OPT_WITH (&filter.with_commit,N_ ("print only tags that contain the commit" )), {OPTION_CALLBACK ,0 ,"sort" , &tag_sort,N_ ("type" ),N_ ("sort tags" ),PARSE_OPT_NONEG , parse_opt_sort }, { -OPTION_CALLBACK ,0 ,"points-at" , &points_at,N_ ("object" ), +OPTION_CALLBACK ,0 ,"points-at" , &filter.points_at,N_ ("object" ),N_ ("print only tags of the object" ),0 , parse_opt_object_name },OPT_END ()
@@ -622,6 +627,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)git_config (git_tag_config,NULL );memset (&opt,0 ,sizeof (opt)); +memset (&filter,0 ,sizeof (filter)); + filter.lines =-1 ; argc =parse_options (argc, argv, prefix, options, git_tag_usage,0 );
@@ -638,7 +645,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)usage_with_options (git_tag_usage, options);finalize_colopts (&colopts,-1 ); -if (cmdmode =='l' && lines !=-1 ) { +if (cmdmode =='l' && filter.lines !=-1 ) {if (explicitly_enable_column (colopts))die (_ ("--column and -n are incompatible" )); colopts =0 ;
@@ -651,18 +658,19 @@ int cmd_tag(int argc, const char **argv, const char *prefix) copts.padding =2 ;run_column_filter (colopts, &copts); } -if (lines !=-1 && tag_sort) +if (filter.lines !=-1 && tag_sort)die (_ ("--sort and -n are incompatible" )); - ret =list_tags (argv, lines ==-1 ?0 : lines, with_commit, tag_sort); + filter.name_patterns = argv; + ret =list_tags (&filter, tag_sort);if (column_active (colopts))stop_column_filter ();return ret; } -if (lines !=-1 ) +if (filter.lines !=-1 )die (_ ("-n option is only allowed with -l." )); -if (with_commit) +if (filter.with_commit)die (_ ("--contains option is only allowed with -l." )); -if (points_at.nr) +if (filter.points_at.nr)die (_ ("--points-at option is only allowed with -l." ));if (cmdmode =='d' )return for_each_tag_name (argv, delete_tag);
Signature · 1 line
2.4.6
PATCH v6 8/10 tag.c: use 'ref-filter' APIs
In reply to Karthik NayakFrom: Karthik Nayak <[email protected]>
Make 'tag.c' use 'ref-filter' APIs for iterating through refs sorting and printing of refs. This removes most of the code used in 'tag.c' replacing it with calls to the 'ref-filter' library.
Make 'tag.c' use the 'filter_refs()' function provided by 'ref-filter' to filter out tags based on the options set.
For printing tags we use 'show_ref_array_item()' function provided by 'ref-filter'.
We improve the sorting option provided by 'tag.c' by using the sorting options provided by 'ref-filter'. This causes the test 'invalid sort parameter on command line' in t7004 to fail, as 'ref-filter' throws an error for all sorting fields which are incorrect. The test is changed to reflect the same.
Modify documentation for the same.
--- Documentation/git-tag.txt | 16 ++- builtin/tag.c | 342 ++++++---------------------------------------- t/t7004-tag.sh | 8 +- 3 files changed, 50 insertions(+), 316 deletions(-)
Documentation/git-tag.txt +9 -7
@@ -13,7 +13,7 @@ SYNOPSIS <tagname> [<commit> | <object>] 'git tag' -d <tagname>... 'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>] - [--column[=<options>] | --no-column] [--create-reflog] [<pattern>...] + [--column[=<options>] | --no-column] [--create-reflog] [--sort=<key>] [<pattern>...] 'git tag' -v <tagname>... DESCRIPTION
@@ -94,14 +94,16 @@ OPTIONS using fnmatch(3)). Multiple patterns may be given; if any of them matches, the tag is shown. ---sort=<type>:: - Sort in a specific order. Supported type is "refname" - (lexicographic order), "version:refname" or "v:refname" (tag +--sort=<key>:: + Sort based on the key given. Prefix `-` to sort in + descending order of the value. You may use the --sort=<key> option + multiple times, in which case the last key becomes the primary + key. Also supports "version:refname" or "v:refname" (tag names are treated as versions). The "version:refname" sort order can also be affected by the - "versionsort.prereleaseSuffix" configuration variable. Prepend - "-" to reverse sort order. When this option is not given, the - sort order defaults to the value configured for the 'tag.sort' + "versionsort.prereleaseSuffix" configuration variable. + The keys supported are the same as those in `git for-each-ref`. + Sort order defaults to the value configured for the 'tag.sort' variable if it exists, or lexicographic order otherwise. See linkgit:git-config[1].
builtin/tag.c +40 -302
416 lines not on this page · raw message
t/t7004-tag.sh +1 -7
@@ -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' '
Signature · 1 line
2.4.6
PATCH v6 9/10 tag.c: implement '--format' option
In reply to Karthik NayakFrom: Karthik Nayak <[email protected]>
Implement the '--format' option provided by 'ref-filter'. This lets the user list tags as per desired format similar to the implementation in 'git for-each-ref'.
Add tests and documentation for the same.
--- Documentation/git-tag.txt | 15 ++++++++++++++- builtin/tag.c | 11 +++++++---- t/t7004-tag.sh | 16 ++++++++++++++++ 3 files changed, 37 insertions(+), 5 deletions(-)
Documentation/git-tag.txt +14 -1
@@ -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 -------------
builtin/tag.c +7 -4
@@ -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;
t/t7004-tag.sh +16 -0
@@ -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
Signature · 1 line
2.4.6
PATCH v6 10/10 tag.c: implement '--merged' and '--no-merged' options
In reply to Karthik NayakFrom: Karthik Nayak <[email protected]>
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.
--- Documentation/git-tag.txt | 7 ++++++- builtin/tag.c | 6 +++++- t/t7004-tag.sh | 27 +++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-)
Documentation/git-tag.txt +6 -1
@@ -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 -------------
builtin/tag.c +5 -1
@@ -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' )
t/t7004-tag.sh +27 -0
@@ -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
Signature · 1 line
2.4.6
Re: [PATCH v6 01/10] ref-filter: introduce 'ref_formatting_state'
In reply to Karthik Nayakref-filter.c void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array) · line 1198
Quoted 7 lines · Karthik Nayak
ref-filter.c +3
-3-static void print_value (struct atom_value *v,int quote_style) +static void apply_formatting_state (struct ref_formatting_state *state, +struct atom_value *v,struct strbuf *value) { -struct strbuf sb =STRBUF_INIT ; -switch (quote_style) { +/* Eventually we'll formatt based on the ref_formatting_state */
s/formatt/format/
Also, we usually use a single space after /*.
(Neither is very important since it disapears in the next commit)
ref-filter.c void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array) · line 1208
Quoted 2 lines
ref-filter.c +2
-0+/* +* Some (pesudo) atoms have no immediate side effect, but only
s/pesudo/pseudo/. But if you are to rename these atoms to "modifier atoms", you should get rid of this "pseudo" here.
Signature · 2 lines
Matthieu Moy http://www-verimag.imag.fr/~moy/
Re: [PATCH v6 01/10] ref-filter: introduce 'ref_formatting_state'
Quoted 14 lines · Matthieu Moy
ref-filter.c void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array) · line 1198
Quoted 7 lines · Karthik Nayak
ref-filter.c +3
-3-static void print_value (struct atom_value *v,int quote_style) +static void apply_formatting_state (struct ref_formatting_state *state, +struct atom_value *v,struct strbuf *value) { -struct strbuf sb =STRBUF_INIT ; -switch (quote_style) { +/* Eventually we'll formatt based on the ref_formatting_state */ s/formatt/format/
Also, we usually use a single space after /*.
Quoted 8 lines
(Neither is very important since it disapears in the next commit)
ref-filter.c void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array) · line 1208
Quoted 2 lines
ref-filter.c +2
-0+/* +* Some (pesudo) atoms have no immediate side effect, but only s/pesudo/pseudo/. But if you are to rename these atoms to "modifier atoms", you should get rid of this "pseudo" here.
Ah, I hate making grammatical errors, Even though I check it always gets away. Anyways waiting for Junio and others to reply on this version. Could do a resend for this series if needed.
Signature · 2 lines
Regards, Karthik Nayak
Re: [PATCH v6 01/10] ref-filter: introduce 'ref_formatting_state'
Quoted 3 lines · Karthik Nayak
Ah, I hate making grammatical errors, Even though I check it always gets away. Anyways waiting for Junio and others to reply on this version. Could do a resend for this series if needed.
If you took all my remarks into account, I think it's worth a resend as it should make it easier for new reviewers.
Signature · 2 lines
Matthieu Moy http://www-verimag.imag.fr/~moy/
Re: [PATCH v6 01/10] ref-filter: introduce 'ref_formatting_state'
Quoted 9 lines · Matthieu Moy
Quoted 3 lines · Karthik Nayak
Ah, I hate making grammatical errors, Even though I check it always gets away. Anyways waiting for Junio and others to reply on this version. Could do a resend for this series if needed.
If you took all my remarks into account, I think it's worth a resend as it should make it easier for new reviewers.
Correct me If I missed something, but the only remark for this series were the ones mentioned
above right? I mean branch.c being an RFC patch series and separate from this.
Signature · 2 lines
Regards, Karthik Nayak
Re: [PATCH v6 01/10] ref-filter: introduce 'ref_formatting_state'
Quoted 8 lines · Karthik Nayak
Quoted 6 lines · Matthieu Moy
Quoted 3 lines · Karthik Nayak
Ah, I hate making grammatical errors, Even though I check it always gets away. Anyways waiting for Junio and others to reply on this version. Could do a resend for this series if needed.
Quoted 7 lines
Quoted 3 lines
If you took all my remarks into account, I think it's worth a resend as it should make it easier for new reviewers.
Correct me If I missed something, but the only remark for this series were the ones mentioned
above right?
Oops, indeed, I did not realize we were in the other series.
Signature · 2 lines
Matthieu Moy http://www-verimag.imag.fr/~moy/
PATCH v6 1/10 ref-filter: introduce 'ref_formatting_state'
In reply to Karthik NayakQuoted 4 lines · Karthik Nayak
Introduce '
ref_formatting' structure to hold values of pseudo atoms which help only in formatting. This will eventually be used by atoms likecolorand thepadrightatom which will be introduced in a later patch.
Isn't this commit message outdated now that you no longer treat color specially and since the terminology is changing from "pseudo" to "modifier"? Also, isn't the structure now called 'ref_formatting_state' rather than 'ref_formatting'?
ref-filter.c static void populate_value(struct ref_array_item *ref) · line 625
Quoted 12 lines
Signed-off-by Karthik Nayak <[email protected]>---
ref-filter.c +1
-1@@ -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 ;
What is this change about? It doesn't seem to be related to anything else in the patch.
Quoted 6 lines
ref-filter.c +2
-0const char *formatp;struct branch *branch =NULL ;@@ -1190,30 +1192,47 @@ void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array) +static void print_value (struct atom_value *v,struct ref_formatting_state *state) +{
ref-filter.c void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array) · line 1203
Quoted 10 lines
ref-filter.c +10
-0+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);
The comment says that this is "storing" formatting state, however, the code is actually "applying" the state. You could move this comment down to show_ref_array_item() where formatting state actually gets stored. Or you could fix it to talk about "applying" the state. However, now that apply_formatting_state() has a meaningful name, you could also drop the comment altogether since it doesn't say much beyond what is said already by the function name.
ref-filter.c static void emit(const char *cp, const char *ep) · line 1277
Quoted 9 lines
ref-filter.c +6
-1+switch (state->quote_style) {case QUOTE_NONE : -fputs (v->s, stdout);@@ -1254,9 +1273,26 @@ static void emit(const char *cp, const char *ep) +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;
I wonder if this sledge-hammer approach of saving one or two values before clearing the entire 'ref_formatting_state' and then restoring the saved values will scale well. Would it be better for this to just individually reset the fields which need resetting and not touch those that don't?
Also, the fact that quote_style has to be handled specially may be an indication that it doesn't belong in this structure grouped with the other modifiers or that you need better classification within the structure. For instance:
struct ref_formatting_state {
struct global {
int quote_style;
};
struct local {
int pad_right;
};where 'local' state gets reset by reset_formatting_state(), and 'global' is left alone.
That's just one idea, not necessarily a proposal, but is something to think about since the current arrangement is kind of yucky.
ref-filter.c static void emit(const char *cp, const char *ep) · line 1295
Quoted 9 lines
ref-filter.c +6
-0+} +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;
It's a little bit ugly to use memset() here when you have reset_formatting_state() available. You could set quote_style first, and then call reset_formatting_state() rather than memset(). Or, perhaps, change reset_formatting_state(), as described above, to stop using the sledge-hammer approach.
Quoted 2 lines
ref-filter.c +0
-0for (cp = format; *cp && (sp =find_next (cp)); cp = ep +1 ) {struct atom_value *atomv;
Re: [PATCH v6 01/10] ref-filter: introduce 'ref_formatting_state'
Quoted 13 lines · Eric Sunshine
ref-filter.c static void emit(const char *cp, const char *ep) · line 1277
Quoted 6 lines
ref-filter.c +5
-0@@ -1254,9 +1273,26 @@ static void emit(const char *cp, const char *ep) +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;I wonder if this sledge-hammer approach of saving one or two values before clearing the entire '
ref_formatting_state' and then restoring the saved values will scale well. Would it be better for this to just individually reset the fields which need resetting and not touch those that don't?
Quoted 4 lines
Also, the fact that
quote_stylehas to be handled specially may be an indication that it doesn't belong in this structure grouped with the other modifiers or that you need better classification within the structure.
Actually, I think it is wrong to have this function in the first place. It is a sign that the caller is doing too little before calling this function.
If the act of printing an atom uses the formatting state that says "next one needs X", then it is responsible to clear that "next one needs X" part of the state, as it is the one who consumed that state. E.g. if it used to say "next one needs to be padded to the right" before entering print_value(), then the function did that "padded output", then the "next one needs to be padded to the right" should be cleared inside print_value().
And with that arrangement, together with calling emit() with formatting state, %(color:blue) can be handled as a normal part of the formatting state mechanism. The pseudo/modifier atom should update the state to "Start printing in blue", and either emit() or print_value(), whichever is called first, would notice that state, does what was requested, and flip that bit down (because we are already printing in "blue" so the next output function does not have to do the "blue" thing again).
Re: [PATCH v6 01/10] ref-filter: introduce 'ref_formatting_state'
Quoted 4 lines · Junio C Hamano
Actually, I think it is wrong to have this function in the first place. It is a sign that the caller is doing too little before calling this function.
Quoted 8 lines
If the act of printing an atom uses the formatting state that says "next one needs X", then it is responsible to clear that "next one needs X" part of the state, as it is the one who consumed that state. E.g. if it used to say "next one needs to be padded to the right" before entering
print_value(), then the function did that "padded output", then the "next one needs to be padded to the right" should be cleared insideprint_value().
Hmm, something like what we did in version 5 I guess.
Quoted 8 lines
And with that arrangement, together with calling
emit()with formatting state, %(color:blue) can be handled as a normal part of the formatting state mechanism. The pseudo/modifier atom should update the state to "Start printing in blue", and eitheremit()orprint_value(), whichever is called first, would notice that state, does what was requested, and flip that bit down (because we are already printing in "blue" so the next output function does not have to do the "blue" thing again).
This makes sense, will work on this thanks :)
Signature · 2 lines
Regards, Karthik Nayak
Re: [PATCH v6 01/10] ref-filter: introduce 'ref_formatting_state'
In reply to Junio C HamanoQuoted 7 lines · Junio C Hamano
If the act of printing an atom uses the formatting state that says "next one needs X", then it is responsible to clear that "next one needs X" part of the state, as it is the one who consumed that state. E.g. if it used to say "next one needs to be padded to the right" before entering
print_value(), then the function did that "padded output", then the "next one needs to be padded to the right" should be cleared insideprint_value().
Good point. This would also reduce the risk of forgetting to reset, as "use" and "reset" would be next to each other in the code.
Signature · 2 lines
Matthieu Moy http://www-verimag.imag.fr/~moy/
Re: [PATCH v6 01/10] ref-filter: introduce 'ref_formatting_state'
In reply to Eric SunshineQuoted 13 lines · Eric Sunshine
ref-filter.c static void emit(const char *cp, const char *ep) · line 1277
Quoted 6 lines
ref-filter.c +5
-0@@ -1254,9 +1273,26 @@ static void emit(const char *cp, const char *ep) +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;I wonder if this sledge-hammer approach of saving one or two values before clearing the entire '
ref_formatting_state' and then restoring the saved values will scale well. Would it be better for this to just individually reset the fields which need resetting and not touch those that don't?
Quoted 4 lines
Also, the fact that
quote_stylehas to be handled specially may be an indication that it doesn't belong in this structure grouped with the other modifiers or that you need better classification within the structure.
Actually, I think it is wrong to have this function in the first place. It is a sign that the caller is doing too little before calling this function.
If the act of printing an atom uses the formatting state that says "next one needs X", then it is responsible to clear that "next one needs X" part of the state, as it is the one who consumed that state. E.g. if it used to say "next one needs to be padded to the right" before entering print_value(), then the function did that "padded output", then the "next one needs to be padded to the right" should be cleared inside print_value().
Re: [PATCH v6 01/10] ref-filter: introduce 'ref_formatting_state'
In reply to Eric SunshineQuoted 10 lines · Eric Sunshine
Quoted 4 lines · Karthik Nayak
Introduce '
ref_formatting' structure to hold values of pseudo atoms which help only in formatting. This will eventually be used by atoms likecolorand thepadrightatom which will be introduced in a later patch.Isn't this commit message outdated now that you no longer treat color specially and since the terminology is changing from "pseudo" to "modifier"? Also, isn't the structure now called '
ref_formatting_state' rather than 'ref_formatting'?
Yes, thanks for pointing it out. will change.
Quoted 17 lines
ref-filter.c static void populate_value(struct ref_array_item *ref) · line 625
Quoted 13 lines
Signed-off-by Karthik Nayak <[email protected]>---
ref-filter.c +1
-1@@ -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 ;What is this change about? It doesn't seem to be related to anything else in the patch.
In previous versions it was giving a refname not assigned error before usage error, in the current version, its not needed. will remove.
Quoted 17 lines
Quoted 6 lines
ref-filter.c +2
-0const char *formatp;struct branch *branch =NULL ;@@ -1190,30 +1192,47 @@ void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array) +static void print_value (struct atom_value *v,struct ref_formatting_state *state) +{ref-filter.c void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array) · line 1203
Quoted 10 lines
ref-filter.c +10
-0+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);
Quoted 8 lines
The comment says that this is "storing" formatting state, however, the code is actually "applying" the state. You could move this comment down to
show_ref_array_item()where formatting state actually gets stored. Or you could fix it to talk about "applying" the state. However, now thatapply_formatting_state()has a meaningful name, you could also drop the comment altogether since it doesn't say much beyond what is said already by the function name.
I guess I'll drop the comment thanks :)
Quoted 29 lines
ref-filter.c static void emit(const char *cp, const char *ep) · line 1277
Quoted 9 lines
ref-filter.c +6
-1+switch (state->quote_style) {case QUOTE_NONE : -fputs (v->s, stdout);@@ -1254,9 +1273,26 @@ static void emit(const char *cp, const char *ep) +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;I wonder if this sledge-hammer approach of saving one or two values before clearing the entire '
ref_formatting_state' and then restoring the saved values will scale well. Would it be better for this to just individually reset the fields which need resetting and not touch those that don't?Also, the fact that
quote_stylehas to be handled specially may be an indication that it doesn't belong in this structure grouped with the other modifiers or that you need better classification within the structure. For instance:struct ref_formatting_state { struct global { int quote_style; }; struct local { int pad_right; };
Quoted 6 lines
where 'local' state gets reset by
reset_formatting_state(), and 'global' is left alone.That's just one idea, not necessarily a proposal, but is something to think about since the current arrangement is kind of yucky.
Did you read Junio's suggestion about not having a reset_formatting_state() and rather just have each state be responsible of resetting itself.
I think thats seems to be a better approach.
Quoted 7 lines
Quoted 7 lines
ref-filter.c +4
-0+} +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; +
Quoted 9 lines
ref-filter.c static void emit(const char *cp, const char *ep) · line 1295
Quoted 2 lines
ref-filter.c +2
-0+memset (&state,0 ,sizeof (state)); + state.quote_style = quote_style;It's a little bit ugly to use
memset()here when you havereset_formatting_state()available. You could setquote_stylefirst, and then callreset_formatting_state()rather thanmemset(). Or, perhaps, changereset_formatting_state(), as described above, to stop using the sledge-hammer approach.
I guess even this would be taken care of by implementing Junio's suggestion.
Signature · 2 lines
Regards, Karthik Nayak
Re: [PATCH v6 01/10] ref-filter: introduce 'ref_formatting_state'
In reply to Eric SunshineQuoted 4 lines · Eric Sunshine
Quoted 4 lines
ref-filter.c +3
-0@@ -1254,9 +1273,26 @@ static void emit(const char *cp, const char *ep) +static void reset_formatting_state (struct ref_formatting_state *state) +{ +int quote_style = state->quote_style;
Quoted 8 lines
ref-filter.c static void emit(const char *cp, const char *ep) · line 1286
Quoted 2 lines
ref-filter.c +2
-0+memset (state,0 ,sizeof (*state)); + state->quote_style = quote_style;I wonder if this sledge-hammer approach of saving one or two values before clearing the entire '
ref_formatting_state' and then restoring the saved values will scale well. Would it be better for this to just individually reset the fields which need resetting and not touch those that don't?
I'm the one who suggested these 3 lines. I wrote them this way with the assumption that there would only be 1 field to keep, and thet the rest of the series was going to add more fields to reset (currently true I think), to avoid the risk of forgetting one value to reset.
I'm fine with the other way around too.
Signature · 2 lines
Matthieu Moy http://www-verimag.imag.fr/~moy/
Quoted 9 lines · Karthik Nayak
Changes in v6:
* Change the flow of commits, introducerest_formatting_state.
* Renameref_formatting->apply_formatting_stateapply_pseudo_state->store_formatting_state
* Remove the patch for making color a pseudo atom, and renamepseudo_atomtomodifier_atom.
* Small grammatical changes.
Quoted 3 lines
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
This isn't supported by the current %(padright:) syntax, but an example would be if someone wants to pad a string composed of atoms and literal strings. For instance, the user might want to right-pad the composed string "%(refattribute1) glorked %(refattribute2)”.
One possible syntax to support this sort of thing would be %(padright:n:content). For instance, using the example above:
%(padright:20:%(refattribute1) glorked %(refattribute2))
Another would be %(padright:n)content%(end). The %(end) token could work with other formatting directives, such as left padding, alignment, etc. For instance:
%(padright:20)%(refattribute1) glorked %(refattribute2)%(end)
In fact, this sort of thing has come up multiple times, with the most general being actual embedding of a full-blown scripting language to support pretty much any desired formatting. Other less ambitious proposals have also been suggested.
This isn't a suggestion that you should implement any of these, but just a heads-up that, when it comes to formatting, it's not at all uncommon for people to come up with needs not anticipated by the design.
Quoted 8 lines · Eric Sunshine
Quoted 3 lines
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
This isn't supported by the current %(padright:) syntax, but an example would be if someone wants to pad a string composed of atoms and literal strings. For instance, the user might want to right-pad the composed string "%(refattribute1) glorked %(refattribute2)”.
It is an excellent example that shows why "something of known length" argument needs to be rethought.
"Currently we do not need it to reimplement the canned 'tag -l' format" is an OK and sensible justification to stick to the current implementation of %(padright:N), but we'd need to think if we would want to keep this limited and strange form that applies to a single atom that comes next (ignoring any literal spans) as a private implementation detail between ref-filter and "git tag". Opening it up to end-users would not mean we cannot add a correctly operating variant of "pad this string to the right" later, but it does mean we have to maintain %(padright) in this limited form forever.
My knee-jerk reaction is that we probably should not want to expose this to the end users, and to discourage its use, perhaps name it somewhat strangely (e.g. "%(x-padright:N)" or something).
Quoted 10 lines · Junio C Hamano
"Currently we do not need it to reimplement the canned 'tag -l' format" is an OK and sensible justification to stick to the current implementation of %(padright:N), but we'd need to think if we would want to keep this limited and strange form that applies to a single atom that comes next (ignoring any literal spans) as a private implementation detail between ref-filter and "git tag". Opening it up to end-users would not mean we cannot add a correctly operating variant of "pad this string to the right" later, but it does mean we have to maintain %(padright) in this limited form forever.
Quoted 3 lines
My knee-jerk reaction is that we probably should not want to expose this to the end users, and to discourage its use, perhaps name it somewhat strangely (e.g. "%(x-padright:N)" or something).
I disagree. The current %(padright) fits 99.9% needs. It's handy for the user if he wants a column-display with --format. It's consistant with the "git log" %<() atoms.
Sure, if the user wants really advanced formatting, it's not sufficient. But first I believe this is a case of YAGNI, "right-pad an arbitrary string" is a funny coding exercice, but not very useful in real-life. And then, if one really has a use-case for advanced formatting, I think a much better approach is to dump an easy-to-parse language (XML/JSON/CSV/...) and pipe it to a formatter written in a real programming language. It will always be more powerful than having to chose in a limited set of %(atoms).
Signature · 2 lines
Matthieu Moy http://www-verimag.imag.fr/~moy/
Quoted 20 lines · Matthieu Moy
Quoted 10 lines · Junio C Hamano
"Currently we do not need it to reimplement the canned 'tag -l' format" is an OK and sensible justification to stick to the current implementation of %(padright:N), but we'd need to think if we would want to keep this limited and strange form that applies to a single atom that comes next (ignoring any literal spans) as a private implementation detail between ref-filter and "git tag". Opening it up to end-users would not mean we cannot add a correctly operating variant of "pad this string to the right" later, but it does mean we have to maintain %(padright) in this limited form forever.
Quoted 3 lines
My knee-jerk reaction is that we probably should not want to expose this to the end users, and to discourage its use, perhaps name it somewhat strangely (e.g. "%(x-padright:N)" or something).
I disagree. The current %(padright) fits 99.9% needs. It's handy for the user if he wants a column-display with --format. It's consistant with the "git log" %<() atoms.
Quoted 9 lines
Sure, if the user wants really advanced formatting, it's not sufficient. But first I believe this is a case of YAGNI, "right-pad an arbitrary string" is a funny coding exercice, but not very useful in real-life. And then, if one really has a use-case for advanced formatting, I think a much better approach is to dump an easy-to-parse language (XML/JSON/CSV/...) and pipe it to a formatter written in a real programming language. It will always be more powerful than having to chose in a limited set of %(atoms).
Exactly! what I was thinking, I mean this is quite useful feature in itself, Agreed there are numerous end cases which are not satisfied, but those are quite minimal and rarely used.
Signature · 2 lines
Regards, Karthik Nayak
PATCH v7 1/11 ref-filter: introduce 'ref_formatting_state'
In reply to Karthik NayakIntroduce 'ref_formatting_state' structure to hold values of modifier atoms which help only in formatting. This will eventually be used by atoms like padright which will be introduced in a later patch.
--- ref-filter.c | 70 ++++++++++++++++++++++++++++++++++++++++++++---------------- ref-filter.h | 5 +++++ 2 files changed, 57 insertions(+), 18 deletions(-)
ref-filter.c +52 -18
@@ -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 ;
@@ -1190,30 +1192,41 @@ 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 apply_formatting_state (struct ref_formatting_state *state, +const char *buf,struct strbuf *value) { -struct strbuf sb =STRBUF_INIT ; -switch (quote_style) { +/* Eventually we'll format based on the ref_formatting_state */ +strbuf_addstr (value, buf); +} + +static void print_value (struct atom_value *v,struct ref_formatting_state *state) +{ +struct strbuf value =STRBUF_INIT ; +struct strbuf formatted =STRBUF_INIT ; + +apply_formatting_state (state, v->s, &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 (&value); +strbuf_release (&formatted); }static int hex1 (char ch)
@@ -1234,8 +1247,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 ] =='%' )
@@ -1249,27 +1266,44 @@ 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) +{ +/* Here the 'ref_formatting_state' variable will be filled */ }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; 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); -print_value (atomv, quote_style); +if (atomv->modifier_atom) +store_formatting_state (&state, atomv); +else +print_value (atomv, &state); }if (*cp) { sp = cp +strlen (cp); -emit (cp, sp); +emit (cp, sp, &state); }if (need_color_reset_at_eol) {struct atom_value resetv;
@@ -1278,7 +1312,7 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int quif (color_parse ("reset" , color) <0 )die ("BUG: couldn't parse 'reset' as a color" ); resetv.s = color; -print_value (&resetv, quote_style); +print_value (&resetv, &state); }putchar ('\n' ); }
ref-filter.h +5 -0
@@ -19,6 +19,11 @@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 */ +}; + +struct ref_formatting_state { +int quote_style; };struct ref_sorting {
Signature · 1 line
2.4.6
PATCH v7 2/11 ref-filter: make `color` use `ref_formatting_state`
Convert the 'color' atom to behave as a modifier atom and make it use 'ref_formatting_state' which was introduced in the previous patch.
--- ref-filter.c | 10 ++++++++-- ref-filter.h | 4 +++- 2 files changed, 11 insertions(+), 3 deletions(-)
ref-filter.c +8 -2
@@ -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->color =1 ; + v->modifier_atom =1 ;continue ; }else if (!strcmp (name,"flag" )) {char buf[256 ], *cp = buf;
@@ -1195,7 +1197,10 @@ void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)static void apply_formatting_state (struct ref_formatting_state *state,const char *buf,struct strbuf *value) { -/* Eventually we'll format based on the ref_formatting_state */ +if (state->color) { +strbuf_addstr (value, state->color); + state->color =NULL ; + }strbuf_addstr (value, buf); }
@@ -1278,7 +1283,8 @@ static void emit(const char *cp, const char *ep,static void store_formatting_state (struct ref_formatting_state *state,struct atom_value *atomv) { -/* Here the 'ref_formatting_state' variable will be filled */ +if (atomv->color) + state->color = atomv->s; }void show_ref_array_item (struct ref_array_item *info,const char *format,int quote_style)
ref-filter.h +3 -1
@@ -19,11 +19,13 @@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 */ +unsigned int modifier_atom :1 ,/* atoms which act as modifiers for the next atom */ + color :1 ; };struct ref_formatting_state {int quote_style; +const char *color; };struct ref_sorting {
Signature · 1 line
2.4.6
PATCH v7 3/11 ref-filter: add option to pad atoms to the right
In reply to Karthik NayakAdd a new atom "padright" and support %(padright:X) where X is a number. This will align the succeeding atom or string to the left followed by spaces for a total length of X characters. If X is less than the atom or string length then no padding is done.
Add tests and documentation for the same.
--- Documentation/git-for-each-ref.txt | 6 ++++++ ref-filter.c | 26 ++++++++++++++++++++++++++ ref-filter.h | 4 +++- t/t6302-for-each-ref-filter.sh | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-)
Documentation/git-for-each-ref.txt +6 -0
@@ -127,6 +127,12 @@ color:: Change output color. Followed by `:<colorname>`, where names are described in `color.branch.*`. +padright:: + 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 be used to specify the value in the header field.
ref-filter.c +26 -0
@@ -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->modifier_atom =1 ; + v->pad_to_right =1 ; +continue ; }else continue ;
@@ -1201,6 +1214,17 @@ static void apply_formatting_state(struct ref_formatting_state *state,strbuf_addstr (value, state->color); state->color =NULL ; } +if (state->pad_to_right) { +if (!is_utf8 (buf)) +strbuf_addf (value,"%-*s" , state->pad_to_right, buf); +else { +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, buf); }
@@ -1285,6 +1309,8 @@ static void store_formatting_state(struct ref_formatting_state *state, {if (atomv->color) state->color = atomv->s; +if (atomv->pad_to_right) + state->pad_to_right = atomv->ul; }void show_ref_array_item (struct ref_array_item *info,const char *format,int quote_style)
ref-filter.h +3 -1
@@ -20,11 +20,13 @@ 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; + color : 1, + pad_to_right : 1; };struct ref_formatting_state {int quote_style; +unsigned int pad_to_right;const char *color; };
t/t6302-for-each-ref-filter.sh +32 -0
@@ -81,4 +81,36 @@ test_expect_success 'filtering with --contains' ' test_cmp expect actual ' +test_expect_success 'padding to the right using `padright`' ' + cat >expect <<-\EOF && + 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: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 +' + test_done
Signature · 1 line
2.4.6
PATCH v7 4/11 ref-filter: add option to filter only tags
In reply to Karthik NayakFrom: Karthik Nayak <[email protected]>
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'
--- ref-filter.c | 2 ++ ref-filter.h | 1 + refs.c | 5 +++++ refs.h | 1 + 4 files changed, 9 insertions(+)
ref-filter.c +2 -0
@@ -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");
ref-filter.h +1 -0
@@ -15,6 +15,7 @@#define FILTER_REFS_INCLUDE_BROKEN 0x1#define FILTER_REFS_ALL 0x2 +#define FILTER_REFS_TAGS 0x4struct atom_value {const char *s;
refs.c +5 -0
@@ -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);
refs.h +1 -0
@@ -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);
Signature · 1 line
2.4.6
PATCH v7 5/11 ref-filter: support printing N lines from tag annotation
In reply to Karthik NayakFrom: Karthik Nayak <[email protected]>
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.
--- 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(-)
builtin/for-each-ref.c +1 -1
@@ -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 ; }
builtin/tag.c +4 -0
@@ -185,6 +185,10 @@ static enum contains_result contains(struct commit *candidate,return contains_test (candidate, want); } +/* +* Currently duplicated in ref-filter, will eventually be removed as +* we port tag.c to use ref-filter APIs. +*/ static void show_tag_lines (const struct object_id *oid,int lines) {int i;
ref-filter.c +50 -1
63 lines not on this page · raw message
ref-filter.h +7 -2
@@ -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 */
Signature · 1 line
2.4.6
PATCH v7 6/11 ref-filter: add support to sort by version
In reply to Karthik NayakFrom: Karthik Nayak <[email protected]>
Add support to sort by version using the "v:refname" and "version:refname" option. This is achieved by using the 'versioncmp()' function as the comparing function for qsort.
This option is included to support sorting by versions in git tag -l which will eventaully be ported to use ref-filter APIs.
Add documentation and tests for the same.
--- 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(-)
Documentation/git-for-each-ref.txt +3 -0
@@ -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 its alias `v:refname`. + In any case, a field name that refers to a field inapplicable to the object referred by the ref does not cause an error. It returns an empty string instead.
ref-filter.c +9 -5
@@ -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, struget_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; }
@@ -1429,6 +1430,9 @@ int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset) s->reverse =1 ; arg++; } +if (skip_prefix (arg,"version:" , &arg) || +skip_prefix (arg,"v:" , &arg)) + s->version =1 ; len =strlen (arg); s->atom =parse_ref_filter_atom (arg, arg+len);return 0 ;
ref-filter.h +2 -1
@@ -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 {
t/t6302-for-each-ref-filter.sh +36 -0
@@ -113,4 +113,40 @@ test_expect_success 'no padding when `padright` length is smaller than atom leng test_cmp expect actual ' +test_expect_success 'setup for version sort' ' + test_commit foo1.3 && + test_commit foo1.6 && + test_commit foo1.10 +' + +test_expect_success 'version sort' ' + git for-each-ref --sort=version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual && + cat >expect <<-\EOF && + foo1.3 + foo1.6 + foo1.10 + EOF + test_cmp expect actual +' + +test_expect_success 'version sort (shortened)' ' + git for-each-ref --sort=v:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual && + cat >expect <<-\EOF && + foo1.3 + foo1.6 + foo1.10 + EOF + test_cmp expect actual +' + +test_expect_success 'reverse version sort' ' + git for-each-ref --sort=-version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual && + cat >expect <<-\EOF && + foo1.10 + foo1.6 + foo1.3 + EOF + test_cmp expect actual +' + test_done
Signature · 1 line
2.4.6
PATCH v7 7/11 ref-filter: add option to match literal pattern
In reply to Karthik NayakFrom: Karthik Nayak <[email protected]>
Since 'ref-filter' only has an option to match path names add an option for plain fnmatch pattern-matching.
This is to support the pattern matching options which are used in git tag -l and git branch -l where we can match patterns like git tag -l foo* which would match all tags which has a "foo*" pattern.
--- builtin/for-each-ref.c | 1 + ref-filter.c | 39 ++++++++++++++++++++++++++++++++++++--- ref-filter.h | 3 ++- 3 files changed, 39 insertions(+), 4 deletions(-)
builtin/for-each-ref.c +1 -0
@@ -68,6 +68,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)git_config (git_default_config,NULL ); filter.name_patterns = argv; + filter.match_as_path =1 ;filter_refs (&array, &filter,FILTER_REFS_ALL |FILTER_REFS_INCLUDE_BROKEN );ref_array_sort (sorting, &array);
ref-filter.c +36 -3
@@ -946,9 +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. +*/ + (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 )) +return 1 ; + } +return 0 ; +} + +/* +* Return 1 if the refname matches one of the patterns, otherwise 0. * A pattern can be path prefix (e.g. a refname "refs/heads/master" -* matches a pattern "refs/heads/") or a wildcard (e.g. the same ref -* matches "refs/heads/m*",too). +* matches a pattern "refs/heads/" but not "refs/heads/m") or a +* wildcard (e.g. the same ref matches "refs/heads/m*", too). */ static int match_name_as_path (const char **pattern,const char *refname) {
@@ -969,6 +992,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 +1070,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))
ref-filter.h +2 -1
@@ -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; };
Signature · 1 line
2.4.6
PATCH v7 8/11 tag.c: use 'ref-filter' data structures
In reply to Karthik NayakFrom: Karthik Nayak <[email protected]>
Make 'tag.c' use 'ref-filter' data structures and make changes to support the new data structures. This is a part of the process of porting 'tag.c' to use 'ref-filter' APIs.
This is a temporary step before porting 'tag.c' to use 'ref-filter' completely. As this is a temporary step, most of the code introduced here will be removed when 'tag.c' is ported over to use 'ref-filter' APIs
--- builtin/tag.c | 106 +++++++++++++++++++++++++++++++--------------------------- 1 file changed, 57 insertions(+), 49 deletions(-)
builtin/tag.c +57 -49
@@ -17,6 +17,7 @@#include "gpg-interface.h" #include "sha1-array.h" #include "column.h" +#include "ref-filter.h" static const char *const git_tag_usage[] = {N_ ("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>]" ),
@@ -34,15 +35,6 @@ static const char * const git_tag_usage[] = {static int tag_sort; -struct tag_filter { -const char **patterns; -int lines; -int sort; -struct string_list tags; -struct commit_list *with_commit; -}; - -static struct sha1_array points_at;static unsigned int colopts;static int match_pattern (const char **patterns,const char *ref)
@@ -61,19 +53,20 @@ static int match_pattern(const char **patterns, const char *ref)* removed as we port tag.c to use the ref-filter APIs. */ static const unsigned char *match_points_at (const char *refname, -const unsigned char *sha1) +const unsigned char *sha1, +struct sha1_array *points_at) {const unsigned char *tagged_sha1 =NULL ;struct object *obj; -if (sha1_array_lookup (&points_at, sha1) >=0 ) +if (sha1_array_lookup (points_at, sha1) >=0 )return sha1; obj =parse_object (sha1);if (!obj)die (_ ("malformed object at '%s'" ), refname);if (obj->type ==OBJ_TAG ) tagged_sha1 = ((struct tag *)obj)->tagged->sha1; -if (tagged_sha1 &&sha1_array_lookup (&points_at, tagged_sha1) >=0 ) +if (tagged_sha1 &&sha1_array_lookup (points_at, tagged_sha1) >=0 )return tagged_sha1;return NULL ; }
@@ -228,12 +221,24 @@ free_return:free (buf); } +static void ref_array_append (struct ref_array *array,const char *refname) +{ +size_t len =strlen (refname); +struct ref_array_item *ref =xcalloc (1 ,sizeof (struct ref_array_item ) + len +1 ); +memcpy (ref->refname, refname, len); + ref->refname[len] ='\0' ; +REALLOC_ARRAY (array->items, array->nr +1 ); + array->items[array->nr++] = ref; +} +static int show_reference (const char *refname,const struct object_id *oid,int flag,void *cb_data) { -struct tag_filter *filter = cb_data; +struct ref_filter_cbdata *data = cb_data; +struct ref_array *array = data->array; +struct ref_filter *filter = data->filter; -if (match_pattern (filter->patterns, refname)) { +if (match_pattern (filter->name_patterns, refname)) {if (filter->with_commit) {struct commit *commit;
@@ -244,12 +249,12 @@ static int show_reference(const char *refname, const struct object_id *oid,return 0 ; } -if (points_at.nr && !match_points_at (refname, oid->hash)) +if (filter->points_at.nr && !match_points_at (refname, oid->hash, &filter->points_at))return 0 ;if (!filter->lines) { -if (filter->sort) -string_list_append (&filter->tags, refname); +if (tag_sort) +ref_array_append (array, refname);else printf ("%s\n" , refname);return 0 ;
@@ -264,36 +269,36 @@ static int show_reference(const char *refname, const struct object_id *oid,static int sort_by_version (const void *a_,const void *b_) { -const struct string_list_item *a = a_; -const struct string_list_item *b = b_; -return versioncmp (a->string, b->string); +const struct ref_array_item *a = *((struct ref_array_item **)a_); +const struct ref_array_item *b = *((struct ref_array_item **)b_); +return versioncmp (a->refname, b->refname); } -static int list_tags (const char **patterns,int lines, -struct commit_list *with_commit,int sort) +static int list_tags (struct ref_filter *filter,int sort) { -struct tag_filter filter; +struct ref_array array; +struct ref_filter_cbdata data; + +memset (&array,0 ,sizeof (array)); + data.array = &array; + data.filter = filter; - filter.patterns = patterns; - filter.lines = lines; - filter.sort = sort; - filter.with_commit = with_commit; -memset (&filter.tags,0 ,sizeof (filter.tags)); - filter.tags.strdup_strings =1 ; +if (filter->lines ==-1 ) + filter->lines =0 ; -for_each_tag_ref (show_reference, (void *)&filter); +for_each_tag_ref (show_reference, &data);if (sort) {int i;if ((sort &SORT_MASK ) ==VERCMP_SORT ) -qsort (filter.tags.items, filter.tags.nr, -sizeof (struct string_list_item ), sort_by_version); +qsort (array.items, array.nr, +sizeof (struct ref_array_item *), sort_by_version);if (sort &REVERSE_SORT ) -for (i = filter.tags.nr -1 ; i >=0 ; i--) -printf ("%s\n" , filter.tags.items[i].string); +for (i = array.nr -1 ; i >=0 ; i--) +printf ("%s\n" , array.items[i]->refname);else -for (i =0 ; i < filter.tags.nr; i++) -printf ("%s\n" , filter.tags.items[i].string); -string_list_clear (&filter.tags,0 ); +for (i =0 ; i < array.nr; i++) +printf ("%s\n" , array.items[i]->refname); +ref_array_clear (&array); }return 0 ; }
@@ -574,17 +579,17 @@ int cmd_tag(int argc, const char **argv, const char *prefix)const char *object_ref, *tag;struct create_tag_options opt;char *cleanup_arg =NULL ; -int annotate =0 , force =0 , lines =-1 ;int create_reflog =0 ; +int annotate =0 , force =0 ;int cmdmode =0 ;const char *msgfile =NULL , *keyid =NULL ;struct msg_arg msg = {0 ,STRBUF_INIT }; -struct commit_list *with_commit =NULL ;struct ref_transaction *transaction;struct strbuf err =STRBUF_INIT ; +struct ref_filter filter;struct option options[] = {OPT_CMDMODE ('l' ,"list" , &cmdmode,N_ ("list tag names" ),'l' ), - {OPTION_INTEGER ,'n' ,NULL , &lines,N_ ("n" ), + {OPTION_INTEGER ,'n' ,NULL , &filter.lines,N_ ("n" ),N_ ("print <n> lines of each tag message" ),PARSE_OPT_OPTARG ,NULL ,1 },OPT_CMDMODE ('d' ,"delete" , &cmdmode,N_ ("delete tags" ),'d' ),
@@ -606,14 +611,14 @@ int cmd_tag(int argc, const char **argv, const char *prefix)OPT_GROUP (N_ ("Tag listing options" )),OPT_COLUMN (0 ,"column" , &colopts,N_ ("show tag list in columns" )), -OPT_CONTAINS (&with_commit,N_ ("print only tags that contain the commit" )), -OPT_WITH (&with_commit,N_ ("print only tags that contain the commit" )), +OPT_CONTAINS (&filter.with_commit,N_ ("print only tags that contain the commit" )), +OPT_WITH (&filter.with_commit,N_ ("print only tags that contain the commit" )), {OPTION_CALLBACK ,0 ,"sort" , &tag_sort,N_ ("type" ),N_ ("sort tags" ),PARSE_OPT_NONEG , parse_opt_sort }, { -OPTION_CALLBACK ,0 ,"points-at" , &points_at,N_ ("object" ), +OPTION_CALLBACK ,0 ,"points-at" , &filter.points_at,N_ ("object" ),N_ ("print only tags of the object" ),0 , parse_opt_object_name },OPT_END ()
@@ -622,6 +627,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)git_config (git_tag_config,NULL );memset (&opt,0 ,sizeof (opt)); +memset (&filter,0 ,sizeof (filter)); + filter.lines =-1 ; argc =parse_options (argc, argv, prefix, options, git_tag_usage,0 );
@@ -638,7 +645,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)usage_with_options (git_tag_usage, options);finalize_colopts (&colopts,-1 ); -if (cmdmode =='l' && lines !=-1 ) { +if (cmdmode =='l' && filter.lines !=-1 ) {if (explicitly_enable_column (colopts))die (_ ("--column and -n are incompatible" )); colopts =0 ;
@@ -651,18 +658,19 @@ int cmd_tag(int argc, const char **argv, const char *prefix) copts.padding =2 ;run_column_filter (colopts, &copts); } -if (lines !=-1 && tag_sort) +if (filter.lines !=-1 && tag_sort)die (_ ("--sort and -n are incompatible" )); - ret =list_tags (argv, lines ==-1 ?0 : lines, with_commit, tag_sort); + filter.name_patterns = argv; + ret =list_tags (&filter, tag_sort);if (column_active (colopts))stop_column_filter ();return ret; } -if (lines !=-1 ) +if (filter.lines !=-1 )die (_ ("-n option is only allowed with -l." )); -if (with_commit) +if (filter.with_commit)die (_ ("--contains option is only allowed with -l." )); -if (points_at.nr) +if (filter.points_at.nr)die (_ ("--points-at option is only allowed with -l." ));if (cmdmode =='d' )return for_each_tag_name (argv, delete_tag);
Signature · 1 line
2.4.6
PATCH v7 9/11 tag.c: use 'ref-filter' APIs
In reply to Karthik NayakFrom: Karthik Nayak <[email protected]>
Make 'tag.c' use 'ref-filter' APIs for iterating through refs, sorting and printing of refs. This removes most of the code used in 'tag.c' replacing it with calls to the 'ref-filter' library.
Make 'tag.c' use the 'filter_refs()' function provided by 'ref-filter' to filter out tags based on the options set.
For printing tags we use 'show_ref_array_item()' function provided by 'ref-filter'.
We improve the sorting option provided by 'tag.c' by using the sorting options provided by 'ref-filter'. This causes the test 'invalid sort parameter on command line' in t7004 to fail, as 'ref-filter' throws an error for all sorting fields which are incorrect. The test is changed to reflect the same.
Modify documentation for the same.
--- Documentation/git-tag.txt | 16 ++- builtin/tag.c | 342 ++++++---------------------------------------- t/t7004-tag.sh | 8 +- 3 files changed, 50 insertions(+), 316 deletions(-)
Documentation/git-tag.txt +9 -7
@@ -13,7 +13,7 @@ SYNOPSIS <tagname> [<commit> | <object>] 'git tag' -d <tagname>... 'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>] - [--column[=<options>] | --no-column] [--create-reflog] [<pattern>...] + [--column[=<options>] | --no-column] [--create-reflog] [--sort=<key>] [<pattern>...] 'git tag' -v <tagname>... DESCRIPTION
@@ -94,14 +94,16 @@ OPTIONS using fnmatch(3)). Multiple patterns may be given; if any of them matches, the tag is shown. ---sort=<type>:: - Sort in a specific order. Supported type is "refname" - (lexicographic order), "version:refname" or "v:refname" (tag +--sort=<key>:: + Sort based on the key given. Prefix `-` to sort in + descending order of the value. You may use the --sort=<key> option + multiple times, in which case the last key becomes the primary + key. Also supports "version:refname" or "v:refname" (tag names are treated as versions). The "version:refname" sort order can also be affected by the - "versionsort.prereleaseSuffix" configuration variable. Prepend - "-" to reverse sort order. When this option is not given, the - sort order defaults to the value configured for the 'tag.sort' + "versionsort.prereleaseSuffix" configuration variable. + The keys supported are the same as those in `git for-each-ref`. + Sort order defaults to the value configured for the 'tag.sort' variable if it exists, or lexicographic order otherwise. See linkgit:git-config[1].
builtin/tag.c +40 -302
416 lines not on this page · raw message
t/t7004-tag.sh +1 -7
@@ -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' '
Signature · 1 line
2.4.6
PATCH v7 10/11 tag.c: implement '--format' option
In reply to Karthik NayakFrom: Karthik Nayak <[email protected]>
Implement the '--format' option provided by 'ref-filter'. This lets the user list tags as per desired format similar to the implementation in 'git for-each-ref'.
Add tests and documentation for the same.
--- Documentation/git-tag.txt | 15 ++++++++++++++- builtin/tag.c | 11 +++++++---- t/t7004-tag.sh | 16 ++++++++++++++++ 3 files changed, 37 insertions(+), 5 deletions(-)
Documentation/git-tag.txt +14 -1
@@ -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 -------------
builtin/tag.c +7 -4
@@ -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;
t/t7004-tag.sh +16 -0
@@ -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
Signature · 1 line
2.4.6
PATCH v7 11/11 tag.c: implement '--merged' and '--no-merged' options
In reply to Karthik NayakFrom: Karthik Nayak <[email protected]>
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.
--- Documentation/git-tag.txt | 7 ++++++- builtin/tag.c | 6 +++++- t/t7004-tag.sh | 27 +++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-)
Documentation/git-tag.txt +6 -1
@@ -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 -------------
builtin/tag.c +5 -1
@@ -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' )
t/t7004-tag.sh +27 -0
@@ -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
Signature · 1 line
2.4.6