statamic / cms

The core Laravel CMS Composer package
https://statamic.com
Other
3.72k stars 510 forks source link

Git commit and push fails: "not a git repository" #4045

Closed sebastiaanluca closed 3 years ago

sebastiaanluca commented 3 years ago

Bug Description

We seem to have an issue related to this bug and fix (although not sure): https://github.com/statamic/cms/pull/3829

After some debugging we've found that it tries to find a .git folder in the current and parent directories of a symlinked storage folder. But since that storage directory is outside of the release directory, which contains .git., it can't find it and the command (git commit and push) fails.

Works fine when we remove the directory or don't use a symlink.

How to Reproduce

With the following directory structure on a DO/Forge server:

current -> /home/forge/site/releases/20210721-155420/
.env
releases
storage

and inside current or the current release:

storage -> /home/forge/site/storage/

And a config/statamic/git.php config:

    'paths' => [
        base_path('content'),
        base_path('users'),
        resource_path('blueprints'),
        resource_path('fieldsets'),
        resource_path('forms'),
        resource_path('users'),
        storage_path('forms'),
        public_path('assets'),
    ],

When committing using php please git:commit or any other interaction, it outputs:

The provided cwd "fatal: not a git repository (or any of the parent directories): .git/" does not exist.

Extra Detail

Debugging this, we found the issue is storage_path('forms').

Getting the return value of \Statamic\Git\Git::groupTrackedContentPathsByRepo() on production using Tinker, we get:

=> Illuminate\Support\Collection {#3948
     all: [
       "/home/forge/www.vitr.nl/releases/20210721-155420" => Illuminate\Support\Collection {#3944
         all: [
           "/home/forge/site/releases/20210721-155420/content",
           "/home/forge/site/releases/20210721-155420/users",
           "/home/forge/site/releases/20210721-155420/resources/blueprints",
           "/home/forge/site/releases/20210721-155420/resources/fieldsets",
           "/home/forge/site/releases/20210721-155420/resources/forms",
           "/home/forge/site/releases/20210721-155420/resources/users",
           "/home/forge/site/releases/20210721-155420/public/assets",
         ],
       },
       "fatal: not a git repository (or any of the parent directories): .git" => Illuminate\Support\Collection {#3949
         all: [
           "/home/forge/site/releases/20210721-155420/storage/forms",
         ],
       },
     ],
   }

Environment

Statamic 3.1.9 Pro Laravel 8.34.0 PHP 8.0.5 No addons installed

Install method (choose one): either starter kit or fresh install, not sure

duncanmcclean commented 3 years ago

Are you using Envoyer?

sebastiaanluca commented 3 years ago

Custom Forge deploy script, but it'd be the same IMO.

Among other steps, we:

The git:commit command works fine if we don't use paths that are in the symlinked directory. We moved it to resources/forms/submissions for now and that works, but it's not ideal. An alternative would be to not commit directories in symlinked directories, but we need to commit that data.

duncanmcclean commented 3 years ago

Ah gotcha - we used Envoyer and it go rid of the .git directory upon deploy so wondered if that was the same for you.

But obviously not πŸ˜…

jesseleite commented 3 years ago

Hey @sebastiaanluca, are you committing your form submissions to a separate repo, or is the intent to symlink and commit them into your main app repo?

The reason I ask is that I've fixed the error you mention in #4062, but git doesn't seem to allow the tracking and committing of symlinked content through soft symlinks. Instead, git stores the properties of the link (i.e., the path of the file system) in a blob just like it would for a normal file, rather than the Statamic content you change on the other end of the symlink. You can read more on how git deals with symlinks here: http://tdongsi.github.io/blog/2016/02/20/symlinks-in-git/

sebastiaanluca commented 3 years ago

@jesseleite Same repository. So we deploy the master branch, keep .git around, and when changes occur we commit and push back to master using php please git:commit.

git doesn't seem to allow the tracking and committing of symlinked content through soft symlinks

Apparently not πŸ‘€ Thanks for the link. Never had any issues with such a setup, but perhaps I've never set it up in such a way. We'll keep the content we want to push back to our repository outside of the symlink then.

I do however think the cause of the error message is unrelated to git. It happens before anything git does. Basically it tries to find a .git folder in the current and parent directories of a symlinked storage folder. The commit command can read the symlinked directory and tries to do its job, but it doesn't anticipate a symlinked directory that outside of the current project directory. It anticipates it's still in the project directory that has a .git directory.

That's something you might or might not want to fix/handle since it's a bit of an edge case. E.g. throw an exception or just ignore the directory if it's (a symlink) outside of the project dir (before git tries to do something). I'll close this since git doesn't support committing contents inside symlinks.

Thanks!

jesseleite commented 3 years ago

@sebastiaanluca Thanks for following up!

I do however think the cause of the error message is unrelated to git. It happens before anything git does.

100% agree, see https://github.com/statamic/cms/pull/4062 πŸ‘

sebastiaanluca commented 3 years ago

Awesome, thanks!