statamic / spock

Automatically perform git commits, pushes, and other cli actions when Statamic content changes
95 stars 16 forks source link

Missing possibility to have a custom Git commit command #17

Closed efoken closed 6 years ago

efoken commented 6 years ago

Just updated to Spock 2.0.1, but I'm missing the possibility to have a custom commit command, like I had in Spock 1:

commands:
  - "git add {{ full_path }}"
  - "git -c user.name='{{ committer:first_name }} {{ committer:last_name }}' -c user.email='{{ committer:username }}' commit -m 'Update {{ url }}'"
jasonvarga commented 6 years ago

By default, the commands are the standard Git ones. You are free to override these.

Check out https://github.com/statamic/spock/blob/master/DOCUMENTATION.md#custom-commands

class YourServiceProvider extends ServiceProvider
{
    public function boot()
    {
        app('spock')->setCommands(function ($spock) {
            $e = $spock->event();
            $user = $spock->user();

            foreach ($e->affectedPaths() as $path) {
                $commands[] = "git add {$path}";
            }

            $commands[] = vsprintf('git -c user.name="%s %s" -c user.email="%s"', [
                $user->get('first_name'),
                $user->get('last_name'),
                $user->email()
            ]);

            // Use the event to create the commit message.
            // eg. "Statamic\Events\Data\DataSaved" becomes "Data saved"
            $class = (new \ReflectionClass($e))->getShortName();
            $message = ucfirst(str_replace('_', ' ', snake_case($class)));
            $commands[] = sprintf('commit -m "%s', $message);

            return $commands;
        });
    }
}
efoken commented 6 years ago

Wow cool, didn't know that 😃 Thanks!