(Grav GitSync) Automatic Commit from GitSync

This commit is contained in:
GitSync
2026-06-14 00:27:27 +00:00
parent a2920f812d
commit 3c1bfda80f
2933 changed files with 491625 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
<?php namespace Grav\Plugin\Console;
use Grav\Console\ConsoleCommand;
use Grav\Plugin\GitSync\GitSync;
/**
* Class InitCommand
*
* @package Grav\Plugin\Console
*/
class InitCommand extends ConsoleCommand
{
protected function configure()
{
$this
->setName('init')
->setDescription('Initializes your git repository')
->setHelp('The <info>init</info> command runs the same git commands as the onAdminAfterSave function. Use this to manually initialize git-sync (useful for automated deployments).')
;
}
protected function serve()
{
require_once __DIR__ . '/../vendor/autoload.php';
$plugin = new GitSync();
$repository = $plugin->getConfig('repository', false);
$this->output->writeln('');
if (!$repository) {
$this->output->writeln('<red>ERROR:</red> No repository has been configured!');
}
$this->output->writeln('Initializing <cyan>' . $repository . '</cyan>');
$this->output->write('Starting initialization...');
$plugin->initializeRepository();
$plugin->setUser();
$plugin->addRemote();
$this->output->writeln('completed.');
return 0;
}
}
+93
View File
@@ -0,0 +1,93 @@
<?php
namespace Grav\Plugin\Console;
use Grav\Console\ConsoleCommand;
use Grav\Plugin\GitSync\GitSync;
use Grav\Plugin\GitSync\Helper;
use Grav\Common\Grav;
use RocketTheme\Toolbox\File\YamlFile;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputOption;
/**
* Class LogCommand
*
* @package Grav\Plugin\Console
*/
class PasswdCommand extends ConsoleCommand
{
/** @var array */
protected $options = [];
protected function configure()
{
$this
->setName('passwd')
->setDescription('Allows to change the user and/or password programmatically')
->addOption(
'user',
'u',
InputOption::VALUE_REQUIRED,
'The username. Use empty double quotes if you need an empty username.'
)
->addOption(
'password',
'p',
InputOption::VALUE_REQUIRED,
"The password."
)
->setHelp('The <info>%command.name%</info> command allows to change the user and/or password. Useful when running automated scripts or needing to programmatically set them without admin access.')
;
}
protected function serve()
{
require_once __DIR__ . '/../vendor/autoload.php';
$grav = Grav::instance();
$config = $grav['config'];
$locator = $grav['locator'];
$filename = 'config://plugins/git-sync.yaml';
$file = YamlFile::instance($locator->findResource($filename, true, true));
$this->options = [
'user' => $this->input->getOption('user'),
'password' => $this->input->getOption('password')
];
if ($this->options['password'] !== null) {
$this->options['password'] = Helper::encrypt($this->options['password']);
}
$user = $this->options['user'] !== null ? $this->options['user'] : $config->get('plugins.git-sync.user');
$password = $this->options['password'] !== null ? $this->options['password'] : $config->get('plugins.git-sync.password');
$config->set('plugins.git-sync.user', $user);
$config->set('plugins.git-sync.password', $password);
$content = $grav['config']->get('plugins.git-sync');
$file->save($content);
$file->free();
$this->output->writeln('');
$this->output->writeln('<green>User / Password updated.</green>');
$this->output->writeln('');
return 0;
}
private function console_header($readable, $cmd = '', $remote_action = false)
{
$this->output->writeln(
"<yellow>$readable</yellow>" . ($cmd ? "(<info>$cmd</info>)" : ''). ($remote_action ? '...' : '')
);
}
private function console_log($lines, $password)
{
foreach ($lines as $line) {
$this->output->writeln(' ' . Helper::preventReadablePassword($line, $password));
}
}
}
+151
View File
@@ -0,0 +1,151 @@
<?php
namespace Grav\Plugin\Console;
use Grav\Console\ConsoleCommand;
use Grav\Plugin\GitSync\GitSync;
use Grav\Plugin\GitSync\Helper;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputOption;
/**
* Class LogCommand
*
* @package Grav\Plugin\Console
*/
class StatusCommand extends ConsoleCommand
{
protected function configure()
{
$this
->setName('status')
->setDescription('Checks the status of plugin config, git and git workspace. No files get modified!')
->addOption(
'fetch', 'f',
InputOption::VALUE_NONE,
'additionally do a git fetch to look updates (changes not files in workspace)'
)
->setHelp(<<<'EOF'
The <info>%command.name%</info> command checks if the plugin is usable the way it has been configured.
While doing this it prints the available information for your inspection.
<comment>No files in the workspace are modified when running this test.</comment>
The <info>--fetch</info> option can be used to see differences between the remote in the <info>git status</info> (last check)
It also returns with an error code and a helpful message when something is not normal:
<error>100</error> : <info>git</info> binary not working as expected
<error>50</error> : <info>repositoryFolder</info> and git workspace root do not match
<error>10</error> : <info>repository</info> is not configured
<error>5</error> : state of workspace not clean
<error>1</error> : Some checks can throw a <info>RuntimeException</info> which is not caught, read the message for details
EOF
)
;
}
protected function serve()
{
require_once __DIR__ . '/../vendor/autoload.php';
$plugin = new GitSync();
$this->output->writeln('');
$this->console_header('plugin runtime information:');
$info = $plugin->getRuntimeInformation();
$info['isGitInitialized'] = Helper::isGitInitialized();
$info['gitVersion'] = Helper::isGitInstalled(true);
ksort($info);
dump($info);
if (!Helper::isGitInstalled()) {
throw new RuntimeException('git binary not found', 100);
}
$this->console_header('detect git workspace root:');
$git_root = $plugin->execute('rev-parse --show-toplevel');
$this->console_log($git_root, '');
if (rtrim($info['repositoryPath'], '/') !== rtrim($git_root[0], '/')) {
throw new RuntimeException('git root and repositoryPath do not match', 50);
}
// needed to prevent output in logs:
$password = Helper::decrypt($plugin->getPassword() ?? '');
$this->console_header('local git config:');
$this->console_log(
$plugin->execute('config --local -l'), $password
);
$this->console_header(
'Testing connection to repository', 'git ls-remote', true
);
$repository = $plugin->getConfig('repository', false);
if (!$repository) {
throw new RuntimeException('No repository has been configured', 10);
}
$testRepository = $plugin->testRepository(
Helper::prepareRepository(
$plugin->getUser() ?? '',
$password,
$repository),
$plugin->getRemote('branch', null),
);
$this->console_log($testRepository, $password);
$fetched = false;
if ($this->input->getOption('fetch')) {
$remote = $plugin->getRemote('name', '');
$this->console_header(
'Looking for updates', "git fetch $remote", true
);
$this->console_log($plugin->fetch($remote), $password);
$fetched = true;
}
$this->console_header(
'Checking workspace status', 'git status', true
);
$git_status = $plugin->execute('status');
$this->console_log($git_status, $password);
if (!$plugin->isWorkingCopyClean()) {
throw new RuntimeException('Working state is not clean.', 5);
}
if ($fetched) {
$uptodate = strpos($git_status[1], 'branch is up-to-date with') > 0;
if ($uptodate) {
$this->console_header(
'Congrats: You should be able to run the <info>sync</info> command without problems!'
);
} else {
$this->output->writeln('<yellow>You are not in sync!</yellow>');
$this->output->writeln('Take a look at the output of git status to see more details.');
$this->output->writeln('In most cases the <info>sync</info> command is able to fix this.');
}
} else {
$this->console_header('Looks good: use <info>--fetch</info> option to check for updates.');
}
return 0;
}
private function console_header($readable, $cmd = '', $remote_action = false)
{
$this->output->writeln(
"<yellow>$readable</yellow>" . ($cmd ? "(<info>$cmd</info>)" : ''). ($remote_action ? '...' : '')
);
}
private function console_log($lines, $password)
{
foreach ($lines as $line) {
$this->output->writeln(' ' . Helper::preventReadablePassword($line, $password));
}
}
}
+53
View File
@@ -0,0 +1,53 @@
<?php
namespace Grav\Plugin\Console;
use Grav\Console\ConsoleCommand;
use Grav\Plugin\GitSync\GitSync;
/**
* Class LogCommand
*
* @package Grav\Plugin\Console
*/
class SyncCommand extends ConsoleCommand
{
protected function configure()
{
$this
->setName('sync')
->setDescription('Performs a synchronization of your site')
->setHelp('The <info>sync</info> command performs a synchronization of your site. Useful if you want to run a periodic crontab job to automate it.')
;
}
protected function serve()
{
require_once __DIR__ . '/../vendor/autoload.php';
$plugin = new GitSync();
$repository = $plugin->getConfig('repository', false);
$this->output->writeln('');
if (!$repository) {
$this->output->writeln('<red>ERROR:</red> No repository has been configured');
}
$this->output->writeln('Synchronizing with <cyan>' . $repository . '</cyan>');
if ($plugin->hasChangesToCommit()) {
$this->output->writeln('Changes detected, adding and committing...');
$plugin->add();
$plugin->commit();
}
$this->output->write('Starting Synchronization...');
$plugin->sync();
$this->output->writeln('completed.');
return 0;
}
}