Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit c3d2bc7

Browse files
committed
Merge branch 'jk/tag-sort'
* jk/tag-sort: tag: support configuring --sort via .gitconfig tag: fix --sort tests to use cat<<-\EOF format
2 parents 247b4d5 + b150794 commit c3d2bc7

File tree

4 files changed

+115
-39
lines changed

4 files changed

+115
-39
lines changed

Documentation/config.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2354,6 +2354,11 @@ submodule.<name>.ignore::
23542354
"--ignore-submodules" option. The 'git submodule' commands are not
23552355
affected by this setting.
23562356

2357+
tag.sort::
2358+
This variable controls the sort ordering of tags when displayed by
2359+
linkgit:git-tag[1]. Without the "--sort=<value>" option provided, the
2360+
value of this variable will be used as the default.
2361+
23572362
tar.umask::
23582363
This variable can be used to restrict the permission bits of
23592364
tar archive entries. The default is 0002, which turns off the

Documentation/git-tag.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ OPTIONS
9999
Sort in a specific order. Supported type is "refname"
100100
(lexicographic order), "version:refname" or "v:refname" (tag
101101
names are treated as versions). Prepend "-" to reverse sort
102-
order.
102+
order. When this option is not given, the sort order defaults to the
103+
value configured for the 'tag.sort' variable if it exists, or
104+
lexicographic order otherwise. See linkgit:git-config[1].
103105

104106
--column[=<options>]::
105107
--no-column::
@@ -317,6 +319,7 @@ include::date-formats.txt[]
317319
SEE ALSO
318320
--------
319321
linkgit:git-check-ref-format[1].
322+
linkgit:git-config[1].
320323

321324
GIT
322325
---

