Skip to content

Commit 6a4e744

Browse files
committed
Merge branch 'ks/ref-filter-sort-numerically'
"git for-each-ref --sort='contents:size'" sorts the refs according to size numerically, giving a ref that points at a blob twelve-byte (12) long before showing a blob hundred-byte (100) long. * ks/ref-filter-sort-numerically: ref-filter: sort numerically when ":size" is used
2 parents d4cab37 + 6d79cd8 commit 6a4e744

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

ref-filter.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,10 @@ static int contents_atom_parser(struct ref_format *format, struct used_atom *ato
583583
atom->u.contents.option = C_BARE;
584584
else if (!strcmp(arg, "body"))
585585
atom->u.contents.option = C_BODY;
586-
else if (!strcmp(arg, "size"))
586+
else if (!strcmp(arg, "size")) {
587+
atom->type = FIELD_ULONG;
587588
atom->u.contents.option = C_LENGTH;
588-
else if (!strcmp(arg, "signature"))
589+
} else if (!strcmp(arg, "signature"))
589590
atom->u.contents.option = C_SIG;
590591
else if (!strcmp(arg, "subject"))
591592
atom->u.contents.option = C_SUB;
@@ -691,9 +692,10 @@ static int raw_atom_parser(struct ref_format *format UNUSED,
691692
{
692693
if (!arg)
693694
atom->u.raw_data.option = RAW_BARE;
694-
else if (!strcmp(arg, "size"))
695+
else if (!strcmp(arg, "size")) {
696+
atom->type = FIELD_ULONG;
695697
atom->u.raw_data.option = RAW_LENGTH;
696-
else
698+
} else
697699
return err_bad_arg(err, "raw", arg);
698700
return 0;
699701
}
@@ -1859,7 +1861,8 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct exp
18591861
v->s = xmemdupz(buf, buf_size);
18601862
v->s_size = buf_size;
18611863
} else if (atom->u.raw_data.option == RAW_LENGTH) {
1862-
v->s = xstrfmt("%"PRIuMAX, (uintmax_t)buf_size);
1864+
v->value = buf_size;
1865+
v->s = xstrfmt("%"PRIuMAX, v->value);
18631866
}
18641867
continue;
18651868
}
@@ -1885,9 +1888,10 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct exp
18851888
v->s = strbuf_detach(&sb, NULL);
18861889
} else if (atom->u.contents.option == C_BODY_DEP)
18871890
v->s = xmemdupz(bodypos, bodylen);
1888-
else if (atom->u.contents.option == C_LENGTH)
1889-
v->s = xstrfmt("%"PRIuMAX, (uintmax_t)strlen(subpos));
1890-
else if (atom->u.contents.option == C_BODY)
1891+
else if (atom->u.contents.option == C_LENGTH) {
1892+
v->value = strlen(subpos);
1893+
v->s = xstrfmt("%"PRIuMAX, v->value);
1894+
} else if (atom->u.contents.option == C_BODY)
18911895
v->s = xmemdupz(bodypos, nonsiglen);
18921896
else if (atom->u.contents.option == C_SIG)
18931897
v->s = xmemdupz(sigpos, siglen);
@@ -2267,6 +2271,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
22672271

22682272
v->s_size = ATOM_SIZE_UNSPECIFIED;
22692273
v->handler = append_atom;
2274+
v->value = 0;
22702275
v->atom = atom;
22712276

22722277
if (*name == '*') {

t/t6300-for-each-ref.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,16 +1017,16 @@ test_expect_success 'Verify sorts with raw' '
10171017
test_expect_success 'Verify sorts with raw:size' '
10181018
cat >expected <<-EOF &&
10191019
refs/myblobs/blob8
1020-
refs/myblobs/first
10211020
refs/myblobs/blob7
1022-
refs/heads/main
10231021
refs/myblobs/blob4
10241022
refs/myblobs/blob1
10251023
refs/myblobs/blob2
10261024
refs/myblobs/blob3
10271025
refs/myblobs/blob5
10281026
refs/myblobs/blob6
1027+
refs/myblobs/first
10291028
refs/mytrees/first
1029+
refs/heads/main
10301030
EOF
10311031
git for-each-ref --format="%(refname)" --sort=raw:size \
10321032
refs/heads/main refs/myblobs/ refs/mytrees/first >actual &&
@@ -1138,6 +1138,17 @@ test_expect_success 'for-each-ref --format compare with cat-file --batch' '
11381138
test_cmp expected actual
11391139
'
11401140

1141+
test_expect_success 'verify sorts with contents:size' '
1142+
cat >expect <<-\EOF &&
1143+
refs/heads/main
1144+
refs/heads/newtag
1145+
refs/heads/ambiguous
1146+
EOF
1147+
git for-each-ref --format="%(refname)" \
1148+
--sort=contents:size refs/heads/ >actual &&
1149+
test_cmp expect actual
1150+
'
1151+
11411152
test_expect_success 'set up multiple-sort tags' '
11421153
for when in 100000 200000
11431154
do

0 commit comments

Comments
 (0)