pedroborges / kirby-autogit

⬢ Saves every change made via Kirby Panel to a Git repository
149 stars 23 forks source link

No automatic commit if content was changed via PHP #27

Closed ghost closed 7 years ago

ghost commented 7 years ago

Assuming I have the following code in one of my controllers that might get executed from time to time:

$comments = $page->comments()->yaml();
$comments[] = $data;

page()->update(array(
    'comments' => yaml::encode($comments),
));

This would change the file according to my needs but it won't invoke an automatic commit by AutoGit. Is there a way to manually create a commit with AutoGit or is this an unwanted bug?

pedroborges commented 7 years ago

That's not a bug. Auto Git is only triggered when Kirby hooks are fired and that only happens when changes are made via the panel.

But you can call it manually from anywhere in your code with:

autogit()->save('page.update', $page->uri());

That will create a commit with the message: Updated page {uri}.

Alternatively you can add a custom message by using the autogit.translation option:

// site/config.php

c::set('autogit.translation', [
    'site.update'  => 'Changed site options',
    'page.create'  => 'Created page %s',
    'page.update'  => 'Updated page %s',
    'page.delete'  => 'Deleted page %s',
    'page.sort'    => 'Sorted page %s',
    'page.hide'    => 'Hid page %s',
    'page.move'    => 'Moved page %1$s to %2$s',
+   'page.comment' => 'Comment added to page %s',
    'file.upload'  => 'Uploaded file %s',
    'file.replace' => 'Replaced file %s',
    'file.rename'  => 'Renamed file %s',
    'file.update'  => 'Updated file %s',
    'file.sort'    => 'Sorted file %s',
    'file.delete'  => 'Deleted file %s',
]);

And then you can use the new key when calling Auto Git after your logic to save the comments:

autogit()->save('page.comment', $page->uri());
ghost commented 7 years ago

Thanks, that is working like a charm!