ref-filter: add support to sort by version

Add support to sort by version using the "v:refname" and "version:refname" option. This is achieved by using the 'versioncmp()' function as the comparing function for qsort. This option is included to support sorting by versions in `git tag -l` which will eventually be ported to use ref-filter APIs. Add documentation and tests for the same. Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Sep 10, 2015 at 21:18 UTC 90c004085cfe65e1b290e5b5fc05817ec2c596a6
4 files changed +51 -6
Documentation/git-for-each-ref.txt
+3
@@ -157,6 +157,9 @@ For sorting purposes, fields with numeric values sort in numeric
157 order (`objectsize`, `authordate`, `committerdate`, `taggerdate`).
158 All other fields are used to sort in their byte-value order.
159
160 +There is also an option to sort by versions, this can be done by using
161 +the fieldname `version:refname` or its alias `v:refname`.
162 +
163 In any case, a field name that refers to a field inapplicable to
164 the object referred by the ref does not cause an error. It
165 returns an empty string instead.
ref-filter.c
+10 -5
@@ -11,6 +11,8 @@
11 #include "ref-filter.h"
12 #include "revision.h"
13 #include "utf8.h"
14 +#include "git-compat-util.h"
15 +#include "version.h"
16
17 typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
18
@@ -1439,19 +1441,19 @@ static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, stru
1441
1442 get_ref_atom_value(a, s->atom, &va);
1443 get_ref_atom_value(b, s->atom, &vb);
1442 - switch (cmp_type) {
1443 - case FIELD_STR:
1444 + if (s->version)
1445 + cmp = versioncmp(va->s, vb->s);
1446 + else if (cmp_type == FIELD_STR)
1447 cmp = strcmp(va->s, vb->s);
1445 - break;
1446 - default:
1448 + else {
1449 if (va->ul < vb->ul)
1450 cmp = -1;
1451 else if (va->ul == vb->ul)
1452 cmp = 0;
1453 else
1454 cmp = 1;
1453 - break;
1455 }
1456 +
1457 return (s->reverse) ? -cmp : cmp;
1458 }
1459
@@ -1584,6 +1586,9 @@ int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
1586 s->reverse = 1;
1587 arg++;
1588 }
1589 + if (skip_prefix(arg, "version:", &arg) ||
1590 + skip_prefix(arg, "v:", &arg))
1591 + s->version = 1;
1592 len = strlen(arg);
1593 s->atom = parse_ref_filter_atom(arg, arg+len);
1594 return 0;
ref-filter.h
+2 -1
@@ -28,7 +28,8 @@ struct atom_value;
28 struct ref_sorting {
29 struct ref_sorting *next;
30 int atom; /* index into used_atom array (internal) */
31 - unsigned reverse : 1;
31 + unsigned reverse : 1,
32 + version : 1;
33 };
34
35 struct ref_array_item {
t/t6302-for-each-ref-filter.sh
+36
@@ -219,4 +219,40 @@ test_expect_success '`%(contents:lines=-1)` should fail' '
219 test_must_fail git for-each-ref --format="%(refname:short) |%(contents:lines=-1)"
220 '
221
222 +test_expect_success 'setup for version sort' '
223 + test_commit foo1.3 &&
224 + test_commit foo1.6 &&
225 + test_commit foo1.10
226 +'
227 +
228 +test_expect_success 'version sort' '
229 + git for-each-ref --sort=version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
230 + cat >expect <<-\EOF &&
231 + foo1.3
232 + foo1.6
233 + foo1.10
234 + EOF
235 + test_cmp expect actual
236 +'
237 +
238 +test_expect_success 'version sort (shortened)' '
239 + git for-each-ref --sort=v:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
240 + cat >expect <<-\EOF &&
241 + foo1.3
242 + foo1.6
243 + foo1.10
244 + EOF
245 + test_cmp expect actual
246 +'
247 +
248 +test_expect_success 'reverse version sort' '
249 + git for-each-ref --sort=-version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
250 + cat >expect <<-\EOF &&
251 + foo1.10
252 + foo1.6
253 + foo1.3
254 + EOF
255 + test_cmp expect actual
256 +'
257 +
258 test_done