diff --git a/action/editcommit.php b/action/editcommit.php index 9990936..ef10034 100644 --- a/action/editcommit.php +++ b/action/editcommit.php @@ -32,23 +32,36 @@ public function register(Doku_Event_Handler $controller) { $controller->register_hook('DOKUWIKI_DONE', 'AFTER', $this, 'handle_periodic_pull'); } - private function initRepo() { - //get path to the repo root (by default DokuWiki's savedir) - if(defined('DOKU_FARM')) { - $repoPath = $this->getConf('repoPath'); - } else { - $repoPath = DOKU_INC.$this->getConf('repoPath'); - } + private function initRepo($initRepo=True, $filePath="") { + if($initRepo) { + //get path to the repo root (by default DokuWiki's savedir) + if(defined('DOKU_FARM')) { + $repoPath = $this->getConf('repoPath'); + } else { + $repoPath = DOKU_INC.$this->getConf('repoPath'); + } + } else { + $repoPath = dirname($filePath); + } //set the path to the git binary $gitPath = trim($this->getConf('gitPath')); if ($gitPath !== '') { Git::set_bin($gitPath); } - //init the repo and create a new one if it is not present - io_mkdir_p($repoPath); - $repo = new GitRepo($repoPath, $this, true, true); + if ($initRepo) { + //init the repo and create a new one if it is not present + io_mkdir_p($repoPath); + $repo = new GitRepo($repoPath, $this, true, true); + } else { + new GitRepo($repoPath, $this, false, false); + } //set git working directory (by default DokuWiki's savedir) - $repoWorkDir = DOKU_INC.$this->getConf('repoWorkDir'); + if ($initRepo) { + $repoWorkDir = DOKU_INC.$this->getConf('repoWorkDir'); + } else { + $repoWorkDir = $repoPath; + } + Git::set_bin(Git::get_bin().' --work-tree '.escapeshellarg($repoWorkDir)); $params = str_replace( @@ -78,7 +91,12 @@ private function isIgnored($filePath) { private function commitFile($filePath,$message) { if (!$this->isIgnored($filePath)) { try { - $repo = $this->initRepo(); + $initRepo = $this->getConf('initRepo'); + if ($initRepo) { + $repo = $this->initRepo(); + } else { + $repo = $this->initRepo($initRepo, $filePath); + } //add the changed file and set the commit message $repo->add($filePath); diff --git a/conf/default.php b/conf/default.php index 8b642e3..e11adde 100644 --- a/conf/default.php +++ b/conf/default.php @@ -5,6 +5,7 @@ * @author Wolfgang Gassler */ +$conf['initRepo'] = 1; $conf['pushAfterCommit'] = 0; $conf['periodicPull'] = 0; $conf['periodicMinutes'] = 60; diff --git a/conf/metadata.php b/conf/metadata.php index 8ff5d3a..865186a 100644 --- a/conf/metadata.php +++ b/conf/metadata.php @@ -5,6 +5,7 @@ * @author Wolfgang Gassler */ +$meta['initRepo'] = array('onoff'); $meta['pushAfterCommit'] = array('onoff'); $meta['periodicPull'] = array('onoff'); $meta['periodicMinutes'] = array('numeric'); diff --git a/lib/Git.php b/lib/Git.php index d09535f..0ef29dc 100644 --- a/lib/Git.php +++ b/lib/Git.php @@ -138,7 +138,7 @@ class GitRepo { protected $repo_path = null; protected $bare = false; protected $envopts = array(); - protected ?\action_plugin_gitbacked_editcommit $plugin = null; + protected \action_plugin_gitbacked_editcommit $plugin = null; /** * Create a new git repository @@ -210,8 +210,11 @@ public function set_repo_path($repo_path, $create_new = false, $_init = true) { if ($new_path = realpath($repo_path)) { $repo_path = $new_path; if (is_dir($repo_path)) { + if ($this->is_in_git_repo($repo_path) { + $this->repo_path = $repo_path; + $this->bare = false; // Is this a work tree? - if (file_exists($repo_path."/.git") && is_dir($repo_path."/.git")) { + } else if (file_exists($repo_path."/.git") && is_dir($repo_path."/.git")) { $this->repo_path = $repo_path; $this->bare = false; // Is this a bare repo? @@ -284,6 +287,29 @@ public function test_git() { return ($status != 127); } + /** + * Tests if we are in a git repository + * + * @access public + * @return bool + */ + public function is_in_git_repo($path) { + $descriptorspec = array( + 1 => array('pipe', 'w'), + 2 => array('pipe', 'w'), + ); + $pipes = array(); + $resource = proc_open(Git::get_bin() + ' rev-parse --is-inside-work-tree', $descriptorspec, $pipes, $path); + $stdout = stream_get_contents($pipes[1]); + $stderr = stream_get_contents($pipes[2]); + foreach ($pipes as $pipe) { + fclose($pipe); + } + + $status = trim(proc_close($resource)); + return ($status == 0); + } + /** * Run a command in the git repository *