@@ -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+
3537struct 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+
349384static 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)),
522566static 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
541573int 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 ;
0 commit comments