diff mbox series

setup: recognize bare repositories with packed-refs

Message ID 20231128142845.11523-1-adamm@zombino.com (mailing list archive)
State New, archived
Headers show
Series setup: recognize bare repositories with packed-refs | expand

Commit Message

Adam Majer Nov. 28, 2023, 2:28 p.m. UTC
In a garbage collected bare git repository, the refs/ subdirectory is
empty.  In use-cases when such a repository is directly added into
another repository, it no longer is detected as valid. Git doesn't
preserve empty paths so refs/ subdirectory is not present. Simply
creating an empty refs/ subdirectory fixes this problem.

Looking more carefully, there are two backends to handle various refs in
git -- the files backend that uses refs/ subdirectory and the
packed-refs backend that uses packed-refs file. If references are not
found in refs/ subdirectory (or directory doesn't exist), the
packed-refs directory will be consulted. Garbage collected repository
will have all its references in packed-refs file.

To allow the use-case when packed-refs is the only source of refs and
refs/ subdirectory is simply not present, augment 'is_git_directory()'
setup function to look for packed-refs file as an alternative to refs/
subdirectory.

Signed-off-by: Adam Majer <adamm@zombino.com>
---
 setup.c       | 10 +++++++---
 t/t6500-gc.sh |  9 +++++++++
 2 files changed, 16 insertions(+), 3 deletions(-)

Comments

Josh Steadmon Nov. 28, 2023, 6:45 p.m. UTC | #1
On 2023.11.28 15:28, Adam Majer wrote:
> In a garbage collected bare git repository, the refs/ subdirectory is
> empty.  In use-cases when such a repository is directly added into
> another repository, it no longer is detected as valid. Git doesn't
> preserve empty paths so refs/ subdirectory is not present. Simply
> creating an empty refs/ subdirectory fixes this problem.
> 
> Looking more carefully, there are two backends to handle various refs in
> git -- the files backend that uses refs/ subdirectory and the
> packed-refs backend that uses packed-refs file. If references are not
> found in refs/ subdirectory (or directory doesn't exist), the
> packed-refs directory will be consulted. Garbage collected repository
> will have all its references in packed-refs file.
> 
> To allow the use-case when packed-refs is the only source of refs and
> refs/ subdirectory is simply not present, augment 'is_git_directory()'
> setup function to look for packed-refs file as an alternative to refs/
> subdirectory.
> 
> Signed-off-by: Adam Majer <adamm@zombino.com>
> ---
>  setup.c       | 10 +++++++---
>  t/t6500-gc.sh |  9 +++++++++
>  2 files changed, 16 insertions(+), 3 deletions(-)
> 
> diff --git a/setup.c b/setup.c
> index fc592dc6dd..2a6dda6ae9 100644
> --- a/setup.c
> +++ b/setup.c
> @@ -348,7 +348,7 @@ int get_common_dir_noenv(struct strbuf *sb, const char *gitdir)
>   *
>   *  - either an objects/ directory _or_ the proper
>   *    GIT_OBJECT_DIRECTORY environment variable
> - *  - a refs/ directory
> + *  - a refs/ directory or packed-refs file
>   *  - either a HEAD symlink or a HEAD file that is formatted as
>   *    a proper "ref:", or a regular file HEAD that has a properly
>   *    formatted sha1 object name.
> @@ -384,8 +384,12 @@ int is_git_directory(const char *suspect)
>  
>  	strbuf_setlen(&path, len);
>  	strbuf_addstr(&path, "/refs");
> -	if (access(path.buf, X_OK))
> -		goto done;
> +	if (access(path.buf, X_OK)) {
> +		strbuf_setlen(&path, len);
> +		strbuf_addstr(&path, "/packed-refs");
> +		if (access(path.buf, R_OK))
> +			goto done;
> +	}
>  
>  	ret = 1;
>  done:
> diff --git a/t/t6500-gc.sh b/t/t6500-gc.sh
> index 18fe1c25e6..4ad1690817 100755
> --- a/t/t6500-gc.sh
> +++ b/t/t6500-gc.sh
> @@ -214,6 +214,15 @@ test_expect_success 'gc.repackFilter launches repack with a filter' '
>  	grep -E "^trace: (built-in|exec|run_command): git repack .* --filter=blob:none ?.*" trace.out
>  '
>  
> +test_expect_success 'GCed bare repos without empty refs/ still recognized' '
> +	GIT_DIR="$PWD"/bare.git git cat-file -e master &&
> +	test_dir_is_empty bare.git/refs/heads &&
> +	test_dir_is_empty bare.git/refs/tags &&
> +	test_file_not_empty bare.git/packed-refs &&
> +	rm -r bare.git/refs &&
> +	GIT_DIR="$PWD"/bare.git git cat-file -e master
> +'
> +
>  test_expect_success 'gc.repackFilterTo store filtered out objects' '
>  	test_when_finished "rm -rf bare.git filtered.git" &&
>  
> -- 
> 2.43.0.1.g67290e5b65

Thanks for the fixes. This looks good to me. BTW, in the future please
add a version number when you send updated patches (e.g. add "-v 2" to
your command-line if you're using git-format-patch).

Reviewed-by: Josh Steadmon <steadmon@google.com>
Jeff King Nov. 28, 2023, 7:04 p.m. UTC | #2
On Tue, Nov 28, 2023 at 03:28:45PM +0100, Adam Majer wrote:

> In a garbage collected bare git repository, the refs/ subdirectory is
> empty.  In use-cases when such a repository is directly added into
> another repository, it no longer is detected as valid. Git doesn't
> preserve empty paths so refs/ subdirectory is not present. Simply
> creating an empty refs/ subdirectory fixes this problem.

I understand your use case, but I still have a vague feeling that this
is bending some assumptions in a way that may create problems or
confusion later. In particular:

> Looking more carefully, there are two backends to handle various refs in
> git -- the files backend that uses refs/ subdirectory and the
> packed-refs backend that uses packed-refs file. If references are not
> found in refs/ subdirectory (or directory doesn't exist), the
> packed-refs directory will be consulted. Garbage collected repository
> will have all its references in packed-refs file.

This second paragraph doesn't seem totally accurate to me. There are not
really two backends that Git can use. For production use, there is just
one, the "files" backend, which happens to also use packed-refs under
the hood (and a convenient way for the code to structure this was a
subordinate backend). But it has never been possible to have a repo that
just uses packed-refs.

There is also the experimental reftable, of course. And there we have
not yet loosened is_git_directory(), and it has to create an unused
"refs/" directory (there has been some discussion about allowing it to
be an empty file, though no patches have been merged).

So with regards to the loosening in your patch, my questions would be:

  - if we are going to change the rules for repository detection, is
    this where we want to end up? We haven't changed them (yet) for
    reftables. If we are going to do so, should we have a scheme that
    will work for that transition, too? The "refs is an empty file"
    scheme would fix your use case, too (though see below).

  - is the rest of Git ready to handle a missing "refs/" directory? It
    looks like making a ref will auto-create it (since we may have to
    make refs/foo/bar/... anyway).

  - what about other implementations? Your embedded repos will
    presumably not work with libgit2, jgit, etc, until they also get
    similar patches.

  - what about empty repositories? In that case there will be no "refs/"
    file and no "packed-refs" file (such a repository is less likely, of
    course, but it may contain objects but no refs, or the point may be
    to have an empty repo as a test vector). Likewise, it is possible
    for a repository to have an empty "objects" directory (even with a
    non-empty refs directory, if there are only symrefs), and your patch
    doesn't address that.

> To allow the use-case when packed-refs is the only source of refs and
> refs/ subdirectory is simply not present, augment 'is_git_directory()'
> setup function to look for packed-refs file as an alternative to refs/
> subdirectory.

Getting back to your use case, I'd suggest one of:

  - do the usual "touch refs/.gitignore" trick to explicitly track the
    empty directory. It looks like the ref code will ignore this (we
    don't allow ref names to start with "." in a path component)

  - whatever is consuming the embedded repos could "mkdir -p refs
    objects" as needed. This is a minor pain, but I think in the long
    term we are moving to a world where you have to explicitly do
    "GIT_DIR=$PWD/embedded.git" to access an embedded bare repo. So
    they're already special and require some setup; adding an extra step
    may not be so bad.

Now it may be that neither of those solutions is acceptable for various
reasons. But it is probably worth detailing those reasons in your commit
message.

-Peff
Patrick Steinhardt Nov. 29, 2023, 10:13 a.m. UTC | #3
On Tue, Nov 28, 2023 at 02:04:46PM -0500, Jeff King wrote:
> On Tue, Nov 28, 2023 at 03:28:45PM +0100, Adam Majer wrote:
> 
> > In a garbage collected bare git repository, the refs/ subdirectory is
> > empty.  In use-cases when such a repository is directly added into
> > another repository, it no longer is detected as valid. Git doesn't
> > preserve empty paths so refs/ subdirectory is not present. Simply
> > creating an empty refs/ subdirectory fixes this problem.
> 
> I understand your use case, but I still have a vague feeling that this
> is bending some assumptions in a way that may create problems or
> confusion later. In particular:
> 
> > Looking more carefully, there are two backends to handle various refs in
> > git -- the files backend that uses refs/ subdirectory and the
> > packed-refs backend that uses packed-refs file. If references are not
> > found in refs/ subdirectory (or directory doesn't exist), the
> > packed-refs directory will be consulted. Garbage collected repository
> > will have all its references in packed-refs file.
> 
> This second paragraph doesn't seem totally accurate to me. There are not
> really two backends that Git can use. For production use, there is just
> one, the "files" backend, which happens to also use packed-refs under
> the hood (and a convenient way for the code to structure this was a
> subordinate backend). But it has never been possible to have a repo that
> just uses packed-refs.
> 
> There is also the experimental reftable, of course. And there we have
> not yet loosened is_git_directory(), and it has to create an unused
> "refs/" directory (there has been some discussion about allowing it to
> be an empty file, though no patches have been merged).

As I'm currently working on the reftable backend this thought has also
crossed my mind. The reftable backend doesn't only create "refs/", but
it also creates "HEAD" with contents "ref: refs/heads/.invalid" so that
Git commands recognize the Git directory properly. Longer-term I would
really love to see us doing a better job of detecting Git repositories
so that we don't have to carry this legacy baggage around.

I can see different ways for how to do this:

    - Either we iterate through all known reference backends, asking
      each of them whether they recognize the directory as something
      they understand.

    - Or we start parsing the gitconfig of the repository so that we can
      learn about which reference backend to expect, and then ask that
      specific backend whether it thinks that the directory indeed looks
      like something it can handle.

I'd personally prefer the latter, but I'm not sure whether we really
want to try and parse any file that happens to be called "config".

> So with regards to the loosening in your patch, my questions would be:
> 
>   - if we are going to change the rules for repository detection, is
>     this where we want to end up? We haven't changed them (yet) for
>     reftables. If we are going to do so, should we have a scheme that
>     will work for that transition, too? The "refs is an empty file"
>     scheme would fix your use case, too (though see below).
> 
>   - is the rest of Git ready to handle a missing "refs/" directory? It
>     looks like making a ref will auto-create it (since we may have to
>     make refs/foo/bar/... anyway).
> 
>   - what about other implementations? Your embedded repos will
>     presumably not work with libgit2, jgit, etc, until they also get
>     similar patches.
> 
>   - what about empty repositories? In that case there will be no "refs/"
>     file and no "packed-refs" file (such a repository is less likely, of
>     course, but it may contain objects but no refs, or the point may be
>     to have an empty repo as a test vector). Likewise, it is possible
>     for a repository to have an empty "objects" directory (even with a
>     non-empty refs directory, if there are only symrefs), and your patch
>     doesn't address that.

Just throwing this out there, but we could use this as an excuse to
introduce "extensions.refFormat". If it's explicitly configured to be
"reffiles" then we accept repositories even if they don't have the
"refs/" directory or a "packed-refs" file. This would still require work
in alternative implementations of Git, but this work will need to happen
anyway when the reftable backend lands.

I'd personally love for this extension to be introduced before I'm
sending the reftable backend upstream so that we can have discussions
around it beforehand.

Patrick

> > To allow the use-case when packed-refs is the only source of refs and
> > refs/ subdirectory is simply not present, augment 'is_git_directory()'
> > setup function to look for packed-refs file as an alternative to refs/
> > subdirectory.
> 
> Getting back to your use case, I'd suggest one of:
> 
>   - do the usual "touch refs/.gitignore" trick to explicitly track the
>     empty directory. It looks like the ref code will ignore this (we
>     don't allow ref names to start with "." in a path component)
> 
>   - whatever is consuming the embedded repos could "mkdir -p refs
>     objects" as needed. This is a minor pain, but I think in the long
>     term we are moving to a world where you have to explicitly do
>     "GIT_DIR=$PWD/embedded.git" to access an embedded bare repo. So
>     they're already special and require some setup; adding an extra step
>     may not be so bad.
> 
> Now it may be that neither of those solutions is acceptable for various
> reasons. But it is probably worth detailing those reasons in your commit
> message.
> 
> -Peff
>
Taylor Blau Nov. 29, 2023, 9:30 p.m. UTC | #4
On Tue, Nov 28, 2023 at 02:04:46PM -0500, Jeff King wrote:
>   - whatever is consuming the embedded repos could "mkdir -p refs
>     objects" as needed. This is a minor pain, but I think in the long
>     term we are moving to a world where you have to explicitly do
>     "GIT_DIR=$PWD/embedded.git" to access an embedded bare repo. So
>     they're already special and require some setup; adding an extra step
>     may not be so bad.

I hope not. I suppose that using embedded bare repositories in a test
requires additional setup at least to "cd" into the directory (if they
are not using `$GIT_DIR` or `--git-dir` already). But I fear that
imposing even a small change like this is too tall an order for how many
millions of these exist in the wild across all sorts of projects.

Thanks,
Taylor
Jeff King Dec. 6, 2023, 8:10 p.m. UTC | #5
On Wed, Nov 29, 2023 at 11:13:18AM +0100, Patrick Steinhardt wrote:

> As I'm currently working on the reftable backend this thought has also
> crossed my mind. The reftable backend doesn't only create "refs/", but
> it also creates "HEAD" with contents "ref: refs/heads/.invalid" so that
> Git commands recognize the Git directory properly. Longer-term I would
> really love to see us doing a better job of detecting Git repositories
> so that we don't have to carry this legacy baggage around.
> 
> I can see different ways for how to do this:
> 
>     - Either we iterate through all known reference backends, asking
>       each of them whether they recognize the directory as something
>       they understand.
> 
>     - Or we start parsing the gitconfig of the repository so that we can
>       learn about which reference backend to expect, and then ask that
>       specific backend whether it thinks that the directory indeed looks
>       like something it can handle.
> 
> I'd personally prefer the latter, but I'm not sure whether we really
> want to try and parse any file that happens to be called "config".

We do eventually parse the config file to pick up repositoryFormatVersion.
But there's sort of a chicken-and-egg here where we only do so after
gaining some confidence that it's a repo directory. :)

I actually think the "ask each backend if it looks plausible" is
reasonable, at least for an implementation that knows about all
backends. Though what gives me pause is how older versions of Git will
behave with a new-format repository that does not have a "refs"
directory.

There are really two compatibility checks. In is_git_directory(), we
want to say "is this a repo or not". And then later we parse the config,
make sure the repository format is OK, and that we support all
extensions. So right now, an older version of Git that encounters a
reftable-formatted repo (that has a vestigial "refs/" directory) says
"ah, that is a repo, but I don't understand it" (the latter because
presumably the repo version/extensions in .git/config are values it
doesn't know about). But if we get rid of "refs/", then older versions
of Git will stop even considering it as a repo at all, and will keep
searching up to the ceiling directory. So either:

  1. They'll find nothing, and you'll get "you're not in a git repo",
     rather than "you're in a git repo, but I don't understand it".
     Which is slightly worse.

  2. They'll find some _other_ containing repo. Which could be quite
     confusing.

So forgetting at all about how we structure the code, it seems to me
that the problem is not new code, but all of the existing code which
looks for access("refs", X_OK).

I dunno. Maybe that is being too paranoid about backwards compatibility.
People will have to turn on reftable manually, at least for a while, and
would hopefully know what they are signing up for, and that old versions
might not work as well. And by the time a new format becomes the
default, it's possible that those older versions would have become quite
rare.

> Just throwing this out there, but we could use this as an excuse to
> introduce "extensions.refFormat". If it's explicitly configured to be
> "reffiles" then we accept repositories even if they don't have the
> "refs/" directory or a "packed-refs" file. This would still require work
> in alternative implementations of Git, but this work will need to happen
> anyway when the reftable backend lands.
> 
> I'd personally love for this extension to be introduced before I'm
> sending the reftable backend upstream so that we can have discussions
> around it beforehand.

We already have an extension config option to specify that we're using
reftable, don't we? But anything in config has the same chicken-and-egg
problems as above, I think.

-Peff
Jeff King Dec. 6, 2023, 9:08 p.m. UTC | #6
On Wed, Nov 29, 2023 at 04:30:46PM -0500, Taylor Blau wrote:

> On Tue, Nov 28, 2023 at 02:04:46PM -0500, Jeff King wrote:
> >   - whatever is consuming the embedded repos could "mkdir -p refs
> >     objects" as needed. This is a minor pain, but I think in the long
> >     term we are moving to a world where you have to explicitly do
> >     "GIT_DIR=$PWD/embedded.git" to access an embedded bare repo. So
> >     they're already special and require some setup; adding an extra step
> >     may not be so bad.
> 
> I hope not. I suppose that using embedded bare repositories in a test
> requires additional setup at least to "cd" into the directory (if they
> are not using `$GIT_DIR` or `--git-dir` already). But I fear that
> imposing even a small change like this is too tall an order for how many
> millions of these exist in the wild across all sorts of projects.

I dunno. I am skeptical that there are millions of these. Who really
wants to embed bare git repos except for projects related to Git itself,
which want test vectors? Is there a use case I'm missing?

-Peff
Patrick Steinhardt Dec. 7, 2023, 7:01 a.m. UTC | #7
On Wed, Dec 06, 2023 at 03:10:48PM -0500, Jeff King wrote:
> On Wed, Nov 29, 2023 at 11:13:18AM +0100, Patrick Steinhardt wrote:
> 
> > As I'm currently working on the reftable backend this thought has also
> > crossed my mind. The reftable backend doesn't only create "refs/", but
> > it also creates "HEAD" with contents "ref: refs/heads/.invalid" so that
> > Git commands recognize the Git directory properly. Longer-term I would
> > really love to see us doing a better job of detecting Git repositories
> > so that we don't have to carry this legacy baggage around.
> > 
> > I can see different ways for how to do this:
> > 
> >     - Either we iterate through all known reference backends, asking
> >       each of them whether they recognize the directory as something
> >       they understand.
> > 
> >     - Or we start parsing the gitconfig of the repository so that we can
> >       learn about which reference backend to expect, and then ask that
> >       specific backend whether it thinks that the directory indeed looks
> >       like something it can handle.
> > 
> > I'd personally prefer the latter, but I'm not sure whether we really
> > want to try and parse any file that happens to be called "config".
> 
> We do eventually parse the config file to pick up repositoryFormatVersion.
> But there's sort of a chicken-and-egg here where we only do so after
> gaining some confidence that it's a repo directory. :)
> 
> I actually think the "ask each backend if it looks plausible" is
> reasonable, at least for an implementation that knows about all
> backends. Though what gives me pause is how older versions of Git will
> behave with a new-format repository that does not have a "refs"
> directory.
> 
> There are really two compatibility checks. In is_git_directory(), we
> want to say "is this a repo or not". And then later we parse the config,
> make sure the repository format is OK, and that we support all
> extensions. So right now, an older version of Git that encounters a
> reftable-formatted repo (that has a vestigial "refs/" directory) says
> "ah, that is a repo, but I don't understand it" (the latter because
> presumably the repo version/extensions in .git/config are values it
> doesn't know about). But if we get rid of "refs/", then older versions
> of Git will stop even considering it as a repo at all, and will keep
> searching up to the ceiling directory. So either:
> 
>   1. They'll find nothing, and you'll get "you're not in a git repo",
>      rather than "you're in a git repo, but I don't understand it".
>      Which is slightly worse.
> 
>   2. They'll find some _other_ containing repo. Which could be quite
>      confusing.
> 
> So forgetting at all about how we structure the code, it seems to me
> that the problem is not new code, but all of the existing code which
> looks for access("refs", X_OK).

True. The question is of course how much value there is in an old tool
to be able to discover a new repository that it wouldn't be able to read
in the first place due to it not understanding the reference format. So
I'd very much like to see that eventually, we're able to get rid of
"legacy" cruft that doesn't serve any purpose anymore.

The question is whether we can do a better job of this going forward so
that at least we don't have to pose the same question in the future.
Right now, we'll face the same problem whenever any part of the current
on-disk repository data structures changes.

I wonder whether it would make sense to introduce something like a
filesystem-level hint, e.g. in the form of a new ".is-git-repository"
file. If Git discovers that file then it assumes the directory to be a
Git repository -- and everything else is set up by parsing the config
and thus the repository's configured format.

> I dunno. Maybe that is being too paranoid about backwards compatibility.
> People will have to turn on reftable manually, at least for a while, and
> would hopefully know what they are signing up for, and that old versions
> might not work as well. And by the time a new format becomes the
> default, it's possible that those older versions would have become quite
> rare.
> 
> > Just throwing this out there, but we could use this as an excuse to
> > introduce "extensions.refFormat". If it's explicitly configured to be
> > "reffiles" then we accept repositories even if they don't have the
> > "refs/" directory or a "packed-refs" file. This would still require work
> > in alternative implementations of Git, but this work will need to happen
> > anyway when the reftable backend lands.
> > 
> > I'd personally love for this extension to be introduced before I'm
> > sending the reftable backend upstream so that we can have discussions
> > around it beforehand.
> 
> We already have an extension config option to specify that we're using
> reftable, don't we? But anything in config has the same chicken-and-egg
> problems as above, I think.

Not yet, no. I plan to submit the new "extensions.refFormat" extension
soonish though, probably next week.

Patrick
Jeff King Dec. 7, 2023, 7:34 a.m. UTC | #8
On Thu, Dec 07, 2023 at 08:01:41AM +0100, Patrick Steinhardt wrote:

> > So forgetting at all about how we structure the code, it seems to me
> > that the problem is not new code, but all of the existing code which
> > looks for access("refs", X_OK).
> 
> True. The question is of course how much value there is in an old tool
> to be able to discover a new repository that it wouldn't be able to read
> in the first place due to it not understanding the reference format. So
> I'd very much like to see that eventually, we're able to get rid of
> "legacy" cruft that doesn't serve any purpose anymore.

Right, nobody is going to be mad about not being able to use the
repository with old code. My concern is that by skipping it in the
discovery phase, though, the user may see unexpected behavior (like
continuing and finding some _other_ repo). I admit it's a pretty narrow
case, though.

> The question is whether we can do a better job of this going forward so
> that at least we don't have to pose the same question in the future.
> Right now, we'll face the same problem whenever any part of the current
> on-disk repository data structures changes.
> 
> I wonder whether it would make sense to introduce something like a
> filesystem-level hint, e.g. in the form of a new ".is-git-repository"
> file. If Git discovers that file then it assumes the directory to be a
> Git repository -- and everything else is set up by parsing the config
> and thus the repository's configured format.

I kind of think the presence of a well-formed HEAD is a good indicator
that it is a Git directory. IOW, I don't have any real problem with
simply loosening is_git_directory() to remove the "refs/" check
entirely. But again, that can only help us going forward; older versions
will still get confused if we truly ditch "refs/" for other formats.

Of course some ref formats may want to avoid the "HEAD" file itself, so
your .is-git-repository would be cleaner. I'm just not sure if it's
worth the headache to try to switch things now.

> > We already have an extension config option to specify that we're using
> > reftable, don't we? But anything in config has the same chicken-and-egg
> > problems as above, I think.
> 
> Not yet, no. I plan to submit the new "extensions.refFormat" extension
> soonish though, probably next week.

Ah, OK. I remember talking about it with Han-Wen long ago, but I admit I
have not paid much attention to reftable work recently. :) So I am happy
you are picking it up.

-Peff
Adam Majer Dec. 7, 2023, 8:20 a.m. UTC | #9
On 12/6/23 22:08, Jeff King wrote:
> On Wed, Nov 29, 2023 at 04:30:46PM -0500, Taylor Blau wrote:
> 
>> On Tue, Nov 28, 2023 at 02:04:46PM -0500, Jeff King wrote:
>>>    - whatever is consuming the embedded repos could "mkdir -p refs
>>>      objects" as needed. This is a minor pain, but I think in the long
>>>      term we are moving to a world where you have to explicitly do
>>>      "GIT_DIR=$PWD/embedded.git" to access an embedded bare repo. So
>>>      they're already special and require some setup; adding an extra step
>>>      may not be so bad.
>>
>> I hope not. I suppose that using embedded bare repositories in a test
>> requires additional setup at least to "cd" into the directory (if they
>> are not using `$GIT_DIR` or `--git-dir` already). But I fear that
>> imposing even a small change like this is too tall an order for how many
>> millions of these exist in the wild across all sorts of projects.
> 
> I dunno. I am skeptical that there are millions of these. Who really
> wants to embed bare git repos except for projects related to Git itself,
> which want test vectors? Is there a use case I'm missing?
Well, it's an "easy" thing to do, instead of recreating these test cases 
from sources like it's done here. It seems this is what happens in 
projects like Gitea.

As to the original questions you've raised earlier in the thread, I 
thought about it, and I don't really have a compelling reason to try to 
force this patch into Git. At least, I do not feel it necessary to try 
to argue the points you've raised. If that means the patch is ignored, 
I'm ok with that.

The reasons I put it here is simply I found that it fixes an issue I 
came across and that "everything else" worked. I don't know the 
intricacies of current or future git plans and I would rather delegate 
such discussion to the experts.

Best regards,
Adam
Patrick Steinhardt Dec. 7, 2023, 8:37 a.m. UTC | #10
On Thu, Dec 07, 2023 at 02:34:51AM -0500, Jeff King wrote:
> On Thu, Dec 07, 2023 at 08:01:41AM +0100, Patrick Steinhardt wrote:
> 
> > > So forgetting at all about how we structure the code, it seems to me
> > > that the problem is not new code, but all of the existing code which
> > > looks for access("refs", X_OK).
> > 
> > True. The question is of course how much value there is in an old tool
> > to be able to discover a new repository that it wouldn't be able to read
> > in the first place due to it not understanding the reference format. So
> > I'd very much like to see that eventually, we're able to get rid of
> > "legacy" cruft that doesn't serve any purpose anymore.
> 
> Right, nobody is going to be mad about not being able to use the
> repository with old code. My concern is that by skipping it in the
> discovery phase, though, the user may see unexpected behavior (like
> continuing and finding some _other_ repo). I admit it's a pretty narrow
> case, though.

Agreed, that's also an angle I brought up in a separate thread [1]. The
second benefit is that the user would get a proper error message stating
that the "extensions.refFormat" is not understood compared to Git just
skipping over the repository completely.

> > The question is whether we can do a better job of this going forward so
> > that at least we don't have to pose the same question in the future.
> > Right now, we'll face the same problem whenever any part of the current
> > on-disk repository data structures changes.
> > 
> > I wonder whether it would make sense to introduce something like a
> > filesystem-level hint, e.g. in the form of a new ".is-git-repository"
> > file. If Git discovers that file then it assumes the directory to be a
> > Git repository -- and everything else is set up by parsing the config
> > and thus the repository's configured format.
> 
> I kind of think the presence of a well-formed HEAD is a good indicator
> that it is a Git directory. IOW, I don't have any real problem with
> simply loosening is_git_directory() to remove the "refs/" check
> entirely. But again, that can only help us going forward; older versions
> will still get confused if we truly ditch "refs/" for other formats.
> 
> Of course some ref formats may want to avoid the "HEAD" file itself, so
> your .is-git-repository would be cleaner. I'm just not sure if it's
> worth the headache to try to switch things now.

I think that both "HEAD" and "refs/" are in the same spirit and consider
both to be legacy cruft that ideally wouldn't exist with the reftable
backend. I think dropping just one of these requirements ("refs/") is
the least favorable option though:

  - We'd still have unneeded files that only exist to aid old clients.

  - At the same time, the old clients wouldn't be able to detect the
    repository anymore and need an update. So we could just as well drop
    both files and would have the same outcome.

  - This is not a long-term solution in case anything else in the
    on-disk format will ever change.

Whether it's worth getting rid of them now... probably not, at least not
for the time being. But if we want to address this issue I'd rather want
to aim for a proper solution that also works for future changes.

Patrick

[1]: <ZXFy0_T1AZLh058g@tanuki>
Junio C Hamano Dec. 8, 2023, 6:17 p.m. UTC | #11
Jeff King <peff@peff.net> writes:

> So with regards to the loosening in your patch, my questions would be:
>
>   - if we are going to change the rules for repository detection, is
>     this where we want to end up? We haven't changed them (yet) for
>     reftables. If we are going to do so, should we have a scheme that
>     will work for that transition, too? The "refs is an empty file"
>     scheme would fix your use case, too (though see below).
>
>   - is the rest of Git ready to handle a missing "refs/" directory? It
>     looks like making a ref will auto-create it (since we may have to
>     make refs/foo/bar/... anyway).
>
>   - what about other implementations? Your embedded repos will
>     presumably not work with libgit2, jgit, etc, until they also get
>     similar patches.
>
>   - what about empty repositories? In that case there will be no "refs/"
>     file and no "packed-refs" file (such a repository is less likely, of
>     course, but it may contain objects but no refs, or the point may be
>     to have an empty repo as a test vector). Likewise, it is possible
>     for a repository to have an empty "objects" directory (even with a
>     non-empty refs directory, if there are only symrefs), and your patch
>     doesn't address that.

All good points.

> Getting back to your use case, I'd suggest one of:
>
>   - do the usual "touch refs/.gitignore" trick to explicitly track the
>     empty directory. It looks like the ref code will ignore this (we
>     don't allow ref names to start with "." in a path component)
>
>   - whatever is consuming the embedded repos could "mkdir -p refs
>     objects" as needed. This is a minor pain, but I think in the long
>     term we are moving to a world where you have to explicitly do
>     "GIT_DIR=$PWD/embedded.git" to access an embedded bare repo. So
>     they're already special and require some setup; adding an extra step
>     may not be so bad.

Yeah, it truly is caused by the combination of the fact that we do
not "track" empty directories and that skeleton Git repository
structure does rely on possibly empty directories.  The above two
are reasonable workarounds when you are dealing with any medium that
does not allow empty directories, not just working tree managed by
Git.
Taylor Blau Dec. 8, 2023, 9:09 p.m. UTC | #12
On Wed, Dec 06, 2023 at 04:08:36PM -0500, Jeff King wrote:
> On Wed, Nov 29, 2023 at 04:30:46PM -0500, Taylor Blau wrote:
>
> > On Tue, Nov 28, 2023 at 02:04:46PM -0500, Jeff King wrote:
> > >   - whatever is consuming the embedded repos could "mkdir -p refs
> > >     objects" as needed. This is a minor pain, but I think in the long
> > >     term we are moving to a world where you have to explicitly do
> > >     "GIT_DIR=$PWD/embedded.git" to access an embedded bare repo. So
> > >     they're already special and require some setup; adding an extra step
> > >     may not be so bad.
> >
> > I hope not. I suppose that using embedded bare repositories in a test
> > requires additional setup at least to "cd" into the directory (if they
> > are not using `$GIT_DIR` or `--git-dir` already). But I fear that
> > imposing even a small change like this is too tall an order for how many
> > millions of these exist in the wild across all sorts of projects.
>
> I dunno. I am skeptical that there are millions of these. Who really
> wants to embed bare git repos except for projects related to Git itself,
> which want test vectors? Is there a use case I'm missing?

Just picking on GitHub as an example, my copy has a fair number of
embedded bare repositories:

    $ find . -mindepth 2 -type d -name '*.git' | wc -l
    279

That might be an unfair example in general, since GitHub probably has a
greater need to embed bare repositories than most other projects. But I
think that we shouldn't make our decision here based on volume of
embedded bare repositories, but rather on the number of projects which
have >1 embedded bare repository.

In other words, the cost of migrating a single project's embedded bare
repositories is roughly the same whether there are 1 or 279 of them. So
the effort scales with the number of projects, not repositories.

Perhaps I'm over-estimating how difficult this transition would be to
impose on users. But it does make me very leery to make this kind of a
change without having a better sense of how many of them exist in the
wild.

Searching just on GitHub for `path:**/*.git/config` [^1], it looks like
there are ~1,400 results. That provides us an upper-bound on the number
of projects which have embedded bare repositories, so perhaps I really
am overestimating the burden we'd be imposing on other projects.

I dunno :-).

Thanks,
Taylor

[^1]: Searching for "path:**/*.git" doesn't quite work, since GitHub's
  search doesn't match directories here. So the search I actually used
  isn't perfect, but it should give us a rough approximation.
Jeff King Dec. 12, 2023, 1:22 a.m. UTC | #13
On Fri, Dec 08, 2023 at 04:09:03PM -0500, Taylor Blau wrote:

> > I dunno. I am skeptical that there are millions of these. Who really
> > wants to embed bare git repos except for projects related to Git itself,
> > which want test vectors? Is there a use case I'm missing?
> 
> Just picking on GitHub as an example, my copy has a fair number of
> embedded bare repositories:
> 
>     $ find . -mindepth 2 -type d -name '*.git' | wc -l
>     279
> 
> That might be an unfair example in general, since GitHub probably has a
> greater need to embed bare repositories than most other projects. But I
> think that we shouldn't make our decision here based on volume of
> embedded bare repositories, but rather on the number of projects which
> have >1 embedded bare repository.

Right, I meant "I am skeptical there are a lot of projects that have
embedded repositories". It is useful if your project is related to
working on Git itself and you store your test vectors that way. So
github.git is not alone there (there is libgit2, other forges, and so
on). But I don't think it is representative in general.

> Perhaps I'm over-estimating how difficult this transition would be to
> impose on users. But it does make me very leery to make this kind of a
> change without having a better sense of how many of them exist in the
> wild.

Just to be clear: I am not proposing any transition here. It is already
the case that your "refs/" directory is necessary for Git to recognize
the bare repo, and you risk committing a broken state if you have no
loose refs in it.

There's been a proposal elsewhere to require extra steps to recognize an
embedded bare repo. Which I agree will be a pain for folks who use them,
but may be worth it for the security benefit. But here I was only saying
that _if_ we do that other change, then adding extra steps might not be
too bad on top. :)

(BTW, I think libgit2 already faces this problem, because it wants
non-bare repos; so there is some magic where it stores ".gitted"
directories, and then renames them on the fly).

> Searching just on GitHub for `path:**/*.git/config` [^1], it looks like
> there are ~1,400 results. That provides us an upper-bound on the number
> of projects which have embedded bare repositories, so perhaps I really
> am overestimating the burden we'd be imposing on other projects.

Thanks, that's an interesting number, and matches my intuition.

Of course it's not a true upper bound anyway. It wouldn't count private
projects (though maybe it hits github.git in your case), not to mention
stuff that isn't hosted on GitHub.

-Peff
diff mbox series

Patch

diff --git a/setup.c b/setup.c
index fc592dc6dd..2a6dda6ae9 100644
--- a/setup.c
+++ b/setup.c
@@ -348,7 +348,7 @@  int get_common_dir_noenv(struct strbuf *sb, const char *gitdir)
  *
  *  - either an objects/ directory _or_ the proper
  *    GIT_OBJECT_DIRECTORY environment variable
- *  - a refs/ directory
+ *  - a refs/ directory or packed-refs file
  *  - either a HEAD symlink or a HEAD file that is formatted as
  *    a proper "ref:", or a regular file HEAD that has a properly
  *    formatted sha1 object name.
@@ -384,8 +384,12 @@  int is_git_directory(const char *suspect)
 
 	strbuf_setlen(&path, len);
 	strbuf_addstr(&path, "/refs");
-	if (access(path.buf, X_OK))
-		goto done;
+	if (access(path.buf, X_OK)) {
+		strbuf_setlen(&path, len);
+		strbuf_addstr(&path, "/packed-refs");
+		if (access(path.buf, R_OK))
+			goto done;
+	}
 
 	ret = 1;
 done:
diff --git a/t/t6500-gc.sh b/t/t6500-gc.sh
index 18fe1c25e6..4ad1690817 100755
--- a/t/t6500-gc.sh
+++ b/t/t6500-gc.sh
@@ -214,6 +214,15 @@  test_expect_success 'gc.repackFilter launches repack with a filter' '
 	grep -E "^trace: (built-in|exec|run_command): git repack .* --filter=blob:none ?.*" trace.out
 '
 
+test_expect_success 'GCed bare repos without empty refs/ still recognized' '
+	GIT_DIR="$PWD"/bare.git git cat-file -e master &&
+	test_dir_is_empty bare.git/refs/heads &&
+	test_dir_is_empty bare.git/refs/tags &&
+	test_file_not_empty bare.git/packed-refs &&
+	rm -r bare.git/refs &&
+	GIT_DIR="$PWD"/bare.git git cat-file -e master
+'
+
 test_expect_success 'gc.repackFilterTo store filtered out objects' '
 	test_when_finished "rm -rf bare.git filtered.git" &&