builtin/tag.c

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ static const char * const git_tag_usage[] = {
3232
#define SORT_MASK 0x7fff
3333
#define REVERSE_SORT 0x8000
3434

35+
static int tag_sort;
36+
3537
struct tag_filter {
3638
const char **patterns;
3739
int lines;
@@ -346,9 +348,51 @@ static const char tag_template_nocleanup[] =
346348
"Lines starting with '%c' will be kept; you may remove them"
347349
" yourself if you want to.\n");
348350

351+
/*
352+
* Parse a sort string, and return 0 if parsed successfully. Will return
353+
* non-zero when the sort string does not parse into a known type. If var is
354+
* given, the error message becomes a warning and includes information about
355+
* the configuration value.
356+
*/
357+
static int parse_sort_string(const char *var, const char *arg, int *sort)
358+
{
359+
int type = 0, flags = 0;
360+
361+
if (skip_prefix(arg, "-", &arg))
362+
flags |= REVERSE_SORT;
363+
364+
if (skip_prefix(arg, "version:", &arg) || skip_prefix(arg, "v:", &arg))
365+
type = VERCMP_SORT;
366+
else
367+
type = STRCMP_SORT;
368+
369+
if (strcmp(arg, "refname")) {
370+
if (!var)
371+
return error(_("unsupported sort specification '%s'"), arg);
372+
else {
373+
warning(_("unsupported sort specification '%s' in variable '%s'"),
374+
var, arg);
375+
return -1;
376+
}
377+
}
378+
379+
*sort = (type | flags);
380+
381+
return 0;
382+
}
383+
349384
static int git_tag_config(const char *var, const char *value, void *cb)
350385
{
351-
int status = git_gpg_config(var, value, cb);
386+
int status;
387+
388+
if (!strcmp(var, "tag.sort")) {
389+
if (!value)
390+
return config_error_nonbool(var);
391+
parse_sort_string(var, value, &tag_sort);
392+
return 0;
393+
}
394+
395+
status = git_gpg_config(var, value, cb);
352396
if (status)
353397
return status;
354398
if (starts_with(var, "column."))
@@ -522,20 +566,8 @@ static int parse_opt_points_at(const struct option *opt __attribute__((unused)),
522566
static int parse_opt_sort(const struct option *opt, const char *arg, int unset)
523567
{
524568
int *sort = opt->value;
525-
int flags = 0;
526569

527-
if (skip_prefix(arg, "-", &arg))
528-
flags |= REVERSE_SORT;
529-
530-
if (skip_prefix(arg, "version:", &arg) || skip_prefix(arg, "v:", &arg))
531-
*sort = VERCMP_SORT;
532-
else
533-
*sort = STRCMP_SORT;
534-
535-
if (strcmp(arg, "refname"))
536-
die(_("unsupported sort specification %s"), arg);
537-
*sort |= flags;
538-
return 0;
570+
return parse_sort_string(NULL, arg, sort);
539571
}
540572

541573
int cmd_tag(int argc, const char **argv, const char *prefix)
@@ -548,7 +580,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
548580
struct create_tag_options opt;
549581
char *cleanup_arg = NULL;
550582
int annotate = 0, force = 0, lines = -1;
551-
int cmdmode = 0, sort = 0;
583+
int cmdmode = 0;
552584
const char *msgfile = NULL, *keyid = NULL;
553585
struct msg_arg msg = { 0, STRBUF_INIT };
554586
struct commit_list *with_commit = NULL;
@@ -574,7 +606,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
574606
OPT__FORCE(&force, N_("replace the tag if exists")),
575607
OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
576608
{
577-
OPTION_CALLBACK, 0, "sort", &sort, N_("type"), N_("sort tags"),
609+
OPTION_CALLBACK, 0, "sort", &tag_sort, N_("type"), N_("sort tags"),
578610
PARSE_OPT_NONEG, parse_opt_sort
579611
},
580612

@@ -630,9 +662,9 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
630662
copts.padding = 2;
631663
run_column_filter(colopts, &copts);
632664
}
633-
if (lines != -1 && sort)
665+
if (lines != -1 && tag_sort)
634666
die(_("--sort and -n are incompatible"));
635-
ret = list_tags(argv, lines == -1 ? 0 : lines, with_commit, sort);
667+
ret = list_tags(argv, lines == -1 ? 0 : lines, with_commit, tag_sort);
636668
if (column_active(colopts))
637669
stop_column_filter();
638670
return ret;

t/t7004-tag.sh

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,41 +1385,77 @@ test_expect_success 'lexical sort' '
13851385
git tag foo1.6 &&
13861386
git tag foo1.10 &&
13871387
git tag -l --sort=refname "foo*" >actual &&
1388-
cat >expect <<EOF &&
1389-
foo1.10
1390-
foo1.3
1391-
foo1.6
1392-
EOF
1388+
cat >expect <<-\EOF &&
1389+
foo1.10
1390+
foo1.3
1391+
foo1.6
1392+
EOF
13931393
test_cmp expect actual
13941394
'
13951395
13961396
test_expect_success 'version sort' '
13971397
git tag -l --sort=version:refname "foo*" >actual &&
1398-
cat >expect <<EOF &&
1399-
foo1.3
1400-
foo1.6
1401-
foo1.10
1402-
EOF
1398+
cat >expect <<-\EOF &&
1399+
foo1.3
1400+
foo1.6
1401+
foo1.10
1402+
EOF
14031403
test_cmp expect actual
14041404
'
14051405
14061406
test_expect_success 'reverse version sort' '
14071407
git tag -l --sort=-version:refname "foo*" >actual &&
1408-
cat >expect <<EOF &&
1409-
foo1.10
1410-
foo1.6
1411-
foo1.3
1412-
EOF
1408+
cat >expect <<-\EOF &&
1409+
foo1.10
1410+
foo1.6
1411+
foo1.3
1412+
EOF
14131413
test_cmp expect actual
14141414
'
14151415
14161416
test_expect_success 'reverse lexical sort' '
14171417
git tag -l --sort=-refname "foo*" >actual &&
1418-
cat >expect <<EOF &&
1419-
foo1.6
1420-
foo1.3
1421-
foo1.10
1422-
EOF
1418+
cat >expect <<-\EOF &&
1419+
foo1.6
1420+
foo1.3
1421+
foo1.10
1422+
EOF
1423+
test_cmp expect actual
1424+
'
1425+
1426+
test_expect_success 'configured lexical sort' '
1427+
git config tag.sort "v:refname" &&
1428+
git tag -l "foo*" >actual &&
1429+
cat >expect <<-\EOF &&
1430+
foo1.3
1431+
foo1.6
1432+
foo1.10
1433+
EOF
1434+
test_cmp expect actual
1435+
'
1436+
1437+
test_expect_success 'option override configured sort' '
1438+
git tag -l --sort=-refname "foo*" >actual &&
1439+
cat >expect <<-\EOF &&
1440+
foo1.6
1441+
foo1.3
1442+
foo1.10
1443+
EOF
1444+
test_cmp expect actual
1445+
'
1446+
1447+
test_expect_success 'invalid sort parameter on command line' '
1448+
test_must_fail git tag -l --sort=notvalid "foo*" >actual
1449+
'
1450+
1451+
test_expect_success 'invalid sort parameter in configuratoin' '
1452+
git config tag.sort "v:notvalid" &&
1453+
git tag -l "foo*" >actual &&
1454+
cat >expect <<-\EOF &&
1455+
foo1.10
1456+
foo1.3
1457+
foo1.6
1458+
EOF
14231459
test_cmp expect actual
14241460
'
14251461

0 commit comments

Comments
 (0)