ref-filter.c: sort formatted dates by byte value

Update the ref sorting functions of 'ref-filter.c' so that when date fields are specified with a format string (such as in 'git for-each-ref --sort=creatordate:<something>'), they are sorted by their formatted string value rather than by the underlying numeric timestamp. Currently, date fields are always sorted by timestamp, regardless of whether formatting information is included in the '--sort' key. Leaving the default (unformatted) date sorting unchanged, sorting by the formatted date string adds some flexibility to 'for-each-ref' by allowing for behavior like "sort by year, then by refname within each year" or "sort by time of day". Because the inclusion of a format string previously had no effect on sort behavior, this change likely will not affect existing usage of 'for-each-ref' or other ref listing commands. Additionally, update documentation & tests to document the new sorting mechanism. Signed-off-by: Victoria Dye <vdye@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Victoria Dye committed Feb 8, 2024 at 01:57 UTC 46176d77c9e0b219a78d64cb99d8dbf4d9bcde2d
3 files changed +57 -3
Documentation/git-for-each-ref.txt
+5 -3
@@ -359,9 +359,11 @@ In any case, a field name that refers to a field inapplicable to
359 the object referred by the ref does not cause an error. It
360 returns an empty string instead.
361
362 -As a special case for the date-type fields, you may specify a format for
363 -the date by adding `:` followed by date format name (see the
364 -values the `--date` option to linkgit:git-rev-list[1] takes).
362 +As a special case for the date-type fields, you may specify a format for the
363 +date by adding `:` followed by date format name (see the values the `--date`
364 +option to linkgit:git-rev-list[1] takes). If this formatting is provided in
365 +a `--sort` key, references will be sorted according to the byte-value of the
366 +formatted string rather than the numeric value of the underlying timestamp.
367
368 Some atoms like %(align) and %(if) always require a matching %(end).
369 We call them "opening atoms" and sometimes denote them as %($open).
ref-filter.c
+6
@@ -1611,6 +1611,12 @@ static void grab_date(const char *buf, struct atom_value *v, const char *atomnam
1611 if (formatp) {
1612 formatp++;
1613 parse_date_format(formatp, &date_mode);
1614 +
1615 + /*
1616 + * If this is a sort field and a format was specified, we'll
1617 + * want to compare formatted date by string value.
1618 + */
1619 + v->atom->type = FIELD_STR;
1620 }
1621
1622 if (!eoemail)
t/t6300-for-each-ref.sh
+46
@@ -1356,6 +1356,52 @@ test_expect_success '--no-sort without subsequent --sort prints expected refs' '
1356 test_cmp expected actual
1357 '
1358
1359 +test_expect_success 'set up custom date sorting' '
1360 + # Dates:
1361 + # - Wed Feb 07 2024 21:34:20 +0000
1362 + # - Tue Dec 14 1999 00:05:22 +0000
1363 + # - Fri Jun 04 2021 11:26:51 +0000
1364 + # - Mon Jan 22 2007 16:44:01 GMT+0000
1365 + i=1 &&
1366 + for when in 1707341660 945129922 1622806011 1169484241
1367 + do
1368 + GIT_COMMITTER_DATE="@$when +0000" \
1369 + GIT_COMMITTER_EMAIL="user@example.com" \
1370 + git tag -m "tag $when" custom-dates-$i &&
1371 + i=$(($i+1)) || return 1
1372 + done
1373 +'
1374 +
1375 +test_expect_success 'sort by date defaults to full timestamp' '
1376 + cat >expected <<-\EOF &&
1377 + 945129922 refs/tags/custom-dates-2
1378 + 1169484241 refs/tags/custom-dates-4
1379 + 1622806011 refs/tags/custom-dates-3
1380 + 1707341660 refs/tags/custom-dates-1
1381 + EOF
1382 +
1383 + git for-each-ref \
1384 + --format="%(creatordate:unix) %(refname)" \
1385 + --sort=creatordate \
1386 + "refs/tags/custom-dates-*" >actual &&
1387 + test_cmp expected actual
1388 +'
1389 +
1390 +test_expect_success 'sort by custom date format' '
1391 + cat >expected <<-\EOF &&
1392 + 00:05:22 refs/tags/custom-dates-2
1393 + 11:26:51 refs/tags/custom-dates-3
1394 + 16:44:01 refs/tags/custom-dates-4
1395 + 21:34:20 refs/tags/custom-dates-1
1396 + EOF
1397 +
1398 + git for-each-ref \
1399 + --format="%(creatordate:format:%H:%M:%S) %(refname)" \
1400 + --sort="creatordate:format:%H:%M:%S" \
1401 + "refs/tags/custom-dates-*" >actual &&
1402 + test_cmp expected actual
1403 +'
1404 +
1405 test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
1406 test_when_finished "git checkout main" &&
1407 git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&