Skip to content

Commit

Permalink
feat: add option to manually push and pull content
Browse files Browse the repository at this point in the history
- call yourdomain.com/gcapc/pull or yourdomain.com/gcapc/push
  • Loading branch information
Pascal Küsgen committed Apr 21, 2016
1 parent e077476 commit 833960f
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 73 deletions.
69 changes: 42 additions & 27 deletions git-commit-and-push-content.php
Original file line number Diff line number Diff line change
@@ -1,49 +1,64 @@
<?php
require(__DIR__ . DS . 'helpers.php');

$gitHelper = new KirbyGitHelper();

/*
* Pages
*/
kirby()->hook('panel.page.create', function ($page) {
gitCommit('create(page): ' . $page->uri());
kirby()->hook('panel.page.create', function ($page) use ($gitHelper) {
$gitHelper->kirbyChange('create(page): ' . $page->uri());
});
kirby()->hook('panel.page.update', function ($page) {
gitCommit('update(page): ' . $page->uri());
kirby()->hook('panel.page.update', function ($page) use ($gitHelper) {
$gitHelper->kirbyChange('update(page): ' . $page->uri());
});
kirby()->hook('panel.page.delete', function ($page) {
gitCommit('delete(page): ' . $page->uri());
kirby()->hook('panel.page.delete', function ($page) use ($gitHelper){
$gitHelper->kirbyChange('delete(page): ' . $page->uri());
});
kirby()->hook('panel.page.sort', function ($page) {
gitCommit('sort(page): ' . $page->uri());
kirby()->hook('panel.page.sort', function ($page) use ($gitHelper){
$gitHelper->kirbyChange('sort(page): ' . $page->uri());
});
kirby()->hook('panel.page.hide', function ($page) {
gitCommit('hide(page): ' . $page->uri());
kirby()->hook('panel.page.hide', function ($page) use ($gitHelper){
$gitHelper->kirbyChange('hide(page): ' . $page->uri());
});
kirby()->hook('panel.page.move', function ($page) {
gitCommit('move(page): ' . $page->uri());
kirby()->hook('panel.page.move', function ($page) use ($gitHelper){
$gitHelper->kirbyChange('move(page): ' . $page->uri());
});

/*
* File
*/
kirby()->hook('panel.file.move', function ($file) {
gitCommit('move(file): ' . $file->page()->uri() . '/' . $file->filename());
});
kirby()->hook('panel.file.upload', function ($file) {
gitCommit('upload(file): ' . $file->page()->uri() . '/' . $file->filename());
kirby()->hook('panel.file.upload', function ($file) use ($gitHelper){
$gitHelper->kirbyChange('upload(file): ' . $file->page()->uri() . '/' . $file->filename());
});
kirby()->hook('panel.file.replace', function ($file) {
gitCommit('replace(file): ' . $file->page()->uri() . '/' . $file->filename());
kirby()->hook('panel.file.replace', function ($file) use ($gitHelper){
$gitHelper->kirbyChange('replace(file): ' . $file->page()->uri() . '/' . $file->filename());
});
kirby()->hook('panel.file.rename', function ($file) {
gitCommit('rename(file): ' . $file->page()->uri() . '/' . $file->filename());
kirby()->hook('panel.file.rename', function ($file) use ($gitHelper){
$gitHelper->kirbyChange('rename(file): ' . $file->page()->uri() . '/' . $file->filename());
});
kirby()->hook('panel.file.update', function ($file) {
gitCommit('update(file): ' . $file->page()->uri() . '/' . $file->filename());
kirby()->hook('panel.file.update', function ($file) use ($gitHelper){
$gitHelper->kirbyChange('update(file): ' . $file->page()->uri() . '/' . $file->filename());
});
kirby()->hook('panel.file.sort', function ($file) {
gitCommit('sort(file): ' . $file->page()->uri() . '/' . $file->filename());
kirby()->hook('panel.file.sort', function ($file) use ($gitHelper){
$gitHelper->kirbyChange('sort(file): ' . $file->page()->uri() . '/' . $file->filename());
});
kirby()->hook('panel.file.delete', function ($file) {
gitCommit('delete(file): ' . $file->page()->uri() . '/' . $file->filename());
kirby()->hook('panel.file.delete', function ($file) use ($gitHelper){
$gitHelper->kirbyChange('delete(file): ' . $file->page()->uri() . '/' . $file->filename());
});

kirby()->routes(array(
array(
'pattern' => 'gcapc/(:any)',
'action' => function($gitCommand) use ($gitHelper) {
switch ($gitCommand) {
case "push":
$gitHelper->push();
break;
case "pull":
$gitHelper->pull();
break;
}
}
)
));
116 changes: 70 additions & 46 deletions helpers.php
Original file line number Diff line number Diff line change
@@ -1,63 +1,87 @@
<?php

require_once('Git.php/Git.php');

/**
* Compose Commit message, appends " by Username"
*
* @param string $commitMessage
*
* @return false
*/
function gitCommit($commitMessage)
class KirbyGitHelper
{
$debugMode = c::get('debug', false);
$branch = c::get('gcapc-branch', 'master');
$pull = c::get('gcapc-pull', false);
$push = c::get('gcapc-push', false);
$commit = c::get('gcapc-commit', false);
$gitBin = c::get('gcapc-gitBin', '');
$windowsMode = c::get('gcapc-windowsMode', false);

/*
* Setup git environment
*/
if ($windowsMode) {
Git::windows_mode();
}
if ($gitBin) {
Git::set_bin($gitBin);
private $repo;
private $repoPath;
private $branch;
private $pullOnChange;
private $pushOnChange;
private $commitOnChange;
private $gitBin;
private $windowsMode;

public function __construct($repoPath = __DIR__ . "/../../../content")
{
$this->repoPath = $repoPath;
}

$repo = Git::open('../content');
private function initRepo()
{
if (!class_exists("Git")) {
require('Git.php/Git.php');
}

$this->branch = c::get('gcapc-branch', 'master');
$this->pullOnChange = c::get('gcapc-pull', false);
$this->pushOnChange = c::get('gcapc-push', false);
$this->commitOnChange = c::get('gcapc-commit', false);
$this->gitBin = c::get('gcapc-gitBin', '');
$this->windowsMode = c::get('gcapc-windowsMode', false);

if ($debugMode) {
if (!$repo->test_git()) {
echo 'git could not be found or is not working properly. ' . Git::get_bin();
exit;
if ($this->windowsMode) {
Git::windows_mode();
}
if ($this->gitBin) {
Git::set_bin($this->gitBin);
}

$this->repo = Git::open($this->repoPath);

if (!Git::is_repo($repo)) {
echo '$repo is not an instance of GitRepo';
exit;
if (!$this->repo->test_git()) {
trigger_error('git could not be found or is not working properly. ' . Git::get_bin());
}
}

/*
* Git Pull, Commit and Push
*/
$repo->checkout($branch);
private function getRepo()
{
if ($this->repo == null) {
$this->initRepo();
}

return $this->repo;
}

if ($pull) {
$repo->pull('origin', $branch);
public function commit($commitMessage)
{
$this->getRepo()->add('-A');
$this->getRepo()->commit($commitMessage);
}
if ($commit) {
$repo->add('-A');
$repo->commit($commitMessage . ' by ' . site()->user());

public function push($branch = false)
{
$branch = $branch ? $branch : $this->branch;
$this->getRepo()->push('origin', $branch);
}
if ($push) {
$repo->push('origin', $branch);

public function pull($branch = false)
{
$branch = $branch ? $branch : $this->branch;
$this->getRepo()->pull('origin', $branch);
}

return false;
public function kirbyChange($commitMessage)
{
$this->getRepo()->checkout($this->branch);

if ($this->pullOnChange) {
$this->pull();
}
if ($this->commitOnChange) {
$this->commit($commitMessage . "\n\nby " . site()->user());
}
if ($this->pushOnChange) {
$this->push();
}
}
}
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ You can use the following [Options](http://getkirby.com/docs/advanced/options) -

(In case you need to use multiple git users on your environment - [Multiple SSH Keys settings for different github account](https://gist.github.com/jexchan/2351996))

If you do not want to Pull and/or Push on every change you can also call `yourdomain.com/gcapc/push` or `yourdomain.com/gcapc/pull` manually (or automated with e.g. a cronjob).

#### gcapc-branch
Type: `String`
Default value: `'master'`
Expand Down

0 comments on commit 833960f

Please sign in to comment.