Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using several git repos #65

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions action/editcommit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
ochurlaud marked this conversation as resolved.
Show resolved Hide resolved
}
} 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);
ochurlaud marked this conversation as resolved.
Show resolved Hide resolved
}
//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(
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions conf/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @author Wolfgang Gassler <[email protected]>
*/

$conf['initRepo'] = 1;
$conf['pushAfterCommit'] = 0;
$conf['periodicPull'] = 0;
$conf['periodicMinutes'] = 60;
Expand Down
1 change: 1 addition & 0 deletions conf/metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @author Wolfgang Gassler <[email protected]>
*/

$meta['initRepo'] = array('onoff');
ochurlaud marked this conversation as resolved.
Show resolved Hide resolved
$meta['pushAfterCommit'] = array('onoff');
$meta['periodicPull'] = array('onoff');
ochurlaud marked this conversation as resolved.
Show resolved Hide resolved
$meta['periodicMinutes'] = array('numeric');
Expand Down
30 changes: 28 additions & 2 deletions lib/Git.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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
*
Expand Down