Skip to content

Commit dbee1e7

Browse files
committed
Allow using several git repos
- editcommit.php, Git.php: - fixed issue for non git repo related paths in case of auto determining repos
1 parent 718bdcd commit dbee1e7

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

action/editcommit.php

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

33+
/**
34+
* Create a GitRepo class instance according to this plugins config.
35+
* If auto determination of git rpos is configured, this method will return null,
36+
* if there is no git repo found.
37+
*
38+
* @access private
39+
* @param string path to the file or directory to be commited (required for auto determination only)
40+
* @return GitRepo instance or null if there is no repo related to fileOrDirPath
41+
*/
3342
private function initRepo($fileOrDirPath="") {
3443
//set the path to the git binary
3544
$gitPath = trim($this->getConf('gitPath'));
@@ -39,22 +48,24 @@ private function initRepo($fileOrDirPath="") {
3948

4049
$isAutoDetermineRepos = $this->getConf('autoDetermineRepos');
4150
if ($isAutoDetermineRepos) {
51+
if (empty($fileOrDirPath)) {
52+
return null;
53+
}
4254
$repoPath = is_dir($fileOrDirPath) ? $fileOrDirPath : dirname($fileOrDirPath);
4355
$repo = new GitRepo($repoPath, $this, false, false);
4456
$repoPath = $repo->get_repo_path();
57+
if (empty($repoPath)) {
58+
return null;
59+
}
4560
$repoWorkDir = '';
4661
} else {
4762
//get path to the repo root (by default DokuWiki's savedir)
4863
$repoPath = GitBackedUtil::getEffectivePath($this->getConf('repoPath'));
4964
//init the repo and create a new one if it is not present
5065
io_mkdir_p($repoPath);
5166
$repo = new GitRepo($repoPath, $this, true, true);
52-
}
53-
//set git working directory (by default DokuWiki's savedir)
54-
if ($isAutoDetermineRepos) {
55-
$repoWorkDir = '';
56-
} else {
57-
$repoWorkDir = $this->getConf('repoWorkDir');
67+
//set git working directory (by default DokuWiki's savedir)
68+
$repoWorkDir = trim($this->getConf('repoWorkDir'));
5869
if (!empty($repoWorkDir)) {
5970
$repoWorkDir = GitBackedUtil::getEffectivePath($repoWorkDir);
6071
}
@@ -90,7 +101,9 @@ private function commitFile($filePath,$message) {
90101
if (!$this->isIgnored($filePath)) {
91102
try {
92103
$repo = $this->initRepo($filePath);
93-
104+
if (is_null($repo)) {
105+
return;
106+
}
94107
//add the changed file and set the commit message
95108
$repo->add($filePath);
96109
$repo->commit($message);
@@ -131,17 +144,19 @@ public function handle_periodic_pull(Doku_Event &$event, $param) {
131144

132145
//if it is time to run a pull request
133146
if ($lastPull+$timeToWait < $now) {
134-
try {
135-
$repo = $this->initRepo();
136-
137-
//execute the pull request
138-
$repo->pull('origin',$repo->active_branch());
139-
} catch (Exception $e) {
140-
if (!$this->isNotifyByEmailOnGitCommandError()) {
141-
throw new Exception('Git command failed to perform periodic pull: '.$e->getMessage(), 2, $e);
142-
}
143-
return;
144-
}
147+
try {
148+
$repo = $this->initRepo();
149+
if (is_null($repo)) {
150+
return;
151+
}
152+
//execute the pull request
153+
$repo->pull('origin',$repo->active_branch());
154+
} catch (Exception $e) {
155+
if (!$this->isNotifyByEmailOnGitCommandError()) {
156+
throw new Exception('Git command failed to perform periodic pull: '.$e->getMessage(), 2, $e);
157+
}
158+
return;
159+
}
145160

146161
//save the current time to the file to track the last pull execution
147162
file_put_contents($lastPullFile,serialize(time()));

lib/Git.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ public function set_repo_path($repo_path, $create_new = false, $_init = true) {
233233
if ($_init) {
234234
$this->run('init');
235235
}
236+
} if (!$_init) {
237+
// If we do not have to init the repo, we just reflect that there is no repo path yet.
238+
// This may be the case for auto determining repos, if there is no repo related to the current resource going to be commited.
239+
$this->repo_path = '';
236240
} else {
237241
throw new Exception($this->handle_repo_path_error($repo_path, '"'.$repo_path.'" is not a git repository'));
238242
}
@@ -344,6 +348,10 @@ public function absolute_git_dir($path) {
344348
* @return string or null in case of an error
345349
*/
346350
protected function run_command($command) {
351+
//dbglog("Git->run_command: repo_path=[".$this->repo_path."])");
352+
if (empty($this->repo_path)) {
353+
throw new Exception($this->handle_repo_path_error($this->repo_path, "Failure on GitRepo->run_command(): Git command must not be run for an empty repo path"));
354+
}
347355
//dbglog("Git->run_command(command=[".$command."])");
348356
$descriptorspec = array(
349357
1 => array('pipe', 'w'),
@@ -368,9 +376,7 @@ protected function run_command($command) {
368376
} else {
369377
$env = array_merge($_ENV, $this->envopts);
370378
}
371-
$cwd = $this->repo_path;
372-
//dbglog("GitBacked - cwd: [".$cwd."]");
373-
$resource = proc_open($command, $descriptorspec, $pipes, $cwd, $env);
379+
$resource = proc_open($command, $descriptorspec, $pipes, $this->repo_path, $env);
374380

375381
$stdout = stream_get_contents($pipes[1]);
376382
$stderr = stream_get_contents($pipes[2]);

0 commit comments

Comments
 (0)