@@ -32,6 +32,8 @@ static const char * const git_tag_usage[] = {
32
32
#define SORT_MASK 0x7fff
33
33
#define REVERSE_SORT 0x8000
34
34
35
+ static int tag_sort ;
36
+
35
37
struct tag_filter {
36
38
const char * * patterns ;
37
39
int lines ;
@@ -346,9 +348,51 @@ static const char tag_template_nocleanup[] =
346
348
"Lines starting with '%c' will be kept; you may remove them"
347
349
" yourself if you want to.\n" );
348
350
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
+
349
384
static int git_tag_config (const char * var , const char * value , void * cb )
350
385
{
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 );
352
396
if (status )
353
397
return status ;
354
398
if (starts_with (var , "column." ))
@@ -522,20 +566,8 @@ static int parse_opt_points_at(const struct option *opt __attribute__((unused)),
522
566
static int parse_opt_sort (const struct option * opt , const char * arg , int unset )
523
567
{
524
568
int * sort = opt -> value ;
525
- int flags = 0 ;
526
569
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 );
539
571
}
540
572
541
573
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)
548
580
struct create_tag_options opt ;
549
581
char * cleanup_arg = NULL ;
550
582
int annotate = 0 , force = 0 , lines = -1 ;
551
- int cmdmode = 0 , sort = 0 ;
583
+ int cmdmode = 0 ;
552
584
const char * msgfile = NULL , * keyid = NULL ;
553
585
struct msg_arg msg = { 0 , STRBUF_INIT };
554
586
struct commit_list * with_commit = NULL ;
@@ -574,7 +606,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
574
606
OPT__FORCE (& force , N_ ("replace the tag if exists" )),
575
607
OPT_COLUMN (0 , "column" , & colopts , N_ ("show tag list in columns" )),
576
608
{
577
- OPTION_CALLBACK , 0 , "sort" , & sort , N_ ("type" ), N_ ("sort tags" ),
609
+ OPTION_CALLBACK , 0 , "sort" , & tag_sort , N_ ("type" ), N_ ("sort tags" ),
578
610
PARSE_OPT_NONEG , parse_opt_sort
579
611
},
580
612
@@ -630,9 +662,9 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
630
662
copts .padding = 2 ;
631
663
run_column_filter (colopts , & copts );
632
664
}
633
- if (lines != -1 && sort )
665
+ if (lines != -1 && tag_sort )
634
666
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 );
636
668
if (column_active (colopts ))
637
669
stop_column_filter ();
638
670
return ret ;
0 commit comments