-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add option to manually push and pull content
- 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
Showing
3 changed files
with
114 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
) | ||
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters