# git tag -h fatal error with global tag.sort config

5 messages from 2021-09-12 to 2021-10-18. Participants: Bagas Sanjaya, SZEDER Gábor, Jeff King.
Thread: https://gitlist.dev/t/56488

## Bagas Sanjaya, 2021-09-12 11:28

Subject: git tag -h fatal error with global tag.sort config
Message-ID: <0daf6907-b472-a756-1240-4c78f8f4d37e@gmail.com>
URL: https://gitlist.dev/e/0daf6907-b472-a756-1240-4c78f8f4d37e%40gmail.com

```
Hi,

I stumbled on Debian bug report [1], and I can reproduce the bug.

In global config (~/.gitconfig), I set:

ˋˋˋ
[tag]
	sort = creatordate
ˋˋˋ

When I run `git tag -h` inside a Git repository (such as git.git), 
instead of usage summary, I got:

ˋˋˋ
fatal: not a git repository, but the field 'creatordate' requires access 
to object data
ˋˋˋ

But plain `git tag` works fine.

This bug occurs on v2.33.0 as well as version in the original bug report 
(v2.26.0-rc2) and v2.25.1.

[1]: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=954811

-- 
An old man doll... just what I always wanted! - Clara

```

## SZEDER Gábor, 2021-09-12 13:27

Subject: Re: git tag -h fatal error with global tag.sort config
Message-ID: <20210912132757.GC76263@szeder.dev>
URL: https://gitlist.dev/e/20210912132757.GC76263%40szeder.dev
In-Reply-To: <0daf6907-b472-a756-1240-4c78f8f4d37e@gmail.com>

```
On Sun, Sep 12, 2021 at 06:28:54PM +0700, Bagas Sanjaya wrote:
> I stumbled on Debian bug report [1], and I can reproduce the bug.
> 
> In global config (~/.gitconfig), I set:
> 
> ˋˋˋ
> [tag]
> 	sort = creatordate
> ˋˋˋ
> 
> When I run `git tag -h` inside a Git repository (such as git.git), instead
> of usage summary, I got:
> 
> ˋˋˋ
> fatal: not a git repository, but the field 'creatordate' requires access to
> object data
> ˋˋˋ
> 
> But plain `git tag` works fine.
> 
> This bug occurs on v2.33.0 as well as version in the original bug report
> (v2.26.0-rc2) and v2.25.1.
> 
> [1]: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=954811

Interesting.  It bisects to 47bd3d0c14 (ref-filter: don't look for
objects when outside of a repository, 2018-11-14), which, based on the
error message, kind of makes sense, because 'git tag' uses the general
ref-filter sorting facility.  Now, even if 'git tag -h' is executed in
a repository, since 99caeed05d (Let 'git <command> -h' show usage
without a git dir, 2009-11-09) run_builtin() special-cases the '-h'
option and does not call setup_git_directory(), so cmd_tag() and
everything invoked from within will mistakenly think that there is no
repository.  And cmd_tag() parses the config before parsing the
options (of course, otherwise command line options couldn't override
the config), so it hits this die() before parse_options would get a
change to act on the '-h' option.

Now, 'git branch' uses the same ref-filter sorting, but the equivalent
'git -c branch.sort=creatordate branch -h' command does show the usage
as expected.  The relevant difference between cmd_branch() and
cmd_tag() is that the former special-cases the '-h' option as well
just before it would call git_config().  Doing the same in cmd_tag()
like in the patch below seems to fix this issue, but I'm not sure that
this is the right fix.


  ---  >8  ---

diff --git a/builtin/tag.c b/builtin/tag.c
index 065b6bf093..31b8cc4600 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -485,6 +485,9 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 
 	setup_ref_filter_porcelain_msg();
 
+	if (argc == 2 && !strcmp(argv[1], "-h"))
+		usage_with_options(git_tag_usage, options);
+
 	git_config(git_tag_config, sorting_tail);
 
 	memset(&opt, 0, sizeof(opt));


```

## Jeff King, 2021-09-12 21:39

Subject: Re: git tag -h fatal error with global tag.sort config
Message-ID: <YT5zrWVlplgE/PWJ@coredump.intra.peff.net>
URL: https://gitlist.dev/e/YT5zrWVlplgE%2FPWJ%40coredump.intra.peff.net
In-Reply-To: <20210912132757.GC76263@szeder.dev>

