Skip to content

Commit d3a61cd

Browse files
ochurlaudmhoffrog
authored andcommitted
Allow using several git repos
Fixes #47
1 parent 00885c3 commit d3a61cd

File tree

4 files changed

+59
-12
lines changed

4 files changed

+59
-12
lines changed

action/editcommit.php

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,36 @@ public function register(Doku_Event_Handler $controller) {
3030
$controller->register_hook('DOKUWIKI_DONE', 'AFTER', $this, 'handle_periodic_pull');
3131
}
3232

33-
private function initRepo() {
34-
//get path to the repo root (by default DokuWiki's savedir)
35-
$repoPath = GitBackedUtil::getEffectivePath($this->getConf('repoPath'));
33+
private function initRepo($initRepo=True, $filePath="") {
34+
if($initRepo) {
35+
$repoPath = GitBackedUtil::getEffectivePath($this->getConf('repoPath'));
36+
} else {
37+
$repoPath = dirname($filePath);
38+
}
39+
//set the path to the git binary
3640
$gitPath = trim($this->getConf('gitPath'));
3741
if ($gitPath !== '') {
3842
Git::set_bin($gitPath);
3943
}
40-
//init the repo and create a new one if it is not present
41-
io_mkdir_p($repoPath);
42-
$repo = new GitRepo($repoPath, $this, true, true);
44+
if ($initRepo) {
45+
//init the repo and create a new one if it is not present
46+
io_mkdir_p($repoPath);
47+
$repo = new GitRepo($repoPath, $this, true, true);
48+
} else {
49+
new GitRepo($repoPath, $this, false, false);
50+
}
4351
//set git working directory (by default DokuWiki's savedir)
44-
$repoWorkDir = $this->getConf('repoWorkDir');
45-
if (!empty($repoWorkDir)) {
46-
$repoWorkDir = GitBackedUtil::getEffectivePath($repoWorkDir);
47-
}
52+
if ($initRepo) {
53+
$repoWorkDir = $this->getConf('repoWorkDir');
54+
if (!empty($repoWorkDir)) {
55+
$repoWorkDir = GitBackedUtil::getEffectivePath($repoWorkDir);
56+
}
57+
} else {
58+
$repoWorkDir = "";
59+
}
60+
4861
Git::set_bin(empty($repoWorkDir) ? Git::get_bin() : Git::get_bin().' --work-tree '.escapeshellarg($repoWorkDir));
62+
4963
$params = str_replace(
5064
array('%mail%','%user%'),
5165
array($this->getAuthorMail(),$this->getAuthor()),
@@ -73,7 +87,12 @@ private function isIgnored($filePath) {
7387
private function commitFile($filePath,$message) {
7488
if (!$this->isIgnored($filePath)) {
7589
try {
76-
$repo = $this->initRepo();
90+
$initRepo = $this->getConf('initRepo');
91+
if ($initRepo) {
92+
$repo = $this->initRepo();
93+
} else {
94+
$repo = $this->initRepo($initRepo, $filePath);
95+
}
7796

7897
//add the changed file and set the commit message
7998
$repo->add($filePath);

conf/default.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* @author Wolfgang Gassler <[email protected]>
66
*/
77

8+
$conf['initRepo'] = 1;
89
$conf['pushAfterCommit'] = 0;
910
$conf['periodicPull'] = 0;
1011
$conf['periodicMinutes'] = 60;

conf/metadata.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* @author Wolfgang Gassler <[email protected]>
66
*/
77

8+
$meta['initRepo'] = array('onoff');
89
$meta['pushAfterCommit'] = array('onoff');
910
$meta['periodicPull'] = array('onoff');
1011
$meta['periodicMinutes'] = array('numeric');

lib/Git.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,11 @@ public function set_repo_path($repo_path, $create_new = false, $_init = true) {
212212
if ($new_path = realpath($repo_path)) {
213213
$repo_path = $new_path;
214214
if (is_dir($repo_path)) {
215+
if ($this->is_in_git_repo($repo_path) {
216+
$this->repo_path = $repo_path;
217+
$this->bare = false;
215218
// Is this a work tree?
216-
if (file_exists($repo_path."/.git") && is_dir($repo_path."/.git")) {
219+
} else if (file_exists($repo_path."/.git") && is_dir($repo_path."/.git")) {
217220
$this->repo_path = $repo_path;
218221
$this->bare = false;
219222
// Is this a bare repo?
@@ -286,6 +289,29 @@ public function test_git() {
286289
return ($status != 127);
287290
}
288291

292+
/**
293+
* Tests if we are in a git repository
294+
*
295+
* @access public
296+
* @return bool
297+
*/
298+
public function is_in_git_repo($path) {
299+
$descriptorspec = array(
300+
1 => array('pipe', 'w'),
301+
2 => array('pipe', 'w'),
302+
);
303+
$pipes = array();
304+
$resource = proc_open(Git::get_bin() + ' rev-parse --is-inside-work-tree', $descriptorspec, $pipes, $path);
305+
$stdout = stream_get_contents($pipes[1]);
306+
$stderr = stream_get_contents($pipes[2]);
307+
foreach ($pipes as $pipe) {
308+
fclose($pipe);
309+
}
310+
311+
$status = trim(proc_close($resource));
312+
return ($status == 0);
313+
}
314+
289315
/**
290316
* Run a command in the git repository
291317
*

0 commit comments

Comments
 (0)