From 63a97ff6fc313372d9cb439a621f12fdecc2fac1 Mon Sep 17 00:00:00 2001 From: Andrew Manning Date: Sun, 29 May 2016 10:18:26 -0400 Subject: Git commit made for the page edits when the page is saved. --- Zotlabs/Module/Wiki.php | 15 +++++++++++++-- Zotlabs/Storage/GitRepo.php | 9 +++++++++ include/wiki.php | 43 ++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/Zotlabs/Module/Wiki.php b/Zotlabs/Module/Wiki.php index 5d293e6f0..70d326faf 100644 --- a/Zotlabs/Module/Wiki.php +++ b/Zotlabs/Module/Wiki.php @@ -268,9 +268,20 @@ class Wiki extends \Zotlabs\Web\Controller { } $saved = wiki_save_page(array('resource_id' => $resource_id, 'name' => $pagename, 'content' => $content)); if($saved['success']) { - json_return_and_die(array('success' => true)); + $ob = \App::get_observer(); + $commit = wiki_git_commit(array( + 'commit_msg' => 'Updated ' . $pagename, + 'resource_id' => $resource_id, + 'observer' => $ob, + 'files' => array($pagename) + )); + if($commit['success']) { + json_return_and_die(array('message' => 'Wiki git repo commit made', 'success' => true)); + } else { + json_return_and_die(array('message' => 'Error making git commit','success' => false)); + } } else { - json_return_and_die(array('success' => false)); + json_return_and_die(array('message' => 'Error saving page', 'success' => false)); } } diff --git a/Zotlabs/Storage/GitRepo.php b/Zotlabs/Storage/GitRepo.php index f4a129bb3..306abc0ba 100644 --- a/Zotlabs/Storage/GitRepo.php +++ b/Zotlabs/Storage/GitRepo.php @@ -127,6 +127,15 @@ class GitRepo { $repo['logs'] = $git->log(array('limit' => 50)); return $repo; } + + // Commit changes to the repo. Default is to stage all changes and commit everything. + public function commit($msg, $options = array()) { + try { + return $this->git->commit($msg, $options); + } catch (\PHPGit\Exception\GitException $ex) { + return false; + } + } public static function isValidGitRepoURL($url) { if (validate_url($url) && strrpos(parse_url($url, PHP_URL_PATH), '.')) { diff --git a/include/wiki.php b/include/wiki.php index e01cc12c3..915478da5 100644 --- a/include/wiki.php +++ b/include/wiki.php @@ -251,7 +251,44 @@ function wiki_save_page($arr) { return array('message' => '', 'success' => true); } else { return array('message' => 'Page file not writable', 'success' => false); + } +} + +function wiki_git_commit($arr) { + $files = ((array_key_exists('files', $arr)) ? $arr['files'] : null); + $commit_msg = ((array_key_exists('commit_msg', $arr)) ? $arr['commit_msg'] : 'Repo updated'); + $resource_id = ((array_key_exists('resource_id', $arr)) ? $arr['resource_id'] : json_return_and_die(array('message' => 'Wiki resource_id required for git commit', 'success' => false))); + $observer = ((array_key_exists('observer', $arr)) ? $arr['observer'] : json_return_and_die(array('message' => 'Observer required for git commit', 'success' => false))); + $w = wiki_get_wiki($resource_id); + if (!$w['path']) { + return array('message' => 'Error reading wiki', 'success' => false); } - - -} \ No newline at end of file + $reponame = ((array_key_exists('title', $w['wiki'])) ? $w['wiki']['title'] : 'repo'); + if($reponame === '') { + $reponame = 'repo'; + } + $git = new GitRepo('sys', null, false, $w['wiki']['title'], $w['path']); + try { + $git->setIdentity($observer['xchan_name'], $observer['xchan_addr']); + if ($files === null) { + $options = array('all' => true); // git commit option to include all changes + } else { + $options = array(); // git commit options + foreach ($files as $file) { + if (!$git->git->add($file)) { // add specified files to the git repo stage + if (!$git->git->reset->hard()) { + json_return_and_die(array('message' => 'Error adding file to git stage: ' . $file . '. Error resetting git repo.', 'success' => false)); + } + json_return_and_die(array('message' => 'Error adding file to git stage: ' . $file, 'success' => false)); + } + } + } + if ($git->commit($commit_msg, $options)) { + json_return_and_die(array('message' => 'Wiki repo commit succeeded', 'success' => true)); + } else { + json_return_and_die(array('message' => 'Wiki repo commit failed', 'success' => false)); + } + } catch (\PHPGit\Exception\GitException $e) { + json_return_and_die(array('message' => 'GitRepo error thrown', 'success' => false)); + } +} -- cgit v1.2.3