```
On Sun, Sep 12, 2021 at 03:27:57PM +0200, SZEDER Gábor wrote:

> Interesting.  It bisects to 47bd3d0c14 (ref-filter: don't look for
> objects when outside of a repository, 2018-11-14), which, based on the
> error message, kind of makes sense, because 'git tag' uses the general
> ref-filter sorting facility.  Now, even if 'git tag -h' is executed in
> a repository, since 99caeed05d (Let 'git <command> -h' show usage
> without a git dir, 2009-11-09) run_builtin() special-cases the '-h'
> option and does not call setup_git_directory(), so cmd_tag() and
> everything invoked from within will mistakenly think that there is no
> repository.  And cmd_tag() parses the config before parsing the
> options (of course, otherwise command line options couldn't override
> the config), so it hits this die() before parse_options would get a
> change to act on the '-h' option.
> 
> Now, 'git branch' uses the same ref-filter sorting, but the equivalent
> 'git -c branch.sort=creatordate branch -h' command does show the usage
> as expected.  The relevant difference between cmd_branch() and
> cmd_tag() is that the former special-cases the '-h' option as well
> just before it would call git_config().  Doing the same in cmd_tag()
> like in the patch below seems to fix this issue, but I'm not sure that
> this is the right fix.
> 
> 
>   ---  >8  ---
> 
> diff --git a/builtin/tag.c b/builtin/tag.c
> index 065b6bf093..31b8cc4600 100644
> --- a/builtin/tag.c
> +++ b/builtin/tag.c
> @@ -485,6 +485,9 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
>  
>  	setup_ref_filter_porcelain_msg();
>  
> +	if (argc == 2 && !strcmp(argv[1], "-h"))
> +		usage_with_options(git_tag_usage, options);
> +
>  	git_config(git_tag_config, sorting_tail);
>  
>  	memset(&opt, 0, sizeof(opt));

I think part of the problem is that git_tag_config() is pretty eager to
parse the ref format. You can similarly see:

  $ git -c tag.sort=foobar tag -h
  fatal: unknown field name: foobar

If git_tag_config() just kept strings, and then we later fed them to
a parser (when we knew they were needed), that would be an appropriate
time to bail.

We do something similar with verify_ref_format(); it is just a string
until we know we are ready to use it. But here, the format that is being
used for sorting gets fed early to parse_ref_sorting(). I guess it is a
bit more complicated, because it is generating a linked list. But maybe
it could generate a list of to-be-parsed entries, with a function like
verify_sort_format() to validate them.

-Peff

```

## Bagas Sanjaya, 2021-09-13 04:37

Subject: Re: git tag -h fatal error with global tag.sort config
Message-ID: <f6673745-a6da-60b5-0782-349b6b76608b@gmail.com>
URL: https://gitlist.dev/e/f6673745-a6da-60b5-0782-349b6b76608b%40gmail.com
In-Reply-To: <20210912132757.GC76263@szeder.dev>

```
On 12/09/21 20.27, SZEDER Gábor wrote:
> Now, 'git branch' uses the same ref-filter sorting, but the equivalent
> 'git -c branch.sort=creatordate branch -h' command does show the usage
> as expected.  The relevant difference between cmd_branch() and
> cmd_tag() is that the former special-cases the '-h' option as well
> just before it would call git_config().  Doing the same in cmd_tag()
> like in the patch below seems to fix this issue, but I'm not sure that
> this is the right fix.

Also note: specifying the config above as local config (in .git/config) 
doesn't trigger the error.

-- 
An old man doll... just what I always wanted! - Clara

```

## Bagas Sanjaya, 2021-10-18 05:24

Subject: Re: git tag -h fatal error with global tag.sort config
Message-ID: <cc1b7611-8ec1-db29-1678-f45ba08d9b68@gmail.com>
URL: https://gitlist.dev/e/cc1b7611-8ec1-db29-1678-f45ba08d9b68%40gmail.com
In-Reply-To: <20210912132757.GC76263@szeder.dev>

```
On 12/09/21 20.27, SZEDER Gábor wrote:
> Interesting.  It bisects to 47bd3d0c14 (ref-filter: don't look for
> objects when outside of a repository, 2018-11-14), which, based on the
> error message, kind of makes sense, because 'git tag' uses the general
> ref-filter sorting facility.  Now, even if 'git tag -h' is executed in
> a repository, since 99caeed05d (Let 'git <command> -h' show usage
> without a git dir, 2009-11-09) run_builtin() special-cases the '-h'
> option and does not call setup_git_directory(), so cmd_tag() and
> everything invoked from within will mistakenly think that there is no
> repository.  And cmd_tag() parses the config before parsing the
> options (of course, otherwise command line options couldn't override
> the config), so it hits this die() before parse_options would get a
> change to act on the '-h' option.
> 
> Now, 'git branch' uses the same ref-filter sorting, but the equivalent
> 'git -c branch.sort=creatordate branch -h' command does show the usage
> as expected.  The relevant difference between cmd_branch() and
> cmd_tag() is that the former special-cases the '-h' option as well
> just before it would call git_config().  Doing the same in cmd_tag()
> like in the patch below seems to fix this issue, but I'm not sure that
> this is the right fix.
> 
> 
>    ---  >8  ---
> 
> diff --git a/builtin/tag.c b/builtin/tag.c
> index 065b6bf093..31b8cc4600 100644
> --- a/builtin/tag.c
> +++ b/builtin/tag.c
> @@ -485,6 +485,9 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
>   
>   	setup_ref_filter_porcelain_msg();
>   
> +	if (argc == 2 && !strcmp(argv[1], "-h"))
> +		usage_with_options(git_tag_usage, options);
> +
>   	git_config(git_tag_config, sorting_tail);
>   
>   	memset(&opt, 0, sizeof(opt));
> 

Sorry for long reply.

Patch applied and usage help appears.

But when I do `./bin-wrappers/git tag -h`, I don't see `usage:` header, 
and instead show the help starting from `-F`.

-- 
An old man doll... just what I always wanted! - Clara

```
