Skip to content

Commit 90c0040

Browse files
KarthikNayakgitster
authored andcommitted
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>
1 parent 1bb38e5 commit 90c0040

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ For sorting purposes, fields with numeric values sort in numeric
157157
order (`objectsize`, `authordate`, `committerdate`, `taggerdate`).
158158
All other fields are used to sort in their byte-value order.
159159

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+
160163
In any case, a field name that refers to a field inapplicable to
161164
the object referred by the ref does not cause an error. It
162165
returns an empty string instead.

ref-filter.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "ref-filter.h"
1212
#include "revision.h"
1313
#include "utf8.h"
14+
#include "git-compat-util.h"
15+
#include "version.h"
1416

1517
typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
1618

@@ -1439,19 +1441,19 @@ static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, stru
14391441

14401442
get_ref_atom_value(a, s->atom, &va);
14411443
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)
14441447
cmp = strcmp(va->s, vb->s);
1445-
break;
1446-
default:
1448+
else {
14471449
if (va->ul < vb->ul)
14481450
cmp = -1;
14491451
else if (va->ul == vb->ul)
14501452
cmp = 0;
14511453
else
14521454
cmp = 1;
1453-
break;
14541455
}
1456+
14551457
return (s->reverse) ? -cmp : cmp;
14561458
}
14571459

@@ -1584,6 +1586,9 @@ int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
15841586
s->reverse = 1;
15851587
arg++;
15861588
}
1589+
if (skip_prefix(arg, "version:", &arg) ||
1590+
skip_prefix(arg, "v:", &arg))
1591+
s->version = 1;
15871592
len = strlen(arg);
15881593
s->atom = parse_ref_filter_atom(arg, arg+len);
15891594
return 0;

ref-filter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ struct atom_value;
2828
struct ref_sorting {
2929
struct ref_sorting *next;
3030
int atom; /* index into used_atom array (internal) */
31-
unsigned reverse : 1;
31+
unsigned reverse : 1,
32+
version : 1;
3233
};
3334

3435
struct ref_array_item {

t/t6302-for-each-ref-filter.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,40 @@ test_expect_success '`%(contents:lines=-1)` should fail' '
219219
test_must_fail git for-each-ref --format="%(refname:short) |%(contents:lines=-1)"
220220
'
221221

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+
222258
test_done

0 commit comments

Comments
 (0)