X-Git-Url: http://git.pk910.de/?p=GITManagedWebpage.git;a=blobdiff_plain;f=GITManagedWebpage.class.php;h=3f6ba4aae7772b0ee2d943f53ea4eaa9d57daf94;hp=305530b1ba1195254bd2cd8c266a25ec3ce5ccfe;hb=05088f3fd28b5492259807cd06637980b5ea49b7;hpb=b71fd6d276e1b3f58903307ec6d3a0f7cd15538c diff --git a/GITManagedWebpage.class.php b/GITManagedWebpage.class.php index 305530b..3f6ba4a 100644 --- a/GITManagedWebpage.class.php +++ b/GITManagedWebpage.class.php @@ -33,6 +33,7 @@ class GITManagedWebpage { private $loopedcall = false; private $config = null; private $config_changed = true; + private $activeSession = null; public function __construct($giturl, $workdir = null, $localdir = null) { if(session_status() != PHP_SESSION_ACTIVE) { @@ -89,7 +90,7 @@ class GITManagedWebpage { } else $argstr .= " ".escapeshellarg($arg); } - $gitcmd = 'git '.escapeshellarg('--git-dir='.$this->workdir.'repository/.git').$argstr; + $gitcmd = 'git '.escapeshellarg('--git-dir='.$this->workdir.'repository/.git').' '.escapeshellarg('--work-tree='.$this->workdir.'repository').$argstr; $output = shell_exec($gitcmd); return $output; } @@ -102,6 +103,16 @@ class GITManagedWebpage { $this->config_changed = true; } + private function checkConfigIntegrity() { + foreach($this->config as $key => $value) { + if(substr($key, 0, strlen('branch_')) == 'branch_') { + if(!file_exists($this->workdir.$key)) { + unset($this->config['key']); + } + } + } + } + /* private function getConfig($name) * get an option from the configuration */ @@ -112,6 +123,7 @@ class GITManagedWebpage { if(file_exists($this->workdir."config.txt")) { $config_txt = @file_get_contents($this->workdir."config.txt"); $this->config = unserialize($config_txt); + $this->checkConfigIntegrity(); } else { $this->config = array(); return null; @@ -152,6 +164,14 @@ class GITManagedWebpage { $this->error(self::ERROR_CRITICAL, "error cloning git repository."); return; } + + $fp = fopen($this->workdir.'.htaccess'); + fwrite($fp, ' +Order deny,allow +Deny from all +'); + fclose($fp); + $this->ready = true; $default_branch = str_replace(array("\r", "\n"), array("", ""), $this->gitcmd("rev-parse", "--abbrev-ref", "HEAD")); @@ -217,7 +237,7 @@ class GITManagedWebpage { if($branch == $default_branch) $dir = $this->localdir; else - $dir = $this->workdir.'branch_'.$branch.'/'; + $dir = $this->workdir.'branch_'.str_replace(array('/'), array('_'), $branch).'/'; if(file_exists($dir)) return $dir; else if($create) { @@ -227,24 +247,29 @@ class GITManagedWebpage { return false; } - private function updateBranch($branch, $path) { + private function updateBranch($branch, $path, $force = false) { if(substr($path, -1) != '/') $path .= '/'; $current_branch = str_replace(array("\r", "\n"), array("", ""), $this->gitcmd("rev-parse", "--abbrev-ref", "HEAD")); - if($current_branch != $branch) - $this->gitcmd("checkout", $branch); - $this->gitcmd("pull"); + $this->gitcmd("fetch"); + $this->gitcmd("reset", "--hard", "origin/".$branch); $gitret = $this->gitcmd("rev-list", "--max-count=1", $branch); preg_match("#([a-z0-9]{40})#", $gitret, $match); $newest_version = $match[1]; + $deleted_files = array(); if(($current_version = $this->getConfig('version_'.$branch))) { - if($current_version == $newest_version) + if($current_version == $newest_version && !$force) return; else { - //applying patch - $difftxt = $this->gitcmd("diff", $current_version, $newest_version); - echo $difftxt; + $override_all = true; + $delfiles = $this->gitcmd("diff", "--diff-filter=D", "--name-only", $current_version, $newest_version); + $delfiles = explode("\n", str_replace(array("\r"), array(""), $delfiles)); + foreach($delfiles as $file) { + if(!$file) + continue; + $deleted_files[] = $file; + } } } else $override_all = true; @@ -254,9 +279,14 @@ class GITManagedWebpage { shell_exec('rsync -avz --exclude ".git" '.escapeshellarg($this->workdir."repository/").' '.escapeshellarg($path)); else shell_exec('tar -c --exclude ".git" -C '.escapeshellarg($this->workdir."repository").' . | tar -x -C '.escapeshellarg($path)); + + // remove deleted files + foreach($deleted_files as $file) { + unlink($path.$file); + } } $this->setConfig('version_'.$branch, $newest_version); - //$this->saveConfig(); + $this->saveConfig(); } /* public function update() @@ -278,17 +308,20 @@ class GITManagedWebpage { if($this->loopedcall) return; - if(!$this->branchExists($branch)) - return false; + if(!$this->branchExists($branch)) { + $this->gitcmd("fetch"); + if(!$this->branchExists('origin/'.$branch)) + return false; + } $this->setActiveBranch($branch, $remember); if(!$this->localBranchPath($branch)) { $dir = $this->localBranchPath($branch, true); - $this->updateBranch($branch, $dir); + $this->updateBranch($branch, $dir, true); } } - public function execute($file = null) { + public function getExecFile($file = null) { if($this->loopedcall) return; define("GITMANAGED_EXECUTED", true); @@ -301,13 +334,14 @@ class GITManagedWebpage { $default_branch = $this->getConfig("defaultbranch"); $active_branch = $this->getActiveBranch(); if($active_branch != $default_branch) { - if(!($dir = $this->localBranchPath($branch))) { + if(!($dir = $this->localBranchPath($active_branch))) { $dir = $this->localBranchPath($active_branch, true); - $this->updateBranch($active_branch, $dir); + $this->updateBranch($active_branch, $dir, true); } - include_once($dir.$file); + chdir($dir); + return $dir.$file; } else { - include_once($this->localdir.$file); + return $file; } }