statamic / cms

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

Git Automation doesn't recognize modified files #6286

Open krsilas opened 2 years ago

krsilas commented 2 years ago

Bug description

I used to be able to commit changes from the CP or automatically commit them if STATAMIC_GIT_AUTOMATIC was set to true. This has stopped working after an update. I can see the modified files if I run git status, but Statamic isn't aware of them.

Bildschirmfoto 2022-07-02 um 14 39 39 Bildschirmfoto 2022-07-02 um 14 38 53

This is my config:

<?php

return [
    'enabled' => true,
    'automatic' => true,
    'queue_connection' => env('STATAMIC_GIT_QUEUE_CONNECTION'),
    'dispatch_delay' => 0,
    'use_authenticated' => true,
    'user' => [
        'name' => env('STATAMIC_GIT_USER_NAME', 'Spock'),
        'email' => env('STATAMIC_GIT_USER_EMAIL', 'spock@example.com'),
    ],
    'paths' => [
        base_path('content'),
        base_path('users'),
        resource_path('blueprints'),
        resource_path('fieldsets'),
        resource_path('forms'),
        resource_path('users'),
        public_path('assets'),
    ],
    'binary' => 'git',
    'commands' => [
        'git add {{ paths }}',
        'git -c "user.name={{ name }}" -c "user.email={{ email }}" commit -m "[BOT] {{ message }}"',
    ],
    'push' => true,
    'ignored_events' => [],
    'locale' => null,
];

How to reproduce

Unfortunately I have no idea whats causing this. I hope someone else has experienced it too or knows what the issue is.

Logs

No response

Versions

Statamic 3.3.17 Pro Laravel 8.83.18 PHP 8.0.20

Installation

Fresh statamic/statamic site via CLI

Antlers Parser

regex (default)

Additional details

No response

mattkibbler commented 2 years ago

I also seem to be having this problem.

Sorry this isn't very detailed, but I've just upgraded from v3.3.16 to v3.3.25 and the git automation no longer works.

Manually running the php please git:commit command works, but the automation does not.

tbruckmaier commented 1 year ago

I see the same error on statamic 3.3.59. Modified files when running git status from the CLI, but statamic's git page shows "Nothing to commit".

The issue seems to originate in git itself though. I have debugged the issue a bit further. Statamic\Console\Processes\Git::isRepo() runs git for each configured folder (the ones configured in statamic.git.paths). If that command produces an error, the folder is omitted.

In my case (and probably others too?), a recent git update on the server resulted in permission errors.

    fatal: detected dubious ownership in repository at '/home/sites/sitefoo/bar'
    To add an exception for this directory, call:
    git config --global --add safe.directory /home/sites/sitefoo/bar

(background: ssh & webserver run with two different users, and git >= 2.35.2 chokes on that per default).

Statamic therefore removed the folders from its list and ends up with no git-tracked folders at all.

So for me, the issue does not come from statamic, but from the server's filesystem / user permission setup. Nevertheless, statamic should IMO display any errors for the configured paths on its status page, so any problems can be discovered easier.

jesseleite commented 1 year ago

I believe we show git errors in your app log, but yes maybe we can show git status errors.

tbruckmaier commented 1 year ago

@jesseleite Seems like any git errors are explictly suppressed. Also just checked the log files, git's error message does no show up there.

https://github.com/statamic/cms/blob/547846d638deb9755edc5aefd9548d2033da855b/src/Console/Processes/Git.php#L22-L29

jhorii commented 1 year ago

I'm just trying this for the first time but I'm experiencing the same issue. git status shows changes have been made but in the control panel git page shows nothing.

marijoo commented 1 year ago

I’m having the same problem. Is there any solution in sight?

jesseleite commented 1 year ago

@jhorii @marijoo Is there anything that gets logged (in storage/logs) when you access the git page in the control panel?

jasonvarga commented 1 year ago

@jesseleite I'm pretty sure the error is happening within the withoutLoggingErrors callback so there wouldn't be anything logged.

jesseleite commented 1 year ago

Likely yep 👍 Just hoping to verify the issue is within isRepo() (the only place we're using withoutLoggingErrors()).

jesseleite commented 1 year ago

To anyone still experiencing this issue, curious if you could composer patch this PR into your app, then try to access the git page in the control panel, and share any git related errors in your app logs?

jboonstra commented 1 year ago

To anyone still experiencing this issue, curious if you could composer patch this PR into your app, then try to access the git page in the control panel, and share any git related errors in your app logs?

I'm running into this identical issue: changes are seen via commandline git, and are committed via php please git:commit, but nothing is showing as changed/commitable via the Utilities -> Git, and automated commits aren't happening (even with sync queueing).

I applied the patch to my application and this is what my error log shows (directory path has been obfuscated) when I load the Utilities -> Git screen:

[2023-04-13 12:43:57] production.ERROR: Git Process: fatal: detected dubious ownership in repository at '/path/to/deploy/releases/46'
To add an exception for this directory, call:

    git config --global --add safe.directory /path/to/deploy/releases/46

In my case, I believe I know why this is happening: because (for annoying historical reasons) I'm running PHP through Apache as mod_php rather than something like mod_fcgid or some other mechanism. This means that the Statamic control panel is running PHP processes as the www-data user, but actual code path is owned by the system user that owns/runs the site.

I don't know if mod_php is a particularly common setup these days, but the same might be true for some of the others experiencing this issue.

Since, in my case, the path to my repository is liable to change (it's managed by deployer) I would need to adjust the safe.directory setting each time I deploy, which is not an ideal setup.

I don't actually know of a great solution to this problem except to not use mod_php (which is on my list).

As an alternative solution for now, I'm probably going to set up commits via scheduling rather than queueing, and at some point stop using mod_php.

Thanks for the helpful patch that pinpointed the issue.

stebogit commented 6 months ago

Facing the same issue, with statamic/cms v4.56 on Laravel 10. The php please git:commit command (run manually as admin user) works fine, however the /cp/utilities/git page does not show changes, and if I apply the patch to show git errors I get the "detected dubious ownership" error in my logs.

My current setup is this (I had to reset permissions in my code since they were all messed up for some reason), where the current user is the owner and www-data has write permission via groups.

Is the www-data user running the code supposed to have permission to execute git commands as well, and for some reason it is not the case on my server? Should I add this line to the sudoers file as suggested here?

www-data ALL=(admin) NOPASSWD: /usr/bin/git

This feels a bit hacky to me, as I imagine this automatic sync feature (with sync queueing) should normally work out of the box - assuming correct permissions from the start...

marnickmenting commented 1 month ago

I got this issue too locally on Docker (macOS). Haven't tested on server yet. After applying the patch I get the same error as @jboonstra in laravel.log:

[2024-09-24 12:33:58] local.ERROR: Process Class: Git
Command: 'git' 'rev-parse' '--show-toplevel'
Error: fatal: detected dubious ownership in repository at '/var/www/html'
To add an exception for this directory, call:

    git config --global --add safe.directory /var/www/html  

Running the suggested git command does not help. Will use cron committing for now, but hope to add the solution by posting this.